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

Backport #424 #427

Merged
merged 1 commit into from
Mar 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ name = "isa_b"
path = "fuzz_targets/isa_b.rs"
test = false
doc = false

[[bin]]
name = "snapshot"
path = "fuzz_targets/snapshot.rs"
test = false
doc = false
55 changes: 55 additions & 0 deletions fuzz/fuzz_targets/snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![no_main]
use ckb_vm::cost_model::constant_cycles;
use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine};
use ckb_vm::machine::{DefaultMachineBuilder, VERSION2};
use ckb_vm::snapshot;
use ckb_vm::{Bytes, Error, SupportMachine, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
let mut machine1 = {
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, 200_000);
let machine = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.instruction_cycle_func(Box::new(constant_cycles))
.build();
AsmMachine::new(machine)
};
let program = Bytes::copy_from_slice(data);
if machine1.load_program(&program, &[]).is_err() {
return;
};
let result1 = machine1.run();
if machine1.machine.cycles() < 4 {
return;
}

let half_cycles = machine1.machine.cycles() / 2;
let mut machine2 = {
let asm_core =
AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, half_cycles);
let machine = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.instruction_cycle_func(Box::new(constant_cycles))
.build();
AsmMachine::new(machine)
};
machine2.load_program(&program, &[]).unwrap();
let result2 = machine2.run();
assert_eq!(result2.unwrap_err(), Error::CyclesExceeded);
let snap = snapshot::make_snapshot(&mut machine2.machine).unwrap();

let mut machine3 = {
let asm_core =
AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, half_cycles);
let machine = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.instruction_cycle_func(Box::new(constant_cycles))
.build();
AsmMachine::new(machine)
};
snapshot::resume(&mut machine3.machine, &snap).unwrap();

machine3.machine.set_cycles(machine2.machine.cycles());
machine3.machine.set_max_cycles(200_000);
let result3 = machine3.run();
assert_eq!(result1, result3);
assert_eq!(machine1.machine.cycles(), machine3.machine.cycles());
});