Skip to content

Commit 19ddafa

Browse files
[llvm] Fix ObjectSizeOffsetVisitor behavior in exact mode upon negati… (#116955)
…ve offset In Exact mode, the approximation of returning (0,0) is invalid. It only holds in min/max mode.
1 parent 68f7b07 commit 19ddafa

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,7 @@ static APInt getSizeWithOverflow(const SizeOffsetAPInt &Data) {
565565
APInt Size = Data.Size;
566566
APInt Offset = Data.Offset;
567567

568-
assert(!Offset.isNegative() &&
569-
"size for a pointer before the allocated object is ambiguous");
570-
571-
if (Size.ult(Offset))
568+
if (Offset.isNegative() || Size.ult(Offset))
572569
return APInt::getZero(Size.getBitWidth());
573570

574571
return Size - Offset;
@@ -756,10 +753,14 @@ OffsetSpan ObjectSizeOffsetVisitor::computeImpl(Value *V) {
756753
}
757754

758755
// We end up pointing on a location that's outside of the original object.
759-
// This is UB, and we'd rather return an empty location then.
760756
if (ORT.knownBefore() && ORT.Before.isNegative()) {
761-
ORT.Before = APInt::getZero(ORT.Before.getBitWidth());
762-
ORT.After = APInt::getZero(ORT.Before.getBitWidth());
757+
// This is UB, and we'd rather return an empty location then.
758+
if (Options.EvalMode == ObjectSizeOpts::Mode::Min ||
759+
Options.EvalMode == ObjectSizeOpts::Mode::Max) {
760+
ORT.Before = APInt::getZero(ORT.Before.getBitWidth());
761+
ORT.After = APInt::getZero(ORT.Before.getBitWidth());
762+
}
763+
// Otherwise it's fine, caller can handle negative offset.
763764
}
764765
return ORT;
765766
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; Check that negative oob gep do not generate invalid check.
3+
; RUN: opt < %s -passes=bounds-checking -S | FileCheck %s
4+
target datalayout = "e-p:64:64:64-p1:16:16:16-p2:64:64:64:48-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"
5+
6+
7+
@str = global [100 x i8] zeroinitializer, align 1
8+
9+
define i16 @main() {
10+
; CHECK-LABEL: @main(
11+
; CHECK-NEXT: entry:
12+
; CHECK-NEXT: br label [[FOR_COND:%.*]]
13+
; CHECK: for.cond:
14+
; CHECK-NEXT: [[I_0:%.*]] = phi i8 [ 65, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[TMP4:%.*]] ]
15+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i8 [[I_0]], 76
16+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_END:%.*]], label [[TMP4]]
17+
; CHECK: for.inc:
18+
; CHECK-NEXT: [[I_0_C:%.*]] = sext i8 [[I_0]] to i64
19+
; CHECK-NEXT: [[TMP0:%.*]] = add i64 -65, [[I_0_C]]
20+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr getelementptr (i8, ptr @str, i8 -65), i8 [[I_0]]
21+
; CHECK-NEXT: [[TMP1:%.*]] = sub i64 100, [[TMP0]]
22+
; CHECK-NEXT: store i8 [[I_0]], ptr [[GEP]], align 1
23+
; CHECK-NEXT: [[INC]] = add nuw nsw i8 [[I_0]], 1
24+
; CHECK-NEXT: br label [[FOR_COND]]
25+
; CHECK: for.end:
26+
; CHECK-NEXT: ret i16 0
27+
;
28+
entry:
29+
br label %for.cond
30+
31+
for.cond:
32+
%i.0 = phi i8 [ 65, %entry ], [ %inc, %for.inc ]
33+
%exitcond.not = icmp eq i8 %i.0, 76
34+
br i1 %exitcond.not, label %for.end, label %for.inc
35+
36+
for.inc: ; preds = %for.cond
37+
%gep = getelementptr i8, ptr getelementptr (i8, ptr @str, i8 -65), i8 %i.0
38+
store i8 %i.0, ptr %gep, align 1
39+
%inc = add nuw nsw i8 %i.0, 1
40+
br label %for.cond
41+
42+
for.end:
43+
ret i16 0
44+
}
45+

0 commit comments

Comments
 (0)