Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Make const expression evaluation fallible during instantiation to handle OOM (#13414)

Nick Fitzgerald311fb9cb6631
diff --git a/crates/fuzzing/tests/oom/instance.rs b/crates/fuzzing/tests/oom/instance.rs
index dae6e0f466..a3c35a7516 100644
--- a/crates/fuzzing/tests/oom/instance.rs
+++ b/crates/fuzzing/tests/oom/instance.rs
@@ -325,3 +325,34 @@ fn instance_global_init_with_imported_global() -> Result<()> {
Ok(())
})
}
+
+#[test]
+fn table_element_segment_init() -> Result<()> {
+ let module_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Module::new(
+ &engine,
+ r#"(module
+ (table (export "table") 10 funcref)
+ (func $f (result i32) (i32.const 42))
+ (elem (i32.const 0) func $f)
+ )"#,
+ )?
+ .serialize()?
+ };
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ let module = unsafe { Module::deserialize(&engine, &module_bytes)? };
+ let linker = Linker::<()>::new(&engine);
+ let instance_pre = linker.instantiate_pre(&module)?;
+
+ OomTest::new().allow_alloc_after_oom(true).test(|| {
+ let mut store = Store::try_new(&engine, ())?;
+ let _instance = instance_pre.instantiate(&mut store)?;
+ Ok(())
+ })
+}
diff --git a/crates/wasmtime/src/runtime/vm/instance/allocator.rs b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
index 41b7670dcd..e2f6ae0824 100644
--- a/crates/wasmtime/src/runtime/vm/instance/allocator.rs
+++ b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
@@ -509,9 +509,7 @@ fn check_table_init_bounds(
let mut store = OpaqueRootScope::new(store);
for segment in module.table_initialization.segments.iter() {
- let start = const_evaluator
- .eval_int(&mut store, context, &segment.offset)
- .expect("const expression should be valid");
+ let start = const_evaluator.eval_int(&mut store, context, &segment.offset)?;
let start = get_index(start, module.tables[segment.table_index].idx_type);
let end = start.checked_add(segment.elements.len());
@@ -565,9 +563,7 @@ async fn initialize_tables(
// iterates over all segments (Segments mode) or leftover
// segments (FuncTable mode) to initialize.
for segment in module.table_initialization.segments.iter() {
- let start = const_evaluator
- .eval_int(&mut store, context, &segment.offset)
- .expect("const expression should be valid");
+ let start = const_evaluator.eval_int(&mut store, context, &segment.offset)?;
let start = get_index(start, module.tables[segment.table_index].idx_type);
let end = start
@@ -710,7 +706,7 @@ fn initialize_memories(
let val = self
.const_evaluator
.eval_int(&mut store, self.context, expr)
- .expect("const expression should be valid");
+ .ok()?;
Some(get_index(
val,
store.instance(self.context.instance).env_module().memories[memory].idx_type,