Skip to content

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

Don't unwrap results of const-eval (#11557)

Alex Crichton7941919851b8
diff --git a/crates/wasmtime/src/runtime/vm/instance.rs b/crates/wasmtime/src/runtime/vm/instance.rs
index 796613d2d1..62eb30304a 100644
--- a/crates/wasmtime/src/runtime/vm/instance.rs
+++ b/crates/wasmtime/src/runtime/vm/instance.rs
@@ -888,7 +888,7 @@ impl Instance {
dst: u64,
src: u64,
len: u64,
- ) -> Result<(), Trap> {
+ ) -> Result<()> {
let mut storage = None;
let elements = store
.instance(instance)
@@ -918,7 +918,7 @@ impl Instance {
dst: u64,
src: u64,
len: u64,
- ) -> Result<(), Trap> {
+ ) -> Result<()> {
// https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-table-init
let store_id = store.id();
@@ -967,8 +967,7 @@ impl Instance {
for (i, expr) in positions.zip(exprs) {
let element = const_evaluator
.eval(&mut store, limiter.as_deref_mut(), &mut context, expr)
- .await
- .expect("const expr should be valid");
+ .await?;
table.set_(&mut store, i, element.ref_().unwrap()).unwrap();
}
}
diff --git a/crates/wasmtime/src/runtime/vm/instance/allocator.rs b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
index c9c7c90cab..24f616aeaf 100644
--- a/crates/wasmtime/src/runtime/vm/instance/allocator.rs
+++ b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
@@ -535,8 +535,7 @@ async fn initialize_tables(
TableInitialValue::Expr(expr) => {
let init = const_evaluator
.eval(&mut store, limiter.as_deref_mut(), context, expr)
- .await
- .expect("const expression should be valid");
+ .await?;
let idx = module.table_index(table);
let id = store.id();
let table = store
@@ -767,8 +766,7 @@ async fn initialize_globals(
} else {
const_evaluator
.eval(&mut store, limiter.as_deref_mut(), context, init)
- .await
- .expect("should be a valid const expr")
+ .await?
};
let id = store.id();
diff --git a/crates/wasmtime/src/runtime/vm/libcalls.rs b/crates/wasmtime/src/runtime/vm/libcalls.rs
index 395c22b52a..3e4ca7c5d4 100644
--- a/crates/wasmtime/src/runtime/vm/libcalls.rs
+++ b/crates/wasmtime/src/runtime/vm/libcalls.rs
@@ -987,8 +987,7 @@ fn array_new_elem(
for x in xs.iter() {
let val = *const_evaluator
.eval(&mut store, limiter.as_mut(), &mut const_context, x)
- .await
- .expect("const expr should be valid");
+ .await?;
vals.push(val);
}
}
@@ -1083,8 +1082,7 @@ fn array_init_elem(
{
let val = *const_evaluator
.eval(&mut store, limiter.as_mut(), &mut const_context, x)
- .await
- .expect("const expr should be valid");
+ .await?;
vals.push(val);
}
vals
diff --git a/tests/all/gc.rs b/tests/all/gc.rs
index e1eb55b55a..99bb4b3698 100644
--- a/tests/all/gc.rs
+++ b/tests/all/gc.rs
@@ -1567,3 +1567,237 @@ fn owned_rooted_lots_of_root_creation() -> Result<()> {
Ok(())
}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn runtime_table_init_oom() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_gc(true);
+ config.wasm_function_references(true);
+ config.memory_may_move(false);
+ config.memory_reservation(64 << 10);
+ config.memory_reservation_for_growth(0);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ let module = Module::new(
+ store.engine(),
+ r#"
+ (module
+ (table 100 arrayref)
+
+ (type $a (array i31ref))
+
+ (func (export "run")
+ i32.const 0
+ i32.const 0
+ i32.const 5
+ table.init $e)
+ (elem $e arrayref
+ (array.new_default $a (i32.const 100))
+ (array.new_default $a (i32.const 10000))
+ (array.new_default $a (i32.const 10000))
+ (array.new_default $a (i32.const 10000))
+ (array.new_default $a (i32.const 1000000))
+ )
+ )
+ "#,
+ )?;
+
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let func = instance.get_typed_func::<(), ()>(&mut store, "run")?;
+ func.call(&mut store, ())
+ .unwrap_err()
+ .downcast::<GcHeapOutOfMemory<()>>()?;
+
+ Ok(())
+}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn instantiate_table_init_oom() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_gc(true);
+ config.wasm_function_references(true);
+ config.memory_may_move(false);
+ config.memory_reservation(64 << 10);
+ config.memory_reservation_for_growth(0);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ let module = Module::new(
+ store.engine(),
+ r#"
+ (module
+ (table 100 arrayref)
+
+ (type $a (array i31ref))
+
+ (elem (i32.const 0) arrayref
+ (array.new_default $a (i32.const 100))
+ (array.new_default $a (i32.const 10000))
+ (array.new_default $a (i32.const 10000))
+ (array.new_default $a (i32.const 10000))
+ (array.new_default $a (i32.const 1000000))
+ )
+ )
+ "#,
+ )?;
+
+ Instance::new(&mut store, &module, &[])
+ .unwrap_err()
+ .downcast::<GcHeapOutOfMemory<()>>()?;
+
+ Ok(())
+}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn instantiate_table_init_expr_oom() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_gc(true);
+ config.wasm_function_references(true);
+ config.memory_may_move(false);
+ config.memory_reservation(64 << 10);
+ config.memory_reservation_for_growth(0);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ let module = Module::new(
+ store.engine(),
+ r#"
+ (module
+ (type $a (array i31ref))
+ (table 100 (ref $a) (array.new_default $a (i32.const 100000)))
+ )
+ "#,
+ )?;
+
+ Instance::new(&mut store, &module, &[])
+ .unwrap_err()
+ .downcast::<GcHeapOutOfMemory<()>>()?;
+
+ Ok(())
+}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn instantiate_global_init_oom() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_gc(true);
+ config.wasm_function_references(true);
+ config.memory_may_move(false);
+ config.memory_reservation(64 << 10);
+ config.memory_reservation_for_growth(0);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ let module = Module::new(
+ store.engine(),
+ r#"
+ (module
+ (table 100 arrayref)
+ (type $a (array i31ref))
+ (global (ref $a) (array.new_default $a (i32.const 10000000)))
+ )
+ "#,
+ )?;
+
+ Instance::new(&mut store, &module, &[])
+ .unwrap_err()
+ .downcast::<GcHeapOutOfMemory<()>>()?;
+
+ Ok(())
+}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn array_new_elem_oom() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_gc(true);
+ config.wasm_function_references(true);
+ config.memory_may_move(false);
+ config.memory_reservation(64 << 10);
+ config.memory_reservation_for_growth(0);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ let module = Module::new(
+ store.engine(),
+ r#"
+ (module
+ (type $a (array (mut arrayref)))
+ (type $i (array i31ref))
+
+ (func (export "run")
+ i32.const 0
+ i32.const 5
+ array.new_elem $a $e
+ drop)
+
+ (elem $e arrayref
+ (array.new_default $i (i32.const 100))
+ (array.new_default $i (i32.const 10000))
+ (array.new_default $i (i32.const 10000))
+ (array.new_default $i (i32.const 10000))
+ (array.new_default $i (i32.const 1000000))
+ )
+ )
+ "#,
+ )?;
+
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let func = instance.get_typed_func::<(), ()>(&mut store, "run")?;
+ func.call(&mut store, ())
+ .unwrap_err()
+ .downcast::<GcHeapOutOfMemory<()>>()?;
+
+ Ok(())
+}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn array_init_elem_oom() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_gc(true);
+ config.wasm_function_references(true);
+ config.memory_may_move(false);
+ config.memory_reservation(64 << 10);
+ config.memory_reservation_for_growth(0);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ let module = Module::new(
+ store.engine(),
+ r#"
+ (module
+ (type $a (array (mut arrayref)))
+ (type $i (array i31ref))
+
+ (func (export "run")
+ i32.const 5
+ array.new_default $a
+ i32.const 0
+ i32.const 0
+ i32.const 5
+ array.init_elem $a $e)
+
+ (elem $e arrayref
+ (array.new_default $i (i32.const 100))
+ (array.new_default $i (i32.const 10000))
+ (array.new_default $i (i32.const 10000))
+ (array.new_default $i (i32.const 10000))
+ (array.new_default $i (i32.const 1000000))
+ )
+ )
+ "#,
+ )?;
+
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let func = instance.get_typed_func::<(), ()>(&mut store, "run")?;
+ func.call(&mut store, ())
+ .unwrap_err()
+ .downcast::<GcHeapOutOfMemory<()>>()?;
+
+ Ok(())
+}