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

fuzz: interpreter #179

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion crates/polkavm-common/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4303,6 +4303,13 @@ impl ProgramBlob {
&self.bitmask
}

#[cfg(feature = "export-internals-for-testing")]
#[doc(hidden)]
pub fn set_bitmask(&mut self, bitmask: ArcBytes) {
self.bitmask = bitmask;
}

/// Returns the import offsets and symbols.
pub fn imports(&self) -> Imports {
Imports {
offsets: &self.import_offsets,
Expand Down Expand Up @@ -4363,7 +4370,7 @@ impl ProgramBlob {
}
}

/// Visits every instrution in the program.
/// Visits every instruction in the program.
#[cfg_attr(not(debug_assertions), inline(always))]
pub fn visit<T>(&self, dispatch_table: T, visitor: &mut T::State)
where
Expand Down
3 changes: 2 additions & 1 deletion fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ libfuzzer-sys = "0.4"
path = "../crates/polkavm"
features = ["export-internals-for-testing"]

[dependencies.polkavm-common]
path = "../crates/polkavm-common"
features = ["export-internals-for-testing"]

[[bin]]
name = "fuzz_shm_allocator"
path = "fuzz_targets/fuzz_shm_allocator.rs"
Expand All @@ -29,6 +33,13 @@ test = false
doc = false
bench = false

[[bin]]
name = "fuzz_interpreter"
path = "fuzz_targets/fuzz_interpreter.rs"
test = false
doc = false
bench = false

[workspace]
resolver = "2"
members = ["."]
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/fuzz_generic_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_main]

#[macro_use]
extern crate libfuzzer_sys;
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;

Expand Down
59 changes: 59 additions & 0 deletions fuzz/fuzz_targets/fuzz_interpreter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use polkavm::Engine;
use polkavm::InterruptKind;
use polkavm::ModuleConfig;
use polkavm::ProgramCounter;
use polkavm_common::program::ProgramBlob;

fn harness(data: &[u8]) {
// configure the polkavm engine
jarkkojs marked this conversation as resolved.
Show resolved Hide resolved
let mut config = polkavm::Config::new();
config.set_backend(Some(polkavm::BackendKind::Interpreter));

let engine = Engine::new(&config).unwrap();

// configure the polkavm module
let mut module_config = ModuleConfig::default();
module_config.set_strict(true);
module_config.set_gas_metering(Some(polkavm::GasMeteringKind::Sync));
module_config.set_step_tracing(true);

// create a polkavm program blob (eventually to be filled with the fuzzed data)
let mut fuzzed_blob = ProgramBlob::default();

let bitmask = vec![0xff; data.len() / 8 + 1];

fuzzed_blob.set_code(data.into());
fuzzed_blob.set_bitmask(bitmask.into());

// create a polkavm module from the engine, module config, and program blob
let module = polkavm::Module::from_blob(&engine, &module_config, fuzzed_blob).unwrap();

let initial_pc = ProgramCounter(0);
let mut final_pc = initial_pc;

// instantiate the module and run it
let mut instance = module.instantiate().unwrap();
instance.set_gas(1000000);
jarkkojs marked this conversation as resolved.
Show resolved Hide resolved
instance.set_next_program_counter(initial_pc);

let expected_status = loop {
match instance.run().unwrap() {
InterruptKind::Finished => break "halt",
InterruptKind::Trap => break "trap",
InterruptKind::Ecalli(..) => todo!(),
InterruptKind::NotEnoughGas => break "out-of-gas",
InterruptKind::Segfault(..) => todo!(),
InterruptKind::Step => {
final_pc = instance.program_counter().unwrap();
continue;
}
}
};
}

fuzz_target!(|data: &[u8]| {
harness(data);
});