Move ASan lit-tests under test/asan
[blocksruntime.git] / test / asan / TestCases / malloc_hook.cc
blob83be1020ea434b18ac955d0fdb9eae6367d0f5ad
1 // RUN: %clangxx_asan -O2 %s -o %t
2 // RUN: %t 2>&1 | FileCheck %s
3 #include <stdlib.h>
4 #include <unistd.h>
6 extern "C" {
7 bool __asan_get_ownership(const void *p);
9 void *global_ptr;
11 // Note: avoid calling functions that allocate memory in malloc/free
12 // to avoid infinite recursion.
13 void __asan_malloc_hook(void *ptr, size_t sz) {
14 if (__asan_get_ownership(ptr)) {
15 write(1, "MallocHook\n", sizeof("MallocHook\n"));
16 global_ptr = ptr;
19 void __asan_free_hook(void *ptr) {
20 if (__asan_get_ownership(ptr) && ptr == global_ptr)
21 write(1, "FreeHook\n", sizeof("FreeHook\n"));
23 } // extern "C"
25 int main() {
26 volatile int *x = new int;
27 // CHECK: MallocHook
28 // Check that malloc hook was called with correct argument.
29 if (global_ptr != (void*)x) {
30 _exit(1);
32 *x = 0;
33 delete x;
34 // CHECK: FreeHook
35 return 0;