Skip to content

Commit bbcb50d

Browse files
authored
Validation fixes for #1317 (#1347)
* add get_global/set_global validation * validate get_local index * update builds * fix tests
1 parent 5aee947 commit bbcb50d

File tree

9 files changed

+230
-182
lines changed

9 files changed

+230
-182
lines changed

bin/binaryen.js

Lines changed: 86 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/wasm.js

Lines changed: 90 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wasm/wasm-validator.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
228228
void visitCallIndirect(CallIndirect *curr);
229229
void visitGetLocal(GetLocal* curr);
230230
void visitSetLocal(SetLocal *curr);
231+
void visitGetGlobal(GetGlobal* curr);
232+
void visitSetGlobal(SetGlobal *curr);
231233
void visitLoad(Load *curr);
232234
void visitStore(Store *curr);
233235
void visitAtomicRMW(AtomicRMW *curr);
@@ -470,6 +472,7 @@ void FunctionValidator::visitCallIndirect(CallIndirect *curr) {
470472
}
471473

472474
void FunctionValidator::visitGetLocal(GetLocal* curr) {
475+
shouldBeTrue(curr->index < getFunction()->getNumLocals(), curr, "get_local index must be small enough");
473476
shouldBeTrue(isConcreteWasmType(curr->type), curr, "get_local must have a valid type - check what you provided when you constructed the node");
474477
}
475478

@@ -483,6 +486,19 @@ void FunctionValidator::visitSetLocal(SetLocal *curr) {
483486
}
484487
}
485488

489+
void FunctionValidator::visitGetGlobal(GetGlobal* curr) {
490+
if (!info.validateGlobally) return;
491+
shouldBeTrue(getModule()->getGlobalOrNull(curr->name) || getModule()->getImportOrNull(curr->name), curr, "get_global name must be valid");
492+
}
493+
494+
void FunctionValidator::visitSetGlobal(SetGlobal *curr) {
495+
if (!info.validateGlobally) return;
496+
auto* global = getModule()->getGlobalOrNull(curr->name);
497+
shouldBeTrue(global, curr, "set_global name must be valid (and not an import; imports can't be modified)");
498+
shouldBeTrue(global->mutable_, curr, "set_global global must be mutable");
499+
shouldBeEqualOrFirstIsUnreachable(curr->value->type, global->type, curr, "set_global value must have right type");
500+
}
501+
486502
void FunctionValidator::visitLoad(Load *curr) {
487503
if (curr->isAtomic) shouldBeTrue(info.features & Feature::Atomics, curr, "Atomic operation (atomics are disabled)");
488504
shouldBeFalse(curr->isAtomic && !getModule()->memory.shared, curr, "Atomic operation with non-shared memory");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(function() {
2+
var mod = new Binaryen.Module();
3+
var funcType = mod.addFunctionType("v", Binaryen.void, []);
4+
var func = mod.addFunction("test", funcType, [],
5+
mod.block("", [
6+
mod.drop(
7+
mod.getGlobal("missing", Binaryen.i32)
8+
)
9+
])
10+
);
11+
mod.addExport("test", func);
12+
console.log(mod.validate())
13+
})();
14+
15+
(function() {
16+
var mod = new Binaryen.Module();
17+
var funcType = mod.addFunctionType("v", Binaryen.void, []);
18+
var func = mod.addFunction("test", funcType, [],
19+
mod.block("", [
20+
mod.drop(
21+
mod.getLocal(0, Binaryen.i32)
22+
)
23+
])
24+
);
25+
mod.addFunctionExport("test", "test", func);
26+
console.log(mod.validate())
27+
})();
28+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[wasm-validator error in function $test] unexpected false: get_global name must be valid, on
2+
[i32] (get_global $missing)
3+
0
4+
[wasm-validator error in function $test] unexpected false: get_local index must be small enough, on
5+
[i32] (get_local $0)
6+
0

test/passes/dce.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@
461461
)
462462
(func $replace-block-changes-later-when-if-goes (; 26 ;) (type $1)
463463
(block $top
464-
(set_global $global$0
464+
(set_global $x
465465
(i32.const 0)
466466
)
467467
(block $inner

test/passes/dce.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@
676676
)
677677
(func $replace-block-changes-later-when-if-goes
678678
(block $top ;; and so should this
679-
(set_global $global$0
679+
(set_global $x
680680
(i32.const 0)
681681
)
682682
(drop

test/passes/precompute.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(type $1 (func (result i32)))
44
(type $2 (func))
55
(type $3 (func (result f64)))
6+
(global $global$0 (mut i32) (i32.const 0))
67
(memory $0 0)
78
(func $x (; 0 ;) (type $0) (param $x i32)
89
(call $x

test/passes/precompute.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(module
22
(memory 0)
33
(type $0 (func (param i32)))
4+
(global $global$0 (mut i32) (i32.const 0))
45
(func $x (type $0) (param $x i32)
56
(call $x
67
(i32.add

0 commit comments

Comments
 (0)