62 lines · c
1// RUN: %clang_cc1 -x c -emit-llvm -o - -triple x86_64-apple-darwin10 %s \2// RUN: -w -fsanitize=signed-integer-overflow,unsigned-integer-overflow,integer-divide-by-zero,float-divide-by-zero \3// RUN: | FileCheck %s4 5// CHECK-LABEL: define{{.*}} void @foo6// CHECK-NOT: !nosanitize7void foo(const int *p) {8 // __builtin_prefetch expects its optional arguments to be constant integers.9 // Check that ubsan does not instrument any safe arithmetic performed in10 // operands to __builtin_prefetch. (A clang frontend check should reject11 // unsafe arithmetic in these operands.)12 13 __builtin_prefetch(p, 0 + 1, 0 + 3);14 __builtin_prefetch(p, 1 - 0, 3 - 0);15 __builtin_prefetch(p, 1 * 1, 1 * 3);16 __builtin_prefetch(p, 1 / 1, 3 / 1);17 __builtin_prefetch(p, 3 % 2, 3 % 1);18 19 __builtin_prefetch(p, 0U + 1U, 0U + 3U);20 __builtin_prefetch(p, 1U - 0U, 3U - 0U);21 __builtin_prefetch(p, 1U * 1U, 1U * 3U);22 __builtin_prefetch(p, 1U / 1U, 3U / 1U);23 __builtin_prefetch(p, 3U % 2U, 3U % 1U);24}25 26// CHECK-LABEL: define{{.*}} void @ub_constant_arithmetic27void ub_constant_arithmetic(void) {28 // Check that we still instrument unsafe arithmetic, even if it is known to29 // be unsafe at compile time.30 31 int INT_MIN = 0xffffffff;32 int INT_MAX = 0x7fffffff;33 34 // CHECK: call void @__ubsan_handle_add_overflow35 // CHECK: call void @__ubsan_handle_add_overflow36 INT_MAX + 1;37 INT_MAX + -1;38 39 // CHECK: call void @__ubsan_handle_negate_overflow40 // CHECK: call void @__ubsan_handle_sub_overflow41 -INT_MIN;42 -INT_MAX - 2;43 44 // CHECK: call void @__ubsan_handle_mul_overflow45 // CHECK: call void @__ubsan_handle_mul_overflow46 INT_MAX * INT_MAX;47 INT_MIN * INT_MIN;48 49 // CHECK: call void @__ubsan_handle_divrem_overflow50 // CHECK: call void @__ubsan_handle_divrem_overflow51 1 / 0;52 INT_MIN / -1;53 54 // CHECK: call void @__ubsan_handle_divrem_overflow55 // CHECK: call void @__ubsan_handle_divrem_overflow56 1 % 0;57 INT_MIN % -1;58 59 // CHECK: call void @__ubsan_handle_divrem_overflow60 1.0 / 0.0;61}62