Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / malloc_hook.cpp
blob44dffcd47a51aa11fdb610d3988da41945391cd7
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.
7 // XFAIL: ubsan
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sanitizer/allocator_interface.h>
13 extern "C" {
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");
23 global_ptr = ptr;
26 void __sanitizer_free_hook(const volatile void *ptr) {
27 if (__sanitizer_get_ownership(ptr) && ptr == global_ptr)
28 WRITE("FreeHook\n");
30 } // extern "C"
32 volatile int *x;
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)));
45 int main() {
46 __sanitizer_install_malloc_and_free_hooks(MallocHook1, FreeHook1);
47 __sanitizer_install_malloc_and_free_hooks(MallocHook2, FreeHook2);
48 int *undef1, *undef2;
49 allocate(undef1, 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(1);
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
65 x[0] = 0;
66 x[127] = -1;
67 free((void *)x);
68 // CHECK: FH1
69 // CHECK: FH2
70 return 0;