125fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov// Regression test for:
225fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov// http://code.google.com/p/address-sanitizer/issues/detail?id=37
325fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov
4a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m64 -O0 %s -o %t && %t | FileCheck %s
5a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m64 -O1 %s -o %t && %t | FileCheck %s
6a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m64 -O2 %s -o %t && %t | FileCheck %s
7a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m64 -O3 %s -o %t && %t | FileCheck %s
8a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m32 -O0 %s -o %t && %t | FileCheck %s
9a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m32 -O1 %s -o %t && %t | FileCheck %s
10a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m32 -O2 %s -o %t && %t | FileCheck %s
11a10d1dccd93a4aed127719e75a457987a2e8932cAlexey Samsonov// RUN: %clangxx_asan -m32 -O3 %s -o %t && %t | FileCheck %s
1225fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov
1325fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov#include <stdio.h>
1425fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov#include <sched.h>
1525fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov#include <sys/syscall.h>
1625fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov#include <sys/types.h>
1725fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov#include <sys/wait.h>
1825fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov#include <unistd.h>
1925fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov
2025fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonovint Child(void *arg) {
2125fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  char x[32] = {0};  // Stack gets poisoned.
2225fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  printf("Child:  %p\n", x);
2325fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  _exit(1);  // NoReturn, stack will remain unpoisoned unless we do something.
2425fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov}
2525fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov
2625fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonovint main(int argc, char **argv) {
2725fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  const int kStackSize = 1 << 20;
2825fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  char child_stack[kStackSize + 1];
2925fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  char *sp = child_stack + kStackSize;  // Stack grows down.
3025fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  printf("Parent: %p\n", sp);
3125fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov  pid_t clone_pid = clone(Child, sp, CLONE_FILES | CLONE_VM, NULL, 0, 0, 0);
3262825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov  int status;
3362825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov  pid_t wait_result = waitpid(clone_pid, &status, __WCLONE);
3462825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov  if (wait_result < 0) {
3562825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    perror("waitpid");
3662825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    return 0;
3762825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov  }
3862825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov  if (wait_result == clone_pid && WIFEXITED(status)) {
3962825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    // Make sure the child stack was indeed unpoisoned.
4062825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    for (int i = 0; i < kStackSize; i++)
4162825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov      child_stack[i] = i;
4262825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    int ret = child_stack[argc - 1];
4362825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    printf("PASSED\n");
4462825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    // CHECK: PASSED
4562825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov    return ret;
4662825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov  }
4762825675e6b4f3d8459f745fe68ae30f236f82a5Alexey Samsonov  return 0;
4825fa5e1f50d74a2499d73657535dc043d2f5607bAlexey Samsonov}
49