[BoundsSafety] Miscompilation with "-O3" and "-fbounds-safety"
Tested on the latest version (as of today) on Linux:
$ clang-fbound -v
clang version 22.0.0git (https://github.com/swiftlang/llvm-project.git 54d626d2454f3821661d541bea933cb97122e255)
Target: x86_64-unknown-linux-gnu
Normal "-O3" produces a correct binary:
$ clang-fbound -O3 abc.c ; ./a.out
0
With "bounds-safety" and "-O3", it produces an incorrect binary:
$ clang-fbound -O3 -fbounds-safety abc.c ; ./a.out
1
The test C program:
$ cat abc.c
#include <ptrcheck.h>
int printf(const char *, ...);
static int a, c;
static char b[] = {228, 228};
static long d(void) {
unsigned e = 0;
for (; e <= 7; e++)
;
for (;;) {
a = 0;
for (; a <= 5; a++) {
if (b[a]) {
c = e <= 0;
return 8;
}
e = 1;
}
}
}
int main(void) {
d();
printf("%d\n", c);
}
@fhahn This may be a bug in the ConstraintElimination pass? %17 from the previous pass is simplified to true (i.e., %16 = zext i1 true to i32), which doesn't seem right.
; *** IR Dump After ReassociatePass on d ***
; Function Attrs: nounwind ssp uwtable(sync)
define internal fastcc i64 @d() unnamed_addr #0 {
br label %1
1: ; preds = %4, %0
%2 = phi i32 [ 0, %0 ], [ %5, %4 ]
%3 = icmp ult i32 %2, 8
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = add nuw nsw i32 %2, 1
br label %1, !llvm.loop !10
6: ; preds = %6, %1
%7 = phi i32 [ %2, %1 ], [ %7, %6 ]
store i32 0, ptr @a, align 4, !tbaa !6
%8 = icmp ult i32 0, 6
br i1 %8, label %9, label %6
9: ; preds = %6
%10 = zext nneg i32 0 to i64
%11 = getelementptr i8, ptr @b, i64 %10
%12 = icmp ult ptr %11, getelementptr inbounds (i8, ptr @b, i64 2), !annotation !12
%13 = icmp uge ptr %11, @b, !annotation !13
%14 = and i1 %12, %13, !annotation !13
br i1 %14, label %16, label %15, !annotation !12
15: ; preds = %9
tail call void @llvm.ubsantrap(i8 25) #3, !annotation !14
unreachable, !annotation !14
16: ; preds = %9
%17 = icmp eq i32 %7, 0
%18 = zext i1 %17 to i32
store i32 %18, ptr @c, align 4, !tbaa !6
ret i64 poison
}
; *** IR Dump After ConstraintEliminationPass on d ***
; Function Attrs: nounwind ssp uwtable(sync)
define internal fastcc i64 @d() unnamed_addr #0 {
br label %1
1: ; preds = %4, %0
%2 = phi i32 [ 0, %0 ], [ %5, %4 ]
%3 = icmp ult i32 %2, 8
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = add nuw nsw i32 %2, 1
br label %1, !llvm.loop !10
6: ; preds = %6, %1
%7 = phi i32 [ %2, %1 ], [ %7, %6 ]
store i32 0, ptr @a, align 4, !tbaa !6
br i1 true, label %8, label %6
8: ; preds = %6
%9 = zext nneg i32 0 to i64
%10 = getelementptr i8, ptr @b, i64 %9
%11 = icmp ult ptr %10, getelementptr inbounds (i8, ptr @b, i64 2), !annotation !12
%12 = icmp uge ptr %10, @b, !annotation !13
%13 = and i1 %11, %12, !annotation !13
br i1 %13, label %15, label %14, !annotation !12
14: ; preds = %8
tail call void @llvm.ubsantrap(i8 25) #3, !annotation !14
unreachable, !annotation !14
15: ; preds = %8
%16 = zext i1 true to i32
store i32 %16, ptr @c, align 4, !tbaa !6
ret i64 poison
}
Here's a slightly more reduced test case (in IR) that exhibits the behavior:
define internal fastcc i64 @d() unnamed_addr #0 {
br label %1
1: ; preds = %4, %0
%2 = phi i32 [ 0, %0 ], [ %5, %4 ]
%3 = icmp samesign ult i32 %2, 8
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = add nuw nsw i32 %2, 1
br label %1
6: ; preds = %6, %1
%7 = phi i32 [ %2, %1 ], [ %7, %6 ]
store i32 0, ptr @a, align 4
%8 = icmp samesign ult i32 0, 6
br i1 %8, label %9, label %6
9: ; preds = %6
%10 = zext nneg i32 0 to i64
%11 = getelementptr i8, ptr @b, i64 %10
%12 = icmp ult ptr %11, getelementptr inbounds nuw (i8, ptr @b, i64 2)
%13 = icmp uge ptr %11, @b
%14 = and i1 %12, %13
br i1 %14, label %16, label %15
15: ; preds = %9
tail call void @llvm.ubsantrap(i8 25)
unreachable
16: ; preds = %9
%17 = icmp eq i32 %7, 0
%18 = zext i1 %17 to i32
store i32 %18, ptr @c, align 4
ret i64 poison
}
After some investigation of the cause of this bug, we've found the following:
Indeed the problem is with constraint elimination; with opt -passes='constraint-elimination' good.ll, constraint elimination reduces the icmp that sets %17 to a constant true, which isn't right. It happens because of incorrect constraints on %7; in particular, the worklist generates three ConditionFacts which imply that %7 is 0:
%7 = phi i32 [ %2, %1 ], [ %7, %6 ] ule i32 0
%7 = phi i32 [ %2, %1 ], [ %7, %6 ] sge i32 0
%7 = phi i32 [ %2, %1 ], [ %7, %6 ] uge i32 0
But the first of these (ule) is not correct. That ConditionFact is inserted at Line 1705 of ConstraintElimination.cpp. It seems that this this branch is only reached because the SCEV obtained on Line 1672
auto *IV = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&PN));
returns an invariant for the wrong loop. When building the worklist from the basic block 1-4 loop, it returns an SCEV expression referencing that loop (IV->getLoop() returns the 1-4 loop), but when handling the basic block 6 loop, IV->getLoop() also returns the 1-4 loop. With some instrumentation, we've verified that the value of PN here is the correct instruction %7 = phi i32 [ %2, %1 ], [ %7, %6 ].
The conclusion is that the bug may be deeper in vanilla LLVM, as we expect SE.getSCEV(&PN) always to return an SCEVAddRecExpr on the same loop that contains PN, but in this case, it does not. Replacing the call with
auto *IV = dyn_cast<SCEVAddRecExpr>(SE.getSCEVAtScope(&PN, L));
to force an SCEV expression on the correct loop does not fix the problem because it causes other test cases to fail.