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
9// FIXME: Doesn't work with DLLs
10// XFAIL: win32-dynamic-asan
11
12int main() {
13 // Disable stderr buffering. Needed on Windows.
14 setvbuf(stderr, NULL, _IONBF, n: 0);
15
16 char *heap_ptr = (char *)malloc(size: 10);
17 free(ptr: heap_ptr);
18 int present = __asan_report_present();
19 fprintf(stderr, format: "%s\n", (present == 0) ? "no report" : "");
20 // CHECK: no report
21 heap_ptr[0] = 'A'; // BOOM
22 return 0;
23}
24
25// If we use %p with MSVC, it comes out all upper case. Use %08x to get
26// lowercase hex.
27#ifdef _MSC_VER
28# ifdef _WIN64
29# define PTR_FMT "0x%08llx"
30# else
31# define PTR_FMT "0x%08x"
32# endif
33// Solaris libc omits the leading 0x.
34#elif defined(__sun__) && defined(__svr4__)
35# define PTR_FMT "0x%p"
36#else
37# define PTR_FMT "%p"
38#endif
39
40// Required for dyld macOS 12.0+
41#if (__APPLE__)
42__attribute__((weak))
43#endif
44extern "C" void
45__asan_on_error() {
46 int present = __asan_report_present();
47 void *pc = __asan_get_report_pc();
48 void *bp = __asan_get_report_bp();
49 void *sp = __asan_get_report_sp();
50 void *addr = __asan_get_report_address();
51 int is_write = __asan_get_report_access_type();
52 size_t access_size = __asan_get_report_access_size();
53 const char *description = __asan_get_report_description();
54
55 fprintf(stderr, format: "%s\n", (present == 1) ? "report" : "");
56 // CHECK: report
57 fprintf(stderr, format: "pc: " PTR_FMT "\n", pc);
58 // CHECK: pc: 0x[[PC:[0-9a-f]+]]
59 fprintf(stderr, format: "bp: " PTR_FMT "\n", bp);
60 // CHECK: bp: 0x[[BP:[0-9a-f]+]]
61 fprintf(stderr, format: "sp: " PTR_FMT "\n", sp);
62 // CHECK: sp: 0x[[SP:[0-9a-f]+]]
63 fprintf(stderr, format: "addr: " PTR_FMT "\n", addr);
64 // CHECK: addr: 0x[[ADDR:[0-9a-f]+]]
65 fprintf(stderr, format: "type: %s\n", (is_write ? "write" : "read"));
66 // CHECK: type: write
67 fprintf(stderr, format: "access_size: %ld\n", access_size);
68 // CHECK: access_size: 1
69 fprintf(stderr, format: "description: %s\n", description);
70 // CHECK: description: heap-use-after-free
71}
72
73// CHECK: AddressSanitizer: heap-use-after-free on address {{0x0*}}[[ADDR]] at pc {{0x0*}}[[PC]] bp {{0x0*}}[[BP]] sp {{0x0*}}[[SP]]
74// CHECK: WRITE of size 1 at {{0x0*}}[[ADDR]] thread T0
75

source code of compiler-rt/test/asan/TestCases/debug_report.cpp