Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pulley-interpreter-fuzz = { workspace = true }
smallvec = { workspace = true }
wasmparser = { workspace = true }
wasmtime = { workspace = true, features = ["winch"] }
wasmtime-core = { workspace = true }
wasmtime-fuzzing = { workspace = true }
wasmtime-test-util = { workspace = true }
log = { workspace = true }
Expand Down Expand Up @@ -126,3 +127,9 @@ path = "fuzz_targets/gc_ops.rs"
test = false
doc = false
bench = false

[[bin]]
name = "oom"
path = "fuzz_targets/oom.rs"
test = false
doc = false
146 changes: 146 additions & 0 deletions fuzz/fuzz_targets/oom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#![no_main]

use libfuzzer_sys::arbitrary::{Arbitrary, Result, Unstructured};
use wasmtime::{Engine, Module, Store, Trap, Val, error::OutOfMemory};
use wasmtime_core::alloc::TryVec;
use wasmtime_fuzzing::generators::Config;
use wasmtime_fuzzing::oom::{OomTest, OomTestAllocator};
use wasmtime_fuzzing::oracles::dummy;
use wasmtime_fuzzing::single_module_fuzzer::KnownValid;

const OOM_TEST_ITERS: u32 = 10;
const OOM_TEST_FUEL: u64 = 1000;

#[global_allocator]
static GLOBAL_ALLOCATOR: OomTestAllocator = OomTestAllocator::new();

wasmtime_fuzzing::single_module_fuzzer!(execute gen_module);

#[derive(Debug)]
struct OomInput {
config: Config,
seed: u64,
}

impl<'a> Arbitrary<'a> for OomInput {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let mut config: Config = u.arbitrary()?;
config.module_config.config.exceptions_enabled = false;
config.module_config.config.gc_enabled = false;
config.module_config.config.reference_types_enabled = false;
config.module_config.function_references_enabled = false;
config.module_config.config.export_everything = true;
config.wasmtime.strategy =
wasmtime_fuzzing::generators::InstanceAllocationStrategy::OnDemand;
let seed = u.arbitrary()?;
Ok(OomInput { config, seed })
}
}

fn compile(config: &Config, wasm: &[u8]) -> wasmtime::Result<Vec<u8>> {
let mut wasmtime_config = config.to_wasmtime();
wasmtime_config.concurrency_support(false);
wasmtime_config.consume_fuel(true);
let engine = Engine::new(&wasmtime_config)?;
let module = Module::new(&engine, wasm)?;
module.serialize()
}

fn execute(
module: &[u8],
_known_valid: KnownValid,
input: OomInput,
_u: &mut Unstructured<'_>,
) -> Result<()> {
if cfg!(not(arc_try_new)) {
panic!(
"The OOM fuzzer is disabled because `cfg(arc_try_new)` was not enabled. Build with \
`RUSTFLAGS=--cfg=arc_try_new` to enable."
);
}

let module_bytes = match compile(&input.config, module) {
Ok(bytes) => bytes,
Err(_) => return Ok(()),
};

let mut oom_config = input.config.to_wasmtime();
oom_config.enable_compiler(false);
oom_config.concurrency_support(false);
oom_config.consume_fuel(true);

// Prevent real process-level OOM: fuzzer-generated configs can set
// `memory_reservation(0)`, forcing `mmap` to commit real pages that bypass
// `OomTestAllocator`. Use large virtual reservations instead.
oom_config.memory_reservation(1 << 32);
oom_config.memory_guard_size(1 << 31);

let oom_engine = match Engine::new(&oom_config) {
Ok(e) => e,
Err(_) => return Ok(()),
};

let _ = OomTest::new()
.seed(input.seed)
.max_iters(OOM_TEST_ITERS)
.allow_alloc_after_oom(true)
.alloc_succeeds_after_oom(true)
.allow_missed_oom_errors(true)
.fuzz(|| {
let module = unsafe { Module::deserialize(&oom_engine, &module_bytes)? };

let mut store = Store::try_new(&oom_engine, ())?;
store.set_fuel(OOM_TEST_FUEL).unwrap();

let linker = dummy::dummy_linker(&mut store, &module)?;
let instance = linker.instantiate(&mut store, &module)?;

'export_loop: for export in module.exports() {
let extern_ty = export.ty();
let Some(func_ty) = extern_ty.func() else {
continue;
};
let func = instance.get_func(&mut store, export.name()).unwrap();

// Build default params; skip if any param type has no default.
let mut params: TryVec<Val> = TryVec::with_capacity(func_ty.params().len())?;
for p in func_ty.params() {
match p.default_value() {
Some(v) => params.push(v)?,
None => {
continue 'export_loop;
}
}
}

let mut results: TryVec<Val> = TryVec::with_capacity(func_ty.results().len())?;
for _ in 0..func_ty.results().len() {
results.push(Val::I32(0))?;
}

match func.call(&mut store, &params, &mut results) {
// OOM; return from this OOM test iteration.
Err(e) if e.is::<OutOfMemory>() => return Err(e),

// Out of fuel; stop calling exports.
Err(e)
if e.downcast_ref::<Trap>()
.is_some_and(|trap| *trap == Trap::OutOfFuel) =>
{
break;
}

Err(_) | Ok(_) => {}
}
}

Ok(())
});

Ok(())
}

fn gen_module(input: &mut OomInput, u: &mut Unstructured<'_>) -> Result<(Vec<u8>, KnownValid)> {
let module = input.config.generate(u, Some(1000))?;
Ok((module.to_bytes(), KnownValid::Yes))
}
Loading