Skip to content

Commit aef8534

Browse files
authored
Fix validation error in wasm-reduce (#8715)
When removing functions, wasm-reduce replaces calls to those functions using `builder.replaceWithIdenticalType`. That method can modify the input expression in-place to have a different types (e.g. replacing a reference with a null), so when the previous code read `curr->type` to set the type of the replacement block, it was possible to get a more refined type, leading to validation failures. Fix the bug by explicitly using the original type. Fixes #8713.
1 parent d3029d2 commit aef8534

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/tools/wasm-reduce/wasm-reduce.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ struct Reducer
11021102
auto* block =
11031103
ChildLocalizer(curr, getFunction(), *getModule(), getPassOptions())
11041104
.getChildrenReplacement();
1105+
auto originalType = curr->type;
11051106
auto* replacement = builder.replaceWithIdenticalType(curr);
11061107
// We may have failed to come up with a replacement (e.g. for
11071108
// non-nullable references), so manually add an `unreachable` in that
@@ -1110,7 +1111,7 @@ struct Reducer
11101111
replacement = builder.makeUnreachable();
11111112
}
11121113
block->list.push_back(replacement);
1113-
block->type = curr->type;
1114+
block->type = originalType;
11141115
replaceCurrent(block);
11151116
}
11161117
void visitRefFunc(RefFunc* curr) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
;; This is a regression test for a crash in wasm-reduce where in-place mutation
2+
;; of a Call node during replaceWithIdenticalType caused the replacement block
3+
;; type to be set incorrectly to nullref instead of the original type, leading
4+
;; to a validation error.
5+
6+
;; TODO: Why does this fail on CI without --force?
7+
;; RUN: wasm-reduce %s -t %t.t.wast -w %t.w.wast --force \
8+
;; RUN: --command='wasm-opt %t.t.wast -all --fuzz-exec'
9+
10+
(module
11+
(func $to_remove (result anyref)
12+
(ref.null none)
13+
)
14+
15+
(func $main (export "main") (result anyref)
16+
;; This will be replaced with a nullref. This should not cause validation
17+
;; failures and cause wasm-reduce to crash.
18+
(call $to_remove)
19+
)
20+
)

0 commit comments

Comments
 (0)