Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
10 KiB

  1. //===- llvm/Analysis/MemoryBuiltins.h- Calls to memory builtins -*- C++ -*-===//
  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. #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
  15. #define LLVM_ANALYSIS_MEMORYBUILTINS_H
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/IR/IRBuilder.h"
  19. #include "llvm/IR/Operator.h"
  20. #include "llvm/InstVisitor.h"
  21. #include "llvm/Support/DataTypes.h"
  22. #include "llvm/Support/TargetFolder.h"
  23. #include "llvm/Support/ValueHandle.h"
  24. namespace llvm {
  25. class CallInst;
  26. class PointerType;
  27. class DataLayout;
  28. class TargetLibraryInfo;
  29. class Type;
  30. class Value;
  31. /// \brief Tests if a value is a call or invoke to a library function that
  32. /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
  33. /// like).
  34. bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
  35. bool LookThroughBitCast = false);
  36. /// \brief Tests if a value is a call or invoke to a function that returns a
  37. /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
  38. bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
  39. bool LookThroughBitCast = false);
  40. /// \brief Tests if a value is a call or invoke to a library function that
  41. /// allocates uninitialized memory (such as malloc).
  42. bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  43. bool LookThroughBitCast = false);
  44. /// \brief Tests if a value is a call or invoke to a library function that
  45. /// allocates zero-filled memory (such as calloc).
  46. bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  47. bool LookThroughBitCast = false);
  48. /// \brief Tests if a value is a call or invoke to a library function that
  49. /// allocates memory (either malloc, calloc, or strdup like).
  50. bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  51. bool LookThroughBitCast = false);
  52. /// \brief Tests if a value is a call or invoke to a library function that
  53. /// reallocates memory (such as realloc).
  54. bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
  55. bool LookThroughBitCast = false);
  56. //===----------------------------------------------------------------------===//
  57. // malloc Call Utility Functions.
  58. //
  59. /// extractMallocCall - Returns the corresponding CallInst if the instruction
  60. /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
  61. /// ignore InvokeInst here.
  62. const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
  63. static inline CallInst *extractMallocCall(Value *I,
  64. const TargetLibraryInfo *TLI) {
  65. return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
  66. }
  67. /// isArrayMalloc - Returns the corresponding CallInst if the instruction
  68. /// is a call to malloc whose array size can be determined and the array size
  69. /// is not constant 1. Otherwise, return NULL.
  70. const CallInst *isArrayMalloc(const Value *I, const DataLayout *TD,
  71. const TargetLibraryInfo *TLI);
  72. /// getMallocType - Returns the PointerType resulting from the malloc call.
  73. /// The PointerType depends on the number of bitcast uses of the malloc call:
  74. /// 0: PointerType is the malloc calls' return type.
  75. /// 1: PointerType is the bitcast's result type.
  76. /// >1: Unique PointerType cannot be determined, return NULL.
  77. PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
  78. /// getMallocAllocatedType - Returns the Type allocated by malloc call.
  79. /// The Type depends on the number of bitcast uses of the malloc call:
  80. /// 0: PointerType is the malloc calls' return type.
  81. /// 1: PointerType is the bitcast's result type.
  82. /// >1: Unique PointerType cannot be determined, return NULL.
  83. Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
  84. /// getMallocArraySize - Returns the array size of a malloc call. If the
  85. /// argument passed to malloc is a multiple of the size of the malloced type,
  86. /// then return that multiple. For non-array mallocs, the multiple is
  87. /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
  88. /// determined.
  89. Value *getMallocArraySize(CallInst *CI, const DataLayout *TD,
  90. const TargetLibraryInfo *TLI,
  91. bool LookThroughSExt = false);
  92. //===----------------------------------------------------------------------===//
  93. // calloc Call Utility Functions.
  94. //
  95. /// extractCallocCall - Returns the corresponding CallInst if the instruction
  96. /// is a calloc call.
  97. const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
  98. static inline CallInst *extractCallocCall(Value *I,
  99. const TargetLibraryInfo *TLI) {
  100. return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
  101. }
  102. //===----------------------------------------------------------------------===//
  103. // free Call Utility Functions.
  104. //
  105. /// isFreeCall - Returns non-null if the value is a call to the builtin free()
  106. const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
  107. static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
  108. return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
  109. }
  110. //===----------------------------------------------------------------------===//
  111. // Utility functions to compute size of objects.
  112. //
  113. /// \brief Compute the size of the object pointed by Ptr. Returns true and the
  114. /// object size in Size if successful, and false otherwise. In this context, by
  115. /// object we mean the region of memory starting at Ptr to the end of the
  116. /// underlying object pointed to by Ptr.
  117. /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
  118. /// byval arguments, and global variables.
  119. bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
  120. const TargetLibraryInfo *TLI, bool RoundToAlign = false);
  121. typedef std::pair<APInt, APInt> SizeOffsetType;
  122. /// \brief Evaluate the size and offset of an object ponted by a Value*
  123. /// statically. Fails if size or offset are not known at compile time.
  124. class ObjectSizeOffsetVisitor
  125. : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
  126. const DataLayout *TD;
  127. const TargetLibraryInfo *TLI;
  128. bool RoundToAlign;
  129. unsigned IntTyBits;
  130. APInt Zero;
  131. SmallPtrSet<Instruction *, 8> SeenInsts;
  132. APInt align(APInt Size, uint64_t Align);
  133. SizeOffsetType unknown() {
  134. return std::make_pair(APInt(), APInt());
  135. }
  136. public:
  137. ObjectSizeOffsetVisitor(const DataLayout *TD, const TargetLibraryInfo *TLI,
  138. LLVMContext &Context, bool RoundToAlign = false);
  139. SizeOffsetType compute(Value *V);
  140. bool knownSize(SizeOffsetType &SizeOffset) {
  141. return SizeOffset.first.getBitWidth() > 1;
  142. }
  143. bool knownOffset(SizeOffsetType &SizeOffset) {
  144. return SizeOffset.second.getBitWidth() > 1;
  145. }
  146. bool bothKnown(SizeOffsetType &SizeOffset) {
  147. return knownSize(SizeOffset) && knownOffset(SizeOffset);
  148. }
  149. SizeOffsetType visitAllocaInst(AllocaInst &I);
  150. SizeOffsetType visitArgument(Argument &A);
  151. SizeOffsetType visitCallSite(CallSite CS);
  152. SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
  153. SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
  154. SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
  155. SizeOffsetType visitGEPOperator(GEPOperator &GEP);
  156. SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
  157. SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
  158. SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
  159. SizeOffsetType visitLoadInst(LoadInst &I);
  160. SizeOffsetType visitPHINode(PHINode&);
  161. SizeOffsetType visitSelectInst(SelectInst &I);
  162. SizeOffsetType visitUndefValue(UndefValue&);
  163. SizeOffsetType visitInstruction(Instruction &I);
  164. };
  165. typedef std::pair<Value*, Value*> SizeOffsetEvalType;
  166. /// \brief Evaluate the size and offset of an object ponted by a Value*.
  167. /// May create code to compute the result at run-time.
  168. class ObjectSizeOffsetEvaluator
  169. : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
  170. typedef IRBuilder<true, TargetFolder> BuilderTy;
  171. typedef std::pair<WeakVH, WeakVH> WeakEvalType;
  172. typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
  173. typedef SmallPtrSet<const Value*, 8> PtrSetTy;
  174. const DataLayout *TD;
  175. const TargetLibraryInfo *TLI;
  176. LLVMContext &Context;
  177. BuilderTy Builder;
  178. IntegerType *IntTy;
  179. Value *Zero;
  180. CacheMapTy CacheMap;
  181. PtrSetTy SeenVals;
  182. SizeOffsetEvalType unknown() {
  183. return std::make_pair((Value*)0, (Value*)0);
  184. }
  185. SizeOffsetEvalType compute_(Value *V);
  186. public:
  187. ObjectSizeOffsetEvaluator(const DataLayout *TD, const TargetLibraryInfo *TLI,
  188. LLVMContext &Context);
  189. SizeOffsetEvalType compute(Value *V);
  190. bool knownSize(SizeOffsetEvalType SizeOffset) {
  191. return SizeOffset.first;
  192. }
  193. bool knownOffset(SizeOffsetEvalType SizeOffset) {
  194. return SizeOffset.second;
  195. }
  196. bool anyKnown(SizeOffsetEvalType SizeOffset) {
  197. return knownSize(SizeOffset) || knownOffset(SizeOffset);
  198. }
  199. bool bothKnown(SizeOffsetEvalType SizeOffset) {
  200. return knownSize(SizeOffset) && knownOffset(SizeOffset);
  201. }
  202. SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
  203. SizeOffsetEvalType visitCallSite(CallSite CS);
  204. SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
  205. SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
  206. SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
  207. SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
  208. SizeOffsetEvalType visitLoadInst(LoadInst &I);
  209. SizeOffsetEvalType visitPHINode(PHINode &PHI);
  210. SizeOffsetEvalType visitSelectInst(SelectInst &I);
  211. SizeOffsetEvalType visitInstruction(Instruction &I);
  212. };
  213. } // End llvm namespace
  214. #endif