| 1 | // RUN: %clang_cc1 -fsanitize=implicit-integer-sign-change -fsanitize-recover=implicit-integer-sign-change -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s -implicit-check-not="call void @__ubsan_handle_implicit_conversion" --check-prefixes=CHECK |
| 2 | |
| 3 | // Test plan: |
| 4 | // * Two types - int and char |
| 5 | // * Two signs - signed and unsigned |
| 6 | // * Square that - we have input and output types. |
| 7 | // Thus, there are total of (2*2)^2 == 16 tests. |
| 8 | // These are all the possible variations/combinations of casts. |
| 9 | // However, not all of them should result in the check. |
| 10 | // So here, we *only* check which should and which should not result in checks. |
| 11 | |
| 12 | // CHECK-DAG: @[[LINE_900_SIGN_CHANGE:.*]] = {{.*}}, i32 900, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 13 | // CHECK-DAG: @[[LINE_1000_SIGN_CHANGE:.*]] = {{.*}}, i32 1000, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 14 | // CHECK-DAG: @[[LINE_1100_SIGN_CHANGE:.*]] = {{.*}}, i32 1100, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 15 | // CHECK-DAG: @[[LINE_1200_SIGN_CHANGE:.*]] = {{.*}}, i32 1200, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 16 | // CHECK-DAG: @[[LINE_1300_SIGN_CHANGE:.*]] = {{.*}}, i32 1300, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 17 | // CHECK-DAG: @[[LINE_1400_SIGN_CHANGE:.*]] = {{.*}}, i32 1400, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 18 | // CHECK-DAG: @[[LINE_1500_SIGN_CHANGE:.*]] = {{.*}}, i32 1500, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 19 | // CHECK-DAG: @[[LINE_1600_SIGN_CHANGE:.*]] = {{.*}}, i32 1600, i32 10 }, {{.*}}, {{.*}}, i8 3 } |
| 20 | |
| 21 | //============================================================================// |
| 22 | // Half of the cases do not need the check. // |
| 23 | //============================================================================// |
| 24 | |
| 25 | //----------------------------------------------------------------------------// |
| 26 | // No cast happens at all. No check needed. |
| 27 | //----------------------------------------------------------------------------// |
| 28 | |
| 29 | // CHECK-LABEL: @convert_unsigned_int_to_unsigned_int |
| 30 | unsigned int convert_unsigned_int_to_unsigned_int(unsigned int x) { |
| 31 | #line 100 |
| 32 | return x; |
| 33 | } |
| 34 | |
| 35 | // CHECK-LABEL: @convert_unsigned_char_to_unsigned_char |
| 36 | unsigned char convert_unsigned_char_to_unsigned_char(unsigned char x) { |
| 37 | #line 200 |
| 38 | return x; |
| 39 | } |
| 40 | |
| 41 | // CHECK-LABEL: @convert_signed_int_to_signed_int |
| 42 | signed int convert_signed_int_to_signed_int(signed int x) { |
| 43 | #line 300 |
| 44 | return x; |
| 45 | } |
| 46 | |
| 47 | // CHECK-LABEL: @convert_signed_char_to_signed_char |
| 48 | signed char convert_signed_char_to_signed_char(signed char x) { |
| 49 | #line 400 |
| 50 | return x; |
| 51 | } |
| 52 | |
| 53 | //----------------------------------------------------------------------------// |
| 54 | // Both types are unsigned. No check needed. |
| 55 | //----------------------------------------------------------------------------// |
| 56 | |
| 57 | // CHECK-LABEL: @convert_unsigned_int_to_unsigned_char |
| 58 | unsigned char convert_unsigned_int_to_unsigned_char(unsigned int x) { |
| 59 | #line 500 |
| 60 | return x; |
| 61 | } |
| 62 | |
| 63 | // CHECK-LABEL: @convert_unsigned_char_to_unsigned_int |
| 64 | unsigned int convert_unsigned_char_to_unsigned_int(unsigned char x) { |
| 65 | #line 600 |
| 66 | return x; |
| 67 | } |
| 68 | |
| 69 | //----------------------------------------------------------------------------// |
| 70 | // Source type was unsigned, destination type is signed, but non-negative. |
| 71 | // Because zero-extension happens - the sign bit will be 0. No check needed. |
| 72 | //----------------------------------------------------------------------------// |
| 73 | |
| 74 | // CHECK-LABEL: @convert_unsigned_char_to_signed_int |
| 75 | signed int convert_unsigned_char_to_signed_int(unsigned char x) { |
| 76 | #line 700 |
| 77 | return x; |
| 78 | } |
| 79 | |
| 80 | //----------------------------------------------------------------------------// |
| 81 | // Both types are signed, and have the same sign, since sign-extension happens, |
| 82 | // i.e. the sign bit will be propagated. No check needed. |
| 83 | //----------------------------------------------------------------------------// |
| 84 | |
| 85 | // CHECK-LABEL: @convert_signed_char_to_signed_int |
| 86 | signed int convert_signed_char_to_signed_int(signed char x) { |
| 87 | #line 800 |
| 88 | return x; |
| 89 | } |
| 90 | |
| 91 | //============================================================================// |
| 92 | // The remaining 8 cases *do* need the check. // |
| 93 | //============================================================================// |
| 94 | |
| 95 | // These 3 result in simple 'icmp sge i32 %x, 0' |
| 96 | |
| 97 | // CHECK-LABEL: @convert_unsigned_int_to_signed_int |
| 98 | signed int convert_unsigned_int_to_signed_int(unsigned int x) { |
| 99 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_900_SIGN_CHANGE]] to i8*) |
| 100 | #line 900 |
| 101 | return x; |
| 102 | } |
| 103 | |
| 104 | // CHECK-LABEL: @convert_signed_int_to_unsigned_int |
| 105 | unsigned int convert_signed_int_to_unsigned_int(signed int x) { |
| 106 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_1000_SIGN_CHANGE]] to i8*) |
| 107 | #line 1000 |
| 108 | return x; |
| 109 | } |
| 110 | |
| 111 | // CHECK-LABEL: @convert_signed_int_to_unsigned_char |
| 112 | unsigned char convert_signed_int_to_unsigned_char(signed int x) { |
| 113 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_1100_SIGN_CHANGE]] to i8*) |
| 114 | #line 1100 |
| 115 | return x; |
| 116 | } |
| 117 | |
| 118 | // These 3 result in simple 'icmp sge i8 %x, 0' |
| 119 | |
| 120 | // CHECK-LABEL: @convert_signed_char_to_unsigned_char |
| 121 | unsigned char convert_signed_char_to_unsigned_char(signed char x) { |
| 122 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_1200_SIGN_CHANGE]] to i8*) |
| 123 | #line 1200 |
| 124 | return x; |
| 125 | } |
| 126 | |
| 127 | // CHECK-LABEL: @convert_unsigned_char_to_signed_char |
| 128 | signed char convert_unsigned_char_to_signed_char(unsigned char x) { |
| 129 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_1300_SIGN_CHANGE]] to i8*) |
| 130 | #line 1300 |
| 131 | return x; |
| 132 | } |
| 133 | |
| 134 | // CHECK-LABEL: @convert_signed_char_to_unsigned_int |
| 135 | unsigned int convert_signed_char_to_unsigned_int(signed char x) { |
| 136 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_1400_SIGN_CHANGE]] to i8*) |
| 137 | #line 1400 |
| 138 | return x; |
| 139 | } |
| 140 | |
| 141 | // 'icmp sge i8 (trunc i32 %x), 0' |
| 142 | |
| 143 | // CHECK-LABEL: @convert_unsigned_int_to_signed_char |
| 144 | signed char convert_unsigned_int_to_signed_char(unsigned int x) { |
| 145 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_1500_SIGN_CHANGE]] to i8*) |
| 146 | #line 1500 |
| 147 | return x; |
| 148 | } |
| 149 | |
| 150 | // 'xor i1 (icmp sge i8 (trunc i32 %x), 0), (icmp sge i32 %x, 0)' |
| 151 | |
| 152 | // CHECK-LABEL: @convert_signed_int_to_signed_char |
| 153 | signed char convert_signed_int_to_signed_char(signed int x) { |
| 154 | // CHECK: call void @__ubsan_handle_implicit_conversion(i8* bitcast ({ {{{.*}}}, {{{.*}}}*, {{{.*}}}*, i8 }* @[[LINE_1600_SIGN_CHANGE]] to i8*) |
| 155 | #line 1600 |
| 156 | return x; |
| 157 | } |
| 158 | |