summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorAyke van Laethem <aykevanlaethem@gmail.com>2019-06-09 10:20:33 +0000
committerAyke van Laethem <aykevanlaethem@gmail.com>2019-06-09 10:20:33 +0000
commitf18cf230e4e2a78fd575a5a1d260653b452f4d9d (patch)
tree36d5f91fc1a187144e9c2a9548fba745a854493a /llvm/lib
parentde329e511606b73d0b06f1afeef8689f84f54102 (diff)
downloadbcm5719-llvm-f18cf230e4e2a78fd575a5a1d260653b452f4d9d.tar.gz
bcm5719-llvm-f18cf230e4e2a78fd575a5a1d260653b452f4d9d.zip
[CaptureTracking] Don't let comparisons against null escape inbounds pointers
Pointers that are in-bounds (either through dereferenceable_or_null or thorough a getelementptr inbounds) cannot be captured with a comparison against null. There is no way to construct a pointer that is still in bounds but also NULL. This helps safe languages that insert null checks before load/store instructions. Without this patch, almost all pointers would be considered captured even for simple loads. With this patch, an icmp with null will not be seen as escaping as long as certain conditions are met. There was a lot of discussion about this patch. See the Phabricator thread for detals. Differential Revision: https://reviews.llvm.org/D60047 llvm-svn: 362900
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Analysis/CaptureTracking.cpp28
1 files changed, 23 insertions, 5 deletions
diff --git a/llvm/lib/Analysis/CaptureTracking.cpp b/llvm/lib/Analysis/CaptureTracking.cpp
index d2a0920413c..adaa83a6c44 100644
--- a/llvm/lib/Analysis/CaptureTracking.cpp
+++ b/llvm/lib/Analysis/CaptureTracking.cpp
@@ -330,14 +330,32 @@ void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
AddUses(I);
break;
case Instruction::ICmp: {
- // Don't count comparisons of a no-alias return value against null as
- // captures. This allows us to ignore comparisons of malloc results
- // with null, for example.
- if (ConstantPointerNull *CPN =
- dyn_cast<ConstantPointerNull>(I->getOperand(1)))
+ if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(1))) {
+ // Don't count comparisons of a no-alias return value against null as
+ // captures. This allows us to ignore comparisons of malloc results
+ // with null, for example.
if (CPN->getType()->getAddressSpace() == 0)
if (isNoAliasCall(V->stripPointerCasts()))
break;
+ if (!I->getFunction()->nullPointerIsDefined()) {
+ auto *O = I->getOperand(0)->stripPointerCastsSameRepresentation();
+ // An inbounds GEP can either be a valid pointer (pointing into
+ // or to the end of an allocation), or be null in the default
+ // address space. So for an inbounds GEPs there is no way to let
+ // the pointer escape using clever GEP hacking because doing so
+ // would make the pointer point outside of the allocated object
+ // and thus make the GEP result a poison value.
+ if (auto *GEP = dyn_cast<GetElementPtrInst>(O))
+ if (GEP->isInBounds())
+ break;
+ // Comparing a dereferenceable_or_null argument against null
+ // cannot lead to pointer escapes, because if it is not null it
+ // must be a valid (in-bounds) pointer.
+ bool CanBeNull;
+ if (O->getPointerDereferenceableBytes(I->getModule()->getDataLayout(), CanBeNull))
+ break;
+ }
+ }
// Comparison against value stored in global variable. Given the pointer
// does not escape, its value cannot be guessed and stored separately in a
// global variable.
OpenPOWER on IntegriCloud