[ASan/Win] Add a comment about DCL-using-static vs threads
[blocksruntime.git] / test / msan / malloc_hook.cc
blob5393080343beda6b0a1f192ec769588d6dbbb153
1 // RUN: %clangxx_msan -O2 %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s
3 #include <stdlib.h>
4 #include <unistd.h>
6 extern "C" {
7 int __msan_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 __msan_malloc_hook(void *ptr, size_t sz) {
14 if (__msan_get_ownership(ptr)) {
15 write(1, "MallocHook\n", sizeof("MallocHook\n"));
16 global_ptr = ptr;
19 void __msan_free_hook(void *ptr) {
20 if (__msan_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;