summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2024-05-17 16:05:58 -0700
committerGitHub <noreply@github.com>2024-05-17 23:05:58 +0000
commit4e9f4953a2e5968c402eb2e950252294cfa02460 (patch)
tree8bb2abf0542fb1041ded4588ed218cfc74a769fe
parentf4a83dd7370021caea7d3b4f97031e206a3ac223 (diff)
downloadbinaryen-4e9f4953a2e5968c402eb2e950252294cfa02460.tar.gz
binaryen-4e9f4953a2e5968c402eb2e950252294cfa02460.tar.bz2
binaryen-4e9f4953a2e5968c402eb2e950252294cfa02460.zip
Fix GlobalRefining's handling of gets in module code and add missing validation (#6603)
GlobalRefining did not traverse module code, so it did not update global.gets in other globals. Add missing validation that actually errors on that: We did not check global.get types. These could be separate PRs but it would be difficult to test them separately.
-rw-r--r--src/passes/GlobalRefining.cpp5
-rw-r--r--src/wasm/wasm-validator.cpp8
-rw-r--r--test/example/c-api-kitchen-sink.c2
-rw-r--r--test/lit/passes/global-refining.wast28
4 files changed, 37 insertions, 6 deletions
diff --git a/src/passes/GlobalRefining.cpp b/src/passes/GlobalRefining.cpp
index 129f34283..1313421d5 100644
--- a/src/passes/GlobalRefining.cpp
+++ b/src/passes/GlobalRefining.cpp
@@ -143,8 +143,9 @@ struct GlobalRefining : public Pass {
ReFinalize().walkFunctionInModule(curr, &wasm);
}
}
- };
- GetUpdater(*this, *module).run(getPassRunner(), module);
+ } updater(*this, *module);
+ updater.run(getPassRunner(), module);
+ updater.runOnModuleCode(getPassRunner(), module);
}
};
diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp
index 74e9b6e79..e8a4033ef 100644
--- a/src/wasm/wasm-validator.cpp
+++ b/src/wasm/wasm-validator.cpp
@@ -1004,9 +1004,11 @@ void FunctionValidator::visitGlobalGet(GlobalGet* curr) {
if (!info.validateGlobally) {
return;
}
- shouldBeTrue(getModule()->getGlobalOrNull(curr->name),
- curr,
- "global.get name must be valid");
+ auto* global = getModule()->getGlobalOrNull(curr->name);
+ if (shouldBeTrue(global, curr, "global.get name must be valid")) {
+ shouldBeEqual(
+ curr->type, global->type, curr, "global.get must have right type");
+ }
}
void FunctionValidator::visitGlobalSet(GlobalSet* curr) {
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c
index 5fb71b4d6..efd343be9 100644
--- a/test/example/c-api-kitchen-sink.c
+++ b/test/example/c-api-kitchen-sink.c
@@ -1176,7 +1176,7 @@ void test_core() {
makeInt32(module, 0)),
BinaryenStringNew(module,
BinaryenStringNewWTF16Array(),
- BinaryenGlobalGet(module, "i16Array-global", i8Array),
+ BinaryenGlobalGet(module, "i16Array-global", i16Array),
makeInt32(module, 0),
makeInt32(module, 0)),
BinaryenStringNew(
diff --git a/test/lit/passes/global-refining.wast b/test/lit/passes/global-refining.wast
index 8f0b3c247..81459affe 100644
--- a/test/lit/passes/global-refining.wast
+++ b/test/lit/passes/global-refining.wast
@@ -193,3 +193,31 @@
(nop)
)
)
+
+;; We can refine $a, after which we should update the global.get in the other
+;; global, or else we'd error on validation.
+;; TODO: we could optimize further here and refine the type of the global $b.
+(module
+ ;; CHECK: (type $super (sub (func)))
+ ;; CLOSD: (type $super (sub (func)))
+ (type $super (sub (func)))
+ ;; CHECK: (type $sub (sub $super (func)))
+ ;; CLOSD: (type $sub (sub $super (func)))
+ (type $sub (sub $super (func)))
+
+ ;; CHECK: (global $a (ref $sub) (ref.func $func))
+ ;; CLOSD: (global $a (ref $sub) (ref.func $func))
+ (global $a (ref $super) (ref.func $func))
+ ;; CHECK: (global $b (ref $super) (global.get $a))
+ ;; CLOSD: (global $b (ref $super) (global.get $a))
+ (global $b (ref $super) (global.get $a))
+
+ ;; CHECK: (func $func (type $sub)
+ ;; CHECK-NEXT: (nop)
+ ;; CHECK-NEXT: )
+ ;; CLOSD: (func $func (type $sub)
+ ;; CLOSD-NEXT: (nop)
+ ;; CLOSD-NEXT: )
+ (func $func (type $sub)
+ )
+)