Skip to content

Commit 1308d0a

Browse files
authored
Fix off-by-one bounds check for atomic operations (#11977)
They're allowed to happen if the final address is the memory's length, as opposed to being one-less-than-the-memory's length. Closes #11975
1 parent 1ec8660 commit 1308d0a

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

crates/test-util/src/wast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ impl WastTest {
411411
"misc_testsuite/memory64/more-than-4gb.wast",
412412
// shared memories + pooling allocator aren't supported yet
413413
"misc_testsuite/memory-combos.wast",
414+
"misc_testsuite/threads/atomics-end-of-memory.wast",
414415
"misc_testsuite/threads/LB.wast",
415416
"misc_testsuite/threads/LB_atomic.wast",
416417
"misc_testsuite/threads/MP.wast",

crates/wasmtime/src/runtime/vm/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ pub fn validate_atomic_addr(
747747
}
748748

749749
let length = u64::try_from(def.current_length()).unwrap();
750-
if !(addr.saturating_add(access_size) < length) {
750+
if !(addr.saturating_add(access_size) <= length) {
751751
return Err(Trap::MemoryOutOfBounds);
752752
}
753753

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
;;! threads = true
2+
3+
(module
4+
(memory (export "mem") 1 1 shared)
5+
(func (export "notify_last") (result i32)
6+
(memory.atomic.notify (i32.const 65532) (i32.const 0))
7+
)
8+
(func (export "wait_last32") (result i32)
9+
(memory.atomic.wait32 (i32.const 65532) (i32.const 0) (i64.const 0))
10+
)
11+
(func (export "wait_last64") (result i32)
12+
(memory.atomic.wait64 (i32.const 65528) (i64.const 0) (i64.const 0))
13+
)
14+
)
15+
16+
(assert_return (invoke "notify_last") (i32.const 0))
17+
(assert_return (invoke "wait_last32") (i32.const 2))
18+
(assert_return (invoke "wait_last64") (i32.const 2))

0 commit comments

Comments
 (0)