1 // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2 
3 #include <sanitizer/asan_interface.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 // FIXME: Doesn't work with DLLs
8 // XFAIL: win32-dynamic-asan
9 
10 // If we use %p with MSVC, it comes out all upper case. Use %08x to get
11 // lowercase hex.
12 #ifdef _MSC_VER
13 # ifdef _WIN64
14 #  define PTR_FMT "0x%08llx"
15 # else
16 #  define PTR_FMT "0x%08x"
17 # endif
18 // Solaris libc omits the leading 0x.
19 #elif defined(__sun__) && defined(__svr4__)
20 # define PTR_FMT "0x%p"
21 #else
22 # define PTR_FMT "%p"
23 #endif
24 
25 char *heap_ptr;
26 
main()27 int main() {
28   // Disable stderr buffering. Needed on Windows.
29   setvbuf(stderr, NULL, _IONBF, 0);
30 
31   heap_ptr = (char *)malloc(10);
32   fprintf(stderr, "heap_ptr: " PTR_FMT "\n", heap_ptr);
33   // CHECK: heap_ptr: 0x[[ADDR:[0-9a-f]+]]
34 
35   free(heap_ptr);
36   free(heap_ptr);  // BOOM
37   return 0;
38 }
39 
__asan_on_error()40 void __asan_on_error() {
41   int present = __asan_report_present();
42   void *addr = __asan_get_report_address();
43   const char *description = __asan_get_report_description();
44 
45   fprintf(stderr, "%s\n", (present == 1) ? "report present" : "");
46   // CHECK: report present
47   fprintf(stderr, "addr: " PTR_FMT "\n", addr);
48   // CHECK: addr: {{0x0*}}[[ADDR]]
49   fprintf(stderr, "description: %s\n", description);
50   // CHECK: description: double-free
51 }
52 
53 // CHECK: AddressSanitizer: attempting double-free on {{0x0*}}[[ADDR]] in thread T0
54