Skip to content

Commit 30ed757

Browse files
committed
Handle OOM in Module::deserialize
Part of bytecodealliance#12069
1 parent c78f891 commit 30ed757

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

crates/fuzzing/tests/oom.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,53 @@ fn linker_func_wrap() -> Result<()> {
508508
})
509509
}
510510

511+
#[test]
512+
#[cfg(arc_try_new)]
513+
fn module_deserialize() -> Result<()> {
514+
let module_bytes = {
515+
let mut config = Config::new();
516+
config.concurrency_support(false);
517+
let engine = Engine::new(&config)?;
518+
let module = Module::new(
519+
&engine,
520+
r#"
521+
(module
522+
(import "module" "func" (func (param i32) (result i32)))
523+
524+
(memory (export "memory") 1)
525+
(data (i32.const 0) "a")
526+
527+
(table (export "table") 1 funcref)
528+
(elem (i32.const 0) func 1)
529+
530+
(func (export "func") (param i32) (result i32)
531+
(call 0 (local.get 0))
532+
)
533+
)
534+
"#,
535+
)?;
536+
module.serialize()?
537+
};
538+
539+
let mut config = Config::new();
540+
config.enable_compiler(false);
541+
config.concurrency_support(false);
542+
let engine = Engine::new(&config)?;
543+
544+
OomTest::new()
545+
// NB: We use `postcard` to deserialize module metadata, and it will
546+
// return a `postcard::Error::SerdeDeCustom` when we generate an
547+
// `OutOfMemory` error during deserialization. That is then converted
548+
// into a `wasmtime::Error`, and in the process we will attempt to box
549+
// that into an `Error` trait object. There is no good way to avoid all
550+
// this, so just allow allocation attempts after OOM here.
551+
.allow_alloc_after_oom(true)
552+
.test(|| unsafe {
553+
let _ = Module::deserialize(&engine, &module_bytes)?;
554+
Ok(())
555+
})
556+
}
557+
511558
#[test]
512559
#[cfg(arc_try_new)]
513560
fn store_try_new() -> Result<()> {

crates/wasmtime/src/runtime/instantiate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl CompiledModule {
5555
let mut ret = Self {
5656
unique_id: CompiledModuleId::new(),
5757
engine_code,
58-
module: Arc::new(info.module),
58+
module: try_new::<Arc<_>>(info.module)?,
5959
meta: info.meta,
6060
index,
6161
func_names: info.func_names,

crates/wasmtime/src/runtime/module.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ impl Module {
523523

524524
// Package up all our data into an `EngineCode` and delegate to the final
525525
// step of module compilation.
526-
let code = Arc::new(EngineCode::new(code_memory, signatures, types.into()));
527-
let index = Arc::new(index);
526+
let code = try_new::<Arc<_>>(EngineCode::new(code_memory, signatures, types.into()))?;
527+
let index = try_new::<Arc<_>>(index)?;
528528
Module::from_parts_raw(engine, code, info, index, true)
529529
}
530530

@@ -547,7 +547,7 @@ impl Module {
547547
let _ = serializable;
548548

549549
Ok(Self {
550-
inner: Arc::new(ModuleInner {
550+
inner: try_new::<Arc<_>>(ModuleInner {
551551
engine: engine.clone(),
552552
code,
553553
memory_images: OnceLock::new(),
@@ -556,7 +556,7 @@ impl Module {
556556
serializable,
557557
offsets,
558558
checksum,
559-
}),
559+
})?,
560560
})
561561
}
562562

0 commit comments

Comments
 (0)