[MCJIT] Fix an iterator invalidation bug in MCJIT::finalizeObject.

The finalizeObject method calls generateCodeForModule on each of the currently
'added' objects, but generateCodeForModule moves objects out of the 'added'
set as it's called. To avoid iterator invalidation issues, the added set is
copied out before any calls to generateCodeForModule.

This should fix http://llvm.org/PR20851 .


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217291 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Lang Hames 2014-09-05 23:38:35 +00:00
parent 469c73bc27
commit 9a833c573d
2 changed files with 10 additions and 5 deletions

View File

@ -225,12 +225,14 @@ void MCJIT::finalizeLoadedModules() {
void MCJIT::finalizeObject() {
MutexGuard locked(lock);
for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
E = OwnedModules.end_added();
I != E; ++I) {
Module *M = *I;
// Generate code for module is going to move objects out of the 'added' list,
// so we need to copy that out before using it:
SmallVector<Module*, 16> ModsToAdd;
for (auto M : OwnedModules.added())
ModsToAdd.push_back(M);
for (auto M : ModsToAdd)
generateCodeForModule(M);
}
finalizeLoadedModules();
}

View File

@ -118,6 +118,9 @@ class MCJIT : public ExecutionEngine {
ModulePtrSet::iterator begin_added() { return AddedModules.begin(); }
ModulePtrSet::iterator end_added() { return AddedModules.end(); }
iterator_range<ModulePtrSet::iterator> added() {
return iterator_range<ModulePtrSet::iterator>(begin_added(), end_added());
}
ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); }
ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); }