-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix mimalloc init re-entrancy on Windows (TLS recursion + page-map) #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev3
Are you sure you want to change the base?
Fix mimalloc init re-entrancy on Windows (TLS recursion + page-map) #1196
Conversation
|
@jinpzhanAMD please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
b822796 to
e6c35b6
Compare
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