]> git.proxmox.com Git - rustc.git/blame - src/compiler-rt/test/asan/TestCases/malloc_hook.cc
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / compiler-rt / test / asan / TestCases / malloc_hook.cc
CommitLineData
1a4d82fc
JJ
1// RUN: %clangxx_asan -O2 %s -o %t
2// RUN: %run %t 2>&1 | FileCheck %s
3#include <stdlib.h>
4#include <unistd.h>
5
6extern "C" {
7bool __asan_get_ownership(const void *p);
8
9void *global_ptr;
10
11// Note: avoid calling functions that allocate memory in malloc/free
12// to avoid infinite recursion.
13void __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}
19void __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
25int 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}