MemoryBuiltins.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <optional>
  30. #include <utility>
  31. namespace llvm {
  32. class AllocaInst;
  33. class AAResults;
  34. class Argument;
  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 SelectInst;
  50. class Type;
  51. class UndefValue;
  52. class Value;
  53. /// Tests if a value is a call or invoke to a library function that
  54. /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
  55. /// like).
  56. bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI);
  57. bool isAllocationFn(const Value *V,
  58. function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
  59. /// Tests if a value is a call or invoke to a library function that
  60. /// allocates memory via new.
  61. bool isNewLikeFn(const Value *V, const TargetLibraryInfo *TLI);
  62. /// Tests if a value is a call or invoke to a library function that
  63. /// allocates memory similar to malloc or calloc.
  64. bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI);
  65. /// Tests if a value is a call or invoke to a library function that
  66. /// allocates memory (either malloc, calloc, or strdup like).
  67. bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI);
  68. /// Tests if a function is a call or invoke to a library function that
  69. /// reallocates memory (e.g., realloc).
  70. bool isReallocLikeFn(const Function *F);
  71. /// If this is a call to a realloc function, return the reallocated operand.
  72. Value *getReallocatedOperand(const CallBase *CB);
  73. //===----------------------------------------------------------------------===//
  74. // free Call Utility Functions.
  75. //
  76. /// isLibFreeFunction - Returns true if the function is a builtin free()
  77. bool isLibFreeFunction(const Function *F, const LibFunc TLIFn);
  78. /// If this if a call to a free function, return the freed operand.
  79. Value *getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI);
  80. //===----------------------------------------------------------------------===//
  81. // Properties of allocation functions
  82. //
  83. /// Return true if this is a call to an allocation function that does not have
  84. /// side effects that we are required to preserve beyond the effect of
  85. /// allocating a new object.
  86. /// Ex: If our allocation routine has a counter for the number of objects
  87. /// allocated, and the program prints it on exit, can the value change due
  88. /// to optimization? Answer is highly language dependent.
  89. /// Note: *Removable* really does mean removable; it does not mean observable.
  90. /// A language (e.g. C++) can allow removing allocations without allowing
  91. /// insertion or speculative execution of allocation routines.
  92. bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI);
  93. /// Gets the alignment argument for an aligned_alloc-like function, using either
  94. /// built-in knowledge based on fuction names/signatures or allocalign
  95. /// attributes. Note: the Value returned may not indicate a valid alignment, per
  96. /// the definition of the allocalign attribute.
  97. Value *getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI);
  98. /// Return the size of the requested allocation. With a trivial mapper, this is
  99. /// similar to calling getObjectSize(..., Exact), but without looking through
  100. /// calls that return their argument. A mapper function can be used to replace
  101. /// one Value* (operand to the allocation) with another. This is useful when
  102. /// doing abstract interpretation.
  103. std::optional<APInt> getAllocSize(
  104. const CallBase *CB, const TargetLibraryInfo *TLI,
  105. function_ref<const Value *(const Value *)> Mapper = [](const Value *V) {
  106. return V;
  107. });
  108. /// If this is a call to an allocation function that initializes memory to a
  109. /// fixed value, return said value in the requested type. Otherwise, return
  110. /// nullptr.
  111. Constant *getInitialValueOfAllocation(const Value *V,
  112. const TargetLibraryInfo *TLI,
  113. Type *Ty);
  114. /// If a function is part of an allocation family (e.g.
  115. /// malloc/realloc/calloc/free), return the identifier for its family
  116. /// of functions.
  117. std::optional<StringRef> getAllocationFamily(const Value *I,
  118. const TargetLibraryInfo *TLI);
  119. //===----------------------------------------------------------------------===//
  120. // Utility functions to compute size of objects.
  121. //
  122. /// Various options to control the behavior of getObjectSize.
  123. struct ObjectSizeOpts {
  124. /// Controls how we handle conditional statements with unknown conditions.
  125. enum class Mode : uint8_t {
  126. /// All branches must be known and have the same size, starting from the
  127. /// offset, to be merged.
  128. ExactSizeFromOffset,
  129. /// All branches must be known and have the same underlying size and offset
  130. /// to be merged.
  131. ExactUnderlyingSizeAndOffset,
  132. /// Evaluate all branches of an unknown condition. If all evaluations
  133. /// succeed, pick the minimum size.
  134. Min,
  135. /// Same as Min, except we pick the maximum size of all of the branches.
  136. Max,
  137. };
  138. /// How we want to evaluate this object's size.
  139. Mode EvalMode = Mode::ExactSizeFromOffset;
  140. /// Whether to round the result up to the alignment of allocas, byval
  141. /// arguments, and global variables.
  142. bool RoundToAlign = false;
  143. /// If this is true, null pointers in address space 0 will be treated as
  144. /// though they can't be evaluated. Otherwise, null is always considered to
  145. /// point to a 0 byte region of memory.
  146. bool NullIsUnknownSize = false;
  147. /// If set, used for more accurate evaluation
  148. AAResults *AA = nullptr;
  149. };
  150. /// Compute the size of the object pointed by Ptr. Returns true and the
  151. /// object size in Size if successful, and false otherwise. In this context, by
  152. /// object we mean the region of memory starting at Ptr to the end of the
  153. /// underlying object pointed to by Ptr.
  154. ///
  155. /// WARNING: The object size returned is the allocation size. This does not
  156. /// imply dereferenceability at site of use since the object may be freeed in
  157. /// between.
  158. bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
  159. const TargetLibraryInfo *TLI, ObjectSizeOpts Opts = {});
  160. /// Try to turn a call to \@llvm.objectsize into an integer value of the given
  161. /// Type. Returns null on failure. If MustSucceed is true, this function will
  162. /// not return null, and may return conservative values governed by the second
  163. /// argument of the call to objectsize.
  164. Value *lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL,
  165. const TargetLibraryInfo *TLI, bool MustSucceed);
  166. Value *lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL,
  167. const TargetLibraryInfo *TLI, AAResults *AA,
  168. bool MustSucceed);
  169. using SizeOffsetType = std::pair<APInt, APInt>;
  170. /// Evaluate the size and offset of an object pointed to by a Value*
  171. /// statically. Fails if size or offset are not known at compile time.
  172. class ObjectSizeOffsetVisitor
  173. : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
  174. const DataLayout &DL;
  175. const TargetLibraryInfo *TLI;
  176. ObjectSizeOpts Options;
  177. unsigned IntTyBits;
  178. APInt Zero;
  179. SmallPtrSet<Instruction *, 8> SeenInsts;
  180. APInt align(APInt Size, MaybeAlign Align);
  181. SizeOffsetType unknown() {
  182. return std::make_pair(APInt(), APInt());
  183. }
  184. public:
  185. ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
  186. LLVMContext &Context, ObjectSizeOpts Options = {});
  187. SizeOffsetType compute(Value *V);
  188. static bool knownSize(const SizeOffsetType &SizeOffset) {
  189. return SizeOffset.first.getBitWidth() > 1;
  190. }
  191. static bool knownOffset(const SizeOffsetType &SizeOffset) {
  192. return SizeOffset.second.getBitWidth() > 1;
  193. }
  194. static bool bothKnown(const SizeOffsetType &SizeOffset) {
  195. return knownSize(SizeOffset) && knownOffset(SizeOffset);
  196. }
  197. // These are "private", except they can't actually be made private. Only
  198. // compute() should be used by external users.
  199. SizeOffsetType visitAllocaInst(AllocaInst &I);
  200. SizeOffsetType visitArgument(Argument &A);
  201. SizeOffsetType visitCallBase(CallBase &CB);
  202. SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
  203. SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
  204. SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
  205. SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
  206. SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
  207. SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
  208. SizeOffsetType visitLoadInst(LoadInst &I);
  209. SizeOffsetType visitPHINode(PHINode&);
  210. SizeOffsetType visitSelectInst(SelectInst &I);
  211. SizeOffsetType visitUndefValue(UndefValue&);
  212. SizeOffsetType visitInstruction(Instruction &I);
  213. private:
  214. SizeOffsetType findLoadSizeOffset(
  215. LoadInst &LoadFrom, BasicBlock &BB, BasicBlock::iterator From,
  216. SmallDenseMap<BasicBlock *, SizeOffsetType, 8> &VisitedBlocks,
  217. unsigned &ScannedInstCount);
  218. SizeOffsetType combineSizeOffset(SizeOffsetType LHS, SizeOffsetType RHS);
  219. SizeOffsetType computeImpl(Value *V);
  220. bool CheckedZextOrTrunc(APInt &I);
  221. };
  222. using SizeOffsetEvalType = std::pair<Value *, Value *>;
  223. /// Evaluate the size and offset of an object pointed to by a Value*.
  224. /// May create code to compute the result at run-time.
  225. class ObjectSizeOffsetEvaluator
  226. : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
  227. using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
  228. using WeakEvalType = std::pair<WeakTrackingVH, WeakTrackingVH>;
  229. using CacheMapTy = DenseMap<const Value *, WeakEvalType>;
  230. using PtrSetTy = SmallPtrSet<const Value *, 8>;
  231. const DataLayout &DL;
  232. const TargetLibraryInfo *TLI;
  233. LLVMContext &Context;
  234. BuilderTy Builder;
  235. IntegerType *IntTy;
  236. Value *Zero;
  237. CacheMapTy CacheMap;
  238. PtrSetTy SeenVals;
  239. ObjectSizeOpts EvalOpts;
  240. SmallPtrSet<Instruction *, 8> InsertedInstructions;
  241. SizeOffsetEvalType compute_(Value *V);
  242. public:
  243. static SizeOffsetEvalType unknown() {
  244. return std::make_pair(nullptr, nullptr);
  245. }
  246. ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
  247. LLVMContext &Context, ObjectSizeOpts EvalOpts = {});
  248. SizeOffsetEvalType compute(Value *V);
  249. bool knownSize(SizeOffsetEvalType SizeOffset) {
  250. return SizeOffset.first;
  251. }
  252. bool knownOffset(SizeOffsetEvalType SizeOffset) {
  253. return SizeOffset.second;
  254. }
  255. bool anyKnown(SizeOffsetEvalType SizeOffset) {
  256. return knownSize(SizeOffset) || knownOffset(SizeOffset);
  257. }
  258. bool bothKnown(SizeOffsetEvalType SizeOffset) {
  259. return knownSize(SizeOffset) && knownOffset(SizeOffset);
  260. }
  261. // The individual instruction visitors should be treated as private.
  262. SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
  263. SizeOffsetEvalType visitCallBase(CallBase &CB);
  264. SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
  265. SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
  266. SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
  267. SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
  268. SizeOffsetEvalType visitLoadInst(LoadInst &I);
  269. SizeOffsetEvalType visitPHINode(PHINode &PHI);
  270. SizeOffsetEvalType visitSelectInst(SelectInst &I);
  271. SizeOffsetEvalType visitInstruction(Instruction &I);
  272. };
  273. } // end namespace llvm
  274. #endif // LLVM_ANALYSIS_MEMORYBUILTINS_H
  275. #ifdef __GNUC__
  276. #pragma GCC diagnostic pop
  277. #endif