2 /* { dg-options "-O2 -Wno-stringop-overread" } */
3 /* { dg-require-effective-target alloca } */
5 #include "builtin-object-size-common.h"
16 extern struct A extc
[];
20 __attribute__ ((noinline
))
21 test1 (void *q
, int x
)
24 void *p
= &a
.a
[3], *r
;
26 struct A vara
[x
+ 10];
31 if (__builtin_object_size (p
, 3) != sizeof (a
.a
) - 3)
33 if (__builtin_object_size (&a
.c
[9], 3)
36 if (__builtin_object_size (q
, 3) != 0)
38 #ifdef __builtin_object_size
39 if (__builtin_object_size (r
, 3)
40 != (x
< 0 ? sizeof (a
.a
) - 9 : sizeof (a
.c
) - 1))
42 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 9)
49 if (__builtin_object_size (&y
, 3) != sizeof (y
))
51 if (__builtin_object_size (w
, 3) != sizeof (w
))
53 if (__builtin_object_size (&y
.b
, 3) != sizeof (a
.b
))
55 #ifdef __builtin_object_size
56 if (__builtin_object_size (r
, 3)
57 != (x
< 6 ? sizeof (w
[2].a
) - 1 : sizeof (a
.a
) - 6))
59 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 6)
66 #ifdef __builtin_object_size
67 if (__builtin_object_size (r
, 3) != (x
< 20 ? 30 : 2 * 16))
69 if (__builtin_object_size (r
, 3) != 30)
76 #ifdef __builtin_object_size
77 if (__builtin_object_size (r
, 3) != (x
< 20 ? 30 : 2 * 14))
79 if (__builtin_object_size (r
, 3) != 2 * 14)
83 r
= malloc (sizeof (a
));
86 #ifdef __builtin_object_size
87 size_t objsz
= x
< 30 ? sizeof (a
) : sizeof (a
.a
) - 3;
88 if (__builtin_object_size (r
, 3) != objsz
)
90 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 3)
93 r
= memcpy (r
, "a", 2);
94 #ifdef __builtin_object_size
95 if (__builtin_object_size (r
, 3) != objsz
)
97 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 3)
100 r
= memcpy (r
+ 2, "b", 2) + 2;
101 #ifdef __builtin_object_size
102 if (__builtin_object_size (r
, 3) != objsz
- 4)
104 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 3 - 4)
108 r
= memset (r
, 'a', 2);
109 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 4)
111 r
= memset (r
+ 2, 'b', 2) + 2;
112 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 8)
115 r
= strcpy (r
, "ab");
116 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 1)
118 r
= strcpy (r
+ 2, "cd") + 2;
119 if (__builtin_object_size (r
, 3) != sizeof (a
.a
) - 5)
121 if (__builtin_object_size (exta
, 3) != 0)
123 if (__builtin_object_size (exta
+ 10, 3) != 0)
125 if (__builtin_object_size (&exta
[5], 3) != 0)
127 if (__builtin_object_size (extb
, 3) != sizeof (extb
))
129 if (__builtin_object_size (extb
+ 10, 3) != sizeof (extb
) - 10)
131 if (__builtin_object_size (&extb
[5], 3) != sizeof (extb
) - 5)
133 if (__builtin_object_size (extc
, 3) != 0)
135 if (__builtin_object_size (extc
+ 10, 3) != 0)
137 if (__builtin_object_size (&extc
[5], 3) != 0)
139 if (__builtin_object_size (&extc
->a
, 3) != 0)
141 if (__builtin_object_size (&(extc
+ 10)->b
, 3) != 0)
143 if (__builtin_object_size (&extc
[5].c
[3], 3) != 0)
145 #ifdef __builtin_object_size
146 if (__builtin_object_size (var
, 3) != x
+ 10)
148 if (__builtin_object_size (var
+ 10, 3) != x
)
150 if (__builtin_object_size (&var
[5], 3) != x
+ 5)
152 if (__builtin_object_size (vara
, 3) != (x
+ 10) * sizeof (struct A
))
154 if (__builtin_object_size (vara
+ 10, 3) != x
* sizeof (struct A
))
156 if (__builtin_object_size (&vara
[5], 3) != (x
+ 5) * sizeof (struct A
))
159 if (__builtin_object_size (var
, 3) != 0)
161 if (__builtin_object_size (var
+ 10, 3) != 0)
163 if (__builtin_object_size (&var
[5], 3) != 0)
165 if (__builtin_object_size (vara
, 3) != 0)
167 if (__builtin_object_size (vara
+ 10, 3) != 0)
169 if (__builtin_object_size (&vara
[5], 3) != 0)
172 if (__builtin_object_size (&vara
[0].a
, 3) != sizeof (vara
[0].a
))
174 if (__builtin_object_size (&vara
[10].a
[0], 3) != sizeof (vara
[0].a
))
176 if (__builtin_object_size (&vara
[5].a
[4], 3) != sizeof (vara
[0].a
) - 4)
178 if (__builtin_object_size (&vara
[5].b
, 3) != sizeof (vara
[0].b
))
180 if (__builtin_object_size (&vara
[7].c
[7], 3) != sizeof (vara
[0].c
) - 7)
182 if (__builtin_object_size (zerol
, 3) != 0)
184 if (__builtin_object_size (&zerol
, 3) != 0)
186 if (__builtin_object_size (&zerol
[0], 3) != 0)
188 if (__builtin_object_size (zerol
[0].a
, 3) != 0)
190 if (__builtin_object_size (&zerol
[0].a
[0], 3) != 0)
192 if (__builtin_object_size (&zerol
[0].b
, 3) != 0)
194 if (__builtin_object_size ("abcdefg", 3) != sizeof ("abcdefg"))
196 if (__builtin_object_size ("abcd\0efg", 3) != sizeof ("abcd\0efg"))
198 if (__builtin_object_size (&"abcd\0efg", 3) != sizeof ("abcd\0efg"))
200 if (__builtin_object_size (&"abcd\0efg"[0], 3) != sizeof ("abcd\0efg"))
202 if (__builtin_object_size (&"abcd\0efg"[4], 3) != sizeof ("abcd\0efg") - 4)
204 if (__builtin_object_size ("abcd\0efg" + 5, 3) != sizeof ("abcd\0efg") - 5)
206 if (__builtin_object_size (L
"abcdefg", 3) != sizeof (L
"abcdefg"))
208 r
= (char *) L
"abcd\0efg";
209 if (__builtin_object_size (r
+ 2, 3) != sizeof (L
"abcd\0efg") - 2)
211 /* Prevent DSE from removing calls that prevent bad combining of
212 addresses and offsets. */
213 asm volatile ("" : : "g" (&a
));
219 __attribute__ ((noinline
))
222 struct B
{ char buf1
[10]; char buf2
[10]; } a
;
225 #ifdef __builtin_object_size
229 if (sizeof (a
) != 20)
233 for (i
= 0; i
< 4; ++i
)
239 else if (i
== l1
+ 1)
241 else if (i
== l1
+ 2)
244 if (__builtin_object_size (r
, 3) != sizeof (a
.buf1
) - 9)
247 for (i
= 0; i
< 4; ++i
)
253 else if (i
== l1
+ 1)
255 else if (i
== l1
+ 2)
258 if (__builtin_object_size (r
, 3) != 0)
261 for (i
= 0; i
< 4; ++i
)
267 else if (i
== l1
+ 1)
269 else if (i
== l1
+ 2)
272 #ifdef __builtin_object_size
273 dyn_res
= sizeof (buf3
) - 1;
275 for (i
= 0; i
< 4; ++i
)
278 dyn_res
= sizeof (a
.buf1
) - 6;
280 dyn_res
= sizeof (a
.buf2
) - 4;
281 else if (i
== l1
+ 1)
282 dyn_res
= sizeof (buf3
) - 5;
283 else if (i
== l1
+ 2)
284 dyn_res
= sizeof (a
.buf1
) - 2;
286 if (__builtin_object_size (r
, 3) != dyn_res
)
289 if (__builtin_object_size (r
, 3) != sizeof (a
.buf1
) - 6)
293 #ifdef __builtin_object_size
294 if (__builtin_object_size (r
, 3) != dyn_res
- 2)
296 if (__builtin_object_size (r
+ 1, 3) != dyn_res
- 3)
299 if (__builtin_object_size (r
, 3) != sizeof (a
.buf1
) - 6 - 2)
301 if (__builtin_object_size (r
+ 1, 3) != sizeof (a
.buf1
) - 6 - 3)
307 __attribute__ ((noinline
))
311 struct B
{ struct A a
[2]; struct A b
; char c
[4]; char d
; double e
;
312 _Complex
double f
; } x
;
317 if (__builtin_object_size (buf4
, 3) != sizeof (buf4
))
319 if (__builtin_object_size (&buf4
, 3) != sizeof (buf4
))
321 if (__builtin_object_size (&buf4
[0], 3) != sizeof (buf4
))
323 if (__builtin_object_size (&buf4
[1], 3) != sizeof (buf4
) - 1)
325 if (__builtin_object_size (&x
, 3) != sizeof (x
))
327 if (__builtin_object_size (&x
.a
, 3) != sizeof (x
.a
))
329 if (__builtin_object_size (&x
.a
[0], 3) != sizeof (x
.a
))
331 if (__builtin_object_size (&x
.a
[0].a
, 3) != sizeof (x
.a
[0].a
))
333 if (__builtin_object_size (&x
.a
[0].a
[0], 3) != sizeof (x
.a
[0].a
))
335 if (__builtin_object_size (&x
.a
[0].a
[3], 3) != sizeof (x
.a
[0].a
) - 3)
337 if (__builtin_object_size (&x
.a
[0].b
, 3) != sizeof (x
.a
[0].b
))
339 if (__builtin_object_size (&x
.a
[1].c
, 3) != sizeof (x
.a
[1].c
))
341 if (__builtin_object_size (&x
.a
[1].c
[0], 3) != sizeof (x
.a
[1].c
))
343 if (__builtin_object_size (&x
.a
[1].c
[3], 3) != sizeof (x
.a
[1].c
) - 3)
345 if (__builtin_object_size (&x
.b
, 3) != sizeof (x
.b
))
347 if (__builtin_object_size (&x
.b
.a
, 3) != sizeof (x
.b
.a
))
349 if (__builtin_object_size (&x
.b
.a
[0], 3) != sizeof (x
.b
.a
))
351 if (__builtin_object_size (&x
.b
.a
[3], 3) != sizeof (x
.b
.a
) - 3)
353 if (__builtin_object_size (&x
.b
.b
, 3) != sizeof (x
.b
.b
))
355 if (__builtin_object_size (&x
.b
.c
, 3) != sizeof (x
.b
.c
))
357 if (__builtin_object_size (&x
.b
.c
[0], 3) != sizeof (x
.b
.c
))
359 if (__builtin_object_size (&x
.b
.c
[3], 3) != sizeof (x
.b
.c
) - 3)
361 if (__builtin_object_size (&x
.c
, 3) != sizeof (x
.c
))
363 if (__builtin_object_size (&x
.c
[0], 3) != sizeof (x
.c
))
365 if (__builtin_object_size (&x
.c
[1], 3) != sizeof (x
.c
) - 1)
367 if (__builtin_object_size (&x
.d
, 3) != sizeof (x
.d
))
369 if (__builtin_object_size (&x
.e
, 3) != sizeof (x
.e
))
371 if (__builtin_object_size (&x
.f
, 3) != sizeof (x
.f
))
374 if (__builtin_object_size (dp
, 3) != sizeof (x
.f
) / 2)
377 if (__builtin_object_size (dp
, 3) != sizeof (x
.f
) / 2)
380 if (__builtin_object_size (dp
, 3) != sizeof (y
))
382 if (__builtin_object_size (&z
, 3) != sizeof (z
))
385 if (__builtin_object_size (dp
, 3) != sizeof (z
) / 2)
388 if (__builtin_object_size (dp
, 3) != sizeof (z
) / 2)
392 struct S
{ unsigned int a
; };
395 __attribute__ ((noinline
))
396 test4 (char *x
, int y
)
401 for (i
= 0; i
< y
; i
++)
405 if (__builtin_object_size (p
, 3) != 0)
412 __attribute__ ((noinline
))
415 struct T
{ char buf
[64]; char buf2
[64]; } t
;
419 for (i
= 0; i
< x
; ++i
)
421 #ifdef __builtin_object_size
422 if (__builtin_object_size (p
, 3) != sizeof (t
.buf
) - 8 - 4 * x
)
424 if (__builtin_object_size (p
, 3) != 0)
427 memset (p
, ' ', sizeof (t
.buf
) - 8 - 4 * 4);
431 __attribute__ ((noinline
))
435 struct T
{ char buf
[64]; char buf2
[64]; } t
;
436 char *p
= &buf
[64], *q
= &t
.buf
[64];
438 if (__builtin_object_size (p
+ 64, 3) != 0)
440 if (__builtin_object_size (q
+ 0, 3) != 0)
442 if (__builtin_object_size (q
+ 64, 3) != 0)
447 __attribute__ ((noinline
))
450 struct T
{ char buf
[10]; char buf2
[10]; } t
;
451 char *p
= &t
.buf2
[-4];
452 char *q
= &t
.buf2
[0];
453 if (__builtin_object_size (p
, 3) != 0)
455 if (__builtin_object_size (q
, 3) != sizeof (t
.buf2
))
458 if (__builtin_object_size (q
, 3) != 0)
461 if (__builtin_object_size (q
, 3) != 0)
464 if (__builtin_object_size (p
, 3) != 0)
469 __attribute__ ((noinline
))
470 test8 (unsigned cond
)
472 char *buf2
= malloc (10);
480 #ifdef __builtin_object_size
481 if (__builtin_object_size (&p
[-4], 3) != (cond
? 6 : 10))
484 if (__builtin_object_size (&p
[-4], 3) != 0)
488 for (unsigned i
= cond
; i
> 0; i
--)
491 #ifdef __builtin_object_size
492 if (__builtin_object_size (p
, 3) != ((cond
? 2 : 6) + cond
))
495 if (__builtin_object_size (p
, 3) != 0)
500 for (unsigned i
= cond
; i
> 0; i
--)
503 #ifdef __builtin_object_size
504 if (__builtin_object_size (p
, 3) != sizeof (y
.c
) - 8 + cond
)
507 if (__builtin_object_size (p
, 3) != 0)
512 /* Tests for strdup/strndup. */
514 __attribute__ ((noinline
))
517 const char *ptr
= "abcdefghijklmnopqrstuvwxyz";
518 char *res
= strndup (ptr
, 21);
519 if (__builtin_object_size (res
, 3) != 22)
524 res
= strndup (ptr
, 32);
525 if (__builtin_object_size (res
, 3) != 27)
531 if (__builtin_object_size (res
, 3) != 27)
536 char *ptr2
= malloc (64);
539 res
= strndup (ptr2
, 21);
540 if (__builtin_object_size (res
, 3) != 1)
545 res
= strndup (ptr2
, 32);
546 if (__builtin_object_size (res
, 3) != 1)
551 res
= strndup (ptr2
, 128);
552 if (__builtin_object_size (res
, 3) != 1)
558 #ifdef __builtin_object_size
559 if (__builtin_object_size (res
, 3) != 27)
561 if (__builtin_object_size (res
, 3) != 1)
568 ptr
= "abcd\0efghijklmnopqrstuvwxyz";
570 if (__builtin_object_size (res
, 3) != 5)
574 res
= strndup (ptr
, 24);
575 if (__builtin_object_size (res
, 3) != 5)
579 res
= strndup (ptr
, 2);
580 if (__builtin_object_size (res
, 3) != 3)
584 res
= strdup (&ptr
[4]);
585 if (__builtin_object_size (res
, 3) != 1)
589 res
= strndup (&ptr
[4], 4);
590 if (__builtin_object_size (res
, 3) != 1)
594 res
= strndup (&ptr
[4], 1);
595 if (__builtin_object_size (res
, 3) != 1)
604 __asm ("" : "=r" (l1
) : "0" (l1
));
608 test4 ((char *) s
, 10);