Skip to content

Commit

Permalink
fuzz: interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
subotic committed Nov 25, 2024
1 parent 0f5dc7b commit bfff32b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
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
1 change: 1 addition & 0 deletions fuzz/Cargo.lock

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

4 changes: 4 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 Down
13 changes: 5 additions & 8 deletions fuzz/fuzz_targets/fuzz_interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use polkavm::Engine;
use polkavm::InterruptKind;
use polkavm::ModuleConfig;
use polkavm::ProgramBlob;
use polkavm::ProgramCounter;
use polkavm::{ArcBytes, Engine};
use polkavm_common::program::ProgramBlob;

fn harness(data: &[u8]) {
// configure the polkavm engine
Expand All @@ -21,15 +21,12 @@ fn harness(data: &[u8]) {
module_config.set_step_tracing(true);

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

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

let fuzzed_blob = ProgramBlob {
code: data.into(),
bitmask: bitmask.into(),
..blob
};
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();
Expand Down

0 comments on commit bfff32b

Please sign in to comment.