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

source code of compiler-rt/test/asan/TestCases/Posix/closed-fds.cpp