Skip to content

Commit d3a601b

Browse files
committedJan 30, 2017
Re-apply "[ubsan] Sanity-check shift amounts before truncation"
This re-applies r293343 (reverts commit r293475) with a fix for an assertion failure caused by a missing integer cast. I tested this patch by using the built compiler to compile X86FastISel.cpp.o with ubsan. Original commit message: Ubsan does not report UB shifts in some cases where the shift exponent needs to be truncated to match the type of the shift base. We perform a range check on the truncated shift amount, leading to false negatives. Fix the issue (PR27271) by performing the range check on the original shift amount. Differential Revision: https://reviews.llvm.org/D29234 llvm-svn: 293572
1 parent 578cf7a commit d3a601b

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed
 

‎clang/lib/CodeGen/CGExprScalar.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -2751,8 +2751,8 @@ Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
27512751
isa<llvm::IntegerType>(Ops.LHS->getType())) {
27522752
CodeGenFunction::SanitizerScope SanScope(&CGF);
27532753
SmallVector<std::pair<Value *, SanitizerMask>, 2> Checks;
2754-
llvm::Value *WidthMinusOne = GetWidthMinusOneValue(Ops.LHS, RHS);
2755-
llvm::Value *ValidExponent = Builder.CreateICmpULE(RHS, WidthMinusOne);
2754+
llvm::Value *WidthMinusOne = GetWidthMinusOneValue(Ops.LHS, Ops.RHS);
2755+
llvm::Value *ValidExponent = Builder.CreateICmpULE(Ops.RHS, WidthMinusOne);
27562756

27572757
if (SanitizeExponent) {
27582758
Checks.push_back(
@@ -2767,12 +2767,14 @@ Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
27672767
llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
27682768
llvm::BasicBlock *CheckShiftBase = CGF.createBasicBlock("check");
27692769
Builder.CreateCondBr(ValidExponent, CheckShiftBase, Cont);
2770+
llvm::Value *PromotedWidthMinusOne =
2771+
(RHS == Ops.RHS) ? WidthMinusOne
2772+
: GetWidthMinusOneValue(Ops.LHS, RHS);
27702773
CGF.EmitBlock(CheckShiftBase);
2771-
llvm::Value *BitsShiftedOff =
2772-
Builder.CreateLShr(Ops.LHS,
2773-
Builder.CreateSub(WidthMinusOne, RHS, "shl.zeros",
2774-
/*NUW*/true, /*NSW*/true),
2775-
"shl.check");
2774+
llvm::Value *BitsShiftedOff = Builder.CreateLShr(
2775+
Ops.LHS, Builder.CreateSub(PromotedWidthMinusOne, RHS, "shl.zeros",
2776+
/*NUW*/ true, /*NSW*/ true),
2777+
"shl.check");
27762778
if (CGF.getLangOpts().CPlusPlus) {
27772779
// In C99, we are not permitted to shift a 1 bit into the sign bit.
27782780
// Under C++11's rules, shifting a 1 bit into the sign bit is

‎clang/test/CodeGen/ubsan-shift.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %clang_cc1 -triple=x86_64-apple-darwin -fsanitize=shift-exponent,shift-base -emit-llvm %s -o - | FileCheck %s
2+
3+
// CHECK-LABEL: define i32 @f1
4+
int f1(int c, int shamt) {
5+
// CHECK: icmp ule i32 %{{.*}}, 31, !nosanitize
6+
// CHECK: icmp ule i32 %{{.*}}, 31, !nosanitize
7+
return 1 << (c << shamt);
8+
}
9+
10+
// CHECK-LABEL: define i32 @f2
11+
int f2(long c, int shamt) {
12+
// CHECK: icmp ule i32 %{{.*}}, 63, !nosanitize
13+
// CHECK: icmp ule i64 %{{.*}}, 31, !nosanitize
14+
return 1 << (c << shamt);
15+
}
16+
17+
// CHECK-LABEL: define i32 @f3
18+
unsigned f3(unsigned c, int shamt) {
19+
// CHECK: icmp ule i32 %{{.*}}, 31, !nosanitize
20+
// CHECK: icmp ule i32 %{{.*}}, 31, !nosanitize
21+
return 1U << (c << shamt);
22+
}
23+
24+
// CHECK-LABEL: define i32 @f4
25+
unsigned f4(unsigned long c, int shamt) {
26+
// CHECK: icmp ule i32 %{{.*}}, 63, !nosanitize
27+
// CHECK: icmp ule i64 %{{.*}}, 31, !nosanitize
28+
return 1U << (c << shamt);
29+
}
30+
31+
// CHECK-LABEL: define i32 @f5
32+
int f5(int c, long long shamt) {
33+
// CHECK: icmp ule i64 %{{[0-9]+}}, 31, !nosanitize
34+
//
35+
// CHECK: sub nuw nsw i32 31, %sh_prom, !nosanitize
36+
// CHECK: lshr i32 %{{.*}}, %shl.zeros, !nosanitize
37+
return c << shamt;
38+
}
39+
40+
// CHECK-LABEL: define i32 @f6
41+
int f6(int c, int shamt) {
42+
// CHECK: icmp ule i32 %[[WIDTH:.*]], 31, !nosanitize
43+
//
44+
// CHECK: sub nuw nsw i32 31, %[[WIDTH]], !nosanitize
45+
// CHECK: lshr i32 %{{.*}}, %shl.zeros, !nosanitize
46+
return c << shamt;
47+
}

0 commit comments

Comments
 (0)
Please sign in to comment.