blob: cbc5d6fed56ef892812f156f55c994835adf8334 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// 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
Stephen Hines6a211c52014-07-21 00:49:56 -07004
Stephen Hines2d1fdb22014-05-28 23:58:16 -07005#include <stdlib.h>
6#include <unistd.h>
Stephen Hines6a211c52014-07-21 00:49:56 -07007#include <sanitizer/allocator_interface.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -07008
9static void *glob_ptr;
10
11extern "C" {
Stephen Hines6a211c52014-07-21 00:49:56 -070012void __sanitizer_free_hook(const volatile void *ptr) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070013 if (ptr == glob_ptr) {
14 *(int*)ptr = 0;
15 write(1, "FreeHook\n", sizeof("FreeHook\n"));
16 }
17}
18}
19
20int main() {
21 int *x = (int*)malloc(100);
22 x[0] = 42;
23 glob_ptr = x;
24 int *y = (int*)realloc(x, 200);
25 // Verify that free hook was called and didn't spoil the memory.
26 if (y[0] != 42) {
27 _exit(1);
28 }
29 write(1, "Passed\n", sizeof("Passed\n"));
30 free(y);
31 // CHECK: FreeHook
32 // CHECK: Passed
33 return 0;
34}