[PATCH] D28749: Fix use-after-free bug in AffectedValueCallbackVH::allUsesReplacedWith

Hal Finkel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 16 07:33:11 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL292133: Fix use-after-free bug in AffectedValueCallbackVH::allUsesReplacedWith (authored by hfinkel).

Changed prior to commit:
  https://reviews.llvm.org/D28749?vs=84505&id=84562#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D28749

Files:
  llvm/trunk/include/llvm/Analysis/AssumptionCache.h
  llvm/trunk/lib/Analysis/AssumptionCache.cpp


Index: llvm/trunk/include/llvm/Analysis/AssumptionCache.h
===================================================================
--- llvm/trunk/include/llvm/Analysis/AssumptionCache.h
+++ llvm/trunk/include/llvm/Analysis/AssumptionCache.h
@@ -68,7 +68,10 @@
   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.
   ///
Index: llvm/trunk/lib/Analysis/AssumptionCache.cpp
===================================================================
--- llvm/trunk/lib/Analysis/AssumptionCache.cpp
+++ llvm/trunk/lib/Analysis/AssumptionCache.cpp
@@ -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 @@
   }
 
   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 @@
   // '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() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28749.84562.patch
Type: text/x-patch
Size: 2708 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170116/58d914e1/attachment.bin>


More information about the llvm-commits mailing list