1 /* PR c/77531 - __attribute__((alloc_size(1,2))) could also warn on
2 multiplication overflow
3 PR c/78284 - warn on malloc with very large arguments
4 Test exercising the ability to detect and diagnose calls to allocation
5 functions decorated with attribute alloc_size that either overflow or
6 exceed the default maximum object size (with -Walloc-size-larger-than
7 not explicitly specified). */
8 /* { dg-do compile { target size32plus } } */
9 /* { dg-options "-O2 -Wall" } */
11 #define SCHAR_MAX __SCHAR_MAX__
12 #define SCHAR_MIN (-SCHAR_MAX - 1)
13 #define UCHAR_MAX (SCHAR_MAX * 2 + 1)
15 #define SHRT_MAX __SHRT_MAX__
16 #define SHRT_MIN (-SHRT_MAX - 1)
17 #define USHRT_MAX (SHRT_MAX * 2 + 1)
19 #define INT_MAX __INT_MAX__
20 #define INT_MIN (-INT_MAX - 1)
21 #define UINT_MAX (INT_MAX * 2U + 1)
23 #define LONG_MAX __LONG_MAX__
24 #define LONG_MIN (-LONG_MAX - 1L)
25 #define ULONG_MAX (LONG_MAX * 2LU + 1)
27 #define LLONG_MAX __LLONG_MAX__
28 #define LLONG_MIN (-LLONG_MAX - 1LL)
29 #define ULLONG_MAX (ULLONG_MAX * 2LLU + 1)
31 #define PTRDIFF_MAX __PTRDIFF_MAX__
32 #define PTRDIFF_MIN (-PTRDIFF_MAX - 1)
33 #define SIZE_MAX __SIZE_MAX__
35 typedef __PTRDIFF_TYPE__
ptrdiff_t;
36 typedef __SIZE_TYPE__
size_t;
38 #define ALLOC_SIZE(...) __attribute__ ((alloc_size (__VA_ARGS__)))
40 void* f_uchar_1 (unsigned char) ALLOC_SIZE (1);
41 void* f_uchar_2 (unsigned char, unsigned char) ALLOC_SIZE (1, 2);
42 void* f_schar_1 (signed char) ALLOC_SIZE (1);
43 void* f_schar_2 (signed char, signed char) ALLOC_SIZE (1, 2);
45 void* f_ushrt_1 (unsigned short) ALLOC_SIZE (1);
46 void* f_ushrt_2 (unsigned short, unsigned short) ALLOC_SIZE (1, 2);
47 void* f_shrt_1 (signed short) ALLOC_SIZE (1);
48 void* f_shrt_2 (signed short, signed short) ALLOC_SIZE (1, 2);
50 void* f_uint_1 (unsigned) ALLOC_SIZE (1);
51 void* f_uint_2 (unsigned, unsigned) ALLOC_SIZE (1, 2);
52 void* f_int_1 (int) ALLOC_SIZE (1);
53 void* f_int_2 (int, int) ALLOC_SIZE (1, 2);
55 void* f_ulong_1 (unsigned long) ALLOC_SIZE (1);
56 void* f_ulong_2 (unsigned long, unsigned long) ALLOC_SIZE (1, 2);
57 void* f_long_1 (long) ALLOC_SIZE (1);
58 void* f_long_2 (long, long) ALLOC_SIZE (1, 2);
60 void* f_ullong_1 (unsigned long long) ALLOC_SIZE (1);
61 void* f_ullong_2 (unsigned long long, unsigned long long) ALLOC_SIZE (1, 2);
62 void* f_llong_1 (long long) ALLOC_SIZE (1);
63 void* f_llong_2 (long long, long long) ALLOC_SIZE (1, 2);
65 void* f_size_1 (size_t) ALLOC_SIZE (1);
66 void* f_size_2 (size_t, size_t) ALLOC_SIZE (1, 2);
69 unsigned_range (size_t min
, size_t max
)
71 extern size_t random_unsigned_value (void);
72 size_t val
= random_unsigned_value ();
73 if (val
< min
|| max
< val
) val
= min
;
78 signed_range (long long min
, long long max
)
80 extern long long random_signed_value (void);
81 long long val
= random_signed_value ();
82 if (val
< min
|| max
< val
) val
= min
;
87 unsigned_anti_range (size_t min
, size_t max
)
89 extern size_t random_unsigned_value (void);
90 size_t val
= random_unsigned_value ();
91 if (min
<= val
&& val
<= max
)
97 signed_anti_range (long long min
, long long max
)
99 extern long long random_signed_value (void);
100 long long val
= random_signed_value ();
101 if (min
<= val
&& val
<= max
)
106 #define UR(min, max) unsigned_range (min, max)
107 #define SR(min, max) signed_range (min, max)
109 #define UAR(min, max) unsigned_anti_range (min, max)
110 #define SAR(min, max) signed_anti_range (min, max)
116 test_uchar_cst (void)
118 const unsigned char max
= UCHAR_MAX
;
120 sink (f_uchar_1 (0));
121 sink (f_uchar_1 (1));
122 sink (f_uchar_1 (max
));
124 sink (f_uchar_2 (0, 0));
125 sink (f_uchar_2 (0, 1));
126 sink (f_uchar_2 (1, 0));
127 sink (f_uchar_2 (1, 1));
128 sink (f_uchar_2 (0, max
));
129 sink (f_uchar_2 (max
, 0));
130 sink (f_uchar_2 (max
, max
));
134 test_uchar_range (unsigned char n
, int i
)
136 const unsigned char max
= UCHAR_MAX
;
138 sink (f_uchar_1 (n
));
140 sink (f_uchar_1 (UR (0, 1)));
141 sink (f_uchar_1 (UR (1, max
)));
142 sink (f_uchar_1 (UR (0, max
- 1)));
144 sink (f_uchar_1 (UAR (1, 1)));
145 sink (f_uchar_1 (UAR (1, max
- 1)));
146 sink (f_uchar_1 (UAR (max
- 2, max
- 1)));
148 sink (f_uchar_2 (0, n
));
149 sink (f_uchar_2 (0, i
));
150 sink (f_uchar_2 (n
, 0));
151 sink (f_uchar_2 (i
, 0));
152 sink (f_uchar_2 (1, n
));
153 sink (f_uchar_2 (1, i
));
154 sink (f_uchar_2 (n
, 1));
155 sink (f_uchar_2 (i
, 1));
156 sink (f_uchar_2 (max
, n
));
157 sink (f_uchar_2 (max
, i
));
158 sink (f_uchar_2 (n
, max
));
159 sink (f_uchar_2 (i
, max
));
160 sink (f_uchar_2 (n
, n
));
161 sink (f_uchar_2 (i
, i
));
163 sink (f_uchar_2 (UR (0, 1), UR (0, 1)));
164 sink (f_uchar_2 (UR (1, 2), UR (1, 2)));
165 sink (f_uchar_2 (UR (1, max
), UR (0, 1)));
166 sink (f_uchar_2 (UR (0, 1), UR (1, max
)));
170 test_schar_cst (void)
172 const signed char min
= SCHAR_MIN
;
173 const signed char max
= SCHAR_MAX
;
175 sink (f_schar_1 (min
)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
176 sink (f_schar_1 (-1)); /* { dg-warning "argument 1 value .-1. is negative" } */
177 sink (f_schar_1 (0));
178 sink (f_schar_1 (1));
179 sink (f_schar_1 (max
));
181 sink (f_schar_2 (0, min
)); /* { dg-warning "argument 2 value .-\[0-9\]+. is negative" } */
182 sink (f_schar_2 (min
, 0)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
183 sink (f_schar_2 (0, -1)); /* { dg-warning "argument 2 value .-1. is negative" } */
184 sink (f_schar_2 (-1, 0)); /* { dg-warning "argument 1 value .-1. is negative" } */
189 test_schar_range (signed char n
)
191 const signed char min
= SCHAR_MIN
;
192 const signed char max
= SCHAR_MAX
;
194 sink (f_schar_1 (n
));
196 sink (f_schar_1 (SR (min
, min
+ 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
197 sink (f_schar_1 (SR (min
, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
198 sink (f_schar_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1, 0\\\] is negative" } */
199 sink (f_schar_1 (SR (-1, 1)));
200 sink (f_schar_1 (SR (0, 1)));
201 sink (f_schar_1 (SR (0, max
- 1)));
202 sink (f_schar_1 (SR (1, max
)));
203 sink (f_schar_1 (SR (max
- 1, max
)));
205 sink (f_schar_2 (n
, n
));
207 sink (f_schar_2 (SR (min
, min
+ 1), n
)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
208 sink (f_schar_2 (n
, SR (min
, min
+ 1))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
209 sink (f_schar_2 (SR (min
, min
+ 1), 0)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
210 sink (f_schar_2 (0, SR (min
, min
+ 1))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
211 sink (f_schar_2 (SR (min
, min
+ 1), min
)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
212 /* { dg-warning "argument 2 value .-\[0-9\]+. is negative" "argument 2" { target *-*-* } .-1 } */
213 sink (f_schar_2 (min
, SR (min
, min
+ 1))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
214 /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" "argument 1" { target *-*-* } .-1 } */
216 sink (f_schar_2 (SR (-1, 0), 0)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
217 sink (f_schar_2 (0, SR (-1, 0))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, 0\\\] is negative" } */
218 sink (f_schar_2 (SR (-1, 0), 1)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
219 sink (f_schar_2 (1, SR (-1, 0))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, 0\\\] is negative" } */
220 sink (f_schar_2 (SR (-1, 0), n
)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
221 sink (f_schar_2 (n
, SR (-1, 0))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, 0\\\] is negative" } */
223 sink (f_schar_2 (max
, SR (1, max
)));
224 sink (f_schar_2 (SR (1, max
), max
));
228 test_ushrt_cst (void)
230 const unsigned short max
= USHRT_MAX
;
232 sink (f_ushrt_1 (0));
233 sink (f_ushrt_1 (1));
234 sink (f_ushrt_1 (max
));
236 sink (f_ushrt_2 (0, 0));
237 sink (f_ushrt_2 (0, 1));
238 sink (f_ushrt_2 (1, 0));
239 sink (f_ushrt_2 (1, 1));
240 sink (f_ushrt_2 (0, max
));
241 sink (f_ushrt_2 (max
, 0));
243 if (max
< SIZE_MAX
&& (size_t)max
* max
< SIZE_MAX
/ 2)
244 sink (f_ushrt_2 (max
, max
));
248 test_ushrt_range (unsigned short n
)
250 const unsigned short max
= USHRT_MAX
;
252 sink (f_ushrt_1 (n
));
253 sink (f_ushrt_1 (UR (0, 1)));
254 sink (f_ushrt_1 (UR (1, max
- 1)));
255 sink (f_ushrt_1 (UR (1, max
)));
256 sink (f_ushrt_1 (UR (0, max
- 1)));
262 const short min
= SHRT_MIN
;
263 const short max
= SHRT_MAX
;
265 sink (f_shrt_1 (min
)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
266 sink (f_shrt_1 (-1)); /* { dg-warning "argument 1 value .-1. is negative" } */
269 sink (f_shrt_1 (max
));
273 test_shrt_range (short n
)
275 const short min
= SHRT_MIN
;
276 const short max
= SHRT_MAX
;
280 sink (f_shrt_1 (SR (min
, min
+ 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
281 sink (f_shrt_1 (SR (min
, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
282 sink (f_shrt_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1, 0\\\] is negative" } */
283 sink (f_shrt_1 (SR (-1, 1)));
284 sink (f_shrt_1 (SR (0, 1)));
285 sink (f_shrt_1 (SR (0, max
- 1)));
286 sink (f_shrt_1 (SR (1, max
)));
287 sink (f_shrt_1 (SR (max
- 1, max
)));
293 const unsigned max
= UINT_MAX
;
300 sink (f_uint_1 (max
- 1));
301 sink (f_uint_1 (max
));
306 test_uint_range (unsigned n
)
308 const unsigned max
= UINT_MAX
;
311 sink (f_uint_1 (UR (0, 1)));
312 sink (f_uint_1 (UR (0, max
- 1)));
313 sink (f_uint_1 (UR (1, max
- 1)));
314 sink (f_uint_1 (UR (1, max
)));
320 const int min
= INT_MIN
;
321 const int max
= INT_MAX
;
323 sink (f_int_1 (min
)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
324 sink (f_int_1 (-1)); /* { dg-warning "argument 1 value .-1. is negative" } */
327 sink (f_int_1 (max
));
331 test_int_range (int n
)
333 const int min
= INT_MIN
;
334 const int max
= INT_MAX
;
338 sink (f_int_1 (SR (min
, min
+ 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
339 sink (f_int_1 (SR (min
, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
340 sink (f_int_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1, 0\\\] is negative" } */
341 sink (f_int_1 (SR (-1, 1)));
342 sink (f_int_1 (SR (0, 1)));
343 sink (f_int_1 (SR (0, max
- 1)));
344 sink (f_int_1 (SR (1, max
)));
345 sink (f_int_1 (SR (max
- 1, max
)));
349 test_ulong_cst (void)
351 const unsigned long max
= ULONG_MAX
;
353 sink (f_ulong_1 (0));
354 sink (f_ulong_1 (1));
355 #if ULONG_MAX < SIZE_MAX
356 sink (f_ulong_1 (max
- 1));
357 sink (f_ulong_1 (max
));
364 test_ulong_range (unsigned long n
)
366 const unsigned long max
= ULONG_MAX
;
368 sink (f_ulong_1 (n
));
369 sink (f_ulong_1 (UR (0, 1)));
370 sink (f_ulong_1 (UR (0, max
- 1)));
371 sink (f_ulong_1 (UR (1, max
- 1)));
372 sink (f_ulong_1 (UR (1, max
)));
378 const long min
= LONG_MIN
;
379 const long max
= LONG_MAX
;
381 sink (f_long_1 (min
)); /* { dg-warning "argument 1 value .-\[0-9\]+l*. is negative" } */
382 sink (f_long_1 (-1)); /* { dg-warning "argument 1 value .-1l*. is negative" } */
385 sink (f_long_1 (max
));
389 test_long_range (long n
)
391 const long min
= LONG_MIN
;
392 const long max
= LONG_MAX
;
396 sink (f_long_1 (SR (min
, min
+ 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+l*, -\[0-9\]+l*\\\] is negative" } */
397 sink (f_long_1 (SR (min
, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+l*, 0l*\\\] is negative" } */
398 sink (f_long_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1l*, 0l*\\\] is negative" } */
399 sink (f_long_1 (SR (-1, 1)));
400 sink (f_long_1 (SR (0, 1)));
401 sink (f_long_1 (SR (0, max
- 1)));
402 sink (f_long_1 (SR (1, max
)));
403 sink (f_long_1 (SR (max
- 1, max
)));
409 const size_t max
= __SIZE_MAX__
;
413 sink (f_size_1 (max
- 1)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
414 sink (f_size_1 (max
)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
416 sink (f_size_2 (0, max
- 1)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
417 sink (f_size_2 (max
- 1, 0)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
418 sink (f_size_2 (1, max
- 1)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
419 sink (f_size_2 (max
- 1, 1)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
420 sink (f_size_2 (max
- 1, max
- 1)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
421 /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" "argument 2" { target *-*-* } .-1 } */
423 sink (f_size_2 (0, max
)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
424 sink (f_size_2 (max
, 0)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
426 sink (f_size_2 (max
/ 2, 2)); /* { dg-warning "product .\[0-9\]+ \\* \[0-9\]+. of arguments 1 and 2 exceeds maximum object size \[0-9\]+" } */
427 sink (f_size_2 (max
/ 2, 3)); /* { dg-warning "product .\[0-9\]+ \\* \[0-9\]+. of arguments 1 and 2 exceeds .SIZE_MAX." } */
431 test_size_range (size_t ui
, ptrdiff_t si
)
433 const ptrdiff_t smin
= PTRDIFF_MIN
;
434 const ptrdiff_t smax
= PTRDIFF_MAX
;
435 const size_t umax
= SIZE_MAX
;
437 sink (f_size_1 (ui
));
438 sink (f_size_1 (si
));
440 sink (f_size_1 (UR (0, 1)));
441 sink (f_size_1 (UR (0, umax
- 1)));
442 sink (f_size_1 (UR (1, umax
- 1)));
443 sink (f_size_1 (UR (1, umax
)));
445 sink (f_size_1 (UAR (1, 1)));
446 /* Since the only valid argument in the anti-range below is zero
447 a warning is expected even though -Walloc-zero is not specified. */
448 sink (f_size_1 (UAR (1, umax
/ 2))); /* { dg-warning "argument 1 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
449 /* The only valid argument in this range is 1. */
450 sink (f_size_1 (UAR (2, umax
/ 2)));
452 sink (f_size_2 (ui
, ui
));
453 sink (f_size_2 (si
, si
));
454 sink (f_size_2 (ui
, umax
/ 2));
455 sink (f_size_2 (si
, umax
/ 2));
456 sink (f_size_2 (umax
/ 2, ui
));
457 sink (f_size_2 (umax
/ 2, si
));
459 sink (f_size_2 (UR (0, 1), umax
)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size " } */
460 sink (f_size_2 (UR (0, 1), umax
/ 2));
461 sink (f_size_2 (UR (0, umax
/ 2), umax
/ 2));
463 sink (f_size_2 (UR (umax
/ 2 + 1, umax
/ 2 + 2), ui
)); /* { dg-warning "argument 1 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
464 sink (f_size_2 (ui
, UR (umax
/ 2 + 1, umax
/ 2 + 2))); /* { dg-warning "argument 2 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
465 sink (f_size_2 (UR (umax
/ 2 + 1, umax
), UR (umax
/ 2 + 1, umax
))); /* { dg-warning "argument 1 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
466 /* { dg-warning "argument 2 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " "argument 2" { target *-*-* } .-1 } */
468 sink (f_size_2 (SR (smin
, 1), 1));
469 sink (f_size_2 (SR (smin
, 1), umax
/ 2));
470 sink (f_size_2 (SR (-1, smax
), 1));
471 sink (f_size_2 (SR (-1, smax
), umax
/ 2));
472 sink (f_size_2 (SR (-1, 1), 1));
473 sink (f_size_2 (SR (-1, 1), umax
/ 2));
474 sink (f_size_2 (SR (-9, 9), 1));
475 sink (f_size_2 (SR (-9, 9), umax
/ 2));