1 //===- MemoryBuiltins.cpp - Identify calls to memory builtins -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This family of functions identifies calls to builtin functions that allocate
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/MemoryBuiltins.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Analysis/TargetFolder.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/Utils/Local.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/IR/Argument.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/raw_ostream.h"
50 #define DEBUG_TYPE "memory-builtins"
52 enum AllocType : uint8_t {
53 OpNewLike = 1<<0, // allocates; never returns null
54 MallocLike = 1<<1 | OpNewLike, // allocates; may return null
55 CallocLike = 1<<2, // allocates + bzero
56 ReallocLike = 1<<3, // reallocates
58 MallocOrCallocLike = MallocLike | CallocLike,
59 AllocLike = MallocLike | CallocLike | StrDupLike,
60 AnyAlloc = AllocLike | ReallocLike
66 // First and Second size parameters (or -1 if unused)
67 int FstParam, SndParam;
70 // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
71 // know which functions are nounwind, noalias, nocapture parameters, etc.
72 static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
73 {LibFunc_malloc, {MallocLike, 1, 0, -1}},
74 {LibFunc_valloc, {MallocLike, 1, 0, -1}},
75 {LibFunc_Znwj, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
76 {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
77 {LibFunc_ZnwjSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned int, align_val_t)
78 {LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t, // new(unsigned int, align_val_t, nothrow)
79 {MallocLike, 3, 0, -1}},
80 {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long)
81 {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow)
82 {LibFunc_ZnwmSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned long, align_val_t)
83 {LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t, // new(unsigned long, align_val_t, nothrow)
84 {MallocLike, 3, 0, -1}},
85 {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
86 {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
87 {LibFunc_ZnajSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned int, align_val_t)
88 {LibFunc_ZnajSt11align_val_tRKSt9nothrow_t, // new[](unsigned int, align_val_t, nothrow)
89 {MallocLike, 3, 0, -1}},
90 {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long)
91 {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow)
92 {LibFunc_ZnamSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned long, align_val_t)
93 {LibFunc_ZnamSt11align_val_tRKSt9nothrow_t, // new[](unsigned long, align_val_t, nothrow)
94 {MallocLike, 3, 0, -1}},
95 {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
96 {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
97 {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1}}, // new(unsigned long long)
98 {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned long long, nothrow)
99 {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
100 {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
101 {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
102 {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
103 {LibFunc_calloc, {CallocLike, 2, 0, 1}},
104 {LibFunc_realloc, {ReallocLike, 2, 1, -1}},
105 {LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
106 {LibFunc_strdup, {StrDupLike, 1, -1, -1}},
107 {LibFunc_strndup, {StrDupLike, 2, 1, -1}}
108 // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
111 static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
113 // Don't care about intrinsics in this case.
114 if (isa<IntrinsicInst>(V))
117 if (LookThroughBitCast)
118 V = V->stripPointerCasts();
120 ImmutableCallSite CS(V);
121 if (!CS.getInstruction())
124 IsNoBuiltin = CS.isNoBuiltin();
126 if (const Function *Callee = CS.getCalledFunction())
131 /// Returns the allocation data for the given value if it's either a call to a
132 /// known allocation function, or a call to a function with the allocsize
134 static Optional<AllocFnsTy>
135 getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
136 const TargetLibraryInfo *TLI) {
137 // Make sure that the function is available.
138 StringRef FnName = Callee->getName();
140 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
143 const auto *Iter = find_if(
144 AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
145 return P.first == TLIFn;
148 if (Iter == std::end(AllocationFnData))
151 const AllocFnsTy *FnData = &Iter->second;
152 if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
155 // Check function prototype.
156 int FstParam = FnData->FstParam;
157 int SndParam = FnData->SndParam;
158 FunctionType *FTy = Callee->getFunctionType();
160 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
161 FTy->getNumParams() == FnData->NumParams &&
163 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
164 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
166 FTy->getParamType(SndParam)->isIntegerTy(32) ||
167 FTy->getParamType(SndParam)->isIntegerTy(64)))
172 static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
173 const TargetLibraryInfo *TLI,
174 bool LookThroughBitCast = false) {
175 bool IsNoBuiltinCall;
176 if (const Function *Callee =
177 getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
178 if (!IsNoBuiltinCall)
179 return getAllocationDataForFunction(Callee, AllocTy, TLI);
183 static Optional<AllocFnsTy>
184 getAllocationData(const Value *V, AllocType AllocTy,
185 function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
186 bool LookThroughBitCast = false) {
187 bool IsNoBuiltinCall;
188 if (const Function *Callee =
189 getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
190 if (!IsNoBuiltinCall)
191 return getAllocationDataForFunction(
192 Callee, AllocTy, &GetTLI(const_cast<Function &>(*Callee)));
196 static Optional<AllocFnsTy> getAllocationSize(const Value *V,
197 const TargetLibraryInfo *TLI) {
198 bool IsNoBuiltinCall;
199 const Function *Callee =
200 getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
204 // Prefer to use existing information over allocsize. This will give us an
206 if (!IsNoBuiltinCall)
207 if (Optional<AllocFnsTy> Data =
208 getAllocationDataForFunction(Callee, AnyAlloc, TLI))
211 Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
212 if (Attr == Attribute())
215 std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
218 // Because allocsize only tells us how many bytes are allocated, we're not
219 // really allowed to assume anything, so we use MallocLike.
220 Result.AllocTy = MallocLike;
221 Result.NumParams = Callee->getNumOperands();
222 Result.FstParam = Args.first;
223 Result.SndParam = Args.second.getValueOr(-1);
227 static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
228 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
229 return CS && CS.hasRetAttr(Attribute::NoAlias);
232 /// Tests if a value is a call or invoke to a library function that
233 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
235 bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
236 bool LookThroughBitCast) {
237 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue();
239 bool llvm::isAllocationFn(
240 const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
241 bool LookThroughBitCast) {
242 return getAllocationData(V, AnyAlloc, GetTLI, LookThroughBitCast).hasValue();
245 /// Tests if a value is a call or invoke to a function that returns a
246 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
247 bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
248 bool LookThroughBitCast) {
249 // it's safe to consider realloc as noalias since accessing the original
250 // pointer is undefined behavior
251 return isAllocationFn(V, TLI, LookThroughBitCast) ||
252 hasNoAliasAttr(V, LookThroughBitCast);
255 /// Tests if a value is a call or invoke to a library function that
256 /// allocates uninitialized memory (such as malloc).
257 bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
258 bool LookThroughBitCast) {
259 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue();
261 bool llvm::isMallocLikeFn(
262 const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
263 bool LookThroughBitCast) {
264 return getAllocationData(V, MallocLike, GetTLI, LookThroughBitCast)
268 /// Tests if a value is a call or invoke to a library function that
269 /// allocates zero-filled memory (such as calloc).
270 bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
271 bool LookThroughBitCast) {
272 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue();
275 /// Tests if a value is a call or invoke to a library function that
276 /// allocates memory similar to malloc or calloc.
277 bool llvm::isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
278 bool LookThroughBitCast) {
279 return getAllocationData(V, MallocOrCallocLike, TLI,
280 LookThroughBitCast).hasValue();
283 /// Tests if a value is a call or invoke to a library function that
284 /// allocates memory (either malloc, calloc, or strdup like).
285 bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
286 bool LookThroughBitCast) {
287 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue();
290 /// Tests if a value is a call or invoke to a library function that
291 /// reallocates memory (e.g., realloc).
292 bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
293 bool LookThroughBitCast) {
294 return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast).hasValue();
297 /// Tests if a functions is a call or invoke to a library function that
298 /// reallocates memory (e.g., realloc).
299 bool llvm::isReallocLikeFn(const Function *F, const TargetLibraryInfo *TLI) {
300 return getAllocationDataForFunction(F, ReallocLike, TLI).hasValue();
303 /// Tests if a value is a call or invoke to a library function that
304 /// allocates memory and throws if an allocation failed (e.g., new).
305 bool llvm::isOpNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
306 bool LookThroughBitCast) {
307 return getAllocationData(V, OpNewLike, TLI, LookThroughBitCast).hasValue();
310 /// Tests if a value is a call or invoke to a library function that
311 /// allocates memory (strdup, strndup).
312 bool llvm::isStrdupLikeFn(const Value *V, const TargetLibraryInfo *TLI,
313 bool LookThroughBitCast) {
314 return getAllocationData(V, StrDupLike, TLI, LookThroughBitCast).hasValue();
317 /// extractMallocCall - Returns the corresponding CallInst if the instruction
318 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
319 /// ignore InvokeInst here.
320 const CallInst *llvm::extractMallocCall(
322 function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
323 return isMallocLikeFn(I, GetTLI) ? dyn_cast<CallInst>(I) : nullptr;
326 static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
327 const TargetLibraryInfo *TLI,
328 bool LookThroughSExt = false) {
332 // The size of the malloc's result type must be known to determine array size.
333 Type *T = getMallocAllocatedType(CI, TLI);
334 if (!T || !T->isSized())
337 unsigned ElementSize = DL.getTypeAllocSize(T);
338 if (StructType *ST = dyn_cast<StructType>(T))
339 ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
341 // If malloc call's arg can be determined to be a multiple of ElementSize,
342 // return the multiple. Otherwise, return NULL.
343 Value *MallocArg = CI->getArgOperand(0);
344 Value *Multiple = nullptr;
345 if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
351 /// getMallocType - Returns the PointerType resulting from the malloc call.
352 /// The PointerType depends on the number of bitcast uses of the malloc call:
353 /// 0: PointerType is the calls' return type.
354 /// 1: PointerType is the bitcast's result type.
355 /// >1: Unique PointerType cannot be determined, return NULL.
356 PointerType *llvm::getMallocType(const CallInst *CI,
357 const TargetLibraryInfo *TLI) {
358 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
360 PointerType *MallocType = nullptr;
361 unsigned NumOfBitCastUses = 0;
363 // Determine if CallInst has a bitcast use.
364 for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
366 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
367 MallocType = cast<PointerType>(BCI->getDestTy());
371 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
372 if (NumOfBitCastUses == 1)
375 // Malloc call was not bitcast, so type is the malloc function's return type.
376 if (NumOfBitCastUses == 0)
377 return cast<PointerType>(CI->getType());
379 // Type could not be determined.
383 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
384 /// The Type depends on the number of bitcast uses of the malloc call:
385 /// 0: PointerType is the malloc calls' return type.
386 /// 1: PointerType is the bitcast's result type.
387 /// >1: Unique PointerType cannot be determined, return NULL.
388 Type *llvm::getMallocAllocatedType(const CallInst *CI,
389 const TargetLibraryInfo *TLI) {
390 PointerType *PT = getMallocType(CI, TLI);
391 return PT ? PT->getElementType() : nullptr;
394 /// getMallocArraySize - Returns the array size of a malloc call. If the
395 /// argument passed to malloc is a multiple of the size of the malloced type,
396 /// then return that multiple. For non-array mallocs, the multiple is
397 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
399 Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL,
400 const TargetLibraryInfo *TLI,
401 bool LookThroughSExt) {
402 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
403 return computeArraySize(CI, DL, TLI, LookThroughSExt);
406 /// extractCallocCall - Returns the corresponding CallInst if the instruction
407 /// is a calloc call.
408 const CallInst *llvm::extractCallocCall(const Value *I,
409 const TargetLibraryInfo *TLI) {
410 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
413 /// isLibFreeFunction - Returns true if the function is a builtin free()
414 bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) {
415 unsigned ExpectedNumParams;
416 if (TLIFn == LibFunc_free ||
417 TLIFn == LibFunc_ZdlPv || // operator delete(void*)
418 TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
419 TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
420 TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
421 TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
422 TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*)
423 ExpectedNumParams = 1;
424 else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint)
425 TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong)
426 TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
427 TLIFn == LibFunc_ZdlPvSt11align_val_t || // delete(void*, align_val_t)
428 TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint)
429 TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong)
430 TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
431 TLIFn == LibFunc_ZdaPvSt11align_val_t || // delete[](void*, align_val_t)
432 TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint)
433 TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
434 TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
435 TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
436 TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint)
437 TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
438 TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
439 TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow)
440 ExpectedNumParams = 2;
441 else if (TLIFn == LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t || // delete(void*, align_val_t, nothrow)
442 TLIFn == LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t) // delete[](void*, align_val_t, nothrow)
443 ExpectedNumParams = 3;
447 // Check free prototype.
448 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
449 // attribute will exist.
450 FunctionType *FTy = F->getFunctionType();
451 if (!FTy->getReturnType()->isVoidTy())
453 if (FTy->getNumParams() != ExpectedNumParams)
455 if (FTy->getParamType(0) != Type::getInt8PtrTy(F->getContext()))
461 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
462 const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
463 bool IsNoBuiltinCall;
464 const Function *Callee =
465 getCalledFunction(I, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
466 if (Callee == nullptr || IsNoBuiltinCall)
469 StringRef FnName = Callee->getName();
471 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
474 return isLibFreeFunction(Callee, TLIFn) ? dyn_cast<CallInst>(I) : nullptr;
478 //===----------------------------------------------------------------------===//
479 // Utility functions to compute size of objects.
481 static APInt getSizeWithOverflow(const SizeOffsetType &Data) {
482 if (Data.second.isNegative() || Data.first.ult(Data.second))
483 return APInt(Data.first.getBitWidth(), 0);
484 return Data.first - Data.second;
487 /// Compute the size of the object pointed by Ptr. Returns true and the
488 /// object size in Size if successful, and false otherwise.
489 /// If RoundToAlign is true, then Size is rounded up to the alignment of
490 /// allocas, byval arguments, and global variables.
491 bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
492 const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
493 ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
494 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
495 if (!Visitor.bothKnown(Data))
498 Size = getSizeWithOverflow(Data).getZExtValue();
502 Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
503 const DataLayout &DL,
504 const TargetLibraryInfo *TLI,
506 assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
507 "ObjectSize must be a call to llvm.objectsize!");
509 bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
510 ObjectSizeOpts EvalOptions;
511 // Unless we have to fold this to something, try to be as accurate as
514 EvalOptions.EvalMode =
515 MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
517 EvalOptions.EvalMode = ObjectSizeOpts::Mode::Exact;
519 EvalOptions.NullIsUnknownSize =
520 cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
522 auto *ResultType = cast<IntegerType>(ObjectSize->getType());
523 bool StaticOnly = cast<ConstantInt>(ObjectSize->getArgOperand(3))->isZero();
525 // FIXME: Does it make sense to just return a failure value if the size won't
526 // fit in the output and `!MustSucceed`?
528 if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
529 isUIntN(ResultType->getBitWidth(), Size))
530 return ConstantInt::get(ResultType, Size);
532 LLVMContext &Ctx = ObjectSize->getFunction()->getContext();
533 ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, EvalOptions);
534 SizeOffsetEvalType SizeOffsetPair =
535 Eval.compute(ObjectSize->getArgOperand(0));
537 if (SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown()) {
538 IRBuilder<TargetFolder> Builder(Ctx, TargetFolder(DL));
539 Builder.SetInsertPoint(ObjectSize);
541 // If we've outside the end of the object, then we can always access
544 Builder.CreateSub(SizeOffsetPair.first, SizeOffsetPair.second);
546 Builder.CreateICmpULT(SizeOffsetPair.first, SizeOffsetPair.second);
547 ResultSize = Builder.CreateZExtOrTrunc(ResultSize, ResultType);
548 return Builder.CreateSelect(UseZero, ConstantInt::get(ResultType, 0),
556 return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
559 STATISTIC(ObjectVisitorArgument,
560 "Number of arguments with unsolved size and offset");
561 STATISTIC(ObjectVisitorLoad,
562 "Number of load instructions with unsolved size and offset");
564 APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Alignment) {
565 if (Options.RoundToAlign && Alignment)
566 return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align(Alignment)));
570 ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
571 const TargetLibraryInfo *TLI,
572 LLVMContext &Context,
573 ObjectSizeOpts Options)
574 : DL(DL), TLI(TLI), Options(Options) {
575 // Pointer size must be rechecked for each object visited since it could have
576 // a different address space.
579 SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
580 IntTyBits = DL.getIndexTypeSizeInBits(V->getType());
581 Zero = APInt::getNullValue(IntTyBits);
583 V = V->stripPointerCasts();
584 if (Instruction *I = dyn_cast<Instruction>(V)) {
585 // If we have already seen this instruction, bail out. Cycles can happen in
586 // unreachable code after constant propagation.
587 if (!SeenInsts.insert(I).second)
590 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
591 return visitGEPOperator(*GEP);
594 if (Argument *A = dyn_cast<Argument>(V))
595 return visitArgument(*A);
596 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
597 return visitConstantPointerNull(*P);
598 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
599 return visitGlobalAlias(*GA);
600 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
601 return visitGlobalVariable(*GV);
602 if (UndefValue *UV = dyn_cast<UndefValue>(V))
603 return visitUndefValue(*UV);
604 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
605 if (CE->getOpcode() == Instruction::IntToPtr)
606 return unknown(); // clueless
607 if (CE->getOpcode() == Instruction::GetElementPtr)
608 return visitGEPOperator(cast<GEPOperator>(*CE));
611 LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: "
616 /// When we're compiling N-bit code, and the user uses parameters that are
617 /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
618 /// trouble with APInt size issues. This function handles resizing + overflow
619 /// checks for us. Check and zext or trunc \p I depending on IntTyBits and
621 bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) {
622 // More bits than we can handle. Checking the bit width isn't necessary, but
623 // it's faster than checking active bits, and should give `false` in the
624 // vast majority of cases.
625 if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
627 if (I.getBitWidth() != IntTyBits)
628 I = I.zextOrTrunc(IntTyBits);
632 SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
633 if (!I.getAllocatedType()->isSized())
636 if (I.getAllocatedType()->isVectorTy() &&
637 I.getAllocatedType()->getVectorIsScalable())
640 APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
641 if (!I.isArrayAllocation())
642 return std::make_pair(align(Size, I.getAlignment()), Zero);
644 Value *ArraySize = I.getArraySize();
645 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
646 APInt NumElems = C->getValue();
647 if (!CheckedZextOrTrunc(NumElems))
651 Size = Size.umul_ov(NumElems, Overflow);
652 return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()),
658 SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
659 // No interprocedural analysis is done at the moment.
660 if (!A.hasByValOrInAllocaAttr()) {
661 ++ObjectVisitorArgument;
664 PointerType *PT = cast<PointerType>(A.getType());
665 APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
666 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
669 SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
670 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
674 // Handle strdup-like functions separately.
675 if (FnData->AllocTy == StrDupLike) {
676 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
680 // Strndup limits strlen.
681 if (FnData->FstParam > 0) {
683 dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
687 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
688 if (Size.ugt(MaxSize))
691 return std::make_pair(Size, Zero);
694 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
698 APInt Size = Arg->getValue();
699 if (!CheckedZextOrTrunc(Size))
702 // Size is determined by just 1 parameter.
703 if (FnData->SndParam < 0)
704 return std::make_pair(Size, Zero);
706 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
710 APInt NumElems = Arg->getValue();
711 if (!CheckedZextOrTrunc(NumElems))
715 Size = Size.umul_ov(NumElems, Overflow);
716 return Overflow ? unknown() : std::make_pair(Size, Zero);
718 // TODO: handle more standard functions (+ wchar cousins):
719 // - strdup / strndup
720 // - strcpy / strncpy
721 // - strcat / strncat
722 // - memcpy / memmove
723 // - strcat / strncat
728 ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) {
729 // If null is unknown, there's nothing we can do. Additionally, non-zero
730 // address spaces can make use of null, so we don't presume to know anything
733 // TODO: How should this work with address space casts? We currently just drop
734 // them on the floor, but it's unclear what we should do when a NULL from
735 // addrspace(1) gets casted to addrspace(0) (or vice-versa).
736 if (Options.NullIsUnknownSize || CPN.getType()->getAddressSpace())
738 return std::make_pair(Zero, Zero);
742 ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
747 ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
748 // Easy cases were already folded by previous passes.
752 SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
753 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
754 APInt Offset(DL.getIndexTypeSizeInBits(GEP.getPointerOperand()->getType()), 0);
755 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
758 return std::make_pair(PtrData.first, PtrData.second + Offset);
761 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
762 if (GA.isInterposable())
764 return compute(GA.getAliasee());
767 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
768 if (!GV.hasDefinitiveInitializer())
771 APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getValueType()));
772 return std::make_pair(align(Size, GV.getAlignment()), Zero);
775 SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
780 SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
785 SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
786 // too complex to analyze statically.
790 SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
791 SizeOffsetType TrueSide = compute(I.getTrueValue());
792 SizeOffsetType FalseSide = compute(I.getFalseValue());
793 if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
794 if (TrueSide == FalseSide) {
798 APInt TrueResult = getSizeWithOverflow(TrueSide);
799 APInt FalseResult = getSizeWithOverflow(FalseSide);
801 if (TrueResult == FalseResult) {
804 if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
805 if (TrueResult.slt(FalseResult))
809 if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
810 if (TrueResult.sgt(FalseResult))
818 SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
819 return std::make_pair(Zero, Zero);
822 SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
823 LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I
828 ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(
829 const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
830 ObjectSizeOpts EvalOpts)
831 : DL(DL), TLI(TLI), Context(Context),
832 Builder(Context, TargetFolder(DL),
833 IRBuilderCallbackInserter(
834 [&](Instruction *I) { InsertedInstructions.insert(I); })),
836 // IntTy and Zero must be set for each compute() since the address space may
837 // be different for later objects.
840 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
841 // XXX - Are vectors of pointers possible here?
842 IntTy = cast<IntegerType>(DL.getIndexType(V->getType()));
843 Zero = ConstantInt::get(IntTy, 0);
845 SizeOffsetEvalType Result = compute_(V);
847 if (!bothKnown(Result)) {
848 // Erase everything that was computed in this iteration from the cache, so
849 // that no dangling references are left behind. We could be a bit smarter if
850 // we kept a dependency graph. It's probably not worth the complexity.
851 for (const Value *SeenVal : SeenVals) {
852 CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
853 // non-computable results can be safely cached
854 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
855 CacheMap.erase(CacheIt);
858 // Erase any instructions we inserted as part of the traversal.
859 for (Instruction *I : InsertedInstructions) {
860 I->replaceAllUsesWith(UndefValue::get(I->getType()));
861 I->eraseFromParent();
866 InsertedInstructions.clear();
870 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
871 ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, EvalOpts);
872 SizeOffsetType Const = Visitor.compute(V);
873 if (Visitor.bothKnown(Const))
874 return std::make_pair(ConstantInt::get(Context, Const.first),
875 ConstantInt::get(Context, Const.second));
877 V = V->stripPointerCasts();
880 CacheMapTy::iterator CacheIt = CacheMap.find(V);
881 if (CacheIt != CacheMap.end())
882 return CacheIt->second;
884 // Always generate code immediately before the instruction being
885 // processed, so that the generated code dominates the same BBs.
886 BuilderTy::InsertPointGuard Guard(Builder);
887 if (Instruction *I = dyn_cast<Instruction>(V))
888 Builder.SetInsertPoint(I);
890 // Now compute the size and offset.
891 SizeOffsetEvalType Result;
893 // Record the pointers that were handled in this run, so that they can be
894 // cleaned later if something fails. We also use this set to break cycles that
895 // can occur in dead code.
896 if (!SeenVals.insert(V).second) {
898 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
899 Result = visitGEPOperator(*GEP);
900 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
902 } else if (isa<Argument>(V) ||
903 (isa<ConstantExpr>(V) &&
904 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
905 isa<GlobalAlias>(V) ||
906 isa<GlobalVariable>(V)) {
907 // Ignore values where we cannot do more than ObjectSizeVisitor.
911 dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: " << *V
916 // Don't reuse CacheIt since it may be invalid at this point.
917 CacheMap[V] = Result;
921 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
922 if (!I.getAllocatedType()->isSized())
926 assert(I.isArrayAllocation());
927 Value *ArraySize = I.getArraySize();
928 Value *Size = ConstantInt::get(ArraySize->getType(),
929 DL.getTypeAllocSize(I.getAllocatedType()));
930 Size = Builder.CreateMul(Size, ArraySize);
931 return std::make_pair(Size, Zero);
934 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
935 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
939 // Handle strdup-like functions separately.
940 if (FnData->AllocTy == StrDupLike) {
945 Value *FirstArg = CS.getArgument(FnData->FstParam);
946 FirstArg = Builder.CreateZExtOrTrunc(FirstArg, IntTy);
947 if (FnData->SndParam < 0)
948 return std::make_pair(FirstArg, Zero);
950 Value *SecondArg = CS.getArgument(FnData->SndParam);
951 SecondArg = Builder.CreateZExtOrTrunc(SecondArg, IntTy);
952 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
953 return std::make_pair(Size, Zero);
955 // TODO: handle more standard functions (+ wchar cousins):
956 // - strdup / strndup
957 // - strcpy / strncpy
958 // - strcat / strncat
959 // - memcpy / memmove
960 // - strcat / strncat
965 ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
970 ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
975 ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
976 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
977 if (!bothKnown(PtrData))
980 Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
981 Offset = Builder.CreateAdd(PtrData.second, Offset);
982 return std::make_pair(PtrData.first, Offset);
985 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
990 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
994 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
995 // Create 2 PHIs: one for size and another for offset.
996 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
997 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
999 // Insert right away in the cache to handle recursive PHIs.
1000 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
1002 // Compute offset/size for each PHI incoming pointer.
1003 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
1004 Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt());
1005 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
1007 if (!bothKnown(EdgeData)) {
1008 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
1009 OffsetPHI->eraseFromParent();
1010 InsertedInstructions.erase(OffsetPHI);
1011 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
1012 SizePHI->eraseFromParent();
1013 InsertedInstructions.erase(SizePHI);
1016 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
1017 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
1020 Value *Size = SizePHI, *Offset = OffsetPHI;
1021 if (Value *Tmp = SizePHI->hasConstantValue()) {
1023 SizePHI->replaceAllUsesWith(Size);
1024 SizePHI->eraseFromParent();
1025 InsertedInstructions.erase(SizePHI);
1027 if (Value *Tmp = OffsetPHI->hasConstantValue()) {
1029 OffsetPHI->replaceAllUsesWith(Offset);
1030 OffsetPHI->eraseFromParent();
1031 InsertedInstructions.erase(OffsetPHI);
1033 return std::make_pair(Size, Offset);
1036 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
1037 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
1038 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
1040 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
1042 if (TrueSide == FalseSide)
1045 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
1047 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
1049 return std::make_pair(Size, Offset);
1052 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
1053 LLVM_DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I