| 1 |
#![cfg(feature = "wasmparser")] |
| 2 |
|
| 3 |
use arbitrary::{Arbitrary, Unstructured}; |
| 4 |
use rand::{rngs::SmallRng, RngCore, SeedableRng}; |
| 5 |
use wasm_smith::{Config, Module}; |
| 6 |
use wasmparser::{ |
| 7 |
types::EntityType, CompositeType, FuncType, GlobalType, Parser, Validator, WasmFeatures, |
| 8 |
}; |
| 9 |
|
| 10 |
mod common; |
| 11 |
use common::{parser_features_from_config, validate}; |
| 12 |
|
| 13 |
#[derive(Debug, PartialEq)] |
| 14 |
enum ExportType { |
| 15 |
Func(FuncType), |
| 16 |
Global(GlobalType), |
| 17 |
} |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn smoke_test_single_export() { |
| 21 |
let test = r#" |
| 22 |
(module |
| 23 |
(func (export "foo") (param i32) (result i64) |
| 24 |
unreachable |
| 25 |
) |
| 26 |
) |
| 27 |
"#; |
| 28 |
smoke_test_exports(test, 11) |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn smoke_test_multiple_exports() { |
| 33 |
let test = r#" |
| 34 |
(module |
| 35 |
(func (export "a") (param i32) (result i64) |
| 36 |
unreachable |
| 37 |
) |
| 38 |
(func (export "b") |
| 39 |
unreachable |
| 40 |
) |
| 41 |
(func (export "c") |
| 42 |
unreachable |
| 43 |
) |
| 44 |
) |
| 45 |
"#; |
| 46 |
smoke_test_exports(test, 12) |
| 47 |
} |
| 48 |
|
| 49 |
#[test] |
| 50 |
fn smoke_test_exported_global() { |
| 51 |
let test = r#" |
| 52 |
(module |
| 53 |
(func (export "a") (param i32 i32 f32 f64) (result f32) |
| 54 |
unreachable |
| 55 |
) |
| 56 |
(global (export "glob") f64 f64.const 0) |
| 57 |
) |
| 58 |
"#; |
| 59 |
smoke_test_exports(test, 20) |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn smoke_test_export_with_imports() { |
| 64 |
let test = r#" |
| 65 |
(module |
| 66 |
(import "" "foo" (func (param i32))) |
| 67 |
(import "" "bar" (global (mut f32))) |
| 68 |
(func (param i64) unreachable) |
| 69 |
(global i32 (i32.const 0)) |
| 70 |
(export "a" (func 0)) |
| 71 |
(export "b" (global 0)) |
| 72 |
(export "c" (func 1)) |
| 73 |
(export "d" (global 1)) |
| 74 |
) |
| 75 |
"#; |
| 76 |
smoke_test_exports(test, 21) |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn smoke_test_with_mutable_global_exports() { |
| 81 |
let test = r#" |
| 82 |
(module |
| 83 |
(global (export "1i32") (mut i32) (i32.const 0)) |
| 84 |
(global (export "2i32") (mut i32) (i32.const 0)) |
| 85 |
(global (export "1i64") (mut i64) (i64.const 0)) |
| 86 |
(global (export "2i64") (mut i64) (i64.const 0)) |
| 87 |
(global (export "3i32") (mut i32) (i32.const 0)) |
| 88 |
(global (export "3i64") (mut i64) (i64.const 0)) |
| 89 |
(global (export "4i32") i32 (i32.const 0)) |
| 90 |
(global (export "4i64") i64 (i64.const 0)) |
| 91 |
)"#; |
| 92 |
smoke_test_exports(test, 22) |
| 93 |
} |
| 94 |
|
| 95 |
fn get_func_and_global_exports(features: WasmFeatures, module: &[u8]) -> Vec<(String, ExportType)> { |
| 96 |
let mut validator = Validator::new_with_features(features); |
| 97 |
let types = validate(&mut validator, module); |
| 98 |
let types = types.as_ref(); |
| 99 |
let mut exports = vec![]; |
| 100 |
|
| 101 |
for payload in Parser::new(0).parse_all(module) { |
| 102 |
let payload = payload.unwrap(); |
| 103 |
if let wasmparser::Payload::ExportSection(rdr) = payload { |
| 104 |
for export in rdr { |
| 105 |
let export = export.unwrap(); |
| 106 |
match types.entity_type_from_export(&export).unwrap() { |
| 107 |
EntityType::Func(core_id) => { |
| 108 |
let sub_type = types.get(core_id).expect("Failed to lookup core id"); |
| 109 |
assert!(sub_type.is_final); |
| 110 |
assert!(sub_type.supertype_idx.is_none()); |
| 111 |
let CompositeType { |
| 112 |
inner: wasmparser::CompositeInnerType::Func(func_type), |
| 113 |
.. |
| 114 |
} = &sub_type.composite_type |
| 115 |
else { |
| 116 |
panic!("Expected Func CompositeType, but found {:?}", sub_type); |
| 117 |
}; |
| 118 |
exports |
| 119 |
.push((export.name.to_string(), ExportType::Func(func_type.clone()))); |
| 120 |
} |
| 121 |
EntityType::Global(global_type) => { |
| 122 |
exports.push((export.name.to_string(), ExportType::Global(global_type))) |
| 123 |
} |
| 124 |
other => { |
| 125 |
panic!("Unexpected entity type {:?}", other) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
exports |
| 132 |
} |
| 133 |
|
| 134 |
fn smoke_test_exports(exports_test_case: &str, seed: u64) { |
| 135 |
let mut rng = SmallRng::seed_from_u64(seed); |
| 136 |
let mut buf = vec![0; 512]; |
| 137 |
let wasm = wat::parse_str(exports_test_case).unwrap(); |
| 138 |
let expected_exports = get_func_and_global_exports(WasmFeatures::default(), &wasm); |
| 139 |
|
| 140 |
for _ in 0..1024 { |
| 141 |
rng.fill_bytes(&mut buf); |
| 142 |
let mut u = Unstructured::new(&buf); |
| 143 |
|
| 144 |
let mut config = Config::arbitrary(&mut u).expect("arbitrary config"); |
| 145 |
config.exports = Some(wasm.clone()); |
| 146 |
|
| 147 |
let features = parser_features_from_config(&config); |
| 148 |
let module = Module::new(config, &mut u).unwrap(); |
| 149 |
let wasm_bytes = module.to_bytes(); |
| 150 |
|
| 151 |
let generated_exports = get_func_and_global_exports(features, &wasm_bytes); |
| 152 |
assert_eq!(expected_exports, generated_exports); |
| 153 |
} |
| 154 |
} |