1 //! Generate a configuration for both Wasmtime and the Wasm module to execute.
2 
3 use super::{AsyncConfig, CodegenSettings, InstanceAllocationStrategy, MemoryConfig, ModuleConfig};
4 use crate::oracles::{StoreLimits, Timeout};
5 use anyhow::Result;
6 use arbitrary::{Arbitrary, Unstructured};
7 use std::time::Duration;
8 use wasmtime::{Enabled, Engine, Module, Store};
9 use wasmtime_test_util::wast::{WastConfig, WastTest, limits};
10 
11 /// Configuration for `wasmtime::Config` and generated modules for a session of
12 /// fuzzing.
13 ///
14 /// This configuration guides what modules are generated, how wasmtime
15 /// configuration is generated, and is typically itself generated through a call
16 /// to `Arbitrary` which allows for a form of "swarm testing".
17 #[derive(Debug, Clone)]
18 pub struct Config {
19     /// Configuration related to the `wasmtime::Config`.
20     pub wasmtime: WasmtimeConfig,
21     /// Configuration related to generated modules.
22     pub module_config: ModuleConfig,
23 }
24 
25 impl Config {
26     /// Indicates that this configuration is being used for differential
27     /// execution.
28     ///
29     /// The purpose of this function is to update the configuration which was
30     /// generated to be compatible with execution in multiple engines. The goal
31     /// is to produce the exact same result in all engines so we need to paper
32     /// over things like nan differences and memory/table behavior differences.
33     pub fn set_differential_config(&mut self) {
34         let config = &mut self.module_config.config;
35 
36         // Make it more likely that there are types available to generate a
37         // function with.
38         config.min_types = config.min_types.max(1);
39         config.max_types = config.max_types.max(1);
40 
41         // Generate at least one function
42         config.min_funcs = config.min_funcs.max(1);
43         config.max_funcs = config.max_funcs.max(1);
44 
45         // Allow a memory to be generated, but don't let it get too large.
46         // Additionally require the maximum size to guarantee that the growth
47         // behavior is consistent across engines.
48         config.max_memory32_bytes = 10 << 16;
49         config.max_memory64_bytes = 10 << 16;
50         config.memory_max_size_required = true;
51 
52         // If tables are generated make sure they don't get too large to avoid
53         // hitting any engine-specific limit. Additionally ensure that the
54         // maximum size is required to guarantee consistent growth across
55         // engines.
56         //
57         // Note that while reference types are disabled below, only allow one
58         // table.
59         config.max_table_elements = 1_000;
60         config.table_max_size_required = true;
61 
62         // Don't allow any imports
63         config.max_imports = 0;
64 
65         // Try to get the function and the memory exported
66         config.export_everything = true;
67 
68         // NaN is canonicalized at the wasm level for differential fuzzing so we
69         // can paper over NaN differences between engines.
70         config.canonicalize_nans = true;
71 
72         // If using the pooling allocator, update the instance limits too
73         if let InstanceAllocationStrategy::Pooling(pooling) = &mut self.wasmtime.strategy {
74             // One single-page memory
75             pooling.total_memories = config.max_memories as u32;
76             pooling.max_memory_size = 10 << 16;
77             pooling.max_memories_per_module = config.max_memories as u32;
78             if pooling.memory_protection_keys == Enabled::Auto
79                 && pooling.max_memory_protection_keys > 1
80             {
81                 pooling.total_memories =
82                     pooling.total_memories * (pooling.max_memory_protection_keys as u32);
83             }
84 
85             pooling.total_tables = config.max_tables as u32;
86             pooling.table_elements = 1_000;
87             pooling.max_tables_per_module = config.max_tables as u32;
88 
89             pooling.core_instance_size = 1_000_000;
90 
91             let cfg = &mut self.wasmtime.memory_config;
92             match &mut cfg.memory_reservation {
93                 Some(size) => *size = (*size).max(pooling.max_memory_size as u64),
94                 other @ None => *other = Some(pooling.max_memory_size as u64),
95             }
96         }
97 
98         // These instructions are explicitly not expected to be exactly the same
99         // across engines. Don't fuzz them.
100         config.relaxed_simd_enabled = false;
101     }
102 
103     /// Uses this configuration and the supplied source of data to generate
104     /// a wasm module.
105     ///
106     /// If a `default_fuel` is provided, the resulting module will be configured
107     /// to ensure termination; as doing so will add an additional global to the module,
108     /// the pooling allocator, if configured, will also have its globals limit updated.
109     pub fn generate(
110         &self,
111         input: &mut Unstructured<'_>,
112         default_fuel: Option<u32>,
113     ) -> arbitrary::Result<wasm_smith::Module> {
114         self.module_config.generate(input, default_fuel)
115     }
116 
117     /// Updates this configuration to be able to run the `test` specified.
118     ///
119     /// This primarily updates `self.module_config` to ensure that it enables
120     /// all features and proposals necessary to execute the `test` specified.
121     /// This will additionally update limits in the pooling allocator to be able
122     /// to execute all tests.
123     pub fn make_wast_test_compliant(&mut self, test: &WastTest) -> WastConfig {
124         let wasmtime_test_util::wast::TestConfig {
125             memory64,
126             custom_page_sizes,
127             multi_memory,
128             threads,
129             shared_everything_threads,
130             gc,
131             function_references,
132             relaxed_simd,
133             reference_types,
134             tail_call,
135             extended_const,
136             wide_arithmetic,
137             component_model_async,
138             component_model_async_builtins,
139             component_model_async_stackful,
140             component_model_error_context,
141             component_model_gc,
142             simd,
143             exceptions,
144             legacy_exceptions: _,
145 
146             hogs_memory: _,
147             nan_canonicalization: _,
148             gc_types: _,
149             stack_switching: _,
150             spec_test: _,
151         } = test.config;
152 
153         // Enable/disable some proposals that aren't configurable in wasm-smith
154         // but are configurable in Wasmtime.
155         self.module_config.function_references_enabled =
156             function_references.or(gc).unwrap_or(false);
157         self.module_config.component_model_async = component_model_async.unwrap_or(false);
158         self.module_config.component_model_async_builtins =
159             component_model_async_builtins.unwrap_or(false);
160         self.module_config.component_model_async_stackful =
161             component_model_async_stackful.unwrap_or(false);
162         self.module_config.component_model_error_context =
163             component_model_error_context.unwrap_or(false);
164         self.module_config.component_model_gc = component_model_gc.unwrap_or(false);
165 
166         // Enable/disable proposals that wasm-smith has knobs for which will be
167         // read when creating `wasmtime::Config`.
168         let config = &mut self.module_config.config;
169         config.bulk_memory_enabled = true;
170         config.multi_value_enabled = true;
171         config.wide_arithmetic_enabled = wide_arithmetic.unwrap_or(false);
172         config.memory64_enabled = memory64.unwrap_or(false);
173         config.relaxed_simd_enabled = relaxed_simd.unwrap_or(false);
174         config.simd_enabled = config.relaxed_simd_enabled || simd.unwrap_or(false);
175         config.tail_call_enabled = tail_call.unwrap_or(false);
176         config.custom_page_sizes_enabled = custom_page_sizes.unwrap_or(false);
177         config.threads_enabled = threads.unwrap_or(false);
178         config.shared_everything_threads_enabled = shared_everything_threads.unwrap_or(false);
179         config.gc_enabled = gc.unwrap_or(false);
180         config.reference_types_enabled = config.gc_enabled
181             || self.module_config.function_references_enabled
182             || reference_types.unwrap_or(false);
183         config.extended_const_enabled = extended_const.unwrap_or(false);
184         config.exceptions_enabled = exceptions.unwrap_or(false);
185         if multi_memory.unwrap_or(false) {
186             config.max_memories = limits::MEMORIES_PER_MODULE as usize;
187         } else {
188             config.max_memories = 1;
189         }
190 
191         if let Some(n) = &mut self.wasmtime.memory_config.memory_reservation {
192             *n = (*n).max(limits::MEMORY_SIZE as u64);
193         }
194 
195         // FIXME: it might be more ideal to avoid the need for this entirely
196         // and to just let the test fail. If a test fails due to a pooling
197         // allocator resource limit being met we could ideally detect that and
198         // let the fuzz test case pass. That would avoid the need to hardcode
199         // so much here and in theory wouldn't reduce the usefulness of fuzzers
200         // all that much. At this time though we can't easily test this configuration.
201         if let InstanceAllocationStrategy::Pooling(pooling) = &mut self.wasmtime.strategy {
202             // Clamp protection keys between 1 & 2 to reduce the number of
203             // slots and then multiply the total memories by the number of keys
204             // we have since a single store has access to only one key.
205             pooling.max_memory_protection_keys = pooling.max_memory_protection_keys.max(1).min(2);
206             pooling.total_memories = pooling
207                 .total_memories
208                 .max(limits::MEMORIES * (pooling.max_memory_protection_keys as u32));
209 
210             // For other limits make sure they meet the minimum threshold
211             // required for our wast tests.
212             pooling.total_component_instances = pooling
213                 .total_component_instances
214                 .max(limits::COMPONENT_INSTANCES);
215             pooling.total_tables = pooling.total_tables.max(limits::TABLES);
216             pooling.max_tables_per_module =
217                 pooling.max_tables_per_module.max(limits::TABLES_PER_MODULE);
218             pooling.max_memories_per_module = pooling
219                 .max_memories_per_module
220                 .max(limits::MEMORIES_PER_MODULE);
221             pooling.max_memories_per_component = pooling
222                 .max_memories_per_component
223                 .max(limits::MEMORIES_PER_MODULE);
224             pooling.total_core_instances = pooling.total_core_instances.max(limits::CORE_INSTANCES);
225             pooling.max_memory_size = pooling.max_memory_size.max(limits::MEMORY_SIZE);
226             pooling.table_elements = pooling.table_elements.max(limits::TABLE_ELEMENTS);
227             pooling.core_instance_size = pooling.core_instance_size.max(limits::CORE_INSTANCE_SIZE);
228             pooling.component_instance_size = pooling
229                 .component_instance_size
230                 .max(limits::CORE_INSTANCE_SIZE);
231             pooling.total_stacks = pooling.total_stacks.max(limits::TOTAL_STACKS);
232         }
233 
234         // Return the test configuration that this fuzz configuration represents
235         // which is used afterwards to test if the `test` here is expected to
236         // fail or not.
237         WastConfig {
238             collector: match self.wasmtime.collector {
239                 Collector::Null => wasmtime_test_util::wast::Collector::Null,
240                 Collector::DeferredReferenceCounting => {
241                     wasmtime_test_util::wast::Collector::DeferredReferenceCounting
242                 }
243             },
244             pooling: matches!(
245                 self.wasmtime.strategy,
246                 InstanceAllocationStrategy::Pooling(_)
247             ),
248             compiler: match self.wasmtime.compiler_strategy {
249                 CompilerStrategy::CraneliftNative => {
250                     wasmtime_test_util::wast::Compiler::CraneliftNative
251                 }
252                 CompilerStrategy::CraneliftPulley => {
253                     wasmtime_test_util::wast::Compiler::CraneliftPulley
254                 }
255                 CompilerStrategy::Winch => wasmtime_test_util::wast::Compiler::Winch,
256             },
257         }
258     }
259 
260     /// Converts this to a `wasmtime::Config` object
261     pub fn to_wasmtime(&self) -> wasmtime::Config {
262         crate::init_fuzzing();
263 
264         let mut cfg = wasmtime_cli_flags::CommonOptions::default();
265         cfg.codegen.native_unwind_info =
266             Some(cfg!(target_os = "windows") || self.wasmtime.native_unwind_info);
267         cfg.codegen.parallel_compilation = Some(false);
268 
269         cfg.debug.address_map = Some(self.wasmtime.generate_address_map);
270         cfg.opts.opt_level = Some(self.wasmtime.opt_level.to_wasmtime());
271         cfg.opts.regalloc_algorithm = Some(self.wasmtime.regalloc_algorithm.to_wasmtime());
272         cfg.opts.signals_based_traps = Some(self.wasmtime.signals_based_traps);
273         cfg.opts.memory_guaranteed_dense_image_size = Some(std::cmp::min(
274             // Clamp this at 16MiB so we don't get huge in-memory
275             // images during fuzzing.
276             16 << 20,
277             self.wasmtime.memory_guaranteed_dense_image_size,
278         ));
279         cfg.wasm.async_stack_zeroing = Some(self.wasmtime.async_stack_zeroing);
280         cfg.wasm.bulk_memory = Some(true);
281         cfg.wasm.component_model_async = Some(self.module_config.component_model_async);
282         cfg.wasm.component_model_async_builtins =
283             Some(self.module_config.component_model_async_builtins);
284         cfg.wasm.component_model_async_stackful =
285             Some(self.module_config.component_model_async_stackful);
286         cfg.wasm.component_model_error_context =
287             Some(self.module_config.component_model_error_context);
288         cfg.wasm.component_model_gc = Some(self.module_config.component_model_gc);
289         cfg.wasm.custom_page_sizes = Some(self.module_config.config.custom_page_sizes_enabled);
290         cfg.wasm.epoch_interruption = Some(self.wasmtime.epoch_interruption);
291         cfg.wasm.extended_const = Some(self.module_config.config.extended_const_enabled);
292         cfg.wasm.fuel = self.wasmtime.consume_fuel.then(|| u64::MAX);
293         cfg.wasm.function_references = Some(self.module_config.function_references_enabled);
294         cfg.wasm.gc = Some(self.module_config.config.gc_enabled);
295         cfg.wasm.memory64 = Some(self.module_config.config.memory64_enabled);
296         cfg.wasm.multi_memory = Some(self.module_config.config.max_memories > 1);
297         cfg.wasm.multi_value = Some(self.module_config.config.multi_value_enabled);
298         cfg.wasm.nan_canonicalization = Some(self.wasmtime.canonicalize_nans);
299         cfg.wasm.reference_types = Some(self.module_config.config.reference_types_enabled);
300         cfg.wasm.simd = Some(self.module_config.config.simd_enabled);
301         cfg.wasm.tail_call = Some(self.module_config.config.tail_call_enabled);
302         cfg.wasm.threads = Some(self.module_config.config.threads_enabled);
303         cfg.wasm.shared_everything_threads =
304             Some(self.module_config.config.shared_everything_threads_enabled);
305         cfg.wasm.wide_arithmetic = Some(self.module_config.config.wide_arithmetic_enabled);
306         cfg.wasm.exceptions = Some(self.module_config.config.exceptions_enabled);
307         if !self.module_config.config.simd_enabled {
308             cfg.wasm.relaxed_simd = Some(false);
309         }
310         cfg.codegen.collector = Some(self.wasmtime.collector.to_wasmtime());
311 
312         let compiler_strategy = &self.wasmtime.compiler_strategy;
313         let cranelift_strategy = match compiler_strategy {
314             CompilerStrategy::CraneliftNative | CompilerStrategy::CraneliftPulley => true,
315             CompilerStrategy::Winch => false,
316         };
317         self.wasmtime.compiler_strategy.configure(&mut cfg);
318 
319         self.wasmtime.codegen.configure(&mut cfg);
320 
321         // Determine whether we will actually enable PCC -- this is
322         // disabled if the module requires memory64, which is not yet
323         // compatible (due to the need for dynamic checks).
324         let pcc = cfg!(feature = "fuzz-pcc")
325             && self.wasmtime.pcc
326             && !self.module_config.config.memory64_enabled;
327 
328         cfg.codegen.inlining = self.wasmtime.inlining;
329 
330         // Only set cranelift specific flags when the Cranelift strategy is
331         // chosen.
332         if cranelift_strategy {
333             if let Some(option) = self.wasmtime.inlining_intra_module {
334                 cfg.codegen.cranelift.push((
335                     "wasmtime_inlining_intra_module".to_string(),
336                     Some(option.to_string()),
337                 ));
338             }
339             if let Some(size) = self.wasmtime.inlining_small_callee_size {
340                 cfg.codegen.cranelift.push((
341                     "wasmtime_inlining_small_callee_size".to_string(),
342                     // Clamp to avoid extreme code size blow up.
343                     Some(std::cmp::min(1000, size).to_string()),
344                 ));
345             }
346             if let Some(size) = self.wasmtime.inlining_sum_size_threshold {
347                 cfg.codegen.cranelift.push((
348                     "wasmtime_inlining_sum_size_threshold".to_string(),
349                     // Clamp to avoid extreme code size blow up.
350                     Some(std::cmp::min(1000, size).to_string()),
351                 ));
352             }
353 
354             // If the wasm-smith-generated module use nan canonicalization then we
355             // don't need to enable it, but if it doesn't enable it already then we
356             // enable this codegen option.
357             cfg.wasm.nan_canonicalization = Some(!self.module_config.config.canonicalize_nans);
358 
359             // Enabling the verifier will at-least-double compilation time, which
360             // with a 20-30x slowdown in fuzzing can cause issues related to
361             // timeouts. If generated modules can have more than a small handful of
362             // functions then disable the verifier when fuzzing to try to lessen the
363             // impact of timeouts.
364             if self.module_config.config.max_funcs > 10 {
365                 cfg.codegen.cranelift_debug_verifier = Some(false);
366             }
367 
368             if self.wasmtime.force_jump_veneers {
369                 cfg.codegen.cranelift.push((
370                     "wasmtime_linkopt_force_jump_veneer".to_string(),
371                     Some("true".to_string()),
372                 ));
373             }
374 
375             if let Some(pad) = self.wasmtime.padding_between_functions {
376                 cfg.codegen.cranelift.push((
377                     "wasmtime_linkopt_padding_between_functions".to_string(),
378                     Some(pad.to_string()),
379                 ));
380             }
381 
382             cfg.codegen.pcc = Some(pcc);
383 
384             // Eager init is currently only supported on Cranelift, not Winch.
385             cfg.opts.table_lazy_init = Some(self.wasmtime.table_lazy_init);
386         }
387 
388         self.wasmtime.strategy.configure(&mut cfg);
389 
390         // Vary the memory configuration, but only if threads are not enabled.
391         // When the threads proposal is enabled we might generate shared memory,
392         // which is less amenable to different memory configurations:
393         // - shared memories are required to be "static" so fuzzing the various
394         //   memory configurations will mostly result in uninteresting errors.
395         //   The interesting part about shared memories is the runtime so we
396         //   don't fuzz non-default settings.
397         // - shared memories are required to be aligned which means that the
398         //   `CustomUnaligned` variant isn't actually safe to use with a shared
399         //   memory.
400         if !self.module_config.config.threads_enabled {
401             // If PCC is enabled, force other options to be compatible: PCC is currently only
402             // supported when bounds checks are elided.
403             let memory_config = if pcc {
404                 MemoryConfig {
405                     memory_reservation: Some(4 << 30), // 4 GiB
406                     memory_guard_size: Some(2 << 30),  // 2 GiB
407                     memory_reservation_for_growth: Some(0),
408                     guard_before_linear_memory: false,
409                     memory_init_cow: true,
410                     // Doesn't matter, only using virtual memory.
411                     cranelift_enable_heap_access_spectre_mitigations: None,
412                 }
413             } else {
414                 self.wasmtime.memory_config.clone()
415             };
416 
417             memory_config.configure(&mut cfg);
418         };
419 
420         // If malloc-based memory is going to be used, which requires these four
421         // options set to specific values (and Pulley auto-sets two of them)
422         // then be sure to cap `memory_reservation_for_growth` at a smaller
423         // value than the default. For malloc-based memory reservation beyond
424         // the end of memory isn't captured by `StoreLimiter` so we need to be
425         // sure it's small enough to not blow OOM limits while fuzzing.
426         if ((cfg.opts.signals_based_traps == Some(true) && cfg.opts.memory_guard_size == Some(0))
427             || self.wasmtime.compiler_strategy == CompilerStrategy::CraneliftPulley)
428             && cfg.opts.memory_reservation == Some(0)
429             && cfg.opts.memory_init_cow == Some(false)
430         {
431             let growth = &mut cfg.opts.memory_reservation_for_growth;
432             let max = 1 << 20;
433             *growth = match *growth {
434                 Some(n) => Some(n.min(max)),
435                 None => Some(max),
436             };
437         }
438 
439         log::debug!("creating wasmtime config with CLI options:\n{cfg}");
440         let mut cfg = cfg.config(None).expect("failed to create wasmtime::Config");
441 
442         if self.wasmtime.async_config != AsyncConfig::Disabled {
443             log::debug!("async config in use {:?}", self.wasmtime.async_config);
444             self.wasmtime.async_config.configure(&mut cfg);
445         }
446 
447         return cfg;
448     }
449 
450     /// Convenience function for generating a `Store<T>` using this
451     /// configuration.
452     pub fn to_store(&self) -> Store<StoreLimits> {
453         let engine = Engine::new(&self.to_wasmtime()).unwrap();
454         let mut store = Store::new(&engine, StoreLimits::new());
455         self.configure_store(&mut store);
456         store
457     }
458 
459     /// Configures a store based on this configuration.
460     pub fn configure_store(&self, store: &mut Store<StoreLimits>) {
461         store.limiter(|s| s as &mut dyn wasmtime::ResourceLimiter);
462 
463         // Configure the store to never abort by default, that is it'll have
464         // max fuel or otherwise trap on an epoch change but the epoch won't
465         // ever change.
466         //
467         // Afterwards though see what `AsyncConfig` is being used an further
468         // refine the store's configuration based on that.
469         if self.wasmtime.consume_fuel {
470             store.set_fuel(u64::MAX).unwrap();
471         }
472         if self.wasmtime.epoch_interruption {
473             store.epoch_deadline_trap();
474             store.set_epoch_deadline(1);
475         }
476         match self.wasmtime.async_config {
477             AsyncConfig::Disabled => {}
478             AsyncConfig::YieldWithFuel(amt) => {
479                 assert!(self.wasmtime.consume_fuel);
480                 store.fuel_async_yield_interval(Some(amt)).unwrap();
481             }
482             AsyncConfig::YieldWithEpochs { ticks, .. } => {
483                 assert!(self.wasmtime.epoch_interruption);
484                 store.set_epoch_deadline(ticks);
485                 store.epoch_deadline_async_yield_and_update(ticks);
486             }
487         }
488     }
489 
490     /// Generates an arbitrary method of timing out an instance, ensuring that
491     /// this configuration supports the returned timeout.
492     pub fn generate_timeout(&mut self, u: &mut Unstructured<'_>) -> arbitrary::Result<Timeout> {
493         let time_duration = Duration::from_millis(100);
494         let timeout = u
495             .choose(&[Timeout::Fuel(100_000), Timeout::Epoch(time_duration)])?
496             .clone();
497         match &timeout {
498             Timeout::Fuel(..) => {
499                 self.wasmtime.consume_fuel = true;
500             }
501             Timeout::Epoch(..) => {
502                 self.wasmtime.epoch_interruption = true;
503             }
504             Timeout::None => unreachable!("Not an option given to choose()"),
505         }
506         Ok(timeout)
507     }
508 
509     /// Compiles the `wasm` within the `engine` provided.
510     ///
511     /// This notably will use `Module::{serialize,deserialize_file}` to
512     /// round-trip if configured in the fuzzer.
513     pub fn compile(&self, engine: &Engine, wasm: &[u8]) -> Result<Module> {
514         // Propagate this error in case the caller wants to handle
515         // valid-vs-invalid wasm.
516         let module = Module::new(engine, wasm)?;
517         if !self.wasmtime.use_precompiled_cwasm {
518             return Ok(module);
519         }
520 
521         // Don't propagate these errors to prevent them from accidentally being
522         // interpreted as invalid wasm, these should never fail on a
523         // well-behaved host system.
524         let dir = tempfile::TempDir::new().unwrap();
525         let file = dir.path().join("module.wasm");
526         std::fs::write(&file, module.serialize().unwrap()).unwrap();
527         unsafe { Ok(Module::deserialize_file(engine, &file).unwrap()) }
528     }
529 
530     /// Updates this configuration to forcibly enable async support. Only useful
531     /// in fuzzers which do async calls.
532     pub fn enable_async(&mut self, u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
533         if self.wasmtime.consume_fuel || u.arbitrary()? {
534             self.wasmtime.async_config =
535                 AsyncConfig::YieldWithFuel(u.int_in_range(1000..=100_000)?);
536             self.wasmtime.consume_fuel = true;
537         } else {
538             self.wasmtime.async_config = AsyncConfig::YieldWithEpochs {
539                 dur: Duration::from_millis(u.int_in_range(1..=10)?),
540                 ticks: u.int_in_range(1..=10)?,
541             };
542             self.wasmtime.epoch_interruption = true;
543         }
544         Ok(())
545     }
546 }
547 
548 impl<'a> Arbitrary<'a> for Config {
549     fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
550         let mut config = Self {
551             wasmtime: u.arbitrary()?,
552             module_config: u.arbitrary()?,
553         };
554 
555         config
556             .wasmtime
557             .update_module_config(&mut config.module_config, u)?;
558 
559         Ok(config)
560     }
561 }
562 
563 /// Configuration related to `wasmtime::Config` and the various settings which
564 /// can be tweaked from within.
565 #[derive(Arbitrary, Clone, Debug, Eq, Hash, PartialEq)]
566 pub struct WasmtimeConfig {
567     opt_level: OptLevel,
568     regalloc_algorithm: RegallocAlgorithm,
569     debug_info: bool,
570     canonicalize_nans: bool,
571     interruptable: bool,
572     pub(crate) consume_fuel: bool,
573     pub(crate) epoch_interruption: bool,
574     /// The Wasmtime memory configuration to use.
575     pub memory_config: MemoryConfig,
576     force_jump_veneers: bool,
577     memory_init_cow: bool,
578     memory_guaranteed_dense_image_size: u64,
579     inlining: Option<bool>,
580     inlining_intra_module: Option<IntraModuleInlining>,
581     inlining_small_callee_size: Option<u32>,
582     inlining_sum_size_threshold: Option<u32>,
583     use_precompiled_cwasm: bool,
584     async_stack_zeroing: bool,
585     /// Configuration for the instance allocation strategy to use.
586     pub strategy: InstanceAllocationStrategy,
587     codegen: CodegenSettings,
588     padding_between_functions: Option<u16>,
589     generate_address_map: bool,
590     native_unwind_info: bool,
591     /// Configuration for the compiler to use.
592     pub compiler_strategy: CompilerStrategy,
593     collector: Collector,
594     table_lazy_init: bool,
595 
596     /// Whether or not fuzzing should enable PCC.
597     pcc: bool,
598 
599     /// Configuration for whether wasm is invoked in an async fashion and how
600     /// it's cooperatively time-sliced.
601     pub async_config: AsyncConfig,
602 
603     /// Whether or not host signal handlers are enabled for this configuration,
604     /// aka whether signal handlers are supported.
605     signals_based_traps: bool,
606 }
607 
608 impl WasmtimeConfig {
609     /// Force `self` to be a configuration compatible with `other`. This is
610     /// useful for differential execution to avoid unhelpful fuzz crashes when
611     /// one engine has a feature enabled and the other does not.
612     pub fn make_compatible_with(&mut self, other: &Self) {
613         // Use the same allocation strategy between the two configs.
614         //
615         // Ideally this wouldn't be necessary, but, during differential
616         // evaluation, if the `lhs` is using ondemand and the `rhs` is using the
617         // pooling allocator (or vice versa), then the module may have been
618         // generated in such a way that is incompatible with the other
619         // allocation strategy.
620         //
621         // We can remove this in the future when it's possible to access the
622         // fields of `wasm_smith::Module` to constrain the pooling allocator
623         // based on what was actually generated.
624         self.strategy = other.strategy.clone();
625         if let InstanceAllocationStrategy::Pooling { .. } = &other.strategy {
626             // Also use the same memory configuration when using the pooling
627             // allocator.
628             self.memory_config = other.memory_config.clone();
629         }
630 
631         self.make_internally_consistent();
632     }
633 
634     /// Updates `config` to be compatible with `self` and the other way around
635     /// too.
636     pub fn update_module_config(
637         &mut self,
638         config: &mut ModuleConfig,
639         _u: &mut Unstructured<'_>,
640     ) -> arbitrary::Result<()> {
641         match self.compiler_strategy {
642             CompilerStrategy::CraneliftNative => {}
643 
644             CompilerStrategy::Winch => {
645                 // Winch is not complete on non-x64 targets, so just abandon this test
646                 // case. We don't want to force Cranelift because we change what module
647                 // config features are enabled based on the compiler strategy, and we
648                 // don't want to make the same fuzz input DNA generate different test
649                 // cases on different targets.
650                 if cfg!(not(target_arch = "x86_64")) {
651                     log::warn!(
652                         "want to compile with Winch but host architecture does not support it"
653                     );
654                     return Err(arbitrary::Error::IncorrectFormat);
655                 }
656 
657                 // Winch doesn't support the same set of wasm proposal as Cranelift
658                 // at this time, so if winch is selected be sure to disable wasm
659                 // proposals in `Config` to ensure that Winch can compile the
660                 // module that wasm-smith generates.
661                 config.config.relaxed_simd_enabled = false;
662                 config.config.gc_enabled = false;
663                 config.config.tail_call_enabled = false;
664                 config.config.reference_types_enabled = false;
665                 config.config.exceptions_enabled = false;
666                 config.function_references_enabled = false;
667 
668                 // Winch's SIMD implementations require AVX and AVX2.
669                 if self
670                     .codegen_flag("has_avx")
671                     .is_some_and(|value| value == "false")
672                     || self
673                         .codegen_flag("has_avx2")
674                         .is_some_and(|value| value == "false")
675                 {
676                     config.config.simd_enabled = false;
677                 }
678 
679                 // Tuning  the following engine options is currently not supported
680                 // by Winch.
681                 self.signals_based_traps = true;
682                 self.table_lazy_init = true;
683                 self.debug_info = false;
684             }
685 
686             CompilerStrategy::CraneliftPulley => {
687                 config.config.threads_enabled = false;
688             }
689         }
690 
691         // If using the pooling allocator, constrain the memory and module configurations
692         // to the module limits.
693         if let InstanceAllocationStrategy::Pooling(pooling) = &mut self.strategy {
694             // If the pooling allocator is used, do not allow shared memory to
695             // be created. FIXME: see
696             // https://github.com/bytecodealliance/wasmtime/issues/4244.
697             config.config.threads_enabled = false;
698 
699             // Ensure the pooling allocator can support the maximal size of
700             // memory, picking the smaller of the two to win.
701             let min_bytes = config
702                 .config
703                 .max_memory32_bytes
704                 // memory64_bytes is a u128, but since we are taking the min
705                 // we can truncate it down to a u64.
706                 .min(
707                     config
708                         .config
709                         .max_memory64_bytes
710                         .try_into()
711                         .unwrap_or(u64::MAX),
712                 );
713             let min = min_bytes
714                 .min(pooling.max_memory_size as u64)
715                 .min(self.memory_config.memory_reservation.unwrap_or(0));
716             pooling.max_memory_size = min as usize;
717             config.config.max_memory32_bytes = min;
718             config.config.max_memory64_bytes = min as u128;
719 
720             // If traps are disallowed then memories must have at least one page
721             // of memory so if we still are only allowing 0 pages of memory then
722             // increase that to one here.
723             if config.config.disallow_traps {
724                 if pooling.max_memory_size < (1 << 16) {
725                     pooling.max_memory_size = 1 << 16;
726                     config.config.max_memory32_bytes = 1 << 16;
727                     config.config.max_memory64_bytes = 1 << 16;
728                     let cfg = &mut self.memory_config;
729                     match &mut cfg.memory_reservation {
730                         Some(size) => *size = (*size).max(pooling.max_memory_size as u64),
731                         size @ None => *size = Some(pooling.max_memory_size as u64),
732                     }
733                 }
734                 // .. additionally update tables
735                 if pooling.table_elements == 0 {
736                     pooling.table_elements = 1;
737                 }
738             }
739 
740             // Don't allow too many linear memories per instance since massive
741             // virtual mappings can fail to get allocated.
742             config.config.min_memories = config.config.min_memories.min(10);
743             config.config.max_memories = config.config.max_memories.min(10);
744 
745             // Force this pooling allocator to always be able to accommodate the
746             // module that may be generated.
747             pooling.total_memories = config.config.max_memories as u32;
748             pooling.total_tables = config.config.max_tables as u32;
749         }
750 
751         if !self.signals_based_traps {
752             // At this time shared memories require a "static" memory
753             // configuration but when signals-based traps are disabled all
754             // memories are forced to the "dynamic" configuration. This is
755             // fixable with some more work on the bounds-checks side of things
756             // to do a full bounds check even on static memories, but that's
757             // left for a future PR.
758             config.config.threads_enabled = false;
759 
760             // Spectre-based heap mitigations require signal handlers so this
761             // must always be disabled if signals-based traps are disabled.
762             self.memory_config
763                 .cranelift_enable_heap_access_spectre_mitigations = None;
764         }
765 
766         self.make_internally_consistent();
767 
768         Ok(())
769     }
770 
771     /// Returns the codegen flag value, if any, for `name`.
772     pub(crate) fn codegen_flag(&self, name: &str) -> Option<&str> {
773         self.codegen.flags().iter().find_map(|(n, value)| {
774             if n == name {
775                 Some(value.as_str())
776             } else {
777                 None
778             }
779         })
780     }
781 
782     /// Helper method to handle some dependencies between various configuration
783     /// options. This is intended to be called whenever a `Config` is created or
784     /// modified to ensure that the final result is an instantiable `Config`.
785     ///
786     /// Note that in general this probably shouldn't exist and anything here can
787     /// be considered a "TODO" to go implement more stuff in Wasmtime to accept
788     /// these sorts of configurations. For now though it's intended to reflect
789     /// the current state of the engine's development.
790     fn make_internally_consistent(&mut self) {
791         if !self.signals_based_traps {
792             let cfg = &mut self.memory_config;
793             // Spectre-based heap mitigations require signal handlers so
794             // this must always be disabled if signals-based traps are
795             // disabled.
796             cfg.cranelift_enable_heap_access_spectre_mitigations = None;
797 
798             // With configuration settings that match the use of malloc for
799             // linear memories cap the `memory_reservation_for_growth` value
800             // to something reasonable to avoid OOM in fuzzing.
801             if !cfg.memory_init_cow
802                 && cfg.memory_guard_size == Some(0)
803                 && cfg.memory_reservation == Some(0)
804             {
805                 let min = 10 << 20; // 10 MiB
806                 if let Some(val) = &mut cfg.memory_reservation_for_growth {
807                     *val = (*val).min(min);
808                 } else {
809                     cfg.memory_reservation_for_growth = Some(min);
810                 }
811             }
812         }
813     }
814 }
815 
816 #[derive(Arbitrary, Clone, Debug, PartialEq, Eq, Hash)]
817 enum OptLevel {
818     None,
819     Speed,
820     SpeedAndSize,
821 }
822 
823 impl OptLevel {
824     fn to_wasmtime(&self) -> wasmtime::OptLevel {
825         match self {
826             OptLevel::None => wasmtime::OptLevel::None,
827             OptLevel::Speed => wasmtime::OptLevel::Speed,
828             OptLevel::SpeedAndSize => wasmtime::OptLevel::SpeedAndSize,
829         }
830     }
831 }
832 
833 #[derive(Arbitrary, Clone, Debug, PartialEq, Eq, Hash)]
834 enum RegallocAlgorithm {
835     Backtracking,
836     // FIXME(#11544 and #11545): rename back to `SinglePass` and handle below
837     // when those issues are fixed
838     TemporarilyDisabledSinglePass,
839 }
840 
841 impl RegallocAlgorithm {
842     fn to_wasmtime(&self) -> wasmtime::RegallocAlgorithm {
843         match self {
844             RegallocAlgorithm::Backtracking => wasmtime::RegallocAlgorithm::Backtracking,
845             RegallocAlgorithm::TemporarilyDisabledSinglePass => {
846                 wasmtime::RegallocAlgorithm::Backtracking
847             }
848         }
849     }
850 }
851 
852 #[derive(Arbitrary, Clone, Copy, Debug, PartialEq, Eq, Hash)]
853 enum IntraModuleInlining {
854     Yes,
855     No,
856     WhenUsingGc,
857 }
858 
859 impl std::fmt::Display for IntraModuleInlining {
860     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
861         match self {
862             IntraModuleInlining::Yes => write!(f, "yes"),
863             IntraModuleInlining::No => write!(f, "no"),
864             IntraModuleInlining::WhenUsingGc => write!(f, "gc"),
865         }
866     }
867 }
868 
869 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
870 /// Compiler to use.
871 pub enum CompilerStrategy {
872     /// Cranelift compiler for the native architecture.
873     CraneliftNative,
874     /// Winch compiler.
875     Winch,
876     /// Cranelift compiler for the native architecture.
877     CraneliftPulley,
878 }
879 
880 impl CompilerStrategy {
881     /// Configures `config` to use this compilation strategy
882     pub fn configure(&self, config: &mut wasmtime_cli_flags::CommonOptions) {
883         match self {
884             CompilerStrategy::CraneliftNative => {
885                 config.codegen.compiler = Some(wasmtime::Strategy::Cranelift);
886             }
887             CompilerStrategy::Winch => {
888                 config.codegen.compiler = Some(wasmtime::Strategy::Winch);
889             }
890             CompilerStrategy::CraneliftPulley => {
891                 config.codegen.compiler = Some(wasmtime::Strategy::Cranelift);
892                 config.target = Some("pulley64".to_string());
893             }
894         }
895     }
896 }
897 
898 impl Arbitrary<'_> for CompilerStrategy {
899     fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
900         // Favor fuzzing native cranelift, but if allowed also enable
901         // winch/pulley.
902         match u.int_in_range(0..=19)? {
903             1 => Ok(Self::CraneliftPulley),
904             2 => Ok(Self::Winch),
905             _ => Ok(Self::CraneliftNative),
906         }
907     }
908 }
909 
910 #[derive(Arbitrary, Clone, Debug, PartialEq, Eq, Hash)]
911 pub enum Collector {
912     DeferredReferenceCounting,
913     Null,
914 }
915 
916 impl Collector {
917     fn to_wasmtime(&self) -> wasmtime::Collector {
918         match self {
919             Collector::DeferredReferenceCounting => wasmtime::Collector::DeferredReferenceCounting,
920             Collector::Null => wasmtime::Collector::Null,
921         }
922     }
923 }
924