From owner-svn-src-projects@freebsd.org Mon Jan 16 19:53:20 2017 Return-Path: Delivered-To: svn-src-projects@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 27030CB35CC for ; Mon, 16 Jan 2017 19:53:20 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id F2F601891; Mon, 16 Jan 2017 19:53:19 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v0GJrJum026192; Mon, 16 Jan 2017 19:53:19 GMT (envelope-from dim@FreeBSD.org) Received: (from dim@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v0GJrIMd026190; Mon, 16 Jan 2017 19:53:18 GMT (envelope-from dim@FreeBSD.org) Message-Id: <201701161953.v0GJrIMd026190@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: dim set sender to dim@FreeBSD.org using -f From: Dimitry Andric Date: Mon, 16 Jan 2017 19:53:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r312308 - in projects/clang400-import/contrib/llvm: include/llvm/Analysis lib/Analysis X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 16 Jan 2017 19:53:20 -0000 Author: dim Date: Mon Jan 16 19:53:18 2017 New Revision: 312308 URL: https://svnweb.freebsd.org/changeset/base/312308 Log: Pull in r292133 from upstream llvm trunk (by Hal Finkel): 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 This should fix crashes when building lld (as part of the llvmXY ports). Reported by: jbeich PR: 216117 Modified: projects/clang400-import/contrib/llvm/include/llvm/Analysis/AssumptionCache.h projects/clang400-import/contrib/llvm/lib/Analysis/AssumptionCache.cpp Modified: projects/clang400-import/contrib/llvm/include/llvm/Analysis/AssumptionCache.h ============================================================================== --- projects/clang400-import/contrib/llvm/include/llvm/Analysis/AssumptionCache.h Mon Jan 16 19:35:19 2017 (r312307) +++ projects/clang400-import/contrib/llvm/include/llvm/Analysis/AssumptionCache.h Mon Jan 16 19:53:18 2017 (r312308) @@ -68,7 +68,10 @@ class AssumptionCache { AffectedValuesMap AffectedValues; /// Get the vector of assumptions which affect a value from the cache. - SmallVector &getAffectedValues(Value *V); + SmallVector &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: projects/clang400-import/contrib/llvm/lib/Analysis/AssumptionCache.cpp ============================================================================== --- projects/clang400-import/contrib/llvm/lib/Analysis/AssumptionCache.cpp Mon Jan 16 19:35:19 2017 (r312307) +++ projects/clang400-import/contrib/llvm/lib/Analysis/AssumptionCache.cpp Mon Jan 16 19:53:18 2017 (r312308) @@ -24,7 +24,7 @@ using namespace llvm; using namespace llvm::PatternMatch; -SmallVector &AssumptionCache::getAffectedValues(Value *V) { +SmallVector &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(NV) && !isa(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() {