Skip to content

Commit

Permalink
Fix WIN32_OVERRIDE_ALLOCATORS for VS2017
Browse files Browse the repository at this point in the history
At first I try to add some functions as what Chrome does at their
https://chromium.googlesource.com/chromium/src/+/master/base/allocator/allocator_shim_override_ucrt_symbols_win.h,
but it still fails. So I decide to remove all heap-related objects
from libucrt.lib to see what happens. At the end I find that a lot of
functions in the CRT directly invoke _malloc_base instead of
malloc (and the others alike), hence we need to override them as well.

This should close issue envoyproxy#716.

[alkondratenko@gmail.com: added reference to ticket]
Signed-off-by: Aliaksey Kandratsenka <alkondratenko@gmail.com>
  • Loading branch information
HolyWu authored and alk committed Apr 30, 2018
1 parent ebc85cc commit 497ea33
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/libc_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#endif
#include <gperftools/tcmalloc.h>

#if __cplusplus >= 201103L
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#define CPP_NOTHROW noexcept
#define CPP_BADALLOC
#else
Expand Down
4 changes: 4 additions & 0 deletions src/tcmalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,10 @@ extern "C" PERFTOOLS_DLL_DECL int tc_set_new_mode(int flag) PERFTOOLS_NOTHROW {
return old_mode;
}

extern "C" PERFTOOLS_DLL_DECL int tc_query_new_mode() PERFTOOLS_NOTHROW {
return tc_new_mode;
}

#ifndef TCMALLOC_USING_DEBUGALLOCATION // debugallocation.cc defines its own

// CAVEAT: The code structure below ensures that MallocHook methods are always
Expand Down
61 changes: 48 additions & 13 deletions src/windows/override_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,55 @@

#include "tcmalloc.cc"

extern "C" void* _recalloc(void* p, size_t n, size_t size) {
extern "C" {

void* _malloc_base(size_t size) {
return malloc(size);
}

void _free_base(void* p) {
free(p);
}

void* _calloc_base(size_t n, size_t size) {
return calloc(n, size);
}

void* _recalloc(void* p, size_t n, size_t size) {
void* result = realloc(p, n * size);
memset(result, 0, n * size);
return result;
}

extern "C" void* _calloc_impl(size_t n, size_t size) {
void* _calloc_impl(size_t n, size_t size) {
return calloc(n, size);
}

extern "C" size_t _msize(void* p) {
size_t _msize(void* p) {
return MallocExtension::instance()->GetAllocatedSize(p);
}

extern "C" intptr_t _get_heap_handle() {
HANDLE __acrt_heap = nullptr;

bool __acrt_initialize_heap() {
new TCMallocGuard();
return true;
}

bool __acrt_uninitialize_heap(bool) {
return true;
}

intptr_t _get_heap_handle() {
return 0;
}

HANDLE __acrt_getheap() {
return __acrt_heap;
}

// The CRT heap initialization stub.
extern "C" int _heap_init() {
int _heap_init() {
// We intentionally leak this object. It lasts for the process
// lifetime. Trying to teardown at _heap_term() is so late that
// you can't do anything useful anyway.
Expand All @@ -80,13 +109,25 @@ extern "C" int _heap_init() {
}

// The CRT heap cleanup stub.
extern "C" void _heap_term() {
void _heap_term() {
}

extern "C" int _set_new_mode(int flag) {
// We set this to 1 because part of the CRT uses a check of _crtheap != 0
// to test whether the CRT has been initialized. Once we've ripped out
// the allocators from libcmt, we need to provide this definition so that
// the rest of the CRT is still usable.
void* _crtheap = reinterpret_cast<void*>(1);

int _set_new_mode(int flag) {
return tc_set_new_mode(flag);
}

int _query_new_mode() {
return tc_query_new_mode();
}

} // extern "C"

#ifndef NDEBUG
#undef malloc
#undef free
Expand Down Expand Up @@ -115,9 +156,3 @@ extern "C" void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
return calloc(n, size);
}
#endif // NDEBUG

// We set this to 1 because part of the CRT uses a check of _crtheap != 0
// to test whether the CRT has been initialized. Once we've ripped out
// the allocators from libcmt, we need to provide this definition so that
// the rest of the CRT is still usable.
extern "C" void* _crtheap = reinterpret_cast<void*>(1);
2 changes: 2 additions & 0 deletions src/windows/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ inline int snprintf(char *str, size_t size, const char *format, ...) {
}
#endif

#ifndef HAVE_INTTYPES_H
#define PRIx64 "I64x"
#define SCNx64 "I64x"
#define PRId64 "I64d"
Expand All @@ -352,6 +353,7 @@ inline int snprintf(char *str, size_t size, const char *format, ...) {
# define PRIuPTR "lu"
# define PRIxPTR "lx"
#endif
#endif

/* ----------------------------------- FILE IO */

Expand Down

0 comments on commit 497ea33

Please sign in to comment.