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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of compiler-rt/test/asan/TestCases/debug_double_free.cpp