diff options
-rw-r--r-- | src/ir/module-splitting.cpp | 19 | ||||
-rw-r--r-- | test/lit/wasm-split/name-collision.wast | 16 |
2 files changed, 32 insertions, 3 deletions
diff --git a/src/ir/module-splitting.cpp b/src/ir/module-splitting.cpp index aad43fc75..f4f00c9af 100644 --- a/src/ir/module-splitting.cpp +++ b/src/ir/module-splitting.cpp @@ -80,6 +80,19 @@ #include "wasm-builder.h" #include "wasm.h" +namespace std { + +// Used in ModuleSplitter::shareImportableItems +template<> struct hash<pair<wasm::ExternalKind, wasm::Name>> { + size_t operator()(const pair<wasm::ExternalKind, wasm::Name>& p) const { + auto digest = wasm::hash(p.first); + wasm::rehash(digest, p.second); + return digest; + } +}; + +} // namespace std + namespace wasm { namespace ModuleSplitting { @@ -496,10 +509,10 @@ void ModuleSplitter::shareImportableItems() { // Map internal names to (one of) their corresponding export names. Don't // consider functions because they have already been imported and exported as // necessary. - std::unordered_map<Name, Name> exports; + std::unordered_map<std::pair<ExternalKind, Name>, Name> exports; for (auto& ex : primary.exports) { if (ex->kind != ExternalKind::Function) { - exports[ex->value] = ex->name; + exports[std::make_pair(ex->kind, ex->value)] = ex->name; } } @@ -510,7 +523,7 @@ void ModuleSplitter::shareImportableItems() { secondaryItem.name = primaryItem.name; secondaryItem.hasExplicitName = primaryItem.hasExplicitName; secondaryItem.module = config.importNamespace; - auto exportIt = exports.find(primaryItem.name); + auto exportIt = exports.find(std::make_pair(kind, primaryItem.name)); if (exportIt != exports.end()) { secondaryItem.base = exportIt->second; } else { diff --git a/test/lit/wasm-split/name-collision.wast b/test/lit/wasm-split/name-collision.wast new file mode 100644 index 000000000..ccb615906 --- /dev/null +++ b/test/lit/wasm-split/name-collision.wast @@ -0,0 +1,16 @@ +;; Regression test for a bug in which colliding internal names between +;; non-function exports would result in the wrong import names being used in the +;; secondary module. + +;; RUN: wasm-split %s -o1 %t.1.wasm -o2 %t.2.wasm +;; RUN: wasm-dis %t.2.wasm | filecheck %s + +;; CHECK-NOT: (import "primary" "memory" (table +;; CHECK: (import "primary" "table" (table + +(module + (table $collide 1 funcref) + (memory $collide 1 1) + (export "table" (table $collide)) + (export "memory" (memory $collide)) +) |