Skip to content

Commit d97e88e

Browse files
authored
Fix fuzz (#476)
* Fix fuzz * Build fuzz in ci
1 parent 65fef7b commit d97e88e

File tree

7 files changed

+48
-20
lines changed

7 files changed

+48
-20
lines changed

.github/workflows/develop.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,13 @@ jobs:
172172
shell: pwsh
173173
run: |
174174
make ci-asm
175+
176+
linux-fuzz:
177+
runs-on: ubuntu-latest
178+
steps:
179+
- uses: actions/checkout@v3
180+
- name: Build fuzz
181+
run: |
182+
sudo apt install device-tree-compiler
183+
cargo install cargo-fuzz
184+
cargo +nightly fuzz build

fuzz/fuzz_targets/asm.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine};
44
use ckb_vm::machine::{DefaultCoreMachine, DefaultMachineBuilder, VERSION2};
55
use ckb_vm::memory::sparse::SparseMemory;
66
use ckb_vm::memory::wxorx::WXorXMemory;
7-
use ckb_vm::{Bytes, Error, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
7+
use ckb_vm::{Bytes, DefaultMachineRunner, Error, SupportMachine, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
88
use libfuzzer_sys::fuzz_target;
99

1010
fn run_asm(data: &[u8]) -> Result<i8, Error> {
11-
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, 200_000);
11+
let asm_core = <Box<AsmCoreMachine> as SupportMachine>::new(
12+
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
13+
VERSION2,
14+
200_000,
15+
);
1216
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
1317
.instruction_cycle_func(Box::new(constant_cycles))
1418
.build();

fuzz/fuzz_targets/interpreter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ckb_vm::cost_model::constant_cycles;
33
use ckb_vm::machine::{DefaultCoreMachine, DefaultMachineBuilder, VERSION2};
44
use ckb_vm::memory::sparse::SparseMemory;
55
use ckb_vm::memory::wxorx::WXorXMemory;
6-
use ckb_vm::{Bytes, Error, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
6+
use ckb_vm::{Bytes, Error, SupportMachine, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
77
use libfuzzer_sys::fuzz_target;
88

99
fn run(data: &[u8]) -> Result<i8, Error> {

fuzz/fuzz_targets/isa_a.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![no_main]
2-
use ckb_vm::{CoreMachine, Memory};
2+
use ckb_vm::{CoreMachine, Memory, SupportMachine};
33
use libfuzzer_sys::fuzz_target;
44
use spike_sys::Spike;
55
use std::collections::VecDeque;
@@ -39,9 +39,11 @@ fuzz_target!(|data: [u8; 512]| {
3939
ckb_vm::SparseMemory<u64>,
4040
>::new(ckb_vm_isa, ckb_vm_version, u64::MAX))
4141
.build();
42-
let mut ckb_vm_asm = ckb_vm::DefaultMachineBuilder::new(
43-
ckb_vm::machine::asm::AsmCoreMachine::new(ckb_vm_isa, ckb_vm_version, u64::MAX),
44-
)
42+
let mut ckb_vm_asm = ckb_vm::DefaultMachineBuilder::new(<Box<
43+
ckb_vm::machine::asm::AsmCoreMachine,
44+
> as SupportMachine>::new(
45+
ckb_vm_isa, ckb_vm_version, u64::MAX
46+
))
4547
.build();
4648

4749
let insts: [u32; 18] = [

fuzz/fuzz_targets/isa_b.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![no_main]
2-
use ckb_vm::CoreMachine;
2+
use ckb_vm::{CoreMachine, SupportMachine};
33
use libfuzzer_sys::fuzz_target;
44
use spike_sys::Spike;
55
use std::collections::VecDeque;
@@ -45,9 +45,11 @@ fuzz_target!(|data: [u8; 512]| {
4545
ckb_vm::SparseMemory<u64>,
4646
>::new(ckb_vm_isa, ckb_vm_version, u64::MAX))
4747
.build();
48-
let mut ckb_vm_asm = ckb_vm::DefaultMachineBuilder::new(
49-
ckb_vm::machine::asm::AsmCoreMachine::new(ckb_vm_isa, ckb_vm_version, u64::MAX),
50-
)
48+
let mut ckb_vm_asm = ckb_vm::DefaultMachineBuilder::new(<Box<
49+
ckb_vm::machine::asm::AsmCoreMachine,
50+
> as SupportMachine>::new(
51+
ckb_vm_isa, ckb_vm_version, u64::MAX
52+
))
5153
.build();
5254

5355
#[rustfmt::skip]

fuzz/fuzz_targets/snapshot.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ use ckb_vm::cost_model::constant_cycles;
33
use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine};
44
use ckb_vm::machine::{DefaultMachineBuilder, VERSION2};
55
use ckb_vm::snapshot;
6-
use ckb_vm::{Bytes, Error, SupportMachine, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
6+
use ckb_vm::{Bytes, DefaultMachineRunner, Error, SupportMachine, ISA_A, ISA_B, ISA_IMC, ISA_MOP};
77
use libfuzzer_sys::fuzz_target;
88

99
fuzz_target!(|data: &[u8]| {
1010
let mut machine1 = {
11-
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, 200_000);
11+
let asm_core = <Box<AsmCoreMachine> as SupportMachine>::new(
12+
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
13+
VERSION2,
14+
200_000,
15+
);
1216
let machine = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
1317
.instruction_cycle_func(Box::new(constant_cycles))
1418
.build();
@@ -25,8 +29,11 @@ fuzz_target!(|data: &[u8]| {
2529

2630
let half_cycles = machine1.machine.cycles() / 2;
2731
let mut machine2 = {
28-
let asm_core =
29-
AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, half_cycles);
32+
let asm_core = <Box<AsmCoreMachine> as SupportMachine>::new(
33+
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
34+
VERSION2,
35+
half_cycles,
36+
);
3037
let machine = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
3138
.instruction_cycle_func(Box::new(constant_cycles))
3239
.build();
@@ -38,8 +45,11 @@ fuzz_target!(|data: &[u8]| {
3845
let snap = snapshot::make_snapshot(&mut machine2.machine).unwrap();
3946

4047
let mut machine3 = {
41-
let asm_core =
42-
AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B | ISA_MOP, VERSION2, half_cycles);
48+
let asm_core = <Box<AsmCoreMachine> as SupportMachine>::new(
49+
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
50+
VERSION2,
51+
half_cycles,
52+
);
4353
let machine = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
4454
.instruction_cycle_func(Box::new(constant_cycles))
4555
.build();

fuzz/fuzz_targets/snapshot2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use ckb_vm::{
44
machine::VERSION2,
55
memory::{round_page_down, round_page_up, FLAG_EXECUTABLE, FLAG_FREEZED},
66
snapshot2::{DataSource, Snapshot2Context},
7-
Bytes, CoreMachine, DefaultMachine, DefaultMachineBuilder, Memory, ISA_A, ISA_B, ISA_IMC,
8-
ISA_MOP, RISCV_MAX_MEMORY, RISCV_PAGESIZE,
7+
Bytes, CoreMachine, DefaultMachine, DefaultMachineBuilder, Memory, SupportMachine, ISA_A,
8+
ISA_B, ISA_IMC, ISA_MOP, RISCV_MAX_MEMORY, RISCV_PAGESIZE,
99
};
1010
use ckb_vm_definitions::asm::AsmCoreMachine;
1111
use libfuzzer_sys::fuzz_target;
@@ -65,7 +65,7 @@ impl DataSource<u32> for DummyData {
6565

6666
fn build_machine() -> DefaultMachine<Box<AsmCoreMachine>> {
6767
let isa = ISA_IMC | ISA_A | ISA_B | ISA_MOP;
68-
let core_machine = AsmCoreMachine::new(isa, VERSION2, u64::MAX);
68+
let core_machine = <Box<AsmCoreMachine> as SupportMachine>::new(isa, VERSION2, u64::MAX);
6969
DefaultMachineBuilder::new(core_machine).build()
7070
}
7171

0 commit comments

Comments
 (0)