Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 4 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 7 | #include <sanitizer/allocator_interface.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 8 | |
| 9 | static void *glob_ptr; |
| 10 | |
| 11 | extern "C" { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 12 | void __sanitizer_free_hook(const volatile void *ptr) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 13 | if (ptr == glob_ptr) { |
| 14 | *(int*)ptr = 0; |
| 15 | write(1, "FreeHook\n", sizeof("FreeHook\n")); |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | int 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 | } |