Skip to content

Commit

Permalink
fix: fuzz test (#30)
Browse files Browse the repository at this point in the history
# What ❔

Fix fuzz test by using correct function parameters

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
mikhailUshakoff authored May 21, 2024
1 parent fabc553 commit d516967
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
3 changes: 1 addition & 2 deletions fuzz/fuzz_targets/fuzz_target_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ fuzz_target!(|data: &[u8]| {
let program = vm2::initial_decommit(&mut world, address);

let mut state = VirtualMachine::new(
Box::new(world),
address,
program,
H160::zero(),
Expand All @@ -35,5 +34,5 @@ fuzz_target!(|data: &[u8]| {
hook_address: 0,
},
);
state.run();
state.run(&mut world);
});
51 changes: 28 additions & 23 deletions src/arbitrary_instruction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::addressing_modes::Arguments;
use crate::instruction_handlers::{
Add, And, CallingMode, Div, Heap, Mul, Or, PtrAdd, PtrPack, PtrShrink, PtrSub, RotateLeft,
RotateRight, ShiftLeft, ShiftRight, Sub, Xor,
Expand All @@ -13,13 +14,17 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?
};

// 1 to 4 are reserved gas costs and also skip 0
let gas_cost = u.arbitrary::<u8>()?.checked_add(5).unwrap_or(255);
let arguments = Arguments::new(predicate, gas_cost as u32);

Ok(match u.choose_index(23)? {
0 => Self::from_binop::<Add>(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -28,7 +33,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -37,7 +42,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -46,7 +51,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -55,7 +60,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -64,7 +69,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -73,7 +78,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -82,7 +87,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -91,7 +96,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -100,7 +105,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
(),
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -109,7 +114,7 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
Expand All @@ -118,60 +123,60 @@ impl<'a> Arbitrary<'a> for Instruction {
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
u.arbitrary()?,
u.arbitrary()?,
),
12 => Self::from_jump(u.arbitrary()?, predicate),
12 => Self::from_jump(u.arbitrary()?, arguments),
13 => Self::from_ptr::<PtrAdd>(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
u.arbitrary()?,
),
14 => Self::from_ptr::<PtrSub>(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
u.arbitrary()?,
),
15 => Self::from_ptr::<PtrShrink>(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
u.arbitrary()?,
),
16 => Self::from_ptr::<PtrPack>(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
u.arbitrary()?,
),
17 => {
Self::from_load::<Heap>(u.arbitrary()?, u.arbitrary()?, u.arbitrary()?, predicate)
Self::from_load::<Heap>(u.arbitrary()?, u.arbitrary()?, u.arbitrary()?, arguments)
}
18 => Self::from_store::<Heap>(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
false,
),
19 => {
Self::from_load_pointer(u.arbitrary()?, u.arbitrary()?, u.arbitrary()?, predicate)
Self::from_load_pointer(u.arbitrary()?, u.arbitrary()?, u.arbitrary()?, arguments)
}
20 => Self::from_sstore(u.arbitrary()?, u.arbitrary()?, predicate),
21 => Self::from_sload(u.arbitrary()?, u.arbitrary()?, predicate),
20 => Self::from_sstore(u.arbitrary()?, u.arbitrary()?, arguments),
21 => Self::from_sload(u.arbitrary()?, u.arbitrary()?, arguments),
22 => Self::from_far_call::<{ CallingMode::Normal as u8 }>(
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
u.arbitrary()?,
predicate,
arguments,
),
_ => unreachable!(),
})
Expand Down

0 comments on commit d516967

Please sign in to comment.