MemoryBuiltins.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- llvm/Analysis/MemoryBuiltins.h - Calls to memory builtins --*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This family of functions identifies calls to builtin functions that allocate
  15. // or free memory.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
  19. #define LLVM_ANALYSIS_MEMORYBUILTINS_H
  20. #include "llvm/ADT/APInt.h"
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ADT/SmallPtrSet.h"
  23. #include "llvm/Analysis/TargetFolder.h"
  24. #include "llvm/Analysis/TargetLibraryInfo.h"
  25. #include "llvm/IR/IRBuilder.h"
  26. #include "llvm/IR/InstVisitor.h"
  27. #include "llvm/IR/ValueHandle.h"
  28. #include <cstdint>
  29. #include <utility>
  30. namespace llvm {
  31. class AllocaInst;
  32. class Argument;
  33. class CallInst;
  34. class ConstantInt;
  35. class ConstantPointerNull;
  36. class DataLayout;
  37. class ExtractElementInst;
  38. class ExtractValueInst;
  39. class GEPOperator;
  40. class GlobalAlias;
  41. class GlobalVariable;
  42. class Instruction;
  43. class IntegerType;
  44. class IntrinsicInst;
  45. class IntToPtrInst;
  46. class LLVMContext;
  47. class LoadInst;
  48. class PHINode;
  49. class PointerType;
  50. class SelectInst;
  51. class Type;
  52. class UndefValue;
  53. class Value;
  54. /// Tests if a value is a call or invoke to a library function that
  55. /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
  56. /// like).
  57. bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
  58. bool LookThroughBitCast = false);
  59. bool isAllocationFn(const Value *V,
  60. function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
  61. bool LookThroughBitCast = false);
  62. /// Tests if a value is a call or invoke to a function that returns a
  63. /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
  64. bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
  65. bool LookThroughBitCast = false);
  66. /// Tests if a value is a call or invoke to a library function that
  67. /// allocates uninitialized memory (such as malloc).
  68. bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  69. bool LookThroughBitCast = false);
  70. bool isMallocLikeFn(const Value *V,
  71. function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
  72. bool LookThroughBitCast = false);
  73. /// Tests if a value is a call or invoke to a library function that
  74. /// allocates uninitialized memory with alignment (such as aligned_alloc).
  75. bool isAlignedAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  76. bool LookThroughBitCast = false);
  77. bool isAlignedAllocLikeFn(
  78. const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
  79. bool LookThroughBitCast = false);
  80. /// Tests if a value is a call or invoke to a library function that
  81. /// allocates zero-filled memory (such as calloc).
  82. bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  83. bool LookThroughBitCast = false);
  84. /// Tests if a value is a call or invoke to a library function that
  85. /// allocates memory similar to malloc or calloc.
  86. bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  87. bool LookThroughBitCast = false);
  88. /// Tests if a value is a call or invoke to a library function that
  89. /// allocates memory (either malloc, calloc, or strdup like).
  90. bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  91. bool LookThroughBitCast = false);
  92. /// Tests if a value is a call or invoke to a library function that
  93. /// reallocates memory (e.g., realloc).
  94. bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  95. bool LookThroughBitCast = false);
  96. /// Tests if a function is a call or invoke to a library function that
  97. /// reallocates memory (e.g., realloc).
  98. bool isReallocLikeFn(const Function *F, const TargetLibraryInfo *TLI);
  99. /// Tests if a value is a call or invoke to a library function that
  100. /// allocates memory and throws if an allocation failed (e.g., new).
  101. bool isOpNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  102. bool LookThroughBitCast = false);
  103. /// Tests if a value is a call or invoke to a library function that
  104. /// allocates memory (strdup, strndup).
  105. bool isStrdupLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  106. bool LookThroughBitCast = false);
  107. //===----------------------------------------------------------------------===//
  108. // malloc Call Utility Functions.
  109. //
  110. /// extractMallocCall - Returns the corresponding CallInst if the instruction
  111. /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
  112. /// ignore InvokeInst here.
  113. const CallInst *
  114. extractMallocCall(const Value *I,
  115. function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
  116. inline CallInst *
  117. extractMallocCall(Value *I,
  118. function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
  119. return const_cast<CallInst *>(extractMallocCall((const Value *)I, GetTLI));
  120. }
  121. /// getMallocType - Returns the PointerType resulting from the malloc call.
  122. /// The PointerType depends on the number of bitcast uses of the malloc call:
  123. /// 0: PointerType is the malloc calls' return type.
  124. /// 1: PointerType is the bitcast's result type.
  125. /// >1: Unique PointerType cannot be determined, return NULL.
  126. PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
  127. /// getMallocAllocatedType - Returns the Type allocated by malloc call.
  128. /// The Type depends on the number of bitcast uses of the malloc call:
  129. /// 0: PointerType is the malloc calls' return type.
  130. /// 1: PointerType is the bitcast's result type.
  131. /// >1: Unique PointerType cannot be determined, return NULL.
  132. Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
  133. /// getMallocArraySize - Returns the array size of a malloc call. If the
  134. /// argument passed to malloc is a multiple of the size of the malloced type,
  135. /// then return that multiple. For non-array mallocs, the multiple is
  136. /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
  137. /// determined.
  138. Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
  139. const TargetLibraryInfo *TLI,
  140. bool LookThroughSExt = false);
  141. //===----------------------------------------------------------------------===//
  142. // calloc Call Utility Functions.
  143. //
  144. /// extractCallocCall - Returns the corresponding CallInst if the instruction
  145. /// is a calloc call.
  146. const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
  147. inline CallInst *extractCallocCall(Value *I, const TargetLibraryInfo *TLI) {
  148. return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
  149. }
  150. //===----------------------------------------------------------------------===//
  151. // free Call Utility Functions.
  152. //
  153. /// isLibFreeFunction - Returns true if the function is a builtin free()
  154. bool isLibFreeFunction(const Function *F, const LibFunc TLIFn);
  155. /// isFreeCall - Returns non-null if the value is a call to the builtin free()
  156. const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
  157. inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
  158. return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
  159. }
  160. //===----------------------------------------------------------------------===//
  161. // Utility functions to compute size of objects.
  162. //
  163. /// Various options to control the behavior of getObjectSize.
  164. struct ObjectSizeOpts {
  165. /// Controls how we handle conditional statements with unknown conditions.
  166. enum class Mode : uint8_t {
  167. /// Fail to evaluate an unknown condition.
  168. Exact,
  169. /// Evaluate all branches of an unknown condition. If all evaluations
  170. /// succeed, pick the minimum size.
  171. Min,
  172. /// Same as Min, except we pick the maximum size of all of the branches.
  173. Max
  174. };
  175. /// How we want to evaluate this object's size.
  176. Mode EvalMode = Mode::Exact;
  177. /// Whether to round the result up to the alignment of allocas, byval
  178. /// arguments, and global variables.
  179. bool RoundToAlign = false;
  180. /// If this is true, null pointers in address space 0 will be treated as
  181. /// though they can't be evaluated. Otherwise, null is always considered to
  182. /// point to a 0 byte region of memory.
  183. bool NullIsUnknownSize = false;
  184. };
  185. /// Compute the size of the object pointed by Ptr. Returns true and the
  186. /// object size in Size if successful, and false otherwise. In this context, by
  187. /// object we mean the region of memory starting at Ptr to the end of the
  188. /// underlying object pointed to by Ptr.
  189. bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
  190. const TargetLibraryInfo *TLI, ObjectSizeOpts Opts = {});
  191. /// Try to turn a call to \@llvm.objectsize into an integer value of the given
  192. /// Type. Returns null on failure. If MustSucceed is true, this function will
  193. /// not return null, and may return conservative values governed by the second
  194. /// argument of the call to objectsize.
  195. Value *lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL,
  196. const TargetLibraryInfo *TLI, bool MustSucceed);
  197. using SizeOffsetType = std::pair<APInt, APInt>;
  198. /// Evaluate the size and offset of an object pointed to by a Value*
  199. /// statically. Fails if size or offset are not known at compile time.
  200. class ObjectSizeOffsetVisitor
  201. : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
  202. const DataLayout &DL;
  203. const TargetLibraryInfo *TLI;
  204. ObjectSizeOpts Options;
  205. unsigned IntTyBits;
  206. APInt Zero;
  207. SmallPtrSet<Instruction *, 8> SeenInsts;
  208. APInt align(APInt Size, uint64_t Align);
  209. SizeOffsetType unknown() {
  210. return std::make_pair(APInt(), APInt());
  211. }
  212. public:
  213. ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
  214. LLVMContext &Context, ObjectSizeOpts Options = {});
  215. SizeOffsetType compute(Value *V);
  216. static bool knownSize(const SizeOffsetType &SizeOffset) {
  217. return SizeOffset.first.getBitWidth() > 1;
  218. }
  219. static bool knownOffset(const SizeOffsetType &SizeOffset) {
  220. return SizeOffset.second.getBitWidth() > 1;
  221. }
  222. static bool bothKnown(const SizeOffsetType &SizeOffset) {
  223. return knownSize(SizeOffset) && knownOffset(SizeOffset);
  224. }
  225. // These are "private", except they can't actually be made private. Only
  226. // compute() should be used by external users.
  227. SizeOffsetType visitAllocaInst(AllocaInst &I);
  228. SizeOffsetType visitArgument(Argument &A);
  229. SizeOffsetType visitCallBase(CallBase &CB);
  230. SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
  231. SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
  232. SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
  233. SizeOffsetType visitGEPOperator(GEPOperator &GEP);
  234. SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
  235. SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
  236. SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
  237. SizeOffsetType visitLoadInst(LoadInst &I);
  238. SizeOffsetType visitPHINode(PHINode&);
  239. SizeOffsetType visitSelectInst(SelectInst &I);
  240. SizeOffsetType visitUndefValue(UndefValue&);
  241. SizeOffsetType visitInstruction(Instruction &I);
  242. private:
  243. bool CheckedZextOrTrunc(APInt &I);
  244. };
  245. using SizeOffsetEvalType = std::pair<Value *, Value *>;
  246. /// Evaluate the size and offset of an object pointed to by a Value*.
  247. /// May create code to compute the result at run-time.
  248. class ObjectSizeOffsetEvaluator
  249. : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
  250. using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
  251. using WeakEvalType = std::pair<WeakTrackingVH, WeakTrackingVH>;
  252. using CacheMapTy = DenseMap<const Value *, WeakEvalType>;
  253. using PtrSetTy = SmallPtrSet<const Value *, 8>;
  254. const DataLayout &DL;
  255. const TargetLibraryInfo *TLI;
  256. LLVMContext &Context;
  257. BuilderTy Builder;
  258. IntegerType *IntTy;
  259. Value *Zero;
  260. CacheMapTy CacheMap;
  261. PtrSetTy SeenVals;
  262. ObjectSizeOpts EvalOpts;
  263. SmallPtrSet<Instruction *, 8> InsertedInstructions;
  264. SizeOffsetEvalType compute_(Value *V);
  265. public:
  266. static SizeOffsetEvalType unknown() {
  267. return std::make_pair(nullptr, nullptr);
  268. }
  269. ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
  270. LLVMContext &Context, ObjectSizeOpts EvalOpts = {});
  271. SizeOffsetEvalType compute(Value *V);
  272. bool knownSize(SizeOffsetEvalType SizeOffset) {
  273. return SizeOffset.first;
  274. }
  275. bool knownOffset(SizeOffsetEvalType SizeOffset) {
  276. return SizeOffset.second;
  277. }
  278. bool anyKnown(SizeOffsetEvalType SizeOffset) {
  279. return knownSize(SizeOffset) || knownOffset(SizeOffset);
  280. }
  281. bool bothKnown(SizeOffsetEvalType SizeOffset) {
  282. return knownSize(SizeOffset) && knownOffset(SizeOffset);
  283. }
  284. // The individual instruction visitors should be treated as private.
  285. SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
  286. SizeOffsetEvalType visitCallBase(CallBase &CB);
  287. SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
  288. SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
  289. SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
  290. SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
  291. SizeOffsetEvalType visitLoadInst(LoadInst &I);
  292. SizeOffsetEvalType visitPHINode(PHINode &PHI);
  293. SizeOffsetEvalType visitSelectInst(SelectInst &I);
  294. SizeOffsetEvalType visitInstruction(Instruction &I);
  295. };
  296. } // end namespace llvm
  297. #endif // LLVM_ANALYSIS_MEMORYBUILTINS_H
  298. #ifdef __GNUC__
  299. #pragma GCC diagnostic pop
  300. #endif