Skip to content

Commit 92ad959

Browse files
authored
Fix a text parser assertion on bad immediates (WebAssembly#7252)
We previously asserted that the end of a nested expression is the same when we parse it with a null context just to find its children and when we parse it for real. It turns out that it is possible for the two end positions to be different when the instruction is invalid in a way that only the real parse catches. Return a normal error instead of asserting because it is possible for invalid input to trigger this condition. Fixes WebAssembly#7251.
1 parent 9498d5e commit 92ad959

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/parser/parsers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,13 @@ template<typename Ctx> MaybeResult<> foldedinstr(Ctx& ctx) {
10051005
auto inst = plaininstr(ctx, std::move(info.annotations));
10061006
assert(inst && "unexpectedly failed to parse instruction");
10071007
CHECK_ERR(inst);
1008+
// We have already parsed the instruction, so we generally know where it
1009+
// ends. But there may have been some invalid extra immediates (e.g.
1010+
// invalid memory indices) that we only realize are invalid now that we've
1011+
// parsed the instruction for real.
1012+
if (ctx.in.getPos() != *info.end) {
1013+
return ctx.in.err("expected end of instruction");
1014+
}
10081015
assert(ctx.in.getPos() == *info.end && "expected end of instruction");
10091016
continue;
10101017
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;; Regression test for a parser bug where the invalid memory index followed by
2+
;; another immediate caused an assertion failure.
3+
4+
;; RUN: not wasm-opt -all %s 2>&1 | filecheck %s
5+
6+
;; CHECK: Fatal: 12:22: error: expected end of instruction
7+
8+
(module
9+
(memory 1 1)
10+
11+
(func $v128.load16_lane1 (param $0 i32) (param $1 v128) (result v128)
12+
(v128.load16_lane 1 0 ;; invalid memory index
13+
(local.get $0)
14+
(local.get $1)
15+
)
16+
)
17+
)

0 commit comments

Comments
 (0)