Skip to content
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

fix(fuzz): prevent int strategy to overflow when complicate #7447

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion crates/evm/fuzz/src/strategies/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ impl ValueTree for IntValueTree {
return false
}

self.lo = self.curr + if self.hi.is_negative() { I256::MINUS_ONE } else { I256::ONE };
self.lo = if self.curr != I256::MIN && self.curr != I256::MAX {
self.curr + if self.hi.is_negative() { I256::MINUS_ONE } else { I256::ONE }
} else {
self.curr
};

self.reposition()
}
Expand Down Expand Up @@ -192,3 +196,25 @@ impl Strategy for IntStrategy {
}
}
}

#[cfg(test)]
mod tests {
use crate::strategies::int::IntValueTree;
use alloy_primitives::I256;
use proptest::strategy::ValueTree;

#[test]
fn test_int_tree_complicate_should_not_overflow() {
let mut int_tree = IntValueTree::new(I256::MAX, false);
assert_eq!(int_tree.hi, I256::MAX);
assert_eq!(int_tree.curr, I256::MAX);
int_tree.complicate();
assert_eq!(int_tree.lo, I256::MAX);

let mut int_tree = IntValueTree::new(I256::MIN, false);
assert_eq!(int_tree.hi, I256::MIN);
assert_eq!(int_tree.curr, I256::MIN);
int_tree.complicate();
assert_eq!(int_tree.lo, I256::MIN);
}
}
16 changes: 16 additions & 0 deletions crates/evm/fuzz/src/strategies/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,19 @@ impl Strategy for UintStrategy {
}
}
}

#[cfg(test)]
mod tests {
use crate::strategies::uint::UintValueTree;
use alloy_primitives::U256;
use proptest::strategy::ValueTree;

#[test]
fn test_uint_tree_complicate_max() {
let mut uint_tree = UintValueTree::new(U256::MAX, false);
assert_eq!(uint_tree.hi, U256::MAX);
assert_eq!(uint_tree.curr, U256::MAX);
uint_tree.complicate();
assert_eq!(uint_tree.lo, U256::MIN);
}
}
Loading