Skip to content

bugfix: add theap->tld null check - #1289

Merged
Daan (daanx) merged 1 commit into
microsoft:dev3from
p2kyc0ng:p/yc0ng/bugfix_theap_tld_null
Jun 22, 2026
Merged

Daan (daanx) merged 1 commit into
microsoft:dev3from
p2kyc0ng:p/yc0ng/bugfix_theap_tld_null

Conversation

@p2kyc0ng

Copy link
Copy Markdown

related to issues 1288

possible trigger condition:

after _mi_thread_done (the pthread destructor) completes execution on a thread, the same thread continues to execute other destructors or callbacks. this triggers a mi_free on an abandoned page, which then enters mimalloc re-entrantly via __emutls_get_address → malloc. at this point, the theap returned by _mi_theap_default() is already in a corrupted/invalid state.

changes:

added a temporary null check for theap->tld. under this edge case, the allocator can safely fall back to subproc_main.

@daanx
Daan (daanx) merged commit f47209e into microsoft:dev3 Jun 22, 2026
1 check passed
@daanx

Copy link
Copy Markdown
Collaborator

Thanks! (I am not fully convinced this should be needed and we should prevent entering re-initialization in the first place, but it cannot hurt to be defensive in this case :-) )

Daan (daanx) pushed a commit that referenced this pull request Sep 5, 2026
…et set

_mi_subproc() defends against partially-initialized thread state (issue #1289)
by checking theap and theap->tld, but not the third member of the same
initialization progression: a window where the theap and its tld exist while
tld->subproc has not been set yet.  In that window it returns NULL instead of
falling back to _mi_subproc_main(), and the first caller that dereferences the
result for stat accounting crashes (observed: a malloc-override shared library
whose constructor allocates before process init completes faults at
offsetof(mi_subproc_t, stats.commit_calls) in
mi_subproc_stat_counter_increase(NULL, commit_calls, 1)).

Extend the guard with the missing disjunct so the existing fallback covers the
whole window it already defends; behavior for initialized threads is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
Tyler (tyler274) pushed a commit to tyler274/ElyMalloc that referenced this pull request Sep 17, 2026
…et set

_mi_subproc() defends against partially-initialized thread state (issue microsoft#1289)
by checking theap and theap->tld, but not the third member of the same
initialization progression: a window where the theap and its tld exist while
tld->subproc has not been set yet.  In that window it returns NULL instead of
falling back to _mi_subproc_main(), and the first caller that dereferences the
result for stat accounting crashes (observed: a malloc-override shared library
whose constructor allocates before process init completes faults at
offsetof(mi_subproc_t, stats.commit_calls) in
mi_subproc_stat_counter_increase(NULL, commit_calls, 1)).

Extend the guard with the missing disjunct so the existing fallback covers the
whole window it already defends; behavior for initialized threads is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants