Skip to content

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

winch: Don't treat memory_reservation as a bound when the minimum exceeds it (#14619)

Saúl Cabrera33c3c7928b6e
diff --git a/tests/all/memory.rs b/tests/all/memory.rs
index f9f4d57b8a..bac12b85c2 100644
--- a/tests/all/memory.rs
+++ b/tests/all/memory.rs
@@ -794,6 +794,59 @@ fn non_page_aligned_static_memory() -> Result<()> {
Ok(())
}
+// When a memory's minimum size exceeds `memory_reservation`, the runtime sizes
+// the allocation from the minimum, so compilers must not treat the reservation
+// as an upper bound on accessible memory.
+#[wasmtime_test]
+#[cfg_attr(miri, ignore)]
+fn minimum_larger_than_reservation_without_moving(cfg: &mut Config) -> Result<()> {
+ cfg.memory_reservation(0);
+ cfg.memory_may_move(false);
+ let engine = Engine::new(cfg)?;
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (memory 2)
+ (func (export "store") (param i32 i32)
+ local.get 0
+ local.get 1
+ i32.store)
+ (func (export "load") (param i32) (result i32)
+ local.get 0
+ i32.load)
+ (func (export "load_offset") (param i32) (result i32)
+ local.get 0
+ i32.load offset=70000)
+ )
+ "#,
+ )?;
+ let mut store = Store::new(&engine, ());
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let store_fn = instance.get_typed_func::<(u32, u32), ()>(&mut store, "store")?;
+ let load = instance.get_typed_func::<u32, u32>(&mut store, "load")?;
+ let load_offset = instance.get_typed_func::<u32, u32>(&mut store, "load_offset")?;
+
+ store_fn.call(&mut store, (0, 42))?;
+ assert_eq!(load.call(&mut store, 0)?, 42);
+ store_fn.call(&mut store, (70_000, 7))?;
+ assert_eq!(load_offset.call(&mut store, 0)?, 7);
+ assert_eq!(load.call(&mut store, 2 * 65536 - 4)?, 0);
+
+ // Accesses past the minimum size must still trap.
+ let trap = load
+ .call(&mut store, 2 * 65536 - 3)
+ .unwrap_err()
+ .downcast::<Trap>()?;
+ assert_eq!(trap, Trap::MemoryOutOfBounds);
+ let trap = load_offset
+ .call(&mut store, 2 * 65536 - 70_000 - 3)
+ .unwrap_err()
+ .downcast::<Trap>()?;
+ assert_eq!(trap, Trap::MemoryOutOfBounds);
+ Ok(())
+}
+
#[test]
fn new_memory_with_custom_page_size() -> Result<()> {
let engine = Engine::default();
diff --git a/tests/disas/winch/aarch64/load/min_exceeds_reservation.wat b/tests/disas/winch/aarch64/load/min_exceeds_reservation.wat
new file mode 100644
index 0000000000..3b63c8f657
--- /dev/null
+++ b/tests/disas/winch/aarch64/load/min_exceeds_reservation.wat
@@ -0,0 +1,63 @@
+;;! target = "aarch64"
+;;! test = "winch"
+;;! flags = ["-Omemory-reservation=0", "-Omemory-may-move=n"]
+
+;; The memory's minimum size exceeds `memory-reservation`, so the runtime
+;; sizes the allocation from the minimum and the reservation is not an upper
+;; bound: this access must get a dynamic bounds check, not a static trap.
+(module
+ (memory 2)
+ (func (export "load_offset") (param i32) (result i32)
+ local.get 0
+ i32.load offset=70000))
+;; wasm[0]::function[0]:
+;; stp x29, x30, [sp, #-0x10]!
+;; mov x29, sp
+;; str x28, [sp, #-0x10]!
+;; mov x28, sp
+;; ldur x16, [x0, #8]
+;; ldur x16, [x16, #0x18]
+;; mov x17, #0
+;; movk x17, #0x18
+;; add x16, x16, x17
+;; cmp sp, x16
+;; b.lo #0xbc
+;; 2c: mov x9, x0
+;; sub x28, x28, #0x18
+;; mov sp, x28
+;; stur x0, [x28, #0x10]
+;; stur x1, [x28, #8]
+;; stur w2, [x28, #4]
+;; ldur w0, [x28, #4]
+;; ldur x1, [x9, #0x40]
+;; mov w2, w0
+;; sub sp, x28, #8
+;; mov w16, #0x1174
+;; movk w16, #1, lsl #16
+;; adds x2, x2, x16, uxtx
+;; b.hs #0xc0
+;; 64: mov sp, x28
+;; cmp x2, x1, uxtx
+;; sub sp, x28, #8
+;; b.hi #0xc4
+;; 74: mov sp, x28
+;; ldur x3, [x9, #0x38]
+;; add x3, x3, w0, uxtw
+;; mov w16, #0x1170
+;; movk w16, #1, lsl #16
+;; add x3, x3, x16, uxtx
+;; mov x4, #0
+;; cmp x2, x1, uxtx
+;; csel x3, x4, x3, hi
+;; sub sp, x28, #8
+;; ldur w0, [x3]
+;; mov sp, x28
+;; add x28, x28, #0x18
+;; mov sp, x28
+;; mov sp, x28
+;; ldr x28, [sp], #0x10
+;; ldp x29, x30, [sp], #0x10
+;; ret
+;; bc: udf #0xc11f
+;; c0: udf #0xc11f
+;; c4: udf #0xc11f
diff --git a/tests/disas/winch/aarch64/load/reservation_static_oob.wat b/tests/disas/winch/aarch64/load/reservation_static_oob.wat
new file mode 100644
index 0000000000..481b96a875
--- /dev/null
+++ b/tests/disas/winch/aarch64/load/reservation_static_oob.wat
@@ -0,0 +1,61 @@
+;;! target = "aarch64"
+;;! test = "winch"
+;;! flags = ["-Omemory-reservation=0x20000", "-Omemory-may-move=n"]
+
+;; The memory's minimum size fits within `memory-reservation` and the memory
+;; can't move, so the reservation is an upper bound: an access whose offset
+;; exceeds it is out of bounds and must trap, via the regular bounds check.
+(module
+ (memory 2)
+ (func (export "load_offset") (param i32) (result i32)
+ local.get 0
+ i32.load offset=0x20000))
+;; wasm[0]::function[0]:
+;; stp x29, x30, [sp, #-0x10]!
+;; mov x29, sp
+;; str x28, [sp, #-0x10]!
+;; mov x28, sp
+;; ldur x16, [x0, #8]
+;; ldur x16, [x16, #0x18]
+;; mov x17, #0
+;; movk x17, #0x18
+;; add x16, x16, x17
+;; cmp sp, x16
+;; b.lo #0xb4
+;; 2c: mov x9, x0
+;; sub x28, x28, #0x18
+;; mov sp, x28
+;; stur x0, [x28, #0x10]
+;; stur x1, [x28, #8]
+;; stur w2, [x28, #4]
+;; ldur w0, [x28, #4]
+;; ldur x1, [x9, #0x40]
+;; mov w2, w0
+;; sub sp, x28, #8
+;; mov w16, #4
+;; movk w16, #2, lsl #16
+;; adds x2, x2, x16, uxtx
+;; b.hs #0xb8
+;; 64: mov sp, x28
+;; cmp x2, x1, uxtx
+;; sub sp, x28, #8
+;; b.hi #0xbc
+;; 74: mov sp, x28
+;; ldur x3, [x9, #0x38]
+;; add x3, x3, w0, uxtw
+;; add x3, x3, #0x20, lsl #12
+;; mov x4, #0
+;; cmp x2, x1, uxtx
+;; csel x3, x4, x3, hi
+;; sub sp, x28, #8
+;; ldur w0, [x3]
+;; mov sp, x28
+;; add x28, x28, #0x18
+;; mov sp, x28
+;; mov sp, x28
+;; ldr x28, [sp], #0x10
+;; ldp x29, x30, [sp], #0x10
+;; ret
+;; b4: udf #0xc11f
+;; b8: udf #0xc11f
+;; bc: udf #0xc11f
diff --git a/tests/disas/winch/x64/load/min_exceeds_reservation.wat b/tests/disas/winch/x64/load/min_exceeds_reservation.wat
new file mode 100644
index 0000000000..59d7e78b8b
--- /dev/null
+++ b/tests/disas/winch/x64/load/min_exceeds_reservation.wat
@@ -0,0 +1,46 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = ["-Omemory-reservation=0", "-Omemory-may-move=n"]
+
+;; The memory's minimum size exceeds `memory-reservation`, so the runtime
+;; sizes the allocation from the minimum and the reservation is not an upper
+;; bound: this access must get a dynamic bounds check, not a static trap.
+(module
+ (memory 2)
+ (func (export "load_offset") (param i32) (result i32)
+ local.get 0
+ i32.load offset=70000))
+;; wasm[0]::function[0]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x18(%r11), %r11
+;; addq $0x20, %r11
+;; cmpq %rsp, %r11
+;; ja 0x7b
+;; 1c: movq %rdi, %r14
+;; subq $0x20, %rsp
+;; movq %rdi, 0x18(%rsp)
+;; movq %rsi, 0x10(%rsp)
+;; movl %edx, 0xc(%rsp)
+;; movl 0xc(%rsp), %eax
+;; movq 0x40(%r14), %rcx
+;; movl %eax, %edx
+;; addq $0x11174, %rdx
+;; jb 0x7d
+;; 4b: cmpq %rcx, %rdx
+;; ja 0x7f
+;; 54: movq 0x38(%r14), %rbx
+;; movl %eax, %eax
+;; addq %rax, %rbx
+;; addq $0x11170, %rbx
+;; movl $0, %esi
+;; cmpq %rcx, %rdx
+;; cmovaq %rsi, %rbx
+;; movl (%rbx), %eax
+;; addq $0x20, %rsp
+;; popq %rbp
+;; retq
+;; 7b: ud2
+;; 7d: ud2
+;; 7f: ud2
diff --git a/tests/disas/winch/x64/load/reservation_static_oob.wat b/tests/disas/winch/x64/load/reservation_static_oob.wat
new file mode 100644
index 0000000000..91c702bcfe
--- /dev/null
+++ b/tests/disas/winch/x64/load/reservation_static_oob.wat
@@ -0,0 +1,46 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = ["-Omemory-reservation=0x20000", "-Omemory-may-move=n"]
+
+;; The memory's minimum size fits within `memory-reservation` and the memory
+;; can't move, so the reservation is an upper bound: an access whose offset
+;; exceeds it is out of bounds and must trap, via the regular bounds check.
+(module
+ (memory 2)
+ (func (export "load_offset") (param i32) (result i32)
+ local.get 0
+ i32.load offset=0x20000))
+;; wasm[0]::function[0]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x18(%r11), %r11
+;; addq $0x20, %r11
+;; cmpq %rsp, %r11
+;; ja 0x7b
+;; 1c: movq %rdi, %r14
+;; subq $0x20, %rsp
+;; movq %rdi, 0x18(%rsp)
+;; movq %rsi, 0x10(%rsp)
+;; movl %edx, 0xc(%rsp)
+;; movl 0xc(%rsp), %eax
+;; movq 0x40(%r14), %rcx
+;; movl %eax, %edx
+;; addq $0x20004, %rdx
+;; jb 0x7d
+;; 4b: cmpq %rcx, %rdx
+;; ja 0x7f
+;; 54: movq 0x38(%r14), %rbx
+;; movl %eax, %eax
+;; addq %rax, %rbx
+;; addq $0x20000, %rbx
+;; movl $0, %esi
+;; cmpq %rcx, %rdx
+;; cmovaq %rsi, %rbx
+;; movl (%rbx), %eax
+;; addq $0x20, %rsp
+;; popq %rbp
+;; retq
+;; 7b: ud2
+;; 7d: ud2
+;; 7f: ud2
diff --git a/winch/codegen/src/codegen/mod.rs b/winch/codegen/src/codegen/mod.rs
index 7b57622225..45942c23b6 100644
--- a/winch/codegen/src/codegen/mod.rs
+++ b/winch/codegen/src/codegen/mod.rs
@@ -1219,8 +1219,6 @@ where
.can_elide_bounds_check(&memory_tunables, self.env.page_size_log2);
let addr = if offset_with_access_size > heap.memory.maximum_byte_size().unwrap_or(u64::MAX)
- || (!self.tunables.memory_may_move
- && offset_with_access_size > self.tunables.memory_reservation)
{
// Detect at compile time if the access is out of bounds.
// Doing so will put the compiler in an unreachable code state,