Skip to content

Commit 6f46640

Browse files
authored
Relax bulk memory rules (#2186)
As decided in the recent in-person CG meeting.
1 parent ec178c9 commit 6f46640

2 files changed

Lines changed: 14 additions & 43 deletions

File tree

src/wasm-interpreter.h

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,11 +1683,6 @@ template<typename GlobalManager, typename SubType> class ModuleInstanceBase {
16831683
Address offsetVal(uint32_t(offset.value.geti32()));
16841684
Address sizeVal(uint32_t(size.value.geti32()));
16851685

1686-
instance.checkLoadAddress(destVal, 0);
1687-
if (offsetVal > segment.data.size()) {
1688-
trap("segment offset out of bounds");
1689-
}
1690-
16911686
for (size_t i = 0; i < sizeVal; ++i) {
16921687
if (offsetVal + i >= segment.data.size()) {
16931688
trap("out of bounds segment access in memory.init");
@@ -1727,20 +1722,16 @@ template<typename GlobalManager, typename SubType> class ModuleInstanceBase {
17271722
Address sourceVal(uint32_t(source.value.geti32()));
17281723
Address sizeVal(uint32_t(size.value.geti32()));
17291724

1730-
instance.checkLoadAddress(destVal, 0);
1731-
instance.checkLoadAddress(sourceVal, 0);
1732-
1733-
size_t start = 0;
1734-
size_t end = sizeVal;
1725+
int64_t start = 0;
1726+
int64_t end = sizeVal;
17351727
int step = 1;
1736-
// Reverse direction if source is below dest and they overlap
1737-
if (sourceVal < destVal &&
1738-
(sourceVal + sizeVal > destVal || sourceVal + sizeVal < sourceVal)) {
1739-
start = sizeVal - 1;
1728+
// Reverse direction if source is below dest
1729+
if (sourceVal < destVal) {
1730+
start = int64_t(sizeVal) - 1;
17401731
end = -1;
17411732
step = -1;
17421733
}
1743-
for (size_t i = start; i != end; i += step) {
1734+
for (int64_t i = start; i != end; i += step) {
17441735
if (i + destVal >= std::numeric_limits<uint32_t>::max()) {
17451736
trap("Out of bounds memory access");
17461737
}
@@ -1771,8 +1762,6 @@ template<typename GlobalManager, typename SubType> class ModuleInstanceBase {
17711762
Address destVal(uint32_t(dest.value.geti32()));
17721763
Address sizeVal(uint32_t(size.value.geti32()));
17731764

1774-
instance.checkLoadAddress(destVal, 0);
1775-
17761765
uint8_t val(value.value.geti32());
17771766
for (size_t i = 0; i < sizeVal; ++i) {
17781767
instance.externalInterface->store8(

test/spec/bulk-memory.wast

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,11 @@
3333
;; Fill all of memory
3434
(invoke "fill" (i32.const 0) (i32.const 0) (i32.const 0x10000))
3535

36-
;; Out-of-bounds writes trap, but all previous writes succeed.
37-
(assert_trap (invoke "fill" (i32.const 0xff00) (i32.const 1) (i32.const 0x101))
38-
"out of bounds memory access")
39-
(assert_return (invoke "load8_u" (i32.const 0xff00)) (i32.const 1))
40-
(assert_return (invoke "load8_u" (i32.const 0xffff)) (i32.const 1))
41-
4236
;; Succeed when writing 0 bytes at the end of the region.
4337
(invoke "fill" (i32.const 0x10000) (i32.const 0) (i32.const 0))
4438

45-
;; Fail on out-of-bounds when writing 0 bytes outside of memory.
46-
(assert_trap (invoke "fill" (i32.const 0x10001) (i32.const 0) (i32.const 0))
47-
"out of bounds memory access")
48-
39+
;; OK to write 0 bytes outside of memory.
40+
(invoke "fill" (i32.const 0x10001) (i32.const 0) (i32.const 0))
4941

5042
;; memory.copy
5143
(module
@@ -105,21 +97,13 @@
10597
(invoke "copy" (i32.const 0xff00) (i32.const 0) (i32.const 0x100))
10698
(invoke "copy" (i32.const 0xfe00) (i32.const 0xff00) (i32.const 0x100))
10799

108-
;; Out-of-bounds writes trap, but all previous writes succeed.
109-
(assert_trap (invoke "copy" (i32.const 0xfffe) (i32.const 0) (i32.const 3))
110-
"out of bounds memory access")
111-
(assert_return (invoke "load8_u" (i32.const 0xfffe)) (i32.const 0xaa))
112-
(assert_return (invoke "load8_u" (i32.const 0xffff)) (i32.const 0xbb))
113-
114100
;; Succeed when copying 0 bytes at the end of the region.
115101
(invoke "copy" (i32.const 0x10000) (i32.const 0) (i32.const 0))
116102
(invoke "copy" (i32.const 0) (i32.const 0x10000) (i32.const 0))
117103

118-
;; Fail on out-of-bounds when copying 0 bytes outside of memory.
119-
(assert_trap (invoke "copy" (i32.const 0x10001) (i32.const 0) (i32.const 0))
120-
"out of bounds memory access")
121-
(assert_trap (invoke "copy" (i32.const 0) (i32.const 0x10001) (i32.const 0))
122-
"out of bounds memory access")
104+
;; OK copying 0 bytes outside of memory.
105+
(invoke "copy" (i32.const 0x10001) (i32.const 0) (i32.const 0))
106+
(invoke "copy" (i32.const 0) (i32.const 0x10001) (i32.const 0))
123107

124108
;; memory.init
125109
(module
@@ -154,11 +138,9 @@
154138
(invoke "init" (i32.const 0x10000) (i32.const 0) (i32.const 0))
155139
(invoke "init" (i32.const 0) (i32.const 4) (i32.const 0))
156140

157-
;; Fail on out-of-bounds when writing 0 bytes outside of memory or segment.
158-
(assert_trap (invoke "init" (i32.const 0x10001) (i32.const 0) (i32.const 0))
159-
"out of bounds memory access")
160-
(assert_trap (invoke "init" (i32.const 0) (i32.const 5) (i32.const 0))
161-
"out of bounds memory access")
141+
;; OK writing 0 bytes outside of memory or segment.
142+
(invoke "init" (i32.const 0x10001) (i32.const 0) (i32.const 0))
143+
(invoke "init" (i32.const 0) (i32.const 5) (i32.const 0))
162144

163145
;; data.drop
164146
(module

0 commit comments

Comments
 (0)