[llvm-branch-commits] [llvm-branch] r292312 - Merging r292133:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jan 17 16:21:36 PST 2017


Author: hans
Date: Tue Jan 17 18:21:36 2017
New Revision: 292312

URL: http://llvm.org/viewvc/llvm-project?rev=292312&view=rev
Log:
Merging r292133:
------------------------------------------------------------------------
r292133 | hfinkel | 2017-01-16 07:22:01 -0800 (Mon, 16 Jan 2017) | 10 lines

Fix use-after-free bug in AffectedValueCallbackVH::allUsesReplacedWith

When transferring affected values in the cache from an old value, identified by
the value of the current callback, to the specified new value we might need to
insert a new entry into the DenseMap which constitutes the cache. Doing so
might delete the current callback object. Move the copying logic into a new
function, a member of the assumption cache itself, so that we don't run into UB
should the callback handle itself be removed mid-copy.

Differential Revision: https://reviews.llvm.org/D28749
------------------------------------------------------------------------

Modified:
    llvm/branches/release_40/   (props changed)
    llvm/branches/release_40/include/llvm/Analysis/AssumptionCache.h
    llvm/branches/release_40/lib/Analysis/AssumptionCache.cpp

Propchange: llvm/branches/release_40/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Jan 17 18:21:36 2017
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,291863,291875,291968,291979,292242,292255
+/llvm/trunk:155241,291863,291875,291968,291979,292133,292242,292255

Modified: llvm/branches/release_40/include/llvm/Analysis/AssumptionCache.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_40/include/llvm/Analysis/AssumptionCache.h?rev=292312&r1=292311&r2=292312&view=diff
==============================================================================
--- llvm/branches/release_40/include/llvm/Analysis/AssumptionCache.h (original)
+++ llvm/branches/release_40/include/llvm/Analysis/AssumptionCache.h Tue Jan 17 18:21:36 2017
@@ -68,7 +68,10 @@ class AssumptionCache {
   AffectedValuesMap AffectedValues;
 
   /// Get the vector of assumptions which affect a value from the cache.
-  SmallVector<WeakVH, 1> &getAffectedValues(Value *V);
+  SmallVector<WeakVH, 1> &getOrInsertAffectedValues(Value *V);
+
+  /// Copy affected values in the cache for OV to be affected values for NV.
+  void copyAffectedValuesInCache(Value *OV, Value *NV);
 
   /// \brief Flag tracking whether we have scanned the function yet.
   ///

Modified: llvm/branches/release_40/lib/Analysis/AssumptionCache.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_40/lib/Analysis/AssumptionCache.cpp?rev=292312&r1=292311&r2=292312&view=diff
==============================================================================
--- llvm/branches/release_40/lib/Analysis/AssumptionCache.cpp (original)
+++ llvm/branches/release_40/lib/Analysis/AssumptionCache.cpp Tue Jan 17 18:21:36 2017
@@ -24,7 +24,7 @@
 using namespace llvm;
 using namespace llvm::PatternMatch;
 
-SmallVector<WeakVH, 1> &AssumptionCache::getAffectedValues(Value *V) {
+SmallVector<WeakVH, 1> &AssumptionCache::getOrInsertAffectedValues(Value *V) {
   // Try using find_as first to avoid creating extra value handles just for the
   // purpose of doing the lookup.
   auto AVI = AffectedValues.find_as(V);
@@ -98,7 +98,7 @@ void AssumptionCache::updateAffectedValu
   }
 
   for (auto &AV : Affected) {
-    auto &AVV = getAffectedValues(AV);
+    auto &AVV = getOrInsertAffectedValues(AV);
     if (std::find(AVV.begin(), AVV.end(), CI) == AVV.end())
       AVV.push_back(CI);
   }
@@ -111,20 +111,27 @@ void AssumptionCache::AffectedValueCallb
   // 'this' now dangles!
 }
 
+void AssumptionCache::copyAffectedValuesInCache(Value *OV, Value *NV) {
+  auto &NAVV = getOrInsertAffectedValues(NV);
+  auto AVI = AffectedValues.find(OV);
+  if (AVI == AffectedValues.end())
+    return;
+
+  for (auto &A : AVI->second)
+    if (std::find(NAVV.begin(), NAVV.end(), A) == NAVV.end())
+      NAVV.push_back(A);
+}
+
 void AssumptionCache::AffectedValueCallbackVH::allUsesReplacedWith(Value *NV) {
   if (!isa<Instruction>(NV) && !isa<Argument>(NV))
     return;
 
   // Any assumptions that affected this value now affect the new value.
 
-  auto &NAVV = AC->getAffectedValues(NV);
-  auto AVI = AC->AffectedValues.find(getValPtr());
-  if (AVI == AC->AffectedValues.end())
-    return;
-
-  for (auto &A : AVI->second)
-    if (std::find(NAVV.begin(), NAVV.end(), A) == NAVV.end())
-      NAVV.push_back(A);
+  AC->copyAffectedValuesInCache(getValPtr(), NV);
+  // 'this' now might dangle! If the AffectedValues map was resized to add an
+  // entry for NV then this object might have been destroyed in favor of some
+  // copy in the grown map.
 }
 
 void AssumptionCache::scanFunction() {




More information about the llvm-branch-commits mailing list