56 lines · cpp
1// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s2 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 get8// lowercase hex.9#ifdef _WIN3210# ifdef _WIN6411# define PTR_FMT "0x%08llx"12# else13# define PTR_FMT "0x%08x"14# endif15// Solaris libc omits the leading 0x.16#elif defined(__sun__) && defined(__svr4__)17# define PTR_FMT "0x%p"18#else19# define PTR_FMT "%p"20#endif21 22char *heap_ptr;23 24int 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); // BOOM34 return 0;35}36 37// Required for dyld macOS 12.0+38#if (__APPLE__)39__attribute__((weak))40#endif41extern "C" void42__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 present49 fprintf(stderr, "addr: " PTR_FMT "\n", addr);50 // CHECK: addr: {{0x0*}}[[ADDR]]51 fprintf(stderr, "description: %s\n", description);52 // CHECK: description: double-free53}54 55// CHECK: AddressSanitizer: attempting double-free on {{0x0*}}[[ADDR]] in thread T056