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 
12 int main() {
13   // Disable stderr buffering. Needed on Windows.
14   setvbuf(stderr, NULL, _IONBF, 0);
15 
16   char *heap_ptr = (char *)malloc(10);
17   free(heap_ptr);
18   int present = __asan_report_present();
19   fprintf(stderr, "%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 void __asan_on_error() {
41   int present = __asan_report_present();
42   void *pc = __asan_get_report_pc();
43   void *bp = __asan_get_report_bp();
44   void *sp = __asan_get_report_sp();
45   void *addr = __asan_get_report_address();
46   int is_write = __asan_get_report_access_type();
47   size_t access_size = __asan_get_report_access_size();
48   const char *description = __asan_get_report_description();
49 
50   fprintf(stderr, "%s\n", (present == 1) ? "report" : "");
51   // CHECK: report
52   fprintf(stderr, "pc: " PTR_FMT "\n", pc);
53   // CHECK: pc: 0x[[PC:[0-9a-f]+]]
54   fprintf(stderr, "bp: " PTR_FMT "\n", bp);
55   // CHECK: bp: 0x[[BP:[0-9a-f]+]]
56   fprintf(stderr, "sp: " PTR_FMT "\n", sp);
57   // CHECK: sp: 0x[[SP:[0-9a-f]+]]
58   fprintf(stderr, "addr: " PTR_FMT "\n", addr);
59   // CHECK: addr: 0x[[ADDR:[0-9a-f]+]]
60   fprintf(stderr, "type: %s\n", (is_write ? "write" : "read"));
61   // CHECK: type: write
62   fprintf(stderr, "access_size: %ld\n", access_size);
63   // CHECK: access_size: 1
64   fprintf(stderr, "description: %s\n", description);
65   // CHECK: description: heap-use-after-free
66 }
67 
68 // CHECK: AddressSanitizer: heap-use-after-free on address {{0x0*}}[[ADDR]] at pc {{0x0*}}[[PC]] bp {{0x0*}}[[BP]] sp {{0x0*}}[[SP]]
69 // CHECK: WRITE of size 1 at {{0x0*}}[[ADDR]] thread T0
70