forked from Snaipe/Criterion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added inheritable heap to fork process
- Loading branch information
Showing
6 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
*(static_cast<size_t*>(ptr)) = len; | ||
T* arr = static_cast<T*>(static_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); | ||
*(static_cast<size_t*>(ptr)) = len; | ||
|
||
T* arr = static_cast<T*>(static_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 = *(static_cast<size_t*>(ptr)); | ||
T* arr = static_cast<T*>(static_cast<size_t*>(ptr) + 1); | ||
for (int i = 0; i < len; ++i) { | ||
arr[i].~T(); | ||
} | ||
cr_free(ptr); | ||
} | ||
|
||
} | ||
# endif | ||
|
||
#endif /* !CRITERION_ALLOC_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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; | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |