| 1 |
/* PR c/78284 - warn on malloc with very large arguments |
| 2 |
Test exercising the ability of the built-in allocation functions to |
| 3 |
detect and diagnose calls that attemnpt to allocate objects in excess |
| 4 |
of the maximum specified by -Walloc-size-larger-than=maximum. */ |
| 5 |
/* { dg-do compile } */ |
| 6 |
/* { dg-options "-O1 -Wall -Walloc-size-larger-than=12345 -Wno-use-after-free" } */ |
| 7 |
|
| 8 |
#define SIZE_MAX __SIZE_MAX__ |
| 9 |
#define MAXOBJSZ 12345 |
| 10 |
|
| 11 |
typedef __SIZE_TYPE__ size_t; |
| 12 |
|
| 13 |
void sink (void*); |
| 14 |
|
| 15 |
#pragma GCC push_options |
| 16 |
/* Verify that constant evaluation takes place even at -O0. */ |
| 17 |
#pragma GCC optimize ("0") |
| 18 |
|
| 19 |
void test_cst (void *p) |
| 20 |
{ |
| 21 |
enum { max = MAXOBJSZ }; |
| 22 |
|
| 23 |
sink (__builtin_aligned_alloc (1, max)); |
| 24 |
sink (__builtin_aligned_alloc (1, max + 1)); /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */ |
| 25 |
|
| 26 |
sink (__builtin_alloca (max)); |
| 27 |
sink (__builtin_alloca (max + 2)); /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */ |
| 28 |
|
| 29 |
sink (__builtin_calloc (1, max)); |
| 30 |
sink (__builtin_calloc (max, 1)); |
| 31 |
|
| 32 |
sink (__builtin_calloc (max / 2, 3)); /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */ |
| 33 |
sink (__builtin_calloc (4, max / 3)); /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */ |
| 34 |
|
| 35 |
sink (__builtin_malloc (max)); |
| 36 |
sink (__builtin_malloc (max + 3)); /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */ |
| 37 |
|
| 38 |
sink (__builtin_realloc (p, max)); |
| 39 |
sink (__builtin_realloc (p, max + 4)); /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */ |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
/* Variable evaluation needs -O1. */ |
| 44 |
#pragma GCC pop_options |
| 45 |
|
| 46 |
__attribute__ ((noipa)) void test_var (void *p) |
| 47 |
{ |
| 48 |
size_t max = MAXOBJSZ; |
| 49 |
|
| 50 |
sink (__builtin_aligned_alloc (1, max)); |
| 51 |
sink (__builtin_aligned_alloc (1, max + 1)); /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */ |
| 52 |
|
| 53 |
sink (__builtin_alloca (max)); |
| 54 |
sink (__builtin_alloca (max + 2)); /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */ |
| 55 |
|
| 56 |
sink (__builtin_calloc (1, max)); |
| 57 |
sink (__builtin_calloc (max, 1)); |
| 58 |
|
| 59 |
sink (__builtin_calloc (max / 2, 3)); /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */ |
| 60 |
sink (__builtin_calloc (4, max / 3)); /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */ |
| 61 |
|
| 62 |
sink (__builtin_malloc (max)); |
| 63 |
sink (__builtin_malloc (max + 3)); /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */ |
| 64 |
|
| 65 |
sink (__builtin_realloc (p, max)); |
| 66 |
sink (__builtin_realloc (p, max + 4)); /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */ |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/* Value range evaluation (apparently) needs -O2 here. */ |
| 71 |
#pragma GCC optimize ("2") |
| 72 |
|
| 73 |
static size_t maxobjsize (void) |
| 74 |
{ |
| 75 |
return MAXOBJSZ; |
| 76 |
} |
| 77 |
|
| 78 |
__attribute__ ((noipa)) void test_range (void *p, size_t range) |
| 79 |
{ |
| 80 |
/* Make sure the variable is at least as large as the maximum object |
| 81 |
size but also make sure that it's guaranteed not to be too big to |
| 82 |
increment (and wrap around). */ |
| 83 |
size_t max = maxobjsize (); |
| 84 |
|
| 85 |
if (range < max || 2 * max <= range) |
| 86 |
range = maxobjsize (); |
| 87 |
|
| 88 |
sink (__builtin_aligned_alloc (1, range)); |
| 89 |
sink (__builtin_aligned_alloc (1, range + 1)); /* { dg-warning "argument 2 range \\\[12346\[lu\]*, \[0-9\]+\[lu\]*\\\] exceeds maximum object size 12345" } */ |
| 90 |
|
| 91 |
sink (__builtin_alloca (range)); |
| 92 |
sink (__builtin_alloca (range + 2)); /* { dg-warning "argument 1 range \\\[12347\[lu\]*, \[0-9\]+\[lu\]*\\\] exceeds maximum object size 12345" } */ |
| 93 |
|
| 94 |
sink (__builtin_calloc (range, 1)); |
| 95 |
sink (__builtin_calloc (1, range)); |
| 96 |
|
| 97 |
sink (__builtin_calloc (range / 2, 3)); /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */ |
| 98 |
sink (__builtin_calloc (4, range / 3)); /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */ |
| 99 |
|
| 100 |
sink (__builtin_malloc (range)); |
| 101 |
sink (__builtin_malloc (range + 3)); /* { dg-warning "argument 1 range \\\[12348\[lu\]*, 24692\[lu\]*\\\] exceeds maximum object size 12345" } */ |
| 102 |
|
| 103 |
sink (__builtin_realloc (p, range)); |
| 104 |
sink (__builtin_realloc (p, range + 4)); /* { dg-warning "argument 2 range \\\[12349\[lu\]*, 24693\[lu\]*\\\] exceeds maximum object size 12345" } */ |
| 105 |
} |