[GlobalsAA] Fix a really horrible iterator invalidation bug
authorJames Molloy <james.molloy@arm.com>
Mon, 19 Oct 2015 08:54:59 +0000 (08:54 +0000)
committerJames Molloy <james.molloy@arm.com>
Mon, 19 Oct 2015 08:54:59 +0000 (08:54 +0000)
We were keeping a reference to an object in a DenseMap then mutating it. At the end of the function we were attempting to clone that reference into other keys in the DenseMap, but DenseMap may well decide to resize its hashtable which would invalidate the reference!

It took an extremely complex testcase to catch this - many thanks to Zhendong Su for catching it in PR25225.

This fixes PR25225.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@250692 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/GlobalsModRef.cpp

index be5d384c435885ad0679d8e04edf3c986e608138..979e2de0fcf17178a4d6c5a171ceef2c2f9c81da 100644 (file)
@@ -587,8 +587,11 @@ void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) {
 
     // Finally, now that we know the full effect on this SCC, clone the
     // information to each function in the SCC.
+    // FI is a reference into FunctionInfos, so copy it now so that it doesn't
+    // get invalidated if DenseMap decides to re-hash.
+    FunctionInfo CachedFI = FI;
     for (unsigned i = 1, e = SCC.size(); i != e; ++i)
-      FunctionInfos[SCC[i]->getFunction()] = FI;
+      FunctionInfos[SCC[i]->getFunction()] = CachedFI;
   }
 }