2009-10-27 21:05:49 +01:00
|
|
|
//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
|
2009-09-10 06:36:43 +02: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 21:05:49 +01:00
|
|
|
// This family of functions identifies calls to builtin functions that allocate
|
|
|
|
// or free memory.
|
2009-09-10 06:36:43 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-27 21:05:49 +01:00
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2009-09-10 06:36:43 +02:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Module.h"
|
2009-11-10 09:32:25 +01:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2009-11-07 01:16:28 +01:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-09-10 06:36:43 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// malloc Call Utility Functions.
|
|
|
|
//
|
|
|
|
|
2010-02-10 17:03:48 +01:00
|
|
|
/// isMalloc - Returns true if the value is either a malloc call or a
|
2009-09-10 06:36:43 +02:00
|
|
|
/// bitcast of the result of a malloc call.
|
2009-11-03 21:02:35 +01:00
|
|
|
bool llvm::isMalloc(const Value *I) {
|
2009-09-10 06:36:43 +02:00
|
|
|
return extractMallocCall(I) || extractMallocCallFromBitCast(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isMallocCall(const CallInst *CI) {
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
|
2009-11-03 21:39:35 +01:00
|
|
|
Function *Callee = CI->getCalledFunction();
|
|
|
|
if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc")
|
2009-09-10 06:36:43 +02:00
|
|
|
return false;
|
|
|
|
|
2009-10-05 23:15:43 +02:00
|
|
|
// Check malloc prototype.
|
2009-10-07 11:22:55 +02:00
|
|
|
// FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
|
|
|
|
// attribute will exist.
|
2009-11-03 21:39:35 +01:00
|
|
|
const FunctionType *FTy = Callee->getFunctionType();
|
2009-10-05 23:15:43 +02:00
|
|
|
if (FTy->getNumParams() != 1)
|
|
|
|
return false;
|
|
|
|
if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
|
|
|
|
if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2009-09-10 06:36:43 +02: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 21:02:35 +01:00
|
|
|
const CallInst *llvm::extractMallocCall(const Value *I) {
|
2009-09-10 06:36:43 +02:00
|
|
|
const CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
return (isMallocCall(CI)) ? CI : NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:02:35 +01:00
|
|
|
CallInst *llvm::extractMallocCall(Value *I) {
|
2009-09-10 06:36:43 +02:00
|
|
|
CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
return (isMallocCall(CI)) ? CI : NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:02:35 +01:00
|
|
|
static bool isBitCastOfMallocCall(const BitCastInst *BCI) {
|
2009-09-10 06:36:43 +02: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 21:02:35 +01:00
|
|
|
CallInst *llvm::extractMallocCallFromBitCast(Value *I) {
|
2009-09-10 06:36:43 +02:00
|
|
|
BitCastInst *BCI = dyn_cast<BitCastInst>(I);
|
2009-09-10 22:18:57 +02:00
|
|
|
return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
|
|
|
|
: NULL;
|
2009-09-10 06:36:43 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:02:35 +01:00
|
|
|
const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
|
2009-09-10 06:36:43 +02:00
|
|
|
const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
|
2009-09-10 22:18:57 +02:00
|
|
|
return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
|
|
|
|
: NULL;
|
2009-09-10 06:36:43 +02:00
|
|
|
}
|
|
|
|
|
2009-11-10 09:32:25 +01:00
|
|
|
static Value *computeArraySize(const CallInst *CI, const TargetData *TD,
|
|
|
|
bool LookThroughSExt = false) {
|
2009-09-10 06:36:43 +02:00
|
|
|
if (!CI)
|
2009-10-28 21:18:55 +01:00
|
|
|
return NULL;
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2009-11-07 01:16:28 +01:00
|
|
|
// The size of the malloc's result type must be known to determine array size.
|
2009-11-03 21:02:35 +01:00
|
|
|
const Type *T = getMallocAllocatedType(CI);
|
2009-11-07 01:16:28 +01:00
|
|
|
if (!T || !T->isSized() || !TD)
|
2009-10-28 21:18:55 +01:00
|
|
|
return NULL;
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2009-11-10 09:32:25 +01:00
|
|
|
unsigned ElementSize = TD->getTypeAllocSize(T);
|
2009-11-07 01:16:28 +01:00
|
|
|
if (const StructType *ST = dyn_cast<StructType>(T))
|
2009-11-10 09:32:25 +01:00
|
|
|
ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
|
2009-11-07 01:16:28 +01:00
|
|
|
|
2010-06-23 23:41:47 +02:00
|
|
|
// If malloc call's arg can be determined to be a multiple of ElementSize,
|
2009-11-10 09:32:25 +01:00
|
|
|
// return the multiple. Otherwise, return NULL.
|
2010-06-23 23:41:47 +02:00
|
|
|
Value *MallocArg = CI->getArgOperand(0);
|
2009-11-10 09:32:25 +01:00
|
|
|
Value *Multiple = NULL;
|
|
|
|
if (ComputeMultiple(MallocArg, ElementSize, Multiple,
|
2009-11-18 01:58:27 +01:00
|
|
|
LookThroughSExt))
|
2009-11-10 09:32:25 +01:00
|
|
|
return Multiple;
|
2009-09-18 21:20:02 +02:00
|
|
|
|
2009-10-28 21:18:55 +01:00
|
|
|
return NULL;
|
2009-09-10 06:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isArrayMalloc - Returns the corresponding CallInst if the instruction
|
2009-10-28 21:18:55 +01: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 05:27:31 +01:00
|
|
|
const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
|
2009-09-10 06:36:43 +02:00
|
|
|
const CallInst *CI = extractMallocCall(I);
|
2009-11-10 09:32:25 +01:00
|
|
|
Value *ArraySize = computeArraySize(CI, TD);
|
2009-10-28 21:18:55 +01:00
|
|
|
|
|
|
|
if (ArraySize &&
|
2010-06-23 23:41:47 +02:00
|
|
|
ArraySize != ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
|
2009-10-28 21:18:55 +01: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 06:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getMallocType - Returns the PointerType resulting from the malloc call.
|
2009-11-07 01:16:28 +01: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.
|
2009-11-03 21:02:35 +01:00
|
|
|
const PointerType *llvm::getMallocType(const CallInst *CI) {
|
2009-11-10 09:32:25 +01:00
|
|
|
assert(isMalloc(CI) && "getMallocType and not malloc call");
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2009-11-07 01:16:28 +01:00
|
|
|
const PointerType *MallocType = NULL;
|
|
|
|
unsigned NumOfBitCastUses = 0;
|
|
|
|
|
2009-09-18 21:20:02 +02:00
|
|
|
// Determine if CallInst has a bitcast use.
|
2010-03-26 00:06:16 +01:00
|
|
|
for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
|
2009-09-18 21:20:02 +02:00
|
|
|
UI != E; )
|
2009-11-07 01:16:28 +01:00
|
|
|
if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
|
|
|
|
MallocType = cast<PointerType>(BCI->getDestTy());
|
|
|
|
NumOfBitCastUses++;
|
|
|
|
}
|
2009-09-18 21:20:02 +02:00
|
|
|
|
2009-11-07 01:16:28 +01:00
|
|
|
// Malloc call has 1 bitcast use, so type is the bitcast's destination type.
|
|
|
|
if (NumOfBitCastUses == 1)
|
|
|
|
return MallocType;
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2009-09-22 20:50:03 +02:00
|
|
|
// Malloc call was not bitcast, so type is the malloc function's return type.
|
2009-11-07 01:16:28 +01:00
|
|
|
if (NumOfBitCastUses == 0)
|
2009-09-18 21:20:02 +02:00
|
|
|
return cast<PointerType>(CI->getType());
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2009-09-18 21:20:02 +02:00
|
|
|
// Type could not be determined.
|
|
|
|
return NULL;
|
2009-09-10 06:36:43 +02:00
|
|
|
}
|
|
|
|
|
2009-11-07 01:16:28 +01: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.
|
2009-11-03 21:02:35 +01:00
|
|
|
const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
|
|
|
|
const PointerType *PT = getMallocType(CI);
|
2009-09-10 06:36:43 +02:00
|
|
|
return PT ? PT->getElementType() : NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-28 21:18:55 +01: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 22:14:52 +02:00
|
|
|
/// determined.
|
2009-11-10 09:32:25 +01: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 06:36:43 +02:00
|
|
|
}
|
2009-10-24 06:23:03 +02:00
|
|
|
|
2009-10-27 00:43:48 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// free Call Utility Functions.
|
|
|
|
//
|
|
|
|
|
2010-06-23 23:51:12 +02: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 06:23:03 +02:00
|
|
|
const CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
if (!CI)
|
2010-06-23 23:51:12 +02:00
|
|
|
return 0;
|
2009-11-03 21:39:35 +01:00
|
|
|
Function *Callee = CI->getCalledFunction();
|
|
|
|
if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free")
|
2010-06-23 23:51:12 +02:00
|
|
|
return 0;
|
2009-10-24 06:23:03 +02:00
|
|
|
|
|
|
|
// Check free prototype.
|
|
|
|
// FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
|
|
|
|
// attribute will exist.
|
2009-11-03 21:39:35 +01:00
|
|
|
const FunctionType *FTy = Callee->getFunctionType();
|
|
|
|
if (!FTy->getReturnType()->isVoidTy())
|
2010-06-23 23:51:12 +02:00
|
|
|
return 0;
|
2009-10-24 06:23:03 +02:00
|
|
|
if (FTy->getNumParams() != 1)
|
2010-06-23 23:51:12 +02:00
|
|
|
return 0;
|
2009-11-03 21:39:35 +01:00
|
|
|
if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext()))
|
2010-06-23 23:51:12 +02:00
|
|
|
return 0;
|
2009-10-24 06:23:03 +02:00
|
|
|
|
2010-06-23 23:51:12 +02:00
|
|
|
return CI;
|
2009-10-24 06:23:03 +02:00
|
|
|
}
|