Home | History | Annotate | Download | only in TestCases
      1 // RUN: %clangxx_asan -O2 %s -o %t
      2 // RUN: %t 2>&1 | FileCheck %s
      3 #include <stdlib.h>
      4 #include <unistd.h>
      5 
      6 extern "C" {
      7 bool __asan_get_ownership(const void *p);
      8 
      9 void *global_ptr;
     10 
     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;
     17   }
     18 }
     19 void __asan_free_hook(void *ptr) {
     20   if (__asan_get_ownership(ptr) && ptr == global_ptr)
     21     write(1, "FreeHook\n", sizeof("FreeHook\n"));
     22 }
     23 }  // extern "C"
     24 
     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);
     31   }
     32   *x = 0;
     33   delete x;
     34   // CHECK: FreeHook
     35   return 0;
     36 }
     37