1 //===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions identifies calls to builtin functions that allocate
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/MemoryBuiltins.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Transforms/Utils/Local.h"
32 #define DEBUG_TYPE "memory-builtins"
34 enum AllocType : uint8_t {
35 OpNewLike = 1<<0, // allocates; never returns null
36 MallocLike = 1<<1 | OpNewLike, // allocates; may return null
37 CallocLike = 1<<2, // allocates + bzero
38 ReallocLike = 1<<3, // reallocates
40 MallocOrCallocLike = MallocLike | CallocLike,
41 AllocLike = MallocLike | CallocLike | StrDupLike,
42 AnyAlloc = AllocLike | ReallocLike
48 // First and Second size parameters (or -1 if unused)
49 int FstParam, SndParam;
52 // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
53 // know which functions are nounwind, noalias, nocapture parameters, etc.
54 static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
55 {LibFunc_malloc, {MallocLike, 1, 0, -1}},
56 {LibFunc_valloc, {MallocLike, 1, 0, -1}},
57 {LibFunc_Znwj, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
58 {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
59 {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long)
60 {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow)
61 {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
62 {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
63 {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long)
64 {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow)
65 {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
66 {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
67 {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1}}, // new(unsigned long long)
68 {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned long long, nothrow)
69 {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
70 {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
71 {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
72 {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
73 {LibFunc_calloc, {CallocLike, 2, 0, 1}},
74 {LibFunc_realloc, {ReallocLike, 2, 1, -1}},
75 {LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
76 {LibFunc_strdup, {StrDupLike, 1, -1, -1}},
77 {LibFunc_strndup, {StrDupLike, 2, 1, -1}}
78 // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
81 static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
83 // Don't care about intrinsics in this case.
84 if (isa<IntrinsicInst>(V))
87 if (LookThroughBitCast)
88 V = V->stripPointerCasts();
90 ImmutableCallSite CS(V);
91 if (!CS.getInstruction())
94 IsNoBuiltin = CS.isNoBuiltin();
96 const Function *Callee = CS.getCalledFunction();
97 if (!Callee || !Callee->isDeclaration())
102 /// Returns the allocation data for the given value if it's either a call to a
103 /// known allocation function, or a call to a function with the allocsize
105 static Optional<AllocFnsTy>
106 getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
107 const TargetLibraryInfo *TLI) {
108 // Make sure that the function is available.
109 StringRef FnName = Callee->getName();
111 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
114 const auto *Iter = find_if(
115 AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
116 return P.first == TLIFn;
119 if (Iter == std::end(AllocationFnData))
122 const AllocFnsTy *FnData = &Iter->second;
123 if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
126 // Check function prototype.
127 int FstParam = FnData->FstParam;
128 int SndParam = FnData->SndParam;
129 FunctionType *FTy = Callee->getFunctionType();
131 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
132 FTy->getNumParams() == FnData->NumParams &&
134 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
135 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
137 FTy->getParamType(SndParam)->isIntegerTy(32) ||
138 FTy->getParamType(SndParam)->isIntegerTy(64)))
143 static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
144 const TargetLibraryInfo *TLI,
145 bool LookThroughBitCast = false) {
146 bool IsNoBuiltinCall;
147 if (const Function *Callee =
148 getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
149 if (!IsNoBuiltinCall)
150 return getAllocationDataForFunction(Callee, AllocTy, TLI);
154 static Optional<AllocFnsTy> getAllocationSize(const Value *V,
155 const TargetLibraryInfo *TLI) {
156 bool IsNoBuiltinCall;
157 const Function *Callee =
158 getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
162 // Prefer to use existing information over allocsize. This will give us an
164 if (!IsNoBuiltinCall)
165 if (Optional<AllocFnsTy> Data =
166 getAllocationDataForFunction(Callee, AnyAlloc, TLI))
169 Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
170 if (Attr == Attribute())
173 std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
176 // Because allocsize only tells us how many bytes are allocated, we're not
177 // really allowed to assume anything, so we use MallocLike.
178 Result.AllocTy = MallocLike;
179 Result.NumParams = Callee->getNumOperands();
180 Result.FstParam = Args.first;
181 Result.SndParam = Args.second.getValueOr(-1);
185 static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
186 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
187 return CS && CS.hasRetAttr(Attribute::NoAlias);
191 /// \brief Tests if a value is a call or invoke to a library function that
192 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
194 bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
195 bool LookThroughBitCast) {
196 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue();
199 /// \brief Tests if a value is a call or invoke to a function that returns a
200 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
201 bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
202 bool LookThroughBitCast) {
203 // it's safe to consider realloc as noalias since accessing the original
204 // pointer is undefined behavior
205 return isAllocationFn(V, TLI, LookThroughBitCast) ||
206 hasNoAliasAttr(V, LookThroughBitCast);
209 /// \brief Tests if a value is a call or invoke to a library function that
210 /// allocates uninitialized memory (such as malloc).
211 bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
212 bool LookThroughBitCast) {
213 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue();
216 /// \brief Tests if a value is a call or invoke to a library function that
217 /// allocates zero-filled memory (such as calloc).
218 bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
219 bool LookThroughBitCast) {
220 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue();
223 /// \brief Tests if a value is a call or invoke to a library function that
224 /// allocates memory similiar to malloc or calloc.
225 bool llvm::isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
226 bool LookThroughBitCast) {
227 return getAllocationData(V, MallocOrCallocLike, TLI,
228 LookThroughBitCast).hasValue();
231 /// \brief Tests if a value is a call or invoke to a library function that
232 /// allocates memory (either malloc, calloc, or strdup like).
233 bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
234 bool LookThroughBitCast) {
235 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue();
238 /// extractMallocCall - Returns the corresponding CallInst if the instruction
239 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
240 /// ignore InvokeInst here.
241 const CallInst *llvm::extractMallocCall(const Value *I,
242 const TargetLibraryInfo *TLI) {
243 return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : nullptr;
246 static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
247 const TargetLibraryInfo *TLI,
248 bool LookThroughSExt = false) {
252 // The size of the malloc's result type must be known to determine array size.
253 Type *T = getMallocAllocatedType(CI, TLI);
254 if (!T || !T->isSized())
257 unsigned ElementSize = DL.getTypeAllocSize(T);
258 if (StructType *ST = dyn_cast<StructType>(T))
259 ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
261 // If malloc call's arg can be determined to be a multiple of ElementSize,
262 // return the multiple. Otherwise, return NULL.
263 Value *MallocArg = CI->getArgOperand(0);
264 Value *Multiple = nullptr;
265 if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
271 /// getMallocType - Returns the PointerType resulting from the malloc call.
272 /// The PointerType depends on the number of bitcast uses of the malloc call:
273 /// 0: PointerType is the calls' return type.
274 /// 1: PointerType is the bitcast's result type.
275 /// >1: Unique PointerType cannot be determined, return NULL.
276 PointerType *llvm::getMallocType(const CallInst *CI,
277 const TargetLibraryInfo *TLI) {
278 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
280 PointerType *MallocType = nullptr;
281 unsigned NumOfBitCastUses = 0;
283 // Determine if CallInst has a bitcast use.
284 for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
286 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
287 MallocType = cast<PointerType>(BCI->getDestTy());
291 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
292 if (NumOfBitCastUses == 1)
295 // Malloc call was not bitcast, so type is the malloc function's return type.
296 if (NumOfBitCastUses == 0)
297 return cast<PointerType>(CI->getType());
299 // Type could not be determined.
303 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
304 /// The Type depends on the number of bitcast uses of the malloc call:
305 /// 0: PointerType is the malloc calls' return type.
306 /// 1: PointerType is the bitcast's result type.
307 /// >1: Unique PointerType cannot be determined, return NULL.
308 Type *llvm::getMallocAllocatedType(const CallInst *CI,
309 const TargetLibraryInfo *TLI) {
310 PointerType *PT = getMallocType(CI, TLI);
311 return PT ? PT->getElementType() : nullptr;
314 /// getMallocArraySize - Returns the array size of a malloc call. If the
315 /// argument passed to malloc is a multiple of the size of the malloced type,
316 /// then return that multiple. For non-array mallocs, the multiple is
317 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
319 Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL,
320 const TargetLibraryInfo *TLI,
321 bool LookThroughSExt) {
322 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
323 return computeArraySize(CI, DL, TLI, LookThroughSExt);
327 /// extractCallocCall - Returns the corresponding CallInst if the instruction
328 /// is a calloc call.
329 const CallInst *llvm::extractCallocCall(const Value *I,
330 const TargetLibraryInfo *TLI) {
331 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
335 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
336 const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
337 const CallInst *CI = dyn_cast<CallInst>(I);
338 if (!CI || isa<IntrinsicInst>(CI))
340 Function *Callee = CI->getCalledFunction();
341 if (Callee == nullptr)
344 StringRef FnName = Callee->getName();
346 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
349 unsigned ExpectedNumParams;
350 if (TLIFn == LibFunc_free ||
351 TLIFn == LibFunc_ZdlPv || // operator delete(void*)
352 TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
353 TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
354 TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
355 TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
356 TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*)
357 ExpectedNumParams = 1;
358 else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint)
359 TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong)
360 TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
361 TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint)
362 TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong)
363 TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
364 TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint)
365 TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
366 TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
367 TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
368 TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint)
369 TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
370 TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
371 TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow)
372 ExpectedNumParams = 2;
376 // Check free prototype.
377 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
378 // attribute will exist.
379 FunctionType *FTy = Callee->getFunctionType();
380 if (!FTy->getReturnType()->isVoidTy())
382 if (FTy->getNumParams() != ExpectedNumParams)
384 if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
392 //===----------------------------------------------------------------------===//
393 // Utility functions to compute size of objects.
395 static APInt getSizeWithOverflow(const SizeOffsetType &Data) {
396 if (Data.second.isNegative() || Data.first.ult(Data.second))
397 return APInt(Data.first.getBitWidth(), 0);
398 return Data.first - Data.second;
401 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
402 /// object size in Size if successful, and false otherwise.
403 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
404 /// byval arguments, and global variables.
405 bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
406 const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
407 ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
408 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
409 if (!Visitor.bothKnown(Data))
412 Size = getSizeWithOverflow(Data).getZExtValue();
416 ConstantInt *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
417 const DataLayout &DL,
418 const TargetLibraryInfo *TLI,
420 assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
421 "ObjectSize must be a call to llvm.objectsize!");
423 bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
424 ObjectSizeOpts EvalOptions;
425 // Unless we have to fold this to something, try to be as accurate as
428 EvalOptions.EvalMode =
429 MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
431 EvalOptions.EvalMode = ObjectSizeOpts::Mode::Exact;
433 EvalOptions.NullIsUnknownSize =
434 cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
436 // FIXME: Does it make sense to just return a failure value if the size won't
437 // fit in the output and `!MustSucceed`?
439 auto *ResultType = cast<IntegerType>(ObjectSize->getType());
440 if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
441 isUIntN(ResultType->getBitWidth(), Size))
442 return ConstantInt::get(ResultType, Size);
447 return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
450 STATISTIC(ObjectVisitorArgument,
451 "Number of arguments with unsolved size and offset");
452 STATISTIC(ObjectVisitorLoad,
453 "Number of load instructions with unsolved size and offset");
456 APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
457 if (Options.RoundToAlign && Align)
458 return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align));
462 ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
463 const TargetLibraryInfo *TLI,
464 LLVMContext &Context,
465 ObjectSizeOpts Options)
466 : DL(DL), TLI(TLI), Options(Options) {
467 // Pointer size must be rechecked for each object visited since it could have
468 // a different address space.
471 SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
472 IntTyBits = DL.getPointerTypeSizeInBits(V->getType());
473 Zero = APInt::getNullValue(IntTyBits);
475 V = V->stripPointerCasts();
476 if (Instruction *I = dyn_cast<Instruction>(V)) {
477 // If we have already seen this instruction, bail out. Cycles can happen in
478 // unreachable code after constant propagation.
479 if (!SeenInsts.insert(I).second)
482 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
483 return visitGEPOperator(*GEP);
486 if (Argument *A = dyn_cast<Argument>(V))
487 return visitArgument(*A);
488 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
489 return visitConstantPointerNull(*P);
490 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
491 return visitGlobalAlias(*GA);
492 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
493 return visitGlobalVariable(*GV);
494 if (UndefValue *UV = dyn_cast<UndefValue>(V))
495 return visitUndefValue(*UV);
496 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
497 if (CE->getOpcode() == Instruction::IntToPtr)
498 return unknown(); // clueless
499 if (CE->getOpcode() == Instruction::GetElementPtr)
500 return visitGEPOperator(cast<GEPOperator>(*CE));
503 DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
508 SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
509 if (!I.getAllocatedType()->isSized())
512 APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
513 if (!I.isArrayAllocation())
514 return std::make_pair(align(Size, I.getAlignment()), Zero);
516 Value *ArraySize = I.getArraySize();
517 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
518 Size *= C->getValue().zextOrSelf(IntTyBits);
519 return std::make_pair(align(Size, I.getAlignment()), Zero);
524 SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
525 // No interprocedural analysis is done at the moment.
526 if (!A.hasByValOrInAllocaAttr()) {
527 ++ObjectVisitorArgument;
530 PointerType *PT = cast<PointerType>(A.getType());
531 APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
532 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
535 SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
536 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
540 // Handle strdup-like functions separately.
541 if (FnData->AllocTy == StrDupLike) {
542 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
546 // Strndup limits strlen.
547 if (FnData->FstParam > 0) {
549 dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
553 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
554 if (Size.ugt(MaxSize))
557 return std::make_pair(Size, Zero);
560 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
564 // When we're compiling N-bit code, and the user uses parameters that are
565 // greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
566 // trouble with APInt size issues. This function handles resizing + overflow
568 auto CheckedZextOrTrunc = [&](APInt &I) {
569 // More bits than we can handle. Checking the bit width isn't necessary, but
570 // it's faster than checking active bits, and should give `false` in the
571 // vast majority of cases.
572 if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
574 if (I.getBitWidth() != IntTyBits)
575 I = I.zextOrTrunc(IntTyBits);
579 APInt Size = Arg->getValue();
580 if (!CheckedZextOrTrunc(Size))
583 // Size is determined by just 1 parameter.
584 if (FnData->SndParam < 0)
585 return std::make_pair(Size, Zero);
587 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
591 APInt NumElems = Arg->getValue();
592 if (!CheckedZextOrTrunc(NumElems))
596 Size = Size.umul_ov(NumElems, Overflow);
597 return Overflow ? unknown() : std::make_pair(Size, Zero);
599 // TODO: handle more standard functions (+ wchar cousins):
600 // - strdup / strndup
601 // - strcpy / strncpy
602 // - strcat / strncat
603 // - memcpy / memmove
604 // - strcat / strncat
609 ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) {
610 if (Options.NullIsUnknownSize && CPN.getType()->getAddressSpace() == 0)
612 return std::make_pair(Zero, Zero);
616 ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
621 ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
622 // Easy cases were already folded by previous passes.
626 SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
627 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
628 APInt Offset(IntTyBits, 0);
629 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
632 return std::make_pair(PtrData.first, PtrData.second + Offset);
635 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
636 if (GA.isInterposable())
638 return compute(GA.getAliasee());
641 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
642 if (!GV.hasDefinitiveInitializer())
645 APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getType()->getElementType()));
646 return std::make_pair(align(Size, GV.getAlignment()), Zero);
649 SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
654 SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
659 SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
660 // too complex to analyze statically.
664 SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
665 SizeOffsetType TrueSide = compute(I.getTrueValue());
666 SizeOffsetType FalseSide = compute(I.getFalseValue());
667 if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
668 if (TrueSide == FalseSide) {
672 APInt TrueResult = getSizeWithOverflow(TrueSide);
673 APInt FalseResult = getSizeWithOverflow(FalseSide);
675 if (TrueResult == FalseResult) {
678 if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
679 if (TrueResult.slt(FalseResult))
683 if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
684 if (TrueResult.sgt(FalseResult))
692 SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
693 return std::make_pair(Zero, Zero);
696 SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
697 DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
701 ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(
702 const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
704 : DL(DL), TLI(TLI), Context(Context), Builder(Context, TargetFolder(DL)),
705 RoundToAlign(RoundToAlign) {
706 // IntTy and Zero must be set for each compute() since the address space may
707 // be different for later objects.
710 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
711 // XXX - Are vectors of pointers possible here?
712 IntTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
713 Zero = ConstantInt::get(IntTy, 0);
715 SizeOffsetEvalType Result = compute_(V);
717 if (!bothKnown(Result)) {
718 // Erase everything that was computed in this iteration from the cache, so
719 // that no dangling references are left behind. We could be a bit smarter if
720 // we kept a dependency graph. It's probably not worth the complexity.
721 for (const Value *SeenVal : SeenVals) {
722 CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
723 // non-computable results can be safely cached
724 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
725 CacheMap.erase(CacheIt);
733 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
734 ObjectSizeOpts ObjSizeOptions;
735 ObjSizeOptions.RoundToAlign = RoundToAlign;
737 ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, ObjSizeOptions);
738 SizeOffsetType Const = Visitor.compute(V);
739 if (Visitor.bothKnown(Const))
740 return std::make_pair(ConstantInt::get(Context, Const.first),
741 ConstantInt::get(Context, Const.second));
743 V = V->stripPointerCasts();
746 CacheMapTy::iterator CacheIt = CacheMap.find(V);
747 if (CacheIt != CacheMap.end())
748 return CacheIt->second;
750 // Always generate code immediately before the instruction being
751 // processed, so that the generated code dominates the same BBs.
752 BuilderTy::InsertPointGuard Guard(Builder);
753 if (Instruction *I = dyn_cast<Instruction>(V))
754 Builder.SetInsertPoint(I);
756 // Now compute the size and offset.
757 SizeOffsetEvalType Result;
759 // Record the pointers that were handled in this run, so that they can be
760 // cleaned later if something fails. We also use this set to break cycles that
761 // can occur in dead code.
762 if (!SeenVals.insert(V).second) {
764 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
765 Result = visitGEPOperator(*GEP);
766 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
768 } else if (isa<Argument>(V) ||
769 (isa<ConstantExpr>(V) &&
770 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
771 isa<GlobalAlias>(V) ||
772 isa<GlobalVariable>(V)) {
773 // Ignore values where we cannot do more than ObjectSizeVisitor.
776 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
781 // Don't reuse CacheIt since it may be invalid at this point.
782 CacheMap[V] = Result;
786 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
787 if (!I.getAllocatedType()->isSized())
791 assert(I.isArrayAllocation());
792 Value *ArraySize = I.getArraySize();
793 Value *Size = ConstantInt::get(ArraySize->getType(),
794 DL.getTypeAllocSize(I.getAllocatedType()));
795 Size = Builder.CreateMul(Size, ArraySize);
796 return std::make_pair(Size, Zero);
799 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
800 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
804 // Handle strdup-like functions separately.
805 if (FnData->AllocTy == StrDupLike) {
810 Value *FirstArg = CS.getArgument(FnData->FstParam);
811 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
812 if (FnData->SndParam < 0)
813 return std::make_pair(FirstArg, Zero);
815 Value *SecondArg = CS.getArgument(FnData->SndParam);
816 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
817 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
818 return std::make_pair(Size, Zero);
820 // TODO: handle more standard functions (+ wchar cousins):
821 // - strdup / strndup
822 // - strcpy / strncpy
823 // - strcat / strncat
824 // - memcpy / memmove
825 // - strcat / strncat
830 ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
835 ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
840 ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
841 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
842 if (!bothKnown(PtrData))
845 Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
846 Offset = Builder.CreateAdd(PtrData.second, Offset);
847 return std::make_pair(PtrData.first, Offset);
850 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
855 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
859 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
860 // Create 2 PHIs: one for size and another for offset.
861 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
862 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
864 // Insert right away in the cache to handle recursive PHIs.
865 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
867 // Compute offset/size for each PHI incoming pointer.
868 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
869 Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt());
870 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
872 if (!bothKnown(EdgeData)) {
873 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
874 OffsetPHI->eraseFromParent();
875 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
876 SizePHI->eraseFromParent();
879 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
880 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
883 Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
884 if ((Tmp = SizePHI->hasConstantValue())) {
886 SizePHI->replaceAllUsesWith(Size);
887 SizePHI->eraseFromParent();
889 if ((Tmp = OffsetPHI->hasConstantValue())) {
891 OffsetPHI->replaceAllUsesWith(Offset);
892 OffsetPHI->eraseFromParent();
894 return std::make_pair(Size, Offset);
897 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
898 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
899 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
901 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
903 if (TrueSide == FalseSide)
906 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
908 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
910 return std::make_pair(Size, Offset);
913 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
914 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');