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

Hook up folded instructions to fuzzing #1831

Merged
merged 1 commit into from
Sep 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions crates/wasmprinter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,16 @@ impl Config {

/// Whether or not to print binary offsets of each item as comments in the
/// text format whenever a newline is printed.
pub fn print_offsets(&mut self, print: bool) {
pub fn print_offsets(&mut self, print: bool) -> &mut Self {
self.print_offsets = print;
self
}

/// Whether or not to print only a "skeleton" which skips function bodies,
/// data segment contents, element segment contents, etc.
pub fn print_skeleton(&mut self, print: bool) {
pub fn print_skeleton(&mut self, print: bool) -> &mut Self {
self.print_skeleton = print;
self
}

/// Assign names to all unnamed items.
Expand All @@ -190,13 +192,29 @@ impl Config {
///
/// Note that if the resulting text output is converted back to binary the
/// resulting `name` custom section will not be the same as before.
pub fn name_unnamed(&mut self, enable: bool) {
pub fn name_unnamed(&mut self, enable: bool) -> &mut Self {
self.name_unnamed = enable;
self
}

/// Print instructions in folded form where possible.
pub fn fold_instructions(&mut self, enable: bool) {
///
/// This will cause printing to favor the s-expression (parenthesized) form
/// of WebAssembly instructions. For example this output would be generated
/// for a simple `add` function:
///
/// ```wasm
/// (module
/// (func $foo (param i32 i32) (result i32)
/// (i32.add
/// (local.get 0)
/// (local.get 1))
/// )
/// )
/// ```
pub fn fold_instructions(&mut self, enable: bool) -> &mut Self {
self.fold_instructions = enable;
self
}

/// Prints a WebAssembly binary into a `String`
Expand Down
35 changes: 21 additions & 14 deletions fuzz/src/validate_valid_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,45 @@ pub fn run(u: &mut Unstructured<'_>) -> Result<()> {
panic!("Invalid {}: {}", component_or_module, e);
}

// After validation make sure that binary-to-text and text-to-binary
// transforms all work as well.
// Round-trip `wasm_bytes` through text and back to binary.
let wat_string = wasmprinter::print_bytes(&wasm_bytes).unwrap_or_else(|e| {
panic!(
"failed first disassembly of Wasm into wat with `wasmprinter::print_bytes`: {}",
e
)
});

let wasm_bytes = wat::parse_str(&wat_string).unwrap_or_else(|e| {
panic!(
"failed to assemble wat into Wasm with `wat::parse_str`: {}",
e
)
});
if log::log_enabled!(log::Level::Debug) {
log::debug!("Writing roundtripped wasm to `test2.wasm`...");
std::fs::write("test2.wasm", &wasm_bytes).unwrap();
}
crate::log_wasm(&wasm_bytes, &config);

let wat_string2 = wasmprinter::print_bytes(&wasm_bytes).unwrap_or_else(|e| {
let mut wat_string2 = String::new();
// Now round-trip the result one more time, but this time with "folded
// instructions" (e.g. s-expressions in the text format).
wasmprinter::Config::new()
.fold_instructions(true)
.print(
&wasm_bytes,
&mut wasmprinter::PrintFmtWrite(&mut wat_string2),
)
.unwrap_or_else(|e| {
panic!(
"failed second disassembly of Wasm into wat with `wasmprinter::print_bytes`: {}",
e
)
});
let wasm_bytes2 = wat::parse_str(&wat_string2).unwrap_or_else(|e| {
panic!(
"failed second disassembly of Wasm into wat with `wasmprinter::print_bytes`: {}",
"failed to assemble wat into Wasm with `wat::parse_str`: {}",
e
)
});
if log::log_enabled!(log::Level::Debug) {
log::debug!("Writing round tripped text format to `test2.wat`...");
std::fs::write("test2.wat", &wat_string2).unwrap();
}
crate::log_wasm(&wasm_bytes2, &config);

if wat_string != wat_string2 {
if wasm_bytes != wasm_bytes2 {
panic!("failed to roundtrip valid module");
}
Ok(())
Expand Down