From 41ebb1b11041bf43f0e8b7ebacbed132511fcc55 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 20 Sep 2018 10:30:22 -0700 Subject: fix an iterator invalidation regression from #1678 (#1684) Fixes the 3 regressions mentioned in a comment on #1678 --- src/wasm/wasm-emscripten.cpp | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/wasm/wasm-emscripten.cpp') 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 { 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> 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(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) { -- cgit v1.2.3