Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/fuzzing/src/oracles.rs
1693 views
1
//! Oracles.
2
//!
3
//! Oracles take a test case and determine whether we have a bug. For example,
4
//! one of the simplest oracles is to take a Wasm binary as our input test case,
5
//! validate and instantiate it, and (implicitly) check that no assertions
6
//! failed or segfaults happened. A more complicated oracle might compare the
7
//! result of executing a Wasm file with and without optimizations enabled, and
8
//! make sure that the two executions are observably identical.
9
//!
10
//! When an oracle finds a bug, it should report it to the fuzzing engine by
11
//! panicking.
12
13
#[cfg(feature = "fuzz-spec-interpreter")]
14
pub mod diff_spec;
15
pub mod diff_wasmi;
16
pub mod diff_wasmtime;
17
pub mod dummy;
18
pub mod engine;
19
pub mod memory;
20
mod stacks;
21
22
use self::diff_wasmtime::WasmtimeInstance;
23
use self::engine::{DiffEngine, DiffInstance};
24
use crate::generators::{self, CompilerStrategy, DiffValue, DiffValueType};
25
use crate::single_module_fuzzer::KnownValid;
26
use arbitrary::Arbitrary;
27
pub use stacks::check_stacks;
28
use std::future::Future;
29
use std::pin::Pin;
30
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
31
use std::sync::{Arc, Condvar, Mutex};
32
use std::task::{Context, Poll, Waker};
33
use std::time::{Duration, Instant};
34
use wasmtime::*;
35
use wasmtime_wast::WastContext;
36
37
#[cfg(not(any(windows, target_arch = "s390x", target_arch = "riscv64")))]
38
mod diff_v8;
39
40
static CNT: AtomicUsize = AtomicUsize::new(0);
41
42
/// Logs a wasm file to the filesystem to make it easy to figure out what wasm
43
/// was used when debugging.
44
pub fn log_wasm(wasm: &[u8]) {
45
super::init_fuzzing();
46
47
if !log::log_enabled!(log::Level::Debug) {
48
return;
49
}
50
51
let i = CNT.fetch_add(1, SeqCst);
52
let name = format!("testcase{i}.wasm");
53
std::fs::write(&name, wasm).expect("failed to write wasm file");
54
log::debug!("wrote wasm file to `{name}`");
55
let wat = format!("testcase{i}.wat");
56
match wasmprinter::print_bytes(wasm) {
57
Ok(s) => std::fs::write(&wat, s).expect("failed to write wat file"),
58
// If wasmprinter failed remove a `*.wat` file, if any, to avoid
59
// confusing a preexisting one with this wasm which failed to get
60
// printed.
61
Err(_) => drop(std::fs::remove_file(&wat)),
62
}
63
}
64
65
/// The `T` in `Store<T>` for fuzzing stores, used to limit resource
66
/// consumption during fuzzing.
67
#[derive(Clone)]
68
pub struct StoreLimits(Arc<LimitsState>);
69
70
struct LimitsState {
71
/// Remaining memory, in bytes, left to allocate
72
remaining_memory: AtomicUsize,
73
/// Remaining amount of memory that's allowed to be copied via a growth.
74
remaining_copy_allowance: AtomicUsize,
75
/// Whether or not an allocation request has been denied
76
oom: AtomicBool,
77
}
78
79
/// Allow up to 1G which is well below the 2G limit on OSS-Fuzz and should allow
80
/// most interesting behavior.
81
const MAX_MEMORY: usize = 1 << 30;
82
83
/// Allow up to 4G of bytes to be copied (conservatively) which should enable
84
/// growth up to `MAX_MEMORY` or at least up to a relatively large amount.
85
const MAX_MEMORY_MOVED: usize = 4 << 30;
86
87
impl StoreLimits {
88
/// Creates the default set of limits for all fuzzing stores.
89
pub fn new() -> StoreLimits {
90
StoreLimits(Arc::new(LimitsState {
91
remaining_memory: AtomicUsize::new(MAX_MEMORY),
92
remaining_copy_allowance: AtomicUsize::new(MAX_MEMORY_MOVED),
93
oom: AtomicBool::new(false),
94
}))
95
}
96
97
fn alloc(&mut self, amt: usize) -> bool {
98
log::trace!("alloc {amt:#x} bytes");
99
100
// Assume that on each allocation of memory that all previous
101
// allocations of memory are moved. This is pretty coarse but is used to
102
// help prevent against fuzz test cases that just move tons of bytes
103
// around continuously. This assumes that all previous memory was
104
// allocated in a single linear memory and growing by `amt` will require
105
// moving all the bytes to a new location. This isn't actually required
106
// all the time nor does it accurately reflect what happens all the
107
// time, but it's a coarse approximation that should be "good enough"
108
// for allowing interesting fuzz behaviors to happen while not timing
109
// out just copying bytes around.
110
let prev_size = MAX_MEMORY - self.0.remaining_memory.load(SeqCst);
111
if self
112
.0
113
.remaining_copy_allowance
114
.fetch_update(SeqCst, SeqCst, |remaining| remaining.checked_sub(prev_size))
115
.is_err()
116
{
117
self.0.oom.store(true, SeqCst);
118
log::debug!("-> too many bytes moved, rejecting allocation");
119
return false;
120
}
121
122
// If we're allowed to move the bytes, then also check if we're allowed
123
// to actually have this much residence at once.
124
match self
125
.0
126
.remaining_memory
127
.fetch_update(SeqCst, SeqCst, |remaining| remaining.checked_sub(amt))
128
{
129
Ok(_) => true,
130
Err(_) => {
131
self.0.oom.store(true, SeqCst);
132
log::debug!("-> OOM hit");
133
false
134
}
135
}
136
}
137
138
fn is_oom(&self) -> bool {
139
self.0.oom.load(SeqCst)
140
}
141
}
142
143
impl ResourceLimiter for StoreLimits {
144
fn memory_growing(
145
&mut self,
146
current: usize,
147
desired: usize,
148
_maximum: Option<usize>,
149
) -> Result<bool> {
150
Ok(self.alloc(desired - current))
151
}
152
153
fn table_growing(
154
&mut self,
155
current: usize,
156
desired: usize,
157
_maximum: Option<usize>,
158
) -> Result<bool> {
159
let delta = (desired - current).saturating_mul(std::mem::size_of::<usize>());
160
Ok(self.alloc(delta))
161
}
162
}
163
164
/// Methods of timing out execution of a WebAssembly module
165
#[derive(Clone, Debug)]
166
pub enum Timeout {
167
/// No timeout is used, it should be guaranteed via some other means that
168
/// the input does not infinite loop.
169
None,
170
/// Fuel-based timeouts are used where the specified fuel is all that the
171
/// provided wasm module is allowed to consume.
172
Fuel(u64),
173
/// An epoch-interruption-based timeout is used with a sleeping
174
/// thread bumping the epoch counter after the specified duration.
175
Epoch(Duration),
176
}
177
178
/// Instantiate the Wasm buffer, and implicitly fail if we have an unexpected
179
/// panic or segfault or anything else that can be detected "passively".
180
///
181
/// The engine will be configured using provided config.
182
pub fn instantiate(
183
wasm: &[u8],
184
known_valid: KnownValid,
185
config: &generators::Config,
186
timeout: Timeout,
187
) {
188
let mut store = config.to_store();
189
190
let module = match compile_module(store.engine(), wasm, known_valid, config) {
191
Some(module) => module,
192
None => return,
193
};
194
195
let mut timeout_state = HelperThread::default();
196
match timeout {
197
Timeout::Fuel(fuel) => store.set_fuel(fuel).unwrap(),
198
199
// If a timeout is requested then we spawn a helper thread to wait for
200
// the requested time and then send us a signal to get interrupted. We
201
// also arrange for the thread's sleep to get interrupted if we return
202
// early (or the wasm returns within the time limit), which allows the
203
// thread to get torn down.
204
//
205
// This prevents us from creating a huge number of sleeping threads if
206
// this function is executed in a loop, like it does on nightly fuzzing
207
// infrastructure.
208
Timeout::Epoch(timeout) => {
209
let engine = store.engine().clone();
210
timeout_state.run_periodically(timeout, move || engine.increment_epoch());
211
}
212
Timeout::None => {}
213
}
214
215
instantiate_with_dummy(&mut store, &module);
216
}
217
218
/// Represents supported commands to the `instantiate_many` function.
219
#[derive(Arbitrary, Debug)]
220
pub enum Command {
221
/// Instantiates a module.
222
///
223
/// The value is the index of the module to instantiate.
224
///
225
/// The module instantiated will be this value modulo the number of modules provided to `instantiate_many`.
226
Instantiate(usize),
227
/// Terminates a "running" instance.
228
///
229
/// The value is the index of the instance to terminate.
230
///
231
/// The instance terminated will be this value modulo the number of currently running
232
/// instances.
233
///
234
/// If no instances are running, the command will be ignored.
235
Terminate(usize),
236
}
237
238
/// Instantiates many instances from the given modules.
239
///
240
/// The engine will be configured using the provided config.
241
///
242
/// The modules are expected to *not* have start functions as no timeouts are configured.
243
pub fn instantiate_many(
244
modules: &[Vec<u8>],
245
known_valid: KnownValid,
246
config: &generators::Config,
247
commands: &[Command],
248
) {
249
log::debug!("instantiate_many: {commands:#?}");
250
251
assert!(!config.module_config.config.allow_start_export);
252
253
let engine = Engine::new(&config.to_wasmtime()).unwrap();
254
255
let modules = modules
256
.iter()
257
.enumerate()
258
.filter_map(
259
|(i, bytes)| match compile_module(&engine, bytes, known_valid, config) {
260
Some(m) => {
261
log::debug!("successfully compiled module {i}");
262
Some(m)
263
}
264
None => {
265
log::debug!("failed to compile module {i}");
266
None
267
}
268
},
269
)
270
.collect::<Vec<_>>();
271
272
// If no modules were valid, we're done
273
if modules.is_empty() {
274
return;
275
}
276
277
// This stores every `Store` where a successful instantiation takes place
278
let mut stores = Vec::new();
279
let limits = StoreLimits::new();
280
281
for command in commands {
282
match command {
283
Command::Instantiate(index) => {
284
let index = *index % modules.len();
285
log::info!("instantiating {index}");
286
let module = &modules[index];
287
let mut store = Store::new(&engine, limits.clone());
288
config.configure_store(&mut store);
289
290
if instantiate_with_dummy(&mut store, module).is_some() {
291
stores.push(Some(store));
292
} else {
293
log::warn!("instantiation failed");
294
}
295
}
296
Command::Terminate(index) => {
297
if stores.is_empty() {
298
continue;
299
}
300
let index = *index % stores.len();
301
302
log::info!("dropping {index}");
303
stores.swap_remove(index);
304
}
305
}
306
}
307
}
308
309
fn compile_module(
310
engine: &Engine,
311
bytes: &[u8],
312
known_valid: KnownValid,
313
config: &generators::Config,
314
) -> Option<Module> {
315
log_wasm(bytes);
316
317
fn is_pcc_error(e: &anyhow::Error) -> bool {
318
// NOTE: please keep this predicate in sync with the display format of CodegenError,
319
// defined in `wasmtime/cranelift/codegen/src/result.rs`
320
e.to_string().to_lowercase().contains("proof-carrying-code")
321
}
322
323
match config.compile(engine, bytes) {
324
Ok(module) => Some(module),
325
Err(e) if is_pcc_error(&e) => {
326
panic!("pcc error in input: {e:#?}");
327
}
328
Err(_) if known_valid == KnownValid::No => None,
329
Err(e) => {
330
if let generators::InstanceAllocationStrategy::Pooling(c) = &config.wasmtime.strategy {
331
// When using the pooling allocator, accept failures to compile
332
// when arbitrary table element limits have been exceeded as
333
// there is currently no way to constrain the generated module
334
// table types.
335
let string = format!("{e:?}");
336
if string.contains("minimum element size") {
337
return None;
338
}
339
340
// Allow modules-failing-to-compile which exceed the requested
341
// size for each instance. This is something that is difficult
342
// to control and ensure it always succeeds, so we simply have a
343
// "random" instance size limit and if a module doesn't fit we
344
// move on to the next fuzz input.
345
if string.contains("instance allocation for this module requires") {
346
return None;
347
}
348
349
// If the pooling allocator is more restrictive on the number of
350
// tables and memories than we allowed wasm-smith to generate
351
// then allow compilation errors along those lines.
352
if c.max_tables_per_module < (config.module_config.config.max_tables as u32)
353
&& string.contains("defined tables count")
354
&& string.contains("exceeds the per-instance limit")
355
{
356
return None;
357
}
358
359
if c.max_memories_per_module < (config.module_config.config.max_memories as u32)
360
&& string.contains("defined memories count")
361
&& string.contains("exceeds the per-instance limit")
362
{
363
return None;
364
}
365
}
366
367
panic!("failed to compile module: {e:?}");
368
}
369
}
370
}
371
372
/// Create a Wasmtime [`Instance`] from a [`Module`] and fill in all imports
373
/// with dummy values (e.g., zeroed values, immediately-trapping functions).
374
/// Also, this function catches certain fuzz-related instantiation failures and
375
/// returns `None` instead of panicking.
376
///
377
/// TODO: we should implement tracing versions of these dummy imports that
378
/// record a trace of the order that imported functions were called in and with
379
/// what values. Like the results of exported functions, calls to imports should
380
/// also yield the same values for each configuration, and we should assert
381
/// that.
382
pub fn instantiate_with_dummy(store: &mut Store<StoreLimits>, module: &Module) -> Option<Instance> {
383
// Creation of imports can fail due to resource limit constraints, and then
384
// instantiation can naturally fail for a number of reasons as well. Bundle
385
// the two steps together to match on the error below.
386
let linker = dummy::dummy_linker(store, module);
387
if let Err(e) = &linker {
388
log::warn!("failed to create dummy linker: {e:?}");
389
}
390
let instance = linker.and_then(|l| l.instantiate(&mut *store, module));
391
unwrap_instance(store, instance)
392
}
393
394
fn unwrap_instance(
395
store: &Store<StoreLimits>,
396
instance: anyhow::Result<Instance>,
397
) -> Option<Instance> {
398
let e = match instance {
399
Ok(i) => return Some(i),
400
Err(e) => e,
401
};
402
403
log::debug!("failed to instantiate: {e:?}");
404
405
// If the instantiation hit OOM for some reason then that's ok, it's
406
// expected that fuzz-generated programs try to allocate lots of
407
// stuff.
408
if store.data().is_oom() {
409
return None;
410
}
411
412
// Allow traps which can happen normally with `unreachable` or a timeout or
413
// such.
414
if e.is::<Trap>()
415
// Also allow failures to instantiate as a result of hitting pooling
416
// limits.
417
|| e.is::<wasmtime::PoolConcurrencyLimitError>()
418
// And GC heap OOMs.
419
|| e.is::<wasmtime::GcHeapOutOfMemory<()>>()
420
// And thrown exceptions.
421
|| e.is::<wasmtime::ThrownException>()
422
{
423
return None;
424
}
425
426
let string = e.to_string();
427
428
// Currently we instantiate with a `Linker` which can't instantiate
429
// every single module under the sun due to using name-based resolution
430
// rather than positional-based resolution
431
if string.contains("incompatible import type") {
432
return None;
433
}
434
435
// Everything else should be a bug in the fuzzer or a bug in wasmtime
436
panic!("failed to instantiate: {e:?}");
437
}
438
439
/// Evaluate the function identified by `name` in two different engine
440
/// instances--`lhs` and `rhs`.
441
///
442
/// Returns `Ok(true)` if more evaluations can happen or `Ok(false)` if the
443
/// instances may have drifted apart and no more evaluations can happen.
444
///
445
/// # Panics
446
///
447
/// This will panic if the evaluation is different between engines (e.g.,
448
/// results are different, hashed instance is different, one side traps, etc.).
449
pub fn differential(
450
lhs: &mut dyn DiffInstance,
451
lhs_engine: &dyn DiffEngine,
452
rhs: &mut WasmtimeInstance,
453
name: &str,
454
args: &[DiffValue],
455
result_tys: &[DiffValueType],
456
) -> anyhow::Result<bool> {
457
log::debug!("Evaluating: `{name}` with {args:?}");
458
let lhs_results = match lhs.evaluate(name, args, result_tys) {
459
Ok(Some(results)) => Ok(results),
460
Err(e) => Err(e),
461
// this engine couldn't execute this type signature, so discard this
462
// execution by returning success.
463
Ok(None) => return Ok(true),
464
};
465
log::debug!(" -> lhs results on {}: {:?}", lhs.name(), &lhs_results);
466
467
let rhs_results = rhs
468
.evaluate(name, args, result_tys)
469
// wasmtime should be able to invoke any signature, so unwrap this result
470
.map(|results| results.unwrap());
471
log::debug!(" -> rhs results on {}: {:?}", rhs.name(), &rhs_results);
472
473
// If Wasmtime hit its OOM condition, which is possible since it's set
474
// somewhat low while fuzzing, then don't return an error but return
475
// `false` indicating that differential fuzzing must stop. There's no
476
// guarantee the other engine has the same OOM limits as Wasmtime, and
477
// it's assumed that Wasmtime is configured to have a more conservative
478
// limit than the other engine.
479
if rhs.is_oom() {
480
return Ok(false);
481
}
482
483
match DiffEqResult::new(lhs_engine, lhs_results, rhs_results) {
484
DiffEqResult::Success(lhs, rhs) => assert_eq!(lhs, rhs),
485
DiffEqResult::Poisoned => return Ok(false),
486
DiffEqResult::Failed => {}
487
}
488
489
for (global, ty) in rhs.exported_globals() {
490
log::debug!("Comparing global `{global}`");
491
let lhs = match lhs.get_global(&global, ty) {
492
Some(val) => val,
493
None => continue,
494
};
495
let rhs = rhs.get_global(&global, ty).unwrap();
496
assert_eq!(lhs, rhs);
497
}
498
for (memory, shared) in rhs.exported_memories() {
499
log::debug!("Comparing memory `{memory}`");
500
let lhs = match lhs.get_memory(&memory, shared) {
501
Some(val) => val,
502
None => continue,
503
};
504
let rhs = rhs.get_memory(&memory, shared).unwrap();
505
if lhs == rhs {
506
continue;
507
}
508
eprintln!("differential memory is {} bytes long", lhs.len());
509
eprintln!("wasmtime memory is {} bytes long", rhs.len());
510
panic!("memories have differing values");
511
}
512
513
Ok(true)
514
}
515
516
/// Result of comparing the result of two operations during differential
517
/// execution.
518
pub enum DiffEqResult<T, U> {
519
/// Both engines succeeded.
520
Success(T, U),
521
/// The result has reached the state where engines may have diverged and
522
/// results can no longer be compared.
523
Poisoned,
524
/// Both engines failed with the same error message, and internal state
525
/// should still match between the two engines.
526
Failed,
527
}
528
529
fn wasmtime_trap_is_non_deterministic(trap: &Trap) -> bool {
530
match trap {
531
// Allocations being too large for the GC are
532
// implementation-defined.
533
Trap::AllocationTooLarge |
534
// Stack size, and therefore when overflow happens, is
535
// implementation-defined.
536
Trap::StackOverflow => true,
537
_ => false,
538
}
539
}
540
541
fn wasmtime_error_is_non_deterministic(error: &wasmtime::Error) -> bool {
542
match error.downcast_ref::<Trap>() {
543
Some(trap) => wasmtime_trap_is_non_deterministic(trap),
544
545
// For general, unknown errors, we can't rely on this being
546
// a deterministic Wasm failure that both engines handled
547
// identically, leaving Wasm in identical states. We could
548
// just as easily be hitting engine-specific failures, like
549
// different implementation-defined limits. So simply poison
550
// this execution and move on to the next test.
551
None => true,
552
}
553
}
554
555
impl<T, U> DiffEqResult<T, U> {
556
/// Computes the differential result from executing in two different
557
/// engines.
558
pub fn new(
559
lhs_engine: &dyn DiffEngine,
560
lhs_result: Result<T>,
561
rhs_result: Result<U>,
562
) -> DiffEqResult<T, U> {
563
match (lhs_result, rhs_result) {
564
(Ok(lhs_result), Ok(rhs_result)) => DiffEqResult::Success(lhs_result, rhs_result),
565
566
// Handle all non-deterministic errors by poisoning this execution's
567
// state, so that we simply move on to the next test.
568
(Err(lhs), _) if lhs_engine.is_non_deterministic_error(&lhs) => {
569
log::debug!("lhs failed non-deterministically: {lhs:?}");
570
DiffEqResult::Poisoned
571
}
572
(_, Err(rhs)) if wasmtime_error_is_non_deterministic(&rhs) => {
573
log::debug!("rhs failed non-deterministically: {rhs:?}");
574
DiffEqResult::Poisoned
575
}
576
577
// Both sides failed deterministically. Check that the trap and
578
// state at the time of failure is the same.
579
(Err(lhs), Err(rhs)) => {
580
let rhs = rhs
581
.downcast::<Trap>()
582
.expect("non-traps handled in earlier match arm");
583
584
debug_assert!(
585
!lhs_engine.is_non_deterministic_error(&lhs),
586
"non-deterministic traps handled in earlier match arm",
587
);
588
debug_assert!(
589
!wasmtime_trap_is_non_deterministic(&rhs),
590
"non-deterministic traps handled in earlier match arm",
591
);
592
593
lhs_engine.assert_error_match(&lhs, &rhs);
594
DiffEqResult::Failed
595
}
596
597
// A real bug is found if only one side fails.
598
(Ok(_), Err(err)) => panic!("only the `rhs` failed for this input: {err:?}"),
599
(Err(err), Ok(_)) => panic!("only the `lhs` failed for this input: {err:?}"),
600
}
601
}
602
}
603
604
/// Invoke the given API calls.
605
pub fn make_api_calls(api: generators::api::ApiCalls) {
606
use crate::generators::api::ApiCall;
607
use std::collections::HashMap;
608
609
let mut store: Option<Store<StoreLimits>> = None;
610
let mut modules: HashMap<usize, Module> = Default::default();
611
let mut instances: HashMap<usize, Instance> = Default::default();
612
613
for call in api.calls {
614
match call {
615
ApiCall::StoreNew(config) => {
616
log::trace!("creating store");
617
assert!(store.is_none());
618
store = Some(config.to_store());
619
}
620
621
ApiCall::ModuleNew { id, wasm } => {
622
log::debug!("creating module: {id}");
623
log_wasm(&wasm);
624
let module = match Module::new(store.as_ref().unwrap().engine(), &wasm) {
625
Ok(m) => m,
626
Err(_) => continue,
627
};
628
let old = modules.insert(id, module);
629
assert!(old.is_none());
630
}
631
632
ApiCall::ModuleDrop { id } => {
633
log::trace!("dropping module: {id}");
634
drop(modules.remove(&id));
635
}
636
637
ApiCall::InstanceNew { id, module } => {
638
log::trace!("instantiating module {module} as {id}");
639
let module = match modules.get(&module) {
640
Some(m) => m,
641
None => continue,
642
};
643
644
let store = store.as_mut().unwrap();
645
if let Some(instance) = instantiate_with_dummy(store, module) {
646
instances.insert(id, instance);
647
}
648
}
649
650
ApiCall::InstanceDrop { id } => {
651
log::trace!("dropping instance {id}");
652
instances.remove(&id);
653
}
654
655
ApiCall::CallExportedFunc { instance, nth } => {
656
log::trace!("calling instance export {instance} / {nth}");
657
let instance = match instances.get(&instance) {
658
Some(i) => i,
659
None => {
660
// Note that we aren't guaranteed to instantiate valid
661
// modules, see comments in `InstanceNew` for details on
662
// that. But the API call generator can't know if
663
// instantiation failed, so we might not actually have
664
// this instance. When that's the case, just skip the
665
// API call and keep going.
666
continue;
667
}
668
};
669
let store = store.as_mut().unwrap();
670
671
let funcs = instance
672
.exports(&mut *store)
673
.filter_map(|e| match e.into_extern() {
674
Extern::Func(f) => Some(f),
675
_ => None,
676
})
677
.collect::<Vec<_>>();
678
679
if funcs.is_empty() {
680
continue;
681
}
682
683
let nth = nth % funcs.len();
684
let f = &funcs[nth];
685
let ty = f.ty(&store);
686
if let Some(params) = ty
687
.params()
688
.map(|p| p.default_value())
689
.collect::<Option<Vec<_>>>()
690
{
691
let mut results = vec![Val::I32(0); ty.results().len()];
692
let _ = f.call(store, &params, &mut results);
693
}
694
}
695
}
696
}
697
}
698
699
/// Executes the wast `test` with the `config` specified.
700
///
701
/// Ensures that wast tests pass regardless of the `Config`.
702
pub fn wast_test(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<()> {
703
crate::init_fuzzing();
704
705
let mut fuzz_config: generators::Config = u.arbitrary()?;
706
let test: generators::WastTest = u.arbitrary()?;
707
708
let test = &test.test;
709
710
if test.config.component_model_async() || u.arbitrary()? {
711
fuzz_config.enable_async(u)?;
712
}
713
714
// Discard tests that allocate a lot of memory as we don't want to OOM the
715
// fuzzer and we also limit memory growth which would cause the test to
716
// fail.
717
if test.config.hogs_memory.unwrap_or(false) {
718
return Err(arbitrary::Error::IncorrectFormat);
719
}
720
721
// Transform `fuzz_config` to be valid for `test` and make sure that this
722
// test is supposed to pass.
723
let wast_config = fuzz_config.make_wast_test_compliant(test);
724
if test.should_fail(&wast_config) {
725
return Err(arbitrary::Error::IncorrectFormat);
726
}
727
728
// Winch requires AVX and AVX2 for SIMD tests to pass so don't run the test
729
// if either isn't enabled.
730
if fuzz_config.wasmtime.compiler_strategy == CompilerStrategy::Winch
731
&& test.config.simd()
732
&& (fuzz_config
733
.wasmtime
734
.codegen_flag("has_avx")
735
.is_some_and(|value| value == "false")
736
|| fuzz_config
737
.wasmtime
738
.codegen_flag("has_avx2")
739
.is_some_and(|value| value == "false"))
740
{
741
log::warn!(
742
"Skipping Wast test because Winch doesn't support SIMD tests with AVX or AVX2 disabled"
743
);
744
return Err(arbitrary::Error::IncorrectFormat);
745
}
746
747
// Fuel and epochs don't play well with threads right now, so exclude any
748
// thread-spawning test if it looks like threads are spawned in that case.
749
if fuzz_config.wasmtime.consume_fuel || fuzz_config.wasmtime.epoch_interruption {
750
if test.contents.contains("(thread") {
751
return Err(arbitrary::Error::IncorrectFormat);
752
}
753
}
754
755
log::debug!("running {:?}", test.path);
756
let async_ = if fuzz_config.wasmtime.async_config == generators::AsyncConfig::Disabled {
757
wasmtime_wast::Async::No
758
} else {
759
wasmtime_wast::Async::Yes
760
};
761
let mut wast_context = WastContext::new(fuzz_config.to_store(), async_);
762
wast_context
763
.register_spectest(&wasmtime_wast::SpectestConfig {
764
use_shared_memory: true,
765
suppress_prints: true,
766
})
767
.unwrap();
768
wast_context
769
.run_wast(test.path.to_str().unwrap(), test.contents.as_bytes())
770
.unwrap();
771
Ok(())
772
}
773
774
/// Execute a series of `table.get` and `table.set` operations.
775
///
776
/// Returns the number of `gc` operations which occurred throughout the test
777
/// case -- used to test below that gc happens reasonably soon and eventually.
778
pub fn table_ops(
779
mut fuzz_config: generators::Config,
780
mut ops: generators::table_ops::TableOps,
781
) -> Result<usize> {
782
let expected_drops = Arc::new(AtomicUsize::new(0));
783
let num_dropped = Arc::new(AtomicUsize::new(0));
784
785
let num_gcs = Arc::new(AtomicUsize::new(0));
786
{
787
fuzz_config.wasmtime.consume_fuel = true;
788
let mut store = fuzz_config.to_store();
789
store.set_fuel(1_000).unwrap();
790
791
let wasm = ops.to_wasm_binary();
792
log_wasm(&wasm);
793
let module = match compile_module(store.engine(), &wasm, KnownValid::No, &fuzz_config) {
794
Some(m) => m,
795
None => return Ok(0),
796
};
797
798
let mut linker = Linker::new(store.engine());
799
800
// To avoid timeouts, limit the number of explicit GCs we perform per
801
// test case.
802
const MAX_GCS: usize = 5;
803
804
let func_ty = FuncType::new(
805
store.engine(),
806
vec![],
807
vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
808
);
809
let func = Func::new(&mut store, func_ty, {
810
let num_dropped = num_dropped.clone();
811
let expected_drops = expected_drops.clone();
812
let num_gcs = num_gcs.clone();
813
move |mut caller: Caller<'_, StoreLimits>, _params, results| {
814
log::info!("table_ops: GC");
815
if num_gcs.fetch_add(1, SeqCst) < MAX_GCS {
816
caller.gc(None);
817
}
818
819
let a = ExternRef::new(
820
&mut caller,
821
CountDrops::new(&expected_drops, num_dropped.clone()),
822
)?;
823
let b = ExternRef::new(
824
&mut caller,
825
CountDrops::new(&expected_drops, num_dropped.clone()),
826
)?;
827
let c = ExternRef::new(
828
&mut caller,
829
CountDrops::new(&expected_drops, num_dropped.clone()),
830
)?;
831
832
log::info!("table_ops: gc() -> ({a:?}, {b:?}, {c:?})");
833
results[0] = Some(a).into();
834
results[1] = Some(b).into();
835
results[2] = Some(c).into();
836
Ok(())
837
}
838
});
839
linker.define(&store, "", "gc", func).unwrap();
840
841
linker
842
.func_wrap("", "take_refs", {
843
let expected_drops = expected_drops.clone();
844
move |caller: Caller<'_, StoreLimits>,
845
a: Option<Rooted<ExternRef>>,
846
b: Option<Rooted<ExternRef>>,
847
c: Option<Rooted<ExternRef>>|
848
-> Result<()> {
849
log::info!("table_ops: take_refs({a:?}, {b:?}, {c:?})",);
850
851
// Do the assertion on each ref's inner data, even though it
852
// all points to the same atomic, so that if we happen to
853
// run into a use-after-free bug with one of these refs we
854
// are more likely to trigger a segfault.
855
if let Some(a) = a {
856
let a = a
857
.data(&caller)?
858
.unwrap()
859
.downcast_ref::<CountDrops>()
860
.unwrap();
861
assert!(a.0.load(SeqCst) <= expected_drops.load(SeqCst));
862
}
863
if let Some(b) = b {
864
let b = b
865
.data(&caller)?
866
.unwrap()
867
.downcast_ref::<CountDrops>()
868
.unwrap();
869
assert!(b.0.load(SeqCst) <= expected_drops.load(SeqCst));
870
}
871
if let Some(c) = c {
872
let c = c
873
.data(&caller)?
874
.unwrap()
875
.downcast_ref::<CountDrops>()
876
.unwrap();
877
assert!(c.0.load(SeqCst) <= expected_drops.load(SeqCst));
878
}
879
Ok(())
880
}
881
})
882
.unwrap();
883
884
let func_ty = FuncType::new(
885
store.engine(),
886
vec![],
887
vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
888
);
889
let func = Func::new(&mut store, func_ty, {
890
let num_dropped = num_dropped.clone();
891
let expected_drops = expected_drops.clone();
892
move |mut caller, _params, results| {
893
log::info!("table_ops: make_refs");
894
895
let a = ExternRef::new(
896
&mut caller,
897
CountDrops::new(&expected_drops, num_dropped.clone()),
898
)?;
899
let b = ExternRef::new(
900
&mut caller,
901
CountDrops::new(&expected_drops, num_dropped.clone()),
902
)?;
903
let c = ExternRef::new(
904
&mut caller,
905
CountDrops::new(&expected_drops, num_dropped.clone()),
906
)?;
907
908
log::info!("table_ops: make_refs() -> ({a:?}, {b:?}, {c:?})");
909
910
results[0] = Some(a).into();
911
results[1] = Some(b).into();
912
results[2] = Some(c).into();
913
914
Ok(())
915
}
916
});
917
linker.define(&store, "", "make_refs", func).unwrap();
918
919
let instance = linker.instantiate(&mut store, &module).unwrap();
920
let run = instance.get_func(&mut store, "run").unwrap();
921
922
{
923
let mut scope = RootScope::new(&mut store);
924
925
log::info!(
926
"table_ops: begin allocating {} externref arguments",
927
ops.limits.num_globals
928
);
929
let args: Vec<_> = (0..ops.limits.num_params)
930
.map(|_| {
931
Ok(Val::ExternRef(Some(ExternRef::new(
932
&mut scope,
933
CountDrops::new(&expected_drops, num_dropped.clone()),
934
)?)))
935
})
936
.collect::<Result<_>>()?;
937
log::info!(
938
"table_ops: end allocating {} externref arguments",
939
ops.limits.num_globals
940
);
941
942
// The generated function should always return a trap. The only two
943
// valid traps are table-out-of-bounds which happens through `table.get`
944
// and `table.set` generated or an out-of-fuel trap. Otherwise any other
945
// error is unexpected and should fail fuzzing.
946
log::info!("table_ops: calling into Wasm `run` function");
947
let err = run.call(&mut scope, &args, &mut []).unwrap_err();
948
match err.downcast::<GcHeapOutOfMemory<CountDrops>>() {
949
Ok(_oom) => {}
950
Err(err) => {
951
let trap = err
952
.downcast::<Trap>()
953
.expect("if not GC oom, error should be a Wasm trap");
954
match trap {
955
Trap::TableOutOfBounds | Trap::OutOfFuel => {}
956
_ => panic!("unexpected trap: {trap}"),
957
}
958
}
959
}
960
}
961
962
// Do a final GC after running the Wasm.
963
store.gc(None);
964
}
965
966
assert_eq!(num_dropped.load(SeqCst), expected_drops.load(SeqCst));
967
return Ok(num_gcs.load(SeqCst));
968
969
struct CountDrops(Arc<AtomicUsize>);
970
971
impl CountDrops {
972
fn new(expected_drops: &AtomicUsize, num_dropped: Arc<AtomicUsize>) -> Self {
973
let expected = expected_drops.fetch_add(1, SeqCst);
974
log::info!(
975
"CountDrops::new: expected drops: {expected} -> {}",
976
expected + 1
977
);
978
Self(num_dropped)
979
}
980
}
981
982
impl Drop for CountDrops {
983
fn drop(&mut self) {
984
let drops = self.0.fetch_add(1, SeqCst);
985
log::info!("CountDrops::drop: actual drops: {drops} -> {}", drops + 1);
986
}
987
}
988
}
989
990
#[derive(Default)]
991
struct HelperThread {
992
state: Arc<HelperThreadState>,
993
thread: Option<std::thread::JoinHandle<()>>,
994
}
995
996
#[derive(Default)]
997
struct HelperThreadState {
998
should_exit: Mutex<bool>,
999
should_exit_cvar: Condvar,
1000
}
1001
1002
impl HelperThread {
1003
fn run_periodically(&mut self, dur: Duration, mut closure: impl FnMut() + Send + 'static) {
1004
let state = self.state.clone();
1005
self.thread = Some(std::thread::spawn(move || {
1006
// Using our mutex/condvar we wait here for the first of `dur` to
1007
// pass or the `HelperThread` instance to get dropped.
1008
let mut should_exit = state.should_exit.lock().unwrap();
1009
while !*should_exit {
1010
let (lock, result) = state
1011
.should_exit_cvar
1012
.wait_timeout(should_exit, dur)
1013
.unwrap();
1014
should_exit = lock;
1015
// If we timed out for sure then there's no need to continue
1016
// since we'll just abort on the next `checked_sub` anyway.
1017
if result.timed_out() {
1018
closure();
1019
}
1020
}
1021
}));
1022
}
1023
}
1024
1025
impl Drop for HelperThread {
1026
fn drop(&mut self) {
1027
let thread = match self.thread.take() {
1028
Some(thread) => thread,
1029
None => return,
1030
};
1031
// Signal our thread that it should exit and wake it up in case it's
1032
// sleeping.
1033
*self.state.should_exit.lock().unwrap() = true;
1034
self.state.should_exit_cvar.notify_one();
1035
1036
// ... and then wait for the thread to exit to ensure we clean up
1037
// after ourselves.
1038
thread.join().unwrap();
1039
}
1040
}
1041
1042
/// Generate and execute a `crate::generators::component_types::TestCase` using the specified `input` to create
1043
/// arbitrary types and values.
1044
pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbitrary::Result<()> {
1045
use crate::generators::component_types;
1046
use wasmtime::component::{Component, Linker, Val};
1047
use wasmtime_test_util::component::FuncExt;
1048
use wasmtime_test_util::component_fuzz::{
1049
EXPORT_FUNCTION, IMPORT_FUNCTION, MAX_TYPE_DEPTH, TestCase, Type,
1050
};
1051
1052
crate::init_fuzzing();
1053
1054
let mut types = Vec::new();
1055
let mut type_fuel = 500;
1056
1057
for _ in 0..5 {
1058
types.push(Type::generate(input, MAX_TYPE_DEPTH, &mut type_fuel)?);
1059
}
1060
let params = (0..input.int_in_range(0..=5)?)
1061
.map(|_| input.choose(&types))
1062
.collect::<arbitrary::Result<Vec<_>>>()?;
1063
let result = if input.arbitrary()? {
1064
Some(input.choose(&types)?)
1065
} else {
1066
None
1067
};
1068
1069
let case = TestCase {
1070
params,
1071
result,
1072
encoding1: input.arbitrary()?,
1073
encoding2: input.arbitrary()?,
1074
};
1075
1076
let mut config = wasmtime_test_util::component::config();
1077
config.debug_adapter_modules(input.arbitrary()?);
1078
let engine = Engine::new(&config).unwrap();
1079
let mut store = Store::new(&engine, (Vec::new(), None));
1080
let wat = case.declarations().make_component();
1081
let wat = wat.as_bytes();
1082
log_wasm(wat);
1083
let component = Component::new(&engine, wat).unwrap();
1084
let mut linker = Linker::new(&engine);
1085
1086
linker
1087
.root()
1088
.func_new(IMPORT_FUNCTION, {
1089
move |mut cx: StoreContextMut<'_, (Vec<Val>, Option<Vec<Val>>)>,
1090
params: &[Val],
1091
results: &mut [Val]|
1092
-> Result<()> {
1093
log::trace!("received params {params:?}");
1094
let (expected_args, expected_results) = cx.data_mut();
1095
assert_eq!(params.len(), expected_args.len());
1096
for (expected, actual) in expected_args.iter().zip(params) {
1097
assert_eq!(expected, actual);
1098
}
1099
results.clone_from_slice(&expected_results.take().unwrap());
1100
log::trace!("returning results {results:?}");
1101
Ok(())
1102
}
1103
})
1104
.unwrap();
1105
1106
let instance = linker.instantiate(&mut store, &component).unwrap();
1107
let func = instance.get_func(&mut store, EXPORT_FUNCTION).unwrap();
1108
let param_tys = func.params(&store);
1109
let result_tys = func.results(&store);
1110
1111
while input.arbitrary()? {
1112
let params = param_tys
1113
.iter()
1114
.map(|(_, ty)| component_types::arbitrary_val(ty, input))
1115
.collect::<arbitrary::Result<Vec<_>>>()?;
1116
let results = result_tys
1117
.iter()
1118
.map(|ty| component_types::arbitrary_val(ty, input))
1119
.collect::<arbitrary::Result<Vec<_>>>()?;
1120
1121
*store.data_mut() = (params.clone(), Some(results.clone()));
1122
1123
log::trace!("passing params {params:?}");
1124
let mut actual = vec![Val::Bool(false); results.len()];
1125
func.call_and_post_return(&mut store, &params, &mut actual)
1126
.unwrap();
1127
log::trace!("received results {actual:?}");
1128
assert_eq!(actual, results);
1129
}
1130
1131
Ok(())
1132
}
1133
1134
/// Instantiates a wasm module and runs its exports with dummy values, all in
1135
/// an async fashion.
1136
///
1137
/// Attempts to stress yields in host functions to ensure that exiting and
1138
/// resuming a wasm function call works.
1139
pub fn call_async(wasm: &[u8], config: &generators::Config, mut poll_amts: &[u32]) {
1140
let mut store = config.to_store();
1141
let module = match compile_module(store.engine(), wasm, KnownValid::Yes, config) {
1142
Some(module) => module,
1143
None => return,
1144
};
1145
1146
// Configure a helper thread to periodically increment the epoch to
1147
// forcibly enable yields-via-epochs if epochs are in use. Note that this
1148
// is required because the wasm isn't otherwise guaranteed to necessarily
1149
// call any imports which will also increment the epoch.
1150
let mut helper_thread = HelperThread::default();
1151
if let generators::AsyncConfig::YieldWithEpochs { dur, .. } = &config.wasmtime.async_config {
1152
let engine = store.engine().clone();
1153
helper_thread.run_periodically(*dur, move || engine.increment_epoch());
1154
}
1155
1156
// Generate a `Linker` where all function imports are custom-built to yield
1157
// periodically and additionally increment the epoch.
1158
let mut imports = Vec::new();
1159
for import in module.imports() {
1160
let item = match import.ty() {
1161
ExternType::Func(ty) => {
1162
let poll_amt = take_poll_amt(&mut poll_amts);
1163
Func::new_async(&mut store, ty.clone(), move |caller, _, results| {
1164
let ty = ty.clone();
1165
Box::new(async move {
1166
caller.engine().increment_epoch();
1167
log::info!("yielding {poll_amt} times in import");
1168
YieldN(poll_amt).await;
1169
for (ret_ty, result) in ty.results().zip(results) {
1170
*result = ret_ty.default_value().unwrap();
1171
}
1172
Ok(())
1173
})
1174
})
1175
.into()
1176
}
1177
other_ty => match other_ty.default_value(&mut store) {
1178
Ok(item) => item,
1179
Err(e) => {
1180
log::warn!("couldn't create import for {import:?}: {e:?}");
1181
return;
1182
}
1183
},
1184
};
1185
imports.push(item);
1186
}
1187
1188
// Run the instantiation process, asynchronously, and if everything
1189
// succeeds then pull out the instance.
1190
// log::info!("starting instantiation");
1191
let instance = run(Timeout {
1192
future: Instance::new_async(&mut store, &module, &imports),
1193
polls: take_poll_amt(&mut poll_amts),
1194
end: Instant::now() + Duration::from_millis(2_000),
1195
});
1196
let instance = match instance {
1197
Ok(instantiation_result) => match unwrap_instance(&store, instantiation_result) {
1198
Some(instance) => instance,
1199
None => {
1200
log::info!("instantiation hit a nominal error");
1201
return; // resource exhaustion or limits met
1202
}
1203
},
1204
Err(_) => {
1205
log::info!("instantiation failed to complete");
1206
return; // Timed out or ran out of polls
1207
}
1208
};
1209
1210
// Run each export of the instance in the same manner as instantiation
1211
// above. Dummy values are passed in for argument values here:
1212
//
1213
// TODO: this should probably be more clever about passing in arguments for
1214
// example they might be used as pointers or something and always using 0
1215
// isn't too interesting.
1216
let funcs = instance
1217
.exports(&mut store)
1218
.filter_map(|e| {
1219
let name = e.name().to_string();
1220
let func = e.into_extern().into_func()?;
1221
Some((name, func))
1222
})
1223
.collect::<Vec<_>>();
1224
for (name, func) in funcs {
1225
let ty = func.ty(&store);
1226
let params = ty
1227
.params()
1228
.map(|ty| ty.default_value().unwrap())
1229
.collect::<Vec<_>>();
1230
let mut results = ty
1231
.results()
1232
.map(|ty| ty.default_value().unwrap())
1233
.collect::<Vec<_>>();
1234
1235
log::info!("invoking export {name:?}");
1236
let future = func.call_async(&mut store, &params, &mut results);
1237
match run(Timeout {
1238
future,
1239
polls: take_poll_amt(&mut poll_amts),
1240
end: Instant::now() + Duration::from_millis(2_000),
1241
}) {
1242
// On success or too many polls, try the next export.
1243
Ok(_) | Err(Exhausted::Polls) => {}
1244
1245
// If time ran out then stop the current test case as we might have
1246
// already sucked up a lot of time for this fuzz test case so don't
1247
// keep it going.
1248
Err(Exhausted::Time) => return,
1249
}
1250
}
1251
1252
fn take_poll_amt(polls: &mut &[u32]) -> u32 {
1253
match polls.split_first() {
1254
Some((a, rest)) => {
1255
*polls = rest;
1256
*a
1257
}
1258
None => 0,
1259
}
1260
}
1261
1262
/// Helper future to yield N times before resolving.
1263
struct YieldN(u32);
1264
1265
impl Future for YieldN {
1266
type Output = ();
1267
1268
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
1269
if self.0 == 0 {
1270
Poll::Ready(())
1271
} else {
1272
self.0 -= 1;
1273
cx.waker().wake_by_ref();
1274
Poll::Pending
1275
}
1276
}
1277
}
1278
1279
/// Helper future for applying a timeout to `future` up to either when `end`
1280
/// is the current time or `polls` polls happen.
1281
///
1282
/// Note that this helps to time out infinite loops in wasm, for example.
1283
struct Timeout<F> {
1284
future: F,
1285
/// If the future isn't ready by this time then the `Timeout<F>` future
1286
/// will return `None`.
1287
end: Instant,
1288
/// If the future doesn't resolve itself in this many calls to `poll`
1289
/// then the `Timeout<F>` future will return `None`.
1290
polls: u32,
1291
}
1292
1293
enum Exhausted {
1294
Time,
1295
Polls,
1296
}
1297
1298
impl<F: Future> Future for Timeout<F> {
1299
type Output = Result<F::Output, Exhausted>;
1300
1301
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1302
let (end, polls, future) = unsafe {
1303
let me = self.get_unchecked_mut();
1304
(me.end, &mut me.polls, Pin::new_unchecked(&mut me.future))
1305
};
1306
match future.poll(cx) {
1307
Poll::Ready(val) => Poll::Ready(Ok(val)),
1308
Poll::Pending => {
1309
if Instant::now() >= end {
1310
log::warn!("future operation timed out");
1311
return Poll::Ready(Err(Exhausted::Time));
1312
}
1313
if *polls == 0 {
1314
log::warn!("future operation ran out of polls");
1315
return Poll::Ready(Err(Exhausted::Polls));
1316
}
1317
*polls -= 1;
1318
Poll::Pending
1319
}
1320
}
1321
}
1322
}
1323
1324
fn run<F: Future>(future: F) -> F::Output {
1325
let mut f = Box::pin(future);
1326
let mut cx = Context::from_waker(Waker::noop());
1327
loop {
1328
match f.as_mut().poll(&mut cx) {
1329
Poll::Ready(val) => break val,
1330
Poll::Pending => {}
1331
}
1332
}
1333
}
1334
}
1335
1336
#[cfg(test)]
1337
mod tests {
1338
use super::*;
1339
use arbitrary::Unstructured;
1340
use rand::prelude::*;
1341
use wasmparser::{Validator, WasmFeatures};
1342
1343
fn gen_until_pass<T: for<'a> Arbitrary<'a>>(
1344
mut f: impl FnMut(T, &mut Unstructured<'_>) -> Result<bool>,
1345
) -> bool {
1346
let mut rng = SmallRng::seed_from_u64(0);
1347
let mut buf = vec![0; 2048];
1348
let n = 3000;
1349
for _ in 0..n {
1350
rng.fill_bytes(&mut buf);
1351
let mut u = Unstructured::new(&buf);
1352
1353
if let Ok(config) = u.arbitrary() {
1354
if f(config, &mut u).unwrap() {
1355
return true;
1356
}
1357
}
1358
}
1359
false
1360
}
1361
1362
/// Runs `f` with random data until it returns `Ok(())` `iters` times.
1363
fn test_n_times<T: for<'a> Arbitrary<'a>>(
1364
iters: u32,
1365
mut f: impl FnMut(T, &mut Unstructured<'_>) -> arbitrary::Result<()>,
1366
) {
1367
let mut to_test = 0..iters;
1368
let ok = gen_until_pass(|a, b| {
1369
if f(a, b).is_ok() {
1370
Ok(to_test.next().is_none())
1371
} else {
1372
Ok(false)
1373
}
1374
});
1375
assert!(ok);
1376
}
1377
1378
// Test that the `table_ops` fuzzer eventually runs the gc function in the host.
1379
// We've historically had issues where this fuzzer accidentally wasn't fuzzing
1380
// anything for a long time so this is an attempt to prevent that from happening
1381
// again.
1382
#[test]
1383
fn table_ops_eventually_gcs() {
1384
// Skip if we're under emulation because some fuzz configurations will do
1385
// large address space reservations that QEMU doesn't handle well.
1386
if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
1387
return;
1388
}
1389
1390
let ok = gen_until_pass(|(config, test), _| {
1391
let result = table_ops(config, test)?;
1392
Ok(result > 0)
1393
});
1394
1395
if !ok {
1396
panic!("gc was never found");
1397
}
1398
}
1399
1400
#[test]
1401
fn module_generation_uses_expected_proposals() {
1402
// Proposals that Wasmtime supports. Eventually a module should be
1403
// generated that needs these proposals.
1404
let mut expected = WasmFeatures::MUTABLE_GLOBAL
1405
| WasmFeatures::FLOATS
1406
| WasmFeatures::SIGN_EXTENSION
1407
| WasmFeatures::SATURATING_FLOAT_TO_INT
1408
| WasmFeatures::MULTI_VALUE
1409
| WasmFeatures::BULK_MEMORY
1410
| WasmFeatures::REFERENCE_TYPES
1411
| WasmFeatures::SIMD
1412
| WasmFeatures::MULTI_MEMORY
1413
| WasmFeatures::RELAXED_SIMD
1414
| WasmFeatures::THREADS
1415
| WasmFeatures::TAIL_CALL
1416
| WasmFeatures::WIDE_ARITHMETIC
1417
| WasmFeatures::MEMORY64
1418
| WasmFeatures::FUNCTION_REFERENCES
1419
| WasmFeatures::GC
1420
| WasmFeatures::GC_TYPES
1421
| WasmFeatures::CUSTOM_PAGE_SIZES
1422
| WasmFeatures::EXTENDED_CONST
1423
| WasmFeatures::EXCEPTIONS;
1424
1425
// All other features that wasmparser supports, which is presumably a
1426
// superset of the features that wasm-smith supports, are listed here as
1427
// unexpected. This means, for example, that if wasm-smith updates to
1428
// include a new proposal by default that wasmtime implements then it
1429
// will be required to be listed above.
1430
let unexpected = WasmFeatures::all() ^ expected;
1431
1432
let ok = gen_until_pass(|config: generators::Config, u| {
1433
let wasm = config.generate(u, None)?.to_bytes();
1434
1435
// Double-check the module is valid
1436
Validator::new_with_features(WasmFeatures::all()).validate_all(&wasm)?;
1437
1438
// If any of the unexpected features are removed then this module
1439
// should always be valid, otherwise something went wrong.
1440
for feature in unexpected.iter() {
1441
let ok =
1442
Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);
1443
if ok.is_err() {
1444
anyhow::bail!("generated a module with {feature:?} but that wasn't expected");
1445
}
1446
}
1447
1448
// If any of `expected` is removed and the module fails to validate,
1449
// then that means the module requires that feature. Remove that
1450
// from the set of features we're then expecting.
1451
for feature in expected.iter() {
1452
let ok =
1453
Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);
1454
if ok.is_err() {
1455
expected ^= feature;
1456
}
1457
}
1458
1459
Ok(expected.is_empty())
1460
});
1461
1462
if !ok {
1463
panic!("never generated wasm module using {expected:?}");
1464
}
1465
}
1466
1467
#[test]
1468
fn wast_smoke_test() {
1469
test_n_times(50, |(), u| super::wast_test(u));
1470
}
1471
}
1472
1473