1 // RUN: %clangxx -O2 %s -o %t && %run %t 2>&1 | FileCheck %s 2 3 // Malloc/free hooks are not supported on Windows. 4 // XFAIL: windows-msvc 5 // XFAIL: ubsan 6 7 #include <stdlib.h> 8 #include <unistd.h> 9 #include <sanitizer/allocator_interface.h> 10 11 extern "C" { 12 const volatile void *global_ptr; 13 14 #define WRITE(s) write(1, s, sizeof(s)) 15 16 // Note: avoid calling functions that allocate memory in malloc/free 17 // to avoid infinite recursion. 18 void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz) { 19 if (__sanitizer_get_ownership(ptr) && sz == 4) { 20 WRITE("MallocHook\n"); 21 global_ptr = ptr; 22 } 23 } 24 void __sanitizer_free_hook(const volatile void *ptr) { 25 if (__sanitizer_get_ownership(ptr) && ptr == global_ptr) 26 WRITE("FreeHook\n"); 27 } 28 } // extern "C" 29 30 volatile int *x; 31 32 void MallocHook1(const volatile void *ptr, size_t sz) { WRITE("MH1\n"); } 33 void MallocHook2(const volatile void *ptr, size_t sz) { WRITE("MH2\n"); } 34 void FreeHook1(const volatile void *ptr) { WRITE("FH1\n"); } 35 void FreeHook2(const volatile void *ptr) { WRITE("FH2\n"); } 36 // Call this function with uninitialized arguments to poison 37 // TLS shadow for function parameters before calling operator 38 // new and, eventually, user-provided hook. 39 __attribute__((noinline)) void allocate(int *unused1, int *unused2) { 40 x = new int; 41 } 42 43 int main() { 44 __sanitizer_install_malloc_and_free_hooks(MallocHook1, FreeHook1); 45 __sanitizer_install_malloc_and_free_hooks(MallocHook2, FreeHook2); 46 int *undef1, *undef2; 47 allocate(undef1, undef2); 48 // CHECK: MallocHook 49 // CHECK: MH1 50 // CHECK: MH2 51 // Check that malloc hook was called with correct argument. 52 if (global_ptr != (void*)x) { 53 _exit(1); 54 } 55 *x = 0; 56 delete x; 57 // CHECK: FreeHook 58 // CHECK: FH1 59 // CHECK: FH2 60 return 0; 61 } 62