MemoryBuiltins.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. //===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This family of functions identifies calls to builtin functions that allocate
  11. // or free memory.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Analysis/MemoryBuiltins.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/Analysis/TargetLibraryInfo.h"
  18. #include "llvm/Analysis/ValueTracking.h"
  19. #include "llvm/IR/DataLayout.h"
  20. #include "llvm/IR/GlobalVariable.h"
  21. #include "llvm/IR/Instructions.h"
  22. #include "llvm/IR/Intrinsics.h"
  23. #include "llvm/IR/Metadata.h"
  24. #include "llvm/IR/Module.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/MathExtras.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include "llvm/Transforms/Utils/Local.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "memory-builtins"
  31. enum AllocType {
  32. OpNewLike = 1<<0, // allocates; never returns null
  33. MallocLike = 1<<1 | OpNewLike, // allocates; may return null
  34. CallocLike = 1<<2, // allocates + bzero
  35. ReallocLike = 1<<3, // reallocates
  36. StrDupLike = 1<<4,
  37. AllocLike = MallocLike | CallocLike | StrDupLike,
  38. AnyAlloc = AllocLike | ReallocLike
  39. };
  40. struct AllocFnsTy {
  41. LibFunc::Func Func;
  42. AllocType AllocTy;
  43. unsigned char NumParams;
  44. // First and Second size parameters (or -1 if unused)
  45. signed char FstParam, SndParam;
  46. };
  47. // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
  48. // know which functions are nounwind, noalias, nocapture parameters, etc.
  49. static const AllocFnsTy AllocationFnData[] = {
  50. {LibFunc::malloc, MallocLike, 1, 0, -1},
  51. {LibFunc::valloc, MallocLike, 1, 0, -1},
  52. {LibFunc::Znwj, OpNewLike, 1, 0, -1}, // new(unsigned int)
  53. {LibFunc::ZnwjRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new(unsigned int, nothrow)
  54. {LibFunc::Znwm, OpNewLike, 1, 0, -1}, // new(unsigned long)
  55. {LibFunc::ZnwmRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new(unsigned long, nothrow)
  56. {LibFunc::Znaj, OpNewLike, 1, 0, -1}, // new[](unsigned int)
  57. {LibFunc::ZnajRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new[](unsigned int, nothrow)
  58. {LibFunc::Znam, OpNewLike, 1, 0, -1}, // new[](unsigned long)
  59. {LibFunc::ZnamRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new[](unsigned long, nothrow)
  60. {LibFunc::calloc, CallocLike, 2, 0, 1},
  61. {LibFunc::realloc, ReallocLike, 2, 1, -1},
  62. {LibFunc::reallocf, ReallocLike, 2, 1, -1},
  63. {LibFunc::strdup, StrDupLike, 1, -1, -1},
  64. {LibFunc::strndup, StrDupLike, 2, 1, -1}
  65. // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
  66. };
  67. static Function *getCalledFunction(const Value *V, bool LookThroughBitCast) {
  68. if (LookThroughBitCast)
  69. V = V->stripPointerCasts();
  70. CallSite CS(const_cast<Value*>(V));
  71. if (!CS.getInstruction())
  72. return nullptr;
  73. if (CS.isNoBuiltin())
  74. return nullptr;
  75. Function *Callee = CS.getCalledFunction();
  76. if (!Callee || !Callee->isDeclaration())
  77. return nullptr;
  78. return Callee;
  79. }
  80. /// \brief Returns the allocation data for the given value if it is a call to a
  81. /// known allocation function, and NULL otherwise.
  82. static const AllocFnsTy *getAllocationData(const Value *V, AllocType AllocTy,
  83. const TargetLibraryInfo *TLI,
  84. bool LookThroughBitCast = false) {
  85. // Skip intrinsics
  86. if (isa<IntrinsicInst>(V))
  87. return nullptr;
  88. Function *Callee = getCalledFunction(V, LookThroughBitCast);
  89. if (!Callee)
  90. return nullptr;
  91. // Make sure that the function is available.
  92. StringRef FnName = Callee->getName();
  93. LibFunc::Func TLIFn;
  94. if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
  95. return nullptr;
  96. unsigned i = 0;
  97. bool found = false;
  98. for ( ; i < array_lengthof(AllocationFnData); ++i) {
  99. if (AllocationFnData[i].Func == TLIFn) {
  100. found = true;
  101. break;
  102. }
  103. }
  104. if (!found)
  105. return nullptr;
  106. const AllocFnsTy *FnData = &AllocationFnData[i];
  107. if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
  108. return nullptr;
  109. // Check function prototype.
  110. int FstParam = FnData->FstParam;
  111. int SndParam = FnData->SndParam;
  112. FunctionType *FTy = Callee->getFunctionType();
  113. if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
  114. FTy->getNumParams() == FnData->NumParams &&
  115. (FstParam < 0 ||
  116. (FTy->getParamType(FstParam)->isIntegerTy(32) ||
  117. FTy->getParamType(FstParam)->isIntegerTy(64))) &&
  118. (SndParam < 0 ||
  119. FTy->getParamType(SndParam)->isIntegerTy(32) ||
  120. FTy->getParamType(SndParam)->isIntegerTy(64)))
  121. return FnData;
  122. return nullptr;
  123. }
  124. static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
  125. ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
  126. return CS && CS.hasFnAttr(Attribute::NoAlias);
  127. }
  128. /// \brief Tests if a value is a call or invoke to a library function that
  129. /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
  130. /// like).
  131. bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
  132. bool LookThroughBitCast) {
  133. return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast);
  134. }
  135. /// \brief Tests if a value is a call or invoke to a function that returns a
  136. /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
  137. bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
  138. bool LookThroughBitCast) {
  139. // it's safe to consider realloc as noalias since accessing the original
  140. // pointer is undefined behavior
  141. return isAllocationFn(V, TLI, LookThroughBitCast) ||
  142. hasNoAliasAttr(V, LookThroughBitCast);
  143. }
  144. /// \brief Tests if a value is a call or invoke to a library function that
  145. /// allocates uninitialized memory (such as malloc).
  146. bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  147. bool LookThroughBitCast) {
  148. return getAllocationData(V, MallocLike, TLI, LookThroughBitCast);
  149. }
  150. /// \brief Tests if a value is a call or invoke to a library function that
  151. /// allocates zero-filled memory (such as calloc).
  152. bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  153. bool LookThroughBitCast) {
  154. return getAllocationData(V, CallocLike, TLI, LookThroughBitCast);
  155. }
  156. /// \brief Tests if a value is a call or invoke to a library function that
  157. /// allocates memory (either malloc, calloc, or strdup like).
  158. bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  159. bool LookThroughBitCast) {
  160. return getAllocationData(V, AllocLike, TLI, LookThroughBitCast);
  161. }
  162. /// \brief Tests if a value is a call or invoke to a library function that
  163. /// reallocates memory (such as realloc).
  164. bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  165. bool LookThroughBitCast) {
  166. return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast);
  167. }
  168. /// \brief Tests if a value is a call or invoke to a library function that
  169. /// allocates memory and never returns null (such as operator new).
  170. bool llvm::isOperatorNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  171. bool LookThroughBitCast) {
  172. return getAllocationData(V, OpNewLike, TLI, LookThroughBitCast);
  173. }
  174. /// extractMallocCall - Returns the corresponding CallInst if the instruction
  175. /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
  176. /// ignore InvokeInst here.
  177. const CallInst *llvm::extractMallocCall(const Value *I,
  178. const TargetLibraryInfo *TLI) {
  179. return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : nullptr;
  180. }
  181. static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
  182. const TargetLibraryInfo *TLI,
  183. bool LookThroughSExt = false) {
  184. if (!CI)
  185. return nullptr;
  186. // The size of the malloc's result type must be known to determine array size.
  187. Type *T = getMallocAllocatedType(CI, TLI);
  188. if (!T || !T->isSized())
  189. return nullptr;
  190. unsigned ElementSize = DL.getTypeAllocSize(T);
  191. if (StructType *ST = dyn_cast<StructType>(T))
  192. ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
  193. // If malloc call's arg can be determined to be a multiple of ElementSize,
  194. // return the multiple. Otherwise, return NULL.
  195. Value *MallocArg = CI->getArgOperand(0);
  196. Value *Multiple = nullptr;
  197. if (ComputeMultiple(MallocArg, ElementSize, Multiple,
  198. LookThroughSExt))
  199. return Multiple;
  200. return nullptr;
  201. }
  202. /// getMallocType - Returns the PointerType resulting from the malloc call.
  203. /// The PointerType depends on the number of bitcast uses of the malloc call:
  204. /// 0: PointerType is the calls' return type.
  205. /// 1: PointerType is the bitcast's result type.
  206. /// >1: Unique PointerType cannot be determined, return NULL.
  207. PointerType *llvm::getMallocType(const CallInst *CI,
  208. const TargetLibraryInfo *TLI) {
  209. assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
  210. PointerType *MallocType = nullptr;
  211. unsigned NumOfBitCastUses = 0;
  212. // Determine if CallInst has a bitcast use.
  213. for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
  214. UI != E;)
  215. if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
  216. MallocType = cast<PointerType>(BCI->getDestTy());
  217. NumOfBitCastUses++;
  218. }
  219. // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
  220. if (NumOfBitCastUses == 1)
  221. return MallocType;
  222. // Malloc call was not bitcast, so type is the malloc function's return type.
  223. if (NumOfBitCastUses == 0)
  224. return cast<PointerType>(CI->getType());
  225. // Type could not be determined.
  226. return nullptr;
  227. }
  228. /// getMallocAllocatedType - Returns the Type allocated by malloc call.
  229. /// The Type depends on the number of bitcast uses of the malloc call:
  230. /// 0: PointerType is the malloc calls' return type.
  231. /// 1: PointerType is the bitcast's result type.
  232. /// >1: Unique PointerType cannot be determined, return NULL.
  233. Type *llvm::getMallocAllocatedType(const CallInst *CI,
  234. const TargetLibraryInfo *TLI) {
  235. PointerType *PT = getMallocType(CI, TLI);
  236. return PT ? PT->getElementType() : nullptr;
  237. }
  238. /// getMallocArraySize - Returns the array size of a malloc call. If the
  239. /// argument passed to malloc is a multiple of the size of the malloced type,
  240. /// then return that multiple. For non-array mallocs, the multiple is
  241. /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
  242. /// determined.
  243. Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL,
  244. const TargetLibraryInfo *TLI,
  245. bool LookThroughSExt) {
  246. assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
  247. return computeArraySize(CI, DL, TLI, LookThroughSExt);
  248. }
  249. /// extractCallocCall - Returns the corresponding CallInst if the instruction
  250. /// is a calloc call.
  251. const CallInst *llvm::extractCallocCall(const Value *I,
  252. const TargetLibraryInfo *TLI) {
  253. return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
  254. }
  255. /// isFreeCall - Returns non-null if the value is a call to the builtin free()
  256. const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
  257. const CallInst *CI = dyn_cast<CallInst>(I);
  258. if (!CI || isa<IntrinsicInst>(CI))
  259. return nullptr;
  260. Function *Callee = CI->getCalledFunction();
  261. if (Callee == nullptr)
  262. return nullptr;
  263. StringRef FnName = Callee->getName();
  264. LibFunc::Func TLIFn;
  265. if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
  266. return nullptr;
  267. unsigned ExpectedNumParams;
  268. if (TLIFn == LibFunc::free ||
  269. TLIFn == LibFunc::ZdlPv || // operator delete(void*)
  270. TLIFn == LibFunc::ZdaPv) // operator delete[](void*)
  271. ExpectedNumParams = 1;
  272. else if (TLIFn == LibFunc::ZdlPvj || // delete(void*, uint)
  273. TLIFn == LibFunc::ZdlPvm || // delete(void*, ulong)
  274. TLIFn == LibFunc::ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
  275. TLIFn == LibFunc::ZdaPvj || // delete[](void*, uint)
  276. TLIFn == LibFunc::ZdaPvm || // delete[](void*, ulong)
  277. TLIFn == LibFunc::ZdaPvRKSt9nothrow_t) // delete[](void*, nothrow)
  278. ExpectedNumParams = 2;
  279. else
  280. return nullptr;
  281. // Check free prototype.
  282. // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
  283. // attribute will exist.
  284. FunctionType *FTy = Callee->getFunctionType();
  285. if (!FTy->getReturnType()->isVoidTy())
  286. return nullptr;
  287. if (FTy->getNumParams() != ExpectedNumParams)
  288. return nullptr;
  289. if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
  290. return nullptr;
  291. return CI;
  292. }
  293. //===----------------------------------------------------------------------===//
  294. // Utility functions to compute size of objects.
  295. //
  296. /// \brief Compute the size of the object pointed by Ptr. Returns true and the
  297. /// object size in Size if successful, and false otherwise.
  298. /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
  299. /// byval arguments, and global variables.
  300. bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
  301. const TargetLibraryInfo *TLI, bool RoundToAlign) {
  302. ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), RoundToAlign);
  303. SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
  304. if (!Visitor.bothKnown(Data))
  305. return false;
  306. APInt ObjSize = Data.first, Offset = Data.second;
  307. // check for overflow
  308. if (Offset.slt(0) || ObjSize.ult(Offset))
  309. Size = 0;
  310. else
  311. Size = (ObjSize - Offset).getZExtValue();
  312. return true;
  313. }
  314. STATISTIC(ObjectVisitorArgument,
  315. "Number of arguments with unsolved size and offset");
  316. STATISTIC(ObjectVisitorLoad,
  317. "Number of load instructions with unsolved size and offset");
  318. APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
  319. if (RoundToAlign && Align)
  320. return APInt(IntTyBits, RoundUpToAlignment(Size.getZExtValue(), Align));
  321. return Size;
  322. }
  323. ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
  324. const TargetLibraryInfo *TLI,
  325. LLVMContext &Context,
  326. bool RoundToAlign)
  327. : DL(DL), TLI(TLI), RoundToAlign(RoundToAlign) {
  328. // Pointer size must be rechecked for each object visited since it could have
  329. // a different address space.
  330. }
  331. SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
  332. IntTyBits = DL.getPointerTypeSizeInBits(V->getType());
  333. Zero = APInt::getNullValue(IntTyBits);
  334. V = V->stripPointerCasts();
  335. if (Instruction *I = dyn_cast<Instruction>(V)) {
  336. // If we have already seen this instruction, bail out. Cycles can happen in
  337. // unreachable code after constant propagation.
  338. if (!SeenInsts.insert(I).second)
  339. return unknown();
  340. if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
  341. return visitGEPOperator(*GEP);
  342. return visit(*I);
  343. }
  344. if (Argument *A = dyn_cast<Argument>(V))
  345. return visitArgument(*A);
  346. if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
  347. return visitConstantPointerNull(*P);
  348. if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
  349. return visitGlobalAlias(*GA);
  350. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
  351. return visitGlobalVariable(*GV);
  352. if (UndefValue *UV = dyn_cast<UndefValue>(V))
  353. return visitUndefValue(*UV);
  354. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  355. if (CE->getOpcode() == Instruction::IntToPtr)
  356. return unknown(); // clueless
  357. if (CE->getOpcode() == Instruction::GetElementPtr)
  358. return visitGEPOperator(cast<GEPOperator>(*CE));
  359. }
  360. DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
  361. << '\n');
  362. return unknown();
  363. }
  364. SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
  365. if (!I.getAllocatedType()->isSized())
  366. return unknown();
  367. APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
  368. if (!I.isArrayAllocation())
  369. return std::make_pair(align(Size, I.getAlignment()), Zero);
  370. Value *ArraySize = I.getArraySize();
  371. if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
  372. Size *= C->getValue().zextOrSelf(IntTyBits);
  373. return std::make_pair(align(Size, I.getAlignment()), Zero);
  374. }
  375. return unknown();
  376. }
  377. SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
  378. // no interprocedural analysis is done at the moment
  379. if (!A.hasByValOrInAllocaAttr()) {
  380. ++ObjectVisitorArgument;
  381. return unknown();
  382. }
  383. PointerType *PT = cast<PointerType>(A.getType());
  384. APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
  385. return std::make_pair(align(Size, A.getParamAlignment()), Zero);
  386. }
  387. SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
  388. const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
  389. TLI);
  390. if (!FnData)
  391. return unknown();
  392. // handle strdup-like functions separately
  393. if (FnData->AllocTy == StrDupLike) {
  394. APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
  395. if (!Size)
  396. return unknown();
  397. // strndup limits strlen
  398. if (FnData->FstParam > 0) {
  399. ConstantInt *Arg= dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
  400. if (!Arg)
  401. return unknown();
  402. APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
  403. if (Size.ugt(MaxSize))
  404. Size = MaxSize + 1;
  405. }
  406. return std::make_pair(Size, Zero);
  407. }
  408. ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
  409. if (!Arg)
  410. return unknown();
  411. APInt Size = Arg->getValue().zextOrSelf(IntTyBits);
  412. // size determined by just 1 parameter
  413. if (FnData->SndParam < 0)
  414. return std::make_pair(Size, Zero);
  415. Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
  416. if (!Arg)
  417. return unknown();
  418. Size *= Arg->getValue().zextOrSelf(IntTyBits);
  419. return std::make_pair(Size, Zero);
  420. // TODO: handle more standard functions (+ wchar cousins):
  421. // - strdup / strndup
  422. // - strcpy / strncpy
  423. // - strcat / strncat
  424. // - memcpy / memmove
  425. // - strcat / strncat
  426. // - memset
  427. }
  428. SizeOffsetType
  429. ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull&) {
  430. return std::make_pair(Zero, Zero);
  431. }
  432. SizeOffsetType
  433. ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
  434. return unknown();
  435. }
  436. SizeOffsetType
  437. ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
  438. // Easy cases were already folded by previous passes.
  439. return unknown();
  440. }
  441. SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
  442. SizeOffsetType PtrData = compute(GEP.getPointerOperand());
  443. APInt Offset(IntTyBits, 0);
  444. if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
  445. return unknown();
  446. return std::make_pair(PtrData.first, PtrData.second + Offset);
  447. }
  448. SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
  449. if (GA.mayBeOverridden())
  450. return unknown();
  451. return compute(GA.getAliasee());
  452. }
  453. SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
  454. if (!GV.hasDefinitiveInitializer())
  455. return unknown();
  456. APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getType()->getElementType()));
  457. return std::make_pair(align(Size, GV.getAlignment()), Zero);
  458. }
  459. SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
  460. // clueless
  461. return unknown();
  462. }
  463. SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
  464. ++ObjectVisitorLoad;
  465. return unknown();
  466. }
  467. SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
  468. // too complex to analyze statically.
  469. return unknown();
  470. }
  471. SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
  472. SizeOffsetType TrueSide = compute(I.getTrueValue());
  473. SizeOffsetType FalseSide = compute(I.getFalseValue());
  474. if (bothKnown(TrueSide) && bothKnown(FalseSide) && TrueSide == FalseSide)
  475. return TrueSide;
  476. return unknown();
  477. }
  478. SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
  479. return std::make_pair(Zero, Zero);
  480. }
  481. SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
  482. DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
  483. return unknown();
  484. }
  485. ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(
  486. const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
  487. bool RoundToAlign)
  488. : DL(DL), TLI(TLI), Context(Context), Builder(Context, TargetFolder(DL)),
  489. RoundToAlign(RoundToAlign) {
  490. // IntTy and Zero must be set for each compute() since the address space may
  491. // be different for later objects.
  492. }
  493. SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
  494. // XXX - Are vectors of pointers possible here?
  495. IntTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
  496. Zero = ConstantInt::get(IntTy, 0);
  497. SizeOffsetEvalType Result = compute_(V);
  498. if (!bothKnown(Result)) {
  499. // erase everything that was computed in this iteration from the cache, so
  500. // that no dangling references are left behind. We could be a bit smarter if
  501. // we kept a dependency graph. It's probably not worth the complexity.
  502. for (PtrSetTy::iterator I=SeenVals.begin(), E=SeenVals.end(); I != E; ++I) {
  503. CacheMapTy::iterator CacheIt = CacheMap.find(*I);
  504. // non-computable results can be safely cached
  505. if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
  506. CacheMap.erase(CacheIt);
  507. }
  508. }
  509. SeenVals.clear();
  510. return Result;
  511. }
  512. SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
  513. ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, RoundToAlign);
  514. SizeOffsetType Const = Visitor.compute(V);
  515. if (Visitor.bothKnown(Const))
  516. return std::make_pair(ConstantInt::get(Context, Const.first),
  517. ConstantInt::get(Context, Const.second));
  518. V = V->stripPointerCasts();
  519. // check cache
  520. CacheMapTy::iterator CacheIt = CacheMap.find(V);
  521. if (CacheIt != CacheMap.end())
  522. return CacheIt->second;
  523. // always generate code immediately before the instruction being
  524. // processed, so that the generated code dominates the same BBs
  525. Instruction *PrevInsertPoint = Builder.GetInsertPoint();
  526. if (Instruction *I = dyn_cast<Instruction>(V))
  527. Builder.SetInsertPoint(I);
  528. // now compute the size and offset
  529. SizeOffsetEvalType Result;
  530. // Record the pointers that were handled in this run, so that they can be
  531. // cleaned later if something fails. We also use this set to break cycles that
  532. // can occur in dead code.
  533. if (!SeenVals.insert(V).second) {
  534. Result = unknown();
  535. } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
  536. Result = visitGEPOperator(*GEP);
  537. } else if (Instruction *I = dyn_cast<Instruction>(V)) {
  538. Result = visit(*I);
  539. } else if (isa<Argument>(V) ||
  540. (isa<ConstantExpr>(V) &&
  541. cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
  542. isa<GlobalAlias>(V) ||
  543. isa<GlobalVariable>(V)) {
  544. // ignore values where we cannot do more than what ObjectSizeVisitor can
  545. Result = unknown();
  546. } else {
  547. DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
  548. << *V << '\n');
  549. Result = unknown();
  550. }
  551. if (PrevInsertPoint)
  552. Builder.SetInsertPoint(PrevInsertPoint);
  553. // Don't reuse CacheIt since it may be invalid at this point.
  554. CacheMap[V] = Result;
  555. return Result;
  556. }
  557. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
  558. if (!I.getAllocatedType()->isSized())
  559. return unknown();
  560. // must be a VLA
  561. assert(I.isArrayAllocation());
  562. Value *ArraySize = I.getArraySize();
  563. Value *Size = ConstantInt::get(ArraySize->getType(),
  564. DL.getTypeAllocSize(I.getAllocatedType()));
  565. Size = Builder.CreateMul(Size, ArraySize);
  566. return std::make_pair(Size, Zero);
  567. }
  568. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
  569. const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
  570. TLI);
  571. if (!FnData)
  572. return unknown();
  573. // handle strdup-like functions separately
  574. if (FnData->AllocTy == StrDupLike) {
  575. // TODO
  576. return unknown();
  577. }
  578. Value *FirstArg = CS.getArgument(FnData->FstParam);
  579. FirstArg = Builder.CreateZExt(FirstArg, IntTy);
  580. if (FnData->SndParam < 0)
  581. return std::make_pair(FirstArg, Zero);
  582. Value *SecondArg = CS.getArgument(FnData->SndParam);
  583. SecondArg = Builder.CreateZExt(SecondArg, IntTy);
  584. Value *Size = Builder.CreateMul(FirstArg, SecondArg);
  585. return std::make_pair(Size, Zero);
  586. // TODO: handle more standard functions (+ wchar cousins):
  587. // - strdup / strndup
  588. // - strcpy / strncpy
  589. // - strcat / strncat
  590. // - memcpy / memmove
  591. // - strcat / strncat
  592. // - memset
  593. }
  594. SizeOffsetEvalType
  595. ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
  596. return unknown();
  597. }
  598. SizeOffsetEvalType
  599. ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
  600. return unknown();
  601. }
  602. SizeOffsetEvalType
  603. ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
  604. SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
  605. if (!bothKnown(PtrData))
  606. return unknown();
  607. Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
  608. Offset = Builder.CreateAdd(PtrData.second, Offset);
  609. return std::make_pair(PtrData.first, Offset);
  610. }
  611. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
  612. // clueless
  613. return unknown();
  614. }
  615. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
  616. return unknown();
  617. }
  618. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
  619. // create 2 PHIs: one for size and another for offset
  620. PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
  621. PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
  622. // insert right away in the cache to handle recursive PHIs
  623. CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
  624. // compute offset/size for each PHI incoming pointer
  625. for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
  626. Builder.SetInsertPoint(PHI.getIncomingBlock(i)->getFirstInsertionPt());
  627. SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
  628. if (!bothKnown(EdgeData)) {
  629. OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
  630. OffsetPHI->eraseFromParent();
  631. SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
  632. SizePHI->eraseFromParent();
  633. return unknown();
  634. }
  635. SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
  636. OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
  637. }
  638. Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
  639. if ((Tmp = SizePHI->hasConstantValue())) {
  640. Size = Tmp;
  641. SizePHI->replaceAllUsesWith(Size);
  642. SizePHI->eraseFromParent();
  643. }
  644. if ((Tmp = OffsetPHI->hasConstantValue())) {
  645. Offset = Tmp;
  646. OffsetPHI->replaceAllUsesWith(Offset);
  647. OffsetPHI->eraseFromParent();
  648. }
  649. return std::make_pair(Size, Offset);
  650. }
  651. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
  652. SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
  653. SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
  654. if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
  655. return unknown();
  656. if (TrueSide == FalseSide)
  657. return TrueSide;
  658. Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
  659. FalseSide.first);
  660. Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
  661. FalseSide.second);
  662. return std::make_pair(Size, Offset);
  663. }
  664. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
  665. DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');
  666. return unknown();
  667. }