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