From e2cf03c327fd839f0cfc8b6d3f79ad5993b07431 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 21 Sep 2015 07:55:40 -0700 Subject: [PATCH] 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; }