- Porting of address santizer patch for ARM in gcc-4.8.1 from gcc-4.9 - Taken from...
[platform/upstream/gcc48.git] / gcc / testsuite / c-c++-common / asan / clone-test-1.c
1 /* Regression test for:
2    http://code.google.com/p/address-sanitizer/issues/detail?id=37 */
3
4 /* { dg-do run { target { *-*-linux* } } } */
5 /* { dg-require-effective-target clone } */
6 /* { dg-require-effective-target hw } */
7 /* { dg-options "-D_GNU_SOURCE" } */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sched.h>
12 #include <sys/syscall.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
16
17 int Child(void *arg) {
18   char x[32] = {0};  /* Stack gets poisoned. */
19   printf("Child:  %p\n", x);
20   _exit(1);  /* NoReturn, stack will remain unpoisoned unless we do something. */
21 }
22
23 volatile int zero = 0;
24
25 int main(int argc, char **argv) {
26   int i;
27   const int kStackSize = 1 << 20;
28   char child_stack[kStackSize + 1];
29   char *sp = child_stack + kStackSize;  /* Stack grows down. */
30   printf("Parent: %p\n", sp);
31   pid_t clone_pid = clone(Child, sp, CLONE_FILES | CLONE_VM, NULL, 0, 0, 0);
32   int status;
33   pid_t wait_result = waitpid(clone_pid, &status, __WCLONE);
34   if (wait_result < 0) {
35     perror("waitpid");
36     return 1;
37   }
38   if (wait_result == clone_pid && WIFEXITED(status)) {
39     /* Make sure the child stack was indeed unpoisoned. */
40     for (i = 0; i < kStackSize; i++)
41       child_stack[i] = i;
42     int ret = child_stack[zero];
43     return ret;
44   }
45   return 1;
46 }