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(format: "Child: %p\n", x);
19 _exit(status: 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 __attribute__((aligned(16))) child_stack[kStackSize + 1];
25 char *sp = child_stack + kStackSize; // Stack grows down.
26 printf(format: "Parent: %p\n", sp);
27 pid_t clone_pid = clone(fn: Child, child_stack: sp, CLONE_FILES | CLONE_VM, NULL);
28 int status;
29 pid_t wait_result = waitpid(pid: clone_pid, stat_loc: &status, __WCLONE);
30 if (wait_result < 0) {
31 perror(s: "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(format: "PASSED\n");
40 // CHECK: PASSED
41 return ret;
42 }
43 return 0;
44}
45

source code of compiler-rt/test/asan/TestCases/Linux/clone_test.cpp