summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-09-20 10:30:22 -0700
committerGitHub <noreply@github.com>2018-09-20 10:30:22 -0700
commit41ebb1b11041bf43f0e8b7ebacbed132511fcc55 (patch)
tree08410ae0dbb91c4d4fca48fc3ad1d477d0c3163a /src
parentfe88b47749115009da0447e340cbdc86edf30984 (diff)
downloadbinaryen-41ebb1b11041bf43f0e8b7ebacbed132511fcc55.tar.gz
binaryen-41ebb1b11041bf43f0e8b7ebacbed132511fcc55.tar.bz2
binaryen-41ebb1b11041bf43f0e8b7ebacbed132511fcc55.zip
fix an iterator invalidation regression from #1678 (#1684)
Fixes the 3 regressions mentioned in a comment on #1678
Diffstat (limited to 'src')
-rw-r--r--src/wasm/wasm-emscripten.cpp29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp
index 635b5dfc5..6667ac855 100644
--- a/src/wasm/wasm-emscripten.cpp
+++ b/src/wasm/wasm-emscripten.cpp
@@ -377,11 +377,16 @@ struct AsmConstWalker : public PostWalker<AsmConstWalker> {
void visitCall(Call* curr);
+ void process();
+
private:
Literal idLiteralForCode(std::string code);
std::string asmConstSig(std::string baseSig);
Name nameForImportWithSig(std::string sig);
- void addImport(Name importName, std::string baseSig);
+ void queueImport(Name importName, std::string baseSig);
+ void addImports();
+
+ std::vector<std::unique_ptr<Function>> queuedImports;
};
void AsmConstWalker::visitCall(Call* curr) {
@@ -398,11 +403,19 @@ void AsmConstWalker::visitCall(Call* curr) {
if (allSigs.count(sig) == 0) {
allSigs.insert(sig);
- addImport(importName, baseSig);
+ queueImport(importName, baseSig);
}
}
}
+void AsmConstWalker::process() {
+ // Find and queue necessary imports
+ walkModule(&wasm);
+ // Add them after the walk, to avoid iterator invalidation on
+ // the list of functions.
+ addImports();
+}
+
Literal AsmConstWalker::idLiteralForCode(std::string code) {
int32_t id;
if (ids.count(code) == 0) {
@@ -430,12 +443,18 @@ Name AsmConstWalker::nameForImportWithSig(std::string sig) {
return Name(fixedTarget.c_str());
}
-void AsmConstWalker::addImport(Name importName, std::string baseSig) {
+void AsmConstWalker::queueImport(Name importName, std::string baseSig) {
auto import = new Function;
import->name = import->base = importName;
import->module = ENV;
import->type = ensureFunctionType(baseSig, &wasm)->name;
- wasm.addFunction(import);
+ queuedImports.push_back(std::unique_ptr<Function>(import));
+}
+
+void AsmConstWalker::addImports() {
+ for (auto& import : queuedImports) {
+ wasm.addFunction(import.release());
+ }
}
AsmConstWalker fixEmAsmConstsAndReturnWalker(Module& wasm) {
@@ -450,7 +469,7 @@ AsmConstWalker fixEmAsmConstsAndReturnWalker(Module& wasm) {
// Walk the module, generate _sig versions of EM_ASM functions
AsmConstWalker walker(wasm);
- walker.walkModule(&wasm);
+ walker.process();
// Remove the base functions that we didn't generate
for (auto importName : toRemove) {