Skip to content

Commit

Permalink
fix windows dynamic malloc overried when both ucrtbase and msvcrt are…
Browse files Browse the repository at this point in the history
… loaded; also fix virtualalloc2 on 32-bit
  • Loading branch information
daanx committed Jul 3, 2019
1 parent 1587058 commit 9390642
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/alloc-override-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ static void mi_module_resolve(HMODULE mod) {
// see if any patches apply
for (size_t i = 0; patches[i].name != NULL; i++) {
mi_patch_t* patch = &patches[i];
if (!patch->applied && patch->original==NULL) {
if (!patch->applied && patch->original==NULL) { // we apply only the first found dll
void* addr = GetProcAddress(mod, patch->name);
if (addr != NULL) {
// found it! set the address
Expand Down Expand Up @@ -554,9 +554,11 @@ static bool mi_patches_resolve(void) {
size_t count = needed / sizeof(HMODULE);
size_t ucrtbase_index = 0;
size_t mimalloc_index = 0;
// iterate through the loaded modules
for (size_t i = 0; i < count; i++) {
HMODULE mod = modules[i];
// iterate through the loaded modules; do this from the end so we prefer the
// first loaded DLL as sometimes both "msvcr" and "ucrt" are both loaded and we should
// override "ucrt" in that situation.
for (size_t i = count; i > 0; i--) {
HMODULE mod = modules[i-1];
char filename[MAX_PATH] = { 0 };
DWORD slen = GetModuleFileName(mod, filename, MAX_PATH);
if (slen > 0 && slen < MAX_PATH) {
Expand All @@ -566,7 +568,7 @@ static bool mi_patches_resolve(void) {
const char* basename = (lastsep==NULL ? filename : lastsep+1);
if (i==0 // main module to allow static crt linking
|| _strnicmp(basename, "ucrt", 4) == 0 // new ucrtbase.dll in windows 10
|| _strnicmp(basename, "msvcr", 5) == 0) // older runtimes
|| _strnicmp(basename, "msvcr", 5) == 0) // older runtimes
{
// remember indices so we can check load order (in debug mode)
if (_stricmp(basename, MIMALLOC_NAME) == 0) mimalloc_index = i;
Expand Down
2 changes: 1 addition & 1 deletion src/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static size_t mi_os_good_alloc_size(size_t size, size_t alignment) {
#if defined(_WIN32)
// We use VirtualAlloc2 for aligned allocation, but it is only supported on Windows 10 and Windows Server 2016.
// So, we need to look it up dynamically to run on older systems.
typedef PVOID (*VirtualAlloc2Ptr)(HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER*, ULONG );
typedef PVOID (__stdcall *VirtualAlloc2Ptr)(HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER*, ULONG );
static VirtualAlloc2Ptr pVirtualAlloc2 = NULL;

void _mi_os_init(void) {
Expand Down
2 changes: 1 addition & 1 deletion test/main-override.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void free_p() {
}

int main() {
mi_stats_reset();
mi_stats_reset();
atexit(free_p);
void* p1 = malloc(78);
void* p2 = malloc(24);
Expand Down

0 comments on commit 9390642

Please sign in to comment.