1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 //
3 // This small test program demonstrate a crash on Windows in cb_create_thread
4 // (Inside CreateThread)
5 //
6 #include "config.h"
7 #include <cassert>
8 
9 #include "daemon/alloc_hooks.h"
10 
11 size_t alloc_size;
12 
13 extern "C" {
NewHook(const void* ptr, size_t)14     static void NewHook(const void* ptr, size_t) {
15         if (ptr != NULL) {
16             void* p = const_cast<void*>(ptr);
17             alloc_size = mc_get_allocation_size(p);
18         }
19     }
20 
DeleteHook(const void* ptr)21     static void DeleteHook(const void* ptr) {
22         if (ptr != NULL) {
23             void* p = const_cast<void*>(ptr);
24             alloc_size = mc_get_allocation_size(p);
25         }
26     }
27 
TestThread(void* arg)28     static void TestThread(void* arg) {
29         alloc_size = 0;
30 
31         char *p = new char[100];
32         assert(alloc_size > 0);
33         size_t allocated = alloc_size;
34 
35         alloc_size = 0;
36         delete []p;
37 
38         assert(alloc_size == allocated);
39     }
40 }
41 
main(void)42 int main(void) {
43    init_alloc_hooks();
44 
45    mc_add_new_hook(NewHook);
46    mc_add_delete_hook(DeleteHook);
47 
48    cb_thread_t tid;
49    assert(cb_create_thread(&tid, TestThread, 0, 0) == 0);
50    assert(cb_join_thread(tid) == 0);
51 
52    mc_remove_new_hook(NewHook);
53    mc_remove_delete_hook(DeleteHook);
54 
55    return 0;
56 }
57