Skip to content

Commit 2d5b2cc

Browse files
Juan-M-VJuanmapefontana
authored
Add abitrary fuzzing (#1306)
* Create fuzzer * Remove cli * Update fuzzer * Change fuzzer name * Add arbitrary * Add fuzzer using arbitrary * Add fuzzer * Use run until steps * Remove end * Update changelog * Update CHANGELOG.md * Update CHANGELOG.md * Change macro cfg * Update bincode * Update Arbitrary deps * Check for std with arbitrary * Update Cargo.toml * Revert "Update bincode" This reverts commit 6b5a95a. * Revert to update bincode * Fix arbitrary imports * Run linter * Run linter --------- Co-authored-by: Juanma <[email protected]> Co-authored-by: Pedro Fontana <[email protected]>
1 parent b7ac1e8 commit 2d5b2cc

File tree

16 files changed

+163
-4
lines changed

16 files changed

+163
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
#### Upcoming Changes
44

5+
* feat: add `arbitrary` feature to enable arbitrary derive in `Program` and `CairoRunConfig`
6+
57
* perf: remove pointless iterator from rc limits tracking [#1316](https://github.com/lambdaclass/cairo-vm/pull/1316)
68

9+
710
#### [0.8.2] - 2023-7-10
811

912
* chore: update dependencies, particularly lamdaworks 0.1.2 -> 0.1.3 [#1323](https://github.com/lambdaclass/cairo-vm/pull/1323)

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@ cairo-lang-casm = { version = "2.0.0", default-features = false }
6161
ark-ff = { version = "0.4.2", default-features = false }
6262
ark-std = { version = "0.4.0", default-features = false }
6363

64+
# For fuzzing
65+
arbitrary = { version = "1.3.0", features = ["derive"] }
66+
6467
[profile.release]
6568
lto = "fat"

felt/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ default = ["std"]
1212
std = []
1313
alloc = []
1414
lambdaworks-felt = ["dep:lambdaworks-math"]
15+
arbitrary = ["dep:arbitrary", "num-bigint/arbitrary"]
1516

1617
[dependencies]
1718
num-integer = { version = "0.1.45", default-features = false }
@@ -22,6 +23,7 @@ lazy_static = { version = "1.4.0", default-features = false, features = [
2223
] }
2324
serde = { version = "1.0", features = ["derive"], default-features = false }
2425
lambdaworks-math = { version = "0.1.2", default-features = false, optional = true }
26+
arbitrary = { version = "1.3.0", features = ["derive"], optional = true }
2527

2628
[dev-dependencies]
2729
proptest = "1.2.0"

felt/src/bigint_felt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use core::{
1414

1515
use crate::{lib_bigint_felt::FeltOps, ParseFeltError};
1616

17+
#[cfg(all(feature = "std", feature = "arbitrary"))]
18+
use arbitrary::Arbitrary;
19+
1720
pub const FIELD_HIGH: u128 = (1 << 123) + (17 << 64); // this is equal to 10633823966279327296825105735305134080
1821
pub const FIELD_LOW: u128 = 1;
1922
use lazy_static::lazy_static;
@@ -31,6 +34,7 @@ lazy_static! {
3134
.expect("Conversion BigUint -> BigInt can't fail");
3235
}
3336

37+
#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))]
3438
#[derive(Eq, Hash, PartialEq, PartialOrd, Ord, Clone, Deserialize, Default, Serialize)]
3539
pub(crate) struct FeltBigInt<const PH: u128, const PL: u128> {
3640
val: BigUint,

felt/src/lib_bigint_felt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use core::{
1919
#[cfg(all(not(feature = "std"), feature = "alloc"))]
2020
use alloc::{string::String, vec::Vec};
2121

22+
#[cfg(all(feature = "arbitrary", feature = "std"))]
23+
use arbitrary::Arbitrary;
24+
2225
pub(crate) trait FeltOps {
2326
fn new<T: Into<FeltBigInt<FIELD_HIGH, FIELD_LOW>>>(value: T) -> Self;
2427

@@ -64,6 +67,7 @@ macro_rules! felt_str {
6467
};
6568
}
6669

70+
#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))]
6771
#[derive(Eq, Hash, PartialEq, PartialOrd, Ord, Clone, Deserialize, Default, Serialize)]
6872
pub struct Felt252 {
6973
pub(crate) value: FeltBigInt<FIELD_HIGH, FIELD_LOW>,

felt/src/lib_lambdaworks.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ use serde::{Deserialize, Serialize};
2222
#[cfg(all(not(feature = "std"), feature = "alloc"))]
2323
use alloc::{string::String, vec::Vec};
2424

25+
#[cfg(feature = "arbitrary")]
26+
use arbitrary::{Arbitrary, Unstructured};
27+
2528
use crate::{ParseFeltError, FIELD_HIGH, FIELD_LOW};
2629

30+
#[cfg(feature = "arbitrary")]
31+
impl<'a> Arbitrary<'a> for Felt252 {
32+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
33+
let num: BigUint = BigUint::arbitrary(u)?;
34+
Ok(Felt252::from(num))
35+
}
36+
}
37+
2738
lazy_static! {
2839
pub static ref CAIRO_PRIME_BIGUINT: BigUint =
2940
(Into::<BigUint>::into(FIELD_HIGH) << 128) + Into::<BigUint>::into(FIELD_LOW);

fuzzer/Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

8+
[workspace]
9+
members = ["."]
10+
811
[dependencies]
912
arbitrary = { version = "1.3.0", features = ["derive"] }
1013
honggfuzz = "0.5.55"
1114
bincode = { version = "2.0.0-rc.3", tag = "v2.0.0-rc.3", git = "https://github.com/bincode-org/bincode.git" }
12-
cairo-vm = { path = "../vm" }
15+
cairo-vm = { path = "../vm", features = ["arbitrary"] }
1316
mimalloc = { version = "0.1.29", default-features = false, optional = true }
1417
nom = "7"
1518
thiserror = { version = "1.0.32" }
@@ -18,13 +21,14 @@ thiserror = { version = "1.0.32" }
1821
assert_matches = "1.5.0"
1922
rstest = "0.17.0"
2023

21-
[workspace]
22-
members = ["."]
23-
2424
[features]
2525
default = ["with_mimalloc"]
2626
with_mimalloc = ["cairo-vm/with_mimalloc", "mimalloc"]
2727

2828
[[bin]]
2929
name = "fuzz_json"
3030
path = "src/fuzz_json.rs"
31+
32+
[[bin]]
33+
name = "fuzz_program"
34+
path = "src/fuzz_program.rs"

fuzzer/src/fuzz_program.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use cairo_vm::{
2+
cairo_run::{cairo_run_parsed_program, CairoRunConfig},
3+
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
4+
types::program::Program,
5+
};
6+
use honggfuzz::fuzz;
7+
8+
const STEPS_LIMIT: usize = 1000000;
9+
fn main() {
10+
loop {
11+
fuzz!(|data: (CairoRunConfig, Program)| {
12+
let (cairo_config, program) = data;
13+
let _ = cairo_run_parsed_program(
14+
program.clone(),
15+
&CairoRunConfig::default(),
16+
&mut BuiltinHintProcessor::new_empty(),
17+
STEPS_LIMIT,
18+
);
19+
let _ = cairo_run_parsed_program(
20+
program,
21+
&cairo_config,
22+
&mut BuiltinHintProcessor::new_empty(),
23+
STEPS_LIMIT,
24+
);
25+
});
26+
}
27+
}

vm/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cairo-1-hints = [
2525
"dep:ark-ff",
2626
"dep:ark-std",
2727
]
28+
arbitrary = ["dep:arbitrary", "felt/arbitrary", "felt/std", "std"]
2829
lambdaworks-felt = ["felt/lambdaworks-felt"]
2930

3031
# Note that these features are not retro-compatible with the cairo Python VM.
@@ -69,6 +70,9 @@ cairo-lang-casm = { workspace = true, optional = true }
6970
ark-ff = { workspace = true, optional = true }
7071
ark-std = { workspace = true, optional = true }
7172

73+
# Enable arbitrary when fuzzing
74+
arbitrary = { workspace = true, features = ["derive"], optional = true }
75+
7276
[dev-dependencies]
7377
assert_matches = "1.5.0"
7478
rstest = { version = "0.17.0", default-features = false }

0 commit comments

Comments
 (0)