Home | History | Annotate | Download | only in Linux
      1 // Regression test for:
      2 // http://code.google.com/p/address-sanitizer/issues/detail?id=37
      3 
      4 // RUN: %clangxx_asan -m64 -O0 %s -o %t && %t | FileCheck %s
      5 // RUN: %clangxx_asan -m64 -O1 %s -o %t && %t | FileCheck %s
      6 // RUN: %clangxx_asan -m64 -O2 %s -o %t && %t | FileCheck %s
      7 // RUN: %clangxx_asan -m64 -O3 %s -o %t && %t | FileCheck %s
      8 // RUN: %clangxx_asan -m32 -O0 %s -o %t && %t | FileCheck %s
      9 // RUN: %clangxx_asan -m32 -O1 %s -o %t && %t | FileCheck %s
     10 // RUN: %clangxx_asan -m32 -O2 %s -o %t && %t | FileCheck %s
     11 // RUN: %clangxx_asan -m32 -O3 %s -o %t && %t | FileCheck %s
     12 
     13 #include <stdio.h>
     14 #include <sched.h>
     15 #include <sys/syscall.h>
     16 #include <sys/types.h>
     17 #include <sys/wait.h>
     18 #include <unistd.h>
     19 
     20 int Child(void *arg) {
     21   char x[32] = {0};  // Stack gets poisoned.
     22   printf("Child:  %p\n", x);
     23   _exit(1);  // NoReturn, stack will remain unpoisoned unless we do something.
     24 }
     25 
     26 int main(int argc, char **argv) {
     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 0;
     37   }
     38   if (wait_result == clone_pid && WIFEXITED(status)) {
     39     // Make sure the child stack was indeed unpoisoned.
     40     for (int i = 0; i < kStackSize; i++)
     41       child_stack[i] = i;
     42     int ret = child_stack[argc - 1];
     43     printf("PASSED\n");
     44     // CHECK: PASSED
     45     return ret;
     46   }
     47   return 0;
     48 }
     49