1use serde::de::DeserializeOwned;
2use serde_derive::Deserialize;
3use std::fmt;
4use std::fs;
5use std::path::Path;
6use std::path::PathBuf;
7use wasmtime_environ::prelude::*;
8
9pub mod limits {
19 pub const MEMORY_SIZE: usize = 805 << 16;
20 pub const MEMORIES: u32 = 450;
21 pub const GC_HEAP_SIZE: usize = 10 << 16;
22 pub const TABLES: u32 = 200;
23 pub const MEMORIES_PER_MODULE: u32 = 9;
24 pub const TABLES_PER_MODULE: u32 = 5;
25 pub const COMPONENT_INSTANCES: u32 = 50;
26 pub const CORE_INSTANCES: u32 = 900;
27 pub const TABLE_ELEMENTS: usize = 1000;
28 pub const CORE_INSTANCE_SIZE: usize = 64 * 1024;
29 pub const TOTAL_STACKS: u32 = 20;
30}
31
32pub fn find_tests(root: &Path) -> Result<Vec<WastTest>> {
35 let mut tests = Vec::new();
36
37 let spec_tests = root.join("tests/spec_testsuite");
38 add_tests(
39 &mut tests,
40 &spec_tests,
41 &FindConfig::Infer(spec_test_config),
42 )
43 .context("Do you need to `git submodule update --init`?")
44 .with_context(|| format!("failed to add tests from `{}`", spec_tests.display()))?;
45
46 let misc_tests = root.join("tests/misc_testsuite");
47 add_tests(&mut tests, &misc_tests, &FindConfig::InTest)
48 .with_context(|| format!("failed to add tests from `{}`", misc_tests.display()))?;
49
50 let cm_tests = root.join("tests/component-model/test");
51 add_tests(
52 &mut tests,
53 &cm_tests,
54 &FindConfig::Infer(component_test_config),
55 )
56 .context("Do you need to `git submodule update --init`?")
57 .with_context(|| format!("failed to add tests from `{}`", cm_tests.display()))?;
58
59 {
62 let skip_list = &[
63 ];
65 tests.retain(|test| {
66 test.path
67 .file_name()
68 .and_then(|name| name.to_str())
69 .map(|name| !skip_list.contains(&name))
70 .unwrap_or(true)
71 });
72 }
73
74 Ok(tests)
75}
76
77enum FindConfig {
78 InTest,
79 Infer(fn(&Path) -> TestConfig),
80}
81
82fn add_tests(tests: &mut Vec<WastTest>, path: &Path, config: &FindConfig) -> Result<()> {
83 for entry in path.read_dir().context("failed to read directory")? {
84 let entry = entry.context("failed to read directory entry")?;
85 let path = entry.path();
86 if entry
87 .file_type()
88 .context("failed to get file type")?
89 .is_dir()
90 {
91 add_tests(tests, &path, config).context("failed to read sub-directory")?;
92 continue;
93 }
94
95 if path.extension().and_then(|s| s.to_str()) != Some("wast") {
96 continue;
97 }
98
99 if path.ends_with("spec_testsuite/custom/custom_annot.wast")
102 || path.ends_with("spec_testsuite/custom/branch_hint.wast")
103 || path.ends_with("spec_testsuite/custom/name_annot.wast")
104 {
105 continue;
106 }
107
108 let contents =
109 fs::read_to_string(&path).with_context(|| format!("failed to read test: {path:?}"))?;
110 let config = match config {
111 FindConfig::InTest => parse_test_config(&contents, ";;!")
112 .with_context(|| format!("failed to parse test configuration: {path:?}"))?,
113 FindConfig::Infer(f) => f(&path),
114 };
115 tests.push(WastTest {
116 path,
117 contents,
118 config,
119 })
120 }
121 Ok(())
122}
123
124fn spec_test_config(test: &Path) -> TestConfig {
125 let mut ret = TestConfig::default();
126 ret.spec_test = Some(true);
127 ret.bulk_memory = Some(true);
128 match spec_proposal_from_path(test) {
129 Some("wide-arithmetic") => {
130 ret.wide_arithmetic = Some(true);
131 }
132 Some("threads") => {
133 ret.threads = Some(true);
134 ret.reference_types = Some(false);
135 }
136 Some("custom-page-sizes") => {
137 ret.custom_page_sizes = Some(true);
138 ret.multi_memory = Some(true);
139 ret.memory64 = Some(true);
140 ret.reference_types = Some(true);
141
142 if test.ends_with("memory_max.wast") || test.ends_with("memory_max_i64.wast") {
145 ret.hogs_memory = Some(true);
146 }
147 }
148 Some("custom-descriptors") => {
149 ret.custom_descriptors = Some(true);
150 }
151 Some(proposal) => panic!("unsupported proposal {proposal:?}"),
152
153 None => {
168 let test_name = test.file_name().unwrap().to_str().unwrap();
169 ret.reference_types = Some(true);
170 ret.multi_memory = Some(true);
171 if test_name.contains("simd") {
172 ret.simd = Some(true);
173 }
174 if test_name.contains("relaxed") {
175 ret.relaxed_simd = Some(true);
176 }
177 if test_name.contains("64") || test_name.contains("mixed") {
178 ret.memory64 = Some(true);
179 }
180 if test_name.contains("ref")
181 || test_name.contains("array")
182 || test_name.contains("struct")
183 || test_name.contains("table")
184 || test_name.contains("type")
185 || test_name.contains("tag")
186 || test_name.contains("extern")
187 || test_name.contains("br_on_")
188 || test_name.contains("linking")
189 || test_name.contains("instance")
190 || test_name.contains("local_init")
191 || test_name.contains("elem")
192 || test_name.contains("global")
193 || test_name.contains("i31")
194 || test_name.contains("unreached-valid")
195 || test_name.contains("select")
196 || test_name.contains("data")
197 {
198 ret.gc = Some(true);
199 }
200 if test_name == "throw_ref.wast" {
201 ret.gc = Some(false);
204 }
205 if test_name.contains("return_") || test_name.contains("try_table") {
206 ret.tail_call = Some(true);
207 }
208 if test_name.contains("tag")
209 || test_name.contains("try_table")
210 || test_name.contains("throw")
211 || test_name.contains("ref")
212 || test_name.contains("instance")
213 || test_name.contains("imports")
214 {
215 ret.exceptions = Some(true);
216 }
217 if test_name.contains("global")
218 || test_name.contains("elem")
219 || test_name.contains("data")
220 {
221 ret.extended_const = Some(true);
222 }
223
224 if test.parent().unwrap().ends_with("legacy") {
225 ret.legacy_exceptions = Some(true);
226 }
227
228 if test.ends_with("memory.wast")
237 || test.ends_with("table.wast")
238 || test.ends_with("memory64.wast")
239 || test.ends_with("table64.wast")
240 {
241 ret.hogs_memory = Some(true);
242 }
243 }
244 }
245
246 ret
247}
248
249fn component_test_config(test: &Path) -> TestConfig {
250 let mut ret = TestConfig::default();
251 ret.spec_test = Some(true);
252 ret.reference_types = Some(true);
253 ret.multi_memory = Some(true);
254 ret.component_model_implements = Some(true);
255 ret.bulk_memory = Some(true);
256 ret.component_model_async = Some(true);
257 ret.component_model_more_async_builtins = Some(true);
258 ret.component_model_async_stackful = Some(true);
259 ret.component_model_threading = Some(true);
260 ret.gc = Some(true);
261 ret.exceptions = Some(true);
262 ret.component_model_map = Some(true);
263 ret.component_model_fixed_length_lists = Some(true);
264
265 if test.ends_with("memory64.wast") {
266 ret.component_model_memory64 = Some(true);
267 }
268
269 if let Some(parent) = test.parent() {
270 if parent.ends_with("wasm-tools") {
271 ret.memory64 = Some(true);
272 ret.threads = Some(true);
273 ret.exceptions = Some(true);
274 ret.gc = Some(true);
275 }
276 if parent.ends_with("wasmtime") {
277 ret.exceptions = Some(true);
278 ret.gc = Some(true);
279 }
280 }
281
282 ret
283}
284
285pub fn parse_test_config<T>(wat: &str, comment: &'static str) -> Result<T>
288where
289 T: DeserializeOwned,
290{
291 let config_lines: Vec<_> = wat
294 .lines()
295 .take_while(|l| l.starts_with(comment))
296 .map(|l| &l[comment.len()..])
297 .collect();
298 let config_text = config_lines.join("\n");
299
300 toml::from_str(&config_text).context("failed to parse the test configuration")
301}
302
303#[derive(Clone)]
305pub struct WastTest {
306 pub path: PathBuf,
307 pub contents: String,
308 pub config: TestConfig,
309}
310
311impl fmt::Debug for WastTest {
312 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
313 f.debug_struct("WastTest")
314 .field("path", &self.path)
315 .field("contents", &"...")
316 .field("config", &self.config)
317 .finish()
318 }
319}
320
321macro_rules! foreach_config_option {
322 ($m:ident) => {
323 $m! {
324 bulk_memory
325 memory64
326 custom_page_sizes
327 multi_memory
328 threads
329 shared_everything_threads
330 gc
331 function_references
332 relaxed_simd
333 reference_types
334 tail_call
335 extended_const
336 wide_arithmetic
337 branch_hinting
338 hogs_memory
339 nan_canonicalization
340 component_model_async
341 component_model_more_async_builtins
342 component_model_async_stackful
343 component_model_threading
344 component_model_error_context
345 component_model_gc
346 component_model_map
347 component_model_memory64
348 component_model_fixed_length_lists
349 component_model_implements
350 simd
351 gc_types
352 exceptions
353 legacy_exceptions
354 stack_switching
355 spec_test
356 custom_descriptors
357 }
358 };
359}
360
361macro_rules! define_test_config {
362 ($($option:ident)*) => {
363 #[derive(Debug, PartialEq, Default, Deserialize, Clone)]
366 #[serde(deny_unknown_fields)]
367 pub struct TestConfig {
368 $(pub $option: Option<bool>,)*
369 }
370
371 impl TestConfig {
372 $(
373 pub fn $option(&self) -> bool {
374 self.$option.unwrap_or(false)
375 }
376 )*
377 }
378 }
379}
380
381foreach_config_option!(define_test_config);
382
383impl TestConfig {
384 pub fn options_mut(&mut self) -> impl Iterator<Item = (&'static str, &mut Option<bool>)> {
386 macro_rules! mk {
387 ($($option:ident)*) => {
388 [
389 $((stringify!($option), &mut self.$option),)*
390 ].into_iter()
391 }
392 }
393 foreach_config_option!(mk)
394 }
395}
396
397#[derive(Debug)]
399pub struct WastConfig {
400 pub compiler: Compiler,
402 pub pooling: bool,
404 pub collector: Collector,
406}
407
408#[derive(PartialEq, Debug, Copy, Clone)]
410pub enum Compiler {
411 CraneliftNative,
418
419 Winch,
424
425 CraneliftPulley,
432}
433
434impl Compiler {
435 pub fn should_fail(&self, config: &TestConfig) -> bool {
446 match self {
447 Compiler::CraneliftNative => {
448 if config.legacy_exceptions() {
449 return true;
450 }
451
452 if config.stack_switching() && !(cfg!(target_arch = "x86_64") && cfg!(unix)) {
455 return true;
456 }
457
458 false
459 }
460
461 Compiler::Winch => {
462 if config.gc()
463 || config.tail_call()
464 || config.function_references()
465 || config.relaxed_simd()
466 || config.legacy_exceptions()
467 || config.stack_switching()
468 {
469 return true;
470 }
471
472 if cfg!(target_arch = "aarch64") {
473 return config.threads();
474 }
475
476 !cfg!(target_arch = "x86_64")
477 }
478
479 Compiler::CraneliftPulley => {
480 config.threads() || config.legacy_exceptions() || config.stack_switching()
481 }
482 }
483 }
484
485 pub fn supports_host(&self) -> bool {
488 match self {
489 Compiler::CraneliftNative => {
490 cfg!(target_arch = "x86_64")
491 || cfg!(target_arch = "aarch64")
492 || cfg!(target_arch = "riscv64")
493 || cfg!(target_arch = "s390x")
494 }
495 Compiler::Winch => cfg!(target_arch = "x86_64") || cfg!(target_arch = "aarch64"),
496 Compiler::CraneliftPulley => true,
497 }
498 }
499}
500
501#[derive(PartialEq, Debug, Copy, Clone)]
502pub enum Collector {
503 Auto,
504 Null,
505 DeferredReferenceCounting,
506 Copying,
507}
508
509impl WastTest {
510 pub fn test_uses_gc_types(&self) -> bool {
513 self.config.gc() || self.config.function_references()
514 }
515
516 pub fn spec_proposal(&self) -> Option<&str> {
518 spec_proposal_from_path(&self.path)
519 }
520
521 pub fn should_fail(&self, config: &WastConfig) -> bool {
524 if !config.compiler.supports_host() {
525 return true;
526 }
527
528 if self.path.ends_with("async/cancellable.wast")
530 || self.path.ends_with("binary/binary.wast")
531 {
532 return true;
533 }
534
535 if config.pooling {
537 if self.config.hogs_memory() {
539 return true;
540 }
541 let unsupported = [
542 "misc_testsuite/memory-combos.wast",
544 "misc_testsuite/threads/atomics-end-of-memory.wast",
545 "misc_testsuite/threads/atomic_wait_endianness.wast",
546 "misc_testsuite/threads/LB.wast",
547 "misc_testsuite/threads/LB_atomic.wast",
548 "misc_testsuite/threads/MP.wast",
549 "misc_testsuite/threads/MP_atomic.wast",
550 "misc_testsuite/threads/MP_wait.wast",
551 "misc_testsuite/threads/SB.wast",
552 "misc_testsuite/threads/SB_atomic.wast",
553 "misc_testsuite/threads/atomics_notify.wast",
554 "misc_testsuite/threads/atomics_wait_address.wast",
555 "misc_testsuite/threads/wait_notify.wast",
556 "spec_testsuite/proposals/threads/atomic.wast",
557 "spec_testsuite/proposals/threads/exports.wast",
558 "spec_testsuite/proposals/threads/memory.wast",
559 "misc_testsuite/memory64/threads.wast",
560 "misc_testsuite/winch/rmw32_cmpxchg_u_wrap.wast",
561 ];
562
563 if unsupported.iter().any(|part| self.path.ends_with(part)) {
564 return true;
565 }
566 }
567
568 if config.compiler.should_fail(&self.config) {
569 return true;
570 }
571
572 if config.compiler == Compiler::Winch {
574 let unsupported = [
576 "extended-const/elem.wast",
577 "extended-const/global.wast",
578 "misc_testsuite/externref-segments.wast",
579 "misc_testsuite/externref-table-dropped-segment-issue-8281.wast",
580 "misc_testsuite/many_table_gets_lead_to_gc.wast",
581 "misc_testsuite/no-panic.wast",
582 ];
583
584 if unsupported.iter().any(|part| self.path.ends_with(part)) {
585 return true;
586 }
587
588 #[cfg(target_arch = "x86_64")]
589 {
590 #[cfg(target_arch = "x86_64")]
592 if !(std::is_x86_feature_detected!("avx") && std::is_x86_feature_detected!("avx2"))
593 {
594 let unsupported = [
595 "annotations/simd_lane.wast",
596 "memory64/simd.wast",
597 "misc_testsuite/int-to-float-splat.wast",
598 "misc_testsuite/issue6562.wast",
599 "misc_testsuite/simd/almost-extmul.wast",
600 "misc_testsuite/simd/canonicalize-nan.wast",
601 "misc_testsuite/simd/cvt-from-uint.wast",
602 "misc_testsuite/simd/edge-of-memory.wast",
603 "misc_testsuite/simd/issue_3327_bnot_lowering.wast",
604 "misc_testsuite/simd/issue6725-no-egraph-panic.wast",
605 "misc_testsuite/simd/replace-lane-preserve.wast",
606 "misc_testsuite/simd/spillslot-size-fuzzbug.wast",
607 "misc_testsuite/simd/sse-cannot-fold-unaligned-loads.wast",
608 "misc_testsuite/winch/issue-10331.wast",
609 "misc_testsuite/int-to-float-splat.wast",
610 "misc_testsuite/simd/cvt-from-uint.wast",
611 "misc_testsuite/winch/replace_lane.wast",
612 "misc_testsuite/simd/riscv64-replicated-imm5-works.wast",
613 "misc_testsuite/simd/v128-equal.wast",
614 "misc_testsuite/winch/issue-10331.wast",
615 "misc_testsuite/int-to-float-splat.wast",
616 "misc_testsuite/simd/cvt-from-uint.wast",
617 "spec_testsuite/simd_align.wast",
618 "spec_testsuite/simd_boolean.wast",
619 "spec_testsuite/simd_conversions.wast",
620 "spec_testsuite/simd_f32x4.wast",
621 "spec_testsuite/simd_f32x4_arith.wast",
622 "spec_testsuite/simd_f32x4_cmp.wast",
623 "spec_testsuite/simd_f32x4_pmin_pmax.wast",
624 "spec_testsuite/simd_f32x4_rounding.wast",
625 "spec_testsuite/simd_f64x2.wast",
626 "spec_testsuite/simd_f64x2_arith.wast",
627 "spec_testsuite/simd_f64x2_cmp.wast",
628 "spec_testsuite/simd_f64x2_pmin_pmax.wast",
629 "spec_testsuite/simd_f64x2_rounding.wast",
630 "spec_testsuite/simd_i16x8_cmp.wast",
631 "spec_testsuite/simd_i32x4_cmp.wast",
632 "spec_testsuite/simd_i64x2_arith2.wast",
633 "spec_testsuite/simd_i64x2_cmp.wast",
634 "spec_testsuite/simd_i8x16_arith2.wast",
635 "spec_testsuite/simd_i8x16_cmp.wast",
636 "spec_testsuite/simd_int_to_int_extend.wast",
637 "spec_testsuite/simd_load.wast",
638 "spec_testsuite/simd_load_extend.wast",
639 "spec_testsuite/simd_load_splat.wast",
640 "spec_testsuite/simd_load_zero.wast",
641 "spec_testsuite/simd_splat.wast",
642 "spec_testsuite/simd_store16_lane.wast",
643 "spec_testsuite/simd_store32_lane.wast",
644 "spec_testsuite/simd_store64_lane.wast",
645 "spec_testsuite/simd_store8_lane.wast",
646 "spec_testsuite/simd_load16_lane.wast",
647 "spec_testsuite/simd_load32_lane.wast",
648 "spec_testsuite/simd_load64_lane.wast",
649 "spec_testsuite/simd_load8_lane.wast",
650 "spec_testsuite/simd_bitwise.wast",
651 "misc_testsuite/simd/load_splat_out_of_bounds.wast",
652 "misc_testsuite/simd/unaligned-load.wast",
653 "misc_testsuite/simd/riscv64-replicated-imm5-works.wast",
654 "misc_testsuite/simd/issue6725-no-egraph-panic.wast",
655 "misc_testsuite/winch/replace_lane.wast",
656 "misc_testsuite/simd/v128-equal.wast",
657 "misc_testsuite/winch/issue-10331.wast",
658 "misc_testsuite/int-to-float-splat.wast",
659 "misc_testsuite/simd/cvt-from-uint.wast",
660 "spec_testsuite/simd_memory-multi.wast",
661 "misc_testsuite/simd/issue4807.wast",
662 "spec_testsuite/simd_const.wast",
663 "spec_testsuite/simd_i8x16_sat_arith.wast",
664 "spec_testsuite/simd_i64x2_arith.wast",
665 "spec_testsuite/simd_i16x8_arith.wast",
666 "spec_testsuite/simd_i16x8_arith2.wast",
667 "spec_testsuite/simd_i16x8_q15mulr_sat_s.wast",
668 "spec_testsuite/simd_i16x8_sat_arith.wast",
669 "spec_testsuite/simd_i32x4_arith.wast",
670 "spec_testsuite/simd_i32x4_dot_i16x8.wast",
671 "spec_testsuite/simd_i32x4_trunc_sat_f32x4.wast",
672 "spec_testsuite/simd_i32x4_trunc_sat_f64x2.wast",
673 "spec_testsuite/simd_i8x16_arith.wast",
674 "spec_testsuite/simd_bit_shift.wast",
675 "spec_testsuite/simd_lane.wast",
676 "spec_testsuite/simd_i16x8_extmul_i8x16.wast",
677 "spec_testsuite/simd_i32x4_extmul_i16x8.wast",
678 "spec_testsuite/simd_i64x2_extmul_i32x4.wast",
679 "spec_testsuite/simd_i16x8_extadd_pairwise_i8x16.wast",
680 "spec_testsuite/simd_i32x4_extadd_pairwise_i16x8.wast",
681 "spec_testsuite/simd_i32x4_arith2.wast",
682 ];
683
684 if unsupported.iter().any(|part| self.path.ends_with(part)) {
685 return true;
686 }
687 }
688 }
689 }
690
691 if self.config.custom_descriptors() {
693 let happens_to_work =
694 ["spec_testsuite/proposals/custom-descriptors/binary-leb128.wast"];
695
696 if happens_to_work.iter().any(|part| self.path.ends_with(part)) {
697 return false;
698 }
699 return true;
700 }
701
702 false
703 }
704}
705
706fn spec_proposal_from_path(path: &Path) -> Option<&str> {
707 let mut iter = path.iter();
708 loop {
709 match iter.next()?.to_str()? {
710 "proposals" => break,
711 _ => {}
712 }
713 }
714 Some(iter.next()?.to_str()?)
715}