From: Lang Hames Date: Wed, 1 Feb 2023 01:55:29 +0000 (-0800) Subject: [ORC] Fix an iterator invalidation issue in JITDylib::defineMaterializing. X-Git-Tag: upstream/17.0.6~18981 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=380355cb4c6ba28cb8b9a573591dcce694a047ee;p=platform%2Fupstream%2Fllvm.git [ORC] Fix an iterator invalidation issue in JITDylib::defineMaterializing. The loop body may add and remove entries in the symbol table so we can't hold iterators to the entries. This commit updates the method to use the newly added NonOwningSymbolStringPtr type as keys for removal instead. Side note: This bug has been present since the introduction of the defineMaterializing method, but the method is called rarely and DenseMap resizes are also rare so we didn't see any fallout until a large program was thrown at it. There's no testcase as I haven't been able to reproduce the failure with smaller testcases. --- diff --git a/llvm/lib/ExecutionEngine/Orc/Core.cpp b/llvm/lib/ExecutionEngine/Orc/Core.cpp index 4a9d0d4..f857191 100644 --- a/llvm/lib/ExecutionEngine/Orc/Core.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Core.cpp @@ -689,10 +689,11 @@ void JITDylib::removeGenerator(DefinitionGenerator &G) { Expected JITDylib::defineMaterializing(SymbolFlagsMap SymbolFlags) { + // TODO: Should we bail out early here if MR is defunct? return ES.runSessionLocked([&]() -> Expected { - std::vector AddedSyms; - std::vector RejectedWeakDefs; + std::vector AddedSyms; + std::vector RejectedWeakDefs; for (auto SFItr = SymbolFlags.begin(), SFEnd = SymbolFlags.end(); SFItr != SFEnd; ++SFItr) { @@ -708,27 +709,27 @@ JITDylib::defineMaterializing(SymbolFlagsMap SymbolFlags) { // If this is a strong definition then error out. if (!Flags.isWeak()) { // Remove any symbols already added. - for (auto &SI : AddedSyms) - Symbols.erase(SI); + for (auto &S : AddedSyms) + Symbols.erase(Symbols.find_as(S)); // FIXME: Return all duplicates. return make_error(std::string(*Name)); } // Otherwise just make a note to discard this symbol after the loop. - RejectedWeakDefs.push_back(SFItr); + RejectedWeakDefs.push_back(NonOwningSymbolStringPtr(Name)); continue; } else EntryItr = Symbols.insert(std::make_pair(Name, SymbolTableEntry(Flags))).first; - AddedSyms.push_back(EntryItr); + AddedSyms.push_back(NonOwningSymbolStringPtr(Name)); EntryItr->second.setState(SymbolState::Materializing); } // Remove any rejected weak definitions from the SymbolFlags map. while (!RejectedWeakDefs.empty()) { - SymbolFlags.erase(RejectedWeakDefs.back()); + SymbolFlags.erase(SymbolFlags.find_as(RejectedWeakDefs.back())); RejectedWeakDefs.pop_back(); }