1// Check that free hook doesn't conflict with Realloc.
2// RUN: %clangxx_memprof -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
3
4#include <sanitizer/allocator_interface.h>
5#include <stdlib.h>
6#include <unistd.h>
7
8static void *glob_ptr;
9
10// (Speculative fix if memprof is enabled on Apple platforms)
11// Required for dyld macOS 12.0+
12#if (__APPLE__)
13__attribute__((weak))
14#endif
15extern "C" void
16__sanitizer_free_hook(const volatile void *ptr) {
17 if (ptr == glob_ptr) {
18 *(int *)ptr = 0;
19 write(fd: 1, buf: "FreeHook\n", n: sizeof("FreeHook\n"));
20 }
21}
22
23int main() {
24 int *x = (int *)malloc(size: 100);
25 x[0] = 42;
26 glob_ptr = x;
27 int *y = (int *)realloc(ptr: x, size: 200);
28 // Verify that free hook was called and didn't spoil the memory.
29 if (y[0] != 42) {
30 _exit(status: 1);
31 }
32 write(fd: 1, buf: "Passed\n", n: sizeof("Passed\n"));
33 free(ptr: y);
34 // CHECK: FreeHook
35 // CHECK: Passed
36 return 0;
37}
38

source code of compiler-rt/test/memprof/TestCases/free_hook_realloc.cpp