| 1 |
use std::str; |
| 2 |
|
| 3 |
pub fn run(string: &str) { |
| 4 |
write_file("wasm1.wat", &string); |
| 5 |
// Weed out `(module binary ...)` because when we print the bytes and |
| 6 |
// convert it back to binary it's not guaranteed to be exactly the same. |
| 7 |
// (think of something like an over-long LEB encoding) |
| 8 |
if string.contains("binary") { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
// Also weed out `@custom` custom sections since we don't print those right |
| 13 |
// now. |
| 14 |
if string.contains("@custom") { |
| 15 |
return; |
| 16 |
} |
| 17 |
let wasm = match wat::parse_str(string) { |
| 18 |
Ok(bytes) => bytes, |
| 19 |
Err(_) => return, |
| 20 |
}; |
| 21 |
write_file("wasm1.wasm", &wasm); |
| 22 |
|
| 23 |
// Only roundtrip valid modules for now since invalid modules can often have |
| 24 |
// bizarre structures which aren't intended to print correctly or roundtrip |
| 25 |
// well. |
| 26 |
if wasmparser::validate(&wasm).is_err() { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// And finally validate that the name section, if present, is valid. This |
| 31 |
// can be invalid if names in the name section are too long (e.g. exceeding |
| 32 |
// the maximum length of a string). The printing process will skip invalid |
| 33 |
// name sections, so if it's invalid then our roundtrip'd bytes will |
| 34 |
// trivially not match, but not in an interesting way. |
| 35 |
if validate_name_section(&wasm).is_err() { |
| 36 |
return; |
| 37 |
} |
| 38 |
let string2 = match wasmprinter::print_bytes(&wasm) { |
| 39 |
Ok(s) => s, |
| 40 |
Err(_) => return, |
| 41 |
}; |
| 42 |
write_file("wasm2.wat", &string2); |
| 43 |
|
| 44 |
let wasm2 = wat::parse_str(&string2).unwrap(); |
| 45 |
write_file("wasm2.wasm", &wasm2); |
| 46 |
if wasm == wasm2 { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
panic!("wasm bytes differ on roundtrip"); |
| 51 |
} |
| 52 |
|
| 53 |
fn write_file(path: &str, contents: impl AsRef<[u8]>) { |
| 54 |
if !log::log_enabled!(log::Level::Debug) { |
| 55 |
return; |
| 56 |
} |
| 57 |
log::debug!("writing file {path}"); |
| 58 |
std::fs::write(path, contents.as_ref()).unwrap(); |
| 59 |
} |
| 60 |
|
| 61 |
fn validate_name_section(wasm: &[u8]) -> wasmparser::Result<()> { |
| 62 |
use wasmparser::*; |
| 63 |
for payload in Parser::new(0).parse_all(wasm) { |
| 64 |
let reader = match payload? { |
| 65 |
Payload::CustomSection(c) => match c.as_known() { |
| 66 |
KnownCustom::Name(name) => name, |
| 67 |
_ => continue, |
| 68 |
}, |
| 69 |
_ => continue, |
| 70 |
}; |
| 71 |
for section in reader { |
| 72 |
match section? { |
| 73 |
Name::Module { .. } => {} |
| 74 |
Name::Function(n) |
| 75 |
| Name::Type(n) |
| 76 |
| Name::Table(n) |
| 77 |
| Name::Memory(n) |
| 78 |
| Name::Global(n) |
| 79 |
| Name::Element(n) |
| 80 |
| Name::Data(n) |
| 81 |
| Name::Tag(n) => { |
| 82 |
for name in n { |
| 83 |
name?; |
| 84 |
} |
| 85 |
} |
| 86 |
Name::Local(n) | Name::Label(n) | Name::Field(n) => { |
| 87 |
for name in n { |
| 88 |
for name in name?.names { |
| 89 |
name?; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
Name::Unknown { .. } => {} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
Ok(()) |
| 98 |
} |