Skip to content

Commit d79b66a

Browse files
authored
Hook up folded instructions to fuzzing (#1831)
Extend the `validate_valid_module` fuzzer to print both in folded and non-folded form to ensure that the output binary is the same when re-parsed by `wast`. While here also tweak some APIs and documentation.
1 parent 7c5b3c0 commit d79b66a

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

crates/wasmprinter/src/lib.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,16 @@ impl Config {
170170

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

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

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

197200
/// Print instructions in folded form where possible.
198-
pub fn fold_instructions(&mut self, enable: bool) {
201+
///
202+
/// This will cause printing to favor the s-expression (parenthesized) form
203+
/// of WebAssembly instructions. For example this output would be generated
204+
/// for a simple `add` function:
205+
///
206+
/// ```wasm
207+
/// (module
208+
/// (func $foo (param i32 i32) (result i32)
209+
/// (i32.add
210+
/// (local.get 0)
211+
/// (local.get 1))
212+
/// )
213+
/// )
214+
/// ```
215+
pub fn fold_instructions(&mut self, enable: bool) -> &mut Self {
199216
self.fold_instructions = enable;
217+
self
200218
}
201219

202220
/// Prints a WebAssembly binary into a `String`

fuzz/src/validate_valid_module.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,45 @@ pub fn run(u: &mut Unstructured<'_>) -> Result<()> {
3535
panic!("Invalid {}: {}", component_or_module, e);
3636
}
3737

38-
// After validation make sure that binary-to-text and text-to-binary
39-
// transforms all work as well.
38+
// Round-trip `wasm_bytes` through text and back to binary.
4039
let wat_string = wasmprinter::print_bytes(&wasm_bytes).unwrap_or_else(|e| {
4140
panic!(
4241
"failed first disassembly of Wasm into wat with `wasmprinter::print_bytes`: {}",
4342
e
4443
)
4544
});
46-
4745
let wasm_bytes = wat::parse_str(&wat_string).unwrap_or_else(|e| {
4846
panic!(
4947
"failed to assemble wat into Wasm with `wat::parse_str`: {}",
5048
e
5149
)
5250
});
53-
if log::log_enabled!(log::Level::Debug) {
54-
log::debug!("Writing roundtripped wasm to `test2.wasm`...");
55-
std::fs::write("test2.wasm", &wasm_bytes).unwrap();
56-
}
51+
crate::log_wasm(&wasm_bytes, &config);
5752

58-
let wat_string2 = wasmprinter::print_bytes(&wasm_bytes).unwrap_or_else(|e| {
53+
let mut wat_string2 = String::new();
54+
// Now round-trip the result one more time, but this time with "folded
55+
// instructions" (e.g. s-expressions in the text format).
56+
wasmprinter::Config::new()
57+
.fold_instructions(true)
58+
.print(
59+
&wasm_bytes,
60+
&mut wasmprinter::PrintFmtWrite(&mut wat_string2),
61+
)
62+
.unwrap_or_else(|e| {
63+
panic!(
64+
"failed second disassembly of Wasm into wat with `wasmprinter::print_bytes`: {}",
65+
e
66+
)
67+
});
68+
let wasm_bytes2 = wat::parse_str(&wat_string2).unwrap_or_else(|e| {
5969
panic!(
60-
"failed second disassembly of Wasm into wat with `wasmprinter::print_bytes`: {}",
70+
"failed to assemble wat into Wasm with `wat::parse_str`: {}",
6171
e
6272
)
6373
});
64-
if log::log_enabled!(log::Level::Debug) {
65-
log::debug!("Writing round tripped text format to `test2.wat`...");
66-
std::fs::write("test2.wat", &wat_string2).unwrap();
67-
}
74+
crate::log_wasm(&wasm_bytes2, &config);
6875

69-
if wat_string != wat_string2 {
76+
if wasm_bytes != wasm_bytes2 {
7077
panic!("failed to roundtrip valid module");
7178
}
7279
Ok(())

0 commit comments

Comments
 (0)