Skip to content

Commit a3eb99c

Browse files
committed
Fix i32 sign extension in MemoryPacking pass
Use getUnsigned() to properly zero-extend i32 values when computing memory.init offset and size. Previously, geti32() returned a signed int32_t that sign-extended when stored as uint64_t, causing values >= 0x80000000 to produce incorrect overflow detection and range calculations. Also clean up existing uint32_t() casts in the same function to use getUnsigned() consistently.
1 parent fd5e86e commit a3eb99c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/passes/MemoryPacking.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,15 @@ void MemoryPacking::optimizeSegmentOps(Module* module) {
455455
bool mustTrap = false;
456456
auto* offset = curr->offset->dynCast<Const>();
457457
auto* size = curr->size->dynCast<Const>();
458-
if (offset && uint32_t(offset->value.geti32()) > maxRuntimeSize) {
458+
if (offset && offset->value.getUnsigned() > maxRuntimeSize) {
459459
mustTrap = true;
460460
}
461-
if (size && uint32_t(size->value.geti32()) > maxRuntimeSize) {
461+
if (size && size->value.getUnsigned() > maxRuntimeSize) {
462462
mustTrap = true;
463463
}
464464
if (offset && size) {
465-
uint64_t offsetVal(offset->value.geti32());
466-
uint64_t sizeVal(size->value.geti32());
465+
auto offsetVal = offset->value.getUnsigned();
466+
auto sizeVal = size->value.getUnsigned();
467467
if (offsetVal + sizeVal > maxRuntimeSize) {
468468
mustTrap = true;
469469
} else if (offsetVal == 0 && sizeVal == 0) {
@@ -710,8 +710,8 @@ void MemoryPacking::createReplacements(Module* module,
710710
}
711711

712712
// Nonconstant offsets or sizes will have inhibited splitting
713-
size_t start = init->offset->cast<Const>()->value.geti32();
714-
size_t end = start + init->size->cast<Const>()->value.geti32();
713+
size_t start = init->offset->cast<Const>()->value.getUnsigned();
714+
size_t end = start + init->size->cast<Const>()->value.getUnsigned();
715715

716716
// Index in `segments` of the segment used in emitted memory.init
717717
// instructions

0 commit comments

Comments
 (0)