Skip to content

Commit

Permalink
MemoryPacking: Ignore empty segments (WebAssembly#6243)
Browse files Browse the repository at this point in the history
They might trap. Leave that for RemoveUnusedModuleElements.

Fixes WebAssembly#6230
  • Loading branch information
kripken authored and radekdoulik committed Jul 12, 2024
1 parent 900440c commit b3d67d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/passes/MemoryPacking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ bool MemoryPacking::canSplit(const std::unique_ptr<DataSegment>& segment,
return false;
}

if (segment->data.empty()) {
// Ignore empty segments, leaving them in place. We may not need them, but
// leave that for RemoveUnusedModuleElements to decide (as they may trap
// during startup if out of bounds, which is an effect).
return false;
}

for (auto* referrer : referrers) {
if (auto* curr = referrer->dynCast<MemoryInit>()) {
if (segment->isPassive) {
Expand Down
7 changes: 6 additions & 1 deletion test/lit/passes/memory-packing_all-features.wast
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
;; CHECK: (memory $0 2048 2048)
(memory $0 2048 2048)
(import "env" "memoryBase" (global $memoryBase i32))
(data (i32.const 4066) "") ;; empty
(data (i32.const 4066) "") ;; empty; leave it as is
;; (remove-unused-module-elements handles such
;; things, taking into account possible traps etc.)
)

;; CHECK: (data $0 (i32.const 4066) "")
(module
;; CHECK: (import "env" "memoryBase" (global $memoryBase i32))

Expand Down Expand Up @@ -2319,6 +2322,8 @@
(memory $0 1 1 shared)
(data (i32.const 0) "")
(data "foo")
;; CHECK: (data $0 (i32.const 0) "")

;; CHECK: (data $1 "foo")

;; CHECK: (func $0 (type $0)
Expand Down

0 comments on commit b3d67d9

Please sign in to comment.