Skip to content

Commit 94c0d4e

Browse files
authored
152 fuzzing constants (#153)
It is easy to generate a graph that results in out-of-memory if we let types with arbitrary sizes. Limiting constants are needed to avoid out-of-memory at the start of fuzzing, so the fuzzer can reach meaningful coverage. Resolves #152
1 parent dfd8f9e commit 94c0d4e

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

ciphercore-base/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bincode = "1.3.3"
3535
[features]
3636
default = []
3737
nightly-features = []
38+
fuzzing = []
3839

3940
[[bin]]
4041
name = "ciphercore_compile"

ciphercore-base/src/constants.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
pub(super) const MAX_TOTAL_SIZE_NODES: u64 = u64::MAX - 1;
2-
pub(super) const MAX_INDIVIDUAL_NODE_SIZE: u64 = u64::MAX - 1;
3-
pub(super) const TYPES_VECTOR_LENGTH_LIMIT: usize = usize::MAX - 1;
4-
pub(super) const TYPE_MEMORY_OVERHEAD: u64 = 1;
5-
pub(super) const NON_STANDARD_SCALAR_LEN_SUPPORT: bool = false;
1+
#[cfg(feature = "fuzzing")]
2+
pub mod type_size_limit_constants {
3+
pub const MAX_TOTAL_SIZE_NODES: u64 = 10000;
4+
pub const MAX_INDIVIDUAL_NODE_SIZE: u64 = 1000;
5+
pub const TYPES_VECTOR_LENGTH_LIMIT: usize = 1000;
6+
pub const TYPE_MEMORY_OVERHEAD: u64 = 1;
7+
pub const NON_STANDARD_SCALAR_LEN_SUPPORT: bool = false;
8+
}
9+
#[cfg(not(feature = "fuzzing"))]
10+
pub mod type_size_limit_constants {
11+
pub const MAX_TOTAL_SIZE_NODES: u64 = u64::MAX - 1;
12+
pub const MAX_INDIVIDUAL_NODE_SIZE: u64 = u64::MAX - 1;
13+
pub const TYPES_VECTOR_LENGTH_LIMIT: usize = usize::MAX - 1;
14+
pub const TYPE_MEMORY_OVERHEAD: u64 = 1;
15+
pub const NON_STANDARD_SCALAR_LEN_SUPPORT: bool = false;
16+
}

ciphercore-base/src/data_types.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Types used within CipherCore and related functions.
2-
use crate::constants;
2+
use crate::constants::type_size_limit_constants;
33
use crate::errors::CiphercoreBaseError;
44
use crate::errors::Result;
55
use serde::{Deserialize, Serialize};
@@ -133,8 +133,8 @@ impl ScalarType {
133133
if let Some(m) = self.modulus {
134134
//Currently our evaluator only supports bit_size = 1,8,16,32,64
135135
let supported_modulus = vec![TWO, TWO.pow(8), TWO.pow(16), TWO.pow(32)];
136-
let supported =
137-
constants::NON_STANDARD_SCALAR_LEN_SUPPORT || supported_modulus.contains(&m);
136+
let supported = type_size_limit_constants::NON_STANDARD_SCALAR_LEN_SUPPORT
137+
|| supported_modulus.contains(&m);
138138
supported && (m > 2 || (m == 2 && !self.signed))
139139
} else {
140140
true
@@ -1202,7 +1202,7 @@ pub(super) fn get_size_estimation_in_bits(t: Type) -> Result<u64> {
12021202
}
12031203
};
12041204
result
1205-
.checked_add(constants::TYPE_MEMORY_OVERHEAD)
1205+
.checked_add(type_size_limit_constants::TYPE_MEMORY_OVERHEAD)
12061206
.ok_or_else(|| runtime_error!("add overflow!"))
12071207
}
12081208

@@ -1253,7 +1253,7 @@ pub(super) fn get_size_estimation_in_bits(t: Type) -> Result<u64> {
12531253
pub fn get_types_vector(t: Type) -> Result<Vec<TypePointer>> {
12541254
match t {
12551255
Type::Vector(length, element_type) => {
1256-
if length > constants::TYPES_VECTOR_LENGTH_LIMIT as u64 {
1256+
if length > type_size_limit_constants::TYPES_VECTOR_LENGTH_LIMIT as u64 {
12571257
return Err(runtime_error!(
12581258
"Vector length is greater than TYPES_VECTOR_LENGTH_LIMIT!"
12591259
));
@@ -1266,7 +1266,7 @@ pub fn get_types_vector(t: Type) -> Result<Vec<TypePointer>> {
12661266
}
12671267
Type::Tuple(types) => {
12681268
let length = types.len();
1269-
if length > constants::TYPES_VECTOR_LENGTH_LIMIT {
1269+
if length > type_size_limit_constants::TYPES_VECTOR_LENGTH_LIMIT {
12701270
return Err(runtime_error!(
12711271
"Tuple length is greater than TYPES_VECTOR_LENGTH_LIMIT!"
12721272
));
@@ -1279,7 +1279,7 @@ pub fn get_types_vector(t: Type) -> Result<Vec<TypePointer>> {
12791279
}
12801280
Type::NamedTuple(names_types) => {
12811281
let length = names_types.len();
1282-
if length > constants::TYPES_VECTOR_LENGTH_LIMIT {
1282+
if length > type_size_limit_constants::TYPES_VECTOR_LENGTH_LIMIT {
12831283
return Err(runtime_error!(
12841284
"NamedTuple length is greater than TYPES_VECTOR_LENGTH_LIMIT!"
12851285
));

ciphercore-base/src/data_values.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ impl ToNdarray<i64> for Value {
13161316
#[cfg(test)]
13171317
mod tests {
13181318
use super::*;
1319-
use crate::constants;
1319+
use crate::constants::type_size_limit_constants;
13201320
use crate::data_types::{
13211321
array_type, create_scalar_type, named_tuple_type, scalar_type, tuple_type, vector_type,
13221322
BIT, INT32, INT64, INT8, UINT16, UINT32, UINT64, UINT8,
@@ -1362,7 +1362,7 @@ mod tests {
13621362
check_type_test_worker_fail(&v, tuple_type(vec![]));
13631363
let v = Value::from_bytes(vec![0, 0, 0]);
13641364
check_type_test_worker_fail(&v, tuple_type(vec![]));
1365-
if constants::NON_STANDARD_SCALAR_LEN_SUPPORT {
1365+
if type_size_limit_constants::NON_STANDARD_SCALAR_LEN_SUPPORT {
13661366
let v = Value::from_bytes(vec![0]);
13671367
check_type_test_worker(&v, scalar_type(create_scalar_type(false, Some(253))));
13681368
check_type_test_worker(&v, scalar_type(create_scalar_type(false, Some(254))));
@@ -1699,7 +1699,7 @@ mod tests {
16991699
let result = get_types_vector(t);
17001700
assert!(result.is_ok());
17011701
let t = vector_type(
1702-
constants::TYPES_VECTOR_LENGTH_LIMIT as u64 + 1,
1702+
type_size_limit_constants::TYPES_VECTOR_LENGTH_LIMIT as u64 + 1,
17031703
scalar_type(UINT16),
17041704
);
17051705
let result = get_types_vector(t);

ciphercore-base/src/graphs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::Weak;
1010

1111
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1212

13-
use crate::constants;
13+
use crate::constants::type_size_limit_constants;
1414
use crate::custom_ops::CustomOperation;
1515
use crate::data_types::{get_size_estimation_in_bits, ArrayShape, ScalarType, Type};
1616
use crate::data_values::Value;
@@ -483,7 +483,7 @@ impl Graph {
483483
self.remove_last_node(result)?;
484484
return Err(runtime_error!("Trying to add a node with invalid size"));
485485
}
486-
if size_estimate? > constants::MAX_INDIVIDUAL_NODE_SIZE {
486+
if size_estimate? > type_size_limit_constants::MAX_INDIVIDUAL_NODE_SIZE {
487487
self.remove_last_node(result)?;
488488
return Err(runtime_error!(
489489
"Trying to add a node larger than MAX_INDIVIDUAL_NODE_SIZE"
@@ -928,7 +928,7 @@ impl Graph {
928928
"Trying to add a reshape node with invalid type size"
929929
));
930930
}
931-
if size_estimate? > constants::MAX_INDIVIDUAL_NODE_SIZE {
931+
if size_estimate? > type_size_limit_constants::MAX_INDIVIDUAL_NODE_SIZE {
932932
return Err(runtime_error!(
933933
"Trying to add a reshape node larger than MAX_INDIVIDUAL_NODE_SIZE"
934934
));
@@ -2219,7 +2219,7 @@ impl Context {
22192219
.get_total_size_nodes()
22202220
.checked_add(get_size_estimation_in_bits(node_type)?)
22212221
.ok_or_else(|| runtime_error!("add overflow!"))?;
2222-
if new_total_size > constants::MAX_TOTAL_SIZE_NODES {
2222+
if new_total_size > type_size_limit_constants::MAX_TOTAL_SIZE_NODES {
22232223
return Err(runtime_error!(
22242224
"Can't add a node: total size of nodes exceeds MAX_TOTAL_SIZE_NODES"
22252225
));

ciphercore-base/src/type_inference.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ mod tests {
17571757
assert!(e.is_err());
17581758
}
17591759

1760-
use crate::constants;
1760+
use crate::constants::type_size_limit_constants;
17611761
#[test]
17621762
fn test_b2a() {
17631763
test_b2a_worker(
@@ -1770,7 +1770,7 @@ mod tests {
17701770
test_b2a_worker_fail(array_type(vec![10, 20, 1], BIT), BIT);
17711771
test_b2a_worker_fail(array_type(vec![10, 20, 1], INT32), INT32);
17721772
test_b2a_worker_fail(array_type(vec![10, 40], BIT), INT32);
1773-
if constants::NON_STANDARD_SCALAR_LEN_SUPPORT {
1773+
if type_size_limit_constants::NON_STANDARD_SCALAR_LEN_SUPPORT {
17741774
let t = create_scalar_type(false, Some(126));
17751775
test_b2a_worker(array_type(vec![7], BIT), t.clone(), scalar_type(t.clone()));
17761776
test_b2a_worker(

0 commit comments

Comments
 (0)