Crash when freeing memory from _strdup()
A possible MiMalloc crash to look into...
Given this simple code example, memory is allocated from the OS in the _strdup() call. It is then freed using MiMalloc. This causes MiMalloc to crash. (version 1.7.1)
While you might say, use "free" instead of "mi_free", this is an example of a larger issue. Any Windows API which allocates memory must be freed by the OS, not MiMalloc. We would want to use MiMalloc for all memory allocations, except it doesn't hook memory allocation calls. I guess one must suss out all possible conflicts like this.
temp = _strdup ("hello");
mi_free(temp);
Exception thrown at 0x00007FF6F57B7562 in mem_strdup.exe: 0xC0000005: Access violation reading location 0x0000025AD4000068.
At a minimum MiMalloc could report an error and return without freeing the memory rather then crash. Or have an option to try and free the memory to the OS when it doesn't find the pointer in it's address space.
void *p = 0x0000025ad4027960 const unsigned __int64tid = 859724070912
// Free a block
void mi_free(void* p) mi_attr_noexcept
{
00007FF6F57B7540 push rdi
00007FF6F57B7542 sub rsp,20h
00007FF6F57B7546 mov r9,rcx
00007FF6F57B7549 mov rdi,rcx
00007FF6F57B754C and r9,0FFFFFFFFFFC00000h
const mi_segment_t* const segment = mi_checked_ptr_segment(p,"mi_free");
if (mi_unlikely(segment == NULL)) return;
00007FF6F57B7553 je mi_free+9Dh (07FF6F57B75DDh)
const uintptr_t tid = _mi_thread_id();
00007FF6F57B7559 mov rdx,qword ptr gs:[30h]
mi_block_t* const block = (mi_block_t*)p;
if (mi_likely(tid == segment->thread_id && page->flags.full_aligned == 0)) { // the thread id matches and it is not a full page, nor has aligned blocks 00007FF6F57B7562 mov rax,qword ptr [r9+68h]
const uintptr_t tid = _mi_thread_id();
00007FF6F57B7566 mov qword ptr [rsp+30h],rbx
mi_page_t* const page = _mi_segment_page_of(segment, p);
00007FF6F57B756B mov rbx,rcx
00007FF6F57B756E mov rcx,qword ptr [r9+60h]
00007FF6F57B7572 sub rbx,r9
00007FF6F57B7575 shr rbx,cl
00007FF6F57B7578 shl rbx,6
00007FF6F57B757C add rbx,r9
generally speaking, inside strdup, malloc (not mi_malloc) is called freeing a pointer from malloc via mi_free will definitely crash
to avoid crash, you need to override strdup too
I understand that in the case of strdup() there should be an override. There are cases where providing an override to an API is not possible. Using strdup() was just a simple example.
The primary issue is that the MiMalloc memory allocator "crashes" when passed a pointer that is not managed by the memory allocator. Crashing is a poor response to a case like this. Can a fix be provided for the issue of the crash?
Allocator from TBB stores original free code and uses it in case a pointer passed to it was not allocated by TBB. Perhaps mimalloc can do something similar.
Hi @mm-p4, apologies for the late reply. The "mixing" of pointers is an interesting issue and there are various approaches that mimalloc supports:
-
The preferred approach is to dynamically override all allocations with only mimalloc (and not use any other allocator). This is the most robust as it will work with other libraries in your program. In particular on Windows this is important as various DLL's may link with their own C library. Now, overriding all allocation calls is not so easy and how to do it depends on your platform; see https://github.com/microsoft/mimalloc#overriding-malloc for details. The Windows override is very robust though (and applied on very large programs with many library modules) and works fine with your test (i.e.
_strdupis redirected to allocate with mimalloc andmi_freeworks fine; just tested this on Windows and Linux and in both environments your example works fine). -
Another approach is to carefully separate which allocator is used statically. This works well if you control all the code. See https://github.com/microsoft/mimalloc#static-override for details; Here using the
mimalloc-override.hfile can help a lot to statically redefine all allocations/frees to use mimalloc. -
The trouble with static overriding (in 2) is that you may get in the situation you showed: allocate using some other allocator and free with mimalloc. In that case, mimalloc does have
mi_cfree(checked free) that checks if this was a valid pointer. This is sometimes used on macOS but it is not ideal as it can have a performance impact. Dynamic overriding (1) is in that sense more robust.
Note that most of this is not mimalloc specific: any custom allocator suffers from this problem. (mimalloc does have a very good Windows override though which is not common; *nix works well with most allocators using LD_PRELOAD, macOS has zones which are recently supported by mimalloc).
Finally, if you choose static overriding, the main calls to watch out for strdup and realpath but there are not many other calls (fortunately) that allocate memory in the standard library. If you look in mimalloc-override.h you see an extensive list of such functions.
Hope this helps, Best
Thank you for the reply. In the short run it seems mi_cfree() is a good option to avoid the crash. Would you consider adding some sort of error reporting to mi_cfree() so that when a non MiMalloc block of memory is not freed, it would be reported?
Thanks for the suggestion; I think I should add that to the debug build.
It might be better if this was a runtime message. possibly gated by mi_option_show_errors. This shouldn't be a performance loss as it should rarely occur. Although when it does it will most likely be at a customers cite and they would not be running a debug build.
By the way, it would be nice to have a mi_cfree_size() as well.
Thanks