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 | |
27 | int main() { |
28 | // Disable stderr buffering. Needed on Windows. |
29 | setvbuf(stderr, NULL, _IONBF, n: 0); |
30 | |
31 | heap_ptr = (char *)malloc(size: 10); |
32 | fprintf(stderr, format: "heap_ptr: " PTR_FMT "\n" , heap_ptr); |
33 | // CHECK: heap_ptr: 0x[[ADDR:[0-9a-f]+]] |
34 | |
35 | free(ptr: heap_ptr); |
36 | free(ptr: heap_ptr); // BOOM |
37 | return 0; |
38 | } |
39 | |
40 | // Required for dyld macOS 12.0+ |
41 | #if (__APPLE__) |
42 | __attribute__((weak)) |
43 | #endif |
44 | extern "C" void |
45 | __asan_on_error() { |
46 | int present = __asan_report_present(); |
47 | void *addr = __asan_get_report_address(); |
48 | const char *description = __asan_get_report_description(); |
49 | |
50 | fprintf(stderr, format: "%s\n" , (present == 1) ? "report present" : "" ); |
51 | // CHECK: report present |
52 | fprintf(stderr, format: "addr: " PTR_FMT "\n" , addr); |
53 | // CHECK: addr: {{0x0*}}[[ADDR]] |
54 | fprintf(stderr, format: "description: %s\n" , description); |
55 | // CHECK: description: double-free |
56 | } |
57 | |
58 | // CHECK: AddressSanitizer: attempting double-free on {{0x0*}}[[ADDR]] in thread T0 |
59 | |