1 // RUN: %clangxx -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
3 // Malloc/free hooks are not supported on Windows.
4 // XFAIL: target={{.*windows-msvc.*}}
6 // Must not be implemented, no other reason to install interceptors.
11 #include <sanitizer/allocator_interface.h>
14 const volatile void *global_ptr
;
16 #define WRITE(s) write(1, s, sizeof(s))
18 // Note: avoid calling functions that allocate memory in malloc/free
19 // to avoid infinite recursion.
20 void __sanitizer_malloc_hook(const volatile void *ptr
, size_t sz
) {
21 if (__sanitizer_get_ownership(ptr
) && sz
== sizeof(int)) {
22 WRITE("MallocHook\n");
26 void __sanitizer_free_hook(const volatile void *ptr
) {
27 if (__sanitizer_get_ownership(ptr
) && ptr
== global_ptr
)
34 void MallocHook1(const volatile void *ptr
, size_t sz
) { WRITE("MH1\n"); }
35 void MallocHook2(const volatile void *ptr
, size_t sz
) { WRITE("MH2\n"); }
36 void FreeHook1(const volatile void *ptr
) { WRITE("FH1\n"); }
37 void FreeHook2(const volatile void *ptr
) { WRITE("FH2\n"); }
38 // Call this function with uninitialized arguments to poison
39 // TLS shadow for function parameters before calling operator
40 // new and, eventually, user-provided hook.
41 __attribute__((noinline
)) void allocate(int *unused1
, int *unused2
) {
42 x
= reinterpret_cast<int *>(malloc(sizeof(int)));
46 __sanitizer_install_malloc_and_free_hooks(MallocHook1
, FreeHook1
);
47 __sanitizer_install_malloc_and_free_hooks(MallocHook2
, FreeHook2
);
49 allocate(undef1
, undef2
);
53 // Check that malloc hook was called with correct argument.
54 if (global_ptr
!= (void*)x
) {
58 // Check that realloc invokes hooks
59 // We realloc to 128 here to avoid potential oversizing by allocators
60 // making this a no-op.
61 x
= reinterpret_cast<int *>(realloc((int *)x
, sizeof(int) * 128));
62 // CHECK-DAG: FreeHook{{[[:space:]].*}}FH1{{[[:space:]].*}}FH2
63 // CHECK-DAG: MH1{{[[:space:]].*}}MH2