1use anyhow::{Context, Result};
2use serde::de::DeserializeOwned;
3use serde_derive::Deserialize;
4use std::fmt;
5use std::fs;
6use std::path::Path;
7use std::path::PathBuf;
8
9pub mod limits {
19 pub const MEMORY_SIZE: usize = 805 << 16;
20 pub const MEMORIES: u32 = 450;
21 pub const TABLES: u32 = 200;
22 pub const MEMORIES_PER_MODULE: u32 = 9;
23 pub const TABLES_PER_MODULE: u32 = 5;
24 pub const COMPONENT_INSTANCES: u32 = 50;
25 pub const CORE_INSTANCES: u32 = 900;
26 pub const TABLE_ELEMENTS: usize = 1000;
27 pub const CORE_INSTANCE_SIZE: usize = 64 * 1024;
28 pub const TOTAL_STACKS: u32 = 10;
29}
30
31pub fn find_tests(root: &Path) -> Result<Vec<WastTest>> {
34 let mut tests = Vec::new();
35 add_tests(
36 &mut tests,
37 &root.join("tests/spec_testsuite"),
38 &FindConfig::Infer(spec_test_config),
39 )?;
40 add_tests(
41 &mut tests,
42 &root.join("tests/misc_testsuite"),
43 &FindConfig::InTest,
44 )?;
45 add_tests(
46 &mut tests,
47 &root.join("tests/component-model/test"),
48 &FindConfig::Infer(component_test_config),
49 )?;
50 Ok(tests)
51}
52
53enum FindConfig {
54 InTest,
55 Infer(fn(&Path) -> TestConfig),
56}
57
58fn add_tests(tests: &mut Vec<WastTest>, path: &Path, config: &FindConfig) -> Result<()> {
59 for entry in path.read_dir().context("failed to read directory")? {
60 let entry = entry.context("failed to read directory entry")?;
61 let path = entry.path();
62 if entry
63 .file_type()
64 .context("failed to get file type")?
65 .is_dir()
66 {
67 add_tests(tests, &path, config).context("failed to read sub-directory")?;
68 continue;
69 }
70
71 if path.extension().and_then(|s| s.to_str()) != Some("wast") {
72 continue;
73 }
74
75 let contents =
76 fs::read_to_string(&path).with_context(|| format!("failed to read test: {path:?}"))?;
77 let config = match config {
78 FindConfig::InTest => parse_test_config(&contents, ";;!")
79 .with_context(|| format!("failed to parse test configuration: {path:?}"))?,
80 FindConfig::Infer(f) => f(&path),
81 };
82 tests.push(WastTest {
83 path,
84 contents,
85 config,
86 })
87 }
88 Ok(())
89}
90
91fn spec_test_config(test: &Path) -> TestConfig {
92 let mut ret = TestConfig::default();
93 ret.spec_test = Some(true);
94 match spec_proposal_from_path(test) {
95 Some("wide-arithmetic") => {
96 ret.wide_arithmetic = Some(true);
97 }
98 Some("threads") => {
99 ret.threads = Some(true);
100 ret.reference_types = Some(false);
101 }
102 Some("relaxed-simd") => {
103 ret.relaxed_simd = Some(true);
104 }
105 Some("custom-page-sizes") => {
106 ret.custom_page_sizes = Some(true);
107 ret.multi_memory = Some(true);
108 ret.memory64 = Some(true);
109
110 if test.ends_with("memory_max.wast") || test.ends_with("memory_max_i64.wast") {
113 ret.hogs_memory = Some(true);
114 }
115 }
116 Some("annotations") => {
117 ret.simd = Some(true);
118 }
119 Some("wasm-3.0") => {
120 ret.simd = Some(true);
121 ret.relaxed_simd = Some(true);
122 ret.multi_memory = Some(true);
123 ret.gc = Some(true);
124 ret.reference_types = Some(true);
125 ret.memory64 = Some(true);
126 ret.tail_call = Some(true);
127 ret.extended_const = Some(true);
128 ret.exceptions = Some(true);
129
130 if test.parent().unwrap().ends_with("legacy") {
131 ret.legacy_exceptions = Some(true);
132 }
133
134 if test.ends_with("memory.wast")
143 || test.ends_with("table.wast")
144 || test.ends_with("memory64.wast")
145 {
146 ret.hogs_memory = Some(true);
147 }
148 }
149 Some(proposal) => panic!("unsuported proposal {proposal:?}"),
150 None => {
151 ret.reference_types = Some(true);
152 ret.simd = Some(true);
153 }
154 }
155
156 ret
157}
158
159fn component_test_config(test: &Path) -> TestConfig {
160 let mut ret = TestConfig::default();
161 ret.spec_test = Some(true);
162 ret.reference_types = Some(true);
163 ret.multi_memory = Some(true);
164
165 if let Some(parent) = test.parent() {
166 if parent.ends_with("async") {
167 ret.component_model_async = Some(true);
168 ret.component_model_async_builtins = Some(true);
169 }
170 }
171
172 ret
173}
174
175pub fn parse_test_config<T>(wat: &str, comment: &'static str) -> Result<T>
178where
179 T: DeserializeOwned,
180{
181 let config_lines: Vec<_> = wat
184 .lines()
185 .take_while(|l| l.starts_with(comment))
186 .map(|l| &l[comment.len()..])
187 .collect();
188 let config_text = config_lines.join("\n");
189
190 toml::from_str(&config_text).context("failed to parse the test configuration")
191}
192
193#[derive(Clone)]
195pub struct WastTest {
196 pub path: PathBuf,
197 pub contents: String,
198 pub config: TestConfig,
199}
200
201impl fmt::Debug for WastTest {
202 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203 f.debug_struct("WastTest")
204 .field("path", &self.path)
205 .field("contents", &"...")
206 .field("config", &self.config)
207 .finish()
208 }
209}
210
211macro_rules! foreach_config_option {
212 ($m:ident) => {
213 $m! {
214 memory64
215 custom_page_sizes
216 multi_memory
217 threads
218 shared_everything_threads
219 gc
220 function_references
221 relaxed_simd
222 reference_types
223 tail_call
224 extended_const
225 wide_arithmetic
226 hogs_memory
227 nan_canonicalization
228 component_model_async
229 component_model_async_builtins
230 component_model_async_stackful
231 component_model_error_context
232 component_model_gc
233 simd
234 gc_types
235 exceptions
236 legacy_exceptions
237 stack_switching
238 spec_test
239 }
240 };
241}
242
243macro_rules! define_test_config {
244 ($($option:ident)*) => {
245 #[derive(Debug, PartialEq, Default, Deserialize, Clone)]
248 #[serde(deny_unknown_fields)]
249 pub struct TestConfig {
250 $(pub $option: Option<bool>,)*
251 }
252
253 impl TestConfig {
254 $(
255 pub fn $option(&self) -> bool {
256 self.$option.unwrap_or(false)
257 }
258 )*
259 }
260 }
261}
262
263foreach_config_option!(define_test_config);
264
265impl TestConfig {
266 pub fn options_mut(&mut self) -> impl Iterator<Item = (&'static str, &mut Option<bool>)> {
268 macro_rules! mk {
269 ($($option:ident)*) => {
270 [
271 $((stringify!($option), &mut self.$option),)*
272 ].into_iter()
273 }
274 }
275 foreach_config_option!(mk)
276 }
277}
278
279#[derive(Debug)]
281pub struct WastConfig {
282 pub compiler: Compiler,
284 pub pooling: bool,
286 pub collector: Collector,
288}
289
290#[derive(PartialEq, Debug, Copy, Clone)]
292pub enum Compiler {
293 CraneliftNative,
300
301 Winch,
306
307 CraneliftPulley,
314}
315
316impl Compiler {
317 pub fn should_fail(&self, config: &TestConfig) -> bool {
328 match self {
329 Compiler::CraneliftNative => config.legacy_exceptions(),
330
331 Compiler::Winch => {
332 if config.gc()
333 || config.tail_call()
334 || config.function_references()
335 || config.gc()
336 || config.relaxed_simd()
337 || config.gc_types()
338 || config.exceptions()
339 || config.legacy_exceptions()
340 || config.stack_switching()
341 || config.legacy_exceptions()
342 || config.component_model_async()
343 {
344 return true;
345 }
346
347 if cfg!(target_arch = "aarch64") {
348 return config.wide_arithmetic()
349 || (config.simd() && !config.spec_test())
350 || config.threads();
351 }
352
353 !cfg!(target_arch = "x86_64")
354 }
355
356 Compiler::CraneliftPulley => {
357 config.threads() || config.legacy_exceptions() || config.stack_switching()
358 }
359 }
360 }
361
362 pub fn supports_host(&self) -> bool {
365 match self {
366 Compiler::CraneliftNative => {
367 cfg!(target_arch = "x86_64")
368 || cfg!(target_arch = "aarch64")
369 || cfg!(target_arch = "riscv64")
370 || cfg!(target_arch = "s390x")
371 }
372 Compiler::Winch => cfg!(target_arch = "x86_64") || cfg!(target_arch = "aarch64"),
373 Compiler::CraneliftPulley => true,
374 }
375 }
376}
377
378#[derive(PartialEq, Debug, Copy, Clone)]
379pub enum Collector {
380 Auto,
381 Null,
382 DeferredReferenceCounting,
383}
384
385impl WastTest {
386 pub fn test_uses_gc_types(&self) -> bool {
389 self.config.gc() || self.config.function_references()
390 }
391
392 pub fn spec_proposal(&self) -> Option<&str> {
394 spec_proposal_from_path(&self.path)
395 }
396
397 pub fn should_fail(&self, config: &WastConfig) -> bool {
400 if !config.compiler.supports_host() {
401 return true;
402 }
403
404 if config.pooling {
406 let unsupported = [
407 "misc_testsuite/memory64/more-than-4gb.wast",
409 "misc_testsuite/memory-combos.wast",
411 "misc_testsuite/threads/LB.wast",
412 "misc_testsuite/threads/LB_atomic.wast",
413 "misc_testsuite/threads/MP.wast",
414 "misc_testsuite/threads/MP_atomic.wast",
415 "misc_testsuite/threads/MP_wait.wast",
416 "misc_testsuite/threads/SB.wast",
417 "misc_testsuite/threads/SB_atomic.wast",
418 "misc_testsuite/threads/atomics_notify.wast",
419 "misc_testsuite/threads/atomics_wait_address.wast",
420 "misc_testsuite/threads/wait_notify.wast",
421 "spec_testsuite/proposals/threads/atomic.wast",
422 "spec_testsuite/proposals/threads/exports.wast",
423 "spec_testsuite/proposals/threads/memory.wast",
424 ];
425
426 if unsupported.iter().any(|part| self.path.ends_with(part)) {
427 return true;
428 }
429 }
430
431 if config.compiler.should_fail(&self.config) {
432 return true;
433 }
434
435 if config.compiler == Compiler::Winch {
437 let unsupported = [
439 "extended-const/elem.wast",
440 "extended-const/global.wast",
441 "misc_testsuite/component-model/modules.wast",
442 "misc_testsuite/externref-id-function.wast",
443 "misc_testsuite/externref-segment.wast",
444 "misc_testsuite/externref-segments.wast",
445 "misc_testsuite/externref-table-dropped-segment-issue-8281.wast",
446 "misc_testsuite/linking-errors.wast",
447 "misc_testsuite/many_table_gets_lead_to_gc.wast",
448 "misc_testsuite/mutable_externref_globals.wast",
449 "misc_testsuite/no-mixup-stack-maps.wast",
450 "misc_testsuite/no-panic.wast",
451 "misc_testsuite/simple_ref_is_null.wast",
452 "misc_testsuite/table_grow_with_funcref.wast",
453 "spec_testsuite/br_table.wast",
454 "spec_testsuite/global.wast",
455 "spec_testsuite/ref_func.wast",
456 "spec_testsuite/ref_is_null.wast",
457 "spec_testsuite/ref_null.wast",
458 "spec_testsuite/select.wast",
459 "spec_testsuite/table_fill.wast",
460 "spec_testsuite/table_get.wast",
461 "spec_testsuite/table_grow.wast",
462 "spec_testsuite/table_set.wast",
463 "spec_testsuite/table_size.wast",
464 "spec_testsuite/elem.wast",
465 "spec_testsuite/linking.wast",
466 ];
467
468 if unsupported.iter().any(|part| self.path.ends_with(part)) {
469 return true;
470 }
471
472 #[cfg(target_arch = "aarch64")]
473 {
474 let unsupported = [
475 "misc_testsuite/int-to-float-splat.wast",
476 "misc_testsuite/issue6562.wast",
477 "misc_testsuite/memory64/simd.wast",
478 "misc_testsuite/simd/almost-extmul.wast",
479 "misc_testsuite/simd/canonicalize-nan.wast",
480 "misc_testsuite/simd/cvt-from-uint.wast",
481 "misc_testsuite/simd/edge-of-memory.wast",
482 "misc_testsuite/simd/interesting-float-splat.wast",
483 "misc_testsuite/simd/issue4807.wast",
484 "misc_testsuite/simd/issue6725-no-egraph-panic.wast",
485 "misc_testsuite/simd/issue_3173_select_v128.wast",
486 "misc_testsuite/simd/issue_3327_bnot_lowering.wast",
487 "misc_testsuite/simd/load_splat_out_of_bounds.wast",
488 "misc_testsuite/simd/replace-lane-preserve.wast",
489 "misc_testsuite/simd/spillslot-size-fuzzbug.wast",
490 "misc_testsuite/simd/sse-cannot-fold-unaligned-loads.wast",
491 "misc_testsuite/simd/unaligned-load.wast",
492 "misc_testsuite/simd/v128-select.wast",
493 "misc_testsuite/winch/issue-10331.wast",
494 "misc_testsuite/winch/issue-10357.wast",
495 "misc_testsuite/winch/issue-10460.wast",
496 "misc_testsuite/winch/replace_lane.wast",
497 "misc_testsuite/winch/simd_multivalue.wast",
498 "misc_testsuite/winch/v128_load_lane_invalid_address.wast",
499 "spec_testsuite/proposals/annotations/simd_lane.wast",
500 "spec_testsuite/proposals/multi-memory/simd_memory-multi.wast",
501 "spec_testsuite/simd_address.wast",
502 "spec_testsuite/simd_align.wast",
503 "spec_testsuite/simd_bit_shift.wast",
504 "spec_testsuite/simd_bitwise.wast",
505 "spec_testsuite/simd_boolean.wast",
506 "spec_testsuite/simd_const.wast",
507 "spec_testsuite/simd_conversions.wast",
508 "spec_testsuite/simd_f32x4.wast",
509 "spec_testsuite/simd_f32x4_arith.wast",
510 "spec_testsuite/simd_f32x4_cmp.wast",
511 "spec_testsuite/simd_f32x4_pmin_pmax.wast",
512 "spec_testsuite/simd_f32x4_rounding.wast",
513 "spec_testsuite/simd_f64x2.wast",
514 "spec_testsuite/simd_f64x2_arith.wast",
515 "spec_testsuite/simd_f64x2_cmp.wast",
516 "spec_testsuite/simd_f64x2_pmin_pmax.wast",
517 "spec_testsuite/simd_f64x2_rounding.wast",
518 "spec_testsuite/simd_i16x8_arith.wast",
519 "spec_testsuite/simd_i16x8_arith2.wast",
520 "spec_testsuite/simd_i16x8_cmp.wast",
521 "spec_testsuite/simd_i16x8_extadd_pairwise_i8x16.wast",
522 "spec_testsuite/simd_i16x8_extmul_i8x16.wast",
523 "spec_testsuite/simd_i16x8_q15mulr_sat_s.wast",
524 "spec_testsuite/simd_i16x8_sat_arith.wast",
525 "spec_testsuite/simd_i32x4_arith.wast",
526 "spec_testsuite/simd_i32x4_arith2.wast",
527 "spec_testsuite/simd_i32x4_cmp.wast",
528 "spec_testsuite/simd_i32x4_dot_i16x8.wast",
529 "spec_testsuite/simd_i32x4_extadd_pairwise_i16x8.wast",
530 "spec_testsuite/simd_i32x4_extmul_i16x8.wast",
531 "spec_testsuite/simd_i32x4_trunc_sat_f32x4.wast",
532 "spec_testsuite/simd_i32x4_trunc_sat_f64x2.wast",
533 "spec_testsuite/simd_i64x2_arith.wast",
534 "spec_testsuite/simd_i64x2_arith2.wast",
535 "spec_testsuite/simd_i64x2_cmp.wast",
536 "spec_testsuite/simd_i64x2_extmul_i32x4.wast",
537 "spec_testsuite/simd_i8x16_arith.wast",
538 "spec_testsuite/simd_i8x16_arith2.wast",
539 "spec_testsuite/simd_i8x16_cmp.wast",
540 "spec_testsuite/simd_i8x16_sat_arith.wast",
541 "spec_testsuite/simd_int_to_int_extend.wast",
542 "spec_testsuite/simd_lane.wast",
543 "spec_testsuite/simd_load.wast",
544 "spec_testsuite/simd_load16_lane.wast",
545 "spec_testsuite/simd_load32_lane.wast",
546 "spec_testsuite/simd_load64_lane.wast",
547 "spec_testsuite/simd_load8_lane.wast",
548 "spec_testsuite/simd_load_extend.wast",
549 "spec_testsuite/simd_load_splat.wast",
550 "spec_testsuite/simd_load_zero.wast",
551 "spec_testsuite/simd_select.wast",
552 "spec_testsuite/simd_splat.wast",
553 "spec_testsuite/simd_store.wast",
554 "spec_testsuite/simd_store16_lane.wast",
555 "spec_testsuite/simd_store32_lane.wast",
556 "spec_testsuite/simd_store64_lane.wast",
557 "spec_testsuite/simd_store8_lane.wast",
558 ];
559
560 if unsupported.iter().any(|part| self.path.ends_with(part)) {
561 return true;
562 }
563 }
564
565 #[cfg(target_arch = "x86_64")]
566 {
567 let unsupported = [
568 "misc_testsuite/simd/canonicalize-nan.wast",
571 ];
572
573 if unsupported.iter().any(|part| self.path.ends_with(part)) {
574 return true;
575 }
576
577 #[cfg(target_arch = "x86_64")]
579 if !(std::is_x86_feature_detected!("avx") && std::is_x86_feature_detected!("avx2"))
580 {
581 let unsupported = [
582 "annotations/simd_lane.wast",
583 "memory64/simd.wast",
584 "misc_testsuite/int-to-float-splat.wast",
585 "misc_testsuite/issue6562.wast",
586 "misc_testsuite/simd/almost-extmul.wast",
587 "misc_testsuite/simd/cvt-from-uint.wast",
588 "misc_testsuite/simd/edge-of-memory.wast",
589 "misc_testsuite/simd/issue_3327_bnot_lowering.wast",
590 "misc_testsuite/simd/issue6725-no-egraph-panic.wast",
591 "misc_testsuite/simd/replace-lane-preserve.wast",
592 "misc_testsuite/simd/spillslot-size-fuzzbug.wast",
593 "misc_testsuite/simd/sse-cannot-fold-unaligned-loads.wast",
594 "misc_testsuite/winch/issue-10331.wast",
595 "misc_testsuite/winch/replace_lane.wast",
596 "spec_testsuite/simd_align.wast",
597 "spec_testsuite/simd_boolean.wast",
598 "spec_testsuite/simd_conversions.wast",
599 "spec_testsuite/simd_f32x4.wast",
600 "spec_testsuite/simd_f32x4_arith.wast",
601 "spec_testsuite/simd_f32x4_cmp.wast",
602 "spec_testsuite/simd_f32x4_pmin_pmax.wast",
603 "spec_testsuite/simd_f32x4_rounding.wast",
604 "spec_testsuite/simd_f64x2.wast",
605 "spec_testsuite/simd_f64x2_arith.wast",
606 "spec_testsuite/simd_f64x2_cmp.wast",
607 "spec_testsuite/simd_f64x2_pmin_pmax.wast",
608 "spec_testsuite/simd_f64x2_rounding.wast",
609 "spec_testsuite/simd_i16x8_cmp.wast",
610 "spec_testsuite/simd_i32x4_cmp.wast",
611 "spec_testsuite/simd_i64x2_arith2.wast",
612 "spec_testsuite/simd_i64x2_cmp.wast",
613 "spec_testsuite/simd_i8x16_arith2.wast",
614 "spec_testsuite/simd_i8x16_cmp.wast",
615 "spec_testsuite/simd_int_to_int_extend.wast",
616 "spec_testsuite/simd_load.wast",
617 "spec_testsuite/simd_load_extend.wast",
618 "spec_testsuite/simd_load_splat.wast",
619 "spec_testsuite/simd_load_zero.wast",
620 "spec_testsuite/simd_splat.wast",
621 "spec_testsuite/simd_store16_lane.wast",
622 "spec_testsuite/simd_store32_lane.wast",
623 "spec_testsuite/simd_store64_lane.wast",
624 "spec_testsuite/simd_store8_lane.wast",
625 "spec_testsuite/simd_load16_lane.wast",
626 "spec_testsuite/simd_load32_lane.wast",
627 "spec_testsuite/simd_load64_lane.wast",
628 "spec_testsuite/simd_load8_lane.wast",
629 "spec_testsuite/simd_bitwise.wast",
630 "misc_testsuite/simd/load_splat_out_of_bounds.wast",
631 "misc_testsuite/simd/unaligned-load.wast",
632 "multi-memory/simd_memory-multi.wast",
633 "misc_testsuite/simd/issue4807.wast",
634 "spec_testsuite/simd_const.wast",
635 "spec_testsuite/simd_i8x16_sat_arith.wast",
636 "spec_testsuite/simd_i64x2_arith.wast",
637 "spec_testsuite/simd_i16x8_arith.wast",
638 "spec_testsuite/simd_i16x8_arith2.wast",
639 "spec_testsuite/simd_i16x8_q15mulr_sat_s.wast",
640 "spec_testsuite/simd_i16x8_sat_arith.wast",
641 "spec_testsuite/simd_i32x4_arith.wast",
642 "spec_testsuite/simd_i32x4_dot_i16x8.wast",
643 "spec_testsuite/simd_i32x4_trunc_sat_f32x4.wast",
644 "spec_testsuite/simd_i32x4_trunc_sat_f64x2.wast",
645 "spec_testsuite/simd_i8x16_arith.wast",
646 "spec_testsuite/simd_bit_shift.wast",
647 "spec_testsuite/simd_lane.wast",
648 "spec_testsuite/simd_i16x8_extmul_i8x16.wast",
649 "spec_testsuite/simd_i32x4_extmul_i16x8.wast",
650 "spec_testsuite/simd_i64x2_extmul_i32x4.wast",
651 "spec_testsuite/simd_i16x8_extadd_pairwise_i8x16.wast",
652 "spec_testsuite/simd_i32x4_extadd_pairwise_i16x8.wast",
653 "spec_testsuite/simd_i32x4_arith2.wast",
654 ];
655
656 if unsupported.iter().any(|part| self.path.ends_with(part)) {
657 return true;
658 }
659 }
660 }
661 }
662
663 let failing_component_model_tests = [
664 "component-model/test/values/trap-in-post-return.wast",
666 ];
667 if failing_component_model_tests
668 .iter()
669 .any(|part| self.path.ends_with(part))
670 {
671 return true;
672 }
673
674 false
675 }
676}
677
678fn spec_proposal_from_path(path: &Path) -> Option<&str> {
679 let mut iter = path.iter();
680 loop {
681 match iter.next()?.to_str()? {
682 "proposals" => break,
683 _ => {}
684 }
685 }
686 Some(iter.next()?.to_str()?)
687}