Skip to content

Conversation

@nstilt1
Copy link
Contributor

@nstilt1 nstilt1 commented Dec 22, 2025

Hey, I was experimenting with your code when I found that the stack_area() function includes an extra guard page in the usable size... unless I'm using it wrong? I believe that the stack area includes 2 guard pages and only 1 of them is subtracted. I have a test that fails, and switching it to subtract 2 guard pages fixes my test.

Fixes #134

@nstilt1
Copy link
Contributor Author

nstilt1 commented Dec 22, 2025

I see that cargo test --lib is not ran by the GitHub Actions. Also, it shouldn't break anything since requested_size is only used instead of the value returned by stack_area(), but it is mentioned in the comments here:

// TODO should we not pass `allocated_stack_size` here?
// TODO should we not pass `allocated_stack_size` here?
let panic = psm::on_stack(stack_base, requested_stack_size, move || {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(callback)).err()
Copy link
Contributor Author

@nstilt1 nstilt1 Dec 22, 2025

Choose a reason for hiding this comment

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

Using allocated_stack_size could result in a segfault if the actual size is smaller than is reported by stack_area(), and if the stack overflows into these guard pages.

Copy link
Member

@nagisa nagisa left a comment

Choose a reason for hiding this comment

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

This seems correct. Relevant is #121.

Comment on lines 116 to 119
assert_eq!(
stack.stack_area().1,
((size + page_size() - 1) / page_size()) * page_size()
)
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind adjusting the test to also attempt a write to every byte of the allocated stack mapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Good idea. I will also simplify that test to use div_ceil instead of that nasty looking operation there. Clippy checks would have caught that. I wish there was just a way to hook into git commit that ran clippy and fmt automatically prior to the commit being committed

Copy link
Contributor Author

@nstilt1 nstilt1 Jan 5, 2026

Choose a reason for hiding this comment

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

I updated the test, but:

  • cargo test --lib probably won't get ran by GitHub Actions, as the original test I wrote wasn't failing on Actions but would fail when I ran that command.
  • I am not aware of any arches/oses that have a "reversed" stack direction. We might want to run the test on one of those targets to make sure it still passes, but if it fails, it can probably be fixed by using rust_psm_stack_direction(). But then again... this function is not directly used by stacker, so it might not be an issue. Copilot says that rust doesn't support any arches that use reversed stack directions, but I would trust your word over Copilot's.

Copy link
Contributor Author

@nstilt1 nstilt1 Jan 5, 2026

Choose a reason for hiding this comment

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

Also, I confirmed segfaults occur when using + 1, 16, 32, 1024 at the max end of the range in the test to see if the stack is any larger by chance. Used different numbers in case alignment was the issue.

@nagisa nagisa merged commit ef073d4 into rust-lang:master Jan 7, 2026
168 of 169 checks passed
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.

mmap stack_area() returns area that includes a guard page

2 participants