| 1 |
#![cfg(feature = "wasmparser")] |
| 2 |
|
| 3 |
use arbitrary::Unstructured; |
| •4 |
use rand::{Rng, SeedableRng, rngs::SmallRng}; |
| 5 |
use wasm_smith::{Config, Module}; |
| 6 |
use wasmparser::{Parser, Validator, WasmFeatures, types::EntityType}; |
| 7 |
|
| 8 |
mod common; |
| 9 |
use common::validate; |
| 10 |
|
| 11 |
#[derive(Debug, PartialEq)] |
| 12 |
struct WasmExport(String, EntityType); |
| 13 |
#[derive(Debug, PartialEq)] |
| 14 |
struct WasmImport(String, String, EntityType); |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn smoke_test_module_shape_with_gc_types() { |
| 18 |
let test = r#" |
| 19 |
(module |
| 20 |
(rec |
| 21 |
(type $t0 (func (param i32 (ref null $t1)) (result (ref null $t0)))) |
| 22 |
(type $t1 (func (param i64 (ref null $t0)))) |
| 23 |
) |
| 24 |
(rec |
| 25 |
(type $t2 (sub (struct (field i8)))) |
| 26 |
(type $t3 (sub $t2 (struct (field i8) (field i16)))) |
| 27 |
(type $t4 (sub final $t3 (struct (field i8) (field i16) (field (ref null $t0))))) |
| 28 |
) |
| 29 |
(rec |
| 30 |
(type $t5 (sub (array (ref null $t2)))) |
| 31 |
(type $t6 (sub final $t5 (array (ref null $t3)))) |
| 32 |
) |
| 33 |
(import "env" "fun" (func (type $t0))) |
| 34 |
(import "env" "tbl" (table i32 1 10 (ref null $t2))) |
| 35 |
(import "env" "mem" (memory 5 10)) |
| 36 |
(import "env" "glb" (global (ref null $t4))) |
| 37 |
(import "env" "tag" (tag (type $t1))) |
| 38 |
(func (export "f") (type $t0) unreachable) |
| 39 |
(table (export "t") i64 10 100 (ref null $t2) struct.new_default $t2) |
| 40 |
(memory (export "m") 10 15) |
| 41 |
(export "tt" (tag 0)) |
| 42 |
(export "g" (global 0)) |
| 43 |
) |
| 44 |
"#; |
| 45 |
smoke_test_imports_exports(test, 42); |
| 46 |
} |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn smoke_test_module_shape_without_gc_types() { |
| 50 |
let test = r#" |
| 51 |
(module |
| 52 |
(import "env" "fun" (func (param i32))) |
| 53 |
(import "env" "tag" (tag (param externref))) |
| 54 |
(import "env" "mem" (memory 1 10)) |
| 55 |
(import "env" "tbl" (table i64 10 10 funcref)) |
| 56 |
(import "env" "glb" (global (mut v128))) |
| 57 |
(func (export "f") (param i32) (result i64) unreachable) |
| 58 |
(memory (export "m") 10) |
| 59 |
(global (export "g") f64 f64.const 0.0) |
| 60 |
(table (export "t") 15 20 externref) |
| 61 |
(export "tt" (tag 0)) |
| 62 |
) |
| 63 |
"#; |
| 64 |
smoke_test_imports_exports(test, 43); |
| 65 |
} |
| 66 |
|
| 67 |
fn get_imports_exports( |
| 68 |
features: WasmFeatures, |
| 69 |
module: &[u8], |
| 70 |
) -> (Vec<WasmImport>, Vec<WasmExport>) { |
| 71 |
let mut validator = Validator::new_with_features(features); |
| 72 |
let types = validate(&mut validator, module); |
| 73 |
let types = types.as_ref(); |
| 74 |
let mut imports = vec![]; |
| 75 |
let mut exports = vec![]; |
| 76 |
for payload in Parser::new(0).parse_all(module) { |
| 77 |
let payload = payload.unwrap(); |
| 78 |
match payload { |
| 79 |
wasmparser::Payload::ImportSection(ir) => { |
| 80 |
for import in ir { |
| 81 |
let import = import.unwrap(); |
| 82 |
imports.push(WasmImport( |
| 83 |
import.module.to_string(), |
| 84 |
import.name.to_string(), |
| 85 |
types.entity_type_from_import(&import).unwrap(), |
| 86 |
)); |
| 87 |
} |
| 88 |
} |
| 89 |
wasmparser::Payload::ExportSection(er) => { |
| 90 |
for export in er { |
| 91 |
let export = export.unwrap(); |
| 92 |
exports.push(WasmExport( |
| 93 |
export.name.to_string(), |
| 94 |
types.entity_type_from_export(&export).unwrap(), |
| 95 |
)); |
| 96 |
} |
| 97 |
} |
| 98 |
_ => {} |
| 99 |
} |
| 100 |
} |
| 101 |
(imports, exports) |
| 102 |
} |
| 103 |
|
| 104 |
fn smoke_test_imports_exports(module_shape_test_case: &str, seed: u64) { |
| 105 |
let mut rng = SmallRng::seed_from_u64(seed); |
| 106 |
let mut buf = vec![0; 512]; |
| 107 |
let wasm = wat::parse_str(module_shape_test_case).unwrap(); |
| 108 |
let (expected_imports, expected_exports) = get_imports_exports(WasmFeatures::default(), &wasm); |
| 109 |
|
| 110 |
for _ in 0..1024 { |
| 111 |
rng.fill_bytes(&mut buf); |
| 112 |
let mut u = Unstructured::new(&buf); |
| 113 |
|
| 114 |
// Enable all standardized proposals. |
| 115 |
let mut config = Config::default(); |
| 116 |
config.max_memories = u.int_in_range(2..=5).unwrap(); |
| 117 |
config.module_shape = Some(wasm.clone()); |
| 118 |
|
| 119 |
let features = config.features(); |
| 120 |
let module = Module::new(config, &mut u).unwrap(); |
| 121 |
let wasm_bytes = module.to_bytes(); |
| 122 |
|
| 123 |
// The generated and expected imports/exports should share the same CoreTypeId, since |
| 124 |
// we copy all types, imports, and exports from the module-shape module into the target |
| 125 |
// module at the start of generation. |
| 126 |
let (generated_imports, generated_exports) = get_imports_exports(features, &wasm_bytes); |
| 127 |
assert_eq!(expected_imports, generated_imports); |
| 128 |
assert_eq!(expected_exports, generated_exports); |
| 129 |
} |
| 130 |
} |