Skip to content

Commit b9889bb

Browse files
committed
[WebAssembly] Seal imports section before counting imports
Summary: Before we can assign entries in the function of global index space we need to know the total number of function and global imports respectively. To avoid programmer error this change seals that imports section before assigned function and global index space. Any attempt to add an import after the section is sealed will assert. The lack this such as check caused https://reviews.llvm.org/D61876 to be reverted. I'm also trying to craft a test case the this failure. Subscribers: dschuff, jgravelle-google, aheejin, sunfish, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D62240 llvm-svn: 361470
1 parent 0baaf45 commit b9889bb

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

lld/wasm/SyntheticSections.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void TypeSection::writeBody() {
9292
}
9393

9494
uint32_t ImportSection::numImports() const {
95+
assert(IsSealed);
9596
uint32_t NumImports = ImportedSymbols.size() + GOTSymbols.size();
9697
if (Config->ImportMemory)
9798
++NumImports;
@@ -101,13 +102,15 @@ uint32_t ImportSection::numImports() const {
101102
}
102103

103104
void ImportSection::addGOTEntry(Symbol *Sym) {
105+
assert(!IsSealed);
104106
if (Sym->hasGOTIndex())
105107
return;
106108
Sym->setGOTIndex(NumImportedGlobals++);
107109
GOTSymbols.push_back(Sym);
108110
}
109111

110112
void ImportSection::addImport(Symbol *Sym) {
113+
assert(!IsSealed);
111114
ImportedSymbols.emplace_back(Sym);
112115
if (auto *F = dyn_cast<FunctionSymbol>(Sym))
113116
F->setFunctionIndex(NumImportedFunctions++);
@@ -202,7 +205,7 @@ void FunctionSection::addFunction(InputFunction *Func) {
202205
if (!Func->Live)
203206
return;
204207
uint32_t FunctionIndex =
205-
Out.ImportSec->NumImportedFunctions + InputFunctions.size();
208+
Out.ImportSec->numImportedFunctions() + InputFunctions.size();
206209
InputFunctions.emplace_back(Func);
207210
Func->setFunctionIndex(FunctionIndex);
208211
}
@@ -251,7 +254,7 @@ void GlobalSection::addGlobal(InputGlobal *Global) {
251254
if (!Global->Live)
252255
return;
253256
uint32_t GlobalIndex =
254-
Out.ImportSec->NumImportedGlobals + InputGlobals.size();
257+
Out.ImportSec->numImportedGlobals() + InputGlobals.size();
255258
LLVM_DEBUG(dbgs() << "addGlobal: " << GlobalIndex << "\n");
256259
Global->setGlobalIndex(GlobalIndex);
257260
Out.GlobalSec->InputGlobals.push_back(Global);
@@ -270,7 +273,7 @@ void EventSection::writeBody() {
270273
void EventSection::addEvent(InputEvent *Event) {
271274
if (!Event->Live)
272275
return;
273-
uint32_t EventIndex = Out.ImportSec->NumImportedEvents + InputEvents.size();
276+
uint32_t EventIndex = Out.ImportSec->numImportedEvents() + InputEvents.size();
274277
LLVM_DEBUG(dbgs() << "addEvent: " << EventIndex << "\n");
275278
Event->setEventIndex(EventIndex);
276279
InputEvents.push_back(Event);
@@ -457,7 +460,7 @@ void LinkingSection::addToSymtab(Symbol *Sym) {
457460
}
458461

459462
unsigned NameSection::numNames() const {
460-
unsigned NumNames = Out.ImportSec->NumImportedFunctions;
463+
unsigned NumNames = Out.ImportSec->numImportedFunctions();
461464
for (const InputFunction *F : Out.FunctionSec->InputFunctions)
462465
if (!F->getName().empty() || !F->getDebugName().empty())
463466
++NumNames;

lld/wasm/SyntheticSections.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,28 @@ class ImportSection : public SyntheticSection {
101101
void writeBody() override;
102102
void addImport(Symbol *Sym);
103103
void addGOTEntry(Symbol *Sym);
104+
void seal() { IsSealed = true; }
104105
uint32_t numImports() const;
106+
uint32_t numImportedGlobals() const {
107+
assert(IsSealed);
108+
return NumImportedGlobals;
109+
}
110+
uint32_t numImportedFunctions() const {
111+
assert(IsSealed);
112+
return NumImportedFunctions;
113+
}
114+
uint32_t numImportedEvents() const {
115+
assert(IsSealed);
116+
return NumImportedEvents;
117+
}
105118

106-
unsigned NumImportedGlobals = 0;
107-
unsigned NumImportedFunctions = 0;
108-
unsigned NumImportedEvents = 0;
109119
std::vector<const Symbol *> ImportedSymbols;
110120

111121
protected:
122+
bool IsSealed = false;
123+
unsigned NumImportedGlobals = 0;
124+
unsigned NumImportedFunctions = 0;
125+
unsigned NumImportedEvents = 0;
112126
std::vector<const Symbol *> GOTSymbols;
113127
};
114128

lld/wasm/Writer.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void Writer::calculateExports() {
439439
WasmExport{FunctionTableName, WASM_EXTERNAL_TABLE, 0});
440440

441441
unsigned FakeGlobalIndex =
442-
Out.ImportSec->NumImportedGlobals + Out.GlobalSec->InputGlobals.size();
442+
Out.ImportSec->numImportedGlobals() + Out.GlobalSec->InputGlobals.size();
443443

444444
for (Symbol *Sym : Symtab->getSymbols()) {
445445
if (!Sym->isExported())
@@ -532,7 +532,9 @@ static void scanRelocations() {
532532
}
533533

534534
void Writer::assignIndexes() {
535-
assert(Out.FunctionSec->InputFunctions.empty());
535+
// Seal the import section, since other index spaces such as function and
536+
// global are effected by the number of imports.
537+
Out.ImportSec->seal();
536538

537539
for (InputFunction *Func : Symtab->SyntheticFunctions)
538540
Out.FunctionSec->addFunction(Func);
@@ -543,8 +545,6 @@ void Writer::assignIndexes() {
543545
Out.FunctionSec->addFunction(Func);
544546
}
545547

546-
scanRelocations();
547-
548548
for (InputGlobal *Global : Symtab->SyntheticGlobals)
549549
Out.GlobalSec->addGlobal(Global);
550550

@@ -724,6 +724,8 @@ void Writer::run() {
724724
populateTargetFeatures();
725725
log("-- calculateImports");
726726
calculateImports();
727+
log("-- scanRelocations");
728+
scanRelocations();
727729
log("-- assignIndexes");
728730
assignIndexes();
729731
log("-- calculateInitFunctions");
@@ -750,9 +752,9 @@ void Writer::run() {
750752
log("Defined Functions: " + Twine(Out.FunctionSec->InputFunctions.size()));
751753
log("Defined Globals : " + Twine(Out.GlobalSec->InputGlobals.size()));
752754
log("Defined Events : " + Twine(Out.EventSec->InputEvents.size()));
753-
log("Function Imports : " + Twine(Out.ImportSec->NumImportedFunctions));
754-
log("Global Imports : " + Twine(Out.ImportSec->NumImportedGlobals));
755-
log("Event Imports : " + Twine(Out.ImportSec->NumImportedEvents));
755+
log("Function Imports : " + Twine(Out.ImportSec->numImportedFunctions()));
756+
log("Global Imports : " + Twine(Out.ImportSec->numImportedGlobals()));
757+
log("Event Imports : " + Twine(Out.ImportSec->numImportedEvents()));
756758
for (ObjFile *File : Symtab->ObjectFiles)
757759
File->dumpInfo();
758760
}

0 commit comments

Comments
 (0)