2 /* { dg-options "-O2" } */
3 /* { dg-require-effective-target alloca } */
5 typedef __SIZE_TYPE__
size_t;
6 extern void abort (void);
7 extern void exit (int);
8 extern void *malloc (size_t);
9 extern void *calloc (size_t, size_t);
10 extern void *alloca (size_t);
11 extern void *memcpy (void *, const void *, size_t);
12 extern void *memset (void *, int, size_t);
13 extern char *strcpy (char *, const char *);
24 extern struct A zerol
[0];
27 __attribute__ ((noinline
))
28 test1 (void *q
, int x
)
31 void *p
= &a
.a
[3], *r
;
37 if (__builtin_object_size (p
, 0)
38 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 3)
40 if (__builtin_object_size (&a
.c
[9], 0)
41 != sizeof (a
) - __builtin_offsetof (struct A
, c
) - 9)
43 if (__builtin_object_size (q
, 0) != (size_t) -1)
45 #ifdef __builtin_object_size
46 if (__builtin_object_size (r
, 0)
48 ? sizeof (a
) - __builtin_offsetof (struct A
, a
) - 9
49 : sizeof (a
) - __builtin_offsetof (struct A
, c
) - 1))
52 if (__builtin_object_size (r
, 0)
53 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 9)
60 if (__builtin_object_size (&y
, 0)
63 if (__builtin_object_size (w
, 0)
66 if (__builtin_object_size (&y
.b
, 0)
67 != sizeof (a
) - __builtin_offsetof (struct A
, b
))
69 #ifdef __builtin_object_size
70 if (__builtin_object_size (r
, 0)
72 ? 2 * sizeof (w
[0]) - __builtin_offsetof (struct A
, a
) - 1
73 : sizeof (a
) - __builtin_offsetof (struct A
, a
) - 6))
76 if (__builtin_object_size (r
, 0)
77 != 2 * sizeof (w
[0]) - __builtin_offsetof (struct A
, a
) - 1)
84 #ifdef __builtin_object_size
85 if (__builtin_object_size (r
, 0) != (x
< 20 ? 30 : 2 * 16))
88 /* We may duplicate this test onto the two exit paths. On one path
89 the size will be 32, the other it will be 30. If we don't duplicate
90 this test, then the size will be 32. */
91 if (__builtin_object_size (r
, 0) != 2 * 16
92 && __builtin_object_size (r
, 0) != 30)
99 #ifdef __builtin_object_size
100 if (__builtin_object_size (r
, 0) != (x
< 20 ? 30 : 2 * 14))
103 if (__builtin_object_size (r
, 0) != 30)
107 r
= malloc (sizeof (a
));
110 #ifdef __builtin_object_size
111 if (__builtin_object_size (r
, 0) != (x
< 30 ? sizeof (a
) : sizeof (a
) - 3))
114 if (__builtin_object_size (r
, 0) != sizeof (a
))
117 r
= memcpy (r
, "a", 2);
118 #ifdef __builtin_object_size
119 if (__builtin_object_size (r
, 0) != (x
< 30 ? sizeof (a
) : sizeof (a
) - 3))
122 if (__builtin_object_size (r
, 0) != sizeof (a
))
125 r
= memcpy (r
+ 2, "b", 2) + 2;
126 #ifdef __builtin_object_size
127 if (__builtin_object_size (r
, 0)
128 != (x
< 30 ? sizeof (a
) - 4 : sizeof (a
) - 7))
131 if (__builtin_object_size (r
, 0) != sizeof (a
) - 4)
135 r
= memset (r
, 'a', 2);
136 if (__builtin_object_size (r
, 0)
137 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 4)
139 r
= memset (r
+ 2, 'b', 2) + 2;
140 if (__builtin_object_size (r
, 0)
141 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 8)
144 r
= strcpy (r
, "ab");
145 if (__builtin_object_size (r
, 0)
146 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 1)
148 r
= strcpy (r
+ 2, "cd") + 2;
149 if (__builtin_object_size (r
, 0)
150 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 5)
152 if (__builtin_object_size (exta
, 0) != (size_t) -1)
154 if (__builtin_object_size (exta
+ 10, 0) != (size_t) -1)
156 if (__builtin_object_size (&exta
[5], 0) != (size_t) -1)
158 if (__builtin_object_size (extb
, 0) != sizeof (extb
))
160 if (__builtin_object_size (extb
+ 10, 0) != sizeof (extb
) - 10)
162 if (__builtin_object_size (&extb
[5], 0) != sizeof (extb
) - 5)
164 #ifdef __builtin_object_size
165 if (__builtin_object_size (var
, 0) != x
+ 10)
167 if (__builtin_object_size (var
+ 10, 0) != x
)
169 if (__builtin_object_size (&var
[5], 0) != x
+ 5)
172 if (__builtin_object_size (var
, 0) != (size_t) -1)
174 if (__builtin_object_size (var
+ 10, 0) != (size_t) -1)
176 if (__builtin_object_size (&var
[5], 0) != (size_t) -1)
179 if (__builtin_object_size (zerol
, 0) != 0)
181 if (__builtin_object_size (&zerol
, 0) != 0)
183 if (__builtin_object_size (&zerol
[0], 0) != 0)
185 if (__builtin_object_size (zerol
[0].a
, 0) != 0)
187 if (__builtin_object_size (&zerol
[0].a
[0], 0) != 0)
189 if (__builtin_object_size (&zerol
[0].b
, 0) != 0)
191 if (__builtin_object_size ("abcdefg", 0) != sizeof ("abcdefg"))
193 if (__builtin_object_size ("abcd\0efg", 0) != sizeof ("abcd\0efg"))
195 if (__builtin_object_size (&"abcd\0efg", 0) != sizeof ("abcd\0efg"))
197 if (__builtin_object_size (&"abcd\0efg"[0], 0) != sizeof ("abcd\0efg"))
199 if (__builtin_object_size (&"abcd\0efg"[4], 0) != sizeof ("abcd\0efg") - 4)
201 if (__builtin_object_size ("abcd\0efg" + 5, 0) != sizeof ("abcd\0efg") - 5)
203 if (__builtin_object_size (L
"abcdefg", 0) != sizeof (L
"abcdefg"))
205 r
= (char *) L
"abcd\0efg";
206 if (__builtin_object_size (r
+ 2, 0) != sizeof (L
"abcd\0efg") - 2)
213 __attribute__ ((noinline
))
216 struct B
{ char buf1
[10]; char buf2
[10]; } a
;
221 if (sizeof (a
) != 20)
225 for (i
= 0; i
< 4; ++i
)
231 else if (i
== l1
+ 1)
233 else if (i
== l1
+ 2)
236 #ifdef __builtin_object_size
239 for (i
= 0; i
< 4; ++i
)
242 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf1
) - 1;
244 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf2
) - 7;
245 else if (i
== l1
+ 1)
246 res
= sizeof (buf3
) - 5;
247 else if (i
== l1
+ 2)
248 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf1
) - 9;
253 if (__builtin_object_size (r
, 0) != res
)
256 for (i
= 0; i
< 4; ++i
)
262 else if (i
== l1
+ 1)
264 else if (i
== l1
+ 2)
267 #ifdef __builtin_object_size
268 res
= sizeof (buf3
) - 20;
270 for (i
= 0; i
< 4; ++i
)
273 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf1
) - 7;
275 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf2
) - 7;
276 else if (i
== l1
+ 1)
277 res
= sizeof (buf3
) - 5;
278 else if (i
== l1
+ 2)
279 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf1
) - 9;
281 if (__builtin_object_size (r
, 0) != res
)
286 if (__builtin_object_size (r
, 0) != res
)
289 #ifdef __builtin_object_size
291 if (__builtin_object_size (r
, 0) != res
)
295 if (__builtin_object_size (r
+ 6, 0) != res
- 6)
298 else if (__builtin_object_size (r
+ 6, 0) != 0)
301 if (__builtin_object_size (r
, 0) != 7)
303 if (__builtin_object_size (r
+ 6, 0) != 1)
307 for (i
= 0; i
< 4; ++i
)
313 else if (i
== l1
+ 1)
315 else if (i
== l1
+ 2)
318 #ifdef __builtin_object_size
319 res
= sizeof (buf3
) - 18;
321 for (i
= 0; i
< 4; ++i
)
324 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf1
) - 9;
326 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf2
) - 9;
327 else if (i
== l1
+ 1)
328 res
= sizeof (buf3
) - 5;
329 else if (i
== l1
+ 2)
330 res
= sizeof (a
) - __builtin_offsetof (struct B
, buf1
) - 4;
334 if (__builtin_object_size (r
+ 12, 0) != res
- 12)
337 else if (__builtin_object_size (r
+ 12, 0) != 0)
340 if (__builtin_object_size (r
+ 12, 0) != 4)
346 __attribute__ ((noinline
))
350 struct B
{ struct A a
[2]; struct A b
; char c
[4]; char d
; double e
;
351 _Complex
double f
; } x
;
356 if (__builtin_object_size (buf4
, 0) != sizeof (buf4
))
358 if (__builtin_object_size (&buf4
, 0) != sizeof (buf4
))
360 if (__builtin_object_size (&buf4
[0], 0) != sizeof (buf4
))
362 if (__builtin_object_size (&buf4
[1], 0) != sizeof (buf4
) - 1)
364 if (__builtin_object_size (&x
, 0) != sizeof (x
))
366 if (__builtin_object_size (&x
.a
, 0) != sizeof (x
))
368 if (__builtin_object_size (&x
.a
[0], 0) != sizeof (x
))
370 if (__builtin_object_size (&x
.a
[0].a
, 0) != sizeof (x
))
372 if (__builtin_object_size (&x
.a
[0].a
[0], 0) != sizeof (x
))
374 if (__builtin_object_size (&x
.a
[0].a
[3], 0) != sizeof (x
) - 3)
376 if (__builtin_object_size (&x
.a
[0].b
, 0)
377 != sizeof (x
) - __builtin_offsetof (struct A
, b
))
379 if (__builtin_object_size (&x
.a
[1].c
, 0)
380 != sizeof (x
) - sizeof (struct A
) - __builtin_offsetof (struct A
, c
))
382 if (__builtin_object_size (&x
.a
[1].c
[0], 0)
383 != sizeof (x
) - sizeof (struct A
) - __builtin_offsetof (struct A
, c
))
385 if (__builtin_object_size (&x
.a
[1].c
[3], 0)
386 != sizeof (x
) - sizeof (struct A
) - __builtin_offsetof (struct A
, c
) - 3)
388 if (__builtin_object_size (&x
.b
, 0)
389 != sizeof (x
) - __builtin_offsetof (struct B
, b
))
391 if (__builtin_object_size (&x
.b
.a
, 0)
392 != sizeof (x
) - __builtin_offsetof (struct B
, b
))
394 if (__builtin_object_size (&x
.b
.a
[0], 0)
395 != sizeof (x
) - __builtin_offsetof (struct B
, b
))
397 if (__builtin_object_size (&x
.b
.a
[3], 0)
398 != sizeof (x
) - __builtin_offsetof (struct B
, b
) - 3)
400 if (__builtin_object_size (&x
.b
.b
, 0)
401 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
402 - __builtin_offsetof (struct A
, b
))
404 if (__builtin_object_size (&x
.b
.c
, 0)
405 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
406 - __builtin_offsetof (struct A
, c
))
408 if (__builtin_object_size (&x
.b
.c
[0], 0)
409 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
410 - __builtin_offsetof (struct A
, c
))
412 if (__builtin_object_size (&x
.b
.c
[3], 0)
413 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
414 - __builtin_offsetof (struct A
, c
) - 3)
416 if (__builtin_object_size (&x
.c
, 0)
417 != sizeof (x
) - __builtin_offsetof (struct B
, c
))
419 if (__builtin_object_size (&x
.c
[0], 0)
420 != sizeof (x
) - __builtin_offsetof (struct B
, c
))
422 if (__builtin_object_size (&x
.c
[1], 0)
423 != sizeof (x
) - __builtin_offsetof (struct B
, c
) - 1)
425 if (__builtin_object_size (&x
.d
, 0)
426 != sizeof (x
) - __builtin_offsetof (struct B
, d
))
428 if (__builtin_object_size (&x
.e
, 0)
429 != sizeof (x
) - __builtin_offsetof (struct B
, e
))
431 if (__builtin_object_size (&x
.f
, 0)
432 != sizeof (x
) - __builtin_offsetof (struct B
, f
))
435 if (__builtin_object_size (dp
, 0)
436 != sizeof (x
) - __builtin_offsetof (struct B
, f
))
439 if (__builtin_object_size (dp
, 0)
440 != sizeof (x
) - __builtin_offsetof (struct B
, f
)
444 if (__builtin_object_size (dp
, 0) != sizeof (y
))
446 if (__builtin_object_size (&z
, 0) != sizeof (z
))
449 if (__builtin_object_size (dp
, 0) != sizeof (z
))
452 if (__builtin_object_size (dp
, 0) != sizeof (z
) / 2)
456 struct S
{ unsigned int a
; };
459 __attribute__ ((noinline
))
460 test4 (char *x
, int y
)
465 for (i
= 0; i
< y
; i
++)
469 if (__builtin_object_size (p
, 0) != (size_t) -1)
476 __attribute__ ((noinline
))
483 for (i
= 0; i
< x
; ++i
)
485 #ifdef __builtin_object_size
486 if (__builtin_object_size (p
, 0) != sizeof (buf
) - 8 - 4 * x
)
489 /* My understanding of ISO C99 6.5.6 is that a conforming
490 program will not end up with p equal to &buf[0]
491 through &buf[7], i.e. calling this function with say
492 UINTPTR_MAX / 4 results in undefined behavior.
493 If that's true, then the maximum number of remaining
494 bytes from p until end of the object is 56, otherwise
495 it would be 64 (or conservative (size_t) -1 == unknown). */
496 if (__builtin_object_size (p
, 0) != sizeof (buf
) - 8)
499 memset (p
, ' ', sizeof (buf
) - 8 - 4 * 4);
503 __attribute__ ((noinline
))
506 struct T
{ char buf
[64]; char buf2
[64]; } t
;
510 for (i
= 0; i
< x
; ++i
)
512 #ifdef __builtin_object_size
513 if (__builtin_object_size (p
, 0) != sizeof (t
) - 8 - 4 * x
)
516 if (__builtin_object_size (p
, 0) != sizeof (t
) - 8)
519 memset (p
, ' ', sizeof (t
) - 8 - 4 * 4);
523 __attribute__ ((noinline
))
527 struct T
{ char buf
[64]; char buf2
[64]; } t
;
528 char *p
= &buf
[64], *q
= &t
.buf
[64];
530 if (__builtin_object_size (p
+ 64, 0) != 0)
532 if (__builtin_object_size (q
+ 63, 0) != sizeof (t
) - 64 - 63)
534 if (__builtin_object_size (q
+ 64, 0) != sizeof (t
) - 64 - 64)
536 if (__builtin_object_size (q
+ 256, 0) != 0)
541 __attribute__ ((noinline
))
544 struct T
{ char buf
[10]; char buf2
[10]; } t
;
545 char *p
= &t
.buf2
[-4];
546 char *q
= &t
.buf2
[0];
547 if (__builtin_object_size (p
, 0) != sizeof (t
) - 10 + 4)
549 if (__builtin_object_size (q
, 0) != sizeof (t
) - 10)
551 /* GCC only handles additions, not subtractions. */
553 if (__builtin_object_size (q
, 0) != (size_t) -1
554 && __builtin_object_size (q
, 0) != sizeof (t
) - 10 + 8)
557 if (__builtin_object_size (p
, 0) != 0)
562 __attribute__ ((noinline
))
563 test9 (unsigned cond
)
565 char *buf2
= malloc (10);
573 #ifdef __builtin_object_size
574 if (__builtin_object_size (&p
[-4], 0) != (cond
? 6 : 10))
577 if (__builtin_object_size (&p
[-4], 0) != 10)
581 for (unsigned i
= cond
; i
> 0; i
--)
584 #ifdef __builtin_object_size
585 if (__builtin_object_size (p
, 0) != ((cond
? 2 : 6) + cond
))
588 if (__builtin_object_size (p
, 0) != 10)
593 for (unsigned i
= cond
; i
> 0; i
--)
596 #ifdef __builtin_object_size
597 if (__builtin_object_size (p
, 0)
598 != sizeof (y
) - __builtin_offsetof (struct A
, c
) - 8 + cond
)
601 if (__builtin_object_size (p
, 0) != sizeof (y
))
607 __attribute__ ((noinline
))
610 static char buf
[255];
611 unsigned int i
, len
= sizeof (buf
);
614 for (i
= 0 ; i
< sizeof (buf
) ; i
++)
618 #ifdef __builtin_object_size
619 if (__builtin_object_size (p
- 3, 0) != sizeof (buf
) - i
+ 3)
622 if (__builtin_object_size (p
- 3, 0) != sizeof (buf
))
636 __asm ("" : "=r" (l1
) : "0" (l1
));
640 test4 ((char *) s
, 10);