PR target/83368
[official-gcc.git] / gcc / testsuite / c-c++-common / Warray-bounds-3.c
blobeb0f05045d59083b32c4601ba784a9a4a647f38e
1 /* Exercise that -Warray-bounds is issued for out-of-bounds offsets
2 in calls to built-in functions.
3 { dg-do compile }
4 { dg-options "-O2 -Wno-stringop-overflow -Warray-bounds -ftrack-macro-expansion=0" } */
6 #include "../gcc.dg/range.h"
8 #if __cplusplus
9 # define restrict __restrict
10 extern "C" {
11 #endif
13 extern void* memcpy (void* restrict, const void* restrict, size_t);
14 extern void* mempcpy (void* restrict, const void* restrict, size_t);
15 extern void* memmove (void*, const void*, size_t);
17 extern char* stpcpy (char* restrict, const char* restrict);
19 extern char* strcat (char* restrict, const char* restrict);
20 extern char* strcpy (char* restrict, const char* restrict);
21 extern char* strncpy (char* restrict, const char* restrict, size_t);
23 #if __cplusplus
24 } /* extern "C" */
25 #endif
27 void sink (void*, ...);
29 #define CAT(x, y) x ## y
30 #define CONCAT(x, y) CAT (x, y)
31 #define UNIQUE_NAME(x) CONCAT(x, __LINE__)
33 #define T(type, N, dst, src, n) do { \
34 extern type UNIQUE_NAME (a)[N]; \
35 type *a = UNIQUE_NAME (a); \
36 type *pd = (dst); \
37 const type *ps = (src); \
38 FUNC (pd, ps, n); \
39 sink (a, pd, ps); \
40 } while (0)
43 void test_memcpy_bounds (char *d, const char *s, size_t n)
45 #define FUNC memcpy
47 /* Verify that invalid offsets into an array of known size are
48 detected. */
50 T (char, 1, a + SR (DIFF_MIN, -1), s, n); /* { dg-warning "offset \\\[-\[0-9\]+, -1] is out of the bounds \\\[0, 1] of object \[^\n\r]* with type .char ?\\\[1]" } */
51 T (char, 1, a + SR (-2, -1), s, n); /* { dg-warning "offset \\\[-2, -1] is out of the bounds \\\[0, 1] of object" } */
52 T (char, 1, a + SR (-2, 0), s, n);
54 T (char, 1, a + UR (0, 1), s, n);
55 T (char, 1, a + UR (0, 2), s, n);
56 T (char, 1, a + UR (1, 2), s, n);
57 T (char, 1, a + UR (2, 3), s, n); /* { dg-warning "offset \\\[2, 3] is out of the bounds \\\[0, 1] of object " } */
58 T (char, 1, a + UR (2, DIFF_MAX), s, n); /* { dg-warning "offset \\\[2, \[0-9\]+] is out of the bounds \\\[0, 1] of object " "memcpy" } */
60 /* Offsets in excess of DIFF_MAX are treated as negative even if
61 they appear as large positive in the source. It would be nice
62 if they retained their type but unfortunately that's not how
63 it works so be prepared for both in case it even gets fixed. */
64 T (char, 1, a + UR (3, SIZE_MAX - 1), s, n); /* { dg-warning "offset \\\[3, -2] is out of the bounds \\\[0, 1] of object" "memcpy" } */
66 /* Verify that invalid offsets into an array of unknown size are
67 detected. */
68 extern char arr[];
69 T (char, 1, arr + SR (DIFF_MIN, 0), s, n);
70 T (char, 1, arr + SR (DIFF_MIN + 1, -1), s, n); /* { dg-warning "offset \\\[-\[0-9\]+, -1] is out of the bounds of object " "memcpy" } */
71 T (char, 1, arr + SR (DIFF_MIN, 1), s, n);
72 T (char, 1, arr + SR (DIFF_MIN, DIFF_MAX), s, n);
73 T (char, 1, arr + SR ( -2, -1), s, n); /* { dg-warning "offset \\\[-2, -1] is out of the bounds of object " "memcpy" } */
74 T (char, 1, arr + SR ( -1, 0), s, n);
75 T (char, 1, arr + SR ( -1, 1), s, n);
76 T (char, 1, arr + SR ( -1, DIFF_MAX - 1), s, n);
77 T (char, 1, arr + SR ( 0, 1), s, n);
78 T (char, 1, arr + SR ( 0, DIFF_MAX - 1), s, n);
79 T (char, 1, arr + SR ( 1, 2), s, n);
80 T (char, 1, arr + SR ( 1, DIFF_MAX - 1), s, n);
82 /* Verify that all offsets via a pointer to an uknown object are
83 accepted. */
85 /* Negative indices between [DIFF_MIN, DIFF_MAX] are valid since
86 the pointer to which the offset is applied can be at a positive
87 offset from the beginning of an object. */
88 T (char, 1, d + SR (DIFF_MIN, 0), s, n);
89 T (char, 1, d + SR (DIFF_MIN, -1), s, n);
90 T (char, 1, d + SR (DIFF_MIN, 1), s, n);
91 T (char, 1, d + SR (DIFF_MIN, DIFF_MAX - 1), s, n);
92 T (char, 1, d + SR ( -2, -1), s, n);
93 T (char, 1, d + SR ( -1, 0), s, n);
94 T (char, 1, d + SR ( -1, 1), s, n);
95 T (char, 1, d + SR ( -1, DIFF_MAX - 1), s, n);
96 T (char, 1, d + SR ( 0, 1), s, n);
97 T (char, 1, d + SR ( 0, DIFF_MAX - 1), s, n);
98 T (char, 1, d + SR ( 1, 2), s, n);
99 T (char, 1, d + SR ( 1, DIFF_MAX - 1), s, n);
102 /* Verify offsets in an anti-range. */
104 void test_memcpy_bounds_anti_range (char *d, const char *s, size_t n)
106 T (char, 9, a, a + SAR (-2, -1), 3);
107 T (char, 9, a, a + SAR (-1, 1), 3);
108 T (char, 9, a, a + SAR ( 0, 1), 3);
109 T (char, 9, a, a + SAR ( 0, 2), 3);
110 T (char, 9, a, a + SAR ( 0, 3), 3);
111 T (char, 9, a, a + SAR ( 0, 4), 3);
112 T (char, 9, a, a + SAR ( 0, 5), 3);
113 /* The initial source range is valid but the final range after the access
114 has complete cannot be. The value mentioned in the warning is the final
115 offset, i.e., 7 + 3. Including the whole final range because would be
116 confusing (the upper bound would either be negative or a very large
117 positive number) so only the lower bound is included. */
118 T (char, 9, a, a + SAR ( 0, 6), 3); /* { dg-warning "forming offset 10 is out of the bounds \\\[0, 9] of object " "memcpy" } */
120 /* This fails because the offset isn't represented as an SSA_NAME
121 but rather as a GIMPLE_PHI (offset, 0). With some effort it is
122 possible to extract the range from the PHI but it's not implemented
123 (yet). */
124 T (char, 9, a, a + SAR ( 1, 6), 3); /* { dg-warning "forming offset \\\[9, 0] is out of the bounds \\\[0, 9] of object " "memcpy" { xfail *-*-* } } */
126 /* The range of offsets is the union of [0, 1] and [7, PTRDIFF_MAX]
127 of which the first subrange is valid and thus no warming for memcpy
128 is issued. Similarly for the next test. */
129 T (char, 9, a, a + SAR ( 2, 6), 3);
130 T (char, 9, a, a + SAR ( 3, 6), 3);
132 T (char, 9, a, a + SAR (-1, 7), 3); /* { dg-warning "forming offset \\\[10, 11] is out of the bounds \\\[0, 9] of object " "memcpy" } */
133 T (char, 9, a, a + SAR (-2, 8), 3); /* { dg-warning "forming offset \\\[10, 12] is out of the bounds \\\[0, 9] of object " "memcpy" } */
134 T (char, 9, a, a + SAR (-3, 7), 5); /* { dg-warning "forming offset \\\[10, 13] is out of the bounds \\\[0, 9] of object " "memcpy" } */
136 T (char, 9, a + SAR (-2, -1), a, 3);
137 T (char, 9, a + SAR (-1, 1), a, 3);
138 T (char, 9, a + SAR ( 0, 1), a, 3);
139 T (char, 9, a + SAR ( 0, 2), a, 3);
140 T (char, 9, a + SAR ( 0, 3), a, 3);
141 T (char, 9, a + SAR ( 0, 6), a, 3); /* { dg-warning "forming offset 10 is out of the bounds \\\[0, 9] of object " "memcpy" } */
142 T (char, 9, a + SAR (-1, 7), a, 3); /* { dg-warning "forming offset \\\[10, 11] is out of the bounds \\\[0, 9] of object " "memcpy" } */
143 T (char, 9, a + SAR (-2, 8), a, 3); /* { dg-warning "forming offset \\\[10, 12] is out of the bounds \\\[0, 9] of object " "memcpy" } */
145 ptrdiff_t i = SAR (DIFF_MIN + 1, DIFF_MAX - 4);
146 T (char, 1, d, d + SAR (DIFF_MIN + 3, DIFF_MAX - 1), 3);
147 T (char, 1, d, d + SAR (DIFF_MIN + 3, DIFF_MAX - 3), 5);
150 /* Verify that pointer overflow in the computation done by memcpy
151 (i.e., offset + size) is detected and diagnosed. */
153 void test_memcpy_overflow (char *d, const char *s, size_t n)
155 extern char arr[];
157 /* Verify that offset overflow involving an array of unknown size
158 but known access size is detected. This works except with small
159 sizes that are powers of 2 due to bug . */
160 T (char, 1, arr + SR (DIFF_MAX - 1, DIFF_MAX), s, 1);
161 T (char, 1, arr + SR (DIFF_MAX - 1, DIFF_MAX), s, 2); /* { dg-warning "pointer overflow between offset \\\[\[0-9\]+, \[0-9\]+] and size 2 accessing array " "bug " { xfail *-*-* } } */
162 T (char, 1, arr + SR (DIFF_MAX - 2, DIFF_MAX), s, 3); /* { dg-warning "pointer overflow between offset \\\[\[0-9\]+, \[0-9\]+] and size 3 accessing array " "memcpy" } */
163 T (char, 1, arr + SR (DIFF_MAX - 4, DIFF_MAX), s, 5); /* { dg-warning "pointer overflow between offset \\\[\[0-9\]+, \[0-9\]+] and size 5 accessing array " "memcpy" } */
166 void test_memcpy_bounds_memarray_range (void)
168 #undef TM
169 #define TM(mem, dst, src, n) \
170 do { \
171 struct MA { char a5[5]; int i; } ma; \
172 sink (&ma); /* Initialize arrays. */ \
173 memcpy (dst, src, n); \
174 sink (&ma); \
175 } while (0)
177 ptrdiff_t i = SR (1, 2);
179 TM (ma.a5, ma.a5 + i, ma.a5, 1);
180 TM (ma.a5, ma.a5 + i, ma.a5, 3);
181 TM (ma.a5, ma.a5 + i, ma.a5, 5);
182 TM (ma.a5, ma.a5 + i, ma.a5, 7); /* diagnosed with -Warray-bounds=2 */
185 void test_memmove_bounds (char *d, const char *s, size_t n)
187 #undef FUNC
188 #define FUNC memmove
190 T (char, 1, a + SR (DIFF_MIN + 1, -1), s, n); /* { dg-warning "offset \\\[-\[0-9\]+, -1] is out of the bounds \\\[0, 1] of object \[^\n\r]+ with type .char ?\\\[1]" } */
191 T (char, 1, a + SR (-2, -1), s, n); /* { dg-warning "offset \\\[-2, -1] is out of the bounds \\\[0, 1] of object" } */
192 T (char, 1, a + SR (-2, 0), s, n);
194 const int *pi = (const int*)s;
195 T (int, 2, a + SR (-1, 1), pi, n);
196 T (int, 2, a + SR (-1, 2), pi, n);
197 T (int, 2, a + SR ( 0, 2), pi, n);
198 T (int, 2, a + SR ( 0, 3), pi, n);
199 T (int, 2, a + SR ( 1, 3), pi, n);
200 T (int, 2, a + SR ( 2, 3), pi, n);
202 T (int32_t, 2, a + SR ( 3, 4), pi, n); /* { dg-warning "offset \\\[12, 16] is out of the bounds \\\[0, 8] of object .\[^\n\r]+. with type .int32_t ?\\\[2]." } */
206 void test_mempcpy_bounds (char *d, const char *s, size_t n)
208 #undef FUNC
209 #define FUNC mempcpy
211 /* Verify that invalid offsets into an array of known size are
212 detected. */
214 T (char, 1, a + SR (DIFF_MIN, -1), s, n); /* { dg-warning "offset \\\[-\[0-9\]+, -1] is out of the bounds" "mempcpy" } */
215 T (char, 1, a + SR (-2, -1), s, n); /* { dg-warning "offset \\\[-2, -1] is out of the bounds" "mempcpy" } */
216 T (char, 1, a + SR (-2, 0), s, n);
218 T (char, 1, a + UR (0, 1), s, n);
219 T (char, 1, a + UR (0, 2), s, n);
220 T (char, 1, a + UR (1, 2), s, n);
221 T (char, 1, a + UR (2, 3), s, n); /* { dg-warning "offset \\\[2, 3] is out of the bounds \\\[0, 1] of object " "mempcpy" } */
222 T (char, 1, a + UR (2, DIFF_MAX), s, n); /* { dg-warning "offset \\\[2, \[0-9\]+] is out of the bounds \\\[0, 1] of object" "mempcpy" } */
224 /* Offsets in excess of DIFF_MAX are treated as negative even if
225 they appear as large positive in the source. It would be nice
226 if they retained their type but unfortunately that's not how
227 it works so be prepared for both in case it ever gets fixed. */
228 T (char, 1, a + UR (3, SIZE_MAX), s, n); /* { dg-warning "offset \\\[3, -1] is out of the bounds \\\[0, 1] of object " "mempcpy" } */
230 /* Verify that invalid offsets into an array of unknown size are
231 detected. */
232 extern char arr[];
233 T (char, 1, arr + SR (DIFF_MIN, 0), s, n);
234 T (char, 1, arr + SR (DIFF_MIN, -1), s, n); /* { dg-warning "offset \\\[-\[0-9\]+, -1] is out of the bounds of object" "mempcpy" } */
235 T (char, 1, arr + SR (DIFF_MIN, 1), s, n);
236 T (char, 1, arr + SR (DIFF_MIN, DIFF_MAX), s, n);
237 T (char, 1, arr + SR ( -2, -1), s, n); /* { dg-warning "offset \\\[-2, -1] is out of the bounds of object" "mempcpy" } */
238 T (char, 1, arr + SR ( -1, 0), s, n);
239 T (char, 1, arr + SR ( -1, 1), s, n);
240 T (char, 1, arr + SR ( -1, DIFF_MAX), s, n);
241 T (char, 1, arr + SR ( 0, 1), s, n);
242 T (char, 1, arr + SR ( 0, DIFF_MAX), s, n);
243 T (char, 1, arr + SR ( 1, 2), s, n);
244 T (char, 1, arr + SR ( 1, DIFF_MAX), s, n);
246 /* Verify that all offsets via a pointer to an uknown object are
247 accepted. */
249 /* Negative indices between [DIFF_MIN, DIFF_MAX] are valid since
250 the pointer to which the offset is applied can be at a positive
251 offset from the beginning of an object. */
252 T (char, 1, d + SR (DIFF_MIN, 0), s, n);
253 T (char, 1, d + SR (DIFF_MIN, -1), s, n);
254 T (char, 1, d + SR (DIFF_MIN, 1), s, n);
255 T (char, 1, d + SR (DIFF_MIN, DIFF_MAX), s, n);
256 T (char, 1, d + SR ( -2, -1), s, n);
257 T (char, 1, d + SR ( -1, 0), s, n);
258 T (char, 1, d + SR ( -1, 1), s, n);
259 T (char, 1, d + SR ( -1, DIFF_MAX), s, n);
260 T (char, 1, d + SR ( 0, 1), s, n);
261 T (char, 1, d + SR ( 0, DIFF_MAX), s, n);
262 T (char, 1, d + SR ( 1, 2), s, n);
263 T (char, 1, d + SR ( 1, DIFF_MAX), s, n);
266 #define TI(type, N, init, dst, src) do { \
267 type UNIQUE_NAME (a)[N] = init; \
268 type *a = UNIQUE_NAME (a); \
269 type *pd = (dst); \
270 const type *ps = (src); \
271 FUNC (pd, ps); \
272 sink (a, pd, ps, s); \
273 } while (0)
275 void test_strcpy_bounds (char *d, const char *s)
277 #undef FUNC
278 #define FUNC strcpy
280 ptrdiff_t i;
282 TI (char, 1, "", a, a + SR (DIFF_MIN, 0));
283 TI (char, 1, "", a, a + SR (-1, 0));
284 TI (char, 1, "", a, a + SR (-1, 1));
285 TI (char, 1, "", a, a + SR (0, 1));
286 TI (char, 1, "", a, a + SR (0, DIFF_MAX - 1));
287 TI (char, 2, "0", a, a + SR (0, DIFF_MAX - 1));
288 TI (char, 2, "0", a, a + SR (1, DIFF_MAX - 1));
289 /* The following needs a warning for reading past the end. */
290 TI (char, 2, "0", a, a + SR (2, DIFF_MAX - 1));
291 TI (char, 2, "0", a, a + SR (3, DIFF_MAX - 1)); /* { dg-warning "offset \\\[3, \[0-9\]+] is out of the bounds \\\[0, 2] of object \[^\n\r\]+ with type .char ?\\\[2\\\]." "strcpy" } */
293 TI (char, 3, "01", a, a + SR (0, DIFF_MAX - 1));
294 TI (char, 3, "01", a, a + SR (1, DIFF_MAX - 1));
295 TI (char, 3, "01", a, a + SR (2, DIFF_MAX - 1));
296 /* The following needs a warning for reading past the end. */
297 TI (char, 3, "01", a, a + SR (3, DIFF_MAX - 1));
298 TI (char, 3, "01", a, a + SR (4, DIFF_MAX - 1)); /* { dg-warning "offset \\\[4, \[0-9\]+] is out of the bounds \\\[0, 3] of object \[^\n\r\]+ with type .char ?\\\[3\\\]." "strcpy" } */
300 TI (char, 4, "012", a, a + SR (DIFF_MAX - 2, DIFF_MAX - 1)); /* { dg-warning "offset \\\[\[0-9\]+, \[0-9\]+] is out of the bounds \\\[0, 4] of object \[^\n\r\]+ with type .char ?\\\[4\\\]." "strcpy" } */
303 TI (char, 1, "", a + SR (DIFF_MIN, 0), s);
304 TI (char, 1, "", a + SR (-1, 0), s);
305 TI (char, 1, "", a + SR (-1, 1), s);
306 TI (char, 1, "", a + SR (0, 1), s);
307 TI (char, 1, "", a + SR (0, DIFF_MAX - 1), s);
308 TI (char, 2, "", a + SR (0, DIFF_MAX - 1), s);
309 TI (char, 2, "", a + SR (1, DIFF_MAX - 1), s);
310 /* The following is diagnosed not because the initial source offset
311 it out of bounds (it isn't) but because the final source offset
312 after the access has completed, is. It would be clearer if
313 the warning mentioned the final offset. */
314 TI (char, 2, "", a + SR (2, DIFF_MAX - 1), s); /* { dg-warning "forming offset 3 is out of the bounds \\\[0, 2] of object \[^\n\r\]+ with type .char ?\\\[2\\\]." "strcpy" } */
315 TI (char, 2, "", a + SR (3, DIFF_MAX - 1), s); /* { dg-warning "offset \\\[3, \[0-9\]+] is out of the bounds \\\[0, 2] of object \[^\n\r\]+ with type .char ?\\\[2\\\]." "strcpy" } */
317 TI (char, 3, "", a + SR (0, DIFF_MAX - 1), s);
318 TI (char, 3, "", a + SR (1, DIFF_MAX - 1), s);
319 TI (char, 3, "", a + SR (2, DIFF_MAX - 1), s);
320 TI (char, 3, "", a + SR (3, DIFF_MAX - 1), s); /* { dg-warning "forming offset 4 is out of the bounds \\\[0, 3] of object \[^\n\r\]+ with type .char ?\\\[3\\\]." "strcpy" } */
321 TI (char, 3, "", a + SR (4, DIFF_MAX - 1), s); /* { dg-warning "offset \\\[4, \[0-9\]+] is out of the bounds \\\[0, 3] of object \[^\n\r\]+ with type .char ?\\\[3\\\]." "strcpy" } */
323 TI (char, 4, "", a + SR (DIFF_MAX - 2, DIFF_MAX - 1), s); /* { dg-warning "offset \\\[\[0-9\]+, \[0-9\]+] is out of the bounds \\\[0, 4] of object \[^\n\r\]+ with type .char ?\\\[4\\\]." "strcpy" } */
326 struct MA
328 int i;
329 char a5[5];
330 char a11[11];
333 struct MA2
335 struct MA ma3[3];
336 struct MA ma5[5];
337 char ax[];
340 struct MA3
342 struct MA2 ma5[3];
343 struct MA2 ma7[7];
346 void test_strcpy_bounds_memarray_range (void)
348 #undef TM
349 #define TM(mem, init, dst, src) \
350 do { \
351 struct MA ma; \
352 strcpy (ma.mem, init); \
353 strcpy (dst, src); \
354 sink (&ma); \
355 } while (0)
357 ptrdiff_t i = SR (1, 2);
359 TM (a5, "0", ma.a5 + i, ma.a5);
360 TM (a5, "01", ma.a5 + i, ma.a5);
361 TM (a5, "012", ma.a5 + i, ma.a5);
362 TM (a5, "0123", ma.a5 + i, ma.a5); /* { dg-warning "offset 10 from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]. at offset 4" "strcpy" } */
364 TM (a11, "0", ma.a5, ma.a11);
365 TM (a11, "01", ma.a5, ma.a11);
366 TM (a11, "012", ma.a5, ma.a11);
367 TM (a11, "0123", ma.a5, ma.a11);
368 TM (a11, "01234", ma.a5, ma.a11); /* { dg-warning "offset 10 from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]' at offset 4" } */
369 TM (a11, "012345", ma.a5, ma.a11); /* { dg-warning "offset \\\[10, 11] from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]' at offset 4" } */
370 TM (a11, "0123456", ma.a5, ma.a11); /* { dg-warning "offset \\\[10, 12] from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]' at offset 4" } */
372 TM (a11, "0123456", ma.a11 + i, "789abcd");
375 void test_strcpy_bounds_memarray_var (struct MA *pma,
376 struct MA2 *pma2,
377 struct MA3 *pma3,
378 const char *s, size_t n)
380 #undef TM
381 #define TM(dst, src) do { \
382 strcpy (dst, src); \
383 sink (dst, src); \
384 } while (0)
386 TM (pma->a5, s);
387 TM (pma->a5 + 0, s);
388 TM (pma->a5 + 1, s);
389 TM (pma->a5 + 4, s);
391 /* The following forms a pointer during the call that's outside
392 the bounds of the array it was derived from (pma->a5) so
393 it should be diagnosed but the representation of the pointer
394 addition doesn't contain information to distinguish it from
395 the valid pma->a11 + 1 so this is an XFAIL. */
396 TM (pma->a5 + 5, s); /* { dg-warning "offset 17 from the object at .pma. is out of the bounds of .struct MA." "strcpy" { xfail *-*-* } } */
398 /* The following also forms an out-of-bounds pointer but similar
399 to the above, there is no reliable way to distinguish it from
400 (char*)&pma[1].i + 1 so this too is not diagnosed. */
401 TM (pma->a5 + sizeof *pma + 1, s); /* { dg-warning "offset 17 from the object at .pma. is out of the bounds of .struct MA." "strcpy" { xfail *-*-* } } */
403 TM (pma->a5 - 1, s); /* { dg-warning "offset -1 from the object at .pma. is out of the bounds of .struct MA." "strcpy" { xfail *-*-* } } */
405 TM (pma[1].a5, s);
406 TM (pma[2].a5 + 0, s);
407 TM (pma[3].a5 + 1, s);
408 TM (pma[4].a5 + 4, s);
411 extern struct MA3 ma3[3];
412 TM (ma3[0].ma5[0].ma3[0].a5 + 6, s);