1//==- llvm/Analysis/MemoryBuiltins.h - Calls to memory builtins --*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This family of functions identifies calls to builtin functions that allocate
10// or free memory.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
15#define LLVM_ANALYSIS_MEMORYBUILTINS_H
16
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Analysis/TargetFolder.h"
21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/InstVisitor.h"
23#include "llvm/IR/ValueHandle.h"
24#include "llvm/Support/Compiler.h"
25#include <cstdint>
26#include <optional>
27#include <utility>
28
29namespace llvm {
30
31class AllocaInst;
32class AAResults;
33class Argument;
34class ConstantPointerNull;
35class DataLayout;
36class ExtractElementInst;
37class ExtractValueInst;
38class GEPOperator;
39class GlobalAlias;
40class GlobalVariable;
41class Instruction;
42class IntegerType;
43class IntrinsicInst;
44class IntToPtrInst;
45class LLVMContext;
46class LoadInst;
47class PHINode;
48class SelectInst;
49class Type;
50class UndefValue;
51class Value;
52
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).
56LLVM_ABI bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI);
57LLVM_ABI bool
58isAllocationFn(const Value *V,
59 function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
60
61/// Tests if a value is a call or invoke to a library function that
62/// allocates memory similar to malloc or calloc.
63LLVM_ABI bool isMallocOrCallocLikeFn(const Value *V,
64 const TargetLibraryInfo *TLI);
65
66/// Tests if a value is a call or invoke to a library function that
67/// allocates memory (either malloc, calloc, or strdup like).
68LLVM_ABI bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI);
69
70/// Tests if a function is a call or invoke to a library function that
71/// reallocates memory (e.g., realloc).
72LLVM_ABI bool isReallocLikeFn(const Function *F);
73
74/// If this is a call to a realloc function, return the reallocated operand.
75LLVM_ABI Value *getReallocatedOperand(const CallBase *CB);
76
77//===----------------------------------------------------------------------===//
78// free Call Utility Functions.
79//
80
81/// isLibFreeFunction - Returns true if the function is a builtin free()
82LLVM_ABI bool isLibFreeFunction(const Function *F, const LibFunc TLIFn);
83
84/// If this if a call to a free function, return the freed operand.
85LLVM_ABI Value *getFreedOperand(const CallBase *CB,
86 const TargetLibraryInfo *TLI);
87
88//===----------------------------------------------------------------------===//
89// Properties of allocation functions
90//
91
92/// Return true if this is a call to an allocation function that does not have
93/// side effects that we are required to preserve beyond the effect of
94/// allocating a new object.
95/// Ex: If our allocation routine has a counter for the number of objects
96/// allocated, and the program prints it on exit, can the value change due
97/// to optimization? Answer is highly language dependent.
98/// Note: *Removable* really does mean removable; it does not mean observable.
99/// A language (e.g. C++) can allow removing allocations without allowing
100/// insertion or speculative execution of allocation routines.
101LLVM_ABI bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI);
102
103/// Gets the alignment argument for an aligned_alloc-like function, using either
104/// built-in knowledge based on fuction names/signatures or allocalign
105/// attributes. Note: the Value returned may not indicate a valid alignment, per
106/// the definition of the allocalign attribute.
107LLVM_ABI Value *getAllocAlignment(const CallBase *V,
108 const TargetLibraryInfo *TLI);
109
110/// Return the size of the requested allocation. With a trivial mapper, this is
111/// similar to calling getObjectSize(..., Exact), but without looking through
112/// calls that return their argument. A mapper function can be used to replace
113/// one Value* (operand to the allocation) with another. This is useful when
114/// doing abstract interpretation.
115LLVM_ABI std::optional<APInt> getAllocSize(
116 const CallBase *CB, const TargetLibraryInfo *TLI,
117 function_ref<const Value *(const Value *)> Mapper = [](const Value *V) {
118 return V;
119 });
120
121/// If this is a call to an allocation function that initializes memory to a
122/// fixed value, return said value in the requested type. Otherwise, return
123/// nullptr.
124LLVM_ABI Constant *getInitialValueOfAllocation(const Value *V,
125 const TargetLibraryInfo *TLI,
126 Type *Ty);
127
128/// If a function is part of an allocation family (e.g.
129/// malloc/realloc/calloc/free), return the identifier for its family
130/// of functions.
131LLVM_ABI std::optional<StringRef>
132getAllocationFamily(const Value *I, const TargetLibraryInfo *TLI);
133
134//===----------------------------------------------------------------------===//
135// Utility functions to compute size of objects.
136//
137
138/// Various options to control the behavior of getObjectSize.
139struct ObjectSizeOpts {
140 /// Controls how we handle conditional statements with unknown conditions.
141 enum class Mode : uint8_t {
142 /// All branches must be known and have the same size, starting from the
143 /// offset, to be merged.
144 ExactSizeFromOffset,
145 /// All branches must be known and have the same underlying size and offset
146 /// to be merged.
147 ExactUnderlyingSizeAndOffset,
148 /// Evaluate all branches of an unknown condition. If all evaluations
149 /// succeed, pick the minimum size.
150 Min,
151 /// Same as Min, except we pick the maximum size of all of the branches.
152 Max,
153 };
154
155 /// How we want to evaluate this object's size.
156 Mode EvalMode = Mode::ExactSizeFromOffset;
157 /// Whether to round the result up to the alignment of allocas, byval
158 /// arguments, and global variables.
159 bool RoundToAlign = false;
160 /// If this is true, null pointers in address space 0 will be treated as
161 /// though they can't be evaluated. Otherwise, null is always considered to
162 /// point to a 0 byte region of memory.
163 bool NullIsUnknownSize = false;
164 /// If set, used for more accurate evaluation
165 AAResults *AA = nullptr;
166};
167
168/// Compute the size of the object pointed by Ptr. Returns true and the
169/// object size in Size if successful, and false otherwise. In this context, by
170/// object we mean the region of memory starting at Ptr to the end of the
171/// underlying object pointed to by Ptr.
172///
173/// WARNING: The object size returned is the allocation size. This does not
174/// imply dereferenceability at site of use since the object may be freeed in
175/// between.
176LLVM_ABI bool getObjectSize(const Value *Ptr, uint64_t &Size,
177 const DataLayout &DL, const TargetLibraryInfo *TLI,
178 ObjectSizeOpts Opts = {});
179
180/// Like getObjectSize(), but only returns the size of base objects (like
181/// allocas, global variables and allocator calls) and std::nullopt otherwise.
182/// Requires ExactSizeFromOffset mode.
183LLVM_ABI std::optional<TypeSize> getBaseObjectSize(const Value *Ptr,
184 const DataLayout &DL,
185 const TargetLibraryInfo *TLI,
186 ObjectSizeOpts Opts = {});
187
188/// Try to turn a call to \@llvm.objectsize into an integer value of the given
189/// Type. Returns null on failure. If MustSucceed is true, this function will
190/// not return null, and may return conservative values governed by the second
191/// argument of the call to objectsize.
192LLVM_ABI Value *lowerObjectSizeCall(IntrinsicInst *ObjectSize,
193 const DataLayout &DL,
194 const TargetLibraryInfo *TLI,
195 bool MustSucceed);
196LLVM_ABI Value *lowerObjectSizeCall(
197 IntrinsicInst *ObjectSize, const DataLayout &DL,
198 const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed,
199 SmallVectorImpl<Instruction *> *InsertedInstructions = nullptr);
200
201/// SizeOffsetType - A base template class for the object size visitors. Used
202/// here as a self-documenting way to handle the values rather than using a
203/// \p std::pair.
204template <typename T, class C> struct SizeOffsetType {
205public:
206 T Size;
207 T Offset;
208
209 SizeOffsetType() = default;
210 SizeOffsetType(T Size, T Offset)
211 : Size(std::move(Size)), Offset(std::move(Offset)) {}
212
213 bool knownSize() const { return C::known(Size); }
214 bool knownOffset() const { return C::known(Offset); }
215 bool anyKnown() const { return knownSize() || knownOffset(); }
216 bool bothKnown() const { return knownSize() && knownOffset(); }
217
218 bool operator==(const SizeOffsetType<T, C> &RHS) const {
219 return Size == RHS.Size && Offset == RHS.Offset;
220 }
221 bool operator!=(const SizeOffsetType<T, C> &RHS) const {
222 return !(*this == RHS);
223 }
224};
225
226/// SizeOffsetAPInt - Used by \p ObjectSizeOffsetVisitor, which works with
227/// \p APInts.
228struct SizeOffsetAPInt : public SizeOffsetType<APInt, SizeOffsetAPInt> {
229 SizeOffsetAPInt() = default;
230 SizeOffsetAPInt(APInt Size, APInt Offset)
231 : SizeOffsetType(std::move(Size), std::move(Offset)) {}
232
233 static bool known(const APInt &V) { return V.getBitWidth() > 1; }
234};
235
236/// OffsetSpan - Used internally by \p ObjectSizeOffsetVisitor. Represents a
237/// point in memory as a pair of allocated bytes before and after it.
238///
239/// \c Before and \c After fields are signed values. It makes it possible to
240/// represent out-of-bound access, e.g. as a result of a GEP, at the expense of
241/// not being able to represent very large allocation.
242struct OffsetSpan {
243 APInt Before; /// Number of allocated bytes before this point.
244 APInt After; /// Number of allocated bytes after this point.
245
246 OffsetSpan() = default;
247 OffsetSpan(APInt Before, APInt After) : Before(Before), After(After) {}
248
249 bool knownBefore() const { return known(V: Before); }
250 bool knownAfter() const { return known(V: After); }
251 bool anyKnown() const { return knownBefore() || knownAfter(); }
252 bool bothKnown() const { return knownBefore() && knownAfter(); }
253
254 bool operator==(const OffsetSpan &RHS) const {
255 return Before == RHS.Before && After == RHS.After;
256 }
257 bool operator!=(const OffsetSpan &RHS) const { return !(*this == RHS); }
258
259 static bool known(const APInt &V) { return V.getBitWidth() > 1; }
260};
261
262/// Evaluate the size and offset of an object pointed to by a Value*
263/// statically. Fails if size or offset are not known at compile time.
264class ObjectSizeOffsetVisitor
265 : public InstVisitor<ObjectSizeOffsetVisitor, OffsetSpan> {
266 const DataLayout &DL;
267 const TargetLibraryInfo *TLI;
268 ObjectSizeOpts Options;
269 unsigned IntTyBits;
270 APInt Zero;
271 SmallDenseMap<Instruction *, OffsetSpan, 8> SeenInsts;
272 unsigned InstructionsVisited;
273
274 APInt align(APInt Size, MaybeAlign Align);
275
276 static OffsetSpan unknown() { return OffsetSpan(); }
277
278public:
279 LLVM_ABI ObjectSizeOffsetVisitor(const DataLayout &DL,
280 const TargetLibraryInfo *TLI,
281 LLVMContext &Context,
282 ObjectSizeOpts Options = {});
283
284 LLVM_ABI SizeOffsetAPInt compute(Value *V);
285
286 // These are "private", except they can't actually be made private. Only
287 // compute() should be used by external users.
288 LLVM_ABI OffsetSpan visitAllocaInst(AllocaInst &I);
289 LLVM_ABI OffsetSpan visitArgument(Argument &A);
290 LLVM_ABI OffsetSpan visitCallBase(CallBase &CB);
291 LLVM_ABI OffsetSpan visitConstantPointerNull(ConstantPointerNull &);
292 LLVM_ABI OffsetSpan visitExtractElementInst(ExtractElementInst &I);
293 LLVM_ABI OffsetSpan visitExtractValueInst(ExtractValueInst &I);
294 LLVM_ABI OffsetSpan visitGlobalAlias(GlobalAlias &GA);
295 LLVM_ABI OffsetSpan visitGlobalVariable(GlobalVariable &GV);
296 LLVM_ABI OffsetSpan visitIntToPtrInst(IntToPtrInst &);
297 LLVM_ABI OffsetSpan visitLoadInst(LoadInst &I);
298 LLVM_ABI OffsetSpan visitPHINode(PHINode &);
299 LLVM_ABI OffsetSpan visitSelectInst(SelectInst &I);
300 LLVM_ABI OffsetSpan visitUndefValue(UndefValue &);
301 LLVM_ABI OffsetSpan visitInstruction(Instruction &I);
302
303private:
304 OffsetSpan
305 findLoadOffsetRange(LoadInst &LoadFrom, BasicBlock &BB,
306 BasicBlock::iterator From,
307 SmallDenseMap<BasicBlock *, OffsetSpan, 8> &VisitedBlocks,
308 unsigned &ScannedInstCount);
309 OffsetSpan combineOffsetRange(OffsetSpan LHS, OffsetSpan RHS);
310 OffsetSpan computeImpl(Value *V);
311 OffsetSpan computeValue(Value *V);
312 bool checkedZextOrTrunc(APInt &I);
313};
314
315/// SizeOffsetValue - Used by \p ObjectSizeOffsetEvaluator, which works with
316/// \p Values.
317struct SizeOffsetWeakTrackingVH;
318struct SizeOffsetValue : public SizeOffsetType<Value *, SizeOffsetValue> {
319 SizeOffsetValue() : SizeOffsetType(nullptr, nullptr) {}
320 SizeOffsetValue(Value *Size, Value *Offset) : SizeOffsetType(Size, Offset) {}
321 LLVM_ABI SizeOffsetValue(const SizeOffsetWeakTrackingVH &SOT);
322
323 static bool known(Value *V) { return V != nullptr; }
324};
325
326/// SizeOffsetWeakTrackingVH - Used by \p ObjectSizeOffsetEvaluator in a
327/// \p DenseMap.
328struct SizeOffsetWeakTrackingVH
329 : public SizeOffsetType<WeakTrackingVH, SizeOffsetWeakTrackingVH> {
330 SizeOffsetWeakTrackingVH() : SizeOffsetType(nullptr, nullptr) {}
331 SizeOffsetWeakTrackingVH(Value *Size, Value *Offset)
332 : SizeOffsetType(Size, Offset) {}
333 SizeOffsetWeakTrackingVH(const SizeOffsetValue &SOV)
334 : SizeOffsetType(SOV.Size, SOV.Offset) {}
335
336 static bool known(WeakTrackingVH V) { return V.pointsToAliveValue(); }
337};
338
339/// Evaluate the size and offset of an object pointed to by a Value*.
340/// May create code to compute the result at run-time.
341class ObjectSizeOffsetEvaluator
342 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetValue> {
343 using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
344 using WeakEvalType = SizeOffsetWeakTrackingVH;
345 using CacheMapTy = DenseMap<const Value *, WeakEvalType>;
346 using PtrSetTy = SmallPtrSet<const Value *, 8>;
347
348 const DataLayout &DL;
349 const TargetLibraryInfo *TLI;
350 LLVMContext &Context;
351 BuilderTy Builder;
352 IntegerType *IntTy;
353 Value *Zero;
354 CacheMapTy CacheMap;
355 PtrSetTy SeenVals;
356 ObjectSizeOpts EvalOpts;
357 SmallPtrSet<Instruction *, 8> InsertedInstructions;
358
359 SizeOffsetValue compute_(Value *V);
360
361public:
362 LLVM_ABI ObjectSizeOffsetEvaluator(const DataLayout &DL,
363 const TargetLibraryInfo *TLI,
364 LLVMContext &Context,
365 ObjectSizeOpts EvalOpts = {});
366
367 static SizeOffsetValue unknown() { return SizeOffsetValue(); }
368
369 LLVM_ABI SizeOffsetValue compute(Value *V);
370
371 // The individual instruction visitors should be treated as private.
372 LLVM_ABI SizeOffsetValue visitAllocaInst(AllocaInst &I);
373 LLVM_ABI SizeOffsetValue visitCallBase(CallBase &CB);
374 LLVM_ABI SizeOffsetValue visitExtractElementInst(ExtractElementInst &I);
375 LLVM_ABI SizeOffsetValue visitExtractValueInst(ExtractValueInst &I);
376 LLVM_ABI SizeOffsetValue visitGEPOperator(GEPOperator &GEP);
377 LLVM_ABI SizeOffsetValue visitIntToPtrInst(IntToPtrInst &);
378 LLVM_ABI SizeOffsetValue visitLoadInst(LoadInst &I);
379 LLVM_ABI SizeOffsetValue visitPHINode(PHINode &PHI);
380 LLVM_ABI SizeOffsetValue visitSelectInst(SelectInst &I);
381 LLVM_ABI SizeOffsetValue visitInstruction(Instruction &I);
382};
383
384} // end namespace llvm
385
386#endif // LLVM_ANALYSIS_MEMORYBUILTINS_H
387