Panic when the global allocator tries to register a TLS destructor by joboet · Pull Request #116402 · rust-lang/rust

@rustbot rustbot added O-hermit

Operating System: Hermit

O-solid

Operating System: SOLID

O-unix

Operating system: Unix-like

O-windows

Operating system: Windows

S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-libs

Relevant to the library team, which will review and decide on the PR/issue.

labels

Oct 3, 2023

@joboet

thomcc

@joboet

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Oct 4, 2023

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Oct 5, 2023
…s, r=thomcc

Panic when the global allocator tries to register a TLS destructor

Using a `RefCell` avoids the undefined behaviour encountered in rust-lang#116390 and reduces the amount of `unsafe` code in the codebase.

bors added a commit to rust-lang-ci/rust that referenced this pull request

Oct 5, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#116223 (Fix misuses of a vs an)
 - rust-lang#116296 (More accurately point to where default return type should go)
 - rust-lang#116402 (Panic when the global allocator tries to register a TLS destructor)
 - rust-lang#116429 (Diagnostics: Be more careful when suggesting struct fields)
 - rust-lang#116431 (Tweak wording of E0562)
 - rust-lang#116432 (rustdoc: rename `issue-\d+.rs` tests to have meaningful names (part 2))

r? `@ghost`
`@rustbot` modify labels: rollup

@bors bors added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

labels

Oct 5, 2023

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Oct 5, 2023

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Oct 18, 2023

@bors bors mentioned this pull request

Oct 19, 2023

@lqd lqd mentioned this pull request

Oct 19, 2023

@joboet joboet deleted the global_alloc_tls_unsoundness branch

October 19, 2023 11:55

bors-ferrocene bot added a commit to ferrocene/ferrocene that referenced this pull request

Oct 31, 2023

bors-ferrocene bot added a commit to ferrocene/ferrocene that referenced this pull request

Oct 31, 2023

@orlp orlp mentioned this pull request

Jul 11, 2025

@orlp orlp mentioned this pull request

Jul 25, 2025

bors added a commit that referenced this pull request

Aug 23, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes #115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as #116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

bors added a commit that referenced this pull request

Oct 21, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes #115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as #116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

bors added a commit that referenced this pull request

Nov 28, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes #115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as #116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

bors added a commit that referenced this pull request

Nov 29, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes #115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as #116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

github-actions bot pushed a commit to rust-lang/miri that referenced this pull request

Nov 30, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes rust-lang/rust#115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as rust-lang/rust#116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request

Dec 1, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes rust-lang/rust#115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as rust-lang/rust#116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

github-actions bot pushed a commit to rust-lang/rust-analyzer that referenced this pull request

Dec 1, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes rust-lang/rust#115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as rust-lang/rust#116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

Kobzol pushed a commit to Kobzol/rustc_codegen_gcc that referenced this pull request

Dec 21, 2025
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes rust-lang/rust#115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as rust-lang/rust#116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`

github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request

Jan 20, 2026
…crum

Allow the global allocator to use thread-local storage and std::thread::current()

Fixes rust-lang#115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as rust-lang#116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`