Skip to content

Commit 9ee557a

Browse files
authored
Fix a crash in MemoryPacking due to an unreachable pointer (#5623)
Previously, the pointer type for newly emitted instructions was determined by the type of the destination pointer on a memory.init instruction, but that did not take into account that the destination pointer may be unreachable. Properly look up the pointer type on the memory instead to fix the problem. Fixes #5620.
1 parent d7c24bc commit 9ee557a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/passes/MemoryPacking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ void MemoryPacking::createReplacements(Module* module,
701701

702702
// Calculate dest, either as a const or as an addition to the dest local
703703
Expression* dest;
704-
Type ptrType = init->dest->type;
704+
Type ptrType = module->getMemory(init->memory)->indexType;
705705
if (auto* c = init->dest->dynCast<Const>()) {
706706
dest =
707707
builder.makeConstPtr(c->value.getInteger() + bytesWritten, ptrType);

test/lit/passes/memory-packing_all-features.wast

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,3 +2347,45 @@
23472347
)
23482348
)
23492349
)
2350+
(module
2351+
;; CHECK: (type $none_=>_none (func))
2352+
2353+
;; CHECK: (global $__mem_segment_drop_state (mut i32) (i32.const 0))
2354+
2355+
;; CHECK: (memory $0 1 1)
2356+
(memory $0 i32 1 1)
2357+
(data "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00test")
2358+
;; CHECK: (data $0 "test")
2359+
2360+
;; CHECK: (func $0 (type $none_=>_none)
2361+
;; CHECK-NEXT: (local $0 i32)
2362+
;; CHECK-NEXT: (local.tee $0
2363+
;; CHECK-NEXT: (unreachable)
2364+
;; CHECK-NEXT: )
2365+
;; CHECK-NEXT: (if
2366+
;; CHECK-NEXT: (global.get $__mem_segment_drop_state)
2367+
;; CHECK-NEXT: (unreachable)
2368+
;; CHECK-NEXT: )
2369+
;; CHECK-NEXT: (memory.fill
2370+
;; CHECK-NEXT: (local.get $0)
2371+
;; CHECK-NEXT: (i32.const 0)
2372+
;; CHECK-NEXT: (i32.const 57)
2373+
;; CHECK-NEXT: )
2374+
;; CHECK-NEXT: (memory.init $0
2375+
;; CHECK-NEXT: (i32.add
2376+
;; CHECK-NEXT: (local.get $0)
2377+
;; CHECK-NEXT: (i32.const 57)
2378+
;; CHECK-NEXT: )
2379+
;; CHECK-NEXT: (i32.const 0)
2380+
;; CHECK-NEXT: (i32.const 4)
2381+
;; CHECK-NEXT: )
2382+
;; CHECK-NEXT: )
2383+
(func $0
2384+
;; test that unreachable destinations are ok
2385+
(memory.init 0
2386+
(unreachable)
2387+
(i32.const 0)
2388+
(i32.const 61)
2389+
)
2390+
)
2391+
)

0 commit comments

Comments
 (0)