File: closed-fds.cc

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (35 lines) | stat: -rw-r--r-- 1,119 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Check that when the program closed its std(in|out|err), running the external
// symbolizer still works.

// RUN: rm -f %t.log.*
// RUN: %clangxx_asan -O0 %s -o %t
// RUN: %env_asan_opts=log_path='"%t.log"':verbosity=2 not %run %t
// RUN: FileCheck %s --check-prefix=CHECK-FILE < %t.log.*

// FIXME: copy %t.log back from the device and re-enable on Android.
// UNSUPPORTED: android
// UNSUPPORTED: ios

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
  int result = fprintf(stderr, "Closing streams.\n");
  assert(result > 0);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  result = fprintf(stderr, "Can you hear me now?\n");
  assert(result < 0);
  char *x = (char *)malloc(10 * sizeof(char));
  free(x);
  x[argc] = 'X';  // BOOM
  // CHECK-FILE: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
  // CHECK-FILE:   {{0x.* at pc 0x.* bp 0x.* sp 0x.*}}
  // CHECK-FILE: {{WRITE of size 1 at 0x.* thread T0}}
  // CHECK-FILE: {{    #0 0x.* in main .*closed-fds.cc:}}[[@LINE-4]]
  return 0;
}