Skip to content
Snippets Groups Projects
Commit f0441e04 authored by Nuno Lopes's avatar Nuno Lopes
Browse files

MemoryBuiltins: add support to determine the size of strdup'ed non-constant strings

llvm-svn: 160742
parent 7ba5b987
No related branches found
No related tags found
No related merge requests found
...@@ -201,6 +201,7 @@ class ObjectSizeOffsetEvaluator ...@@ -201,6 +201,7 @@ class ObjectSizeOffsetEvaluator
typedef SmallPtrSet<const Value*, 8> PtrSetTy; typedef SmallPtrSet<const Value*, 8> PtrSetTy;
const TargetData *TD; const TargetData *TD;
const TargetLibraryInfo *TLI;
LLVMContext &Context; LLVMContext &Context;
BuilderTy Builder; BuilderTy Builder;
ObjectSizeOffsetVisitor Visitor; ObjectSizeOffsetVisitor Visitor;
...@@ -215,7 +216,8 @@ class ObjectSizeOffsetEvaluator ...@@ -215,7 +216,8 @@ class ObjectSizeOffsetEvaluator
SizeOffsetEvalType compute_(Value *V); SizeOffsetEvalType compute_(Value *V);
public: public:
ObjectSizeOffsetEvaluator(const TargetData *TD, LLVMContext &Context); ObjectSizeOffsetEvaluator(const TargetData *TD, const TargetLibraryInfo *TLI,
LLVMContext &Context);
SizeOffsetEvalType compute(Value *V); SizeOffsetEvalType compute(Value *V);
bool knownSize(SizeOffsetEvalType SizeOffset) { bool knownSize(SizeOffsetEvalType SizeOffset) {
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Transforms/Utils/BuildLibCalls.h"
#include "llvm/Transforms/Utils/Local.h" #include "llvm/Transforms/Utils/Local.h"
using namespace llvm; using namespace llvm;
...@@ -448,11 +449,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) { ...@@ -448,11 +449,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
return std::make_pair(Size, Zero); return std::make_pair(Size, Zero);
// TODO: handle more standard functions (+ wchar cousins): // TODO: handle more standard functions (+ wchar cousins):
// - strdup / strndup
// - strcpy / strncpy // - strcpy / strncpy
// - strcat / strncat // - strcat / strncat
// - memcpy / memmove // - memcpy / memmove
// - strcat / strncat
// - memset // - memset
} }
...@@ -524,8 +523,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) { ...@@ -524,8 +523,9 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const TargetData *TD, ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const TargetData *TD,
const TargetLibraryInfo *TLI,
LLVMContext &Context) LLVMContext &Context)
: TD(TD), Context(Context), Builder(Context, TargetFolder(TD)), : TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)),
Visitor(TD, Context) { Visitor(TD, Context) {
IntTy = TD->getIntPtrType(Context); IntTy = TD->getIntPtrType(Context);
Zero = ConstantInt::get(IntTy, 0); Zero = ConstantInt::get(IntTy, 0);
...@@ -619,8 +619,21 @@ SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) { ...@@ -619,8 +619,21 @@ SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
// handle strdup-like functions separately // handle strdup-like functions separately
if (FnData->AllocTy == StrDupLike) { if (FnData->AllocTy == StrDupLike) {
// TODO IRBuilder<> StdBuilder(Builder.GetInsertPoint());
return unknown(); 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); Value *FirstArg = CS.getArgument(FnData->FstParam);
...@@ -634,11 +647,9 @@ SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) { ...@@ -634,11 +647,9 @@ SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
return std::make_pair(Size, Zero); return std::make_pair(Size, Zero);
// TODO: handle more standard functions (+ wchar cousins): // TODO: handle more standard functions (+ wchar cousins):
// - strdup / strndup
// - strcpy / strncpy // - strcpy / strncpy
// - strcat / strncat // - strcat / strncat
// - memcpy / memmove // - memcpy / memmove
// - strcat / strncat
// - memset // - memset
} }
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "llvm/Support/TargetFolder.h" #include "llvm/Support/TargetFolder.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Instrumentation.h" #include "llvm/Transforms/Instrumentation.h"
using namespace llvm; using namespace llvm;
...@@ -48,6 +49,7 @@ namespace { ...@@ -48,6 +49,7 @@ namespace {
virtual void getAnalysisUsage(AnalysisUsage &AU) const { virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<TargetData>(); AU.addRequired<TargetData>();
AU.addRequired<TargetLibraryInfo>();
} }
private: private:
...@@ -166,11 +168,12 @@ bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) { ...@@ -166,11 +168,12 @@ bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) {
bool BoundsChecking::runOnFunction(Function &F) { bool BoundsChecking::runOnFunction(Function &F) {
TD = &getAnalysis<TargetData>(); TD = &getAnalysis<TargetData>();
const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
TrapBB = 0; TrapBB = 0;
BuilderTy TheBuilder(F.getContext(), TargetFolder(TD)); BuilderTy TheBuilder(F.getContext(), TargetFolder(TD));
Builder = &TheBuilder; Builder = &TheBuilder;
ObjectSizeOffsetEvaluator TheObjSizeEval(TD, F.getContext()); ObjectSizeOffsetEvaluator TheObjSizeEval(TD, TLI, F.getContext());
ObjSizeEval = &TheObjSizeEval; ObjSizeEval = &TheObjSizeEval;
// check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
......
; RUN: opt < %s -bounds-checking -S | FileCheck %s
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
declare noalias i8* @strdup(i8* nocapture) nounwind
declare noalias i8* @strndup(i8* nocapture, i64) nounwind
; CHECK: @f1
define i8 @f1(i8* nocapture %str, i8** nocapture %esc) nounwind uwtable ssp {
; CHECK: call i64 @strlen(i8* %str)
; CHECK-NEXT: %1 = add nuw i64 {{.*}}, 1
%call = tail call i8* @strdup(i8* %str) nounwind
store i8* %call, i8** %esc, align 8
%arrayidx = getelementptr inbounds i8* %call, i64 3
; CHECK: sub i64 %1, 3
%1 = load i8* %arrayidx, align 1
ret i8 %1
; CHECK: call void @llvm.trap
}
; CHECK: @f2
define i8 @f2(i8* nocapture %str, i8** nocapture %esc, i64 %limit) nounwind uwtable ssp {
; CHECK: call i64 @strnlen(i8* %str, i64 %limit)
; CHECK-NEXT: %1 = add nuw i64 {{.*}}, 1
%call = tail call i8* @strndup(i8* %str, i64 %limit) nounwind
store i8* %call, i8** %esc, align 8
%arrayidx = getelementptr inbounds i8* %call, i64 3
; CHECK: sub i64 %1, 3
%1 = load i8* %arrayidx, align 1
ret i8 %1
; CHECK: call void @llvm.trap
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment