Skip to content

std: refactor UNIX stack overflow code - #162296

Draft
joboet wants to merge 4 commits into
rust-lang:mainfrom
joboet:refactor_stack_overflow
Draft

joboet wants to merge 4 commits into
rust-lang:mainfrom
joboet:refactor_stack_overflow

Conversation

@joboet

@joboet joboet commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

The idea here is to split the code up into two main areas:

  • The actual stack overflow handling involving signals/exceptions
  • Guard page installing, itself split into three functions:
    • install_main_guard installs the main thread's guard page
    • find_main_guard (new) finds the guard page the system has set up for the main thread (this was previously mashed in with install_main_guard)
    • current_guard finds the guard page of the current (non-main) thread

Splitting the guard page stuff out will allow us to enable the soundness-critical guard page install logic even if the system doesn't support signals; and also makes it easier to work on the individual areas in parallel.

Unfortunately this isn't as simple as moving the code around: the current code caches the system's page size in a PAGE_SIZE atomic, but that atomic is not always accessed. Thus there are some non-functional changes here in regards to how the page size is passed around. Also, since the guard page code now needs to compile on all systems, the cfg!()-based switching used to simplify cross-platform code checking would necessitate some stub implementations, so I've switched to proper cfg_select! based logic.

These kinds of code moves don't display nicely in the diff unfortunately, let me know if there's anything I can do to help with the review!

@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

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

cc @rust-lang/miri

@rustbot rustbot added O-unix Operating system: Unix-like 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 Sep 4, 2026
@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
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 13 candidates
  • Random selection from ChrisDenton, Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey

Comment thread library/std/src/sys/pal/unix/stack_overflow/mod.rs
@joboet
joboet force-pushed the refactor_stack_overflow branch from 305f083 to f8cd163 Compare September 4, 2026 17:42
@rust-log-analyzer

This comment has been minimized.

@ChrisDenton

Copy link
Copy Markdown
Member

Could you perhaps make the changes all in the original file and then move them into separate files in a different commit?

@joboet
joboet force-pushed the refactor_stack_overflow branch from f8cd163 to ae721d8 Compare September 9, 2026 12:58
@joboet

joboet commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

I added some intermediate commits to simplify the diffs.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-x86_64-gnu-miri failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
tests/fail/tree_borrows/wildcard/single_exposed_foreign.rs ... ok
tests/fail/tree_borrows/wildcard/subtree_internal_relatedness_wildcard.rs ... ok

FAILED TEST: tests/fail/data_race/dangling_thread_race.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-vVJ8FE" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/miri" "--error-format=json" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/fail/data_race" "tests/fail/data_race/dangling_thread_race.rs" "-Zmiri-deterministic-concurrency" "-Zmiri-disable-stacked-borrows" "--edition" "2021"

error: test got exit status: 0, but expected 1
 = note: compilation succeeded, but was expected to fail

error: no output was emitted
Execute `./miri test --bless` to remove `tests/fail/data_race/dangling_thread_race.stderr`

error: `Data race detected between (1) non-atomic write on thread `unnamed-1` and (2) non-atomic write on thread `main`` not found in diagnostics on line 37
##[error]  --> tests/fail/data_race/dangling_thread_race.rs:37:31
   |
37 | ...ERROR: Data race detected between (1) non-atomic write on thread `unnamed-1` and (2) non-atomic write on thread `main`
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected because of this pattern
   |

full stderr:

full stdout:



FAILED TEST: tests/fail/data_race/dangling_thread_async_race.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-vVJ8FE" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/miri" "--error-format=json" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/fail/data_race" "tests/fail/data_race/dangling_thread_async_race.rs" "-Zmiri-deterministic-concurrency" "-Zmiri-disable-stacked-borrows" "--edition" "2021"

error: test got exit status: 0, but expected 1
 = note: compilation succeeded, but was expected to fail

error: no output was emitted
Execute `./miri test --bless` to remove `tests/fail/data_race/dangling_thread_async_race.stderr`

error: `Data race detected between (1) non-atomic write on thread `unnamed-1` and (2) non-atomic write on thread `unnamed-3`` not found in diagnostics on line 39
##[error]  --> tests/fail/data_race/dangling_thread_async_race.rs:39:35
   |
39 | ...ROR: Data race detected between (1) non-atomic write on thread `unnamed-1` and (2) non-atomic write on thread `unnamed-3`
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected because of this pattern
   |

full stderr:

---

Location:
   /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ui_test-0.30.7/src/lib.rs:365

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
error: test failed, to rerun pass `--test ui`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/build/miri/54d3518e03e017c2/out/ui-54d3518e03e017c2` (exit status: 1)
Bootstrap failed while executing `test --stage 2 miri cargo-miri`
Currently active steps:
test::Miri { target: x86_64-unknown-linux-gnu } at src/bootstrap/src/core/build_steps/test.rs:716
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo test --target x86_64-unknown-linux-gnu -Zbinary-dep-depinfo -j 4 -Zroot-dir=/checkout -Zembed-metadata=no --locked --color=always --profile=release --manifest-path /checkout/src/tools/miri/Cargo.toml -- [workdir=/checkout]` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:208:21
Executed at: src/bootstrap/src/core/build_steps/test.rs:790:19

Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:36:54
  local time: Wed Sep  9 13:39:48 UTC 2026
  network time: Wed, 09 Sep 2026 13:39:48 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

@joboet

joboet commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

I should do this in multiple PRs...

@joboet
joboet marked this pull request as draft September 23, 2026 11:47
@rustbot rustbot 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 Sep 23, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 29, 2026
…isDenton

std: split stack overflow module

One of my current projects is to improve the UNIX stack overflow handling code. Outlining the inner `imp` modules out of the current 844-line module makes future work like rust-lang#162296 much easier to review.

The first three commits only exist to diffing simpler, the last one contains the import adjustments.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 29, 2026
…isDenton

std: split stack overflow module

One of my current projects is to improve the UNIX stack overflow handling code. Outlining the inner `imp` modules out of the current 844-line module makes future work like rust-lang#162296 much easier to review.

The first three commits only exist to diffing simpler, the last one contains the import adjustments.
rust-bors Bot pushed a commit that referenced this pull request Sep 29, 2026
Rollup merge of #163210 - joboet:split_stack_overflow, r=ChrisDenton

std: split stack overflow module

One of my current projects is to improve the UNIX stack overflow handling code. Outlining the inner `imp` modules out of the current 844-line module makes future work like #162296 much easier to review.

The first three commits only exist to diffing simpler, the last one contains the import adjustments.
@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #163472) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Sep 30, 2026
std: split stack overflow module

One of my current projects is to improve the UNIX stack overflow handling code. Outlining the inner `imp` modules out of the current 844-line module makes future work like rust-lang/rust#162296 much easier to review.

The first three commits only exist to diffing simpler, the last one contains the import adjustments.

This branch has not been deployed

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

Labels

O-unix Operating system: Unix-like 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.

5 participants