Skip to content

Commit 0ca2ed3

Browse files
committed
return a pointer to the end of the valid part of the sigstack, no further
also unmap the whole thing when cleaning up, rather than leaving a spare page floating around.
1 parent 041d97f commit 0ca2ed3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/libstd/sys/unix/stack_overflow.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ mod imp {
4747
use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
4848
use libc::{MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE, SIGSEGV};
4949

50+
use crate::sys::unix::os::page_size;
5051
use crate::sys_common::thread_info;
5152

5253
#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -137,17 +138,16 @@ mod imp {
137138
}
138139

139140
unsafe fn get_stackp() -> *mut libc::c_void {
140-
let page_size = crate::sys::unix::os::page_size();
141141
let stackp =
142-
mmap(ptr::null_mut(), SIGSTKSZ + page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
142+
mmap(ptr::null_mut(), SIGSTKSZ + page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
143143
if stackp == MAP_FAILED {
144144
panic!("failed to allocate an alternative stack");
145145
}
146-
let guard_result = libc::mprotect(stackp, page_size, PROT_NONE);
146+
let guard_result = libc::mprotect(stackp, page_size(), PROT_NONE);
147147
if guard_result != 0 {
148148
panic!("failed to set up alternative stack guard page");
149149
}
150-
stackp
150+
stackp.add(page_size())
151151
}
152152

153153
#[cfg(any(
@@ -195,7 +195,9 @@ mod imp {
195195
ss_size: SIGSTKSZ,
196196
};
197197
sigaltstack(&stack, ptr::null_mut());
198-
munmap(handler._data, SIGSTKSZ);
198+
// We know from `get_stackp` that the alternate stack we installed is part of a mapping
199+
// that started one page earlier, so walk back a page and unmap from there.
200+
munmap(handler._data.sub(page_size()), SIGSTKSZ + page_size());
199201
}
200202
}
201203
}

0 commit comments

Comments
 (0)