1// RUN: %clangxx_asan -DWAIT4 -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2// RUN: %clangxx_asan -DWAIT4 -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
3
4// RUN: %clangxx_asan -DWAIT4_RUSAGE -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
5// RUN: %clangxx_asan -DWAIT4_RUSAGE -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
6
7// XFAIL: android
8
9#include <assert.h>
10#include <sys/wait.h>
11#include <unistd.h>
12
13int main(int argc, char **argv) {
14  // This test passes on some versions of Android NDK and fails on other.
15  // https://code.google.com/p/memory-sanitizer/issues/detail?id=64
16  // Make it fail unconditionally on Android.
17#ifdef __ANDROID__
18  return 0;
19#endif
20
21  pid_t pid = fork();
22  if (pid) { // parent
23    int x[3];
24    int *status = x + argc * 3;
25    int res;
26#if defined(WAIT4)
27    res = wait4(pid, status, WNOHANG, NULL);
28#elif defined(WAIT4_RUSAGE)
29    struct rusage *ru = (struct rusage*)(x + argc * 3);
30    int good_status;
31    res = wait4(pid, &good_status, WNOHANG, ru);
32#endif
33    // CHECK: stack-buffer-overflow
34    // CHECK: {{WRITE of size .* at 0x.* thread T0}}
35    // CHECK: {{in .*wait}}
36    // CHECK: {{in main .*wait4.cc:}}
37    // CHECK: is located in stack of thread T0 at offset
38    // CHECK: {{in main}}
39    return res == -1 ? 1 : 0;
40  }
41  // child
42  return 0;
43}
44