clone_test.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
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
9#include <stdio.h>
10#include <sched.h>
11#include <sys/syscall.h>
12#include <sys/types.h>
13#include <sys/wait.h>
14#include <unistd.h>
15
16int Child(void *arg) {
17  char x[32] = {0};  // Stack gets poisoned.
18  printf("Child:  %p\n", x);
19  _exit(1);  // NoReturn, stack will remain unpoisoned unless we do something.
20}
21
22int main(int argc, char **argv) {
23  const int kStackSize = 1 << 20;
24  char child_stack[kStackSize + 1];
25  char *sp = child_stack + kStackSize;  // Stack grows down.
26  printf("Parent: %p\n", sp);
27  pid_t clone_pid = clone(Child, sp, CLONE_FILES | CLONE_VM, NULL);
28  int status;
29  pid_t wait_result = waitpid(clone_pid, &status, __WCLONE);
30  if (wait_result < 0) {
31    perror("waitpid");
32    return 0;
33  }
34  if (wait_result == clone_pid && WIFEXITED(status)) {
35    // Make sure the child stack was indeed unpoisoned.
36    for (int i = 0; i < kStackSize; i++)
37      child_stack[i] = i;
38    int ret = child_stack[argc - 1];
39    printf("PASSED\n");
40    // CHECK: PASSED
41    return ret;
42  }
43  return 0;
44}
45