MemoryBuiltins.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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/APInt.h"
  16. #include "llvm/ADT/None.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Analysis/TargetFolder.h"
  22. #include "llvm/Analysis/TargetLibraryInfo.h"
  23. #include "llvm/Analysis/Utils/Local.h"
  24. #include "llvm/Analysis/ValueTracking.h"
  25. #include "llvm/IR/Argument.h"
  26. #include "llvm/IR/Attributes.h"
  27. #include "llvm/IR/Constants.h"
  28. #include "llvm/IR/DataLayout.h"
  29. #include "llvm/IR/DerivedTypes.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/IR/GlobalAlias.h"
  32. #include "llvm/IR/GlobalVariable.h"
  33. #include "llvm/IR/Instruction.h"
  34. #include "llvm/IR/Instructions.h"
  35. #include "llvm/IR/IntrinsicInst.h"
  36. #include "llvm/IR/Operator.h"
  37. #include "llvm/IR/Type.h"
  38. #include "llvm/IR/Value.h"
  39. #include "llvm/Support/Casting.h"
  40. #include "llvm/Support/Debug.h"
  41. #include "llvm/Support/MathExtras.h"
  42. #include "llvm/Support/raw_ostream.h"
  43. #include <cassert>
  44. #include <cstdint>
  45. #include <iterator>
  46. #include <utility>
  47. using namespace llvm;
  48. #define DEBUG_TYPE "memory-builtins"
  49. enum AllocType : uint8_t {
  50. OpNewLike = 1<<0, // allocates; never returns null
  51. MallocLike = 1<<1 | OpNewLike, // allocates; may return null
  52. CallocLike = 1<<2, // allocates + bzero
  53. ReallocLike = 1<<3, // reallocates
  54. StrDupLike = 1<<4,
  55. MallocOrCallocLike = MallocLike | CallocLike,
  56. AllocLike = MallocLike | CallocLike | StrDupLike,
  57. AnyAlloc = AllocLike | ReallocLike
  58. };
  59. struct AllocFnsTy {
  60. AllocType AllocTy;
  61. unsigned NumParams;
  62. // First and Second size parameters (or -1 if unused)
  63. int FstParam, SndParam;
  64. };
  65. // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
  66. // know which functions are nounwind, noalias, nocapture parameters, etc.
  67. static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
  68. {LibFunc_malloc, {MallocLike, 1, 0, -1}},
  69. {LibFunc_valloc, {MallocLike, 1, 0, -1}},
  70. {LibFunc_Znwj, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
  71. {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
  72. {LibFunc_ZnwjSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned int, align_val_t)
  73. {LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t, // new(unsigned int, align_val_t, nothrow)
  74. {MallocLike, 3, 0, -1}},
  75. {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long)
  76. {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow)
  77. {LibFunc_ZnwmSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned long, align_val_t)
  78. {LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t, // new(unsigned long, align_val_t, nothrow)
  79. {MallocLike, 3, 0, -1}},
  80. {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
  81. {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
  82. {LibFunc_ZnajSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned int, align_val_t)
  83. {LibFunc_ZnajSt11align_val_tRKSt9nothrow_t, // new[](unsigned int, align_val_t, nothrow)
  84. {MallocLike, 3, 0, -1}},
  85. {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long)
  86. {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow)
  87. {LibFunc_ZnamSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned long, align_val_t)
  88. {LibFunc_ZnamSt11align_val_tRKSt9nothrow_t, // new[](unsigned long, align_val_t, nothrow)
  89. {MallocLike, 3, 0, -1}},
  90. {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
  91. {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
  92. {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1}}, // new(unsigned long long)
  93. {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned long long, nothrow)
  94. {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
  95. {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
  96. {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
  97. {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
  98. {LibFunc_calloc, {CallocLike, 2, 0, 1}},
  99. {LibFunc_realloc, {ReallocLike, 2, 1, -1}},
  100. {LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
  101. {LibFunc_strdup, {StrDupLike, 1, -1, -1}},
  102. {LibFunc_strndup, {StrDupLike, 2, 1, -1}}
  103. // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
  104. };
  105. static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
  106. bool &IsNoBuiltin) {
  107. // Don't care about intrinsics in this case.
  108. if (isa<IntrinsicInst>(V))
  109. return nullptr;
  110. if (LookThroughBitCast)
  111. V = V->stripPointerCasts();
  112. ImmutableCallSite CS(V);
  113. if (!CS.getInstruction())
  114. return nullptr;
  115. IsNoBuiltin = CS.isNoBuiltin();
  116. if (const Function *Callee = CS.getCalledFunction())
  117. return Callee;
  118. return nullptr;
  119. }
  120. /// Returns the allocation data for the given value if it's either a call to a
  121. /// known allocation function, or a call to a function with the allocsize
  122. /// attribute.
  123. static Optional<AllocFnsTy>
  124. getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
  125. const TargetLibraryInfo *TLI) {
  126. // Make sure that the function is available.
  127. StringRef FnName = Callee->getName();
  128. LibFunc TLIFn;
  129. if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
  130. return None;
  131. const auto *Iter = find_if(
  132. AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
  133. return P.first == TLIFn;
  134. });
  135. if (Iter == std::end(AllocationFnData))
  136. return None;
  137. const AllocFnsTy *FnData = &Iter->second;
  138. if ((FnData->AllocTy & AllocTy) == 0)
  139. return None;
  140. // Check function prototype.
  141. int FstParam = FnData->FstParam;
  142. int SndParam = FnData->SndParam;
  143. FunctionType *FTy = Callee->getFunctionType();
  144. if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
  145. FTy->getNumParams() == FnData->NumParams &&
  146. (FstParam < 0 ||
  147. (FTy->getParamType(FstParam)->isIntegerTy(32) ||
  148. FTy->getParamType(FstParam)->isIntegerTy(64))) &&
  149. (SndParam < 0 ||
  150. FTy->getParamType(SndParam)->isIntegerTy(32) ||
  151. FTy->getParamType(SndParam)->isIntegerTy(64)))
  152. return *FnData;
  153. return None;
  154. }
  155. static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
  156. const TargetLibraryInfo *TLI,
  157. bool LookThroughBitCast = false) {
  158. bool IsNoBuiltinCall;
  159. if (const Function *Callee =
  160. getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
  161. if (!IsNoBuiltinCall)
  162. return getAllocationDataForFunction(Callee, AllocTy, TLI);
  163. return None;
  164. }
  165. static Optional<AllocFnsTy> getAllocationSize(const Value *V,
  166. const TargetLibraryInfo *TLI) {
  167. bool IsNoBuiltinCall;
  168. const Function *Callee =
  169. getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
  170. if (!Callee)
  171. return None;
  172. // Prefer to use existing information over allocsize. This will give us an
  173. // accurate AllocTy.
  174. if (!IsNoBuiltinCall)
  175. if (Optional<AllocFnsTy> Data =
  176. getAllocationDataForFunction(Callee, AnyAlloc, TLI))
  177. return Data;
  178. Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
  179. if (Attr == Attribute())
  180. return None;
  181. std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
  182. AllocFnsTy Result;
  183. // Because allocsize only tells us how many bytes are allocated, we're not
  184. // really allowed to assume anything, so we use MallocLike.
  185. Result.AllocTy = MallocLike;
  186. Result.NumParams = Callee->getNumOperands();
  187. Result.FstParam = Args.first;
  188. Result.SndParam = Args.second.getValueOr(-1);
  189. return Result;
  190. }
  191. static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
  192. ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
  193. return CS && CS.hasRetAttr(Attribute::NoAlias);
  194. }
  195. /// Tests if a value is a call or invoke to a library function that
  196. /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
  197. /// like).
  198. bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
  199. bool LookThroughBitCast) {
  200. return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue();
  201. }
  202. /// Tests if a value is a call or invoke to a function that returns a
  203. /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
  204. bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
  205. bool LookThroughBitCast) {
  206. // it's safe to consider realloc as noalias since accessing the original
  207. // pointer is undefined behavior
  208. return isAllocationFn(V, TLI, LookThroughBitCast) ||
  209. hasNoAliasAttr(V, LookThroughBitCast);
  210. }
  211. /// Tests if a value is a call or invoke to a library function that
  212. /// allocates uninitialized memory (such as malloc).
  213. bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  214. bool LookThroughBitCast) {
  215. return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue();
  216. }
  217. /// Tests if a value is a call or invoke to a library function that
  218. /// allocates zero-filled memory (such as calloc).
  219. bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  220. bool LookThroughBitCast) {
  221. return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue();
  222. }
  223. /// Tests if a value is a call or invoke to a library function that
  224. /// allocates memory similar to malloc or calloc.
  225. bool llvm::isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  226. bool LookThroughBitCast) {
  227. return getAllocationData(V, MallocOrCallocLike, TLI,
  228. LookThroughBitCast).hasValue();
  229. }
  230. /// Tests if a value is a call or invoke to a library function that
  231. /// allocates memory (either malloc, calloc, or strdup like).
  232. bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  233. bool LookThroughBitCast) {
  234. return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue();
  235. }
  236. /// extractMallocCall - Returns the corresponding CallInst if the instruction
  237. /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
  238. /// ignore InvokeInst here.
  239. const CallInst *llvm::extractMallocCall(const Value *I,
  240. const TargetLibraryInfo *TLI) {
  241. return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : nullptr;
  242. }
  243. static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
  244. const TargetLibraryInfo *TLI,
  245. bool LookThroughSExt = false) {
  246. if (!CI)
  247. return nullptr;
  248. // The size of the malloc's result type must be known to determine array size.
  249. Type *T = getMallocAllocatedType(CI, TLI);
  250. if (!T || !T->isSized())
  251. return nullptr;
  252. unsigned ElementSize = DL.getTypeAllocSize(T);
  253. if (StructType *ST = dyn_cast<StructType>(T))
  254. ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
  255. // If malloc call's arg can be determined to be a multiple of ElementSize,
  256. // return the multiple. Otherwise, return NULL.
  257. Value *MallocArg = CI->getArgOperand(0);
  258. Value *Multiple = nullptr;
  259. if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
  260. return Multiple;
  261. return nullptr;
  262. }
  263. /// getMallocType - Returns the PointerType resulting from the malloc call.
  264. /// The PointerType depends on the number of bitcast uses of the malloc call:
  265. /// 0: PointerType is the calls' return type.
  266. /// 1: PointerType is the bitcast's result type.
  267. /// >1: Unique PointerType cannot be determined, return NULL.
  268. PointerType *llvm::getMallocType(const CallInst *CI,
  269. const TargetLibraryInfo *TLI) {
  270. assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
  271. PointerType *MallocType = nullptr;
  272. unsigned NumOfBitCastUses = 0;
  273. // Determine if CallInst has a bitcast use.
  274. for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
  275. UI != E;)
  276. if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
  277. MallocType = cast<PointerType>(BCI->getDestTy());
  278. NumOfBitCastUses++;
  279. }
  280. // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
  281. if (NumOfBitCastUses == 1)
  282. return MallocType;
  283. // Malloc call was not bitcast, so type is the malloc function's return type.
  284. if (NumOfBitCastUses == 0)
  285. return cast<PointerType>(CI->getType());
  286. // Type could not be determined.
  287. return nullptr;
  288. }
  289. /// getMallocAllocatedType - Returns the Type allocated by malloc call.
  290. /// The Type depends on the number of bitcast uses of the malloc call:
  291. /// 0: PointerType is the malloc calls' return type.
  292. /// 1: PointerType is the bitcast's result type.
  293. /// >1: Unique PointerType cannot be determined, return NULL.
  294. Type *llvm::getMallocAllocatedType(const CallInst *CI,
  295. const TargetLibraryInfo *TLI) {
  296. PointerType *PT = getMallocType(CI, TLI);
  297. return PT ? PT->getElementType() : nullptr;
  298. }
  299. /// getMallocArraySize - Returns the array size of a malloc call. If the
  300. /// argument passed to malloc is a multiple of the size of the malloced type,
  301. /// then return that multiple. For non-array mallocs, the multiple is
  302. /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
  303. /// determined.
  304. Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL,
  305. const TargetLibraryInfo *TLI,
  306. bool LookThroughSExt) {
  307. assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
  308. return computeArraySize(CI, DL, TLI, LookThroughSExt);
  309. }
  310. /// extractCallocCall - Returns the corresponding CallInst if the instruction
  311. /// is a calloc call.
  312. const CallInst *llvm::extractCallocCall(const Value *I,
  313. const TargetLibraryInfo *TLI) {
  314. return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
  315. }
  316. /// isFreeCall - Returns non-null if the value is a call to the builtin free()
  317. const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
  318. bool IsNoBuiltinCall;
  319. const Function *Callee =
  320. getCalledFunction(I, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
  321. if (Callee == nullptr || IsNoBuiltinCall)
  322. return nullptr;
  323. StringRef FnName = Callee->getName();
  324. LibFunc TLIFn;
  325. if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
  326. return nullptr;
  327. unsigned ExpectedNumParams;
  328. if (TLIFn == LibFunc_free ||
  329. TLIFn == LibFunc_ZdlPv || // operator delete(void*)
  330. TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
  331. TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
  332. TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
  333. TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
  334. TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*)
  335. ExpectedNumParams = 1;
  336. else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint)
  337. TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong)
  338. TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
  339. TLIFn == LibFunc_ZdlPvSt11align_val_t || // delete(void*, align_val_t)
  340. TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint)
  341. TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong)
  342. TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
  343. TLIFn == LibFunc_ZdaPvSt11align_val_t || // delete[](void*, align_val_t)
  344. TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint)
  345. TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
  346. TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
  347. TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
  348. TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint)
  349. TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
  350. TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
  351. TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow)
  352. ExpectedNumParams = 2;
  353. else if (TLIFn == LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t || // delete(void*, align_val_t, nothrow)
  354. TLIFn == LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t) // delete[](void*, align_val_t, nothrow)
  355. ExpectedNumParams = 3;
  356. else
  357. return nullptr;
  358. // Check free prototype.
  359. // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
  360. // attribute will exist.
  361. FunctionType *FTy = Callee->getFunctionType();
  362. if (!FTy->getReturnType()->isVoidTy())
  363. return nullptr;
  364. if (FTy->getNumParams() != ExpectedNumParams)
  365. return nullptr;
  366. if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
  367. return nullptr;
  368. return dyn_cast<CallInst>(I);
  369. }
  370. //===----------------------------------------------------------------------===//
  371. // Utility functions to compute size of objects.
  372. //
  373. static APInt getSizeWithOverflow(const SizeOffsetType &Data) {
  374. if (Data.second.isNegative() || Data.first.ult(Data.second))
  375. return APInt(Data.first.getBitWidth(), 0);
  376. return Data.first - Data.second;
  377. }
  378. /// Compute the size of the object pointed by Ptr. Returns true and the
  379. /// object size in Size if successful, and false otherwise.
  380. /// If RoundToAlign is true, then Size is rounded up to the alignment of
  381. /// allocas, byval arguments, and global variables.
  382. bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
  383. const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
  384. ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
  385. SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
  386. if (!Visitor.bothKnown(Data))
  387. return false;
  388. Size = getSizeWithOverflow(Data).getZExtValue();
  389. return true;
  390. }
  391. ConstantInt *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
  392. const DataLayout &DL,
  393. const TargetLibraryInfo *TLI,
  394. bool MustSucceed) {
  395. assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
  396. "ObjectSize must be a call to llvm.objectsize!");
  397. bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
  398. ObjectSizeOpts EvalOptions;
  399. // Unless we have to fold this to something, try to be as accurate as
  400. // possible.
  401. if (MustSucceed)
  402. EvalOptions.EvalMode =
  403. MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
  404. else
  405. EvalOptions.EvalMode = ObjectSizeOpts::Mode::Exact;
  406. EvalOptions.NullIsUnknownSize =
  407. cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
  408. // FIXME: Does it make sense to just return a failure value if the size won't
  409. // fit in the output and `!MustSucceed`?
  410. uint64_t Size;
  411. auto *ResultType = cast<IntegerType>(ObjectSize->getType());
  412. if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
  413. isUIntN(ResultType->getBitWidth(), Size))
  414. return ConstantInt::get(ResultType, Size);
  415. if (!MustSucceed)
  416. return nullptr;
  417. return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
  418. }
  419. STATISTIC(ObjectVisitorArgument,
  420. "Number of arguments with unsolved size and offset");
  421. STATISTIC(ObjectVisitorLoad,
  422. "Number of load instructions with unsolved size and offset");
  423. APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
  424. if (Options.RoundToAlign && Align)
  425. return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align));
  426. return Size;
  427. }
  428. ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
  429. const TargetLibraryInfo *TLI,
  430. LLVMContext &Context,
  431. ObjectSizeOpts Options)
  432. : DL(DL), TLI(TLI), Options(Options) {
  433. // Pointer size must be rechecked for each object visited since it could have
  434. // a different address space.
  435. }
  436. SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
  437. IntTyBits = DL.getPointerTypeSizeInBits(V->getType());
  438. Zero = APInt::getNullValue(IntTyBits);
  439. V = V->stripPointerCasts();
  440. if (Instruction *I = dyn_cast<Instruction>(V)) {
  441. // If we have already seen this instruction, bail out. Cycles can happen in
  442. // unreachable code after constant propagation.
  443. if (!SeenInsts.insert(I).second)
  444. return unknown();
  445. if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
  446. return visitGEPOperator(*GEP);
  447. return visit(*I);
  448. }
  449. if (Argument *A = dyn_cast<Argument>(V))
  450. return visitArgument(*A);
  451. if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
  452. return visitConstantPointerNull(*P);
  453. if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
  454. return visitGlobalAlias(*GA);
  455. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
  456. return visitGlobalVariable(*GV);
  457. if (UndefValue *UV = dyn_cast<UndefValue>(V))
  458. return visitUndefValue(*UV);
  459. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  460. if (CE->getOpcode() == Instruction::IntToPtr)
  461. return unknown(); // clueless
  462. if (CE->getOpcode() == Instruction::GetElementPtr)
  463. return visitGEPOperator(cast<GEPOperator>(*CE));
  464. }
  465. LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: "
  466. << *V << '\n');
  467. return unknown();
  468. }
  469. /// When we're compiling N-bit code, and the user uses parameters that are
  470. /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
  471. /// trouble with APInt size issues. This function handles resizing + overflow
  472. /// checks for us. Check and zext or trunc \p I depending on IntTyBits and
  473. /// I's value.
  474. bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) {
  475. // More bits than we can handle. Checking the bit width isn't necessary, but
  476. // it's faster than checking active bits, and should give `false` in the
  477. // vast majority of cases.
  478. if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
  479. return false;
  480. if (I.getBitWidth() != IntTyBits)
  481. I = I.zextOrTrunc(IntTyBits);
  482. return true;
  483. }
  484. SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
  485. if (!I.getAllocatedType()->isSized())
  486. return unknown();
  487. APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
  488. if (!I.isArrayAllocation())
  489. return std::make_pair(align(Size, I.getAlignment()), Zero);
  490. Value *ArraySize = I.getArraySize();
  491. if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
  492. APInt NumElems = C->getValue();
  493. if (!CheckedZextOrTrunc(NumElems))
  494. return unknown();
  495. bool Overflow;
  496. Size = Size.umul_ov(NumElems, Overflow);
  497. return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()),
  498. Zero);
  499. }
  500. return unknown();
  501. }
  502. SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
  503. // No interprocedural analysis is done at the moment.
  504. if (!A.hasByValOrInAllocaAttr()) {
  505. ++ObjectVisitorArgument;
  506. return unknown();
  507. }
  508. PointerType *PT = cast<PointerType>(A.getType());
  509. APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
  510. return std::make_pair(align(Size, A.getParamAlignment()), Zero);
  511. }
  512. SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
  513. Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
  514. if (!FnData)
  515. return unknown();
  516. // Handle strdup-like functions separately.
  517. if (FnData->AllocTy == StrDupLike) {
  518. APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
  519. if (!Size)
  520. return unknown();
  521. // Strndup limits strlen.
  522. if (FnData->FstParam > 0) {
  523. ConstantInt *Arg =
  524. dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
  525. if (!Arg)
  526. return unknown();
  527. APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
  528. if (Size.ugt(MaxSize))
  529. Size = MaxSize + 1;
  530. }
  531. return std::make_pair(Size, Zero);
  532. }
  533. ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
  534. if (!Arg)
  535. return unknown();
  536. APInt Size = Arg->getValue();
  537. if (!CheckedZextOrTrunc(Size))
  538. return unknown();
  539. // Size is determined by just 1 parameter.
  540. if (FnData->SndParam < 0)
  541. return std::make_pair(Size, Zero);
  542. Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
  543. if (!Arg)
  544. return unknown();
  545. APInt NumElems = Arg->getValue();
  546. if (!CheckedZextOrTrunc(NumElems))
  547. return unknown();
  548. bool Overflow;
  549. Size = Size.umul_ov(NumElems, Overflow);
  550. return Overflow ? unknown() : std::make_pair(Size, Zero);
  551. // TODO: handle more standard functions (+ wchar cousins):
  552. // - strdup / strndup
  553. // - strcpy / strncpy
  554. // - strcat / strncat
  555. // - memcpy / memmove
  556. // - strcat / strncat
  557. // - memset
  558. }
  559. SizeOffsetType
  560. ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) {
  561. // If null is unknown, there's nothing we can do. Additionally, non-zero
  562. // address spaces can make use of null, so we don't presume to know anything
  563. // about that.
  564. //
  565. // TODO: How should this work with address space casts? We currently just drop
  566. // them on the floor, but it's unclear what we should do when a NULL from
  567. // addrspace(1) gets casted to addrspace(0) (or vice-versa).
  568. if (Options.NullIsUnknownSize || CPN.getType()->getAddressSpace())
  569. return unknown();
  570. return std::make_pair(Zero, Zero);
  571. }
  572. SizeOffsetType
  573. ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
  574. return unknown();
  575. }
  576. SizeOffsetType
  577. ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
  578. // Easy cases were already folded by previous passes.
  579. return unknown();
  580. }
  581. SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
  582. SizeOffsetType PtrData = compute(GEP.getPointerOperand());
  583. APInt Offset(IntTyBits, 0);
  584. if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
  585. return unknown();
  586. return std::make_pair(PtrData.first, PtrData.second + Offset);
  587. }
  588. SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
  589. if (GA.isInterposable())
  590. return unknown();
  591. return compute(GA.getAliasee());
  592. }
  593. SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
  594. if (!GV.hasDefinitiveInitializer())
  595. return unknown();
  596. APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getType()->getElementType()));
  597. return std::make_pair(align(Size, GV.getAlignment()), Zero);
  598. }
  599. SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
  600. // clueless
  601. return unknown();
  602. }
  603. SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
  604. ++ObjectVisitorLoad;
  605. return unknown();
  606. }
  607. SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
  608. // too complex to analyze statically.
  609. return unknown();
  610. }
  611. SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
  612. SizeOffsetType TrueSide = compute(I.getTrueValue());
  613. SizeOffsetType FalseSide = compute(I.getFalseValue());
  614. if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
  615. if (TrueSide == FalseSide) {
  616. return TrueSide;
  617. }
  618. APInt TrueResult = getSizeWithOverflow(TrueSide);
  619. APInt FalseResult = getSizeWithOverflow(FalseSide);
  620. if (TrueResult == FalseResult) {
  621. return TrueSide;
  622. }
  623. if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
  624. if (TrueResult.slt(FalseResult))
  625. return TrueSide;
  626. return FalseSide;
  627. }
  628. if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
  629. if (TrueResult.sgt(FalseResult))
  630. return TrueSide;
  631. return FalseSide;
  632. }
  633. }
  634. return unknown();
  635. }
  636. SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
  637. return std::make_pair(Zero, Zero);
  638. }
  639. SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
  640. LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I
  641. << '\n');
  642. return unknown();
  643. }
  644. ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(
  645. const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
  646. bool RoundToAlign)
  647. : DL(DL), TLI(TLI), Context(Context), Builder(Context, TargetFolder(DL)),
  648. RoundToAlign(RoundToAlign) {
  649. // IntTy and Zero must be set for each compute() since the address space may
  650. // be different for later objects.
  651. }
  652. SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
  653. // XXX - Are vectors of pointers possible here?
  654. IntTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
  655. Zero = ConstantInt::get(IntTy, 0);
  656. SizeOffsetEvalType Result = compute_(V);
  657. if (!bothKnown(Result)) {
  658. // Erase everything that was computed in this iteration from the cache, so
  659. // that no dangling references are left behind. We could be a bit smarter if
  660. // we kept a dependency graph. It's probably not worth the complexity.
  661. for (const Value *SeenVal : SeenVals) {
  662. CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
  663. // non-computable results can be safely cached
  664. if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
  665. CacheMap.erase(CacheIt);
  666. }
  667. }
  668. SeenVals.clear();
  669. return Result;
  670. }
  671. SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
  672. ObjectSizeOpts ObjSizeOptions;
  673. ObjSizeOptions.RoundToAlign = RoundToAlign;
  674. ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, ObjSizeOptions);
  675. SizeOffsetType Const = Visitor.compute(V);
  676. if (Visitor.bothKnown(Const))
  677. return std::make_pair(ConstantInt::get(Context, Const.first),
  678. ConstantInt::get(Context, Const.second));
  679. V = V->stripPointerCasts();
  680. // Check cache.
  681. CacheMapTy::iterator CacheIt = CacheMap.find(V);
  682. if (CacheIt != CacheMap.end())
  683. return CacheIt->second;
  684. // Always generate code immediately before the instruction being
  685. // processed, so that the generated code dominates the same BBs.
  686. BuilderTy::InsertPointGuard Guard(Builder);
  687. if (Instruction *I = dyn_cast<Instruction>(V))
  688. Builder.SetInsertPoint(I);
  689. // Now compute the size and offset.
  690. SizeOffsetEvalType Result;
  691. // Record the pointers that were handled in this run, so that they can be
  692. // cleaned later if something fails. We also use this set to break cycles that
  693. // can occur in dead code.
  694. if (!SeenVals.insert(V).second) {
  695. Result = unknown();
  696. } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
  697. Result = visitGEPOperator(*GEP);
  698. } else if (Instruction *I = dyn_cast<Instruction>(V)) {
  699. Result = visit(*I);
  700. } else if (isa<Argument>(V) ||
  701. (isa<ConstantExpr>(V) &&
  702. cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
  703. isa<GlobalAlias>(V) ||
  704. isa<GlobalVariable>(V)) {
  705. // Ignore values where we cannot do more than ObjectSizeVisitor.
  706. Result = unknown();
  707. } else {
  708. LLVM_DEBUG(
  709. dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: " << *V
  710. << '\n');
  711. Result = unknown();
  712. }
  713. // Don't reuse CacheIt since it may be invalid at this point.
  714. CacheMap[V] = Result;
  715. return Result;
  716. }
  717. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
  718. if (!I.getAllocatedType()->isSized())
  719. return unknown();
  720. // must be a VLA
  721. assert(I.isArrayAllocation());
  722. Value *ArraySize = I.getArraySize();
  723. Value *Size = ConstantInt::get(ArraySize->getType(),
  724. DL.getTypeAllocSize(I.getAllocatedType()));
  725. Size = Builder.CreateMul(Size, ArraySize);
  726. return std::make_pair(Size, Zero);
  727. }
  728. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
  729. Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
  730. if (!FnData)
  731. return unknown();
  732. // Handle strdup-like functions separately.
  733. if (FnData->AllocTy == StrDupLike) {
  734. // TODO
  735. return unknown();
  736. }
  737. Value *FirstArg = CS.getArgument(FnData->FstParam);
  738. FirstArg = Builder.CreateZExt(FirstArg, IntTy);
  739. if (FnData->SndParam < 0)
  740. return std::make_pair(FirstArg, Zero);
  741. Value *SecondArg = CS.getArgument(FnData->SndParam);
  742. SecondArg = Builder.CreateZExt(SecondArg, IntTy);
  743. Value *Size = Builder.CreateMul(FirstArg, SecondArg);
  744. return std::make_pair(Size, Zero);
  745. // TODO: handle more standard functions (+ wchar cousins):
  746. // - strdup / strndup
  747. // - strcpy / strncpy
  748. // - strcat / strncat
  749. // - memcpy / memmove
  750. // - strcat / strncat
  751. // - memset
  752. }
  753. SizeOffsetEvalType
  754. ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
  755. return unknown();
  756. }
  757. SizeOffsetEvalType
  758. ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
  759. return unknown();
  760. }
  761. SizeOffsetEvalType
  762. ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
  763. SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
  764. if (!bothKnown(PtrData))
  765. return unknown();
  766. Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
  767. Offset = Builder.CreateAdd(PtrData.second, Offset);
  768. return std::make_pair(PtrData.first, Offset);
  769. }
  770. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
  771. // clueless
  772. return unknown();
  773. }
  774. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
  775. return unknown();
  776. }
  777. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
  778. // Create 2 PHIs: one for size and another for offset.
  779. PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
  780. PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
  781. // Insert right away in the cache to handle recursive PHIs.
  782. CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
  783. // Compute offset/size for each PHI incoming pointer.
  784. for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
  785. Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt());
  786. SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
  787. if (!bothKnown(EdgeData)) {
  788. OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
  789. OffsetPHI->eraseFromParent();
  790. SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
  791. SizePHI->eraseFromParent();
  792. return unknown();
  793. }
  794. SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
  795. OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
  796. }
  797. Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
  798. if ((Tmp = SizePHI->hasConstantValue())) {
  799. Size = Tmp;
  800. SizePHI->replaceAllUsesWith(Size);
  801. SizePHI->eraseFromParent();
  802. }
  803. if ((Tmp = OffsetPHI->hasConstantValue())) {
  804. Offset = Tmp;
  805. OffsetPHI->replaceAllUsesWith(Offset);
  806. OffsetPHI->eraseFromParent();
  807. }
  808. return std::make_pair(Size, Offset);
  809. }
  810. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
  811. SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
  812. SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
  813. if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
  814. return unknown();
  815. if (TrueSide == FalseSide)
  816. return TrueSide;
  817. Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
  818. FalseSide.first);
  819. Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
  820. FalseSide.second);
  821. return std::make_pair(Size, Offset);
  822. }
  823. SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
  824. LLVM_DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I
  825. << '\n');
  826. return unknown();
  827. }