diff options
author | Vedant Kumar <vsk@apple.com> | 2017-07-29 00:19:51 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2017-07-29 00:19:51 +0000 |
commit | 10c3102071efff9ecf3828d3e4e0bc528c493d81 (patch) | |
tree | 9ce2bc823acb6d0f3567bcd1b8a0349fbc772d00 /clang/test/CodeGen/ubsan-builtin-checks.c | |
parent | 4335c3992a521800d4473315847083f0beaf244f (diff) | |
download | bcm5719-llvm-10c3102071efff9ecf3828d3e4e0bc528c493d81.tar.gz bcm5719-llvm-10c3102071efff9ecf3828d3e4e0bc528c493d81.zip |
[ubsan] Diagnose invalid uses of builtins (clang)
On some targets, passing zero to the clz() or ctz() builtins has undefined
behavior. I ran into this issue while debugging UB in __hash_table from libcxx:
the bug I was seeing manifested itself differently under -O0 vs -Os, due to a
UB call to clz() (see: libcxx/r304617).
This patch introduces a check which can detect UB calls to builtins.
llvm.org/PR26979
Differential Revision: https://reviews.llvm.org/D34590
llvm-svn: 309459
Diffstat (limited to 'clang/test/CodeGen/ubsan-builtin-checks.c')
-rw-r--r-- | clang/test/CodeGen/ubsan-builtin-checks.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/clang/test/CodeGen/ubsan-builtin-checks.c b/clang/test/CodeGen/ubsan-builtin-checks.c new file mode 100644 index 00000000000..9733b0e0479 --- /dev/null +++ b/clang/test/CodeGen/ubsan-builtin-checks.c @@ -0,0 +1,44 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s +// RUN: %clang_cc1 -triple arm64-none-linux-gnu -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s --check-prefix=NOT-UB + +// NOT-UB-NOT: __ubsan_handle_invalid_builtin + +// 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: + // CHECK: call i32 @llvm.cttz.i32(i32 [[N]], i1 true) + __builtin_ctz(n); + + // CHECK: call void @__ubsan_handle_invalid_builtin + __builtin_ctzl(n); + + // CHECK: call void @__ubsan_handle_invalid_builtin + __builtin_ctzll(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: + // CHECK: call i32 @llvm.ctlz.i32(i32 [[N]], i1 true) + __builtin_clz(n); + + // CHECK: call void @__ubsan_handle_invalid_builtin + __builtin_clzl(n); + + // CHECK: call void @__ubsan_handle_invalid_builtin + __builtin_clzll(n); +} |