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