Skip to content

Commit dfaca1e

Browse files
committed
fix
1 parent c3f7e2e commit dfaca1e

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/wasm-binary.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,11 @@ class WasmBinaryReader {
16581658
std::unordered_map<Index, Name> dataNames;
16591659
std::unordered_map<Index, Name> elemNames;
16601660

1661+
// The names that are already used (either from the names section, or that we
1662+
// generate as internal names for un-named things).
1663+
std::unordered_set<Name> usedFunctionNames, usedTableNames, usedMemoryNames,
1664+
usedGlobalNames, usedTagNames;
1665+
16611666
Function* currFunction = nullptr;
16621667
// before we see a function (like global init expressions), there is no end of
16631668
// function to check

src/wasm/wasm-binary.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,17 +2529,15 @@ getOrMakeName(const std::unordered_map<Index, Name>& nameMap,
25292529
void WasmBinaryReader::readMemories() {
25302530
auto num = getU32LEB();
25312531
auto numImports = wasm.memories.size();
2532-
std::unordered_set<Name> usedNames;
25332532
for (auto& [index, name] : memoryNames) {
25342533
if (index >= num + numImports) {
25352534
std::cerr << "warning: memory index out of bounds in name section: "
25362535
<< name << " at index " << index << '\n';
25372536
}
2538-
usedNames.insert(name);
25392537
}
25402538
for (size_t i = 0; i < num; i++) {
25412539
auto [name, isExplicit] =
2542-
getOrMakeName(memoryNames, numImports + i, makeName("", i), usedNames);
2540+
getOrMakeName(memoryNames, numImports + i, makeName("", i), usedMemoryNames);
25432541
auto memory = Builder::makeMemory(name);
25442542
memory->hasExplicitName = isExplicit;
25452543
getResizableLimits(memory->initial,
@@ -2871,8 +2869,6 @@ void WasmBinaryReader::getResizableLimits(Address& initial,
28712869
void WasmBinaryReader::readImports() {
28722870
size_t num = getU32LEB();
28732871
Builder builder(wasm);
2874-
std::unordered_set<Name> usedFunctionNames, usedTableNames, usedMemoryNames,
2875-
usedGlobalNames, usedTagNames;
28762872
for (size_t i = 0; i < num; i++) {
28772873
auto module = getInlineString();
28782874
auto base = getInlineString();
@@ -3007,13 +3003,14 @@ void WasmBinaryReader::setLocalNames(Function& func, Index i) {
30073003
void WasmBinaryReader::readFunctionSignatures() {
30083004
size_t num = getU32LEB();
30093005
auto numImports = wasm.functions.size();
3010-
std::unordered_set<Name> usedNames;
3006+
std::cout << "adding existing\n";
30113007
for (auto& [index, name] : functionNames) {
30123008
if (index >= num + numImports) {
30133009
std::cerr << "warning: function index out of bounds in name section: "
30143010
<< name << " at index " << index << '\n';
30153011
}
3016-
usedNames.insert(name);
3012+
usedFunctionNames.insert(name);
3013+
std::cout << " adding: " << name << "\n";
30173014
}
30183015
// Also check that the function indices in the local names subsection are
30193016
// in-bounds, even though we don't use them here.
@@ -3026,7 +3023,7 @@ void WasmBinaryReader::readFunctionSignatures() {
30263023
}
30273024
for (size_t i = 0; i < num; i++) {
30283025
auto [name, isExplicit] =
3029-
getOrMakeName(functionNames, numImports + i, makeName("", i), usedNames);
3026+
getOrMakeName(functionNames, numImports + i, makeName("", i), usedFunctionNames);
30303027
auto index = getU32LEB();
30313028
HeapType type = getTypeByIndex(index);
30323029
functionTypes.push_back(type);
@@ -4761,17 +4758,15 @@ Name WasmBinaryReader::getIndexedString() {
47614758
void WasmBinaryReader::readGlobals() {
47624759
size_t num = getU32LEB();
47634760
auto numImports = wasm.globals.size();
4764-
std::unordered_set<Name> usedNames;
47654761
for (auto& [index, name] : globalNames) {
47664762
if (index >= num + numImports) {
47674763
std::cerr << "warning: global index out of bounds in name section: "
47684764
<< name << " at index " << index << '\n';
47694765
}
4770-
usedNames.insert(name);
47714766
}
47724767
for (size_t i = 0; i < num; i++) {
47734768
auto [name, isExplicit] = getOrMakeName(
4774-
globalNames, numImports + i, makeName("global$", i), usedNames);
4769+
globalNames, numImports + i, makeName("global$", i), usedGlobalNames);
47754770
auto type = getConcreteType();
47764771
auto mutable_ = getU32LEB();
47774772
if (mutable_ & ~1) {
@@ -4860,17 +4855,15 @@ void WasmBinaryReader::readDataSegments() {
48604855
void WasmBinaryReader::readTableDeclarations() {
48614856
auto num = getU32LEB();
48624857
auto numImports = wasm.tables.size();
4863-
std::unordered_set<Name> usedNames;
48644858
for (auto& [index, name] : tableNames) {
48654859
if (index >= num + numImports) {
48664860
std::cerr << "warning: table index out of bounds in name section: "
48674861
<< name << " at index " << index << '\n';
48684862
}
4869-
usedNames.insert(name);
48704863
}
48714864
for (size_t i = 0; i < num; i++) {
48724865
auto [name, isExplicit] =
4873-
getOrMakeName(tableNames, numImports + i, makeName("", i), usedNames);
4866+
getOrMakeName(tableNames, numImports + i, makeName("", i), usedTableNames);
48744867
auto elemType = getType();
48754868
if (!elemType.isRef()) {
48764869
throwError("Table type must be a reference type");
@@ -4977,18 +4970,16 @@ void WasmBinaryReader::readElementSegments() {
49774970
void WasmBinaryReader::readTags() {
49784971
size_t num = getU32LEB();
49794972
auto numImports = wasm.tags.size();
4980-
std::unordered_set<Name> usedNames;
49814973
for (auto& [index, name] : tagNames) {
49824974
if (index >= num + numImports) {
49834975
std::cerr << "warning: tag index out of bounds in name section: " << name
49844976
<< " at index " << index << '\n';
49854977
}
4986-
usedNames.insert(name);
49874978
}
49884979
for (size_t i = 0; i < num; i++) {
49894980
getInt8(); // Reserved 'attribute' field
49904981
auto [name, isExplicit] =
4991-
getOrMakeName(tagNames, numImports + i, makeName("tag$", i), usedNames);
4982+
getOrMakeName(tagNames, numImports + i, makeName("tag$", i), usedTagNames);
49924983
auto typeIndex = getU32LEB();
49934984
auto tag = Builder::makeTag(name, getSignatureByTypeIndex(typeIndex));
49944985
tag->hasExplicitName = isExplicit;
@@ -5081,6 +5072,7 @@ void WasmBinaryReader::readNames(size_t sectionPos, size_t payloadLen) {
50815072
auto rawName = getInlineString();
50825073
auto name = processor.process(rawName);
50835074
functionNames[index] = name;
5075+
usedFunctionNames.insert(name);
50845076
}
50855077
} else if (nameType == Subsection::NameLocal) {
50865078
auto numFuncs = getU32LEB();
@@ -5112,6 +5104,7 @@ void WasmBinaryReader::readNames(size_t sectionPos, size_t payloadLen) {
51125104
auto rawName = getInlineString();
51135105
auto name = processor.process(rawName);
51145106
tableNames[index] = name;
5107+
usedTableNames.insert(name);
51155108
}
51165109
} else if (nameType == Subsection::NameElem) {
51175110
auto num = getU32LEB();
@@ -5130,6 +5123,7 @@ void WasmBinaryReader::readNames(size_t sectionPos, size_t payloadLen) {
51305123
auto rawName = getInlineString();
51315124
auto name = processor.process(rawName);
51325125
memoryNames[index] = name;
5126+
usedMemoryNames.insert(name);
51335127
}
51345128
} else if (nameType == Subsection::NameData) {
51355129
auto num = getU32LEB();
@@ -5148,6 +5142,7 @@ void WasmBinaryReader::readNames(size_t sectionPos, size_t payloadLen) {
51485142
auto rawName = getInlineString();
51495143
auto name = processor.process(rawName);
51505144
globalNames[index] = name;
5145+
usedGlobalNames.insert(name);
51515146
}
51525147
} else if (nameType == Subsection::NameField) {
51535148
auto numTypes = getU32LEB();
@@ -5170,6 +5165,7 @@ void WasmBinaryReader::readNames(size_t sectionPos, size_t payloadLen) {
51705165
auto rawName = getInlineString();
51715166
auto name = processor.process(rawName);
51725167
tagNames[index] = name;
5168+
usedTagNames.insert(name);
51735169
}
51745170
} else {
51755171
std::cerr << "warning: unknown name subsection with id "

test/lit/name-overlap.wast

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
3+
;; RUN: foreach %s %t wasm-opt -all --instrument-branch-hints --roundtrip -S -o - | filecheck %s
4+
5+
;; Two imports exist here, and instrument-branch-hints will add another. The
6+
;; name "fimport$2" happens to be the name that would be chosen for that new
7+
;; import, leading to a situation that the existing import has a forced name
8+
;; from the names section (it is named here in the wat) while we pick an
9+
;; internal name (not from the name section) that overlaps with it, causing an
10+
;; error if we do not make sure to avoid duplication between import and non-
11+
;; import names.
12+
13+
(module
14+
(import "fuzzing-support" "log-i64" (func $fimport$2 (param i64)))
15+
(import "fuzzing-support" "log-f32" (func $fimport$3 (param f32)))
16+
)

0 commit comments

Comments
 (0)