From 5f9f617ee7e78b7e812f289b0954165d7798071f Mon Sep 17 00:00:00 2001 From: Snaipe Date: Sat, 19 Sep 2015 18:08:37 +0200 Subject: [PATCH 1/9] Added inheritable heap to fork process --- CMakeLists.txt | 3 ++ include/criterion/alloc.h | 100 ++++++++++++++++++++++++++++++++++++++ src/compat/alloc.c | 94 +++++++++++++++++++++++++++++++++++ src/compat/alloc.h | 35 +++++++++++++ src/compat/process.c | 5 ++ test/alloc.cc | 36 ++++++++++++++ 6 files changed, 273 insertions(+) create mode 100644 include/criterion/alloc.h create mode 100644 src/compat/alloc.c create mode 100644 src/compat/alloc.h create mode 100644 test/alloc.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 27c5666..246a302 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,8 @@ set(SOURCE_FILES src/compat/time.c src/compat/time.h src/compat/posix.h + src/compat/alloc.c + src/compat/alloc.h src/io/redirect.c src/io/event.c src/io/event.h @@ -140,6 +142,7 @@ set(INTERFACE_FILES include/criterion/asprintf-compat.h include/criterion/designated-initializer-compat.h include/criterion/preprocess.h + include/criterion/alloc.h ) # Generate the configure file diff --git a/include/criterion/alloc.h b/include/criterion/alloc.h new file mode 100644 index 0000000..d39c25a --- /dev/null +++ b/include/criterion/alloc.h @@ -0,0 +1,100 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2015 Franklin "Snaipe" Mathieu + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef CRITERION_ALLOC_H_ +# define CRITERION_ALLOC_H_ + +# include +# include "common.h" + +CR_BEGIN_C_API + +CR_API void *cr_malloc(size_t size); +CR_API void *cr_calloc(size_t nmemb, size_t size); +CR_API void *cr_realloc(void *ptr, size_t size); +CR_API void cr_free(void *ptr); + +CR_END_C_API + +# ifdef __cplusplus +# include + +namespace criterion { + + void *(*const malloc)(size_t) = cr_malloc; + void (*const free)(void *) = cr_free; + void *(*const calloc)(size_t, size_t) = cr_calloc; + void *(*const realloc)(void *, size_t) = cr_realloc; + + template + T* new_obj(Params... params) { + T* obj = static_cast(cr_malloc(sizeof (T))); + new (obj) T(params...); + return obj; + } + + template + typename std::enable_if::value>::type* + new_arr(size_t len) { + void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len); + *(static_cast(ptr)) = len; + T* arr = static_cast(static_cast(ptr) + 1); + return arr; + } + + template + T* new_arr(size_t len) { + void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len); + *(static_cast(ptr)) = len; + + T* arr = static_cast(static_cast(ptr) + 1); + for (size_t i = 0; i < len; ++i) + new (arr + i) T(); + return arr; + } + + template + void delete_obj(T* ptr) { + ptr->~T(); + cr_free(ptr); + } + + template + void delete_arr(typename std::enable_if::value>::type* ptr) { + cr_free(ptr); + } + + template + void delete_arr(T* ptr) { + size_t len = *(static_cast(ptr)); + T* arr = static_cast(static_cast(ptr) + 1); + for (int i = 0; i < len; ++i) { + arr[i].~T(); + } + cr_free(ptr); + } + +} +# endif + +#endif /* !CRITERION_ALLOC_H_ */ diff --git a/src/compat/alloc.c b/src/compat/alloc.c new file mode 100644 index 0000000..2480ef2 --- /dev/null +++ b/src/compat/alloc.c @@ -0,0 +1,94 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2015 Franklin "Snaipe" Mathieu + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "alloc.h" +#include "internal.h" +#include + +#ifdef VANILLA_WIN32 +HANDLE g_heap; + +void init_inheritable_heap(void) { + g_heap = HeapCreate(0, 0, 0); + if (g_heap == (HANDLE) NULL) { + fputs("Could not create the private inheritable heap.", stderr); + abort(); + } +} + +int inherit_heap(HANDLE child_process) { + PROCESS_HEAP_ENTRY entry = { .lpData = NULL }; + + while (HeapWalk(g_heap, &entry)) { + if (!(entry.wFlags & PROCESS_HEAP_REGION)) + continue; + + if (!VirtualAllocEx(child_process, + entry.lpData, + entry.cbData + entry.cbOverhead, + MEM_COMMIT, + PAGE_READWRITE)) + return -1; + + if (!WriteProcessMemory(child_process, + entry.lpData, + entry.lpData, + entry.cbData + entry.cbOverhead, + NULL)) + return -1; + } + return 0; +} +#endif + +void *cr_malloc(size_t size) { +#ifdef VANILLA_WIN32 + return HeapAlloc(g_heap, 0, size); +#else + return malloc(size); +#endif +} + +void *cr_calloc(size_t nmemb, size_t size) { +#ifdef VANILLA_WIN32 + return HeapAlloc(g_heap, HEAP_ZERO_MEMORY, nmemb * size); +#else + return calloc(nmemb, size); +#endif +} + +void *cr_realloc(void *ptr, size_t size) { +#ifdef VANILLA_WIN32 + return HeapReAlloc(g_heap, 0, ptr, size); +#else + return realloc(ptr, size); +#endif +} + +void cr_free(void *ptr) { +#ifdef VANILLA_WIN32 + HeapFree(g_heap, 0, ptr); +#else + free(ptr); +#endif +} diff --git a/src/compat/alloc.h b/src/compat/alloc.h new file mode 100644 index 0000000..3440e18 --- /dev/null +++ b/src/compat/alloc.h @@ -0,0 +1,35 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2015 Franklin "Snaipe" Mathieu + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef COMPAT_ALLOC_H_ +# define COMPAT_ALLOC_H_ + +# include "criterion/alloc.h" +# include "posix.h" + +# ifdef VANILLA_WIN32 +void init_inheritable_heap(void); +int inherit_heap(HANDLE child_process); +# endif + +#endif /* !COMPAT_ALLOC_H_ */ diff --git a/src/compat/process.c b/src/compat/process.c index 8a67623..cf2281d 100644 --- a/src/compat/process.c +++ b/src/compat/process.c @@ -30,6 +30,7 @@ #include "process.h" #include "internal.h" #include "pipe-internal.h" +#include "alloc.h" #include @@ -168,6 +169,8 @@ static void CALLBACK handle_child_terminated(PVOID lpParameter, int resume_child(void) { #ifdef VANILLA_WIN32 + init_inheritable_heap(); + TCHAR mapping_name[128]; _sntprintf(mapping_name, 128, g_mapping_name, GetCurrentProcessId()); @@ -322,6 +325,8 @@ s_proc_handle *fork_process() { if (g_worker_context.suite->data) ctx->suite_data = *g_worker_context.suite->data; + inherit_heap(info.hProcess); + if (ResumeThread(info.hThread) == (DWORD) -1) goto failure; diff --git a/test/alloc.cc b/test/alloc.cc new file mode 100644 index 0000000..2d8366f --- /dev/null +++ b/test/alloc.cc @@ -0,0 +1,36 @@ +#include "criterion/criterion.h" +#include "criterion/alloc.h" + +struct Obj { + int foo; + long bar; + + Obj() {} + Obj(int foo, long bar) : foo(foo), bar(bar) {} +}; + +Test(alloc, object) { + Obj *o = criterion::new_obj(42, 314); + cr_assert_not_null(o); + + cr_assert_eq(o->foo, 42); + cr_assert_eq(o->bar, 314); + + criterion::delete_obj(o); +} + +Test(alloc, array) { + Obj *o = criterion::new_arr(3); + cr_assert_not_null(o); + + new (&o[0]) Obj(1, 2); + new (&o[1]) Obj(2, 4); + new (&o[2]) Obj(3, 6); + + for (int i = 0; i < 3; ++i) { + cr_assert_eq(o[i].foo, i + 1); + cr_assert_eq(o[i].bar, 2 * (i + 1)); + } + + criterion::delete_arr(o); +} From acce462995341bea8d9e8c0f2e58967972822ea2 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 00:03:08 +0200 Subject: [PATCH 2/9] Delayed private heap allocation to after child the process started on windows --- src/compat/process.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/compat/process.c b/src/compat/process.c index cf2281d..8d69a79 100644 --- a/src/compat/process.c +++ b/src/compat/process.c @@ -169,8 +169,6 @@ static void CALLBACK handle_child_terminated(PVOID lpParameter, int resume_child(void) { #ifdef VANILLA_WIN32 - init_inheritable_heap(); - TCHAR mapping_name[128]; _sntprintf(mapping_name, 128, g_mapping_name, GetCurrentProcessId()); @@ -179,8 +177,10 @@ int resume_child(void) { FALSE, mapping_name); - if (sharedMem == NULL) + if (sharedMem == NULL) { + init_inheritable_heap(); return 0; + } struct full_context *ctx = (struct full_context *) MapViewOfFile(sharedMem, FILE_MAP_ALL_ACCESS, @@ -196,6 +196,20 @@ int resume_child(void) { local_ctx = *ctx; UnmapViewOfFile(ctx); + HANDLE self = GetCurrentThread(); + DuplicateHandle(GetCurrentProcess(), + self, + GetCurrentProcess(), + &self, + 0, + FALSE, + DUPLICATE_SAME_ACCESS); + + SetEvent(local_ctx.sync); + SuspendThread(self); + + init_inheritable_heap(); + struct test_single_param *param = NULL; if (local_ctx.param.size != 0) { ctx = (struct full_context*) MapViewOfFile(sharedMem, @@ -325,8 +339,6 @@ s_proc_handle *fork_process() { if (g_worker_context.suite->data) ctx->suite_data = *g_worker_context.suite->data; - inherit_heap(info.hProcess); - if (ResumeThread(info.hThread) == (DWORD) -1) goto failure; @@ -336,6 +348,11 @@ s_proc_handle *fork_process() { if (wres == WAIT_OBJECT_0) goto failure; + inherit_heap(info.hProcess); + + if (ResumeThread(info.hThread) == (DWORD) -1) + goto failure; + CloseHandle(info.hThread); UnmapViewOfFile(ctx); From 4352c402c02ac38c14e64bb2d169007b3c484d58 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 03:17:25 +0200 Subject: [PATCH 3/9] Revert "Delayed private heap allocation to after child the process started on windows" This reverts commit acce462995341bea8d9e8c0f2e58967972822ea2. --- src/compat/process.c | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/compat/process.c b/src/compat/process.c index 8d69a79..cf2281d 100644 --- a/src/compat/process.c +++ b/src/compat/process.c @@ -169,6 +169,8 @@ static void CALLBACK handle_child_terminated(PVOID lpParameter, int resume_child(void) { #ifdef VANILLA_WIN32 + init_inheritable_heap(); + TCHAR mapping_name[128]; _sntprintf(mapping_name, 128, g_mapping_name, GetCurrentProcessId()); @@ -177,10 +179,8 @@ int resume_child(void) { FALSE, mapping_name); - if (sharedMem == NULL) { - init_inheritable_heap(); + if (sharedMem == NULL) return 0; - } struct full_context *ctx = (struct full_context *) MapViewOfFile(sharedMem, FILE_MAP_ALL_ACCESS, @@ -196,20 +196,6 @@ int resume_child(void) { local_ctx = *ctx; UnmapViewOfFile(ctx); - HANDLE self = GetCurrentThread(); - DuplicateHandle(GetCurrentProcess(), - self, - GetCurrentProcess(), - &self, - 0, - FALSE, - DUPLICATE_SAME_ACCESS); - - SetEvent(local_ctx.sync); - SuspendThread(self); - - init_inheritable_heap(); - struct test_single_param *param = NULL; if (local_ctx.param.size != 0) { ctx = (struct full_context*) MapViewOfFile(sharedMem, @@ -339,6 +325,8 @@ s_proc_handle *fork_process() { if (g_worker_context.suite->data) ctx->suite_data = *g_worker_context.suite->data; + inherit_heap(info.hProcess); + if (ResumeThread(info.hThread) == (DWORD) -1) goto failure; @@ -348,11 +336,6 @@ s_proc_handle *fork_process() { if (wres == WAIT_OBJECT_0) goto failure; - inherit_heap(info.hProcess); - - if (ResumeThread(info.hThread) == (DWORD) -1) - goto failure; - CloseHandle(info.hThread); UnmapViewOfFile(ctx); From 2d95fa4bde26041a94ca2cdc5871681fea984364 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Sun, 20 Sep 2015 18:18:41 -0700 Subject: [PATCH 4/9] Added garbage, low quality memory allocator as cr_malloc implementation --- src/compat/alloc.c | 49 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/compat/alloc.c b/src/compat/alloc.c index 2480ef2..348de69 100644 --- a/src/compat/alloc.c +++ b/src/compat/alloc.c @@ -26,34 +26,30 @@ #include #ifdef VANILLA_WIN32 -HANDLE g_heap; +struct block { + void *ptr; + size_t size; + struct block *next; +}; + +struct block *g_blocks = NULL; void init_inheritable_heap(void) { - g_heap = HeapCreate(0, 0, 0); - if (g_heap == (HANDLE) NULL) { - fputs("Could not create the private inheritable heap.", stderr); - abort(); - } } int inherit_heap(HANDLE child_process) { - PROCESS_HEAP_ENTRY entry = { .lpData = NULL }; - - while (HeapWalk(g_heap, &entry)) { - if (!(entry.wFlags & PROCESS_HEAP_REGION)) - continue; - + for (struct block *b = g_blocks; b != NULL; b = b->next) { if (!VirtualAllocEx(child_process, - entry.lpData, - entry.cbData + entry.cbOverhead, + b->ptr, + b->size, MEM_COMMIT, PAGE_READWRITE)) return -1; if (!WriteProcessMemory(child_process, - entry.lpData, - entry.lpData, - entry.cbData + entry.cbOverhead, + b->ptr, + b->ptr, + b->size, NULL)) return -1; } @@ -63,7 +59,15 @@ int inherit_heap(HANDLE child_process) { void *cr_malloc(size_t size) { #ifdef VANILLA_WIN32 - return HeapAlloc(g_heap, 0, size); + void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); + if (ptr == NULL) + return NULL; + struct block *b = malloc(sizeof (struct block)); + b->next = g_blocks; + b->ptr = ptr; + b->size = size; + g_blocks = b; + return ptr; #else return malloc(size); #endif @@ -71,7 +75,9 @@ void *cr_malloc(size_t size) { void *cr_calloc(size_t nmemb, size_t size) { #ifdef VANILLA_WIN32 - return HeapAlloc(g_heap, HEAP_ZERO_MEMORY, nmemb * size); + void *ptr = cr_malloc(nmemb * size); + memset(ptr, 0, nmemb * size); + return ptr; #else return calloc(nmemb, size); #endif @@ -79,7 +85,9 @@ void *cr_calloc(size_t nmemb, size_t size) { void *cr_realloc(void *ptr, size_t size) { #ifdef VANILLA_WIN32 - return HeapReAlloc(g_heap, 0, ptr, size); + if (size > 4096) + return NULL; + return ptr; #else return realloc(ptr, size); #endif @@ -87,7 +95,6 @@ void *cr_realloc(void *ptr, size_t size) { void cr_free(void *ptr) { #ifdef VANILLA_WIN32 - HeapFree(g_heap, 0, ptr); #else free(ptr); #endif From e0b31827860081051481414f3f542e799e4a929c Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 06:39:40 -0700 Subject: [PATCH 5/9] Revert "Added garbage, low quality memory allocator as cr_malloc implementation" This reverts commit 2d95fa4bde26041a94ca2cdc5871681fea984364. --- src/compat/alloc.c | 49 ++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/compat/alloc.c b/src/compat/alloc.c index 348de69..2480ef2 100644 --- a/src/compat/alloc.c +++ b/src/compat/alloc.c @@ -26,30 +26,34 @@ #include #ifdef VANILLA_WIN32 -struct block { - void *ptr; - size_t size; - struct block *next; -}; - -struct block *g_blocks = NULL; +HANDLE g_heap; void init_inheritable_heap(void) { + g_heap = HeapCreate(0, 0, 0); + if (g_heap == (HANDLE) NULL) { + fputs("Could not create the private inheritable heap.", stderr); + abort(); + } } int inherit_heap(HANDLE child_process) { - for (struct block *b = g_blocks; b != NULL; b = b->next) { + PROCESS_HEAP_ENTRY entry = { .lpData = NULL }; + + while (HeapWalk(g_heap, &entry)) { + if (!(entry.wFlags & PROCESS_HEAP_REGION)) + continue; + if (!VirtualAllocEx(child_process, - b->ptr, - b->size, + entry.lpData, + entry.cbData + entry.cbOverhead, MEM_COMMIT, PAGE_READWRITE)) return -1; if (!WriteProcessMemory(child_process, - b->ptr, - b->ptr, - b->size, + entry.lpData, + entry.lpData, + entry.cbData + entry.cbOverhead, NULL)) return -1; } @@ -59,15 +63,7 @@ int inherit_heap(HANDLE child_process) { void *cr_malloc(size_t size) { #ifdef VANILLA_WIN32 - void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); - if (ptr == NULL) - return NULL; - struct block *b = malloc(sizeof (struct block)); - b->next = g_blocks; - b->ptr = ptr; - b->size = size; - g_blocks = b; - return ptr; + return HeapAlloc(g_heap, 0, size); #else return malloc(size); #endif @@ -75,9 +71,7 @@ void *cr_malloc(size_t size) { void *cr_calloc(size_t nmemb, size_t size) { #ifdef VANILLA_WIN32 - void *ptr = cr_malloc(nmemb * size); - memset(ptr, 0, nmemb * size); - return ptr; + return HeapAlloc(g_heap, HEAP_ZERO_MEMORY, nmemb * size); #else return calloc(nmemb, size); #endif @@ -85,9 +79,7 @@ void *cr_calloc(size_t nmemb, size_t size) { void *cr_realloc(void *ptr, size_t size) { #ifdef VANILLA_WIN32 - if (size > 4096) - return NULL; - return ptr; + return HeapReAlloc(g_heap, 0, ptr, size); #else return realloc(ptr, size); #endif @@ -95,6 +87,7 @@ void *cr_realloc(void *ptr, size_t size) { void cr_free(void *ptr) { #ifdef VANILLA_WIN32 + HeapFree(g_heap, 0, ptr); #else free(ptr); #endif From 40f7646e22e4220fcbb007cf2e78ddd11e589722 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 07:03:11 -0700 Subject: [PATCH 6/9] Added retry routine on heap creation until the base address is suitable. --- src/compat/alloc.c | 22 +++++++++++++++++++++- src/compat/process.c | 6 +++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/compat/alloc.c b/src/compat/alloc.c index 2480ef2..aa7f1fc 100644 --- a/src/compat/alloc.c +++ b/src/compat/alloc.c @@ -28,8 +28,28 @@ #ifdef VANILLA_WIN32 HANDLE g_heap; +struct garbage_heap { + HANDLE handle; + struct garbage_heap *next; +}; + +# define HEAP_MIN_BASE 0x08000000 + void init_inheritable_heap(void) { - g_heap = HeapCreate(0, 0, 0); + struct garbage_heap *heaps = NULL; + + while ((void*)(g_heap = HeapCreate(0, 0, 0)) < (void*)HEAP_MIN_BASE) { + if (g_heap == NULL) + break; + + struct garbage_heap *heap = malloc(sizeof(struct garbage_heap)); + heap->handle = g_heap; + heap->next = heaps; + heaps = heap; + } + for (struct garbage_heap *h = heaps; h != NULL; h = h->next) + HeapDestroy(h->handle); + if (g_heap == (HANDLE) NULL) { fputs("Could not create the private inheritable heap.", stderr); abort(); diff --git a/src/compat/process.c b/src/compat/process.c index cf2281d..a553a05 100644 --- a/src/compat/process.c +++ b/src/compat/process.c @@ -169,8 +169,6 @@ static void CALLBACK handle_child_terminated(PVOID lpParameter, int resume_child(void) { #ifdef VANILLA_WIN32 - init_inheritable_heap(); - TCHAR mapping_name[128]; _sntprintf(mapping_name, 128, g_mapping_name, GetCurrentProcessId()); @@ -179,8 +177,10 @@ int resume_child(void) { FALSE, mapping_name); - if (sharedMem == NULL) + if (sharedMem == NULL) { + init_inheritable_heap(); return 0; + } struct full_context *ctx = (struct full_context *) MapViewOfFile(sharedMem, FILE_MAP_ALL_ACCESS, From e2cf03c327fd839f0cfc8b6d3f79ad5993b07431 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 07:55:40 -0700 Subject: [PATCH 7/9] Fixed context passing errors for the heap and added dynamic parameter allocation for parameterized tests --- include/criterion/alloc.h | 12 ++++++------ samples/parameterized.c | 36 ++++++++++++++++++++++++++---------- src/compat/alloc.c | 8 +++++--- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/include/criterion/alloc.h b/include/criterion/alloc.h index d39c25a..8b80ead 100644 --- a/include/criterion/alloc.h +++ b/include/criterion/alloc.h @@ -57,17 +57,17 @@ namespace criterion { typename std::enable_if::value>::type* new_arr(size_t len) { void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len); - *(static_cast(ptr)) = len; - T* arr = static_cast(static_cast(ptr) + 1); + *(reinterpret_cast(ptr)) = len; + T* arr = reinterpret_cast(reinterpret_cast(ptr) + 1); return arr; } template T* new_arr(size_t len) { void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len); - *(static_cast(ptr)) = len; + *(reinterpret_cast(ptr)) = len; - T* arr = static_cast(static_cast(ptr) + 1); + T* arr = reinterpret_cast(reinterpret_cast(ptr) + 1); for (size_t i = 0; i < len; ++i) new (arr + i) T(); return arr; @@ -86,8 +86,8 @@ namespace criterion { template void delete_arr(T* ptr) { - size_t len = *(static_cast(ptr)); - T* arr = static_cast(static_cast(ptr) + 1); + size_t len = *(reinterpret_cast(ptr)); + T* arr = reinterpret_cast(reinterpret_cast(ptr) + 1); for (int i = 0; i < len; ++i) { arr[i].~T(); } diff --git a/samples/parameterized.c b/samples/parameterized.c index 6c58b9a..2bf1341 100644 --- a/samples/parameterized.c +++ b/samples/parameterized.c @@ -38,24 +38,40 @@ ParameterizedTest(struct parameter_tuple *tup, params, multiple) { // Cleaning up dynamically generated parameters -// Note: Do **NOT** embed dynamically allocated pointers inside of structures -// or this will fail on windows +// you **MUST** use cr_malloc, cr_free, cr_realloc, and cr_calloc instead of their +// unprefixed counterparts to allocate dynamic memory in parameters, otherwise +// this will crash on Windows builds of the test. + +struct parameter_tuple_dyn { + int i; + double *d; +}; void free_params(struct criterion_test_params *crp) { - free(crp->params); + for (size_t i = 0; i < crp->length; ++i) { + struct parameter_tuple_dyn *tup = (struct parameter_tuple_dyn*) crp->params + i; + cr_free(tup->d); + } + cr_free(crp->params); +} + +double *gen_double(double val) { + double *ptr = cr_malloc(sizeof (double)); + *ptr = val; + return ptr; } ParameterizedTestParameters(params, cleanup) { const size_t nb_tuples = 3; - struct parameter_tuple *params = malloc(sizeof(struct parameter_tuple) * nb_tuples); - params[0] = (struct parameter_tuple) { 1, 2 }; - params[1] = (struct parameter_tuple) { 3, 4 }; - params[2] = (struct parameter_tuple) { 5, 6 }; + struct parameter_tuple_dyn *params = cr_malloc(sizeof (struct parameter_tuple_dyn) * nb_tuples); + params[0] = (struct parameter_tuple_dyn) { 1, gen_double(2) }; + params[1] = (struct parameter_tuple_dyn) { 3, gen_double(4) }; + params[2] = (struct parameter_tuple_dyn) { 5, gen_double(6) }; - return cr_make_param_array(struct parameter_tuple, params, nb_tuples, free_params); + return cr_make_param_array(struct parameter_tuple_dyn, params, nb_tuples, free_params); } -ParameterizedTest(struct parameter_tuple *tup, params, cleanup) { - cr_assert_fail("Parameters: (%d, %f)", tup->i, tup->d); +ParameterizedTest(struct parameter_tuple_dyn *tup, params, cleanup) { + cr_assert_fail("Parameters: (%d, %f)", tup->i, *tup->d); } diff --git a/src/compat/alloc.c b/src/compat/alloc.c index aa7f1fc..7ae9d6d 100644 --- a/src/compat/alloc.c +++ b/src/compat/alloc.c @@ -63,17 +63,19 @@ int inherit_heap(HANDLE child_process) { if (!(entry.wFlags & PROCESS_HEAP_REGION)) continue; + DWORD region_size = entry.Region.dwCommittedSize; + if (!VirtualAllocEx(child_process, entry.lpData, - entry.cbData + entry.cbOverhead, - MEM_COMMIT, + region_size, + MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) return -1; if (!WriteProcessMemory(child_process, entry.lpData, entry.lpData, - entry.cbData + entry.cbOverhead, + region_size, NULL)) return -1; } From 942de4a5834536b0fb5248073b2279b473d09cbb Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 17:02:16 +0200 Subject: [PATCH 8/9] Fixed line numbers on regression tests for parameterized tests --- samples/outputs/parameterized.c.bin.err.expected | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/outputs/parameterized.c.bin.err.expected b/samples/outputs/parameterized.c.bin.err.expected index f4b3134..a080c6d 100644 --- a/samples/outputs/parameterized.c.bin.err.expected +++ b/samples/outputs/parameterized.c.bin.err.expected @@ -1,8 +1,8 @@ -[----] parameterized.c:60: Assertion failed: Parameters: (1, 2.000000) +[----] parameterized.c:76: Assertion failed: Parameters: (1, 2.000000) [FAIL] params::cleanup: (0.00s) -[----] parameterized.c:60: Assertion failed: Parameters: (3, 4.000000) +[----] parameterized.c:76: Assertion failed: Parameters: (3, 4.000000) [FAIL] params::cleanup: (0.00s) -[----] parameterized.c:60: Assertion failed: Parameters: (5, 6.000000) +[----] parameterized.c:76: Assertion failed: Parameters: (5, 6.000000) [FAIL] params::cleanup: (0.00s) [----] parameterized.c:36: Assertion failed: Parameters: (1, 2.000000) [FAIL] params::multiple: (0.00s) From 2819cf9ebe22aac047705c240ded26a9a95267c4 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 17:11:20 +0200 Subject: [PATCH 9/9] Added missing alloc.h header inclusion --- include/criterion/criterion.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/criterion/criterion.h b/include/criterion/criterion.h index 0beb926..db9dfdc 100644 --- a/include/criterion/criterion.h +++ b/include/criterion/criterion.h @@ -28,6 +28,7 @@ # include "common.h" # include "types.h" # include "assert.h" +# include "alloc.h" # define IDENTIFIER_(Category, Name, Suffix) \ Category ## _ ## Name ## _ ## Suffix