#include <stdlib.h>
#include <unistd.h>
#include <sanitizer/allocator_interface.h>
extern "C" {
const volatile void *global_ptr;
#define WRITE(s) write(1, s, sizeof(s))
void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz) {
if (__sanitizer_get_ownership(ptr) && sz == sizeof(int)) {
WRITE("MallocHook\n");
global_ptr = ptr;
}
}
void __sanitizer_free_hook(const volatile void *ptr) {
if (__sanitizer_get_ownership(ptr) && ptr == global_ptr)
WRITE("FreeHook\n");
}
}
volatile int *x;
void MallocHook1(const volatile void *ptr, size_t sz) { WRITE("MH1\n"); }
void MallocHook2(const volatile void *ptr, size_t sz) { WRITE("MH2\n"); }
void FreeHook1(const volatile void *ptr) { WRITE("FH1\n"); }
void FreeHook2(const volatile void *ptr) { WRITE("FH2\n"); }
__attribute__((noinline)) void allocate(int *unused1, int *unused2) {
x = reinterpret_cast<int *>(malloc(sizeof(int)));
}
int main() {
__sanitizer_install_malloc_and_free_hooks(MallocHook1, FreeHook1);
__sanitizer_install_malloc_and_free_hooks(MallocHook2, FreeHook2);
int *undef1, *undef2;
allocate(undef1, undef2);
if (global_ptr != (void*)x) {
_exit(1);
}
x = reinterpret_cast<int *>(realloc((int *)x, sizeof(int) * 128));
x[0] = 0;
x[127] = -1;
free((void *)x);
return 0;
}