summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Clegg <sbc@chromium.org>2020-04-21 21:21:23 -0400
committerGitHub <noreply@github.com>2020-04-21 18:21:23 -0700
commit5fbbdbf8601811f57b880fa02861b7f602fe4b83 (patch)
tree30a264aa655d7c7731b2f9ffc4b86d67808862e8
parent086bee68e4f4bef146970f8dc6740356473c8bed (diff)
downloadbinaryen-5fbbdbf8601811f57b880fa02861b7f602fe4b83.tar.gz
binaryen-5fbbdbf8601811f57b880fa02861b7f602fe4b83.tar.bz2
binaryen-5fbbdbf8601811f57b880fa02861b7f602fe4b83.zip
Also update internal name in fixEmJsFuncsAndReturnWalker (#2782)
Without this change only the import gets renamed not the internal name. Since the internal name is the one that ends up in the name section this means that rename wasn't effecting the name section.
-rw-r--r--src/wasm/wasm-emscripten.cpp38
-rw-r--r--test/lld/longjmp.wat.out10
-rw-r--r--test/lld/shared_longjmp.wat.out8
3 files changed, 30 insertions, 26 deletions
diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp
index 5a4bda0a7..2e1aac0a3 100644
--- a/src/wasm/wasm-emscripten.cpp
+++ b/src/wasm/wasm-emscripten.cpp
@@ -1072,8 +1072,9 @@ EmJsWalker fixEmJsFuncsAndReturnWalker(Module& wasm) {
struct FixInvokeFunctionNamesWalker
: public PostWalker<FixInvokeFunctionNamesWalker> {
Module& wasm;
+ std::vector<Name> toRemove;
std::map<Name, Name> importRenames;
- std::map<Name, Name> functionReplace;
+ std::map<Name, Name> functionRenames;
std::set<Signature> invokeSigs;
ImportInfo imports;
@@ -1134,29 +1135,32 @@ struct FixInvokeFunctionNamesWalker
BYN_TRACE("renaming import: " << curr->module << "." << curr->base << " ("
<< curr->name << ") -> " << newname << "\n");
- assert(importRenames.count(curr->base) == 0);
- importRenames[curr->base] = newname;
- // Either rename the import, or replace it with an existing one
- Function* existingFunc = imports.getImportedFunction(curr->module, newname);
- if (existingFunc) {
- BYN_TRACE("replacing with an existing import: " << existingFunc->name
- << "\n");
- functionReplace[curr->name] = existingFunc->name;
+
+ if (auto* f = imports.getImportedFunction(curr->module, newname)) {
+ BYN_TRACE("remove redundant import: " << curr->base << "\n");
+ toRemove.push_back(curr->name);
+ // Make sure the existing import has the correct internal name.
+ if (f->name != newname) {
+ functionRenames[f->name] = newname;
+ }
} else {
- BYN_TRACE("renaming the import in place\n");
+ BYN_TRACE("rename import: " << curr->base << "\n");
curr->base = newname;
}
+
+ functionRenames[curr->name] = newname;
+
+ // Ensure that an imported functions of this name exists.
+ importRenames[curr->base] = newname;
}
void visitModule(Module* curr) {
- // For each replaced function first remove the function itself then
- // rename all uses to the point to the new function.
- for (auto& pair : functionReplace) {
- BYN_TRACE("removeFunction " << pair.first << "\n");
- wasm.removeFunction(pair.first);
+ for (auto name : toRemove) {
+ wasm.removeFunction(name);
}
- // Rename all uses of the removed functions
- ModuleUtils::renameFunctions(wasm, functionReplace);
+
+ // Rename all uses of the old function to the new import name
+ ModuleUtils::renameFunctions(wasm, functionRenames);
// For imports that for renamed, update any associated GOT.func imports.
for (auto& pair : importRenames) {
diff --git a/test/lld/longjmp.wat.out b/test/lld/longjmp.wat.out
index 83c4b5a83..7eaebd592 100644
--- a/test/lld/longjmp.wat.out
+++ b/test/lld/longjmp.wat.out
@@ -11,14 +11,14 @@
(import "env" "malloc" (func $fimport$0 (param i32) (result i32)))
(import "env" "saveSetjmp" (func $fimport$1 (param i32 i32 i32 i32) (result i32)))
(import "env" "getTempRet0" (func $fimport$2 (result i32)))
- (import "env" "invoke_vii" (func $fimport$4 (param i32 i32 i32)))
+ (import "env" "invoke_vii" (func $invoke_vii (param i32 i32 i32)))
(import "env" "testSetjmp" (func $fimport$5 (param i32 i32 i32) (result i32)))
(import "env" "setTempRet0" (func $fimport$6 (param i32)))
(import "env" "free" (func $fimport$7 (param i32)))
- (import "env" "emscripten_longjmp" (func $fimport$8 (param i32 i32)))
+ (import "env" "emscripten_longjmp" (func $emscripten_longjmp (param i32 i32)))
(memory $0 2)
(table $0 2 2 funcref)
- (elem (i32.const 1) $fimport$8)
+ (elem (i32.const 1) $emscripten_longjmp)
(global $global$0 (mut i32) (i32.const 66112))
(global $global$1 i32 (i32.const 576))
(export "memory" (memory $0))
@@ -70,7 +70,7 @@
(i32.const 0)
(i32.const 0)
)
- (call $fimport$4
+ (call $invoke_vii
(i32.const 1)
(local.get $0)
(i32.const 1)
@@ -127,7 +127,7 @@
(i32.const 0)
)
)
- (call $fimport$8
+ (call $emscripten_longjmp
(local.get $0)
(local.get $3)
)
diff --git a/test/lld/shared_longjmp.wat.out b/test/lld/shared_longjmp.wat.out
index 628412ed9..a029d5338 100644
--- a/test/lld/shared_longjmp.wat.out
+++ b/test/lld/shared_longjmp.wat.out
@@ -15,11 +15,11 @@
(import "env" "malloc" (func $fimport$4 (param i32) (result i32)))
(import "env" "saveSetjmp" (func $fimport$5 (param i32 i32 i32 i32) (result i32)))
(import "env" "getTempRet0" (func $fimport$6 (result i32)))
- (import "env" "invoke_vii" (func $fimport$8 (param i32 i32 i32)))
+ (import "env" "invoke_vii" (func $invoke_vii (param i32 i32 i32)))
(import "env" "testSetjmp" (func $fimport$9 (param i32 i32 i32) (result i32)))
(import "env" "setTempRet0" (func $fimport$10 (param i32)))
(import "env" "free" (func $fimport$11 (param i32)))
- (import "env" "emscripten_longjmp" (func $fimport$12 (param i32 i32)))
+ (import "env" "emscripten_longjmp" (func $emscripten_longjmp (param i32 i32)))
(import "env" "g$__THREW__" (func $g$__THREW__ (result i32)))
(import "env" "g$__threwValue" (func $g$__threwValue (result i32)))
(import "env" "fp$emscripten_longjmp$vii" (func $fp$emscripten_longjmp$vii (result i32)))
@@ -78,7 +78,7 @@
)
(i32.const 0)
)
- (call $fimport$8
+ (call $invoke_vii
(global.get $gimport$14)
(local.get $0)
(i32.const 1)
@@ -136,7 +136,7 @@
)
(return)
)
- (call $fimport$12
+ (call $emscripten_longjmp
(local.get $3)
(local.get $0)
)