Skip to content

std: don't overflow when rounding up huge thread stack sizes on unix - #165703

Closed
lazerg wants to merge 3 commits into
rust-lang:mainfrom
lazerg:fix/issue-165683-stack-size-overflow
Closed

lazerg wants to merge 3 commits into
rust-lang:mainfrom
lazerg:fix/issue-165683-stack-size-overflow

Conversation

@lazerg

@lazerg lazerg commented Nov 9, 2026

Copy link
Copy Markdown

When pthread_attr_setstacksize rejects the requested size, the unix Thread::new rounds it up to the page size and tries again, but that rounding overflows for sizes close to usize::MAX. With overflow checks on this panics inside spawn, and without them the wrapped value only fails by luck. This uses checked_next_multiple_of and returns the existing "invalid stack size" error when the rounding overflows.

Fixes #165683

@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Any special-casing of Miri in the standard library requires review.

cc @rust-lang/miri

@rustbot rustbot added A-thread Area: `std::thread` 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 Nov 9, 2026
@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

r? @nia-e

rustbot has assigned @nia-e.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 38 candidates
  • Random selection from JohnTitor, LawnGnome, Mark-Simulacrum, maxdexh, nia-e

@maxdexh maxdexh assigned maxdexh and unassigned nia-e Nov 9, 2026
@maxdexh

maxdexh commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Thanks! This was exactly the fix I had in mind. Let's see if CI passes

@bors r+ rollup

@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 8fc9d05 has been tentatively approved by maxdexh

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot 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 Nov 9, 2026
@maxdexh

maxdexh commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Interesting that miri just entirely ignores the stack size argument, even if it's impossibly large ^^

Not sure what could be done here on their side though, other than maybe enforce some max size.

Comment thread library/std/src/thread/tests.rs Outdated

// Test that a stack size too large to round up to the page size is an error.
#[cfg(all(unix, not(any(target_os = "espidf", target_os = "nuttx"))))]
#[cfg_attr(miri, ignore)] // Miri ignores the requested stack size

@RalfJung RalfJung Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this a FIXME that points to rust-lang/miri#5594

View changes since the review

@RalfJung RalfJung Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW in the future this should use #165117 but that's not been approved yet.

@RalfJung

RalfJung commented Nov 9, 2026

Copy link
Copy Markdown
Member

@bors r-

@rust-bors rust-bors Bot 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 Nov 9, 2026
@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

View changes since this unapproval

}

// Test that a stack size too large to round up to the page size is an error.
#[cfg(all(unix, not(any(target_os = "espidf", target_os = "nuttx"))))]

@maxdexh maxdexh Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a comment explaining that this mirrors the cfgs in the function while you're fixing ralf's? ^^

View changes since the review

@maxdexh maxdexh left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to comment something about the FIXME(https://github.com/rust-lang/miri/issues/5594): being too long, but apparently this is consistent with the rest of std 🫠

@bors r+ rollup

View changes since this review

@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f7a6486 has been tentatively approved by maxdexh

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot 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 Nov 9, 2026
@oli-obk oli-obk closed this Nov 9, 2026
@rust-bors rust-bors Bot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 9, 2026
@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Nov 9, 2026
@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved due to being closed.

@maxdexh

maxdexh commented Nov 9, 2026

Copy link
Copy Markdown
Member

@oli-obk can I reopen and approve this? The author got blocked, but I don't see a reason for me to open essentially the exact same PR

@maxdexh

maxdexh commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Actually this can use the cfg flag from #165117 after it gets merged, ig i'll just do that tomorrow..

@programskillforverification

Copy link
Copy Markdown
Contributor

Actually this can use the cfg flag from #165117 after it gets merged, ig i'll just do that tomorrow..

@maxdexh I'm the author of #165683. If you haven't started on it yet, I'd be happy to open the new PR. If you'd rather do it yourself, that's totally fine too. Just let me know so we don't duplicate work.

@maxdexh

maxdexh commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

@programskillforverification Feel free to, and assign me as reviewer with r? maxdexh :D
I was just expecting to have to do it myself because issues with closed PRs are often not picked up again ^^

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 11, 2026
…hread-stack-size, r=maxdexh

std: don't overflow when rounding up huge thread stack sizes on unix

This is a resubmission of rust-lang#165703 by lazerg.

Fixes rust-lang#165683

r? maxdexh
rust-bors Bot pushed a commit that referenced this pull request Nov 11, 2026
Rollup merge of #165746 - programskillforverification:huge-thread-stack-size, r=maxdexh

std: don't overflow when rounding up huge thread stack sizes on unix

This is a resubmission of #165703 by lazerg.

Fixes #165683

r? maxdexh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-thread Area: `std::thread` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant 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.

thread::Builder::stack_size(usize::MAX).spawn() panics with "attempt to add with overflow" on Apple targets

7 participants