aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNuno Lopes <nunoplopes@sapo.pt>2012-07-20 23:07:40 +0000
committerNuno Lopes <nunoplopes@sapo.pt>2012-07-20 23:07:40 +0000
commitc606c3ff911eddcbf8bab95e67c7d8c1f69a493e (patch)
tree419225500a5672a9cb93e01358f2de62a7e5a6f3
parent78435f6bb7574d3d26f8c5151e2c140c525b7994 (diff)
baby steps toward fixing some problems with inbound GEPs that overflow, as discussed 2 months ago or so.
Make sure we do not emit index computations with NSW flags so that we dont get an undef value if the GEP overflows git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160589 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Transforms/Utils/Local.h7
-rw-r--r--lib/Analysis/MemoryBuiltins.cpp2
-rw-r--r--test/Instrumentation/BoundsChecking/simple.ll10
3 files changed, 16 insertions, 3 deletions
diff --git a/include/llvm/Transforms/Utils/Local.h b/include/llvm/Transforms/Utils/Local.h
index 84c0c5862e..495eab7328 100644
--- a/include/llvm/Transforms/Utils/Local.h
+++ b/include/llvm/Transforms/Utils/Local.h
@@ -168,15 +168,18 @@ static inline unsigned getKnownAlignment(Value *V, const TargetData *TD = 0) {
/// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
/// code necessary to compute the offset from the base pointer (without adding
/// in the base pointer). Return the result as a signed integer of intptr size.
+/// When NoAssumptions is true, no assumptions about index computation not
+/// overflowing is made.
template<typename IRBuilderTy>
-Value *EmitGEPOffset(IRBuilderTy *Builder, const TargetData &TD, User *GEP) {
+Value *EmitGEPOffset(IRBuilderTy *Builder, const TargetData &TD, User *GEP,
+ bool NoAssumptions = false) {
gep_type_iterator GTI = gep_type_begin(GEP);
Type *IntPtrTy = TD.getIntPtrType(GEP->getContext());
Value *Result = Constant::getNullValue(IntPtrTy);
// If the GEP is inbounds, we know that none of the addressing operations will
// overflow in an unsigned sense.
- bool isInBounds = cast<GEPOperator>(GEP)->isInBounds();
+ bool isInBounds = cast<GEPOperator>(GEP)->isInBounds() && !NoAssumptions;
// Build a mask for high order bits.
unsigned IntPtrWidth = TD.getPointerSizeInBits();
diff --git a/lib/Analysis/MemoryBuiltins.cpp b/lib/Analysis/MemoryBuiltins.cpp
index 39edaaf1c4..8d99ec3e56 100644
--- a/lib/Analysis/MemoryBuiltins.cpp
+++ b/lib/Analysis/MemoryBuiltins.cpp
@@ -645,7 +645,7 @@ ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
if (!bothKnown(PtrData))
return unknown();
- Value *Offset = EmitGEPOffset(&Builder, *TD, &GEP);
+ Value *Offset = EmitGEPOffset(&Builder, *TD, &GEP, /*NoAssumptions=*/true);
Offset = Builder.CreateAdd(PtrData.second, Offset);
return std::make_pair(PtrData.first, Offset);
}
diff --git a/test/Instrumentation/BoundsChecking/simple.ll b/test/Instrumentation/BoundsChecking/simple.ll
index 3d532c3cf3..16870c78a8 100644
--- a/test/Instrumentation/BoundsChecking/simple.ll
+++ b/test/Instrumentation/BoundsChecking/simple.ll
@@ -116,3 +116,13 @@ define void @f11(i128* byval %x) nounwind {
%3 = load i8* %2, align 4
ret void
}
+
+; CHECK: @f12
+define i64 @f12(i64 %x, i64 %y) nounwind {
+ %1 = tail call i8* @calloc(i64 1, i64 %x)
+; CHECK: mul i64 %y, 8
+ %2 = bitcast i8* %1 to i64*
+ %3 = getelementptr inbounds i64* %2, i64 %y
+ %4 = load i64* %3, align 8
+ ret i64 %4
+}