Skip to content

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

Fix panicking overflow when calculating table sizes (#13245)

Alex Crichton1fc537358120
diff --git a/crates/wasmtime/src/runtime/vm/table.rs b/crates/wasmtime/src/runtime/vm/table.rs
index 7a26676438..c4c15c7598 100644
--- a/crates/wasmtime/src/runtime/vm/table.rs
+++ b/crates/wasmtime/src/runtime/vm/table.rs
@@ -309,7 +309,9 @@ unsafe fn alloc_dynamic_table_elements<T>(len: usize) -> Result<TryVec<Option<T>
let size = mem::size_of::<Option<T>>();
let size = size.next_multiple_of(align);
- let size = size.checked_mul(len).unwrap();
+ let size = size
+ .checked_mul(len)
+ .ok_or_else(|| format_err!("overflow calculating table allocation size"))?;
let layout = Layout::from_size_align(size, align)?;
diff --git a/tests/misc_testsuite/memory64/table-too-big.wast b/tests/misc_testsuite/memory64/table-too-big.wast
new file mode 100644
index 0000000000..ccb0a3cfb2
--- /dev/null
+++ b/tests/misc_testsuite/memory64/table-too-big.wast
@@ -0,0 +1,17 @@
+;;! memory64 = true
+;;! hogs_memory = true
+;;! reference_types = true
+
+(assert_trap
+ (module (table i64 0x2000_0000_0000_0000 funcref))
+ "overflow calculating table allocation size")
+
+(module
+ (table i64 0 funcref)
+ (func (export "grow") (param i64) (result i64)
+ (table.grow 0 (ref.null func) (local.get 0))
+ )
+)
+
+(assert_trap (invoke "grow" (i64.const 0x2000_0000_0000_0000))
+ "failed to allocate")