Fix mimalloc init re-entrancy on Windows (TLS recursion + page-map)#1196
Fix mimalloc init re-entrancy on Windows (TLS recursion + page-map)#1196jinpzhanAMD wants to merge 1 commit intomicrosoft:dev3from
Conversation
b822796 to
e6c35b6
Compare
|
@microsoft-github-policy-service agree [company="{AMD}"] |
|
@microsoft-github-policy-service agree company="AMD" |
|
Hi @jinpzhanAMD -- thanks for the PR; this is tricky stuff.
|
Signed-off-by: Ross Charles C. <10781734+rossbridger@users.noreply.github.com>
Signed-off-by: Ross Charles C. <10781734+rossbridger@users.noreply.github.com>
Signed-off-by: Ross Charles C. <10781734+rossbridger@users.noreply.github.com>
|
Hi @daanx, Thank you for the review. I need to rethink this PR as it was based on the code prior to the TLS refactor implemented in commit a7f84f3. The issue this PR addressed has been resolved in the latest release (v3.2.6), which includes that commit. However, we've now encountered a different issue with v3.2.6. Could you please review this PR first: #1202? Thank you! |
|
Thank you! |
PR Description:
Problem
On Windows, accessing
__declspec(thread)storage can trigger on-demand TLS initialization (__dyn_tls_init) which may run user TLS constructors that allocate memory. During mimalloc process initialization, this can cause recursive allocations before global state (like the page map) is ready, leading to assertion failures.Root Causes
mi_prim_get_default_heap()accesses TLS, which can trigger__dyn_tls_initand user constructorsmi_process_init()can occur before_mi_page_mapis initializedmi_atomic_onceis non-blocking, allowing other threads to observe incomplete initializationSolution
This PR addresses the issues by:
MI_TLS_RECURSE_GUARD): Prevents TLS access beforemi_process_initcompletes on Windowsmi_process_init()with atomic state tracking (0=not started, 1=in progress, 2=done)Changes
include/mimalloc/prim.h: AddedMI_TLS_RECURSE_GUARDdefinition for Windowssrc/init.c: Rewrotemi_process_init()with proper synchronization (48 lines)src/page-map.c: Adjusted assertion order in_mi_page_map_register()Testing
Tested on Windows with applications that trigger TLS initialization during mimalloc init. No more assertion failures or crashes.
Compatibility