/src/wasm-tools/fuzz/fuzz_targets/roundtrip-valid-module.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #![no_main] |
2 | | |
3 | | use libfuzzer_sys::fuzz_target; |
4 | | use wasm_smith::Module; |
5 | | |
6 | | fuzz_target!(|module: Module| { |
7 | | let bytes = module.to_bytes(); |
8 | | |
9 | | let wat_string = wasmprinter::print_bytes(&bytes).unwrap_or_else(|e| { |
10 | | fail( |
11 | | &bytes, |
12 | | &e, |
13 | | "failed first disassembly of Wasm into wat with `wasmprinter::print_bytes`", |
14 | | ) |
15 | | }); |
16 | | let wasm_bytes = wat::parse_str(&wat_string).unwrap_or_else(|e| { |
17 | | fail( |
18 | | &bytes, |
19 | | &e, |
20 | | "failed to assemble wat into Wasm with `wat::parse_str`", |
21 | | ) |
22 | | }); |
23 | | let wat_string2 = wasmprinter::print_bytes(&wasm_bytes).unwrap_or_else(|e| { |
24 | | fail( |
25 | | &bytes, |
26 | | &e, |
27 | | "failed second disassembly of Wasm into wat with `wasmprinter::print_bytes`", |
28 | | ) |
29 | | }); |
30 | | |
31 | | if wat_string != wat_string2 { |
32 | | fail( |
33 | | &bytes, |
34 | | &"first and second disassembly is not equal", |
35 | | "failed to roundtrip valid module", |
36 | | ); |
37 | | } |
38 | | }); |
39 | | |
40 | 0 | fn fail(bytes: &[u8], error: &impl std::fmt::Display, msg: &str) -> ! { |
41 | 0 | eprintln!("Writing test case to `test.wasm`."); |
42 | 0 | std::fs::write("test.wasm", bytes).unwrap(); |
43 | 0 | panic!("{}: {}", msg, error)Unexecuted instantiation: roundtrip_valid_module::fail::<&str> Unexecuted instantiation: roundtrip_valid_module::fail::<wat::Error> Unexecuted instantiation: roundtrip_valid_module::fail::<anyhow::Error> |
44 | | } |