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