closed-fds.cc revision 7c9150579ed0278492f51cc8434b1d63a44b9bd1
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 2>&1 && ASAN_OPTIONS=log_path=%t.log:verbosity=2 not %run %t 2>&1
6// RUN: FileCheck %s --check-prefix=CHECK-FILE < %t.log.*
7
8#include <assert.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13
14int main(int argc, char **argv) {
15  int result = fprintf(stderr, "Closing streams.\n");
16  assert(result > 0);
17  close(STDIN_FILENO);
18  close(STDOUT_FILENO);
19  close(STDERR_FILENO);
20  result = fprintf(stderr, "Can you hear me now?\n");
21  assert(result < 0);
22  char *x = (char *)malloc(10 * sizeof(char));
23  free(x);
24  x[argc] = 'X';  // BOOM
25  // CHECK-FILE: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
26  // CHECK-FILE:   {{0x.* at pc 0x.* bp 0x.* sp 0x.*}}
27  // CHECK-FILE: {{WRITE of size 1 at 0x.* thread T0}}
28  // CHECK-FILE: {{    #0 0x.* in main .*closed-fds.cc:}}[[@LINE-4]]
29  return 0;
30}
31