Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/taint-assert.c

raw
1 /* We need this, otherwise the warnings are emitted inside the macros, which
2 makes it hard to write the DejaGnu directives. */
3 /* { dg-additional-options " -ftrack-macro-expansion=0" } */
4
5 #include "analyzer-decls.h"
6
7 /* An assertion macro that has a call to a __noreturn__ function. */
8
9 extern void my_assert_fail (const char *expr, const char *file, int line)
10 __attribute__ ((__noreturn__));
11
12 #define MY_ASSERT_1(EXPR) \
13 do { if (!(EXPR)) my_assert_fail (#EXPR, __FILE__, __LINE__); } while (0)
14
15 int
16 test_not_tainted_MY_ASSERT_1 (int n)
17 {
18 MY_ASSERT_1 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
19 return n * n;
20 }
21
22 int __attribute__((tainted_args))
23 test_tainted_MY_ASSERT_1 (int n)
24 {
25 MY_ASSERT_1 (n > 0); /* { dg-warning "use of attacked-controlled value in condition for assertion \\\[CWE-617\\\] \\\[-Wanalyzer-tainted-assertion\\\]" "warning" } */
26 /* { dg-message "treating 'my_assert_fail' as an assertion failure handler due to '__attribute__\\(\\(__noreturn__\\)\\)'" "final event" { target *-*-* } .-1 } */
27 return n * n;
28 }
29
30
31 /* An assertion macro that has a call to __builtin_unreachable. */
32
33 #define MY_ASSERT_2(EXPR) \
34 do { if (!(EXPR)) __builtin_unreachable (); } while (0)
35
36 int
37 test_not_tainted_MY_ASSERT_2 (int n)
38 {
39 MY_ASSERT_2 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
40 return n * n;
41 }
42
43 int __attribute__((tainted_args))
44 test_tainted_MY_ASSERT_2 (int n)
45 {
46 MY_ASSERT_2 (n > 0); /* { dg-warning "-Wanalyzer-tainted-assertion" "warning" } */
47 /* { dg-message "treating '__builtin_unreachable' as an assertion failure handler" "final event" { target *-*-* } .-1 } */
48 return n * n;
49 }
50
51
52 /* An assertion macro that's preprocessed away.
53 The analyzer doesn't see this, so can't warn. */
54
55 #define MY_ASSERT_3(EXPR) do { } while (0)
56
57 int
58 test_not_tainted_MY_ASSERT_3 (int n)
59 {
60 MY_ASSERT_3 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
61 return n * n;
62 }
63
64 int __attribute__((tainted_args))
65 test_tainted_MY_ASSERT_3 (int n)
66 {
67 MY_ASSERT_3 (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
68 return n * n;
69 }
70
71
72 /* A macro that isn't an assertion. */
73
74 extern void do_something_benign ();
75
76 #define NOT_AN_ASSERT(EXPR) \
77 do { if (!(EXPR)) do_something_benign (); } while (0)
78
79 int
80 test_not_tainted_NOT_AN_ASSERT (int n)
81 {
82 NOT_AN_ASSERT (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
83 return n * n;
84 }
85
86 int __attribute__((tainted_args))
87 test_tainted_NOT_AN_ASSERT (int n)
88 {
89 NOT_AN_ASSERT (n > 0); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
90 return n * n;
91 }
92
93
94 /* A condition that isn't an assertion. */
95
96 int __attribute__((tainted_args))
97 test_tainted_condition (int n)
98 {
99 if (n > 0) /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
100 return 1;
101 else
102 return -1;
103 }
104
105
106 /* More complicated expressions in assertions. */
107
108 int g;
109
110 void __attribute__((tainted_args))
111 test_compound_condition_in_assert_1 (int n)
112 {
113 MY_ASSERT_1 ((n * 2) < (g + 3)); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
114 }
115
116 void __attribute__((tainted_args))
117 test_compound_condition_in_assert_2 (int x, int y)
118 {
119 MY_ASSERT_1 (x < 100 && y < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
120 }
121
122 void __attribute__((tainted_args))
123 test_compound_condition_in_assert_3 (int x, int y)
124 {
125 MY_ASSERT_1 (x < 100 || y < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
126 }
127
128 void __attribute__((tainted_args))
129 test_sanitized_expression_in_assert (int n)
130 {
131 __analyzer_dump_state ("taint", n); /* { dg-warning "state: 'tainted'" } */
132 if (n < 0 || n >= 100)
133 return;
134 __analyzer_dump_state ("taint", n); /* { dg-warning "state: 'stop'" } */
135 MY_ASSERT_1 (n < 200); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
136 }
137
138 void __attribute__((tainted_args))
139 test_sanitization_then_ok_assertion (unsigned n)
140 {
141 if (n >= 100)
142 return;
143
144 /* Shouldn't warn here, as g isn't attacker-controlled. */
145 MY_ASSERT_1 (g > 42); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
146 }
147
148 void __attribute__((tainted_args))
149 test_good_assert_then_bad_assert (unsigned n)
150 {
151 /* Shouldn't warn here, as g isn't attacker-controlled. */
152 MY_ASSERT_1 (g > 42); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
153
154 /* ...but n is: */
155 MY_ASSERT_1 (n < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
156 }
157
158 void __attribute__((tainted_args))
159 test_bad_assert_then_good_assert (unsigned n)
160 {
161 MY_ASSERT_1 (n < 100); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
162 MY_ASSERT_1 (g > 42); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
163 }
164
165
166 /* */
167
168 void __attribute__((tainted_args))
169 test_zero_MY_ASSERT_1 (unsigned n)
170 {
171 if (n >= 100)
172 MY_ASSERT_1 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
173 }
174
175 void __attribute__((tainted_args))
176 test_nonzero_MY_ASSERT_1 (unsigned n)
177 {
178 if (n >= 100)
179 MY_ASSERT_1 (1); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
180 }
181
182 void __attribute__((tainted_args))
183 test_zero_MY_ASSERT_2 (unsigned n)
184 {
185 if (n >= 100)
186 MY_ASSERT_2 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
187 }
188
189 void __attribute__((tainted_args))
190 test_nonzero_MY_ASSERT_2 (unsigned n)
191 {
192 if (n >= 100)
193 MY_ASSERT_2 (1); /* { dg-bogus "-Wanalyzer-tainted-assertion" } */
194 }
195
196
197 /* Assertions that call a subroutine to do validity checking. */
198
199 static int
200 __analyzer_valid_1 (int x)
201 {
202 return x < 100;
203 }
204
205 void __attribute__((tainted_args))
206 test_assert_calling_valid_1 (int n)
207 {
208 MY_ASSERT_1 (__analyzer_valid_1 (n)); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
209 }
210
211 static int
212 __analyzer_valid_2 (int x)
213 {
214 return x < 100;
215 }
216
217 void __attribute__((tainted_args))
218 test_assert_calling_valid_2 (int n)
219 {
220 MY_ASSERT_1 (__analyzer_valid_2 (n)); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
221 }
222
223 static int
224 __analyzer_valid_3 (int x, int y)
225 {
226 if (x >= 100)
227 return 0;
228 if (y >= 100)
229 return 0;
230 return 1;
231 }
232
233 void __attribute__((tainted_args))
234 test_assert_calling_valid_3 (int a, int b)
235 {
236 MY_ASSERT_1 (__analyzer_valid_3 (a, b)); /* { dg-warning "-Wanalyzer-tainted-assertion" "TODO" { xfail *-*-* } } */
237 }
238
239
240 /* 'switch' statements with supposedly unreachable cases/defaults. */
241
242 int __attribute__((tainted_args))
243 test_switch_default (int n)
244 {
245 switch (n) /* { dg-message "use of attacker-controlled value for control flow" "why" } */
246 /* { dg-message "following 'default:' branch" "dest" { target *-*-* } .-1 } */
247 {
248 case 0:
249 return 5;
250 case 1:
251 return 22;
252 case 2:
253 return -1;
254 default:
255 /* The wording is rather inaccurate here. */
256 __builtin_unreachable (); /* { dg-warning "use of attacked-controlled value in condition for assertion" } */
257 }
258 }
259
260 int __attribute__((tainted_args))
261 test_switch_unhandled_case (int n)
262 {
263 switch (n) /* { dg-message "use of attacker-controlled value for control flow" "why" } */
264 /* { dg-message "following 'default:' branch" "dest" { target *-*-* } .-1 } */
265 {
266 case 0:
267 return 5;
268 case 1:
269 return 22;
270 case 2:
271 return -1;
272 }
273
274 /* The wording is rather inaccurate here. */
275 __builtin_unreachable (); /* { dg-warning "use of attacked-controlled value in condition for assertion" } */
276 }
277
278 int __attribute__((tainted_args))
279 test_switch_bogus_case_MY_ASSERT_1 (int n)
280 {
281 switch (n)
282 {
283 default:
284 case 0:
285 return 5;
286 case 1:
287 return 22;
288 case 2:
289 return -1;
290 case 42:
291 MY_ASSERT_1 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
292 }
293 }
294
295 int __attribute__((tainted_args))
296 test_switch_bogus_case_MY_ASSERT_2 (int n)
297 {
298 switch (n)
299 {
300 default:
301 case 0:
302 return 5;
303 case 1:
304 return 22;
305 case 2:
306 return -1;
307 case 42:
308 MY_ASSERT_2 (0); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
309 }
310 }
311
312 int __attribute__((tainted_args))
313 test_switch_bogus_case_unreachable (int n)
314 {
315 switch (n)
316 {
317 default:
318 case 0:
319 return 5;
320 case 1:
321 return 22;
322 case 2:
323 return -1;
324 case 42:
325 /* This case gets optimized away before we see it. */
326 __builtin_unreachable ();
327 }
328 }
329
330
331 /* Contents of a struct. */
332
333 struct s
334 {
335 int x;
336 int y;
337 };
338
339 int __attribute__((tainted_args))
340 test_assert_struct (struct s *p)
341 {
342 MY_ASSERT_1 (p->x < p->y); /* { dg-warning "-Wanalyzer-tainted-assertion" } */
343 }