Debian Codegraph @

crates/wasm-smith/tests/exports.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #![cfg(feature = "wasmparser")]
2
3 use arbitrary::Unstructured;
4 use rand::{RngCore, SeedableRng, rngs::SmallRng};
5 use wasm_smith::{Config, Module};
6 use wasmparser::{
7 CompositeType, FuncType, GlobalType, MemoryType, Parser, TableType, Validator, WasmFeatures,
8 types::EntityType,
9 };
10
11 mod common;
12 use common::validate;
13
14 #[derive(Debug, PartialEq)]
15 enum ExportType {
16 Func(FuncType),
17 Global(GlobalType),
18 Memory(MemoryType),
19 Table(TableType),
20 Tag(FuncType),
21 }
22
23 #[test]
24 fn smoke_test_single_export() {
25 let test = r#"
26 (module
27 (func (export "foo") (param i32) (result i64)
28 unreachable
29 )
30 )
31 "#;
32 smoke_test_exports(test, 11)
33 }
34
35 #[test]
36 fn smoke_test_multiple_exports() {
37 let test = r#"
38 (module
39 (func (export "a") (param i32) (result i64)
40 unreachable
41 )
42 (func (export "b")
43 unreachable
44 )
45 (func (export "c")
46 unreachable
47 )
48 )
49 "#;
50 smoke_test_exports(test, 12)
51 }
52
53 #[test]
54 fn smoke_test_exported_global() {
55 let test = r#"
56 (module
57 (func (export "a") (param i32 i32 f32 f64) (result f32)
58 unreachable
59 )
60 (global (export "glob") f64 f64.const 0)
61 )
62 "#;
63 smoke_test_exports(test, 20)
64 }
65
66 #[test]
67 fn smoke_test_export_with_imports() {
68 let test = r#"
69 (module
70 (import "" "foo" (func (param i32)))
71 (import "" "bar" (global (mut f32)))
72 (import "" "mem" (memory 5))
73 (import "" "baz" (table 1 10 funcref))
74 (import "" "qux" (tag (param i32 f64)))
75 (func (param i64) unreachable)
76 (global i32 (i32.const 0))
77 (memory 10)
78 (table 10 (ref null any))
79 (tag (param f32 v128))
80 (export "a" (func 0))
81 (export "b" (global 0))
82 (export "c" (memory 0))
83 (export "d" (table 0))
84 (export "e" (tag 0))
85 (export "f" (func 1))
86 (export "g" (global 1))
87 (export "h" (memory 1))
88 (export "i" (table 1))
89 (export "j" (tag 1))
90 )
91 "#;
92 smoke_test_exports(test, 21)
93 }
94
95 #[test]
96 fn smoke_test_with_mutable_global_exports() {
97 let test = r#"
98 (module
99 (global (export "1i32") (mut i32) (i32.const 0))
100 (global (export "2i32") (mut i32) (i32.const 0))
101 (global (export "1i64") (mut i64) (i64.const 0))
102 (global (export "2i64") (mut i64) (i64.const 0))
103 (global (export "3i32") (mut i32) (i32.const 0))
104 (global (export "3i64") (mut i64) (i64.const 0))
105 (global (export "4i32") i32 (i32.const 0))
106 (global (export "4i64") i64 (i64.const 0))
107 )"#;
108 smoke_test_exports(test, 22)
109 }
110
111 #[test]
112 fn smoke_test_all_exports() {
113 let test = r#"
114 (module
115 (func (export "foo") (param i32) (result i64) unreachable)
116 (global (export "bar") f32 f32.const 0)
117 (memory (export "baz") 1 10)
118 (table (export "qux") 5 10 (ref null extern))
119 (tag (export "quux") (param f32))
120 )
121 "#;
122 smoke_test_exports(test, 23);
123 }
124
125 fn get_exports(features: WasmFeatures, module: &[u8]) -> Vec<(String, ExportType)> {
126 let mut validator = Validator::new_with_features(features);
127 let types = validate(&mut validator, module);
128 let types = types.as_ref();
129 let mut exports = vec![];
130 for payload in Parser::new(0).parse_all(module) {
131 let payload = payload.unwrap();
132 if let wasmparser::Payload::ExportSection(rdr) = payload {
133 for export in rdr {
134 let export = export.unwrap();
135 match types.entity_type_from_export(&export).unwrap() {
136 EntityType::Func(core_id) => {
137 let sub_type = types.get(core_id).expect("Failed to lookup core id");
138 assert!(sub_type.is_final);
139 assert!(sub_type.supertype_idx.is_none());
140 let CompositeType {
141 inner: wasmparser::CompositeInnerType::Func(func_type),
142 ..
143 } = &sub_type.composite_type
144 else {
145 panic!("Expected Func CompositeType, but found {sub_type:?}");
146 };
147 exports
148 .push((export.name.to_string(), ExportType::Func(func_type.clone())));
149 }
150 EntityType::Global(global_type) => {
151 exports.push((export.name.to_string(), ExportType::Global(global_type)));
152 }
153 EntityType::Memory(memory_type) => {
154 exports.push((export.name.to_string(), ExportType::Memory(memory_type)));
155 }
156 EntityType::Table(table_type) => {
157 exports.push((export.name.to_string(), ExportType::Table(table_type)));
158 }
159 EntityType::Tag(core_id) => {
160 let sub_type = types.get(core_id).expect("Failed to lookup core id");
161 assert!(sub_type.is_final);
162 assert!(sub_type.supertype_idx.is_none());
163 let CompositeType {
164 inner: wasmparser::CompositeInnerType::Func(func_type),
165 ..
166 } = &sub_type.composite_type
167 else {
168 panic!("Expected Func CompositeType, but found {sub_type:?}");
169 };
170 assert!(func_type.results().is_empty());
171 exports.push((export.name.to_string(), ExportType::Tag(func_type.clone())));
172 }
173 }
174 }
175 }
176 }
177 exports
178 }
179
180 fn smoke_test_exports(exports_test_case: &str, seed: u64) {
181 let mut rng = SmallRng::seed_from_u64(seed);
182 let mut buf = vec![0; 512];
183 let wasm = wat::parse_str(exports_test_case).unwrap();
184 let expected_exports = get_exports(WasmFeatures::default(), &wasm);
185
186 for _ in 0..1024 {
187 rng.fill_bytes(&mut buf);
188 let mut u = Unstructured::new(&buf);
189
190 // Enable all standardized proposals.
191 let mut config = Config::default();
192 config.max_memories = u.int_in_range(2..=5).unwrap();
193 config.exports = Some(wasm.clone());
194
195 let features = config.features();
196 let module = Module::new(config, &mut u).unwrap();
197 let wasm_bytes = module.to_bytes();
198
199 let generated_exports = get_exports(features, &wasm_bytes);
200 assert_eq!(expected_exports, generated_exports);
201 }
202 }