[Issue #47] Merge branch 'features/winfork' into bleeding
This commit is contained in:
commit
c474567472
9 changed files with 326 additions and 14 deletions
|
@ -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
|
||||
|
|
100
include/criterion/alloc.h
Normal file
100
include/criterion/alloc.h
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
|
||||
*
|
||||
* 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 <stddef.h>
|
||||
# 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 <type_traits>
|
||||
|
||||
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<typename T, typename... Params>
|
||||
T* new_obj(Params... params) {
|
||||
T* obj = static_cast<T*>(cr_malloc(sizeof (T)));
|
||||
new (obj) T(params...);
|
||||
return obj;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<std::is_fundamental<T>::value>::type*
|
||||
new_arr(size_t len) {
|
||||
void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len);
|
||||
*(reinterpret_cast<size_t*>(ptr)) = len;
|
||||
T* arr = reinterpret_cast<T*>(reinterpret_cast<size_t*>(ptr) + 1);
|
||||
return arr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* new_arr(size_t len) {
|
||||
void *ptr = cr_malloc(sizeof (size_t) + sizeof (T) * len);
|
||||
*(reinterpret_cast<size_t*>(ptr)) = len;
|
||||
|
||||
T* arr = reinterpret_cast<T*>(reinterpret_cast<size_t*>(ptr) + 1);
|
||||
for (size_t i = 0; i < len; ++i)
|
||||
new (arr + i) T();
|
||||
return arr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_obj(T* ptr) {
|
||||
ptr->~T();
|
||||
cr_free(ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_arr(typename std::enable_if<std::is_fundamental<T>::value>::type* ptr) {
|
||||
cr_free(ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_arr(T* ptr) {
|
||||
size_t len = *(reinterpret_cast<size_t*>(ptr));
|
||||
T* arr = reinterpret_cast<T*>(reinterpret_cast<size_t*>(ptr) + 1);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
arr[i].~T();
|
||||
}
|
||||
cr_free(ptr);
|
||||
}
|
||||
|
||||
}
|
||||
# endif
|
||||
|
||||
#endif /* !CRITERION_ALLOC_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
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
[[0;34m----[0m] [0;1mparameterized.c[0m:[0;31m60[0m: Assertion failed: Parameters: (1, 2.000000)
|
||||
[[0;34m----[0m] [0;1mparameterized.c[0m:[0;31m76[0m: Assertion failed: Parameters: (1, 2.000000)
|
||||
[[0;31mFAIL[0m] params::cleanup: (0.00s)
|
||||
[[0;34m----[0m] [0;1mparameterized.c[0m:[0;31m60[0m: Assertion failed: Parameters: (3, 4.000000)
|
||||
[[0;34m----[0m] [0;1mparameterized.c[0m:[0;31m76[0m: Assertion failed: Parameters: (3, 4.000000)
|
||||
[[0;31mFAIL[0m] params::cleanup: (0.00s)
|
||||
[[0;34m----[0m] [0;1mparameterized.c[0m:[0;31m60[0m: Assertion failed: Parameters: (5, 6.000000)
|
||||
[[0;34m----[0m] [0;1mparameterized.c[0m:[0;31m76[0m: Assertion failed: Parameters: (5, 6.000000)
|
||||
[[0;31mFAIL[0m] params::cleanup: (0.00s)
|
||||
[[0;34m----[0m] [0;1mparameterized.c[0m:[0;31m36[0m: Assertion failed: Parameters: (1, 2.000000)
|
||||
[[0;31mFAIL[0m] params::multiple: (0.00s)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
116
src/compat/alloc.c
Normal file
116
src/compat/alloc.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
|
||||
#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) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
int inherit_heap(HANDLE child_process) {
|
||||
PROCESS_HEAP_ENTRY entry = { .lpData = NULL };
|
||||
|
||||
while (HeapWalk(g_heap, &entry)) {
|
||||
if (!(entry.wFlags & PROCESS_HEAP_REGION))
|
||||
continue;
|
||||
|
||||
DWORD region_size = entry.Region.dwCommittedSize;
|
||||
|
||||
if (!VirtualAllocEx(child_process,
|
||||
entry.lpData,
|
||||
region_size,
|
||||
MEM_RESERVE | MEM_COMMIT,
|
||||
PAGE_READWRITE))
|
||||
return -1;
|
||||
|
||||
if (!WriteProcessMemory(child_process,
|
||||
entry.lpData,
|
||||
entry.lpData,
|
||||
region_size,
|
||||
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
|
||||
}
|
35
src/compat/alloc.h
Normal file
35
src/compat/alloc.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
|
||||
*
|
||||
* 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_ */
|
|
@ -30,6 +30,7 @@
|
|||
#include "process.h"
|
||||
#include "internal.h"
|
||||
#include "pipe-internal.h"
|
||||
#include "alloc.h"
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
|
@ -176,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,
|
||||
|
@ -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;
|
||||
|
||||
|
|
36
test/alloc.cc
Normal file
36
test/alloc.cc
Normal file
|
@ -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<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<Obj>(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);
|
||||
}
|
Loading…
Add table
Reference in a new issue