Skip to main content

wasmtime_test_util/
wasmtime_wast.rs

1//! Translation of test configuration in [`crate::wast`] into Wasmtime
2//! configuration.
3//!
4//! Everything is expressed as [`CommonOptions`], the same options the
5//! `wasmtime` CLI accepts. Harnesses follow the same steps:
6//!
7//! 1. Start from [`Compiler::common_options`], or their own options.
8//! 2. Make their own adjustments, such as memory limits.
9//! 3. Apply the test with [`TestConfig::apply`], last so the test has the
10//!    final say.
11//! 4. Create a [`wasmtime::Config`] with [`CommonOptions::config`] and
12//!    configure each [`wasmtime::Store`] with
13//!    [`CommonOptions::configure_store`].
14
15use crate::wast::{Compiler, TestConfig};
16use wasmtime::{Result, bail, format_err};
17use wasmtime_cli_flags::CommonOptions;
18
19impl TestConfig {
20    /// Applies this test's configuration on top of `opts`.
21    ///
22    /// This sets the test's WebAssembly features and then applies its own
23    /// `flags`, which override anything already in `opts`.
24    ///
25    /// Returns an error if the flags set a WebAssembly feature, which must use
26    /// the boolean options of `TestConfig` instead, or an option that the
27    /// harness owns because it's described by [`crate::wast::WastConfig`]: the compiler,
28    /// the target, or the pooling allocator.
29    pub fn apply(&self, opts: &mut CommonOptions) -> Result<()> {
30        self.apply_features(opts);
31        if self.guest_debug() {
32            // Guest debugging is incompatible with signals-based traps.
33            opts.debug.guest_debug = Some(true);
34            opts.opts.signals_based_traps = Some(false);
35        }
36        let mut flags = self.flags()?;
37        opts.merge(&mut flags)?;
38
39        // Reject `flags` that are supposed to be configured elsewhere in the
40        // harness for testing wasts and such. This ensures that `should_fail`
41        // calculations have all relevant info to make a decision since `flags`
42        // aren't factored in at all there.
43        if let Some(name) = flags.wasm_features_specified().first() {
44            bail!("`flags` cannot set the wasm feature `-W {name}`, use `;;!` feature options");
45        }
46        if flags.codegen.compiler.is_some() {
47            bail!("`flags` cannot set `-C compiler`, it's chosen by the harness");
48        }
49        if flags.target.is_some() {
50            bail!("`flags` cannot set `--target`, it's chosen by the harness");
51        }
52        if flags.opts.pooling_allocator.is_some() {
53            bail!("`flags` cannot set `-O pooling-allocator`, it's chosen by the harness");
54        }
55        Ok(())
56    }
57
58    /// Sets the WebAssembly features in `opts` to those of this test.
59    ///
60    /// Every feature is set explicitly, and features the test doesn't list
61    /// are disabled. This ensures tests are annotated with the features they
62    /// need, even after a feature becomes enabled by default. Features that
63    /// enabled features depend on, such as `function_references` for `gc`,
64    /// are enabled too.
65    fn apply_features(&self, opts: &mut CommonOptions) {
66        let TestConfig {
67            bulk_memory,
68            memory64,
69            custom_page_sizes,
70            multi_memory,
71            threads,
72            gc,
73            function_references,
74            relaxed_simd,
75            reference_types,
76            tail_call,
77            extended_const,
78            wide_arithmetic,
79            compact_imports,
80            component_model_async,
81            component_model_more_async_builtins,
82            component_model_async_stackful,
83            component_model_threading,
84            component_model_error_context,
85            component_model_gc,
86            component_model_map,
87            component_model_memory64,
88            component_model_fixed_length_lists,
89            component_model_implements,
90            component_model_canonical_names,
91            component_model_accessors,
92            simd,
93            exceptions,
94            legacy_exceptions,
95            stack_switching,
96            // Not implemented in Wasmtime yet.
97            custom_descriptors: _,
98
99            // Not features.
100            hogs_memory: _,
101            guest_debug: _,
102            spec_test: _,
103            flags: _,
104        } = *self;
105
106        let on = |feature: Option<bool>| feature.unwrap_or(false);
107
108        // Enable the features that enabled features depend on, so tests only
109        // need to list the feature they're testing.
110        let gc = on(gc);
111        let function_references = gc || on(function_references);
112        let reference_types =
113            function_references || on(component_model_async) || on(reference_types);
114        let relaxed_simd = on(relaxed_simd);
115        let simd = relaxed_simd || on(simd);
116        let stack_switching = on(stack_switching);
117        let exceptions = stack_switching || on(exceptions);
118
119        let w = &mut opts.wasm;
120        w.bulk_memory = Some(on(bulk_memory));
121        w.memory64 = Some(on(memory64));
122        w.custom_page_sizes = Some(on(custom_page_sizes));
123        w.multi_memory = Some(on(multi_memory));
124        w.threads = Some(on(threads));
125        w.gc = Some(gc);
126        w.function_references = Some(function_references);
127        w.relaxed_simd = Some(relaxed_simd);
128        w.reference_types = Some(reference_types);
129        w.tail_call = Some(on(tail_call));
130        w.extended_const = Some(on(extended_const));
131        w.wide_arithmetic = Some(on(wide_arithmetic));
132        w.compact_imports = Some(on(compact_imports));
133        w.component_model_async = Some(on(component_model_async));
134        w.component_model_more_async_builtins = Some(on(component_model_more_async_builtins));
135        w.component_model_async_stackful = Some(on(component_model_async_stackful));
136        w.component_model_threading = Some(on(component_model_threading));
137        w.component_model_error_context = Some(on(component_model_error_context));
138        w.component_model_gc = Some(on(component_model_gc));
139        w.component_model_map = Some(on(component_model_map));
140        w.component_model_memory64 = Some(on(component_model_memory64));
141        w.component_model_fixed_length_lists = Some(on(component_model_fixed_length_lists));
142        w.component_model_implements = Some(on(component_model_implements));
143        w.component_model_canonical_names = Some(on(component_model_canonical_names));
144        w.component_model_accessors = Some(on(component_model_accessors));
145        w.simd = Some(simd);
146        w.exceptions = Some(exceptions);
147        w.legacy_exceptions = Some(on(legacy_exceptions));
148        w.stack_switching = Some(stack_switching);
149
150        // Multi-value is part of the baseline that all tests assume.
151        w.multi_value = Some(true);
152
153        // GC infrastructure in Wasmtime is needed for any GC reference.
154        w.gc_support = Some(gc || exceptions || reference_types);
155    }
156
157    /// Parses this test's `flags`. Options that the flags don't specify are
158    /// unset.
159    fn flags(&self) -> Result<CommonOptions> {
160        use clap::Parser;
161
162        let mut args = vec!["wasmtime"];
163        if let Some(flags) = &self.flags {
164            args.extend(flags.to_vec());
165        }
166        let mut flags = CommonOptions::try_parse_from(&args)
167            .map_err(|e| format_err!("failed to parse `flags`: {e}"))?;
168        // Tests are configured by their `flags`, not the environment.
169        flags.ignore_env = true;
170        Ok(flags)
171    }
172}
173
174impl Compiler {
175    /// Returns the options to run tests with this compiler.
176    pub fn common_options(&self) -> CommonOptions {
177        use wasmtime_environ::TripleExt;
178
179        let mut opts = CommonOptions::new();
180        // Tests shouldn't read or write the global compilation cache.
181        opts.codegen.cache = Some(false);
182
183        opts.codegen.compiler = Some(match self {
184            Compiler::CraneliftNative | Compiler::CraneliftPulley => wasmtime::Strategy::Cranelift,
185            Compiler::Winch => wasmtime::Strategy::Winch,
186        });
187        if let Compiler::CraneliftPulley = self {
188            opts.target = Some(target_lexicon::Triple::pulley_host().to_string());
189        }
190        opts
191    }
192}