Skip to content

Commit

Permalink
Backport #399
Browse files Browse the repository at this point in the history
* Fuzz data will be executed twice and then compared

* Execute 3 times
  • Loading branch information
mohanson authored Jan 30, 2024
1 parent f4892e4 commit 98787e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
15 changes: 9 additions & 6 deletions fuzz/fuzz_targets/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
use ckb_vm::cost_model::constant_cycles;
use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine};
use ckb_vm::machine::{DefaultMachineBuilder, VERSION2};
use ckb_vm::{Bytes, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
use ckb_vm::{Bytes, Error, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
use libfuzzer_sys::fuzz_target;

fn run(data: &[u8]) {
fn run(data: &[u8]) -> Result<i8, Error> {
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, 200_000);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.instruction_cycle_func(Box::new(constant_cycles))
.build();
let mut machine = AsmMachine::new(core);
let program = Bytes::copy_from_slice(data);
if let Ok(_) = machine.load_program(&program, &[]) {
let _ = machine.run();
}
machine.load_program(&program, &[])?;
machine.run()
}

fuzz_target!(|data: &[u8]| {
run(data);
let r0 = run(data);
let r1 = run(data);
let r2 = run(data);
assert_eq!(r0, r1);
assert_eq!(r1, r2);
});
15 changes: 9 additions & 6 deletions fuzz/fuzz_targets/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use ckb_vm::cost_model::constant_cycles;
use ckb_vm::machine::{DefaultCoreMachine, DefaultMachineBuilder, VERSION2};
use ckb_vm::memory::sparse::SparseMemory;
use ckb_vm::memory::wxorx::WXorXMemory;
use ckb_vm::{Bytes, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
use ckb_vm::{Bytes, Error, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
use libfuzzer_sys::fuzz_target;

fn run(data: &[u8]) {
fn run(data: &[u8]) -> Result<i8, Error> {
let machine_core = DefaultCoreMachine::<u64, WXorXMemory<SparseMemory<u64>>>::new(
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
VERSION2,
Expand All @@ -16,11 +16,14 @@ fn run(data: &[u8]) {
.instruction_cycle_func(Box::new(constant_cycles))
.build();
let program = Bytes::copy_from_slice(data);
if let Ok(_) = machine.load_program(&program, &[]) {
let _ = machine.run();
}
machine.load_program(&program, &[])?;
machine.run()
}

fuzz_target!(|data: &[u8]| {
run(data);
let r0 = run(data);
let r1 = run(data);
let r2 = run(data);
assert_eq!(r0, r1);
assert_eq!(r1, r2);
});

0 comments on commit 98787e0

Please sign in to comment.