1 // Check that free hook doesn't conflict with Realloc.
2 // RUN: %clangxx_asan -O2 %s -o %t
3 // RUN: %run %t 2>&1 | FileCheck %s
4 
5 // FIXME: merge this with the common free_hook_realloc test when we can run
6 // common tests on Windows.
7 
8 // FIXME: Doesn't work with DLLs
9 // XFAIL: win32-dynamic-asan
10 
11 #include <stdlib.h>
12 #include <io.h>
13 #include <sanitizer/allocator_interface.h>
14 
15 static void *glob_ptr;
16 
17 extern "C" {
__sanitizer_free_hook(const volatile void * ptr)18 void __sanitizer_free_hook(const volatile void *ptr) {
19   if (ptr == glob_ptr) {
20     *(int*)ptr = 0;
21     write(1, "FreeHook\n", sizeof("FreeHook\n"));
22   }
23 }
24 }
25 
main()26 int main() {
27   int *x = (int*)malloc(100);
28   x[0] = 42;
29   glob_ptr = x;
30   int *y = (int*)realloc(x, 200);
31   // Verify that free hook was called and didn't spoil the memory.
32   if (y[0] != 42) {
33     _exit(1);
34   }
35   write(1, "Passed\n", sizeof("Passed\n"));
36   free(y);
37   // CHECK: FreeHook
38   // CHECK: Passed
39   return 0;
40 }
41