Crash in mi_segment_init
We pulled memory modules from a machine to limit the amount of available memory. Then we tested our server process which would intentionally run out of memory.
The mimalloc library crashed in mi_segment_init() on the line "segment->memid = memid". Apparently segment was not NULL, and it was not a valid memory location.
Exception: dwFirstChance 1 2021/10/19 12:27:39 dwProcessId 1156 dwThreadId 6356 ExceptionCode '0xc0000005: EXCEPTION_ACCESS_VIOLATION' ExceptionFlags 'EXCEPTION_CONTINUABLE' ExceptionAddress 0x00007ff74466b133 Reason 'write' Data Address 0x0000058852c00000
Still single stepping this code in the debugger to make sense of this.
ah interesting -- it is hard to debug out of memory situations. I think I know what happens here though: the segment->memid is the first location getting written after memory is allocated; on systems with overcommit (like Linux) we can allocate the memory but on the first access there may be no more swap or physical memory available resulting in a page fault. I guess that is what you are seeing here. If you disable overcommit (in the OS), a NULL should be returned as the initial OS allocation will fail.
Going to close this for now as this is really an OS overcommit issue. I'll add a comment in segment_init to point this out.
If the mimalloc memory manager is expected to be used in a production environment at customer sites, it is not reasonable to leave areas of vulnerability which can cause an application crash. It is also unreasonable to think that a computer will not run out of memory, and when this happens, crashing in a memory manager is a reasonable result.
Why wasn't a pointer check added as this problem had been clearly defined?
Yes, I agree with you. Perhaps I misunderstood your situation. Just to clarify:
- If you are using Linux with overcommit enabled, then you can get an access error on a first access if the memory is just not available. This usually happens at the segment->memid assignment as that is the first locations to access. This kind of error cannot be fixed by mimalloc or even checked for -- it is how the OS works. You can change this by disabling overcommit for example. This is what I thought your situation is.
- However, on other systems without overcommit (like Windows) we should never see this. In that case it could be that this is a bug in mimalloc where the segment memory is somehow not committed or still not accessible. This can be the case on Windows where reserved memory needs to be committed first. When I look again at your report, it seems you are on Windows? What version of mimalloc are you using? Can you upgrade to v2.0.3 (or v1.7.3) and try again -- can you repro this?
Reopening the issue for now :-)
Yes this is a problem we had mostly on Windows.
Setting the vm.overcommit_memory=2 on Linux has helped resolved the issue on that platform.
We also tried using mimalloc eager commit = 0 on Windows. There was still a crash when we tested this configuration. Although I think the fault was slightly different.
We are currently using mimalloc 1.7.2. We can try testing on 1.7.3 to see if we can repro the issue.
We are performing acceptance testing on 2.0.3. Our concern here is that mimalloc configurations and internal operations can change with a new major release. We are seeing different memory usage patterns release to release. Is there some documentation we can review to help in this area?
Thanks!
Ah, it sounds like a real bug in mimalloc in the commit state of the segments. This will probably not happen on v2.0.3 as that uses another mechanism so I am very interested to hear your v1.7.3 vs. v2.0.3 experience.
On the other hand, v1 is used widely on many many machines and services 24/7 and we have not seen this error - it must be a quite special situation for it to occur although you trigger it by running out of memory right? it seems from your report that it is not deterministic? If it can be repro'd easily let me know -- perhaps we can set up a meeting and debug. I will also review the code for this and see if I can spot anything.
A customer of us also ran into this problem. For us, it is definitely reproducible. We use mimalloc 1.7.5.
I have tried to extract a minimal example, see the attached VS 2022 solution. When i run that program in a powershell it reports an access violation after some time. Unfortunately, since the program eats up all memory, it also causes other currently running programs to crash/exit, like VS 2022, so i cant attach with the debugger.
P.s. make sure to copy the mimalloc dlls into the build output directory before running the exe.
Edit: After running the debug build for 15 min in the debugger (64 GB RAM machine), the debugger finally reported an access violation at:
mimalloc-override.dll!mi_segment_init(mi_segment_s * segment, unsigned __int64 required, mi_page_kind_e page_kind, unsigned __int64 page_shift, mi_segments_tld_s * tld, mi_os_tld_s * os_tld) Line 647
It seems the mi_segment_s * is invalid: 0x0000060d5e400000 {memid=??? mem_is_pinned=??? mem_is_committed=??? ...}
which is just outside readable memory:
0x0000060D5E3FFFE5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...........................
0x0000060D5E400000 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ...........................