Path: blob/main/crates/fuzzing/src/oracles.rs
3054 views
//! Oracles.1//!2//! Oracles take a test case and determine whether we have a bug. For example,3//! one of the simplest oracles is to take a Wasm binary as our input test case,4//! validate and instantiate it, and (implicitly) check that no assertions5//! failed or segfaults happened. A more complicated oracle might compare the6//! result of executing a Wasm file with and without optimizations enabled, and7//! make sure that the two executions are observably identical.8//!9//! When an oracle finds a bug, it should report it to the fuzzing engine by10//! panicking.1112pub mod component_api;13pub mod component_async;14#[cfg(feature = "fuzz-spec-interpreter")]15pub mod diff_spec;16pub mod diff_wasmi;17pub mod diff_wasmtime;18pub mod dummy;19pub mod engine;20pub mod memory;21mod stacks;2223use self::diff_wasmtime::WasmtimeInstance;24use self::engine::{DiffEngine, DiffInstance};25use crate::generators::GcOps;26use crate::generators::{self, CompilerStrategy, DiffValue, DiffValueType};27use crate::single_module_fuzzer::KnownValid;28use crate::{YieldN, block_on};29use arbitrary::Arbitrary;30pub use stacks::check_stacks;31use std::future::Future;32use std::pin::Pin;33use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};34use std::sync::{Arc, Condvar, Mutex};35use std::task::{Context, Poll};36use std::time::{Duration, Instant};37use wasmtime::*;38use wasmtime_wast::WastContext;3940#[cfg(not(any(windows, target_arch = "s390x", target_arch = "riscv64")))]41mod diff_v8;4243static CNT: AtomicUsize = AtomicUsize::new(0);4445/// Logs a wasm file to the filesystem to make it easy to figure out what wasm46/// was used when debugging.47pub fn log_wasm(wasm: &[u8]) {48super::init_fuzzing();4950if !log::log_enabled!(log::Level::Debug) {51return;52}5354let i = CNT.fetch_add(1, SeqCst);55let name = format!("testcase{i}.wasm");56std::fs::write(&name, wasm).expect("failed to write wasm file");57log::debug!("wrote wasm file to `{name}`");58let wat = format!("testcase{i}.wat");59match wasmprinter::print_bytes(wasm) {60Ok(s) => {61std::fs::write(&wat, s).expect("failed to write wat file");62log::debug!("wrote wat file to `{wat}`");63}64// If wasmprinter failed remove a `*.wat` file, if any, to avoid65// confusing a preexisting one with this wasm which failed to get66// printed.67Err(e) => {68log::debug!("failed to print to wat: {e}");69drop(std::fs::remove_file(&wat));70}71}72}7374/// The `T` in `Store<T>` for fuzzing stores, used to limit resource75/// consumption during fuzzing.76#[derive(Clone)]77pub struct StoreLimits(Arc<LimitsState>);7879struct LimitsState {80/// Remaining memory, in bytes, left to allocate81remaining_memory: AtomicUsize,82/// Remaining amount of memory that's allowed to be copied via a growth.83remaining_copy_allowance: AtomicUsize,84/// Whether or not an allocation request has been denied85oom: AtomicBool,86}8788/// Allow up to 1G which is well below the 2G limit on OSS-Fuzz and should allow89/// most interesting behavior.90const MAX_MEMORY: usize = 1 << 30;9192/// Allow up to 4G of bytes to be copied (conservatively) which should enable93/// growth up to `MAX_MEMORY` or at least up to a relatively large amount.94const MAX_MEMORY_MOVED: usize = 4 << 30;9596impl StoreLimits {97/// Creates the default set of limits for all fuzzing stores.98pub fn new() -> StoreLimits {99StoreLimits(Arc::new(LimitsState {100remaining_memory: AtomicUsize::new(MAX_MEMORY),101remaining_copy_allowance: AtomicUsize::new(MAX_MEMORY_MOVED),102oom: AtomicBool::new(false),103}))104}105106fn alloc(&mut self, amt: usize) -> bool {107log::trace!("alloc {amt:#x} bytes");108109// Assume that on each allocation of memory that all previous110// allocations of memory are moved. This is pretty coarse but is used to111// help prevent against fuzz test cases that just move tons of bytes112// around continuously. This assumes that all previous memory was113// allocated in a single linear memory and growing by `amt` will require114// moving all the bytes to a new location. This isn't actually required115// all the time nor does it accurately reflect what happens all the116// time, but it's a coarse approximation that should be "good enough"117// for allowing interesting fuzz behaviors to happen while not timing118// out just copying bytes around.119let prev_size = MAX_MEMORY - self.0.remaining_memory.load(SeqCst);120if self121.0122.remaining_copy_allowance123.fetch_update(SeqCst, SeqCst, |remaining| remaining.checked_sub(prev_size))124.is_err()125{126self.0.oom.store(true, SeqCst);127log::debug!("-> too many bytes moved, rejecting allocation");128return false;129}130131// If we're allowed to move the bytes, then also check if we're allowed132// to actually have this much residence at once.133match self134.0135.remaining_memory136.fetch_update(SeqCst, SeqCst, |remaining| remaining.checked_sub(amt))137{138Ok(_) => true,139Err(_) => {140self.0.oom.store(true, SeqCst);141log::debug!("-> OOM hit");142false143}144}145}146147fn is_oom(&self) -> bool {148self.0.oom.load(SeqCst)149}150}151152impl ResourceLimiter for StoreLimits {153fn memory_growing(154&mut self,155current: usize,156desired: usize,157_maximum: Option<usize>,158) -> Result<bool> {159Ok(self.alloc(desired - current))160}161162fn table_growing(163&mut self,164current: usize,165desired: usize,166_maximum: Option<usize>,167) -> Result<bool> {168let delta = (desired - current).saturating_mul(std::mem::size_of::<usize>());169Ok(self.alloc(delta))170}171}172173/// Methods of timing out execution of a WebAssembly module174#[derive(Clone, Debug)]175pub enum Timeout {176/// No timeout is used, it should be guaranteed via some other means that177/// the input does not infinite loop.178None,179/// Fuel-based timeouts are used where the specified fuel is all that the180/// provided wasm module is allowed to consume.181Fuel(u64),182/// An epoch-interruption-based timeout is used with a sleeping183/// thread bumping the epoch counter after the specified duration.184Epoch(Duration),185}186187/// Instantiate the Wasm buffer, and implicitly fail if we have an unexpected188/// panic or segfault or anything else that can be detected "passively".189///190/// The engine will be configured using provided config.191pub fn instantiate(192wasm: &[u8],193known_valid: KnownValid,194config: &generators::Config,195timeout: Timeout,196) {197let mut store = config.to_store();198199let module = match compile_module(store.engine(), wasm, known_valid, config) {200Some(module) => module,201None => return,202};203204let mut timeout_state = HelperThread::default();205match timeout {206Timeout::Fuel(fuel) => store.set_fuel(fuel).unwrap(),207208// If a timeout is requested then we spawn a helper thread to wait for209// the requested time and then send us a signal to get interrupted. We210// also arrange for the thread's sleep to get interrupted if we return211// early (or the wasm returns within the time limit), which allows the212// thread to get torn down.213//214// This prevents us from creating a huge number of sleeping threads if215// this function is executed in a loop, like it does on nightly fuzzing216// infrastructure.217Timeout::Epoch(timeout) => {218let engine = store.engine().clone();219timeout_state.run_periodically(timeout, move || engine.increment_epoch());220}221Timeout::None => {}222}223224instantiate_with_dummy(&mut store, &module);225}226227/// Represents supported commands to the `instantiate_many` function.228#[derive(Arbitrary, Debug)]229pub enum Command {230/// Instantiates a module.231///232/// The value is the index of the module to instantiate.233///234/// The module instantiated will be this value modulo the number of modules provided to `instantiate_many`.235Instantiate(usize),236/// Terminates a "running" instance.237///238/// The value is the index of the instance to terminate.239///240/// The instance terminated will be this value modulo the number of currently running241/// instances.242///243/// If no instances are running, the command will be ignored.244Terminate(usize),245}246247/// Instantiates many instances from the given modules.248///249/// The engine will be configured using the provided config.250///251/// The modules are expected to *not* have start functions as no timeouts are configured.252pub fn instantiate_many(253modules: &[Vec<u8>],254known_valid: KnownValid,255config: &generators::Config,256commands: &[Command],257) {258log::debug!("instantiate_many: {commands:#?}");259260assert!(!config.module_config.config.allow_start_export);261262let engine = Engine::new(&config.to_wasmtime()).unwrap();263264let modules = modules265.iter()266.enumerate()267.filter_map(268|(i, bytes)| match compile_module(&engine, bytes, known_valid, config) {269Some(m) => {270log::debug!("successfully compiled module {i}");271Some(m)272}273None => {274log::debug!("failed to compile module {i}");275None276}277},278)279.collect::<Vec<_>>();280281// If no modules were valid, we're done282if modules.is_empty() {283return;284}285286// This stores every `Store` where a successful instantiation takes place287let mut stores = Vec::new();288let limits = StoreLimits::new();289290for command in commands {291match command {292Command::Instantiate(index) => {293let index = *index % modules.len();294log::info!("instantiating {index}");295let module = &modules[index];296let mut store = Store::new(&engine, limits.clone());297config.configure_store(&mut store);298299if instantiate_with_dummy(&mut store, module).is_some() {300stores.push(Some(store));301} else {302log::warn!("instantiation failed");303}304}305Command::Terminate(index) => {306if stores.is_empty() {307continue;308}309let index = *index % stores.len();310311log::info!("dropping {index}");312stores.swap_remove(index);313}314}315}316}317318fn compile_module(319engine: &Engine,320bytes: &[u8],321known_valid: KnownValid,322config: &generators::Config,323) -> Option<Module> {324log_wasm(bytes);325326fn is_pcc_error(e: &wasmtime::Error) -> bool {327// NOTE: please keep this predicate in sync with the display format of CodegenError,328// defined in `wasmtime/cranelift/codegen/src/result.rs`329e.to_string().to_lowercase().contains("proof-carrying-code")330}331332match config.compile(engine, bytes) {333Ok(module) => Some(module),334Err(e) if is_pcc_error(&e) => {335panic!("pcc error in input: {e:#?}");336}337Err(_) if known_valid == KnownValid::No => None,338Err(e) => {339if let generators::InstanceAllocationStrategy::Pooling(c) = &config.wasmtime.strategy {340// When using the pooling allocator, accept failures to compile341// when arbitrary table element limits have been exceeded as342// there is currently no way to constrain the generated module343// table types.344let string = format!("{e:?}");345if string.contains("minimum element size") {346return None;347}348349// Allow modules-failing-to-compile which exceed the requested350// size for each instance. This is something that is difficult351// to control and ensure it always succeeds, so we simply have a352// "random" instance size limit and if a module doesn't fit we353// move on to the next fuzz input.354if string.contains("instance allocation for this module requires") {355return None;356}357358// If the pooling allocator is more restrictive on the number of359// tables and memories than we allowed wasm-smith to generate360// then allow compilation errors along those lines.361if c.max_tables_per_module < (config.module_config.config.max_tables as u32)362&& string.contains("defined tables count")363&& string.contains("exceeds the per-instance limit")364{365return None;366}367368if c.max_memories_per_module < (config.module_config.config.max_memories as u32)369&& string.contains("defined memories count")370&& string.contains("exceeds the per-instance limit")371{372return None;373}374}375376panic!("failed to compile module: {e:?}");377}378}379}380381/// Create a Wasmtime [`Instance`] from a [`Module`] and fill in all imports382/// with dummy values (e.g., zeroed values, immediately-trapping functions).383/// Also, this function catches certain fuzz-related instantiation failures and384/// returns `None` instead of panicking.385///386/// TODO: we should implement tracing versions of these dummy imports that387/// record a trace of the order that imported functions were called in and with388/// what values. Like the results of exported functions, calls to imports should389/// also yield the same values for each configuration, and we should assert390/// that.391pub fn instantiate_with_dummy(store: &mut Store<StoreLimits>, module: &Module) -> Option<Instance> {392// Creation of imports can fail due to resource limit constraints, and then393// instantiation can naturally fail for a number of reasons as well. Bundle394// the two steps together to match on the error below.395let linker = dummy::dummy_linker(store, module);396if let Err(e) = &linker {397log::warn!("failed to create dummy linker: {e:?}");398}399let instance = linker.and_then(|l| l.instantiate(&mut *store, module));400unwrap_instance(store, instance)401}402403fn unwrap_instance(404store: &Store<StoreLimits>,405instance: wasmtime::Result<Instance>,406) -> Option<Instance> {407let e = match instance {408Ok(i) => return Some(i),409Err(e) => e,410};411412log::debug!("failed to instantiate: {e:?}");413414// If the instantiation hit OOM for some reason then that's ok, it's415// expected that fuzz-generated programs try to allocate lots of416// stuff.417if store.data().is_oom() {418return None;419}420421// Allow traps which can happen normally with `unreachable` or a timeout or422// such.423if e.is::<Trap>()424// Also allow failures to instantiate as a result of hitting pooling425// limits.426|| e.is::<wasmtime::PoolConcurrencyLimitError>()427// And GC heap OOMs.428|| e.is::<wasmtime::GcHeapOutOfMemory<()>>()429// And thrown exceptions.430|| e.is::<wasmtime::ThrownException>()431{432return None;433}434435let string = e.to_string();436437// Currently we instantiate with a `Linker` which can't instantiate438// every single module under the sun due to using name-based resolution439// rather than positional-based resolution440if string.contains("incompatible import type") {441return None;442}443444// Everything else should be a bug in the fuzzer or a bug in wasmtime445panic!("failed to instantiate: {e:?}");446}447448/// Evaluate the function identified by `name` in two different engine449/// instances--`lhs` and `rhs`.450///451/// Returns `Ok(true)` if more evaluations can happen or `Ok(false)` if the452/// instances may have drifted apart and no more evaluations can happen.453///454/// # Panics455///456/// This will panic if the evaluation is different between engines (e.g.,457/// results are different, hashed instance is different, one side traps, etc.).458pub fn differential(459lhs: &mut dyn DiffInstance,460lhs_engine: &dyn DiffEngine,461rhs: &mut WasmtimeInstance,462name: &str,463args: &[DiffValue],464result_tys: &[DiffValueType],465) -> wasmtime::Result<bool> {466log::debug!("Evaluating: `{name}` with {args:?}");467let lhs_results = match lhs.evaluate(name, args, result_tys) {468Ok(Some(results)) => Ok(results),469Err(e) => Err(e),470// this engine couldn't execute this type signature, so discard this471// execution by returning success.472Ok(None) => return Ok(true),473};474log::debug!(" -> lhs results on {}: {:?}", lhs.name(), &lhs_results);475476let rhs_results = rhs477.evaluate(name, args, result_tys)478// wasmtime should be able to invoke any signature, so unwrap this result479.map(|results| results.unwrap());480log::debug!(" -> rhs results on {}: {:?}", rhs.name(), &rhs_results);481482// If Wasmtime hit its OOM condition, which is possible since it's set483// somewhat low while fuzzing, then don't return an error but return484// `false` indicating that differential fuzzing must stop. There's no485// guarantee the other engine has the same OOM limits as Wasmtime, and486// it's assumed that Wasmtime is configured to have a more conservative487// limit than the other engine.488if rhs.is_oom() {489return Ok(false);490}491492match DiffEqResult::new(lhs_engine, lhs_results, rhs_results) {493DiffEqResult::Success(lhs, rhs) => assert_eq!(lhs, rhs),494DiffEqResult::Poisoned => return Ok(false),495DiffEqResult::Failed => {}496}497498for (global, ty) in rhs.exported_globals() {499log::debug!("Comparing global `{global}`");500let lhs = match lhs.get_global(&global, ty) {501Some(val) => val,502None => continue,503};504let rhs = rhs.get_global(&global, ty).unwrap();505assert_eq!(lhs, rhs);506}507for (memory, shared) in rhs.exported_memories() {508log::debug!("Comparing memory `{memory}`");509let lhs = match lhs.get_memory(&memory, shared) {510Some(val) => val,511None => continue,512};513let rhs = rhs.get_memory(&memory, shared).unwrap();514if lhs == rhs {515continue;516}517eprintln!("differential memory is {} bytes long", lhs.len());518eprintln!("wasmtime memory is {} bytes long", rhs.len());519panic!("memories have differing values");520}521522Ok(true)523}524525/// Result of comparing the result of two operations during differential526/// execution.527pub enum DiffEqResult<T, U> {528/// Both engines succeeded.529Success(T, U),530/// The result has reached the state where engines may have diverged and531/// results can no longer be compared.532Poisoned,533/// Both engines failed with the same error message, and internal state534/// should still match between the two engines.535Failed,536}537538fn wasmtime_trap_is_non_deterministic(trap: &Trap) -> bool {539match trap {540// Allocations being too large for the GC are541// implementation-defined.542Trap::AllocationTooLarge |543// Stack size, and therefore when overflow happens, is544// implementation-defined.545Trap::StackOverflow => true,546_ => false,547}548}549550fn wasmtime_error_is_non_deterministic(error: &wasmtime::Error) -> bool {551match error.downcast_ref::<Trap>() {552Some(trap) => wasmtime_trap_is_non_deterministic(trap),553554// For general, unknown errors, we can't rely on this being555// a deterministic Wasm failure that both engines handled556// identically, leaving Wasm in identical states. We could557// just as easily be hitting engine-specific failures, like558// different implementation-defined limits. So simply poison559// this execution and move on to the next test.560None => true,561}562}563564impl<T, U> DiffEqResult<T, U> {565/// Computes the differential result from executing in two different566/// engines.567pub fn new(568lhs_engine: &dyn DiffEngine,569lhs_result: Result<T>,570rhs_result: Result<U>,571) -> DiffEqResult<T, U> {572match (lhs_result, rhs_result) {573(Ok(lhs_result), Ok(rhs_result)) => DiffEqResult::Success(lhs_result, rhs_result),574575// Handle all non-deterministic errors by poisoning this execution's576// state, so that we simply move on to the next test.577(Err(lhs), _) if lhs_engine.is_non_deterministic_error(&lhs) => {578log::debug!("lhs failed non-deterministically: {lhs:?}");579DiffEqResult::Poisoned580}581(_, Err(rhs)) if wasmtime_error_is_non_deterministic(&rhs) => {582log::debug!("rhs failed non-deterministically: {rhs:?}");583DiffEqResult::Poisoned584}585586// Both sides failed deterministically. Check that the trap and587// state at the time of failure is the same.588(Err(lhs), Err(rhs)) => {589let rhs = rhs590.downcast::<Trap>()591.expect("non-traps handled in earlier match arm");592593debug_assert!(594!lhs_engine.is_non_deterministic_error(&lhs),595"non-deterministic traps handled in earlier match arm",596);597debug_assert!(598!wasmtime_trap_is_non_deterministic(&rhs),599"non-deterministic traps handled in earlier match arm",600);601602lhs_engine.assert_error_match(&lhs, &rhs);603DiffEqResult::Failed604}605606// A real bug is found if only one side fails.607(Ok(_), Err(err)) => panic!("only the `rhs` failed for this input: {err:?}"),608(Err(err), Ok(_)) => panic!("only the `lhs` failed for this input: {err:?}"),609}610}611}612613/// Invoke the given API calls.614pub fn make_api_calls(api: generators::api::ApiCalls) {615use crate::generators::api::ApiCall;616use std::collections::HashMap;617618let mut store: Option<Store<StoreLimits>> = None;619let mut modules: HashMap<usize, Module> = Default::default();620let mut instances: HashMap<usize, Instance> = Default::default();621622for call in api.calls {623match call {624ApiCall::StoreNew(config) => {625log::trace!("creating store");626assert!(store.is_none());627store = Some(config.to_store());628}629630ApiCall::ModuleNew { id, wasm } => {631log::debug!("creating module: {id}");632log_wasm(&wasm);633let module = match Module::new(store.as_ref().unwrap().engine(), &wasm) {634Ok(m) => m,635Err(_) => continue,636};637let old = modules.insert(id, module);638assert!(old.is_none());639}640641ApiCall::ModuleDrop { id } => {642log::trace!("dropping module: {id}");643drop(modules.remove(&id));644}645646ApiCall::InstanceNew { id, module } => {647log::trace!("instantiating module {module} as {id}");648let module = match modules.get(&module) {649Some(m) => m,650None => continue,651};652653let store = store.as_mut().unwrap();654if let Some(instance) = instantiate_with_dummy(store, module) {655instances.insert(id, instance);656}657}658659ApiCall::InstanceDrop { id } => {660log::trace!("dropping instance {id}");661instances.remove(&id);662}663664ApiCall::CallExportedFunc { instance, nth } => {665log::trace!("calling instance export {instance} / {nth}");666let instance = match instances.get(&instance) {667Some(i) => i,668None => {669// Note that we aren't guaranteed to instantiate valid670// modules, see comments in `InstanceNew` for details on671// that. But the API call generator can't know if672// instantiation failed, so we might not actually have673// this instance. When that's the case, just skip the674// API call and keep going.675continue;676}677};678let store = store.as_mut().unwrap();679680let funcs = instance681.exports(&mut *store)682.filter_map(|e| match e.into_extern() {683Extern::Func(f) => Some(f),684_ => None,685})686.collect::<Vec<_>>();687688if funcs.is_empty() {689continue;690}691692let nth = nth % funcs.len();693let f = &funcs[nth];694let ty = f.ty(&store);695if let Some(params) = ty696.params()697.map(|p| p.default_value())698.collect::<Option<Vec<_>>>()699{700let mut results = vec![Val::I32(0); ty.results().len()];701let _ = f.call(store, ¶ms, &mut results);702}703}704}705}706}707708/// Executes the wast `test` with the `config` specified.709///710/// Ensures that wast tests pass regardless of the `Config`.711pub fn wast_test(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<()> {712crate::init_fuzzing();713714let mut fuzz_config: generators::Config = u.arbitrary()?;715fuzz_config.module_config.shared_memory = true;716let test: generators::WastTest = u.arbitrary()?;717718let test = &test.test;719720if test.config.component_model_async() || u.arbitrary()? {721fuzz_config.enable_async(u)?;722}723724// Discard tests that allocate a lot of memory as we don't want to OOM the725// fuzzer and we also limit memory growth which would cause the test to726// fail.727if test.config.hogs_memory.unwrap_or(false) {728return Err(arbitrary::Error::IncorrectFormat);729}730731// Transform `fuzz_config` to be valid for `test` and make sure that this732// test is supposed to pass.733let wast_config = fuzz_config.make_wast_test_compliant(test);734if test.should_fail(&wast_config) {735return Err(arbitrary::Error::IncorrectFormat);736}737738// Winch requires AVX and AVX2 for SIMD tests to pass so don't run the test739// if either isn't enabled.740if fuzz_config.wasmtime.compiler_strategy == CompilerStrategy::Winch741&& test.config.simd()742&& (fuzz_config743.wasmtime744.codegen_flag("has_avx")745.is_some_and(|value| value == "false")746|| fuzz_config747.wasmtime748.codegen_flag("has_avx2")749.is_some_and(|value| value == "false"))750{751log::warn!(752"Skipping Wast test because Winch doesn't support SIMD tests with AVX or AVX2 disabled"753);754return Err(arbitrary::Error::IncorrectFormat);755}756757// Fuel and epochs don't play well with threads right now, so exclude any758// thread-spawning test if it looks like threads are spawned in that case.759if fuzz_config.wasmtime.consume_fuel || fuzz_config.wasmtime.epoch_interruption {760if test.contents.contains("(thread") {761return Err(arbitrary::Error::IncorrectFormat);762}763}764765log::debug!("running {:?}", test.path);766let async_ = if fuzz_config.wasmtime.async_config == generators::AsyncConfig::Disabled {767wasmtime_wast::Async::No768} else {769wasmtime_wast::Async::Yes770};771log::debug!("async: {async_:?}");772let engine = Engine::new(&fuzz_config.to_wasmtime()).unwrap();773let mut wast_context = WastContext::new(&engine, async_, move |store| {774fuzz_config.configure_store_epoch_and_fuel(store);775});776wast_context777.register_spectest(&wasmtime_wast::SpectestConfig {778use_shared_memory: true,779suppress_prints: true,780})781.unwrap();782wast_context783.run_wast(test.path.to_str().unwrap(), test.contents.as_bytes())784.unwrap();785Ok(())786}787788/// Execute a series of `gc` operations.789///790/// Returns the number of `gc` operations which occurred throughout the test791/// case -- used to test below that gc happens reasonably soon and eventually.792pub fn gc_ops(mut fuzz_config: generators::Config, mut ops: GcOps) -> Result<usize> {793let expected_drops = Arc::new(AtomicUsize::new(0));794let num_dropped = Arc::new(AtomicUsize::new(0));795796let num_gcs = Arc::new(AtomicUsize::new(0));797{798fuzz_config.wasmtime.consume_fuel = true;799let mut store = fuzz_config.to_store();800store.set_fuel(1_000).unwrap();801802let wasm = ops.to_wasm_binary();803log_wasm(&wasm);804let module = match compile_module(store.engine(), &wasm, KnownValid::No, &fuzz_config) {805Some(m) => m,806None => return Ok(0),807};808809let mut linker = Linker::new(store.engine());810811// To avoid timeouts, limit the number of explicit GCs we perform per812// test case.813const MAX_GCS: usize = 5;814815let func_ty = FuncType::new(816store.engine(),817vec![],818vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],819);820let func = Func::new(&mut store, func_ty, {821let num_dropped = num_dropped.clone();822let expected_drops = expected_drops.clone();823let num_gcs = num_gcs.clone();824move |mut caller: Caller<'_, StoreLimits>, _params, results| {825log::info!("gc_ops: GC");826if num_gcs.fetch_add(1, SeqCst) < MAX_GCS {827caller.gc(None)?;828}829830let a = ExternRef::new(831&mut caller,832CountDrops::new(&expected_drops, num_dropped.clone()),833)?;834let b = ExternRef::new(835&mut caller,836CountDrops::new(&expected_drops, num_dropped.clone()),837)?;838let c = ExternRef::new(839&mut caller,840CountDrops::new(&expected_drops, num_dropped.clone()),841)?;842843log::info!("gc_ops: gc() -> ({a:?}, {b:?}, {c:?})");844results[0] = Some(a).into();845results[1] = Some(b).into();846results[2] = Some(c).into();847Ok(())848}849});850linker.define(&store, "", "gc", func).unwrap();851852linker853.func_wrap("", "take_refs", {854let expected_drops = expected_drops.clone();855move |caller: Caller<'_, StoreLimits>,856a: Option<Rooted<ExternRef>>,857b: Option<Rooted<ExternRef>>,858c: Option<Rooted<ExternRef>>|859-> Result<()> {860log::info!("gc_ops: take_refs({a:?}, {b:?}, {c:?})",);861862// Do the assertion on each ref's inner data, even though it863// all points to the same atomic, so that if we happen to864// run into a use-after-free bug with one of these refs we865// are more likely to trigger a segfault.866if let Some(a) = a {867let a = a868.data(&caller)?869.unwrap()870.downcast_ref::<CountDrops>()871.unwrap();872assert!(a.0.load(SeqCst) <= expected_drops.load(SeqCst));873}874if let Some(b) = b {875let b = b876.data(&caller)?877.unwrap()878.downcast_ref::<CountDrops>()879.unwrap();880assert!(b.0.load(SeqCst) <= expected_drops.load(SeqCst));881}882if let Some(c) = c {883let c = c884.data(&caller)?885.unwrap()886.downcast_ref::<CountDrops>()887.unwrap();888assert!(c.0.load(SeqCst) <= expected_drops.load(SeqCst));889}890Ok(())891}892})893.unwrap();894895let func_ty = FuncType::new(896store.engine(),897vec![],898vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],899);900let func = Func::new(&mut store, func_ty, {901let num_dropped = num_dropped.clone();902let expected_drops = expected_drops.clone();903move |mut caller, _params, results| {904log::info!("gc_ops: make_refs");905906let a = ExternRef::new(907&mut caller,908CountDrops::new(&expected_drops, num_dropped.clone()),909)?;910let b = ExternRef::new(911&mut caller,912CountDrops::new(&expected_drops, num_dropped.clone()),913)?;914let c = ExternRef::new(915&mut caller,916CountDrops::new(&expected_drops, num_dropped.clone()),917)?;918919log::info!("gc_ops: make_refs() -> ({a:?}, {b:?}, {c:?})");920921results[0] = Some(a).into();922results[1] = Some(b).into();923results[2] = Some(c).into();924925Ok(())926}927});928linker.define(&store, "", "make_refs", func).unwrap();929930let func_ty = FuncType::new(931store.engine(),932vec![ValType::Ref(RefType::new(true, HeapType::Struct))],933vec![],934);935936let func = Func::new(&mut store, func_ty, {937move |_caller: Caller<'_, StoreLimits>, _params, _results| {938log::info!("gc_ops: take_struct(<ref null struct>)");939Ok(())940}941});942943linker.define(&store, "", "take_struct", func).unwrap();944945for imp in module.imports() {946if imp.module() == "" {947let name = imp.name();948if name.starts_with("take_struct_") {949if let wasmtime::ExternType::Func(ft) = imp.ty() {950let imp_name = name.to_string();951let func =952Func::new(&mut store, ft.clone(), move |_caller, _params, _results| {953log::info!("gc_ops: {imp_name}(<typed structref>)");954Ok(())955});956linker.define(&store, "", name, func).unwrap();957}958}959}960}961962let instance = linker.instantiate(&mut store, &module).unwrap();963let run = instance.get_func(&mut store, "run").unwrap();964965{966let mut scope = RootScope::new(&mut store);967968log::info!(969"gc_ops: begin allocating {} externref arguments",970ops.limits.num_globals971);972let args: Vec<_> = (0..ops.limits.num_params)973.map(|_| {974Ok(Val::ExternRef(Some(ExternRef::new(975&mut scope,976CountDrops::new(&expected_drops, num_dropped.clone()),977)?)))978})979.collect::<Result<_>>()?;980log::info!(981"gc_ops: end allocating {} externref arguments",982ops.limits.num_globals983);984985// The generated function should always return a trap. The only two986// valid traps are table-out-of-bounds which happens through `table.get`987// and `table.set` generated or an out-of-fuel trap. Otherwise any other988// error is unexpected and should fail fuzzing.989log::info!("gc_ops: calling into Wasm `run` function");990let err = run.call(&mut scope, &args, &mut []).unwrap_err();991if err.is::<GcHeapOutOfMemory<CountDrops>>() || err.is::<GcHeapOutOfMemory<()>>() {992// Accept GC OOM as an allowed outcome for this fuzzer.993} else {994let trap = err995.downcast::<Trap>()996.expect("if not GC oom, error should be a Wasm trap");997match trap {998Trap::TableOutOfBounds | Trap::OutOfFuel | Trap::AllocationTooLarge => {}999_ => panic!("unexpected trap: {trap}"),1000}1001}1002}10031004// Do a final GC after running the Wasm.1005store.gc(None)?;1006}10071008assert_eq!(num_dropped.load(SeqCst), expected_drops.load(SeqCst));1009return Ok(num_gcs.load(SeqCst));10101011struct CountDrops(Arc<AtomicUsize>);10121013impl CountDrops {1014fn new(expected_drops: &AtomicUsize, num_dropped: Arc<AtomicUsize>) -> Self {1015let expected = expected_drops.fetch_add(1, SeqCst);1016log::info!(1017"CountDrops::new: expected drops: {expected} -> {}",1018expected + 11019);1020Self(num_dropped)1021}1022}10231024impl Drop for CountDrops {1025fn drop(&mut self) {1026let drops = self.0.fetch_add(1, SeqCst);1027log::info!("CountDrops::drop: actual drops: {drops} -> {}", drops + 1);1028}1029}1030}10311032#[derive(Default)]1033struct HelperThread {1034state: Arc<HelperThreadState>,1035thread: Option<std::thread::JoinHandle<()>>,1036}10371038#[derive(Default)]1039struct HelperThreadState {1040should_exit: Mutex<bool>,1041should_exit_cvar: Condvar,1042}10431044impl HelperThread {1045fn run_periodically(&mut self, dur: Duration, mut closure: impl FnMut() + Send + 'static) {1046let state = self.state.clone();1047self.thread = Some(std::thread::spawn(move || {1048// Using our mutex/condvar we wait here for the first of `dur` to1049// pass or the `HelperThread` instance to get dropped.1050let mut should_exit = state.should_exit.lock().unwrap();1051while !*should_exit {1052let (lock, result) = state1053.should_exit_cvar1054.wait_timeout(should_exit, dur)1055.unwrap();1056should_exit = lock;1057// If we timed out for sure then there's no need to continue1058// since we'll just abort on the next `checked_sub` anyway.1059if result.timed_out() {1060closure();1061}1062}1063}));1064}1065}10661067impl Drop for HelperThread {1068fn drop(&mut self) {1069let thread = match self.thread.take() {1070Some(thread) => thread,1071None => return,1072};1073// Signal our thread that it should exit and wake it up in case it's1074// sleeping.1075*self.state.should_exit.lock().unwrap() = true;1076self.state.should_exit_cvar.notify_one();10771078// ... and then wait for the thread to exit to ensure we clean up1079// after ourselves.1080thread.join().unwrap();1081}1082}10831084/// Instantiates a wasm module and runs its exports with dummy values, all in1085/// an async fashion.1086///1087/// Attempts to stress yields in host functions to ensure that exiting and1088/// resuming a wasm function call works.1089pub fn call_async(wasm: &[u8], config: &generators::Config, mut poll_amts: &[u32]) {1090let mut store = config.to_store();1091let module = match compile_module(store.engine(), wasm, KnownValid::Yes, config) {1092Some(module) => module,1093None => return,1094};10951096// Configure a helper thread to periodically increment the epoch to1097// forcibly enable yields-via-epochs if epochs are in use. Note that this1098// is required because the wasm isn't otherwise guaranteed to necessarily1099// call any imports which will also increment the epoch.1100let mut helper_thread = HelperThread::default();1101if let generators::AsyncConfig::YieldWithEpochs { dur, .. } = &config.wasmtime.async_config {1102let engine = store.engine().clone();1103helper_thread.run_periodically(*dur, move || engine.increment_epoch());1104}11051106// Generate a `Linker` where all function imports are custom-built to yield1107// periodically and additionally increment the epoch.1108let mut imports = Vec::new();1109for import in module.imports() {1110let item = match import.ty() {1111ExternType::Func(ty) => {1112let poll_amt = take_poll_amt(&mut poll_amts);1113Func::new_async(&mut store, ty.clone(), move |caller, _, results| {1114let ty = ty.clone();1115Box::new(async move {1116caller.engine().increment_epoch();1117log::info!("yielding {poll_amt} times in import");1118YieldN(poll_amt).await;1119for (ret_ty, result) in ty.results().zip(results) {1120*result = ret_ty.default_value().unwrap();1121}1122Ok(())1123})1124})1125.into()1126}1127other_ty => match other_ty.default_value(&mut store) {1128Ok(item) => item,1129Err(e) => {1130log::warn!("couldn't create import for {import:?}: {e:?}");1131return;1132}1133},1134};1135imports.push(item);1136}11371138// Run the instantiation process, asynchronously, and if everything1139// succeeds then pull out the instance.1140// log::info!("starting instantiation");1141let instance = block_on(Timeout {1142future: Instance::new_async(&mut store, &module, &imports),1143polls: take_poll_amt(&mut poll_amts),1144end: Instant::now() + Duration::from_millis(2_000),1145});1146let instance = match instance {1147Ok(instantiation_result) => match unwrap_instance(&store, instantiation_result) {1148Some(instance) => instance,1149None => {1150log::info!("instantiation hit a nominal error");1151return; // resource exhaustion or limits met1152}1153},1154Err(_) => {1155log::info!("instantiation failed to complete");1156return; // Timed out or ran out of polls1157}1158};11591160// Run each export of the instance in the same manner as instantiation1161// above. Dummy values are passed in for argument values here:1162//1163// TODO: this should probably be more clever about passing in arguments for1164// example they might be used as pointers or something and always using 01165// isn't too interesting.1166let funcs = instance1167.exports(&mut store)1168.filter_map(|e| {1169let name = e.name().to_string();1170let func = e.into_extern().into_func()?;1171Some((name, func))1172})1173.collect::<Vec<_>>();1174for (name, func) in funcs {1175let ty = func.ty(&store);1176let params = ty1177.params()1178.map(|ty| ty.default_value().unwrap())1179.collect::<Vec<_>>();1180let mut results = ty1181.results()1182.map(|ty| ty.default_value().unwrap())1183.collect::<Vec<_>>();11841185log::info!("invoking export {name:?}");1186let future = func.call_async(&mut store, ¶ms, &mut results);1187match block_on(Timeout {1188future,1189polls: take_poll_amt(&mut poll_amts),1190end: Instant::now() + Duration::from_millis(2_000),1191}) {1192// On success or too many polls, try the next export.1193Ok(_) | Err(Exhausted::Polls) => {}11941195// If time ran out then stop the current test case as we might have1196// already sucked up a lot of time for this fuzz test case so don't1197// keep it going.1198Err(Exhausted::Time) => return,1199}1200}12011202fn take_poll_amt(polls: &mut &[u32]) -> u32 {1203match polls.split_first() {1204Some((a, rest)) => {1205*polls = rest;1206*a1207}1208None => 0,1209}1210}12111212/// Helper future for applying a timeout to `future` up to either when `end`1213/// is the current time or `polls` polls happen.1214///1215/// Note that this helps to time out infinite loops in wasm, for example.1216struct Timeout<F> {1217future: F,1218/// If the future isn't ready by this time then the `Timeout<F>` future1219/// will return `None`.1220end: Instant,1221/// If the future doesn't resolve itself in this many calls to `poll`1222/// then the `Timeout<F>` future will return `None`.1223polls: u32,1224}12251226enum Exhausted {1227Time,1228Polls,1229}12301231impl<F: Future> Future for Timeout<F> {1232type Output = Result<F::Output, Exhausted>;12331234fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {1235let (end, polls, future) = unsafe {1236let me = self.get_unchecked_mut();1237(me.end, &mut me.polls, Pin::new_unchecked(&mut me.future))1238};1239match future.poll(cx) {1240Poll::Ready(val) => Poll::Ready(Ok(val)),1241Poll::Pending => {1242if Instant::now() >= end {1243log::warn!("future operation timed out");1244return Poll::Ready(Err(Exhausted::Time));1245}1246if *polls == 0 {1247log::warn!("future operation ran out of polls");1248return Poll::Ready(Err(Exhausted::Polls));1249}1250*polls -= 1;1251Poll::Pending1252}1253}1254}1255}1256}12571258#[cfg(test)]1259mod tests {1260use super::*;1261use crate::test::{gen_until_pass, test_n_times};1262use wasmparser::{Validator, WasmFeatures};12631264// Test that the `gc_ops` fuzzer eventually runs the gc function in the host.1265// We've historically had issues where this fuzzer accidentally wasn't fuzzing1266// anything for a long time so this is an attempt to prevent that from happening1267// again.1268#[test]1269fn gc_ops_eventually_gcs() {1270// Skip if we're under emulation because some fuzz configurations will do1271// large address space reservations that QEMU doesn't handle well.1272if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {1273return;1274}12751276let ok = gen_until_pass(|(config, test), _| {1277let result = gc_ops(config, test)?;1278Ok(result > 0)1279});12801281if !ok {1282panic!("gc was never found");1283}1284}12851286#[test]1287fn module_generation_uses_expected_proposals() {1288// Proposals that Wasmtime supports. Eventually a module should be1289// generated that needs these proposals.1290let mut expected = WasmFeatures::MUTABLE_GLOBAL1291| WasmFeatures::FLOATS1292| WasmFeatures::SIGN_EXTENSION1293| WasmFeatures::SATURATING_FLOAT_TO_INT1294| WasmFeatures::MULTI_VALUE1295| WasmFeatures::BULK_MEMORY1296| WasmFeatures::REFERENCE_TYPES1297| WasmFeatures::SIMD1298| WasmFeatures::MULTI_MEMORY1299| WasmFeatures::RELAXED_SIMD1300| WasmFeatures::TAIL_CALL1301| WasmFeatures::WIDE_ARITHMETIC1302| WasmFeatures::MEMORY641303| WasmFeatures::FUNCTION_REFERENCES1304| WasmFeatures::GC1305| WasmFeatures::GC_TYPES1306| WasmFeatures::CUSTOM_PAGE_SIZES1307| WasmFeatures::EXTENDED_CONST1308| WasmFeatures::EXCEPTIONS;13091310// All other features that wasmparser supports, which is presumably a1311// superset of the features that wasm-smith supports, are listed here as1312// unexpected. This means, for example, that if wasm-smith updates to1313// include a new proposal by default that wasmtime implements then it1314// will be required to be listed above.1315let unexpected = WasmFeatures::all() ^ expected;13161317let ok = gen_until_pass(|config: generators::Config, u| {1318let wasm = config.generate(u, None)?.to_bytes();13191320// Double-check the module is valid1321Validator::new_with_features(WasmFeatures::all()).validate_all(&wasm)?;13221323// If any of the unexpected features are removed then this module1324// should always be valid, otherwise something went wrong.1325for feature in unexpected.iter() {1326let ok =1327Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);1328if ok.is_err() {1329wasmtime::bail!("generated a module with {feature:?} but that wasn't expected");1330}1331}13321333// If any of `expected` is removed and the module fails to validate,1334// then that means the module requires that feature. Remove that1335// from the set of features we're then expecting.1336for feature in expected.iter() {1337let ok =1338Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);1339if ok.is_err() {1340expected ^= feature;1341}1342}13431344Ok(expected.is_empty())1345});13461347if !ok {1348panic!("never generated wasm module using {expected:?}");1349}1350}13511352#[test]1353fn wast_smoke_test() {1354test_n_times(50, |(), u| super::wast_test(u));1355}1356}135713581359