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