xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp (revision 3ab36712da6e43588dc15c8672949fa12fe5f74f)
174989affSNico Weber // RUN: %clangxx -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
274989affSNico Weber 
374989affSNico Weber // Malloc/free hooks are not supported on Windows.
4975fa725SPaul Robinson // XFAIL: target={{.*windows-msvc.*}}
569e01fa1SVitaly Buka 
669e01fa1SVitaly Buka // Must not be implemented, no other reason to install interceptors.
774989affSNico Weber // XFAIL: ubsan
874989affSNico Weber 
974989affSNico Weber #include <stdlib.h>
1074989affSNico Weber #include <unistd.h>
1174989affSNico Weber #include <sanitizer/allocator_interface.h>
1274989affSNico Weber 
1374989affSNico Weber extern "C" {
1474989affSNico Weber const volatile void *global_ptr;
1574989affSNico Weber 
1674989affSNico Weber #define WRITE(s) write(1, s, sizeof(s))
1774989affSNico Weber 
1874989affSNico Weber // Note: avoid calling functions that allocate memory in malloc/free
1974989affSNico Weber // to avoid infinite recursion.
__sanitizer_malloc_hook(const volatile void * ptr,size_t sz)2074989affSNico Weber void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz) {
21*90418dc9SJin Xin Ng   if (__sanitizer_get_ownership(ptr) && sz == sizeof(int)) {
2274989affSNico Weber     WRITE("MallocHook\n");
2374989affSNico Weber     global_ptr = ptr;
2474989affSNico Weber   }
2574989affSNico Weber }
__sanitizer_free_hook(const volatile void * ptr)2674989affSNico Weber void __sanitizer_free_hook(const volatile void *ptr) {
2774989affSNico Weber   if (__sanitizer_get_ownership(ptr) && ptr == global_ptr)
2874989affSNico Weber     WRITE("FreeHook\n");
2974989affSNico Weber }
3074989affSNico Weber }  // extern "C"
3174989affSNico Weber 
3274989affSNico Weber volatile int *x;
3374989affSNico Weber 
MallocHook1(const volatile void * ptr,size_t sz)3474989affSNico Weber void MallocHook1(const volatile void *ptr, size_t sz) { WRITE("MH1\n"); }
MallocHook2(const volatile void * ptr,size_t sz)3574989affSNico Weber void MallocHook2(const volatile void *ptr, size_t sz) { WRITE("MH2\n"); }
FreeHook1(const volatile void * ptr)3674989affSNico Weber void FreeHook1(const volatile void *ptr) { WRITE("FH1\n"); }
FreeHook2(const volatile void * ptr)3774989affSNico Weber void FreeHook2(const volatile void *ptr) { WRITE("FH2\n"); }
3874989affSNico Weber // Call this function with uninitialized arguments to poison
3974989affSNico Weber // TLS shadow for function parameters before calling operator
4074989affSNico Weber // new and, eventually, user-provided hook.
allocate(int * unused1,int * unused2)4174989affSNico Weber __attribute__((noinline)) void allocate(int *unused1, int *unused2) {
42*90418dc9SJin Xin Ng   x = reinterpret_cast<int *>(malloc(sizeof(int)));
4374989affSNico Weber }
4474989affSNico Weber 
main()4574989affSNico Weber int main() {
4674989affSNico Weber   __sanitizer_install_malloc_and_free_hooks(MallocHook1, FreeHook1);
4774989affSNico Weber   __sanitizer_install_malloc_and_free_hooks(MallocHook2, FreeHook2);
4874989affSNico Weber   int *undef1, *undef2;
4974989affSNico Weber   allocate(undef1, undef2);
5074989affSNico Weber   // CHECK: MallocHook
5174989affSNico Weber   // CHECK: MH1
5274989affSNico Weber   // CHECK: MH2
5374989affSNico Weber   // Check that malloc hook was called with correct argument.
5474989affSNico Weber   if (global_ptr != (void*)x) {
5574989affSNico Weber     _exit(1);
5674989affSNico Weber   }
57*90418dc9SJin Xin Ng 
58*90418dc9SJin Xin Ng   // Check that realloc invokes hooks
59*90418dc9SJin Xin Ng   // We realloc to 128 here to avoid potential oversizing by allocators
60*90418dc9SJin Xin Ng   // making this a no-op.
61*90418dc9SJin Xin Ng   x = reinterpret_cast<int *>(realloc((int *)x, sizeof(int) * 128));
62*90418dc9SJin Xin Ng   // CHECK-DAG: FreeHook{{[[:space:]].*}}FH1{{[[:space:]].*}}FH2
63*90418dc9SJin Xin Ng   // CHECK-DAG: MH1{{[[:space:]].*}}MH2
64*90418dc9SJin Xin Ng 
65*90418dc9SJin Xin Ng   x[0] = 0;
66*90418dc9SJin Xin Ng   x[127] = -1;
67*90418dc9SJin Xin Ng   free((void *)x);
6874989affSNico Weber   // CHECK: FH1
6974989affSNico Weber   // CHECK: FH2
7074989affSNico Weber   return 0;
7174989affSNico Weber }
72