1// Checks that the ASan debugging API for getting report information
2// returns correct values.
3// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
4
5#include <sanitizer/asan_interface.h>
6#include <stdio.h>
7#include <stdlib.h>
8
9int main() {
10  // Disable stderr buffering. Needed on Windows.
11  setvbuf(stderr, NULL, _IONBF, 0);
12
13  char *heap_ptr = (char *)malloc(10);
14  free(heap_ptr);
15  int present = __asan_report_present();
16  fprintf(stderr, "%s\n", (present == 0) ? "no report" : "");
17  // CHECK: no report
18  heap_ptr[0] = 'A'; // BOOM
19  return 0;
20}
21
22// If we use %p with MSVC, it comes out all upper case. Use %08x to get
23// lowercase hex.
24#ifdef _MSC_VER
25# ifdef _WIN64
26#  define PTR_FMT "0x%08llx"
27# else
28#  define PTR_FMT "0x%08x"
29# endif
30#else
31# define PTR_FMT "%p"
32#endif
33
34void __asan_on_error() {
35  int present = __asan_report_present();
36  void *pc = __asan_get_report_pc();
37  void *bp = __asan_get_report_bp();
38  void *sp = __asan_get_report_sp();
39  void *addr = __asan_get_report_address();
40  int is_write = __asan_get_report_access_type();
41  size_t access_size = __asan_get_report_access_size();
42  const char *description = __asan_get_report_description();
43
44  fprintf(stderr, "%s\n", (present == 1) ? "report" : "");
45  // CHECK: report
46  fprintf(stderr, "pc: " PTR_FMT "\n", pc);
47  // CHECK: pc: 0x[[PC:[0-9a-f]+]]
48  fprintf(stderr, "bp: " PTR_FMT "\n", bp);
49  // CHECK: bp: 0x[[BP:[0-9a-f]+]]
50  fprintf(stderr, "sp: " PTR_FMT "\n", sp);
51  // CHECK: sp: 0x[[SP:[0-9a-f]+]]
52  fprintf(stderr, "addr: " PTR_FMT "\n", addr);
53  // CHECK: addr: 0x[[ADDR:[0-9a-f]+]]
54  fprintf(stderr, "type: %s\n", (is_write ? "write" : "read"));
55  // CHECK: type: write
56  fprintf(stderr, "access_size: %ld\n", access_size);
57  // CHECK: access_size: 1
58  fprintf(stderr, "description: %s\n", description);
59  // CHECK: description: heap-use-after-free
60}
61
62// CHECK: AddressSanitizer: heap-use-after-free on address {{0x0*}}[[ADDR]] at pc {{0x0*}}[[PC]] bp {{0x0*}}[[BP]] sp {{0x0*}}[[SP]]
63// CHECK: WRITE of size 1 at {{0x0*}}[[ADDR]] thread T0
64