microsoft / microsoft/mimalloc
Issues with MinGW-built mimalloc DLL
- Dominant language
- C
- Stars
- 13.4k
- Forks
- 1.2k
- Avg merge
- 4d 45m
- Merged PRs (30d)
- 13
Description
I have been working on replacing malloc with mimalloc for [OpenSCAD](https://github.com/openscad/openscad) on all our various supported platforms. So far there have been great improvements to performance on Linux and Mac, but I have not been able to get something fully working on Windows.
Most recently I attempted the recommended method of dynamic override / redirect with DLLs.
Our builds which target Windows use [MXE](https://mxe.cc/) to cross compile from Linux This creates a mingw64/gcc based toolchain. Our application is built with cmake, with mimalloc included as a git submodule (using cmake's `add_subdirectory()`) from our project's CMakeLists.txt
It successfully builds a DLL and links our application to it, but when I run the program, there are a number of issues:
1) No console output. I have tried setting `MIMALLOC_VERBOSE`, `MIMALLOC_SHOW_STATS`, etc. and am unable to get any output from the mimalloc library on Windows (from any of cmd.exe, powershell or even git Bash from Git for Windows).
- Our application already has to jump through some hoops to get console output, since openscad can be run either as a GUI or non-interactively from the command line (i.e. if given an input and output filename from the command line). So our main `openscad.exe` is built with `/SUBSYSTEM:WINDOWS` and a separate `openscad.com` file (actually a renamed .exe) is built with `/SUBSYSTEM:CONSOLE`. The console app is just a wrapper that opens the exe and forwards stdout etc.
So I'm wondering if mimalloc is doing anything special with its console output that would conflict with this?
- Oddly, I *am* able to see some console output from mimalloc, when run on Linux using `wine-7.0`, but this comes with some warnings and an error, bringing us to the next point.
2) Standard malloc is _not_ redirected! (on wine at least)
Here is a log from wine, which is the only way I was able to see any console messages from mimalloc:
```
$ MIMALLOC_VERBOSE=1 MIMALLOC_SHOW_STATS=1 wine ./openscad.com
mimalloc: option 'show_errors': 0
mimalloc: option 'show_stats': 1
mimalloc: option 'eager_commit': 1
mimalloc: option 'eager_region_commit': 0
mimalloc: option 'reset_decommits': 1
mimalloc: option 'large_os_pages': 0
mimalloc: option 'reserve_huge_os_pages': 0
mimalloc: option 'reserve_huge_os_pages_at': -1
mimalloc: option 'reserve_os_memory': 0
mimalloc: option 'segment_cache': 0
mimalloc: option 'page_reset': 1
mimalloc: option 'abandoned_page_reset': 0
mimalloc: option 'segment_reset': 0
mimalloc: option 'eager_commit_delay': 1
mimalloc: option 'reset_delay': 100
mimalloc: option 'use_numa_nodes': 0
mimalloc: option 'limit_os_alloc': 0
mimalloc: option 'os_tag': 100
mimalloc: option 'max_errors': 16
mimalloc: option 'max_warnings': 16
mimalloc: process init: 0x3e0000
mimalloc: secure level: 0
mimalloc-redirect: warning: unable to resolve "ucrtbase.dll!_expand_base" -- enabling MIMALLOC_PATCH_IMPORTS to prevent allocation errors.
mimalloc-redirect: warning: unable to resolve "ucrtbase.dll!_recalloc_base" -- enabling MIMALLOC_PATCH_IMPORTS to prevent allocation errors.
mimalloc-redirect: warning: unable to resolve "ucrtbase.dll!_msize_base" -- enabling MIMALLOC_PATCH_IMPORTS to prevent allocation errors.
mimalloc-redirect: error: mimalloc-redirect.dll seems to be initialized after ucrtbase.dll
(hint: try to link with 'mimalloc-override.lib' earlier on the command line?)
mimalloc-redirect: warning: standard malloc is _not_ redirected! -- using regular malloc/free
0110:fixme:heap:GetNumaHighestNodeNumber semi-stub: 0000000023A2F7CC
mimalloc: using 1 numa regions
```
3) I keep seeing references to `mimalloc-override.dll`; In documentation, the warnings above, and various github issue comments, but cmake never generates such a file.
In fact it was generating a `libmimalloc.dll` for me, and when I ran the build I got a message which said something like "redirect: unable to find module".
It may just be a unique consequence of us cross-compiling from linux, but I basically ended up adding this to our cmake, just before `add_subdirectory(mimalloc)`
```cmake
if(WIN32)
set(CMAKE_SHARED_LIBRARY_PREFIX "") # avoid "lib" prefix
endif()
```
Now it generates `mimalloc.dll` and links to that, and instead of "unable to find module" I see the messages which I pasted above for point 2, but... **does it actually need to be named:** `mimalloc-override.dll` for override to work?
4) Despite the dynamic override/redirect not working, I still do a manual override of allocation functions specifically for [GMP](https://gmplib.org/manual/Custom-Allocation). When I run an interactive session, with Qt based GUI, it functions ok, but upon closing I often get a crash, pasted below. (This may just be a bug in Wine? I don't recall if I got similar crashes on native windows, I didn't do much testing there due to the lack of console output)
```
Unhandled exception: page fault on write access to 0x0000000e1637304d in 64-bit code (0x000000017002b098).
Register dump:
rip:000000017002b098 rsp:000000000572a730 rbp:0000000006c40000 eflags:00010206 ( R- -- I - -P- )
rax:00000006c401c000 rbx:0000000000000002 rcx:0000000000110000 rdx:0000000e16373045
rsi:0000000000000068 rdi:0000000000000068 r8:0000000000000068 r9:0000000000000018 r10:0000000000000110
r11:0000000000000002 r12:0000000006c401d7 r13:000000000000005a r14:0000000006c40000 r15:0000000000000000
Stack dump:
0x000000000572a730: 000000000572a860 000000000572a820
0x000000000572a740: 000000000572a850 000000000572a800
0x000000000572a750: 000000000572a880 0000000000000000
0x000000000572a760: 0000000000000001 000000006c265260
0x000000000572a770: 0000000000000000 0000000006c40078
0x000000000572a780: 0000000000000021 000000000000005a
0x000000000572a790: 0000000000000008 0000000000000000
0x000000000572a7a0: 0000000000000018 000000000572a9d0
0x000000000572a7b0: 000000000572a9b0 000000000572a9b0
0x000000000572a7c0: 000000000572a9d0 000000006a0a5104
0x000000000572a7d0: 0000000000000002 0000000000000028
0x000000000572a7e0: 0000000000000028 0000000000000002
Backtrace:
=>0 0x000000017002b098 list_remove+0x8(elem=, elem=) [Z:\usr\src\packages\BUILD\include\wine\list.h:100] in ntdll (0x0000000006c40000)
1 0x000000017002b098 RtlAllocateHeap+0xa0(heap=, flags=, size=) [Z:\usr\src\packages\BUILD\dlls\ntdll\heap.c:1709] in ntdll (0x0000000006c40000)
2 0x000000006a0a5104 malloc+0x14(size=) [Z:\usr\src\packages\BUILD\dlls\msvcrt\heap.c:432] in msvcrt (0x0000000000000018)
3 0x00000000015676ca pthread_rwlockattr_setpshared+0x39d0ea(a=, s=) in openscad (0x0000000000000018)
4 0x0000000001ef3325 pthread_rwlockattr_setpshared+0xd28d45(a=, s=) in openscad (0x000004d9cdcbd650)
5 0x0000000001e5d35b pthread_rwlockattr_setpshared+0xc92d7b(a=, s=) in openscad (0x000004d9cdcbd650)
6 0x0000000001eedf12 pthread_rwlockattr_setpshared+0xd23932(a=, s=) in openscad (0x000000000572a980)
7 0x00000000005c6d4c atexit+0x1c585c(func=) in openscad (0x000000000572a980)
8 0x00000000005d4a48 atexit+0x1d3558(func=) in openscad (0x000000000000000e)
9 0x0000000000405181 atexit+0x3c91(func=) in openscad (0x000000000000000e)
10 0x00000000021220bb pthread_rwlockattr_setpshared+0xf57adb(a=, s=) in openscad (0x000000000000000e)
11 0x00000000015e8f27 pthread_rwlockattr_setpshared+0x41e947(a=, s=) in openscad (0x0000000000000000)
12 0x00000000005ce0b4 atexit+0x1ccbc4(func=) in openscad (0x000000000572b410)
13 0x000000000048fd35 atexit+0x8e845(func=) in openscad (0x000000000572b410)
14 0x0000000000490559 atexit+0x8f069(func=) in openscad (0x000000000572b748)
15 0x00000000012f3fd2 pthread_rwlockattr_setpshared+0x1299f2(a=, s=) in openscad (0x000000000572b748)
16 0x00000000004d6b7f atexit+0xd568f(func=) in openscad (0x00000000004d62c0)
17 0x00000000004d5f2c atexit+0xd4a3c(func=) in openscad (0x00000000004d62c0)
18 0x00000000005e02d7 atexit+0x1dede7(func=) in openscad (0x000000000572b860)
19 0x0000000000404ff6 atexit+0x3b06(func=) in openscad (0x000004d9eca67fc0)
20 0x0000000001ee20ab pthread_rwlockattr_setpshared+0xd17acb(a=, s=) in openscad (0x000004d9eca67fc0)
21 0x0000000001f3981b pthread_rwlockattr_setpshared+0xd6f23b(a=, s=) in openscad (0x000004d9eca67fc0)
22 0x00000000015e531b pthread_rwlockattr_setpshared+0x41ad3b(a=, s=) in openscad (0x000004d9ca8c0020)
23 0x00000000019a7e2e pthread_rwlockattr_setpshared+0x7dd84e(a=, s=) in openscad (0x000004d9ca8c0020)
24 0x0000000001652d63 pthread_rwlockattr_setpshared+0x488783(a=, s=) in openscad (0x000004d9ca880a24)
25 0x00000000005eafb4 atexit+0x1e9ac4(func=) in openscad (0x000004d9ca880a24)
26 0x000000000184b74e pthread_rwlockattr_setpshared+0x68116e(a=, s=) in openscad (0x000004d9ca880a24)
27 0x0000000001b52887 pthread_rwlockattr_setpshared+0x9882a7(a=, s=) in openscad (0x000004d9ca880a24)
28 0x0000000001bedc0d pthread_rwlockattr_setpshared+0xa2362d(a=, s=) in openscad (0x000004d9ca820eb8)
29 0x0000000001a817c3 pthread_rwlockattr_setpshared+0x8b71e3(a=, s=) in openscad (0x000004d9ca820eb8)
30 0x0000000001bedbf4 pthread_rwlockattr_setpshared+0xa23614(a=, s=) in openscad (0x000004d9ca820eb8)
31 0x000000000157619a pthread_rwlockattr_setpshared+0x3abbba(a=, s=) in openscad (0x000004d9ca820eb8)
32 0x000000000184f0f7 pthread_rwlockattr_setpshared+0x684b17(a=, s=) in openscad (0x000000000572f370)
33 0x000000000040f727 atexit+0xe237(func=) in openscad (0x000000000572f370)
34 0x00000000026e0f54 pthread_rwlockattr_setpshared+0x1516974(a=, s=) in openscad (0x000000000572f9a0)
35 0x00000000004013c7 __tmainCRTStartup+0x237() in openscad (0x0000000006c41b30)
36 0x00000000004014c6 WinMainCRTStartup+0x16() in openscad (0x0000000000000000)
37 0x000000007b62d389 BaseThreadInitThunk+0x9(unknown=, entry=, arg=) [Z:\usr\src\packages\BUILD\dlls\kernel32\thread.c:61] in kernel32 (0x0000000000000000)
38 0x000000017005eed3 __wine_pop_frame(unknown=, entry=, arg=) [Z:\usr\src\packages\BUILD\include\wine\exception.h:273] in ntdll (0x0000000000000000)
39 0x000000017005eed3 RtlUserThreadStart+0x83(entry=[, arg=[) [Z:\usr\src\packages\BUILD\dlls\ntdll\thread.c:240] in ntdll (0x0000000000000000)
0x000000017002b098 RtlAllocateHeap+0xa0 [Z:\usr\src\packages\BUILD\dlls\ntdll\heap.c:1709] in ntdll: movq %rax,0x0000000000000008(%rdx)
Unable to access file 'Z:\usr\src\packages\BUILD\dlls\ntdll\heap.c'
Modules:
Module Address Debug info Name (51 modules)
PE 0000000000130000-0000000000151000 Deferred nsi
PE 0000000000160000-0000000000200000 Deferred netapi32
PE 0000000000400000-0000000004f2a000 Dwarf-2-4 openscad
PE 0000000005750000-0000000005d15000 Deferred ole32
PE 0000000005d20000-0000000005eb7000 Deferred combase
PE 0000000005ec0000-0000000006103000 Deferred setupapi
PE 0000000006110000-0000000006391000 Deferred mf
PE 00000000063a0000-00000000065bf000 Deferred mfplat
PE 00000000065c0000-00000000065ea000 Deferred userenv
PE 00000000065f0000-00000000066a7000 Deferred uxtheme
PE 00000000066b0000-0000000006874000 Deferred winmm
PE 0000000006880000-00000000068af000 Deferred wtsapi32
PE 0000000061300000-000000006138e000 Deferred iphlpapi
PE 0000000061540000-000000006166b000 Deferred advapi32
PE 0000000062dc0000-000000006307e000 Deferred rpcrt4
PE 0000000063280000-00000000632a0000 Deferred version
PE 00000000639c0000-0000000063a15000 Deferred shcore
PE 0000000064840000-0000000064990000 Deferred shlwapi
PE 0000000064cc0000-00000000651dd000 Deferred oleaut32
PE 0000000065380000-000000006559e000 Deferred evr
PE 0000000066040000-0000000066088000 Deferred bcrypt
PE 0000000066540000-0000000066576000 Deferred rtworkq
PE 0000000066900000-0000000066960000 Deferred win32u
PE 0000000067580000-0000000067625000 Deferred glu32
PE 0000000067640000-0000000067652000 Deferred psapi
PE 0000000067a80000-0000000067c29000 Deferred d3d9
PE 0000000067cc0000-0000000068bcc000 Deferred shell32
PE 0000000069740000-00000000697c7000 Deferred propsys
PE 0000000069800000-000000006983b000 Deferred wintab32
PE 0000000069e40000-0000000069eb8000 Deferred msacm32
PE 000000006a080000-000000006a39a000 Dwarf-4 msvcrt
PE 000000006ba00000-000000006bacf000 Deferred sechost
PE 000000006bc40000-000000006bf5c000 Deferred dwrite
PE 000000006c240000-000000006c30e000 Deferred libmimalloc
PE 000000006c7c0000-000000006ca1e000 Deferred gdi32
PE 000000006d580000-000000006d61d000 Deferred ws2_32
PE 000000006dc80000-000000006dc8f000 Deferred api-ms-win-core-synch-l1-2-0
PE 000000006e2c0000-000000006e308000 Deferred dnsapi
PE 000000006e6c0000-000000006ea2b000 Deferred crypt32Jujutsu Kaisen
PE 000000006ea40000-000000006ea60000 Deferred dwmapi
PE 000000006eb00000-000000006f41d000 Deferred user32
PE 0000000070940000-0000000070cd4000 Deferred ucrtbase
PE 0000000071000000-000000007105d000 Deferred imm32
PE 00000000710c0000-0000000071128000 Deferred dxva2
PE 000000007a850000-000000007a854000 Deferred opengl32
PE 000000007b000000-000000007b3cf000 Deferred kernelbase
PE 000000007b600000-000000007b964000 Dwarf-4 kernel32
PE 0000000170000000-0000000170342000 Dwarf-4 ntdll
PE 0000000180000000-000000018001c000 Deferred mimalloc-redirect
PE 00007f6fd6d10000-00007f6fd6d14000 Deferred winex11
PE 00007f6fd7710000-00007f6fd7714000 Deferred wined3d
Threads:
process tid prio (all id:s are in hex)
00000020 start.exe
00000024 0
00000038 services.exe
0000003c 0
00000040 0
00000054 0
00000070 0
00000088 0
000000d0 0
00000158 0
00000044 plugplay.exe
00000048 0
0000005c 0
00000060 0
00000064 0
0000004c explorer.exe
00000050 0
000000b4 0
000000b8 0
00000068 winedevice.exe
0000006c 0
00000074 0
00000078 0
0000007c 0
00000184 0
00000080 winedevice.exe
00000084 0
0000008c 0
00000090 0
00000094 0
00000098 0
000000c0 rpcss.exe
000000c4 0
000000dc 0
000000e0 0
000000e4 0
000000e8 0
000000ec 0
00000150 winedevice.exe
00000154 0
00000160 0
00000164 0
00000168 0
00000188 conhost.exe
0000018c 0
00000190 (D) Z:\home\hans\Desktop\OpenSCAD-2022.01.26-x86-64\openscad.exe
00000194 0 <==
000001c4 0
000001dc 0
System information:
Wine build: wine-7.0
Platform: x86_64
Version: Windows 7
Host system: Linux
Host version: 5.13.0-27-generic
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the project’s CMakeLists.txt and reproduce the shown wine command using the MinGW-built Windows DLL. Trace how the redirect DLL and import library are selected, including the console and GUI entry points. Done means the Windows setup’s allocation redirection and diagnostics are reproducible, or the limitation is narrowed to a confirmed failure mode.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, cmake
- Domain
- backend, build-system
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100