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.contains("return_") || test_name.contains("try_table") {
201 ret.tail_call = Some(true);
202 }
203 if test_name.contains("tag")
204 || test_name.contains("try_table")
205 || test_name.contains("throw")
206 || test_name.contains("ref")
207 || test_name.contains("instance")
208 || test_name.contains("imports")
209 {
210 ret.exceptions = Some(true);
211 }
212 if test_name.contains("global")
213 || test_name.contains("elem")
214 || test_name.contains("data")
215 {
216 ret.extended_const = Some(true);
217 }
218
219 if test.parent().unwrap().ends_with("legacy") {
220 ret.legacy_exceptions = Some(true);
221 }
222
223 if test.ends_with("memory.wast")
232 || test.ends_with("table.wast")
233 || test.ends_with("memory64.wast")
234 || test.ends_with("table64.wast")
235 {
236 ret.hogs_memory = Some(true);
237 }
238 }
239 }
240
241 ret
242}
243
244fn component_test_config(test: &Path) -> TestConfig {
245 let mut ret = TestConfig::default();
246 ret.spec_test = Some(true);
247 ret.reference_types = Some(true);
248 ret.multi_memory = Some(true);
249 ret.component_model_implements = Some(true);
250 ret.bulk_memory = Some(true);
251 ret.component_model_async = Some(true);
252 ret.component_model_more_async_builtins = Some(true);
253 ret.component_model_async_stackful = Some(true);
254 ret.component_model_threading = Some(true);
255
256 if test.ends_with("memory64.wast") {
257 ret.component_model_memory64 = Some(true);
258 }
259
260 if let Some(parent) = test.parent() {
261 if parent.ends_with("wasm-tools") {
262 ret.memory64 = Some(true);
263 ret.threads = Some(true);
264 ret.exceptions = Some(true);
265 ret.gc = Some(true);
266 }
267 if parent.ends_with("wasmtime") {
268 ret.exceptions = Some(true);
269 ret.gc = Some(true);
270 }
271 }
272
273 ret
274}
275
276pub fn parse_test_config<T>(wat: &str, comment: &'static str) -> Result<T>
279where
280 T: DeserializeOwned,
281{
282 let config_lines: Vec<_> = wat
285 .lines()
286 .take_while(|l| l.starts_with(comment))
287 .map(|l| &l[comment.len()..])
288 .collect();
289 let config_text = config_lines.join("\n");
290
291 toml::from_str(&config_text).context("failed to parse the test configuration")
292}
293
294#[derive(Clone)]
296pub struct WastTest {
297 pub path: PathBuf,
298 pub contents: String,
299 pub config: TestConfig,
300}
301
302impl fmt::Debug for WastTest {
303 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
304 f.debug_struct("WastTest")
305 .field("path", &self.path)
306 .field("contents", &"...")
307 .field("config", &self.config)
308 .finish()
309 }
310}
311
312macro_rules! foreach_config_option {
313 ($m:ident) => {
314 $m! {
315 bulk_memory
316 memory64
317 custom_page_sizes
318 multi_memory
319 threads
320 shared_everything_threads
321 gc
322 function_references
323 relaxed_simd
324 reference_types
325 tail_call
326 extended_const
327 wide_arithmetic
328 branch_hinting
329 hogs_memory
330 nan_canonicalization
331 component_model_async
332 component_model_more_async_builtins
333 component_model_async_stackful
334 component_model_threading
335 component_model_error_context
336 component_model_gc
337 component_model_map
338 component_model_memory64
339 component_model_fixed_length_lists
340 component_model_implements
341 simd
342 gc_types
343 exceptions
344 legacy_exceptions
345 stack_switching
346 spec_test
347 custom_descriptors
348 }
349 };
350}
351
352macro_rules! define_test_config {
353 ($($option:ident)*) => {
354 #[derive(Debug, PartialEq, Default, Deserialize, Clone)]
357 #[serde(deny_unknown_fields)]
358 pub struct TestConfig {
359 $(pub $option: Option<bool>,)*
360 }
361
362 impl TestConfig {
363 $(
364 pub fn $option(&self) -> bool {
365 self.$option.unwrap_or(false)
366 }
367 )*
368 }
369 }
370}
371
372foreach_config_option!(define_test_config);
373
374impl TestConfig {
375 pub fn options_mut(&mut self) -> impl Iterator<Item = (&'static str, &mut Option<bool>)> {
377 macro_rules! mk {
378 ($($option:ident)*) => {
379 [
380 $((stringify!($option), &mut self.$option),)*
381 ].into_iter()
382 }
383 }
384 foreach_config_option!(mk)
385 }
386}
387
388#[derive(Debug)]
390pub struct WastConfig {
391 pub compiler: Compiler,
393 pub pooling: bool,
395 pub collector: Collector,
397}
398
399#[derive(PartialEq, Debug, Copy, Clone)]
401pub enum Compiler {
402 CraneliftNative,
409
410 Winch,
415
416 CraneliftPulley,
423}
424
425impl Compiler {
426 pub fn should_fail(&self, config: &TestConfig) -> bool {
437 match self {
438 Compiler::CraneliftNative => {
439 if config.legacy_exceptions() {
440 return true;
441 }
442
443 if config.stack_switching() && !(cfg!(target_arch = "x86_64") && cfg!(unix)) {
446 return true;
447 }
448
449 false
450 }
451
452 Compiler::Winch => {
453 if config.gc()
454 || config.tail_call()
455 || config.function_references()
456 || config.relaxed_simd()
457 || config.gc_types()
458 || config.exceptions()
459 || config.legacy_exceptions()
460 || config.stack_switching()
461 {
462 return true;
463 }
464
465 if cfg!(target_arch = "aarch64") {
466 return config.threads();
467 }
468
469 !cfg!(target_arch = "x86_64")
470 }
471
472 Compiler::CraneliftPulley => {
473 config.threads() || config.legacy_exceptions() || config.stack_switching()
474 }
475 }
476 }
477
478 pub fn supports_host(&self) -> bool {
481 match self {
482 Compiler::CraneliftNative => {
483 cfg!(target_arch = "x86_64")
484 || cfg!(target_arch = "aarch64")
485 || cfg!(target_arch = "riscv64")
486 || cfg!(target_arch = "s390x")
487 }
488 Compiler::Winch => cfg!(target_arch = "x86_64") || cfg!(target_arch = "aarch64"),
489 Compiler::CraneliftPulley => true,
490 }
491 }
492}
493
494#[derive(PartialEq, Debug, Copy, Clone)]
495pub enum Collector {
496 Auto,
497 Null,
498 DeferredReferenceCounting,
499 Copying,
500}
501
502impl WastTest {
503 pub fn test_uses_gc_types(&self) -> bool {
506 self.config.gc() || self.config.function_references()
507 }
508
509 pub fn spec_proposal(&self) -> Option<&str> {
511 spec_proposal_from_path(&self.path)
512 }
513
514 pub fn should_fail(&self, config: &WastConfig) -> bool {
517 if !config.compiler.supports_host() {
518 return true;
519 }
520
521 let unsupported = [
522 "test/values/post-return.wast",
524 ];
525 if unsupported.iter().any(|part| self.path.ends_with(part)) {
526 return true;
527 }
528
529 if config.pooling {
531 if self.config.hogs_memory() {
533 return true;
534 }
535 let unsupported = [
536 "misc_testsuite/memory-combos.wast",
538 "misc_testsuite/threads/atomics-end-of-memory.wast",
539 "misc_testsuite/threads/LB.wast",
540 "misc_testsuite/threads/LB_atomic.wast",
541 "misc_testsuite/threads/MP.wast",
542 "misc_testsuite/threads/MP_atomic.wast",
543 "misc_testsuite/threads/MP_wait.wast",
544 "misc_testsuite/threads/SB.wast",
545 "misc_testsuite/threads/SB_atomic.wast",
546 "misc_testsuite/threads/atomics_notify.wast",
547 "misc_testsuite/threads/atomics_wait_address.wast",
548 "misc_testsuite/threads/wait_notify.wast",
549 "spec_testsuite/proposals/threads/atomic.wast",
550 "spec_testsuite/proposals/threads/exports.wast",
551 "spec_testsuite/proposals/threads/memory.wast",
552 "misc_testsuite/memory64/threads.wast",
553 "misc_testsuite/winch/rmw32_cmpxchg_u_wrap.wast",
554 ];
555
556 if unsupported.iter().any(|part| self.path.ends_with(part)) {
557 return true;
558 }
559 }
560
561 if config.compiler.should_fail(&self.config) {
562 return true;
563 }
564
565 if config.compiler == Compiler::Winch {
567 let unsupported = [
569 "extended-const/elem.wast",
570 "extended-const/global.wast",
571 "misc_testsuite/component-model/modules.wast",
572 "misc_testsuite/externref-id-function.wast",
573 "misc_testsuite/externref-segment.wast",
574 "misc_testsuite/externref-segments.wast",
575 "misc_testsuite/externref-table-dropped-segment-issue-8281.wast",
576 "misc_testsuite/linking-errors.wast",
577 "misc_testsuite/many_table_gets_lead_to_gc.wast",
578 "misc_testsuite/mutable_externref_globals.wast",
579 "misc_testsuite/no-mixup-stack-maps.wast",
580 "misc_testsuite/no-panic.wast",
581 "misc_testsuite/simple_ref_is_null.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}