2009-10-27 20:05:49 +00:00
|
|
|
//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
|
2009-09-10 04:36:43 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-10-27 20:05:49 +00:00
|
|
|
// This family of functions identifies calls to builtin functions that allocate
|
|
|
|
// or free memory.
|
2009-09-10 04:36:43 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-27 20:05:49 +00:00
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2009-09-10 04:36:43 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Module.h"
|
2009-11-10 08:32:25 +00:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2009-11-07 00:16:28 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-09-10 04:36:43 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// malloc Call Utility Functions.
|
|
|
|
//
|
|
|
|
|
2010-02-10 16:03:48 +00:00
|
|
|
/// isMalloc - Returns true if the value is either a malloc call or a
|
2009-09-10 04:36:43 +00:00
|
|
|
/// bitcast of the result of a malloc call.
|
2009-11-03 20:02:35 +00:00
|
|
|
bool llvm::isMalloc(const Value *I) {
|
2009-09-10 04:36:43 +00:00
|
|
|
return extractMallocCall(I) || extractMallocCallFromBitCast(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isMallocCall(const CallInst *CI) {
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
|
2009-11-03 20:39:35 +00:00
|
|
|
Function *Callee = CI->getCalledFunction();
|
2011-03-15 07:31:32 +00:00
|
|
|
if (Callee == 0 || !Callee->isDeclaration())
|
|
|
|
return false;
|
|
|
|
if (Callee->getName() != "malloc" &&
|
2011-03-17 05:20:12 +00:00
|
|
|
Callee->getName() != "_Znwj" && // operator new(unsigned int)
|
|
|
|
Callee->getName() != "_Znwm" && // operator new(unsigned long)
|
|
|
|
Callee->getName() != "_Znaj" && // operator new[](unsigned int)
|
|
|
|
Callee->getName() != "_Znam") // operator new[](unsigned long)
|
2009-09-10 04:36:43 +00:00
|
|
|
return false;
|
|
|
|
|
2009-10-05 21:15:43 +00:00
|
|
|
// Check malloc prototype.
|
2009-10-07 09:22:55 +00:00
|
|
|
// FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
|
|
|
|
// attribute will exist.
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = Callee->getFunctionType();
|
2011-11-23 17:58:47 +00:00
|
|
|
return FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
|
|
|
|
FTy->getNumParams() == 1 &&
|
|
|
|
(FTy->getParamType(0)->isIntegerTy(32) ||
|
|
|
|
FTy->getParamType(0)->isIntegerTy(64));
|
2009-09-10 04:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// extractMallocCall - Returns the corresponding CallInst if the instruction
|
|
|
|
/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
|
|
|
|
/// ignore InvokeInst here.
|
2009-11-03 20:02:35 +00:00
|
|
|
const CallInst *llvm::extractMallocCall(const Value *I) {
|
2009-09-10 04:36:43 +00:00
|
|
|
const CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
return (isMallocCall(CI)) ? CI : NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-03 20:02:35 +00:00
|
|
|
CallInst *llvm::extractMallocCall(Value *I) {
|
2009-09-10 04:36:43 +00:00
|
|
|
CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
return (isMallocCall(CI)) ? CI : NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-03 20:02:35 +00:00
|
|
|
static bool isBitCastOfMallocCall(const BitCastInst *BCI) {
|
2009-09-10 04:36:43 +00:00
|
|
|
if (!BCI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
|
|
|
|
/// instruction is a bitcast of the result of a malloc call.
|
2009-11-03 20:02:35 +00:00
|
|
|
CallInst *llvm::extractMallocCallFromBitCast(Value *I) {
|
2009-09-10 04:36:43 +00:00
|
|
|
BitCastInst *BCI = dyn_cast<BitCastInst>(I);
|
2009-09-10 20:18:57 +00:00
|
|
|
return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
|
|
|
|
: NULL;
|
2009-09-10 04:36:43 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 20:02:35 +00:00
|
|
|
const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
|
2009-09-10 04:36:43 +00:00
|
|
|
const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
|
2009-09-10 20:18:57 +00:00
|
|
|
return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
|
|
|
|
: NULL;
|
2009-09-10 04:36:43 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 08:32:25 +00:00
|
|
|
static Value *computeArraySize(const CallInst *CI, const TargetData *TD,
|
|
|
|
bool LookThroughSExt = false) {
|
2009-09-10 04:36:43 +00:00
|
|
|
if (!CI)
|
2009-10-28 20:18:55 +00:00
|
|
|
return NULL;
|
2009-09-10 04:36:43 +00:00
|
|
|
|
2009-11-07 00:16:28 +00:00
|
|
|
// The size of the malloc's result type must be known to determine array size.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *T = getMallocAllocatedType(CI);
|
2009-11-07 00:16:28 +00:00
|
|
|
if (!T || !T->isSized() || !TD)
|
2009-10-28 20:18:55 +00:00
|
|
|
return NULL;
|
2009-09-10 04:36:43 +00:00
|
|
|
|
2009-11-10 08:32:25 +00:00
|
|
|
unsigned ElementSize = TD->getTypeAllocSize(T);
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *ST = dyn_cast<StructType>(T))
|
2009-11-10 08:32:25 +00:00
|
|
|
ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
|
2009-11-07 00:16:28 +00:00
|
|
|
|
2010-06-23 21:41:47 +00:00
|
|
|
// If malloc call's arg can be determined to be a multiple of ElementSize,
|
2009-11-10 08:32:25 +00:00
|
|
|
// return the multiple. Otherwise, return NULL.
|
2010-06-23 21:41:47 +00:00
|
|
|
Value *MallocArg = CI->getArgOperand(0);
|
2009-11-10 08:32:25 +00:00
|
|
|
Value *Multiple = NULL;
|
|
|
|
if (ComputeMultiple(MallocArg, ElementSize, Multiple,
|
2009-11-18 00:58:27 +00:00
|
|
|
LookThroughSExt))
|
2009-11-10 08:32:25 +00:00
|
|
|
return Multiple;
|
2009-09-18 19:20:02 +00:00
|
|
|
|
2009-10-28 20:18:55 +00:00
|
|
|
return NULL;
|
2009-09-10 04:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isArrayMalloc - Returns the corresponding CallInst if the instruction
|
2009-10-28 20:18:55 +00:00
|
|
|
/// is a call to malloc whose array size can be determined and the array size
|
|
|
|
/// is not constant 1. Otherwise, return NULL.
|
2009-11-06 04:27:31 +00:00
|
|
|
const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
|
2009-09-10 04:36:43 +00:00
|
|
|
const CallInst *CI = extractMallocCall(I);
|
2009-11-10 08:32:25 +00:00
|
|
|
Value *ArraySize = computeArraySize(CI, TD);
|
2009-10-28 20:18:55 +00:00
|
|
|
|
|
|
|
if (ArraySize &&
|
2010-06-23 21:41:47 +00:00
|
|
|
ArraySize != ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
|
2009-10-28 20:18:55 +00:00
|
|
|
return CI;
|
|
|
|
|
|
|
|
// CI is a non-array malloc or we can't figure out that it is an array malloc.
|
|
|
|
return NULL;
|
2009-09-10 04:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getMallocType - Returns the PointerType resulting from the malloc call.
|
2009-11-07 00:16:28 +00:00
|
|
|
/// The PointerType depends on the number of bitcast uses of the malloc call:
|
|
|
|
/// 0: PointerType is the calls' return type.
|
|
|
|
/// 1: PointerType is the bitcast's result type.
|
|
|
|
/// >1: Unique PointerType cannot be determined, return NULL.
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *llvm::getMallocType(const CallInst *CI) {
|
2009-11-10 08:32:25 +00:00
|
|
|
assert(isMalloc(CI) && "getMallocType and not malloc call");
|
2009-09-10 04:36:43 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *MallocType = NULL;
|
2009-11-07 00:16:28 +00:00
|
|
|
unsigned NumOfBitCastUses = 0;
|
|
|
|
|
2009-09-18 19:20:02 +00:00
|
|
|
// Determine if CallInst has a bitcast use.
|
2010-03-25 23:06:16 +00:00
|
|
|
for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
|
2009-09-18 19:20:02 +00:00
|
|
|
UI != E; )
|
2009-11-07 00:16:28 +00:00
|
|
|
if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
|
|
|
|
MallocType = cast<PointerType>(BCI->getDestTy());
|
|
|
|
NumOfBitCastUses++;
|
|
|
|
}
|
2009-09-18 19:20:02 +00:00
|
|
|
|
2009-11-07 00:16:28 +00:00
|
|
|
// Malloc call has 1 bitcast use, so type is the bitcast's destination type.
|
|
|
|
if (NumOfBitCastUses == 1)
|
|
|
|
return MallocType;
|
2009-09-10 04:36:43 +00:00
|
|
|
|
2009-09-22 18:50:03 +00:00
|
|
|
// Malloc call was not bitcast, so type is the malloc function's return type.
|
2009-11-07 00:16:28 +00:00
|
|
|
if (NumOfBitCastUses == 0)
|
2009-09-18 19:20:02 +00:00
|
|
|
return cast<PointerType>(CI->getType());
|
2009-09-10 04:36:43 +00:00
|
|
|
|
2009-09-18 19:20:02 +00:00
|
|
|
// Type could not be determined.
|
|
|
|
return NULL;
|
2009-09-10 04:36:43 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 00:16:28 +00:00
|
|
|
/// getMallocAllocatedType - Returns the Type allocated by malloc call.
|
|
|
|
/// The Type depends on the number of bitcast uses of the malloc call:
|
|
|
|
/// 0: PointerType is the malloc calls' return type.
|
|
|
|
/// 1: PointerType is the bitcast's result type.
|
|
|
|
/// >1: Unique PointerType cannot be determined, return NULL.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *llvm::getMallocAllocatedType(const CallInst *CI) {
|
|
|
|
PointerType *PT = getMallocType(CI);
|
2009-09-10 04:36:43 +00:00
|
|
|
return PT ? PT->getElementType() : NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-28 20:18:55 +00:00
|
|
|
/// getMallocArraySize - Returns the array size of a malloc call. If the
|
|
|
|
/// argument passed to malloc is a multiple of the size of the malloced type,
|
|
|
|
/// then return that multiple. For non-array mallocs, the multiple is
|
|
|
|
/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
|
2009-10-15 20:14:52 +00:00
|
|
|
/// determined.
|
2009-11-10 08:32:25 +00:00
|
|
|
Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD,
|
|
|
|
bool LookThroughSExt) {
|
|
|
|
assert(isMalloc(CI) && "getMallocArraySize and not malloc call");
|
|
|
|
return computeArraySize(CI, TD, LookThroughSExt);
|
2009-09-10 04:36:43 +00:00
|
|
|
}
|
2009-10-24 04:23:03 +00:00
|
|
|
|
2009-10-26 23:43:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// free Call Utility Functions.
|
|
|
|
//
|
|
|
|
|
2010-06-23 21:51:12 +00:00
|
|
|
/// isFreeCall - Returns non-null if the value is a call to the builtin free()
|
|
|
|
const CallInst *llvm::isFreeCall(const Value *I) {
|
2009-10-24 04:23:03 +00:00
|
|
|
const CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
if (!CI)
|
2010-06-23 21:51:12 +00:00
|
|
|
return 0;
|
2009-11-03 20:39:35 +00:00
|
|
|
Function *Callee = CI->getCalledFunction();
|
2011-03-15 07:31:32 +00:00
|
|
|
if (Callee == 0 || !Callee->isDeclaration())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (Callee->getName() != "free" &&
|
2011-03-17 05:20:12 +00:00
|
|
|
Callee->getName() != "_ZdlPv" && // operator delete(void*)
|
|
|
|
Callee->getName() != "_ZdaPv") // operator delete[](void*)
|
2010-06-23 21:51:12 +00:00
|
|
|
return 0;
|
2009-10-24 04:23:03 +00:00
|
|
|
|
|
|
|
// Check free prototype.
|
|
|
|
// FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
|
|
|
|
// attribute will exist.
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = Callee->getFunctionType();
|
2009-11-03 20:39:35 +00:00
|
|
|
if (!FTy->getReturnType()->isVoidTy())
|
2010-06-23 21:51:12 +00:00
|
|
|
return 0;
|
2009-10-24 04:23:03 +00:00
|
|
|
if (FTy->getNumParams() != 1)
|
2010-06-23 21:51:12 +00:00
|
|
|
return 0;
|
2011-06-18 21:46:23 +00:00
|
|
|
if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
|
2010-06-23 21:51:12 +00:00
|
|
|
return 0;
|
2009-10-24 04:23:03 +00:00
|
|
|
|
2010-06-23 21:51:12 +00:00
|
|
|
return CI;
|
2009-10-24 04:23:03 +00:00
|
|
|
}
|