Skip to content

Ensure TLS accesses don't call the global allocator through panic (part 2) - #160976

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
maxdexh:fix-thread-local-global-alloc-reenter-2
Aug 13, 2026
Merged

Ensure TLS accesses don't call the global allocator through panic (part 2)#160976
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
maxdexh:fix-thread-local-global-alloc-reenter-2

Conversation

@maxdexh

@maxdexhmaxdexh commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

View all comments

In #160934, I missed some calls that are reachable from LocalKey::with on platforms that use the racy LazyKey implementation.

How it didn't occur to me that the other functions (besides get and set) are reachable is beyond me ^^
I guess this is what I get for going through call graphs of disabled code by hand.

Yet another argument for function coloring.

r? nia-e

@rustbotrustbot added 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 Aug 12, 2026
@maxdexh

Copy link
Copy Markdown
ContributorAuthor

There are still some code paths that can panic...

@maxdexh
maxdexh marked this pull request as draft August 12, 2026 11:04
@rustbotrustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2026
@maxdexh
maxdexhforce-pushed the fix-thread-local-global-alloc-reenter-2 branch from 38e1492 to 46ec7c3CompareAugust 12, 2026 11:09
@maxdexh
maxdexh marked this pull request as ready for review August 12, 2026 11:09
@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 12, 2026
@maxdexh

Copy link
Copy Markdown
ContributorAuthor

It wasn't that bad, most of the panicking code in the modules (which are way too spread apart) was in the destructor code, which can panic anyway and isn't called on access

@nia-e

Copy link
Copy Markdown
Member

grepping for unwrap and assert in sys/thread_local also brought up:

// unwrap
os.rs106:let layout = Layout::new::<Value<T>>().align_to(ALIGN).unwrap();142:let layout = Layout::new::<Value<T>>().align_to(ALIGN).unwrap();
key/xous.rs191:.unwrap()
key/windows.rs191:let dtor = unsafe{(*cur).dtor.unwrap()};// assert
guard/key.rs53: debug_assert_eq!(state,RUN);
key/xous.rs111: assert!((*val).is_null());

are these not also possible violations of our guarantee? ^^

@maxdexh

Copy link
Copy Markdown
ContributorAuthor

As long as they can't happen, no. But I'll take a look at them

@maxdexh

maxdexh commented Aug 12, 2026

Copy link
Copy Markdown
ContributorAuthor

Note that there are also uses of indexing and addition, but to me they didn't look like they can go wrong. Maybe we should replace them anyway?

@maxdexh

Copy link
Copy Markdown
ContributorAuthor

Thinking about it, we probably should. They are essentially unchecked operations without safety comments. I'll get back to it...

@maxdexh
maxdexh marked this pull request as draft August 12, 2026 16:28
@rustbotrustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2026
@maxdexh

maxdexh commented Aug 12, 2026

Copy link
Copy Markdown
ContributorAuthor

@nia-e is there a way to test the build against all targets? I would like to slap all the panic-related deny attributes we have on the code

@nia-e

Copy link
Copy Markdown
Member

i think the only way unless you have a lot of cross-compilation set up locally is to do a bors try job with a bunch of architectures ^^ you should(?) have perms for that

@maxdexh

Copy link
Copy Markdown
ContributorAuthor

Uh I'll try (haha get it)

@maxdexh
maxdexhforce-pushed the fix-thread-local-global-alloc-reenter-2 branch from 46ec7c3 to 4385391CompareAugust 12, 2026 17:54
@maxdexh

maxdexh commented Aug 12, 2026

Copy link
Copy Markdown
ContributorAuthor

I think this is it?
I'm just gonna try a bors command from one of the rollups, pretty sure I don't have the perms though.
I also couldn't find a list of jobs anywhere so I'll just try a random one I saw on a rollup
@bors try jobs=test-various

Edit: Welp

@rust-bors

rust-borsBot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

@maxdexh: 🔑 Insufficient privileges: not in try users

@rust-log-analyzer

This comment has been minimized.

@maxdexh
maxdexhforce-pushed the fix-thread-local-global-alloc-reenter-2 branch from 4385391 to 09946ecCompareAugust 12, 2026 18:11
@maxdexh

Copy link
Copy Markdown
ContributorAuthor

Okay at least some targets are in the CI ^^

@maxdexh
maxdexhforce-pushed the fix-thread-local-global-alloc-reenter-2 branch 2 times, most recently from 464d969 to 9b1df30CompareAugust 12, 2026 18:20
@maxdexh

Copy link
Copy Markdown
ContributorAuthor

test-various was just a random job i picked up from a rollup PR, idk what that job even does

@maxdexh

Copy link
Copy Markdown
ContributorAuthor

Yeah test-various doesn't test any exotic platforms from what i can see in the job details

@rust-log-analyzer

This comment has been minimized.

@maxdexh
maxdexhforce-pushed the fix-thread-local-global-alloc-reenter-2 branch from f77d1cb to 5b71324CompareAugust 12, 2026 18:42
@maxdexh

maxdexh commented Aug 12, 2026

Copy link
Copy Markdown
ContributorAuthor

For some reason ./x clippy library/std doesn't limit clippy to just std for me.
And when I pass --target, it doesn't change the target...
That makes it impossible for me to lint on all platforms I changed here...

@rust-bors

rust-borsBot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 995aa02 (995aa02cd857529ed7195ac264ddc51c01c6afd3)
Base parent: c98d0cb (c98d0cb27cc63afdd62602a52eb4feb8a1c682dd)

@nia-e

Copy link
Copy Markdown
Member

this looks a lot better ^^ even if it's not everything, it's def meaningfully reducing the potential unwind surface. ty!

@bors r+ rollup=iffy

@rust-bors

rust-borsBot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5b71324 has been approved by nia-e

It is now in the queue for this repository.

@rust-borsrust-borsBot 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 Aug 12, 2026
@jhprattjhpratt mentioned this pull request Aug 13, 2026
rust-borsBot pushed a commit that referenced this pull request Aug 13, 2026
Rollup of 13 pull requests
Successful merges:
- #160976 (Ensure TLS accesses don't call the global allocator through panic (part 2))
- #160438 ([rustdoc] Change table odd table rows background color to not make it the same as inline code)
- #160985 (self-profile more of borrowck)
- #137858 (Add new `unused_footnote_definition` rustdoc lint)
- #159566 (Add support for generics in `offload`and remove `no_mangle` attribute)
- #160676 (rustc_public: split `def`s out of `ty`)
- #160861 (rustc_parse: suggest removing semicolon before `if` block)
- #160958 (Remove unused `#[non_exhaustive]` in library)
- #160978 (move resolve_path to Session inherent method)
- #160990 (Remove old cfg parser which is now dead code)
- #160991 (Add offload component on nightly)
- #160999 (doc changes to expect messages in process.rs)
- #161003 (Also warn if an invalid `doc` attribute is used on a macro invocation)
@maxdexh

Copy link
Copy Markdown
ContributorAuthor

Yeah I don't feel comfortable closing the issue until we have a way to auto detect such code

rust-borsBot pushed a commit that referenced this pull request Aug 13, 2026
Rollup of 13 pull requests
Successful merges:
- #160976 (Ensure TLS accesses don't call the global allocator through panic (part 2))
- #160438 ([rustdoc] Change table odd table rows background color to not make it the same as inline code)
- #160985 (self-profile more of borrowck)
- #137858 (Add new `unused_footnote_definition` rustdoc lint)
- #159566 (Add support for generics in `offload`and remove `no_mangle` attribute)
- #160676 (rustc_public: split `def`s out of `ty`)
- #160861 (rustc_parse: suggest removing semicolon before `if` block)
- #160958 (Remove unused `#[non_exhaustive]` in library)
- #160978 (move resolve_path to Session inherent method)
- #160990 (Remove old cfg parser which is now dead code)
- #160991 (Add offload component on nightly)
- #160999 (doc changes to expect messages in process.rs)
- #161003 (Also warn if an invalid `doc` attribute is used on a macro invocation)
@rust-bors
rust-borsBot merged commit deddd57 into rust-lang:mainAug 13, 2026
13 checks passed
rust-timer added a commit that referenced this pull request Aug 13, 2026
Rollup merge of #160976 - maxdexh:fix-thread-local-global-alloc-reenter-2, r=nia-e
Ensure TLS accesses don't call the global allocator through panic (part 2)
In #160934, I missed some calls that are reachable from `LocalKey::with` on platforms that use the racy `LazyKey` implementation.
How it didn't occur to me that the other functions (besides `get` and `set`) are reachable is beyond me ^^
I guess this is what I get for going through call graphs of disabled code by hand.
Yet another argument for function coloring.
r? nia-e
@rustbotrustbot added this to the 1.99.0 milestone Aug 13, 2026
@maxdexh
maxdexh deleted the fix-thread-local-global-alloc-reenter-2 branch August 13, 2026 18:08
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (004192f): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

meanrangecount
Regressions ❌
(primary)
0.4%[0.1%, 0.8%]6
Regressions ❌
(secondary)
--0
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
-5.5%[-8.9%, -1.5%]4
All ❌✅ (primary)0.4%[0.1%, 0.8%]6

Max RSS (memory usage)

Results (primary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
--0
Regressions ❌
(secondary)
--0
Improvements ✅
(primary)
-1.6%[-1.6%, -1.6%]1
Improvements ✅
(secondary)
--0
All ❌✅ (primary)-1.6%[-1.6%, -1.6%]1

Cycles

Results (primary -2.0%, secondary -3.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
--0
Regressions ❌
(secondary)
2.5%[2.5%, 2.5%]1
Improvements ✅
(primary)
-2.0%[-2.1%, -1.8%]2
Improvements ✅
(secondary)
-5.2%[-8.4%, -2.5%]4
All ❌✅ (primary)-2.0%[-2.1%, -1.8%]2

Binary size

Results (primary 0.2%, secondary -7.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

meanrangecount
Regressions ❌
(primary)
0.3%[0.1%, 0.7%]16
Regressions ❌
(secondary)
--0
Improvements ✅
(primary)
-0.2%[-0.2%, -0.0%]5
Improvements ✅
(secondary)
-7.4%[-8.8%, -6.9%]4
All ❌✅ (primary)0.2%[-0.2%, 0.7%]21

Bootstrap: 455.642s -> 457.256s (0.35%)
Artifact size: 396.40 MiB -> 396.37 MiB (-0.01%)

@rustbotrustbot added the perf-regression Performance regression. label Aug 18, 2026
@Kobzol

Copy link
Copy Markdown
Member

This caused a small regression, but also a large win on one stress test. Overall, it is mostly neutral.

@rustbot label: +perf-regression-triaged

@rustbotrustbot added the perf-regression-triaged The performance regression has been triaged. label Aug 18, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 19, 2026
…loc-reenter-3, r=nia-e
Ensure TLS accesses don't call the global allocator through panic (part 3)
Follow-up to rust-lang#160976
Missed TLS code that is in a completely different module for some reason
cc rust-lang#160930
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 19, 2026
…loc-reenter-3, r=nia-e
Ensure TLS accesses don't call the global allocator through panic (part 3)
Follow-up to rust-lang#160976
Missed TLS code that is in a completely different module for some reason
cc rust-lang#160930
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 20, 2026
…loc-reenter-3, r=nia-e
Ensure TLS accesses don't call the global allocator through panic (part 3)
Follow-up to rust-lang#160976
Missed TLS code that is in a completely different module for some reason
cc rust-lang#160930
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 20, 2026
…loc-reenter-3, r=nia-e
Ensure TLS accesses don't call the global allocator through panic (part 3)
Follow-up to rust-lang#160976
Missed TLS code that is in a completely different module for some reason
cc rust-lang#160930
rust-borsBot pushed a commit that referenced this pull request Aug 20, 2026
Rollup merge of #161068 - maxdexh:fix-thread-local-global-alloc-reenter-3, r=nia-e
Ensure TLS accesses don't call the global allocator through panic (part 3)
Follow-up to #160976
Missed TLS code that is in a completely different module for some reason
cc #160930
pullBot pushed a commit to LeeeeeeM/miri that referenced this pull request Aug 21, 2026
…er-3, r=nia-e
Ensure TLS accesses don't call the global allocator through panic (part 3)
Follow-up to rust-lang/rust#160976
Missed TLS code that is in a completely different module for some reason
cc rust-lang/rust#160930
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regressionPerformance regression.perf-regression-triagedThe performance regression has been triaged.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@maxdexh@nia-e@rust-log-analyzer@rust-timer@Kobzol@rustbot