| 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 | int main() { |
| 10 | // Disable stderr buffering. Needed on Windows. |
| 11 | setvbuf(stderr, NULL, _IONBF, n: 0); |
| 12 | |
| 13 | char *heap_ptr = (char *)malloc(size: 10); |
| 14 | free(ptr: heap_ptr); |
| 15 | int present = __asan_report_present(); |
| 16 | fprintf(stderr, format: "%s\n" , (present == 0) ? "no report" : "" ); |
| 17 | // CHECK: no report |
| 18 | heap_ptr[0] = 'A'; // BOOM |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | // If we use %p with MS CRTs, it comes out all upper case. Use %08x to get |
| 23 | // lowercase hex. |
| 24 | #ifdef _WIN32 |
| 25 | # ifdef _WIN64 |
| 26 | # define PTR_FMT "0x%08llx" |
| 27 | # else |
| 28 | # define PTR_FMT "0x%08x" |
| 29 | # endif |
| 30 | // Solaris libc omits the leading 0x. |
| 31 | #elif defined(__sun__) && defined(__svr4__) |
| 32 | # define PTR_FMT "0x%p" |
| 33 | #else |
| 34 | # define PTR_FMT "%p" |
| 35 | #endif |
| 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 *pc = __asan_get_report_pc(); |
| 45 | void *bp = __asan_get_report_bp(); |
| 46 | void *sp = __asan_get_report_sp(); |
| 47 | void *addr = __asan_get_report_address(); |
| 48 | int is_write = __asan_get_report_access_type(); |
| 49 | size_t access_size = __asan_get_report_access_size(); |
| 50 | const char *description = __asan_get_report_description(); |
| 51 | |
| 52 | fprintf(stderr, format: "%s\n" , (present == 1) ? "report" : "" ); |
| 53 | // CHECK: report |
| 54 | fprintf(stderr, format: "pc: " PTR_FMT "\n" , pc); |
| 55 | // CHECK: pc: 0x[[PC:[0-9a-f]+]] |
| 56 | fprintf(stderr, format: "bp: " PTR_FMT "\n" , bp); |
| 57 | // CHECK: bp: 0x[[BP:[0-9a-f]+]] |
| 58 | fprintf(stderr, format: "sp: " PTR_FMT "\n" , sp); |
| 59 | // CHECK: sp: 0x[[SP:[0-9a-f]+]] |
| 60 | fprintf(stderr, format: "addr: " PTR_FMT "\n" , addr); |
| 61 | // CHECK: addr: 0x[[ADDR:[0-9a-f]+]] |
| 62 | fprintf(stderr, format: "type: %s\n" , (is_write ? "write" : "read" )); |
| 63 | // CHECK: type: write |
| 64 | fprintf(stderr, format: "access_size: %ld\n" , access_size); |
| 65 | // CHECK: access_size: 1 |
| 66 | fprintf(stderr, format: "description: %s\n" , description); |
| 67 | // CHECK: description: heap-use-after-free |
| 68 | } |
| 69 | |
| 70 | // CHECK: AddressSanitizer: heap-use-after-free on address {{0x0*}}[[ADDR]] at pc {{0x0*}}[[PC]] bp {{0x0*}}[[BP]] sp {{0x0*}}[[SP]] |
| 71 | // CHECK: WRITE of size 1 at {{0x0*}}[[ADDR]] thread T0 |
| 72 | |