1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s --check-prefixes=CHECK,POISON
// RUN: %clang_cc1 -triple arm64-none-linux-gnu -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s --check-prefixes=CHECK,NOPOISON
// A zero input to __bultin_ctz/clz is considered UB even if the target does not
// want to optimize based on zero input being undefined.
// CHECK: define{{.*}} void @check_ctz
void check_ctz(int n) {
// CHECK: [[NOT_ZERO:%.*]] = icmp ne i32 [[N:%.*]], 0, !nosanitize
// CHECK-NEXT: br i1 [[NOT_ZERO]]
//
// Handler block:
// CHECK: call void @__ubsan_handle_invalid_builtin
// CHECK-NEXT: unreachable
//
// Continuation block:
// POISON: call i32 @llvm.cttz.i32(i32 [[N]], i1 true)
// NOPOISON: call i32 @llvm.cttz.i32(i32 [[N]], i1 false)
__builtin_ctz(n);
// CHECK: call void @__ubsan_handle_invalid_builtin
__builtin_ctzl(n);
// CHECK: call void @__ubsan_handle_invalid_builtin
__builtin_ctzll(n);
// CHECK: call void @__ubsan_handle_invalid_builtin
__builtin_ctzg((unsigned int)n);
}
// CHECK: define{{.*}} void @check_clz
void check_clz(int n) {
// CHECK: [[NOT_ZERO:%.*]] = icmp ne i32 [[N:%.*]], 0, !nosanitize
// CHECK-NEXT: br i1 [[NOT_ZERO]]
//
// Handler block:
// CHECK: call void @__ubsan_handle_invalid_builtin
// CHECK-NEXT: unreachable
//
// Continuation block:
// POISON: call i32 @llvm.ctlz.i32(i32 [[N]], i1 true)
// NOPOISON: call i32 @llvm.ctlz.i32(i32 [[N]], i1 false)
__builtin_clz(n);
// CHECK: call void @__ubsan_handle_invalid_builtin
__builtin_clzl(n);
// CHECK: call void @__ubsan_handle_invalid_builtin
__builtin_clzll(n);
// CHECK: call void @__ubsan_handle_invalid_builtin
__builtin_clzg((unsigned int)n);
}
// CHECK: define{{.*}} void @check_assume
void check_assume(int n) {
// CHECK: [[TOBOOL:%.*]] = icmp ne i32 [[N:%.*]], 0
// CHECK-NEXT: br i1 [[TOBOOL]]
//
// Handler block:
// CHECK: call void @__ubsan_handle_invalid_builtin
// CHECK-NEXT: unreachable
//
// Continuation block:
// CHECK: call void @llvm.assume(i1 [[TOBOOL]])
__builtin_assume(n);
// CHECK: call void @__ubsan_handle_invalid_builtin
__attribute__((assume(n)));
}
|