Skip to content

Commit adca3a1

Browse files
authored
Handle extended const segment offsets in the fuzzer (#6382)
The fuzzer already had logic to remove all references to non-imported globals from global initializers and data segment offsets, but it was missing for element segment offsets. Add it, and also add a missing check line for the new test that uncovered this bug as initial fuzzer input.
1 parent 3c779e2 commit adca3a1

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/tools/fuzzing/fuzzing.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,12 @@ void TranslateToFuzzReader::finalizeMemory() {
465465
// definition to what used to be an imported global in initial contents.
466466
// To fix that, replace such invalid offsets with a constant.
467467
for (auto* get : FindAll<GlobalGet>(segment->offset).list) {
468-
// N.B: We never currently encounter imported globals here, but we do
469-
// the check for robustness.
470-
if (!wasm.getGlobal(get->name)->imported()) {
471-
// TODO: It would be better to avoid segment overlap so that
472-
// MemoryPacking can run.
473-
segment->offset =
474-
builder.makeConst(Literal::makeFromInt32(0, Type::i32));
475-
break;
476-
}
468+
// No imported globals should remain.
469+
assert(!wasm.getGlobal(get->name)->imported());
470+
// TODO: It would be better to avoid segment overlap so that
471+
// MemoryPacking can run.
472+
segment->offset =
473+
builder.makeConst(Literal::makeFromInt32(0, Type::i32));
477474
}
478475
}
479476
if (auto* offset = segment->offset->dynCast<Const>()) {
@@ -507,10 +504,13 @@ void TranslateToFuzzReader::finalizeTable() {
507504
for (auto& table : wasm.tables) {
508505
ModuleUtils::iterTableSegments(
509506
wasm, table->name, [&](ElementSegment* segment) {
510-
// If the offset is a global that was imported (which is ok) but no
511-
// longer is (not ok) we need to change that.
512-
if (auto* offset = segment->offset->dynCast<GlobalGet>()) {
513-
if (!wasm.getGlobal(offset->name)->imported()) {
507+
// If the offset contains a global that was imported (which is ok) but
508+
// no longer is (not ok unless GC is enabled), we may need to change
509+
// that.
510+
if (!wasm.features.hasGC()) {
511+
for (auto* get : FindAll<GlobalGet>(segment->offset).list) {
512+
// No imported globals should remain.
513+
assert(!wasm.getGlobal(get->name)->imported());
514514
// TODO: the segments must not overlap...
515515
segment->offset =
516516
builder.makeConst(Literal::makeFromInt32(0, Type::i32));

test/lit/validation/extended-const.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
;; NO-EXTENDED: unexpected false: global init must be constant
77
;; NO-EXTENDED: unexpected false: memory segment offset should be constant
8+
;; NO-EXTENDED: unexpected false: table segment offset should be constant
89

910
;; EXTENDED: (import "env" "global" (global $gimport$0 i32))
1011
;; EXTENDED: (global $1 i32 (i32.add

0 commit comments

Comments
 (0)