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: target={{.*windows-msvc.*}}
5
6// Must not be implemented, no other reason to install interceptors.
7// XFAIL: ubsan
8
9#include <stdlib.h>
10#include <unistd.h>
11#include <sanitizer/allocator_interface.h>
12
13extern "C" {
14const volatile void *global_ptr;
15
16#define WRITE(s) write(1, s, sizeof(s))
17
18// Note: avoid calling functions that allocate memory in malloc/free
19// to avoid infinite recursion.
20void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz) {
21 if (__sanitizer_get_ownership(p: ptr) && sz == sizeof(int)) {
22 WRITE("MallocHook\n");
23 global_ptr = ptr;
24 }
25}
26void __sanitizer_free_hook(const volatile void *ptr) {
27 if (__sanitizer_get_ownership(p: ptr) && ptr == global_ptr)
28 WRITE("FreeHook\n");
29}
30} // extern "C"
31
32volatile int *x;
33
34void MallocHook1(const volatile void *ptr, size_t sz) { WRITE("MH1\n"); }
35void MallocHook2(const volatile void *ptr, size_t sz) { WRITE("MH2\n"); }
36void FreeHook1(const volatile void *ptr) { WRITE("FH1\n"); }
37void 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(size: sizeof(int)));
43}
44
45int main() {
46 __sanitizer_install_malloc_and_free_hooks(malloc_hook: MallocHook1, free_hook: FreeHook1);
47 __sanitizer_install_malloc_and_free_hooks(malloc_hook: MallocHook2, free_hook: FreeHook2);
48 int *undef1, *undef2;
49 allocate(unused1: undef1, unused2: undef2);
50 // CHECK: MallocHook
51 // CHECK: MH1
52 // CHECK: MH2
53 // Check that malloc hook was called with correct argument.
54 if (global_ptr != (void*)x) {
55 _exit(status: 1);
56 }
57
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(ptr: (int *)x, size: sizeof(int) * 128));
62 // CHECK-DAG: FreeHook{{[[:space:]].*}}FH1{{[[:space:]].*}}FH2
63 // CHECK-DAG: MH1{{[[:space:]].*}}MH2
64
65 x[0] = 0;
66 x[127] = -1;
67 free(ptr: (void *)x);
68 // CHECK: FH1
69 // CHECK: FH2
70 return 0;
71}
72

source code of compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp