| 1 |
use arbitrary::{Result, Unstructured}; |
| 2 |
use std::path::Path; |
| 3 |
use wasm_encoder::reencode::{self, Reencode}; |
| 4 |
use wasm_encoder::{ImportSection, Module}; |
| 5 |
use wit_component::*; |
| 6 |
use wit_parser::{LiftLowerAbi, ManglingAndAbi, PackageId, Resolve}; |
| 7 |
|
| 8 |
pub fn run(u: &mut Unstructured<'_>) -> Result<()> { |
| 9 |
let wasm = u.arbitrary().and_then(|config| { |
| 10 |
log::debug!("config: {config:#?}"); |
| 11 |
wit_smith::smith(&config, u) |
| 12 |
})?; |
| 13 |
write_file("doc1.wasm", &wasm); |
| 14 |
let (resolve, pkg) = match wit_component::decode(&wasm).unwrap() { |
| 15 |
DecodedWasm::WitPackage(resolve, pkg) => (resolve, pkg), |
| 16 |
DecodedWasm::Component(..) => unreachable!(), |
| 17 |
}; |
| 18 |
resolve.assert_valid(); |
| 19 |
|
| 20 |
roundtrip_through_printing("doc1", &resolve, pkg, &wasm); |
| 21 |
|
| 22 |
let (resolve2, pkg2) = match wit_component::decode(&wasm).unwrap() { |
| 23 |
DecodedWasm::WitPackage(resolve, pkgs) => (resolve, pkgs), |
| 24 |
DecodedWasm::Component(..) => unreachable!(), |
| 25 |
}; |
| 26 |
resolve2.assert_valid(); |
| 27 |
|
| 28 |
let wasm2 = wit_component::encode(&resolve2, pkg2).expect("failed to encode WIT document"); |
| 29 |
write_file("doc2.wasm", &wasm2); |
| 30 |
roundtrip_through_printing("doc2", &resolve2, pkg2, &wasm2); |
| 31 |
|
| 32 |
if wasm != wasm2 { |
| 33 |
panic!("roundtrip wasm didn't match"); |
| 34 |
} |
| 35 |
|
| 36 |
// If there's hundreds or thousands of worlds only work with the first few |
| 37 |
// to avoid timing out this fuzzer with asan enabled. |
| 38 |
let mut decoded_bindgens = Vec::new(); |
| 39 |
for (id, world) in resolve.worlds.iter().take(20) { |
| 40 |
let mangling = match u.int_in_range(0..=3)? { |
| 41 |
0 => ManglingAndAbi::Legacy(LiftLowerAbi::Sync), |
| 42 |
1 => ManglingAndAbi::Legacy(LiftLowerAbi::AsyncCallback), |
| 43 |
2 => ManglingAndAbi::Legacy(LiftLowerAbi::AsyncStackful), |
| 44 |
3 => ManglingAndAbi::Standard32, |
| 45 |
_ => unreachable!(), |
| 46 |
}; |
| 47 |
log::debug!( |
| 48 |
"embedding world {} as in a dummy module with abi {mangling:?}", |
| 49 |
world.name |
| 50 |
); |
| 51 |
let mut dummy = wit_component::dummy_module(&resolve, id, mangling); |
| 52 |
if u.arbitrary()? { |
| 53 |
let mut dst = Module::default(); |
| 54 |
let mut reencode = RemoveImports { |
| 55 |
u, |
| 56 |
removed_funcs: 0, |
| 57 |
}; |
| 58 |
if reencode |
| 59 |
.parse_core_module(&mut dst, Default::default(), &dummy) |
| 60 |
.is_ok() |
| 61 |
{ |
| 62 |
dummy = dst.finish(); |
| 63 |
} |
| 64 |
} |
| 65 |
wit_component::embed_component_metadata(&mut dummy, &resolve, id, StringEncoding::UTF8) |
| 66 |
.unwrap(); |
| 67 |
write_file("dummy.wasm", &dummy); |
| 68 |
|
| 69 |
log::debug!("... componentizing the world into a binary component"); |
| 70 |
let wasm = wit_component::ComponentEncoder::default() |
| 71 |
.module(&dummy) |
| 72 |
.unwrap() |
| 73 |
.encode() |
| 74 |
.unwrap(); |
| 75 |
write_file("dummy.component.wasm", &wasm); |
| 76 |
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all()) |
| 77 |
.validate_all(&wasm) |
| 78 |
.unwrap(); |
| 79 |
|
| 80 |
// Decode what was just created and record it later for testing merging |
| 81 |
// worlds together. |
| 82 |
let (_, decoded) = wit_component::metadata::decode(&dummy).unwrap(); |
| 83 |
decoded_bindgens.push((decoded, dummy, world.name.clone())); |
| 84 |
|
| 85 |
log::debug!("... decoding the component itself"); |
| 86 |
wit_component::decode(&wasm).unwrap(); |
| 87 |
|
| 88 |
// Test out importizing the world and then assert the world is still |
| 89 |
// valid. |
| 90 |
log::debug!("... importizing this world"); |
| 91 |
let mut resolve2 = resolve.clone(); |
| 92 |
let _ = resolve2.importize(id, None); |
| 93 |
} |
| 94 |
|
| 95 |
if decoded_bindgens.len() < 2 { |
| 96 |
return Ok(()); |
| 97 |
} |
| 98 |
|
| 99 |
let i = u.choose_index(decoded_bindgens.len())?; |
| 100 |
let (mut b1, wasm1, world1) = decoded_bindgens.swap_remove(i); |
| 101 |
|
| 102 |
if u.arbitrary()? { |
| 103 |
let i = u.choose_index(decoded_bindgens.len())?; |
| 104 |
let (b2, wasm2, world2) = decoded_bindgens.swap_remove(i); |
| 105 |
|
| 106 |
log::debug!("merging bindgens world {world1} <- world {world2}"); |
| 107 |
|
| 108 |
write_file("bindgen1.wasm", &wasm1); |
| 109 |
write_file("bindgen2.wasm", &wasm2); |
| 110 |
|
| 111 |
// Merging worlds may fail but if successful then a `Resolve` is asserted |
| 112 |
// to be valid which is what we're interested in here. Note that failure |
| 113 |
// here can be due to the structure of worlds which aren't reasonable to |
| 114 |
// control in this generator, so it's just done to see what happens and try |
| 115 |
// to trigger panics in `Resolve::assert_valid`. |
| 116 |
let _ = b1.merge(b2); |
| 117 |
} else { |
| 118 |
log::debug!("merging world imports based on semver {world1}"); |
| 119 |
write_file("bindgen1.wasm", &wasm1); |
| 120 |
let _ = b1.resolve.merge_world_imports_based_on_semver(b1.world); |
| 121 |
} |
| 122 |
Ok(()) |
| 123 |
} |
| 124 |
|
| 125 |
fn roundtrip_through_printing(file: &str, resolve: &Resolve, pkg: PackageId, wasm: &[u8]) { |
| 126 |
// Print to a single string, using nested `package ... { .. }` statements, |
| 127 |
// and then parse that in a new `Resolve`. |
| 128 |
let mut new_resolve = Resolve::default(); |
| 129 |
new_resolve.all_features = true; |
| 130 |
let package_deps = resolve |
| 131 |
.packages |
| 132 |
.iter() |
| 133 |
.map(|p| p.0) |
| 134 |
.filter(|k| *k != pkg) |
| 135 |
.collect::<Vec<_>>(); |
| 136 |
let mut printer = WitPrinter::default(); |
| 137 |
printer.print(resolve, pkg, &package_deps).unwrap(); |
| 138 |
let doc = printer.output.to_string(); |
| 139 |
let new_pkg = new_resolve |
| 140 |
.push_str(&format!("printed-{file}.wit"), &doc) |
| 141 |
.unwrap(); |
| 142 |
|
| 143 |
// Finally encode the `new_resolve` which should be the exact same as |
| 144 |
// before. |
| 145 |
let wasm2 = wit_component::encode(&new_resolve, new_pkg).unwrap(); |
| 146 |
write_file(&format!("{file}-reencoded.wasm"), &wasm2); |
| 147 |
if wasm != wasm2 { |
| 148 |
panic!("failed to roundtrip through text printing"); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
fn write_file(path: &str, contents: impl AsRef<[u8]>) { |
| 153 |
if !log::log_enabled!(log::Level::Debug) { |
| 154 |
return; |
| 155 |
} |
| 156 |
log::debug!("writing file {path}"); |
| 157 |
let contents = contents.as_ref(); |
| 158 |
let path = Path::new(path); |
| 159 |
std::fs::write(path, contents).unwrap(); |
| 160 |
if path.extension().and_then(|s| s.to_str()) == Some("wasm") { |
| 161 |
let path = path.with_extension("wat"); |
| 162 |
log::debug!("writing file {}", path.display()); |
| 163 |
std::fs::write(path, wasmprinter::print_bytes(&contents).unwrap()).unwrap(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
struct RemoveImports<'a, 'b> { |
| 168 |
u: &'a mut Unstructured<'b>, |
| 169 |
removed_funcs: u32, |
| 170 |
} |
| 171 |
|
| 172 |
impl Reencode for RemoveImports<'_, '_> { |
| 173 |
type Error = std::convert::Infallible; |
| 174 |
|
| 175 |
fn function_index(&mut self, idx: u32) -> Result<u32, reencode::Error<Self::Error>> { |
| 176 |
Ok(idx - self.removed_funcs) |
| 177 |
} |
| 178 |
|
| 179 |
fn parse_import( |
| 180 |
&mut self, |
| 181 |
imports: &mut ImportSection, |
| 182 |
import: wasmparser::Import<'_>, |
| 183 |
) -> Result<(), reencode::Error<Self::Error>> { |
| 184 |
if self.u.arbitrary().unwrap_or(false) { |
| 185 |
self.removed_funcs += 1; |
| 186 |
Ok(()) |
| 187 |
} else { |
| 188 |
reencode::utils::parse_import(self, imports, import) |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
#[should_panic] // TODO: this should get fixed |
| 194 |
#[test] |
| 195 |
fn smoke() { |
| 196 |
super::test::test_n_times(100, run); |
| 197 |
} |