diff options
author | Hal Finkel <hfinkel@anl.gov> | 2017-01-16 15:22:01 +0000 |
---|---|---|
committer | Hal Finkel <hfinkel@anl.gov> | 2017-01-16 15:22:01 +0000 |
commit | c29d5f1674469a887c0763362fc9f965a0ea1740 (patch) | |
tree | ff117afcd74a99ac54b9bcb56750708d32700856 /llvm/lib/Analysis/AssumptionCache.cpp | |
parent | ab8194def0e39797f9b1d47cf2d82ececa4bf508 (diff) | |
download | bcm5719-llvm-c29d5f1674469a887c0763362fc9f965a0ea1740.tar.gz bcm5719-llvm-c29d5f1674469a887c0763362fc9f965a0ea1740.zip |
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
llvm-svn: 292133
Diffstat (limited to 'llvm/lib/Analysis/AssumptionCache.cpp')
-rw-r--r-- | llvm/lib/Analysis/AssumptionCache.cpp | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/llvm/lib/Analysis/AssumptionCache.cpp b/llvm/lib/Analysis/AssumptionCache.cpp index aa55d79b761..5851594700a 100644 --- a/llvm/lib/Analysis/AssumptionCache.cpp +++ b/llvm/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 @@ void AssumptionCache::updateAffectedValues(CallInst *CI) { } 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::AffectedValueCallbackVH::deleted() { // '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() { |