aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis/MemoryBuiltins.cpp
diff options
context:
space:
mode:
authorNuno Lopes <nunoplopes@sapo.pt>2012-07-25 17:29:22 +0000
committerNuno Lopes <nunoplopes@sapo.pt>2012-07-25 17:29:22 +0000
commite3094283e38d9e6f6a4a7a14a3a5d0f2af67d5d5 (patch)
tree86fed444bf0263f4cfc2a78f815c9b6c59e942f7 /lib/Analysis/MemoryBuiltins.cpp
parenta536835230afcd334068bc6f5781a244938feaf9 (diff)
MemoryBuiltins: add support to determine the size of strdup'ed non-constant strings
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160742 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/MemoryBuiltins.cpp')
-rw-r--r--lib/Analysis/MemoryBuiltins.cpp25
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/Analysis/MemoryBuiltins.cpp b/lib/Analysis/MemoryBuiltins.cpp
index 4833b5212b..21417781bd 100644
--- a/lib/Analysis/MemoryBuiltins.cpp
+++ b/lib/Analysis/MemoryBuiltins.cpp
@@ -26,6 +26,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetData.h"
+#include "llvm/Transforms/Utils/BuildLibCalls.h"
#include "llvm/Transforms/Utils/Local.h"
using namespace llvm;
@@ -448,11 +449,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
return std::make_pair(Size, Zero);
// TODO: handle more standard functions (+ wchar cousins):
- // - strdup / strndup
// - strcpy / strncpy
// - strcat / strncat
// - memcpy / memmove
- // - strcat / strncat
// - memset
}
@@ -524,8 +523,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const TargetData *TD,
+ const TargetLibraryInfo *TLI,
LLVMContext &Context)
-: TD(TD), Context(Context), Builder(Context, TargetFolder(TD)),
+: TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)),
Visitor(TD, Context) {
IntTy = TD->getIntPtrType(Context);
Zero = ConstantInt::get(IntTy, 0);
@@ -619,8 +619,21 @@ SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
// handle strdup-like functions separately
if (FnData->AllocTy == StrDupLike) {
- // TODO
- return unknown();
+ IRBuilder<> StdBuilder(Builder.GetInsertPoint());
+ Value *Size;
+
+ // strdup(str): size = strlen(str)+1
+ if (FnData->FstParam < 0)
+ Size = EmitStrLen(CS.getArgument(0), StdBuilder, TD, TLI);
+ else
+ // strndup(str, maxlen): size = strnlen(str, maxlen)+1
+ Size = EmitStrNLen(CS.getArgument(0), CS.getArgument(FnData->FstParam),
+ StdBuilder, TD, TLI);
+ if (!Size)
+ return unknown();
+ Builder.SetInsertPoint(StdBuilder.GetInsertPoint());
+ Size = Builder.CreateNUWAdd(Size, ConstantInt::get(IntTy, 1));
+ return std::make_pair(Size, Zero);
}
Value *FirstArg = CS.getArgument(FnData->FstParam);
@@ -634,11 +647,9 @@ SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
return std::make_pair(Size, Zero);
// TODO: handle more standard functions (+ wchar cousins):
- // - strdup / strndup
// - strcpy / strncpy
// - strcat / strncat
// - memcpy / memmove
- // - strcat / strncat
// - memset
}