xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/Posix/closed-fds.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Check that when the program closed its std(in|out|err), running the external
2*7c3d14c8STreehugger Robot // symbolizer still works.
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // RUN: rm -f %t.log.*
5*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O0 %s -o %t 2>&1 && %env_asan_opts=log_path='"%t.log"':verbosity=2 not %run %t 2>&1
6*7c3d14c8STreehugger Robot // RUN: FileCheck %s --check-prefix=CHECK-FILE < %t.log.*
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot // FIXME: copy %t.log back from the device and re-enable on Android.
9*7c3d14c8STreehugger Robot // UNSUPPORTED: android
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot #include <assert.h>
12*7c3d14c8STreehugger Robot #include <stdio.h>
13*7c3d14c8STreehugger Robot #include <stdlib.h>
14*7c3d14c8STreehugger Robot #include <string.h>
15*7c3d14c8STreehugger Robot #include <unistd.h>
16*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)17*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
18*7c3d14c8STreehugger Robot   int result = fprintf(stderr, "Closing streams.\n");
19*7c3d14c8STreehugger Robot   assert(result > 0);
20*7c3d14c8STreehugger Robot   close(STDIN_FILENO);
21*7c3d14c8STreehugger Robot   close(STDOUT_FILENO);
22*7c3d14c8STreehugger Robot   close(STDERR_FILENO);
23*7c3d14c8STreehugger Robot   result = fprintf(stderr, "Can you hear me now?\n");
24*7c3d14c8STreehugger Robot   assert(result < 0);
25*7c3d14c8STreehugger Robot   char *x = (char *)malloc(10 * sizeof(char));
26*7c3d14c8STreehugger Robot   free(x);
27*7c3d14c8STreehugger Robot   x[argc] = 'X';  // BOOM
28*7c3d14c8STreehugger Robot   // CHECK-FILE: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
29*7c3d14c8STreehugger Robot   // CHECK-FILE:   {{0x.* at pc 0x.* bp 0x.* sp 0x.*}}
30*7c3d14c8STreehugger Robot   // CHECK-FILE: {{WRITE of size 1 at 0x.* thread T0}}
31*7c3d14c8STreehugger Robot   // CHECK-FILE: {{    #0 0x.* in main .*closed-fds.cc:}}[[@LINE-4]]
32*7c3d14c8STreehugger Robot   return 0;
33*7c3d14c8STreehugger Robot }
34