Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / testsuite / gcc.c-torture / execute / builtins / memset-chk.c
bloba8f09a739312e83b53da092cd03bbe219df86360
1 /* Copyright (C) 2004, 2005 Free Software Foundation.
3 Ensure builtin __memset_chk performs correctly. */
5 extern void abort (void);
6 typedef __SIZE_TYPE__ size_t;
7 extern size_t strlen(const char *);
8 extern void *memcpy (void *, const void *, size_t);
9 extern void *memset (void *, int, size_t);
10 extern int memcmp (const void *, const void *, size_t);
12 #include "chk.h"
14 char buffer[32];
15 int argc = 1;
16 size_t l1 = 1;
17 char *s3 = "FGH";
18 char *s4;
20 void
21 __attribute__((noinline))
22 test1 (void)
24 memset_disallowed = 1;
25 chk_calls = 0;
26 memset (buffer, argc, 0);
27 memset (buffer, argc, 1);
28 memset (buffer, argc, 2);
29 memset (buffer, argc, 3);
30 memset (buffer, argc, 4);
31 memset (buffer, argc, 5);
32 memset (buffer, argc, 6);
33 memset (buffer, argc, 7);
34 memset (buffer, argc, 8);
35 memset (buffer, argc, 9);
36 memset (buffer, argc, 10);
37 memset (buffer, argc, 11);
38 memset (buffer, argc, 12);
39 memset (buffer, argc, 13);
40 memset (buffer, argc, 14);
41 memset (buffer, argc, 15);
42 memset (buffer, argc, 16);
43 memset (buffer, argc, 17);
44 memset_disallowed = 0;
45 if (chk_calls)
46 abort ();
49 /* Test whether compile time checking is done where it should
50 and so is runtime object size checking. */
51 void
52 __attribute__((noinline))
53 test2 (void)
55 struct A { char buf1[10]; char buf2[10]; } a;
56 char *r = l1 == 1 ? &a.buf1[5] : &a.buf2[4];
57 char buf3[20];
58 int i;
59 size_t l;
61 /* The following calls should do runtime checking
62 - length is not known, but destination is. */
63 chk_calls = 0;
64 memset (a.buf1 + 2, 'a', l1);
65 memset (r, '\0', l1 + 1);
66 r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
67 memset (r, argc, l1 + 2);
68 memset (r + 2, 'Q', l1);
69 r = buf3;
70 for (i = 0; i < 4; ++i)
72 if (i == l1 - 1)
73 r = &a.buf1[1];
74 else if (i == l1)
75 r = &a.buf2[7];
76 else if (i == l1 + 1)
77 r = &buf3[5];
78 else if (i == l1 + 2)
79 r = &a.buf1[9];
81 memset (r, '\0', l1);
82 if (chk_calls != 5)
83 abort ();
85 /* Following have known destination and known length,
86 so if optimizing certainly shouldn't result in the checking
87 variants. */
88 chk_calls = 0;
89 memset (a.buf1 + 2, '\0', 1);
90 memset (r, argc, 2);
91 r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
92 memset (r, 'N', 3);
93 r = buf3;
94 l = 4;
95 for (i = 0; i < 4; ++i)
97 if (i == l1 - 1)
98 r = &a.buf1[1], l = 2;
99 else if (i == l1)
100 r = &a.buf2[7], l = 3;
101 else if (i == l1 + 1)
102 r = &buf3[5], l = 4;
103 else if (i == l1 + 2)
104 r = &a.buf1[9], l = 1;
106 memset (r, 'H', 1);
107 /* Here, l is known to be at most 4 and __builtin_object_size (&buf3[16], 0)
108 is 4, so this doesn't need runtime checking. */
109 memset (&buf3[16], 'd', l);
110 /* Neither length nor destination known. Doesn't need runtime checking. */
111 memset (s4, 'a', l1);
112 memset (s4 + 2, '\0', l1 + 2);
113 /* Destination unknown. */
114 memset (s4 + 4, 'b', 2);
115 memset (s4 + 6, '\0', 4);
116 if (chk_calls)
117 abort ();
118 chk_calls = 0;
121 /* Test whether runtime and/or compile time checking catches
122 buffer overflows. */
123 void
124 __attribute__((noinline))
125 test3 (void)
127 struct A { char buf1[10]; char buf2[10]; } a;
128 char buf3[20];
130 chk_fail_allowed = 1;
131 /* Runtime checks. */
132 if (__builtin_setjmp (chk_fail_buf) == 0)
134 memset (&a.buf2[9], '\0', l1 + 1);
135 abort ();
137 if (__builtin_setjmp (chk_fail_buf) == 0)
139 memset (&a.buf2[7], 'T', strlen (s3) + 1);
140 abort ();
142 /* This should be detectable at compile time already. */
143 if (__builtin_setjmp (chk_fail_buf) == 0)
145 memset (&buf3[19], 'b', 2);
146 abort ();
148 chk_fail_allowed = 0;
151 #ifndef MAX_OFFSET
152 #define MAX_OFFSET (sizeof (long long))
153 #endif
155 #ifndef MAX_COPY
156 #define MAX_COPY (10 * sizeof (long long))
157 #define MAX_COPY2 15
158 #else
159 #define MAX_COPY2 MAX_COPY
160 #endif
162 #ifndef MAX_EXTRA
163 #define MAX_EXTRA (sizeof (long long))
164 #endif
166 #define MAX_LENGTH (MAX_OFFSET + MAX_COPY + MAX_EXTRA)
167 #define MAX_LENGTH2 (MAX_OFFSET + MAX_COPY2 + MAX_EXTRA)
169 static union {
170 char buf[MAX_LENGTH];
171 long long align_int;
172 long double align_fp;
173 } u;
175 char A = 'A';
177 void
178 __attribute__((noinline))
179 test4 (void)
181 int off, len, i;
182 char *p, *q;
184 for (off = 0; off < MAX_OFFSET; off++)
185 for (len = 1; len < MAX_COPY; len++)
187 for (i = 0; i < MAX_LENGTH; i++)
188 u.buf[i] = 'a';
190 p = memset (u.buf + off, '\0', len);
191 if (p != u.buf + off)
192 abort ();
194 q = u.buf;
195 for (i = 0; i < off; i++, q++)
196 if (*q != 'a')
197 abort ();
199 for (i = 0; i < len; i++, q++)
200 if (*q != '\0')
201 abort ();
203 for (i = 0; i < MAX_EXTRA; i++, q++)
204 if (*q != 'a')
205 abort ();
207 p = memset (u.buf + off, A, len);
208 if (p != u.buf + off)
209 abort ();
211 q = u.buf;
212 for (i = 0; i < off; i++, q++)
213 if (*q != 'a')
214 abort ();
216 for (i = 0; i < len; i++, q++)
217 if (*q != 'A')
218 abort ();
220 for (i = 0; i < MAX_EXTRA; i++, q++)
221 if (*q != 'a')
222 abort ();
224 p = memset (u.buf + off, 'B', len);
225 if (p != u.buf + off)
226 abort ();
228 q = u.buf;
229 for (i = 0; i < off; i++, q++)
230 if (*q != 'a')
231 abort ();
233 for (i = 0; i < len; i++, q++)
234 if (*q != 'B')
235 abort ();
237 for (i = 0; i < MAX_EXTRA; i++, q++)
238 if (*q != 'a')
239 abort ();
243 static union {
244 char buf[MAX_LENGTH2];
245 long long align_int;
246 long double align_fp;
247 } u2;
249 void reset ()
251 int i;
253 for (i = 0; i < MAX_LENGTH2; i++)
254 u2.buf[i] = 'a';
257 void check (int off, int len, int ch)
259 char *q;
260 int i;
262 q = u2.buf;
263 for (i = 0; i < off; i++, q++)
264 if (*q != 'a')
265 abort ();
267 for (i = 0; i < len; i++, q++)
268 if (*q != ch)
269 abort ();
271 for (i = 0; i < MAX_EXTRA; i++, q++)
272 if (*q != 'a')
273 abort ();
276 void
277 __attribute__((noinline))
278 test5 (void)
280 int off;
281 char *p;
283 /* len == 1 */
284 for (off = 0; off < MAX_OFFSET; off++)
286 reset ();
288 p = memset (u2.buf + off, '\0', 1);
289 if (p != u2.buf + off) abort ();
290 check (off, 1, '\0');
292 p = memset (u2.buf + off, A, 1);
293 if (p != u2.buf + off) abort ();
294 check (off, 1, 'A');
296 p = memset (u2.buf + off, 'B', 1);
297 if (p != u2.buf + off) abort ();
298 check (off, 1, 'B');
301 /* len == 2 */
302 for (off = 0; off < MAX_OFFSET; off++)
304 reset ();
306 p = memset (u2.buf + off, '\0', 2);
307 if (p != u2.buf + off) abort ();
308 check (off, 2, '\0');
310 p = memset (u2.buf + off, A, 2);
311 if (p != u2.buf + off) abort ();
312 check (off, 2, 'A');
314 p = memset (u2.buf + off, 'B', 2);
315 if (p != u2.buf + off) abort ();
316 check (off, 2, 'B');
319 /* len == 3 */
320 for (off = 0; off < MAX_OFFSET; off++)
322 reset ();
324 p = memset (u2.buf + off, '\0', 3);
325 if (p != u2.buf + off) abort ();
326 check (off, 3, '\0');
328 p = memset (u2.buf + off, A, 3);
329 if (p != u2.buf + off) abort ();
330 check (off, 3, 'A');
332 p = memset (u2.buf + off, 'B', 3);
333 if (p != u2.buf + off) abort ();
334 check (off, 3, 'B');
337 /* len == 4 */
338 for (off = 0; off < MAX_OFFSET; off++)
340 reset ();
342 p = memset (u2.buf + off, '\0', 4);
343 if (p != u2.buf + off) abort ();
344 check (off, 4, '\0');
346 p = memset (u2.buf + off, A, 4);
347 if (p != u2.buf + off) abort ();
348 check (off, 4, 'A');
350 p = memset (u2.buf + off, 'B', 4);
351 if (p != u2.buf + off) abort ();
352 check (off, 4, 'B');
355 /* len == 5 */
356 for (off = 0; off < MAX_OFFSET; off++)
358 reset ();
360 p = memset (u2.buf + off, '\0', 5);
361 if (p != u2.buf + off) abort ();
362 check (off, 5, '\0');
364 p = memset (u2.buf + off, A, 5);
365 if (p != u2.buf + off) abort ();
366 check (off, 5, 'A');
368 p = memset (u2.buf + off, 'B', 5);
369 if (p != u2.buf + off) abort ();
370 check (off, 5, 'B');
373 /* len == 6 */
374 for (off = 0; off < MAX_OFFSET; off++)
376 reset ();
378 p = memset (u2.buf + off, '\0', 6);
379 if (p != u2.buf + off) abort ();
380 check (off, 6, '\0');
382 p = memset (u2.buf + off, A, 6);
383 if (p != u2.buf + off) abort ();
384 check (off, 6, 'A');
386 p = memset (u2.buf + off, 'B', 6);
387 if (p != u2.buf + off) abort ();
388 check (off, 6, 'B');
391 /* len == 7 */
392 for (off = 0; off < MAX_OFFSET; off++)
394 reset ();
396 p = memset (u2.buf + off, '\0', 7);
397 if (p != u2.buf + off) abort ();
398 check (off, 7, '\0');
400 p = memset (u2.buf + off, A, 7);
401 if (p != u2.buf + off) abort ();
402 check (off, 7, 'A');
404 p = memset (u2.buf + off, 'B', 7);
405 if (p != u2.buf + off) abort ();
406 check (off, 7, 'B');
409 /* len == 8 */
410 for (off = 0; off < MAX_OFFSET; off++)
412 reset ();
414 p = memset (u2.buf + off, '\0', 8);
415 if (p != u2.buf + off) abort ();
416 check (off, 8, '\0');
418 p = memset (u2.buf + off, A, 8);
419 if (p != u2.buf + off) abort ();
420 check (off, 8, 'A');
422 p = memset (u2.buf + off, 'B', 8);
423 if (p != u2.buf + off) abort ();
424 check (off, 8, 'B');
427 /* len == 9 */
428 for (off = 0; off < MAX_OFFSET; off++)
430 reset ();
432 p = memset (u2.buf + off, '\0', 9);
433 if (p != u2.buf + off) abort ();
434 check (off, 9, '\0');
436 p = memset (u2.buf + off, A, 9);
437 if (p != u2.buf + off) abort ();
438 check (off, 9, 'A');
440 p = memset (u2.buf + off, 'B', 9);
441 if (p != u2.buf + off) abort ();
442 check (off, 9, 'B');
445 /* len == 10 */
446 for (off = 0; off < MAX_OFFSET; off++)
448 reset ();
450 p = memset (u2.buf + off, '\0', 10);
451 if (p != u2.buf + off) abort ();
452 check (off, 10, '\0');
454 p = memset (u2.buf + off, A, 10);
455 if (p != u2.buf + off) abort ();
456 check (off, 10, 'A');
458 p = memset (u2.buf + off, 'B', 10);
459 if (p != u2.buf + off) abort ();
460 check (off, 10, 'B');
463 /* len == 11 */
464 for (off = 0; off < MAX_OFFSET; off++)
466 reset ();
468 p = memset (u2.buf + off, '\0', 11);
469 if (p != u2.buf + off) abort ();
470 check (off, 11, '\0');
472 p = memset (u2.buf + off, A, 11);
473 if (p != u2.buf + off) abort ();
474 check (off, 11, 'A');
476 p = memset (u2.buf + off, 'B', 11);
477 if (p != u2.buf + off) abort ();
478 check (off, 11, 'B');
481 /* len == 12 */
482 for (off = 0; off < MAX_OFFSET; off++)
484 reset ();
486 p = memset (u2.buf + off, '\0', 12);
487 if (p != u2.buf + off) abort ();
488 check (off, 12, '\0');
490 p = memset (u2.buf + off, A, 12);
491 if (p != u2.buf + off) abort ();
492 check (off, 12, 'A');
494 p = memset (u2.buf + off, 'B', 12);
495 if (p != u2.buf + off) abort ();
496 check (off, 12, 'B');
499 /* len == 13 */
500 for (off = 0; off < MAX_OFFSET; off++)
502 reset ();
504 p = memset (u2.buf + off, '\0', 13);
505 if (p != u2.buf + off) abort ();
506 check (off, 13, '\0');
508 p = memset (u2.buf + off, A, 13);
509 if (p != u2.buf + off) abort ();
510 check (off, 13, 'A');
512 p = memset (u2.buf + off, 'B', 13);
513 if (p != u2.buf + off) abort ();
514 check (off, 13, 'B');
517 /* len == 14 */
518 for (off = 0; off < MAX_OFFSET; off++)
520 reset ();
522 p = memset (u2.buf + off, '\0', 14);
523 if (p != u2.buf + off) abort ();
524 check (off, 14, '\0');
526 p = memset (u2.buf + off, A, 14);
527 if (p != u2.buf + off) abort ();
528 check (off, 14, 'A');
530 p = memset (u2.buf + off, 'B', 14);
531 if (p != u2.buf + off) abort ();
532 check (off, 14, 'B');
535 /* len == 15 */
536 for (off = 0; off < MAX_OFFSET; off++)
538 reset ();
540 p = memset (u2.buf + off, '\0', 15);
541 if (p != u2.buf + off) abort ();
542 check (off, 15, '\0');
544 p = memset (u2.buf + off, A, 15);
545 if (p != u2.buf + off) abort ();
546 check (off, 15, 'A');
548 p = memset (u2.buf + off, 'B', 15);
549 if (p != u2.buf + off) abort ();
550 check (off, 15, 'B');
554 void
555 __attribute__((noinline))
556 test6 (void)
558 int len;
559 char *p;
561 /* off == 0 */
562 for (len = 0; len < MAX_COPY2; len++)
564 reset ();
566 p = memset (u2.buf, '\0', len);
567 if (p != u2.buf) abort ();
568 check (0, len, '\0');
570 p = memset (u2.buf, A, len);
571 if (p != u2.buf) abort ();
572 check (0, len, 'A');
574 p = memset (u2.buf, 'B', len);
575 if (p != u2.buf) abort ();
576 check (0, len, 'B');
579 /* off == 1 */
580 for (len = 0; len < MAX_COPY2; len++)
582 reset ();
584 p = memset (u2.buf+1, '\0', len);
585 if (p != u2.buf+1) abort ();
586 check (1, len, '\0');
588 p = memset (u2.buf+1, A, len);
589 if (p != u2.buf+1) abort ();
590 check (1, len, 'A');
592 p = memset (u2.buf+1, 'B', len);
593 if (p != u2.buf+1) abort ();
594 check (1, len, 'B');
597 /* off == 2 */
598 for (len = 0; len < MAX_COPY2; len++)
600 reset ();
602 p = memset (u2.buf+2, '\0', len);
603 if (p != u2.buf+2) abort ();
604 check (2, len, '\0');
606 p = memset (u2.buf+2, A, len);
607 if (p != u2.buf+2) abort ();
608 check (2, len, 'A');
610 p = memset (u2.buf+2, 'B', len);
611 if (p != u2.buf+2) abort ();
612 check (2, len, 'B');
615 /* off == 3 */
616 for (len = 0; len < MAX_COPY2; len++)
618 reset ();
620 p = memset (u2.buf+3, '\0', len);
621 if (p != u2.buf+3) abort ();
622 check (3, len, '\0');
624 p = memset (u2.buf+3, A, len);
625 if (p != u2.buf+3) abort ();
626 check (3, len, 'A');
628 p = memset (u2.buf+3, 'B', len);
629 if (p != u2.buf+3) abort ();
630 check (3, len, 'B');
633 /* off == 4 */
634 for (len = 0; len < MAX_COPY2; len++)
636 reset ();
638 p = memset (u2.buf+4, '\0', len);
639 if (p != u2.buf+4) abort ();
640 check (4, len, '\0');
642 p = memset (u2.buf+4, A, len);
643 if (p != u2.buf+4) abort ();
644 check (4, len, 'A');
646 p = memset (u2.buf+4, 'B', len);
647 if (p != u2.buf+4) abort ();
648 check (4, len, 'B');
651 /* off == 5 */
652 for (len = 0; len < MAX_COPY2; len++)
654 reset ();
656 p = memset (u2.buf+5, '\0', len);
657 if (p != u2.buf+5) abort ();
658 check (5, len, '\0');
660 p = memset (u2.buf+5, A, len);
661 if (p != u2.buf+5) abort ();
662 check (5, len, 'A');
664 p = memset (u2.buf+5, 'B', len);
665 if (p != u2.buf+5) abort ();
666 check (5, len, 'B');
669 /* off == 6 */
670 for (len = 0; len < MAX_COPY2; len++)
672 reset ();
674 p = memset (u2.buf+6, '\0', len);
675 if (p != u2.buf+6) abort ();
676 check (6, len, '\0');
678 p = memset (u2.buf+6, A, len);
679 if (p != u2.buf+6) abort ();
680 check (6, len, 'A');
682 p = memset (u2.buf+6, 'B', len);
683 if (p != u2.buf+6) abort ();
684 check (6, len, 'B');
687 /* off == 7 */
688 for (len = 0; len < MAX_COPY2; len++)
690 reset ();
692 p = memset (u2.buf+7, '\0', len);
693 if (p != u2.buf+7) abort ();
694 check (7, len, '\0');
696 p = memset (u2.buf+7, A, len);
697 if (p != u2.buf+7) abort ();
698 check (7, len, 'A');
700 p = memset (u2.buf+7, 'B', len);
701 if (p != u2.buf+7) abort ();
702 check (7, len, 'B');
706 void
707 main_test (void)
709 #ifndef __OPTIMIZE__
710 /* Object size checking is only intended for -O[s123]. */
711 return;
712 #endif
713 __asm ("" : "=r" (l1) : "0" (l1));
714 s4 = buffer;
715 test1 ();
716 test2 ();
717 test3 ();
718 test4 ();
719 test5 ();
720 test6 ();