Skip to content

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

Fix a bounds check for imported memories (#14754)

Alex Crichton70d35066e714
diff --git a/crates/cranelift/src/bounds_checks.rs b/crates/cranelift/src/bounds_checks.rs
index 3c72fd0a2f..f3e8fe5503 100644
--- a/crates/cranelift/src/bounds_checks.rs
+++ b/crates/cranelift/src/bounds_checks.rs
@@ -328,7 +328,7 @@ fn bounds_check_field_access(
// Special case for when we can rely on virtual memory, the minimum
// byte size of this memory fits within the memory reservation, and
// memory isn't allowed to move. In this situation we know that
- // memory will statically not grow beyond `memory_reservation` so we
+ // memory will statically not grow beyond `memory_reservation`
// and we know that memory from 0 to that limit is guaranteed to be
// valid or trap. Here we effectively assume that the dynamic size
// of linear memory is its maximal value, `memory_reservation`, and
@@ -343,7 +343,15 @@ fn bounds_check_field_access(
// Since we have to emit explicit bounds checks, we might as well be
// precise, not rely on the virtual memory subsystem at all, and not
// factor in the guard pages here.
+ //
+ // Note that this is not applicable to imported memories. The minimum size
+ // of an imported memory's type is only a lower bound on the actual
+ // memory's minimum size, which may be larger than `memory_reservation`.
+ // In that situation the actual memory's allocation is larger than
+ // `memory_reservation` and accesses beyond `memory_reservation` may be
+ // in-bounds.
if can_use_virtual_memory
+ && !heap.imported
&& heap.memory.minimum_byte_size().unwrap_or(u64::MAX) <= memory_reservation
&& !heap.memory.memory_may_move(&memory_tunables)
&& memory_reservation >= offset_and_size
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 1fb1fe1781..44d5da3bd6 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -1690,6 +1690,7 @@ impl FuncEnvironment<'_> {
bound,
kind: MemoryKind::LinearMemory,
memory,
+ imported: self.module.is_imported_memory(index),
})
}
diff --git a/crates/cranelift/src/func_environ/gc.rs b/crates/cranelift/src/func_environ/gc.rs
index 96cbbf89fd..126291c906 100644
--- a/crates/cranelift/src/func_environ/gc.rs
+++ b/crates/cranelift/src/func_environ/gc.rs
@@ -1754,6 +1754,7 @@ impl FuncEnvironment<'_> {
bound: VmctxLoadChain::new(smallvec![store_ctx, bound]),
memory,
kind: MemoryKind::GcHeap,
+ imported: false,
});
self.gc_heap = Some(heap);
heap
diff --git a/crates/cranelift/src/translate/heap.rs b/crates/cranelift/src/translate/heap.rs
index 0eefc01763..09146f38c4 100644
--- a/crates/cranelift/src/translate/heap.rs
+++ b/crates/cranelift/src/translate/heap.rs
@@ -130,6 +130,9 @@ pub struct HeapData {
/// Whether this is a linear memory or a GC heap.
pub kind: MemoryKind,
+
+ /// Whether this heap is an imported linear memory.
+ pub imported: bool,
}
impl HeapData {
diff --git a/tests/all/memory.rs b/tests/all/memory.rs
index bac12b85c2..462310dbf1 100644
--- a/tests/all/memory.rs
+++ b/tests/all/memory.rs
@@ -847,6 +847,59 @@ fn minimum_larger_than_reservation_without_moving(cfg: &mut Config) -> Result<()
Ok(())
}
+#[wasmtime_test]
+#[cfg_attr(miri, ignore)]
+fn imported_memory_larger_than_reservation_without_moving(cfg: &mut Config) -> Result<()> {
+ cfg.wasm_memory64(true);
+ cfg.wasm_multi_memory(true);
+ cfg.memory_reservation(65536);
+ cfg.memory_may_move(false);
+ let engine = Engine::new(cfg)?;
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (import "" "m32" (memory 1))
+ (import "" "m64" (memory i64 1))
+ (func (export "load32") (param i32) (result i32)
+ local.get 0
+ i32.load8_u 0)
+ (func (export "load64") (param i64) (result i32)
+ local.get 0
+ i32.load8_u 1 offset=1)
+ )
+ "#,
+ )?;
+ let mut store = Store::new(&engine, ());
+ let m32 = Memory::new(&mut store, MemoryType::new(4, None)?)?;
+ let m64 = Memory::new(&mut store, MemoryType::new64(4, None)?)?;
+ m32.data_mut(&mut store)[4 * 65536 - 1] = 1;
+ m64.data_mut(&mut store)[4 * 65536 - 1] = 2;
+ let instance = Instance::new(&mut store, &module, &[m32.into(), m64.into()])?;
+ let load32 = instance.get_typed_func::<u32, u32>(&mut store, "load32")?;
+ let load64 = instance.get_typed_func::<u64, u32>(&mut store, "load64")?;
+
+ // Accesses beyond the importer's minimum size and the memory reservation,
+ // but within the actual memory's size, must succeed.
+ assert_eq!(load32.call(&mut store, 0x20000)?, 0);
+ assert_eq!(load32.call(&mut store, 4 * 65536 - 1)?, 1);
+ assert_eq!(load64.call(&mut store, 0x20000)?, 0);
+ assert_eq!(load64.call(&mut store, 4 * 65536 - 2)?, 2);
+
+ // Accesses past the actual memory's size must still trap.
+ let trap = load32
+ .call(&mut store, 4 * 65536)
+ .unwrap_err()
+ .downcast::<Trap>()?;
+ assert_eq!(trap, Trap::MemoryOutOfBounds);
+ let trap = load64
+ .call(&mut store, 4 * 65536 - 1)
+ .unwrap_err()
+ .downcast::<Trap>()?;
+ assert_eq!(trap, Trap::MemoryOutOfBounds);
+ Ok(())
+}
+
#[test]
fn new_memory_with_custom_page_size() -> Result<()> {
let engine = Engine::default();