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