1 // Regression test for:
2 // http://code.google.com/p/address-sanitizer/issues/detail?id=37
3 
4 // RUN: %clangxx_asan -O0 %s -o %t && %run %t | FileCheck %s
5 // RUN: %clangxx_asan -O1 %s -o %t && %run %t | FileCheck %s
6 // RUN: %clangxx_asan -O2 %s -o %t && %run %t | FileCheck %s
7 // RUN: %clangxx_asan -O3 %s -o %t && %run %t | FileCheck %s
8 // XFAIL: arm-linux-gnueabi
9 
10 #include <stdio.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 
Child(void * arg)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 
main(int argc,char ** argv)23 int main(int argc, char **argv) {
24   const int kStackSize = 1 << 20;
25   char __attribute__((aligned(16))) child_stack[kStackSize + 1];
26   char *sp = child_stack + kStackSize;  // Stack grows down.
27   printf("Parent: %p\n", sp);
28   pid_t clone_pid = clone(Child, sp, CLONE_FILES | CLONE_VM, NULL);
29   int status;
30   pid_t wait_result = waitpid(clone_pid, &status, __WCLONE);
31   if (wait_result < 0) {
32     perror("waitpid");
33     return 0;
34   }
35   if (wait_result == clone_pid && WIFEXITED(status)) {
36     // Make sure the child stack was indeed unpoisoned.
37     for (int i = 0; i < kStackSize; i++)
38       child_stack[i] = i;
39     int ret = child_stack[argc - 1];
40     printf("PASSED\n");
41     // CHECK: PASSED
42     return ret;
43   }
44   return 0;
45 }
46