xref: /llvm-project/compiler-rt/test/asan/TestCases/Posix/wait4.cpp (revision 349eab186986e56d3e314193733a0443f6575850)
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 // UNSUPPORTED: darwin
8 
9 #include <assert.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12 
13 int main(int argc, char **argv) {
14   pid_t pid = fork();
15   if (pid) { // parent
16     int x[3];
17     int *status = x + argc * 3;
18     int res;
19 #if defined(WAIT4)
20     res = wait4(pid, status, WNOHANG, NULL);
21 #elif defined(WAIT4_RUSAGE)
22     struct rusage *ru = (struct rusage*)(x + argc * 3);
23     int good_status;
24     res = wait4(pid, &good_status, WNOHANG, ru);
25 #endif
26     // CHECK: stack-buffer-overflow
27     // CHECK: {{WRITE of size .* at 0x.* thread T0}}
28     // CHECK: {{in .*wait}}
29     // CHECK: {{in main .*wait4.cpp:}}
30     // CHECK: is located in stack of thread T0 at offset
31     // CHECK: {{in main}}
32     return res == -1 ? 1 : 0;
33   }
34   // child
35   return 0;
36 }
37