From 83114c51d5aedcb540d578790dbf3173d1775d5c Mon Sep 17 00:00:00 2001 From: Thomas Lively <7121787+tlively@users.noreply.github.com> Date: Wed, 16 Dec 2020 16:40:15 -0500 Subject: Fix wasm-split name collision bug (#3447) During module splitting, a map is constructed from internal names to their corresponding export names. This code previously did not take into account the fact that the same internal name may be used by different kinds of entities (e.g. a table and a memory may have the same internal name), which resulted in the secondary module incorrectly using the same import name for all of the entities that shared an internal name. This PR fixes the problem by including the ExternalKind of the entity in the keys of the export map. --- src/ir/module-splitting.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/ir/module-splitting.cpp') 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> { + size_t operator()(const pair& 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 exports; + std::unordered_map, 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 { -- cgit v1.2.3