1 //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains routines that help determine which pointers are captured. 10 // A pointer value is captured if the function makes a copy of any part of the 11 // pointer that outlives the call. Not being captured means, more or less, that 12 // the pointer is only dereferenced and not stored in a global. Returning part 13 // of the pointer as the function return value may or may not count as capturing 14 // the pointer, depending on the context. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/Analysis/CaptureTracking.h" 19 #include "llvm/ADT/SmallSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/Analysis/AliasAnalysis.h" 22 #include "llvm/Analysis/CFG.h" 23 #include "llvm/Analysis/ValueTracking.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/Dominators.h" 26 #include "llvm/IR/Instructions.h" 27 #include "llvm/IR/IntrinsicInst.h" 28 29 using namespace llvm; 30 31 /// The default value for MaxUsesToExplore argument. It's relatively small to 32 /// keep the cost of analysis reasonable for clients like BasicAliasAnalysis, 33 /// where the results can't be cached. 34 /// TODO: we should probably introduce a caching CaptureTracking analysis and 35 /// use it where possible. The caching version can use much higher limit or 36 /// don't have this cap at all. 37 static cl::opt<unsigned> 38 DefaultMaxUsesToExplore("capture-tracking-max-uses-to-explore", cl::Hidden, 39 cl::desc("Maximal number of uses to explore."), 40 cl::init(20)); 41 42 unsigned llvm::getDefaultMaxUsesToExploreForCaptureTracking() { 43 return DefaultMaxUsesToExplore; 44 } 45 46 CaptureTracker::~CaptureTracker() {} 47 48 bool CaptureTracker::shouldExplore(const Use *U) { return true; } 49 50 bool CaptureTracker::isDereferenceableOrNull(Value *O, const DataLayout &DL) { 51 // An inbounds GEP can either be a valid pointer (pointing into 52 // or to the end of an allocation), or be null in the default 53 // address space. So for an inbounds GEP there is no way to let 54 // the pointer escape using clever GEP hacking because doing so 55 // would make the pointer point outside of the allocated object 56 // and thus make the GEP result a poison value. Similarly, other 57 // dereferenceable pointers cannot be manipulated without producing 58 // poison. 59 if (auto *GEP = dyn_cast<GetElementPtrInst>(O)) 60 if (GEP->isInBounds()) 61 return true; 62 bool CanBeNull; 63 return O->getPointerDereferenceableBytes(DL, CanBeNull); 64 } 65 66 namespace { 67 struct SimpleCaptureTracker : public CaptureTracker { 68 explicit SimpleCaptureTracker(bool ReturnCaptures) 69 : ReturnCaptures(ReturnCaptures), Captured(false) {} 70 71 void tooManyUses() override { Captured = true; } 72 73 bool captured(const Use *U) override { 74 if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures) 75 return false; 76 77 Captured = true; 78 return true; 79 } 80 81 bool ReturnCaptures; 82 83 bool Captured; 84 }; 85 86 /// Only find pointer captures which happen before the given instruction. Uses 87 /// the dominator tree to determine whether one instruction is before another. 88 /// Only support the case where the Value is defined in the same basic block 89 /// as the given instruction and the use. 90 struct CapturesBefore : public CaptureTracker { 91 92 CapturesBefore(bool ReturnCaptures, const Instruction *I, const DominatorTree *DT, 93 bool IncludeI) 94 : BeforeHere(I), DT(DT), 95 ReturnCaptures(ReturnCaptures), IncludeI(IncludeI), Captured(false) {} 96 97 void tooManyUses() override { Captured = true; } 98 99 bool isSafeToPrune(Instruction *I) { 100 BasicBlock *BB = I->getParent(); 101 // We explore this usage only if the usage can reach "BeforeHere". 102 // If use is not reachable from entry, there is no need to explore. 103 if (BeforeHere != I && !DT->isReachableFromEntry(BB)) 104 return true; 105 106 // Compute the case where both instructions are inside the same basic 107 // block. 108 if (BB == BeforeHere->getParent()) { 109 // 'I' dominates 'BeforeHere' => not safe to prune. 110 // 111 // The value defined by an invoke dominates an instruction only 112 // if it dominates every instruction in UseBB. A PHI is dominated only 113 // if the instruction dominates every possible use in the UseBB. Since 114 // UseBB == BB, avoid pruning. 115 if (isa<InvokeInst>(BeforeHere) || isa<PHINode>(I) || I == BeforeHere) 116 return false; 117 if (!BeforeHere->comesBefore(I)) 118 return false; 119 120 // 'BeforeHere' comes before 'I', it's safe to prune if we also 121 // guarantee that 'I' never reaches 'BeforeHere' through a back-edge or 122 // by its successors, i.e, prune if: 123 // 124 // (1) BB is an entry block or have no successors. 125 // (2) There's no path coming back through BB successors. 126 if (BB == &BB->getParent()->getEntryBlock() || 127 !BB->getTerminator()->getNumSuccessors()) 128 return true; 129 130 SmallVector<BasicBlock*, 32> Worklist; 131 Worklist.append(succ_begin(BB), succ_end(BB)); 132 return !isPotentiallyReachableFromMany(Worklist, BB, nullptr, DT); 133 } 134 135 // If the value is defined in the same basic block as use and BeforeHere, 136 // there is no need to explore the use if BeforeHere dominates use. 137 // Check whether there is a path from I to BeforeHere. 138 if (BeforeHere != I && DT->dominates(BeforeHere, I) && 139 !isPotentiallyReachable(I, BeforeHere, nullptr, DT)) 140 return true; 141 142 return false; 143 } 144 145 bool shouldExplore(const Use *U) override { 146 Instruction *I = cast<Instruction>(U->getUser()); 147 148 if (BeforeHere == I && !IncludeI) 149 return false; 150 151 if (isSafeToPrune(I)) 152 return false; 153 154 return true; 155 } 156 157 bool captured(const Use *U) override { 158 if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures) 159 return false; 160 161 if (!shouldExplore(U)) 162 return false; 163 164 Captured = true; 165 return true; 166 } 167 168 const Instruction *BeforeHere; 169 const DominatorTree *DT; 170 171 bool ReturnCaptures; 172 bool IncludeI; 173 174 bool Captured; 175 }; 176 } 177 178 /// PointerMayBeCaptured - Return true if this pointer value may be captured 179 /// by the enclosing function (which is required to exist). This routine can 180 /// be expensive, so consider caching the results. The boolean ReturnCaptures 181 /// specifies whether returning the value (or part of it) from the function 182 /// counts as capturing it or not. The boolean StoreCaptures specified whether 183 /// storing the value (or part of it) into memory anywhere automatically 184 /// counts as capturing it or not. 185 bool llvm::PointerMayBeCaptured(const Value *V, 186 bool ReturnCaptures, bool StoreCaptures, 187 unsigned MaxUsesToExplore) { 188 assert(!isa<GlobalValue>(V) && 189 "It doesn't make sense to ask whether a global is captured."); 190 191 // TODO: If StoreCaptures is not true, we could do Fancy analysis 192 // to determine whether this store is not actually an escape point. 193 // In that case, BasicAliasAnalysis should be updated as well to 194 // take advantage of this. 195 (void)StoreCaptures; 196 197 SimpleCaptureTracker SCT(ReturnCaptures); 198 PointerMayBeCaptured(V, &SCT, MaxUsesToExplore); 199 return SCT.Captured; 200 } 201 202 /// PointerMayBeCapturedBefore - Return true if this pointer value may be 203 /// captured by the enclosing function (which is required to exist). If a 204 /// DominatorTree is provided, only captures which happen before the given 205 /// instruction are considered. This routine can be expensive, so consider 206 /// caching the results. The boolean ReturnCaptures specifies whether 207 /// returning the value (or part of it) from the function counts as capturing 208 /// it or not. The boolean StoreCaptures specified whether storing the value 209 /// (or part of it) into memory anywhere automatically counts as capturing it 210 /// or not. 211 bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, 212 bool StoreCaptures, const Instruction *I, 213 const DominatorTree *DT, bool IncludeI, 214 unsigned MaxUsesToExplore) { 215 assert(!isa<GlobalValue>(V) && 216 "It doesn't make sense to ask whether a global is captured."); 217 218 if (!DT) 219 return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures, 220 MaxUsesToExplore); 221 222 // TODO: See comment in PointerMayBeCaptured regarding what could be done 223 // with StoreCaptures. 224 225 CapturesBefore CB(ReturnCaptures, I, DT, IncludeI); 226 PointerMayBeCaptured(V, &CB, MaxUsesToExplore); 227 return CB.Captured; 228 } 229 230 void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker, 231 unsigned MaxUsesToExplore) { 232 assert(V->getType()->isPointerTy() && "Capture is for pointers only!"); 233 SmallVector<const Use *, 20> Worklist; 234 Worklist.reserve(getDefaultMaxUsesToExploreForCaptureTracking()); 235 SmallSet<const Use *, 20> Visited; 236 237 auto AddUses = [&](const Value *V) { 238 unsigned Count = 0; 239 for (const Use &U : V->uses()) { 240 // If there are lots of uses, conservatively say that the value 241 // is captured to avoid taking too much compile time. 242 if (Count++ >= MaxUsesToExplore) 243 return Tracker->tooManyUses(); 244 if (!Visited.insert(&U).second) 245 continue; 246 if (!Tracker->shouldExplore(&U)) 247 continue; 248 Worklist.push_back(&U); 249 } 250 }; 251 AddUses(V); 252 253 while (!Worklist.empty()) { 254 const Use *U = Worklist.pop_back_val(); 255 Instruction *I = cast<Instruction>(U->getUser()); 256 V = U->get(); 257 258 switch (I->getOpcode()) { 259 case Instruction::Call: 260 case Instruction::Invoke: { 261 auto *Call = cast<CallBase>(I); 262 // Not captured if the callee is readonly, doesn't return a copy through 263 // its return value and doesn't unwind (a readonly function can leak bits 264 // by throwing an exception or not depending on the input value). 265 if (Call->onlyReadsMemory() && Call->doesNotThrow() && 266 Call->getType()->isVoidTy()) 267 break; 268 269 // The pointer is not captured if returned pointer is not captured. 270 // NOTE: CaptureTracking users should not assume that only functions 271 // marked with nocapture do not capture. This means that places like 272 // GetUnderlyingObject in ValueTracking or DecomposeGEPExpression 273 // in BasicAA also need to know about this property. 274 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call, 275 true)) { 276 AddUses(Call); 277 break; 278 } 279 280 // Volatile operations effectively capture the memory location that they 281 // load and store to. 282 if (auto *MI = dyn_cast<MemIntrinsic>(Call)) 283 if (MI->isVolatile()) 284 if (Tracker->captured(U)) 285 return; 286 287 // Not captured if only passed via 'nocapture' arguments. Note that 288 // calling a function pointer does not in itself cause the pointer to 289 // be captured. This is a subtle point considering that (for example) 290 // the callee might return its own address. It is analogous to saying 291 // that loading a value from a pointer does not cause the pointer to be 292 // captured, even though the loaded value might be the pointer itself 293 // (think of self-referential objects). 294 for (auto IdxOpPair : enumerate(Call->data_ops())) { 295 int Idx = IdxOpPair.index(); 296 Value *A = IdxOpPair.value(); 297 if (A == V && !Call->doesNotCapture(Idx)) 298 // The parameter is not marked 'nocapture' - captured. 299 if (Tracker->captured(U)) 300 return; 301 } 302 break; 303 } 304 case Instruction::Load: 305 // Volatile loads make the address observable. 306 if (cast<LoadInst>(I)->isVolatile()) 307 if (Tracker->captured(U)) 308 return; 309 break; 310 case Instruction::VAArg: 311 // "va-arg" from a pointer does not cause it to be captured. 312 break; 313 case Instruction::Store: 314 // Stored the pointer - conservatively assume it may be captured. 315 // Volatile stores make the address observable. 316 if (V == I->getOperand(0) || cast<StoreInst>(I)->isVolatile()) 317 if (Tracker->captured(U)) 318 return; 319 break; 320 case Instruction::AtomicRMW: { 321 // atomicrmw conceptually includes both a load and store from 322 // the same location. 323 // As with a store, the location being accessed is not captured, 324 // but the value being stored is. 325 // Volatile stores make the address observable. 326 auto *ARMWI = cast<AtomicRMWInst>(I); 327 if (ARMWI->getValOperand() == V || ARMWI->isVolatile()) 328 if (Tracker->captured(U)) 329 return; 330 break; 331 } 332 case Instruction::AtomicCmpXchg: { 333 // cmpxchg conceptually includes both a load and store from 334 // the same location. 335 // As with a store, the location being accessed is not captured, 336 // but the value being stored is. 337 // Volatile stores make the address observable. 338 auto *ACXI = cast<AtomicCmpXchgInst>(I); 339 if (ACXI->getCompareOperand() == V || ACXI->getNewValOperand() == V || 340 ACXI->isVolatile()) 341 if (Tracker->captured(U)) 342 return; 343 break; 344 } 345 case Instruction::BitCast: 346 case Instruction::GetElementPtr: 347 case Instruction::PHI: 348 case Instruction::Select: 349 case Instruction::AddrSpaceCast: 350 // The original value is not captured via this if the new value isn't. 351 AddUses(I); 352 break; 353 case Instruction::ICmp: { 354 unsigned Idx = (I->getOperand(0) == V) ? 0 : 1; 355 unsigned OtherIdx = 1 - Idx; 356 if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(OtherIdx))) { 357 // Don't count comparisons of a no-alias return value against null as 358 // captures. This allows us to ignore comparisons of malloc results 359 // with null, for example. 360 if (CPN->getType()->getAddressSpace() == 0) 361 if (isNoAliasCall(V->stripPointerCasts())) 362 break; 363 if (!I->getFunction()->nullPointerIsDefined()) { 364 auto *O = I->getOperand(Idx)->stripPointerCastsSameRepresentation(); 365 // Comparing a dereferenceable_or_null pointer against null cannot 366 // lead to pointer escapes, because if it is not null it must be a 367 // valid (in-bounds) pointer. 368 if (Tracker->isDereferenceableOrNull(O, I->getModule()->getDataLayout())) 369 break; 370 } 371 } 372 // Comparison against value stored in global variable. Given the pointer 373 // does not escape, its value cannot be guessed and stored separately in a 374 // global variable. 375 auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIdx)); 376 if (LI && isa<GlobalVariable>(LI->getPointerOperand())) 377 break; 378 // Otherwise, be conservative. There are crazy ways to capture pointers 379 // using comparisons. 380 if (Tracker->captured(U)) 381 return; 382 break; 383 } 384 default: 385 // Something else - be conservative and say it is captured. 386 if (Tracker->captured(U)) 387 return; 388 break; 389 } 390 } 391 392 // All uses examined. 393 } 394