catch-implicit-integer-truncations.cpp
8.96 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// RUN: %clang_cc1 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK
// RUN: %clang_cc1 -fsanitize=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -fno-sanitize-recover=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,CHECK-SANITIZE,CHECK-SANITIZE-ANYRECOVER,CHECK-SANITIZE-NORECOVER
// RUN: %clang_cc1 -fsanitize=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -fsanitize-recover=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,CHECK-SANITIZE,CHECK-SANITIZE-ANYRECOVER,CHECK-SANITIZE-RECOVER
// RUN: %clang_cc1 -fsanitize=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -fsanitize-trap=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,CHECK-SANITIZE,CHECK-SANITIZE-TRAP
extern "C" { // Disable name mangling.
// ========================================================================== //
// Check that explicit cast does not interfere with implicit conversion
// ========================================================================== //
// These contain one implicit truncating conversion, and one explicit truncating cast.
// We want to make sure that we still diagnose the implicit conversion.
// Implicit truncation after explicit truncation.
// CHECK-LABEL: @explicit_cast_interference0
unsigned char explicit_cast_interference0(unsigned int c) {
// CHECK-SANITIZE: %[[ANYEXT:.*]] = zext i8 %[[DST:.*]] to i16, !nosanitize
// CHECK-SANITIZE: call
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (unsigned short)c;
}
// Implicit truncation before explicit truncation.
// CHECK-LABEL: @explicit_cast_interference1
unsigned char explicit_cast_interference1(unsigned int c) {
// CHECK-SANITIZE: %[[ANYEXT:.*]] = zext i16 %[[DST:.*]] to i32, !nosanitize
// CHECK-SANITIZE: call
// CHECK-SANITIZE-NOT: call
// CHECK: }
unsigned short b;
return (unsigned char)(b = c);
}
// ========================================================================== //
// The expected true-negatives.
// ========================================================================== //
// Explicit truncating casts.
// ========================================================================== //
// CHECK-LABEL: @explicit_unsigned_int_to_unsigned_char
unsigned char explicit_unsigned_int_to_unsigned_char(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (unsigned char)src;
}
// CHECK-LABEL: @explicit_signed_int_to_unsigned_char
unsigned char explicit_signed_int_to_unsigned_char(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (unsigned char)src;
}
// CHECK-LABEL: @explicit_unsigned_int_to_signed_char
signed char explicit_unsigned_int_to_signed_char(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (signed char)src;
}
// CHECK-LABEL: @explicit_signed_int_to_signed_char
signed char explicit_signed_int_to_signed_char(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (signed char)src;
}
// Explicit NOP casts.
// ========================================================================== //
// CHECK-LABEL: @explicit_unsigned_int_to_unsigned_int
unsigned int explicit_unsigned_int_to_unsigned_int(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (unsigned int)src;
}
// CHECK-LABEL: @explicit_signed_int_to_signed_int
signed int explicit_signed_int_to_signed_int(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (signed int)src;
}
// CHECK-LABEL: @explicit_unsigned_char_to_signed_char
unsigned char explicit_unsigned_char_to_signed_char(unsigned char src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (unsigned char)src;
}
// CHECK-LABEL: @explicit_signed_char_to_signed_char
signed char explicit_signed_char_to_signed_char(signed char src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return (signed char)src;
}
// Explicit functional truncating casts.
// ========================================================================== //
using UnsignedChar = unsigned char;
using SignedChar = signed char;
using UnsignedInt = unsigned int;
using SignedInt = signed int;
// CHECK-LABEL: @explicit_functional_unsigned_int_to_unsigned_char
unsigned char explicit_functional_unsigned_int_to_unsigned_char(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return UnsignedChar(src);
}
// CHECK-LABEL: @explicit_functional_signed_int_to_unsigned_char
unsigned char explicit_functional_signed_int_to_unsigned_char(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return UnsignedChar(src);
}
// CHECK-LABEL: @explicit_functional_unsigned_int_to_signed_char
signed char explicit_functional_unsigned_int_to_signed_char(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return SignedChar(src);
}
// CHECK-LABEL: @explicit_functional_signed_int_to_signed_char
signed char explicit_functional_signed_int_to_signed_char(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return SignedChar(src);
}
// Explicit functional NOP casts.
// ========================================================================== //
// CHECK-LABEL: @explicit_functional_unsigned_int_to_unsigned_int
unsigned int explicit_functional_unsigned_int_to_unsigned_int(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return UnsignedInt(src);
}
// CHECK-LABEL: @explicit_functional_signed_int_to_signed_int
signed int explicit_functional_signed_int_to_signed_int(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return SignedInt(src);
}
// CHECK-LABEL: @explicit_functional_unsigned_char_to_signed_char
unsigned char explicit_functional_unsigned_char_to_signed_char(unsigned char src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return UnsignedChar(src);
}
// CHECK-LABEL: @explicit_functional_signed_char_to_signed_char
signed char explicit_functional_signed_char_to_signed_char(signed char src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return SignedChar(src);
}
// Explicit C++-style casts truncating casts.
// ========================================================================== //
// CHECK-LABEL: @explicit_cppstyleunsigned_int_to_unsigned_char
unsigned char explicit_cppstyleunsigned_int_to_unsigned_char(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<unsigned char>(src);
}
// CHECK-LABEL: @explicit_cppstylesigned_int_to_unsigned_char
unsigned char explicit_cppstylesigned_int_to_unsigned_char(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<unsigned char>(src);
}
// CHECK-LABEL: @explicit_cppstyleunsigned_int_to_signed_char
signed char explicit_cppstyleunsigned_int_to_signed_char(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<signed char>(src);
}
// CHECK-LABEL: @explicit_cppstylesigned_int_to_signed_char
signed char explicit_cppstylesigned_int_to_signed_char(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<signed char>(src);
}
// Explicit C++-style casts NOP casts.
// ========================================================================== //
// CHECK-LABEL: @explicit_cppstyleunsigned_int_to_unsigned_int
unsigned int explicit_cppstyleunsigned_int_to_unsigned_int(unsigned int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<unsigned int>(src);
}
// CHECK-LABEL: @explicit_cppstylesigned_int_to_signed_int
signed int explicit_cppstylesigned_int_to_signed_int(signed int src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<signed int>(src);
}
// CHECK-LABEL: @explicit_cppstyleunsigned_char_to_signed_char
unsigned char explicit_cppstyleunsigned_char_to_signed_char(unsigned char src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<unsigned char>(src);
}
// CHECK-LABEL: @explicit_cppstylesigned_char_to_signed_char
signed char explicit_cppstylesigned_char_to_signed_char(signed char src) {
// CHECK-SANITIZE-NOT: call
// CHECK: }
return static_cast<signed char>(src);
}
} // extern "C"
// ---------------------------------------------------------------------------//
// A problematic true-negative involving simple C++ code.
// The problem is tha the NoOp ExplicitCast is directly within MaterializeTemporaryExpr(),
// so a special care is neeeded.
// See https://reviews.llvm.org/D48958#1161345
template <typename a>
a b(a c, const a &d) {
if (d)
;
return c;
}
extern "C" { // Disable name mangling.
// CHECK-LABEL: @false_positive_with_MaterializeTemporaryExpr
int false_positive_with_MaterializeTemporaryExpr() {
// CHECK-SANITIZE-NOT: call{{.*}}ubsan
// CHECK: }
int e = b<unsigned>(4, static_cast<unsigned>(4294967296));
return e;
}
// ---------------------------------------------------------------------------//
} // extern "C"