Skip to content

Commit e675984

Browse files
authored
Log module "DNA" during fuzzing (#10612)
This commit updates the fuzzing infrastructure of the `wasmtime-fuzzing` crate to record the "DNA string" of a module used to generate a module in a `*.dna` file. This is accompanied with a `*.json` file to pass to `wasm-tools smith --config`. The end result is that it should be possible now to more easily reproduce a module generation outside of Wasmtime itself when reproducing bugs and such. Creation of these files is gated on the debug log level in a similar manner to creation of normal wasm files is gated on the debug log level too.
1 parent c9db233 commit e675984

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/fuzzing/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ wasmprinter = { workspace = true }
2727
wasmtime-wast = { workspace = true, features = ['component-model'] }
2828
wasmtime = { workspace = true, features = ['default', 'winch'] }
2929
wasm-encoder = { workspace = true }
30-
wasm-smith = { workspace = true }
30+
wasm-smith = { workspace = true, features = ['serde'] }
3131
wasm-mutate = { workspace = true }
3232
wasm-spec-interpreter = { path = "./wasm-spec-interpreter", optional = true }
3333
wasmi = { version = "0.43.1", default-features = false, features = ["std", "simd"] }
3434
futures = { workspace = true }
3535
wasmtime-test-util = { workspace = true, features = ['wast', 'component-fuzz', 'component'] }
36+
serde_json = { workspace = true }
3637

3738
[dependencies.wasmtime-cli-flags]
3839
workspace = true

crates/fuzzing/src/generators/module.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Generate a Wasm module and the configuration for generating it.
22
33
use arbitrary::{Arbitrary, Unstructured};
4+
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
45

56
/// Default module-level configuration for fuzzing Wasmtime.
67
///
@@ -87,8 +88,30 @@ impl ModuleConfig {
8788
input: &mut Unstructured<'_>,
8889
default_fuel: Option<u32>,
8990
) -> arbitrary::Result<wasm_smith::Module> {
91+
crate::init_fuzzing();
92+
93+
// If requested, save `*.{dna,json}` files for recreating this module
94+
// in wasm-tools alone.
95+
let input_before = if log::log_enabled!(log::Level::Debug) {
96+
let len = input.len();
97+
Some(input.peek_bytes(len).unwrap().to_vec())
98+
} else {
99+
None
100+
};
101+
90102
let mut module = wasm_smith::Module::new(self.config.clone(), input)?;
91103

104+
if let Some(before) = input_before {
105+
static GEN_CNT: AtomicUsize = AtomicUsize::new(0);
106+
let used = before.len() - input.len();
107+
let i = GEN_CNT.fetch_add(1, Relaxed);
108+
let dna = format!("testcase{i}.dna");
109+
let config = format!("testcase{i}.json");
110+
log::debug!("writing `{dna}` and `{config}`");
111+
std::fs::write(&dna, &before[..used]).unwrap();
112+
std::fs::write(&config, serde_json::to_string_pretty(&self.config).unwrap()).unwrap();
113+
}
114+
92115
if let Some(default_fuel) = default_fuel {
93116
module.ensure_termination(default_fuel).unwrap();
94117
}

0 commit comments

Comments
 (0)