| 1 |
/* PR middle-end/83859 - attribute to establish relation between parameters |
| 2 |
for buffer and its size |
| 3 |
Test to verify that with optimization enabled, -Wstringop-overflow |
| 4 |
warnings are issued for calls to user-defined functions with attribute |
| 5 |
access and with non-constant out-of-bounds arguments. |
| 6 |
{ dg-do compile } |
| 7 |
{ dg-options "-O2 -Wall" } */ |
| 8 |
|
| 9 |
#include "range.h" |
| 10 |
|
| 11 |
#define INT_MAX __INT_MAX__ |
| 12 |
#define INT_MIN (-INT_MAX - 1) |
| 13 |
|
| 14 |
#define RDONLY(...) __attribute__ ((access (read_only, __VA_ARGS__))) |
| 15 |
#define WRONLY(...) __attribute__ ((access (write_only, __VA_ARGS__))) |
| 16 |
#define RDWR(...) __attribute__ ((access (read_write, __VA_ARGS__))) |
| 17 |
|
| 18 |
typedef __INT32_TYPE__ int32_t; |
| 19 |
|
| 20 |
/* Exercise null pointer detection. */ |
| 21 |
|
| 22 |
RDONLY (2, 1) void |
| 23 |
rd2_1 (int, const void*); // { dg-message "in a call to function 'rd2_1' declared with attribute 'access \\\(read_only, 2, 1\\\)" "note" } |
| 24 |
|
| 25 |
void test_rd2_1 (void) |
| 26 |
{ |
| 27 |
{ |
| 28 |
void *null = 0; |
| 29 |
void *p = &null; |
| 30 |
|
| 31 |
rd2_1 (0, null); |
| 32 |
rd2_1 (1, p); |
| 33 |
} |
| 34 |
|
| 35 |
{ |
| 36 |
void *null = 0; |
| 37 |
rd2_1 (1, null); // { dg-warning "argument 2 is null but the corresponding size argument 1 value is 1" } |
| 38 |
} |
| 39 |
|
| 40 |
{ |
| 41 |
void *null = 0; |
| 42 |
/* Ideally the message would say "range" for a range and "value" |
| 43 |
for a singular value but using the same reduces the complexity |
| 44 |
of the code and keeps down the number of messages that need to |
| 45 |
be translated, withot sacrificing (too much) clarity. */ |
| 46 |
rd2_1 (SR (1, 2), null); // { dg-warning "argument 2 is null but the corresponding size argument 1 range|value is \\\[1, 2]" } |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
WRONLY (3, 1) void |
| 51 |
wr3_1 (int, int, void*); // { dg-message "in a call to function 'wr3_1' declared with attribute 'access \\\(write_only, 3, 1\\\)" } |
| 52 |
|
| 53 |
void test_wr3_1 (void) |
| 54 |
{ |
| 55 |
{ |
| 56 |
void *null = 0; |
| 57 |
void *p = &null; |
| 58 |
|
| 59 |
wr3_1 (SR (0, 1), 0, null); |
| 60 |
wr3_1 (SR (1, 1), 0, p); |
| 61 |
} |
| 62 |
|
| 63 |
void *null = 0; |
| 64 |
|
| 65 |
wr3_1 (SR (1, 2), 1, null); // { dg-warning "argument 3 is null but the corresponding size argument 1 range|value is \\\[1, 2]" } |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
WRONLY (2, 1) void |
| 70 |
wr2_1 (int, void*); |
| 71 |
|
| 72 |
void test_wrd2_1 (int n) |
| 73 |
{ |
| 74 |
wr2_1 (0, 0); |
| 75 |
wr2_1 (SR (-1, 1), 0); |
| 76 |
wr2_1 (SR (0, 1), 0); |
| 77 |
wr2_1 (SR (1, 2), 0); // { dg-warning "argument 2 is null but the corresponding size argument 1 range|value is \\\[1, 2]" } |
| 78 |
|
| 79 |
/* This should probably be diagnosed but to avoid false positives |
| 80 |
caused by jump threading and such it would have to be done |
| 81 |
earlier than it is now. */ |
| 82 |
wr2_1 (n, 0); // { dg-warning "argument 2 is null" "unimplemented" { xfail *-*-* } } |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/* Exercise pointer to an incomplete type other than void. */ |
| 87 |
|
| 88 |
struct Incomplete; |
| 89 |
extern struct Incomplete inc; |
| 90 |
|
| 91 |
extern char ax[]; |
| 92 |
|
| 93 |
WRONLY (1, 2) void |
| 94 |
wr1_2_inc (struct Incomplete*, unsigned); |
| 95 |
|
| 96 |
void test_wr1_2_inc (struct Incomplete *pinc, unsigned n) |
| 97 |
{ |
| 98 |
wr1_2_inc (0, 0); |
| 99 |
wr1_2_inc (0, 1); // { dg-warning "argument 1 is null but the corresponding size argument 2 value is 1" } |
| 100 |
|
| 101 |
wr1_2_inc (pinc, 1); |
| 102 |
wr1_2_inc (&inc, 1); |
| 103 |
|
| 104 |
wr1_2_inc (pinc, 123); |
| 105 |
wr1_2_inc (&inc, 456); |
| 106 |
|
| 107 |
char a3[3]; |
| 108 |
pinc = (struct Incomplete*)a3; |
| 109 |
wr1_2_inc (pinc, SR (3, 4)); |
| 110 |
wr1_2_inc (pinc, SR (4, 5)); |
| 111 |
// { dg-warning "'wr1_2_inc' writing between 4 and 5 bytes into a region of size 3" "small buffer cast to incomplete" { target *-*-* } .-1 } |
| 112 |
|
| 113 |
pinc = (struct Incomplete*)ax; |
| 114 |
wr1_2_inc (pinc, SR (123, 456)); |
| 115 |
|
| 116 |
char vla[n]; |
| 117 |
pinc = (struct Incomplete*)vla; |
| 118 |
wr1_2_inc (pinc, SR (345, 456)); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
RDONLY (1, 3) WRONLY (2, 4) void |
| 123 |
rd1_3_wr2_4 (const void*, void*, int, int); |
| 124 |
|
| 125 |
void test_rd1_3_wr2_4 (const void *s, void *d, int n1, int n2) |
| 126 |
{ |
| 127 |
rd1_3_wr2_4 (s, d, 1, 2); |
| 128 |
rd1_3_wr2_4 (s, d, 123, 456); |
| 129 |
rd1_3_wr2_4 (s, d, INT_MAX, INT_MAX); |
| 130 |
rd1_3_wr2_4 (s, d, -1, 2); // { dg-warning "argument 3 value -1 is negative" } |
| 131 |
|
| 132 |
const int ir_min_m1 = SR (INT_MIN, -1); |
| 133 |
rd1_3_wr2_4 (s, d, ir_min_m1, 2); // { dg-warning "argument 3 range|value \\\[-\[0-9\]+, -1] is negative" } |
| 134 |
|
| 135 |
rd1_3_wr2_4 (s, d, SR (-1, 0), 2); |
| 136 |
rd1_3_wr2_4 (s, d, SR (INT_MIN, INT_MAX), 2); |
| 137 |
|
| 138 |
rd1_3_wr2_4 (s, d, n1, n2); |
| 139 |
|
| 140 |
|
| 141 |
const char s11[11] = "0123456789"; |
| 142 |
|
| 143 |
rd1_3_wr2_4 (s11, d, 11, n2); |
| 144 |
rd1_3_wr2_4 (s11, d, 12, n2); // { dg-warning "'rd1_3_wr2_4' reading 12 bytes from a region of size 11" } |
| 145 |
|
| 146 |
rd1_3_wr2_4 (s11, d, SR (0, 11), n2); |
| 147 |
rd1_3_wr2_4 (s11, d, SR (0, 12), n2); |
| 148 |
rd1_3_wr2_4 (s11, d, SR (11, 12), n2); |
| 149 |
rd1_3_wr2_4 (s11, d, SR (11, INT_MAX), n2); |
| 150 |
rd1_3_wr2_4 (s11, d, SR (12, 13), n2); // { dg-warning "'rd1_3_wr2_4' reading between 12 and 13 bytes from a region of size 11" } |
| 151 |
|
| 152 |
char d4[4]; |
| 153 |
rd1_3_wr2_4 (s, d4, n1, 4); |
| 154 |
rd1_3_wr2_4 (s, d4, n1, 5); // { dg-warning "'rd1_3_wr2_4' writing 5 bytes into a region of size 4" } |
| 155 |
|
| 156 |
rd1_3_wr2_4 (s11, d4, SR (12, 13), SR (5, 6)); |
| 157 |
// { dg-warning "'rd1_3_wr2_4' reading between 12 and 13 bytes from a region of size 11" "read" { target *-*-* } .-1 } |
| 158 |
// { dg-warning "'rd1_3_wr2_4' writing between 5 and 6 bytes into a region of size 4" "read" { target *-*-* } .-2 } |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
/* Verify that function pointers are handled. */ |
| 163 |
|
| 164 |
RDONLY (1) void (*pfrd1)(const void*, const void*); |
| 165 |
|
| 166 |
void test_pfrd1 (void) |
| 167 |
{ |
| 168 |
pfrd1 ("" + SR (0, 9), "" + SR (1, 9)); |
| 169 |
pfrd1 ("" + SR (1, 2), ""); // { dg-warning "reading 1 byte from a region of size 0" } |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
WRONLY (4, 3) void (*pfwr4_3)(int, const char*, int, int32_t*); |
| 174 |
|
| 175 |
void test_pfwr4_3 (void) |
| 176 |
{ |
| 177 |
int32_t i; |
| 178 |
pfwr4_3 (3, "", 0, &i + SR (0, 9)); |
| 179 |
pfwr4_3 (5, "", 1, &i + SR (1, 2)); // { dg-warning "writing 4 bytes into a region of size 0" } |
| 180 |
} |