1 // Check that MemProf correctly handles malloc and free hooks.
2 // RUN: %clangxx_memprof -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
4 #include <sanitizer/allocator_interface.h>
9 const volatile void *global_ptr
;
11 #define WRITE(s) write(1, s, sizeof(s))
13 // Note: avoid calling functions that allocate memory in malloc/free
14 // to avoid infinite recursion.
15 void __sanitizer_malloc_hook(const volatile void *ptr
, size_t sz
) {
16 if (__sanitizer_get_ownership(ptr
) && sz
== 4) {
17 WRITE("MallocHook\n");
21 void __sanitizer_free_hook(const volatile void *ptr
) {
22 if (__sanitizer_get_ownership(ptr
) && ptr
== global_ptr
)
29 void MallocHook1(const volatile void *ptr
, size_t sz
) { WRITE("MH1\n"); }
30 void MallocHook2(const volatile void *ptr
, size_t sz
) { WRITE("MH2\n"); }
31 void FreeHook1(const volatile void *ptr
) { WRITE("FH1\n"); }
32 void FreeHook2(const volatile void *ptr
) { WRITE("FH2\n"); }
33 // Call this function with uninitialized arguments to poison
34 // TLS shadow for function parameters before calling operator
35 // new and, eventually, user-provided hook.
36 __attribute__((noinline
)) void allocate(int *unused1
, int *unused2
) {
41 __sanitizer_install_malloc_and_free_hooks(MallocHook1
, FreeHook1
);
42 __sanitizer_install_malloc_and_free_hooks(MallocHook2
, FreeHook2
);
44 allocate(undef1
, undef2
);
48 // Check that malloc hook was called with correct argument.
49 if (global_ptr
!= (void *)x
) {