Skip to content

Commit 0048f5b

Browse files
authored
Fix trapping and dangling insts in memory packing (#2540)
This does two things: - Restore `visitDataDrop` handler deleted in #2529, but now we convert invalid `data.drop`s to not `unreachable` but `nop`. This conforms to the revised spec that `data.drop` on the active segment can be treated as a nop. - Make `visitMemoryInit` trap if offset or size are not equal to 0 or if the dest address is out of bounds. Otherwise drop all its argument. Fixes #2535.
1 parent 8b15cee commit 0048f5b

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

src/passes/MemoryPacking.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,20 @@ struct MemoryPacking : public Pass {
135135
void visitMemoryInit(MemoryInit* curr) {
136136
if (!getModule()->memory.segments[curr->segment].isPassive) {
137137
Builder builder(*getModule());
138-
replaceCurrent(builder.blockify(builder.makeDrop(curr->dest),
139-
builder.makeDrop(curr->offset),
140-
builder.makeDrop(curr->size),
141-
builder.makeUnreachable()));
138+
// trap if (dest > memory.size | offset | size) != 0
139+
replaceCurrent(builder.makeIf(
140+
builder.makeBinary(
141+
OrInt32,
142+
builder.makeBinary(
143+
GtUInt32, curr->dest, builder.makeHost(MemorySize, Name(), {})),
144+
builder.makeBinary(OrInt32, curr->offset, curr->size)),
145+
builder.makeUnreachable()));
146+
changed = true;
147+
}
148+
}
149+
void visitDataDrop(DataDrop* curr) {
150+
if (!getModule()->memory.segments[curr->segment].isPassive) {
151+
ExpressionManipulator::nop(curr);
142152
changed = true;
143153
}
144154
}

test/passes/memory-packing_all-features.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,26 @@
2222
(type $none_=>_none (func))
2323
(memory $0 1 1)
2424
(func $foo (; 0 ;)
25-
(drop
26-
(i32.const 0)
27-
)
28-
(drop
29-
(i32.const 0)
25+
(if
26+
(i32.or
27+
(i32.gt_u
28+
(i32.const 0)
29+
(memory.size)
30+
)
31+
(i32.or
32+
(i32.const 0)
33+
(i32.const 0)
34+
)
35+
)
36+
(unreachable)
3037
)
38+
)
39+
(func $bar (; 1 ;)
3140
(drop
32-
(i32.const 0)
41+
(loop $loop-in (result i32)
42+
(nop)
43+
(i32.const 42)
44+
)
3345
)
34-
(unreachable)
3546
)
3647
)

test/passes/memory-packing_all-features.wast

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@
2727
(i32.const 0)
2828
)
2929
)
30+
(func $bar
31+
(drop
32+
(loop (result i32)
33+
(data.drop 0)
34+
(i32.const 42)
35+
)
36+
)
37+
)
3038
)

0 commit comments

Comments
 (0)