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("compact-import-section") => {
152 ret.compact_imports = Some(true);
153 ret.reference_types = Some(true);
154 ret.multi_memory = Some(true);
155 ret.exceptions = Some(true);
156 }
157 Some("extended-name-section") => {
158 ret.gc = Some(true);
159 ret.exceptions = Some(true);
160 }
161 Some(proposal) => panic!("unsupported proposal {proposal:?}"),
162
163 None => {
178 let test_name = test.file_name().unwrap().to_str().unwrap();
179 ret.reference_types = Some(true);
180 ret.multi_memory = Some(true);
181 if test_name.contains("simd") {
182 ret.simd = Some(true);
183 }
184 if test_name.contains("relaxed") {
185 ret.relaxed_simd = Some(true);
186 }
187 if test_name.contains("64") || test_name.contains("mixed") {
188 ret.memory64 = Some(true);
189 }
190 if test_name.contains("ref")
191 || test_name.contains("array")
192 || test_name.contains("struct")
193 || test_name.contains("table")
194 || test_name.contains("type")
195 || test_name.contains("tag")
196 || test_name.contains("extern")
197 || test_name.contains("br_on_")
198 || test_name.contains("linking")
199 || test_name.contains("instance")
200 || test_name.contains("local_init")
201 || test_name.contains("elem")
202 || test_name.contains("global")
203 || test_name.contains("i31")
204 || test_name.contains("unreached-valid")
205 || test_name.contains("select")
206 || test_name.contains("data")
207 {
208 ret.gc = Some(true);
209 }
210 if test_name == "throw_ref.wast" {
211 ret.gc = Some(false);
214 }
215 if test_name.contains("return_") || test_name.contains("try_table") {
216 ret.tail_call = Some(true);
217 }
218 if test_name == "return_call.wast" || test_name == "return_call_indirect.wast" {
219 ret.function_references = Some(true);
220 }
221 if test_name.contains("tag")
222 || test_name.contains("try_table")
223 || test_name.contains("throw")
224 || test_name.contains("ref")
225 || test_name.contains("instance")
226 || test_name.contains("imports")
227 {
228 ret.exceptions = Some(true);
229 }
230 if test_name.contains("global")
231 || test_name.contains("elem")
232 || test_name.contains("data")
233 {
234 ret.extended_const = Some(true);
235 }
236
237 if test.parent().unwrap().ends_with("legacy") {
238 ret.legacy_exceptions = Some(true);
239 }
240
241 if test.ends_with("memory.wast")
250 || test.ends_with("table.wast")
251 || test.ends_with("memory64.wast")
252 || test.ends_with("table64.wast")
253 {
254 ret.hogs_memory = Some(true);
255 }
256 }
257 }
258
259 ret
260}
261
262fn component_test_config(test: &Path) -> TestConfig {
263 let mut ret = TestConfig::default();
264 ret.spec_test = Some(true);
265 ret.reference_types = Some(true);
266 ret.multi_memory = Some(true);
267 ret.component_model_implements = Some(true);
268 ret.component_model_canonical_names = Some(true);
269 ret.component_model_accessors = Some(true);
270 ret.bulk_memory = Some(true);
271 ret.component_model_async = Some(true);
272 ret.component_model_more_async_builtins = Some(true);
273 ret.component_model_async_stackful = Some(true);
274 ret.component_model_threading = Some(true);
275 ret.gc = Some(true);
276 ret.exceptions = Some(true);
277 ret.component_model_map = Some(true);
278 ret.component_model_fixed_length_lists = Some(true);
279
280 if test.ends_with("memory64.wast") {
281 ret.component_model_memory64 = Some(true);
282 }
283
284 if let Some(parent) = test.parent() {
285 if parent.ends_with("wasm-tools") {
286 ret.memory64 = Some(true);
287 ret.threads = Some(true);
288 ret.exceptions = Some(true);
289 ret.gc = Some(true);
290 }
291 if parent.ends_with("wasmtime") {
292 ret.exceptions = Some(true);
293 ret.gc = Some(true);
294 }
295 }
296
297 ret
298}
299
300pub fn parse_test_config<T>(wat: &str, comment: &'static str) -> Result<T>
303where
304 T: DeserializeOwned,
305{
306 let config_lines: Vec<_> = wat
309 .lines()
310 .take_while(|l| l.starts_with(comment))
311 .map(|l| &l[comment.len()..])
312 .collect();
313 let config_text = config_lines.join("\n");
314
315 toml::from_str(&config_text).context("failed to parse the test configuration")
316}
317
318#[derive(Clone)]
320pub struct WastTest {
321 pub path: PathBuf,
322 pub contents: String,
323 pub config: TestConfig,
324}
325
326impl fmt::Debug for WastTest {
327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328 f.debug_struct("WastTest")
329 .field("path", &self.path)
330 .field("contents", &"...")
331 .field("config", &self.config)
332 .finish()
333 }
334}
335
336macro_rules! foreach_config_option {
337 ($m:ident) => {
338 $m! {
339 bulk_memory
340 memory64
341 custom_page_sizes
342 multi_memory
343 threads
344 shared_everything_threads
345 gc
346 function_references
347 relaxed_simd
348 reference_types
349 tail_call
350 extended_const
351 wide_arithmetic
352 branch_hinting
353 compact_imports
354 hogs_memory
355 nan_canonicalization
356 component_model_async
357 component_model_more_async_builtins
358 component_model_async_stackful
359 component_model_threading
360 component_model_error_context
361 component_model_gc
362 component_model_map
363 component_model_memory64
364 component_model_fixed_length_lists
365 component_model_implements
366 component_model_canonical_names
367 component_model_accessors
368 simd
369 gc_types
370 exceptions
371 legacy_exceptions
372 stack_switching
373 spec_test
374 custom_descriptors
375 }
376 };
377}
378
379macro_rules! define_test_config {
380 ($($option:ident)*) => {
381 #[derive(Debug, PartialEq, Default, Deserialize, Clone)]
384 #[serde(deny_unknown_fields)]
385 pub struct TestConfig {
386 $(pub $option: Option<bool>,)*
387 }
388
389 impl TestConfig {
390 $(
391 pub fn $option(&self) -> bool {
392 self.$option.unwrap_or(false)
393 }
394 )*
395 }
396 }
397}
398
399foreach_config_option!(define_test_config);
400
401impl TestConfig {
402 pub fn options_mut(&mut self) -> impl Iterator<Item = (&'static str, &mut Option<bool>)> {
404 macro_rules! mk {
405 ($($option:ident)*) => {
406 [
407 $((stringify!($option), &mut self.$option),)*
408 ].into_iter()
409 }
410 }
411 foreach_config_option!(mk)
412 }
413}
414
415#[derive(Debug)]
417pub struct WastConfig {
418 pub compiler: Compiler,
420 pub pooling: bool,
422 pub collector: Collector,
424}
425
426#[derive(PartialEq, Debug, Copy, Clone)]
428pub enum Compiler {
429 CraneliftNative,
436
437 Winch,
442
443 CraneliftPulley,
450}
451
452impl Compiler {
453 pub fn should_fail(&self, config: &TestConfig) -> bool {
464 match self {
465 Compiler::CraneliftNative => {
466 if config.legacy_exceptions() {
467 return true;
468 }
469
470 if config.stack_switching() && !(cfg!(target_arch = "x86_64") && cfg!(unix)) {
473 return true;
474 }
475
476 false
477 }
478
479 Compiler::Winch => {
480 if config.gc()
481 || config.function_references()
482 || config.relaxed_simd()
483 || config.legacy_exceptions()
484 || config.stack_switching()
485 {
486 return true;
487 }
488
489 if cfg!(target_arch = "aarch64") {
490 return config.threads();
491 }
492
493 !cfg!(target_arch = "x86_64")
494 }
495
496 Compiler::CraneliftPulley => {
497 config.threads() || config.legacy_exceptions() || config.stack_switching()
498 }
499 }
500 }
501
502 pub fn supports_host(&self) -> bool {
505 match self {
506 Compiler::CraneliftNative => {
507 cfg!(target_arch = "x86_64")
508 || cfg!(target_arch = "aarch64")
509 || cfg!(target_arch = "riscv64")
510 || cfg!(target_arch = "s390x")
511 }
512 Compiler::Winch => cfg!(target_arch = "x86_64") || cfg!(target_arch = "aarch64"),
513 Compiler::CraneliftPulley => true,
514 }
515 }
516}
517
518#[derive(PartialEq, Debug, Copy, Clone)]
519pub enum Collector {
520 Auto,
521 Null,
522 DeferredReferenceCounting,
523 Copying,
524}
525
526impl WastTest {
527 pub fn test_uses_gc_types(&self) -> bool {
530 self.config.gc() || self.config.function_references()
531 }
532
533 pub fn spec_proposal(&self) -> Option<&str> {
535 spec_proposal_from_path(&self.path)
536 }
537
538 pub fn should_fail(&self, config: &WastConfig) -> bool {
541 if !config.compiler.supports_host() {
542 return true;
543 }
544
545 if config.pooling {
547 if self.config.hogs_memory() {
549 return true;
550 }
551 let unsupported = [
552 "misc_testsuite/memory-combos.wast",
554 "misc_testsuite/threads/atomics-end-of-memory.wast",
555 "misc_testsuite/threads/atomic_wait_endianness.wast",
556 "misc_testsuite/threads/LB.wast",
557 "misc_testsuite/threads/LB_atomic.wast",
558 "misc_testsuite/threads/MP.wast",
559 "misc_testsuite/threads/MP_atomic.wast",
560 "misc_testsuite/threads/MP_wait.wast",
561 "misc_testsuite/threads/SB.wast",
562 "misc_testsuite/threads/SB_atomic.wast",
563 "misc_testsuite/threads/atomics_notify.wast",
564 "misc_testsuite/threads/atomics_wait_address.wast",
565 "misc_testsuite/threads/wait_notify.wast",
566 "spec_testsuite/proposals/threads/atomic.wast",
567 "spec_testsuite/proposals/threads/exports.wast",
568 "spec_testsuite/proposals/threads/memory.wast",
569 "misc_testsuite/memory64/threads.wast",
570 "misc_testsuite/winch/rmw32_cmpxchg_u_wrap.wast",
571 ];
572
573 if unsupported.iter().any(|part| self.path.ends_with(part)) {
574 return true;
575 }
576 }
577
578 if config.compiler.should_fail(&self.config) {
579 return true;
580 }
581
582 let unsupported = [
584 "spec_testsuite/proposals/extended-name-section/custom/name_annot.wast",
585 "spec_testsuite/proposals/extended-name-section/custom/name.wast",
586 ];
587 if unsupported.iter().any(|part| self.path.ends_with(part)) {
588 return true;
589 }
590
591 if config.compiler == Compiler::Winch {
593 let unsupported = [
595 "extended-const/elem.wast",
596 "extended-const/global.wast",
597 "misc_testsuite/externref-segments.wast",
598 "misc_testsuite/externref-table-dropped-segment-issue-8281.wast",
599 "misc_testsuite/many_table_gets_lead_to_gc.wast",
600 "misc_testsuite/no-panic.wast",
601 ];
602
603 if unsupported.iter().any(|part| self.path.ends_with(part)) {
604 return true;
605 }
606
607 #[cfg(target_arch = "x86_64")]
608 {
609 #[cfg(target_arch = "x86_64")]
611 if !(std::is_x86_feature_detected!("avx") && std::is_x86_feature_detected!("avx2"))
612 {
613 let unsupported = [
614 "annotations/simd_lane.wast",
615 "memory64/simd.wast",
616 "misc_testsuite/int-to-float-splat.wast",
617 "misc_testsuite/issue6562.wast",
618 "misc_testsuite/simd/almost-extmul.wast",
619 "misc_testsuite/simd/canonicalize-nan.wast",
620 "misc_testsuite/simd/cvt-from-uint.wast",
621 "misc_testsuite/simd/edge-of-memory.wast",
622 "misc_testsuite/simd/issue_3327_bnot_lowering.wast",
623 "misc_testsuite/simd/issue6725-no-egraph-panic.wast",
624 "misc_testsuite/simd/replace-lane-preserve.wast",
625 "misc_testsuite/simd/spillslot-size-fuzzbug.wast",
626 "misc_testsuite/simd/sse-cannot-fold-unaligned-loads.wast",
627 "misc_testsuite/winch/issue-10331.wast",
628 "misc_testsuite/int-to-float-splat.wast",
629 "misc_testsuite/simd/cvt-from-uint.wast",
630 "misc_testsuite/winch/replace_lane.wast",
631 "misc_testsuite/simd/riscv64-replicated-imm5-works.wast",
632 "misc_testsuite/simd/v128-equal.wast",
633 "misc_testsuite/winch/issue-10331.wast",
634 "misc_testsuite/int-to-float-splat.wast",
635 "misc_testsuite/simd/cvt-from-uint.wast",
636 "spec_testsuite/simd_align.wast",
637 "spec_testsuite/simd_boolean.wast",
638 "spec_testsuite/simd_conversions.wast",
639 "spec_testsuite/simd_f32x4.wast",
640 "spec_testsuite/simd_f32x4_arith.wast",
641 "spec_testsuite/simd_f32x4_cmp.wast",
642 "spec_testsuite/simd_f32x4_pmin_pmax.wast",
643 "spec_testsuite/simd_f32x4_rounding.wast",
644 "spec_testsuite/simd_f64x2.wast",
645 "spec_testsuite/simd_f64x2_arith.wast",
646 "spec_testsuite/simd_f64x2_cmp.wast",
647 "spec_testsuite/simd_f64x2_pmin_pmax.wast",
648 "spec_testsuite/simd_f64x2_rounding.wast",
649 "spec_testsuite/simd_i16x8_cmp.wast",
650 "spec_testsuite/simd_i32x4_cmp.wast",
651 "spec_testsuite/simd_i64x2_arith2.wast",
652 "spec_testsuite/simd_i64x2_cmp.wast",
653 "spec_testsuite/simd_i8x16_arith2.wast",
654 "spec_testsuite/simd_i8x16_cmp.wast",
655 "spec_testsuite/simd_int_to_int_extend.wast",
656 "spec_testsuite/simd_load.wast",
657 "spec_testsuite/simd_load_extend.wast",
658 "spec_testsuite/simd_load_splat.wast",
659 "spec_testsuite/simd_load_zero.wast",
660 "spec_testsuite/simd_splat.wast",
661 "spec_testsuite/simd_store16_lane.wast",
662 "spec_testsuite/simd_store32_lane.wast",
663 "spec_testsuite/simd_store64_lane.wast",
664 "spec_testsuite/simd_store8_lane.wast",
665 "spec_testsuite/simd_load16_lane.wast",
666 "spec_testsuite/simd_load32_lane.wast",
667 "spec_testsuite/simd_load64_lane.wast",
668 "spec_testsuite/simd_load8_lane.wast",
669 "spec_testsuite/simd_bitwise.wast",
670 "misc_testsuite/simd/load_splat_out_of_bounds.wast",
671 "misc_testsuite/simd/unaligned-load.wast",
672 "misc_testsuite/simd/riscv64-replicated-imm5-works.wast",
673 "misc_testsuite/simd/issue6725-no-egraph-panic.wast",
674 "misc_testsuite/winch/replace_lane.wast",
675 "misc_testsuite/simd/v128-equal.wast",
676 "misc_testsuite/winch/issue-10331.wast",
677 "misc_testsuite/int-to-float-splat.wast",
678 "misc_testsuite/simd/cvt-from-uint.wast",
679 "spec_testsuite/simd_memory-multi.wast",
680 "misc_testsuite/simd/issue4807.wast",
681 "spec_testsuite/simd_const.wast",
682 "spec_testsuite/simd_i8x16_sat_arith.wast",
683 "spec_testsuite/simd_i64x2_arith.wast",
684 "spec_testsuite/simd_i16x8_arith.wast",
685 "spec_testsuite/simd_i16x8_arith2.wast",
686 "spec_testsuite/simd_i16x8_q15mulr_sat_s.wast",
687 "spec_testsuite/simd_i16x8_sat_arith.wast",
688 "spec_testsuite/simd_i32x4_arith.wast",
689 "spec_testsuite/simd_i32x4_dot_i16x8.wast",
690 "spec_testsuite/simd_i32x4_trunc_sat_f32x4.wast",
691 "spec_testsuite/simd_i32x4_trunc_sat_f64x2.wast",
692 "spec_testsuite/simd_i8x16_arith.wast",
693 "spec_testsuite/simd_bit_shift.wast",
694 "spec_testsuite/simd_lane.wast",
695 "spec_testsuite/simd_i16x8_extmul_i8x16.wast",
696 "spec_testsuite/simd_i32x4_extmul_i16x8.wast",
697 "spec_testsuite/simd_i64x2_extmul_i32x4.wast",
698 "spec_testsuite/simd_i16x8_extadd_pairwise_i8x16.wast",
699 "spec_testsuite/simd_i32x4_extadd_pairwise_i16x8.wast",
700 "spec_testsuite/simd_i32x4_arith2.wast",
701 ];
702
703 if unsupported.iter().any(|part| self.path.ends_with(part)) {
704 return true;
705 }
706 }
707 }
708 }
709
710 if self.config.custom_descriptors() {
712 let happens_to_work =
713 ["spec_testsuite/proposals/custom-descriptors/binary-leb128.wast"];
714
715 if happens_to_work.iter().any(|part| self.path.ends_with(part)) {
716 return false;
717 }
718 return true;
719 }
720
721 let uses_stream_forward = [
723 "component-model/test/values/post-return.wast",
724 "component-model/test/async/big-interleaving-test.wast",
725 "component-model/test/async/forward.wast",
726 ];
727
728 if uses_stream_forward
729 .iter()
730 .any(|part| self.path.ends_with(part))
731 {
732 return true;
733 }
734
735 if self
737 .path
738 .ends_with("component-model/test/async/cancel-instance-wide-resume.wast")
739 {
740 return true;
741 }
742
743 false
744 }
745}
746
747fn spec_proposal_from_path(path: &Path) -> Option<&str> {
748 let mut iter = path.iter();
749 loop {
750 match iter.next()?.to_str()? {
751 "proposals" => break,
752 _ => {}
753 }
754 }
755 Some(iter.next()?.to_str()?)
756}