Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ psm_stack_manipulation! {
let (stack_base, allocated_stack_size) = guard.stack_area();
debug_assert!(allocated_stack_size >= requested_stack_size);
set_stack_limit(Some(stack_base as usize));
// TODO should we not pass `allocated_stack_size` here?
let panic = psm::on_stack(stack_base, requested_stack_size, move || {
let panic = psm::on_stack(stack_base, allocated_stack_size, move || {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(callback)).err()
Copy link
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.

});
drop(guard);
Expand Down
19 changes: 18 additions & 1 deletion src/mmap_stack_restore_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl StackRestoreGuard {
unsafe {
(
self.mapping.add(self.page_size),
self.size_with_guard - self.page_size,
self.size_with_guard - 2 * self.page_size,
)
}
}
Expand All @@ -103,3 +103,20 @@ fn page_size() -> usize {
// FIXME: consider caching the page size.
unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) as usize }
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stack_area() {
for stack_size_kb in 1..64 {
let size = stack_size_kb * 1024;
let stack = StackRestoreGuard::new(size);
assert_eq!(
stack.stack_area().1,
((size + page_size() - 1) / page_size()) * page_size()
)
}
}
}
Loading