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 // If we use %p with MS CRTs, it comes out all upper case. Use %08x to get 8 // lowercase hex. 9 #ifdef _WIN32 10 # ifdef _WIN64 11 # define PTR_FMT "0x%08llx" 12 # else 13 # define PTR_FMT "0x%08x" 14 # endif 15 // Solaris libc omits the leading 0x. 16 #elif defined(__sun__) && defined(__svr4__) 17 # define PTR_FMT "0x%p" 18 #else 19 # define PTR_FMT "%p" 20 #endif 21 22 char *heap_ptr; 23 24 int main() { 25 // Disable stderr buffering. Needed on Windows. 26 setvbuf(stderr, NULL, _IONBF, 0); 27 28 heap_ptr = (char *)malloc(10); 29 fprintf(stderr, "heap_ptr: " PTR_FMT "\n", heap_ptr); 30 // CHECK: heap_ptr: 0x[[ADDR:[0-9a-f]+]] 31 32 free(heap_ptr); 33 free(heap_ptr); // BOOM 34 return 0; 35 } 36 37 // Required for dyld macOS 12.0+ 38 #if (__APPLE__) 39 __attribute__((weak)) 40 #endif 41 extern "C" void 42 __asan_on_error() { 43 int present = __asan_report_present(); 44 void *addr = __asan_get_report_address(); 45 const char *description = __asan_get_report_description(); 46 47 fprintf(stderr, "%s\n", (present == 1) ? "report present" : ""); 48 // CHECK: report present 49 fprintf(stderr, "addr: " PTR_FMT "\n", addr); 50 // CHECK: addr: {{0x0*}}[[ADDR]] 51 fprintf(stderr, "description: %s\n", description); 52 // CHECK: description: double-free 53 } 54 55 // CHECK: AddressSanitizer: attempting double-free on {{0x0*}}[[ADDR]] in thread T0 56