2012-11-27 23:01:55 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsanitize=unsigned-integer-overflow %s -emit-llvm -o - | FileCheck %s
|
|
|
|
// Verify checked operations are emitted for integers and longs.
|
|
|
|
// unsigned short/char's tested in unsigned-promotion.c
|
|
|
|
|
|
|
|
unsigned long li, lj, lk;
|
|
|
|
unsigned int ii, ij, ik;
|
|
|
|
|
|
|
|
extern void opaquelong(unsigned long);
|
|
|
|
extern void opaqueint(unsigned int);
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testlongadd()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testlongadd() {
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i64, i64* @lj
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = load i64, i64* @lk
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T3:%.*]] = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[T1]], i64 [[T2]])
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T3]], 0
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = extractvalue { i64, i1 } [[T3]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_add_overflow
|
|
|
|
li = lj + lk;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testlongsub()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testlongsub() {
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i64, i64* @lj
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = load i64, i64* @lk
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T3:%.*]] = call { i64, i1 } @llvm.usub.with.overflow.i64(i64 [[T1]], i64 [[T2]])
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T3]], 0
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = extractvalue { i64, i1 } [[T3]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_sub_overflow
|
|
|
|
li = lj - lk;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testlongmul()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testlongmul() {
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i64, i64* @lj
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = load i64, i64* @lk
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T3:%.*]] = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 [[T1]], i64 [[T2]])
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T3]], 0
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = extractvalue { i64, i1 } [[T3]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_mul_overflow
|
|
|
|
li = lj * lk;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testlongpostinc()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testlongpostinc() {
|
|
|
|
opaquelong(li++);
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i64, i64* @li
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T2:%.*]] = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[T1]], i64 1)
|
|
|
|
// CHECK-NEXT: [[T3:%.*]] = extractvalue { i64, i1 } [[T2]], 0
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T2]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_add_overflow
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testlongpreinc()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testlongpreinc() {
|
|
|
|
opaquelong(++li);
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i64, i64* @li
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T2:%.*]] = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[T1]], i64 1)
|
|
|
|
// CHECK-NEXT: [[T3:%.*]] = extractvalue { i64, i1 } [[T2]], 0
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T2]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_add_overflow
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testintadd()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testintadd() {
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i32, i32* @ij
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = load i32, i32* @ik
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T3:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T1]], i32 [[T2]])
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T3]], 0
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T3]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_add_overflow
|
|
|
|
ii = ij + ik;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testintsub()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testintsub() {
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i32, i32* @ij
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = load i32, i32* @ik
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T3:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[T1]], i32 [[T2]])
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T3]], 0
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T3]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_sub_overflow
|
|
|
|
ii = ij - ik;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testintmul()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testintmul() {
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i32, i32* @ij
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = load i32, i32* @ik
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T3:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[T1]], i32 [[T2]])
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T3]], 0
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T3]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_mul_overflow
|
|
|
|
ii = ij * ik;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testintpostinc()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testintpostinc() {
|
|
|
|
opaqueint(ii++);
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i32, i32* @ii
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T1]], i32 1)
|
|
|
|
// CHECK-NEXT: [[T3:%.*]] = extractvalue { i32, i1 } [[T2]], 0
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T2]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_add_overflow
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @testintpreinc()
|
2012-11-27 23:01:55 +08:00
|
|
|
void testintpreinc() {
|
|
|
|
opaqueint(++ii);
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[T1:%.*]] = load i32, i32* @ii
|
2012-11-27 23:01:55 +08:00
|
|
|
// CHECK-NEXT: [[T2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T1]], i32 1)
|
|
|
|
// CHECK-NEXT: [[T3:%.*]] = extractvalue { i32, i1 } [[T2]], 0
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T2]], 1
|
|
|
|
// CHECK: call void @__ubsan_handle_add_overflow
|
|
|
|
}
|