Skip to content

Synchronize TLS and FLS slot retirement and reuse - #6

Closed
GhostBassist wants to merge 1 commit into
cyanide-burnout:mainfrom
GhostBassist:codex/provider-slot-lifecycle
Closed

GhostBassist wants to merge 1 commit into
cyanide-burnout:mainfrom
GhostBassist:codex/provider-slot-lifecycle

Conversation

@GhostBassist

Copy link
Copy Markdown

Codex Worker submission.

Closes #3.

  • Serialize allocation, release, lookup and mutation in the Kernel32 provider. Clear retired TLS slots across prepared TEBs before reuse, including for DLL code that reads the TEB directly. Tag fallback TLS and FLS values with their allocation generation so old values cannot reappear when the numeric index is reused.
  • Claim the FLS value and its callback under the same lock, then invoke the callback after releasing the lock. Thread teardown only dispatches values from live allocations. A callback can reenter the provider without deadlocking.
  • Preserve the documented provider scope. FLS remains per Linux thread, with callbacks on prepared-thread exit and for the calling thread on FlsFree. Other threads’ retired values are ignored.

Verification

  • Barrier-based tests cover cross-thread reuse with and without a TEB, direct TEB reads, suppression of stale callbacks, and callback ownership for replacement values.
  • The new tests fail against the upstream baseline. The patched provider passed 20 consecutive runs.
  • make check passed on Ubuntu under WSL, including all 121 loader checks.

@cyanide-burnout

Copy link
Copy Markdown
Owner

Thanks for the analysis, the generations of FLS values are the right idea. Not taking it as is, for two reasons: a global mutex on every TlsGetValue/FlsGetValue puts all threads of a host on one lock in the hot path of the CRT (on Windows these are lock-free reads of the TEB; using a freed index is a bug of the DLL there too), and ClearLibraryThreadTlsSlot moves a concern of the provider into the API of the loader, which knows nothing about emulated DLLs by design. The case matters only when a DLL with its own CRT is freed while threads that called it are alive, so it stays in Documentation/TODO.md. The two fixes in PrepareLibraryThread (the result of pthread_key_create, pthread_setspecific before ARCH_SET_GS and checked) are taken in 7e7add3.

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.

Track concurrent TLS and FLS slot release and reuse

2 participants