-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[aptos fuzz] add fuzzing target for u256 #18081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
d3d4294 to
c022e9b
Compare
c022e9b to
5216de7
Compare
|
|
||
| fuzz_target!(|data: &[u8]| { | ||
| if data.len() < 64 { | ||
| return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure it's correctly discarded by libFuzzer, return:
Corpus::Reject| bytes.copy_from_slice(&data[0..32]); | ||
| let pri_u256_1 = PrimitiveU256::get_from_le_bytes(&bytes); | ||
| let eth_u256_1 = EthnumU256::get_from_le_bytes(&bytes); | ||
|
|
||
| bytes.copy_from_slice(&data[32..64]); | ||
| let pri_u256_2 = PrimitiveU256::get_from_le_bytes(&bytes); | ||
| let eth_u256_2 = EthnumU256::get_from_le_bytes(&bytes); | ||
|
|
||
| // Check conversion consistency | ||
| assert_eq!(pri_u256_1.turn_to_le_bytes(), eth_u256_1.turn_to_le_bytes()); | ||
| assert_eq!(pri_u256_2.turn_to_le_bytes(), eth_u256_2.turn_to_le_bytes()); | ||
|
|
||
| // Check arithmetic operations | ||
| let add1 = PrimitiveU256::checked_add(pri_u256_1, pri_u256_2); | ||
| let add2 = EthnumU256::checked_add(eth_u256_1, eth_u256_2); | ||
| assert_eq!( | ||
| add1.map(|x| x.turn_to_le_bytes()), | ||
| add2.map(|x| x.turn_to_le_bytes()) | ||
| ); | ||
|
|
||
| let sub1 = PrimitiveU256::checked_sub(pri_u256_1, pri_u256_2); | ||
| let sub2 = EthnumU256::checked_sub(eth_u256_1, eth_u256_2); | ||
| assert_eq!( | ||
| sub1.map(|x| x.turn_to_le_bytes()), | ||
| sub2.map(|x| x.turn_to_le_bytes()) | ||
| ); | ||
|
|
||
| let mul1 = PrimitiveU256::checked_mul(pri_u256_1, pri_u256_2); | ||
| let mul2 = EthnumU256::checked_mul(eth_u256_1, eth_u256_2); | ||
| assert_eq!( | ||
| mul1.map(|x| x.turn_to_le_bytes()), | ||
| mul2.map(|x| x.turn_to_le_bytes()) | ||
| ); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally, it's suggested to split test cases/fuzzers as those are explored better stand-alone. But I guess the fuzzer is quite fast, and we can ignore it.
Description
When introducing signed integers, we switched from
primitive-typestoethnumas the underneath crate to support ouru256type. This PR adds a harness to differentially fuzz the two crates on their operations that we reuse foru256.Running the harness can effectively cover the operations we care about. No disparity issues were found.
Type of Change
Which Components or Systems Does This Change Impact?
Note
Adds a fuzz target to differentially test
ethnum::U256againstprimitive_types::U256, with a comprehensive dictionary and dependency updates.u256_diff_fuzzintestsuite/fuzzer/fuzz/fuzz_targets/u256_diff_fuzz.rsto differentially testethnum::U256vsprimitive_types::U256across arithmetic, bitwise, shifts, and conversions.testsuite/fuzzer/fuzz/dictionaries/u256.dictcovering boundary cases for multiple bit-widths.ethnumand includeprimitive-typesin the fuzz crate.primitive-typesin rootCargo.tomlfrom0.10to0.12.2.Written by Cursor Bugbot for commit 5216de7. This will update automatically on new commits. Configure here.