-
Notifications
You must be signed in to change notification settings - Fork 60
treat hermit like wasm32 #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::{get_stack_limit, set_stack_limit}; | ||
|
||
pub struct StackRestoreGuard { | ||
new_stack: *mut u8, | ||
stack_bytes: usize, | ||
old_stack_limit: Option<usize>, | ||
} | ||
|
||
const ALIGNMENT: usize = 16; | ||
|
||
impl StackRestoreGuard { | ||
pub fn new(stack_bytes: usize) -> StackRestoreGuard { | ||
// On these platforms we do not use stack guards. this is very unfortunate, | ||
// but there is not much we can do about it without OS support. | ||
// We simply allocate the requested size from the global allocator with a suitable | ||
// alignment. | ||
let stack_bytes = stack_bytes | ||
.checked_add(ALIGNMENT - 1) | ||
.expect("unreasonably large stack requested") | ||
/ ALIGNMENT | ||
* ALIGNMENT; | ||
let layout = std::alloc::Layout::from_size_align(stack_bytes, ALIGNMENT).unwrap(); | ||
let ptr = unsafe { std::alloc::alloc(layout) }; | ||
assert!(!ptr.is_null(), "unable to allocate stack"); | ||
StackRestoreGuard { | ||
new_stack: ptr, | ||
stack_bytes, | ||
old_stack_limit: get_stack_limit(), | ||
} | ||
} | ||
|
||
pub fn stack_area(&self) -> (*mut u8, usize) { | ||
(self.new_stack, self.stack_bytes) | ||
} | ||
} | ||
|
||
impl Drop for StackRestoreGuard { | ||
fn drop(&mut self) { | ||
unsafe { | ||
std::alloc::dealloc( | ||
self.new_stack, | ||
std::alloc::Layout::from_size_align_unchecked(self.stack_bytes, ALIGNMENT), | ||
); | ||
} | ||
set_stack_limit(self.old_stack_limit); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::{get_stack_limit, set_stack_limit}; | ||
|
||
pub struct StackRestoreGuard { | ||
mapping: *mut u8, | ||
size_with_guard: usize, | ||
page_size: usize, | ||
old_stack_limit: Option<usize>, | ||
} | ||
|
||
impl StackRestoreGuard { | ||
pub fn new(requested_size: usize) -> StackRestoreGuard { | ||
// For maximum portability we want to produce a stack that is aligned to a page and has | ||
// a size that’s a multiple of page size. It is natural to use mmap to allocate | ||
// these pages. Furthermore, we want to allocate two extras pages for the stack guard. | ||
// To achieve that we do our calculations in number of pages and convert to bytes last. | ||
let page_size = page_size(); | ||
let requested_pages = requested_size | ||
.checked_add(page_size - 1) | ||
.expect("unreasonably large stack requested") | ||
/ page_size; | ||
let page_count_with_guard = std::cmp::max(1, requested_pages) + 2; | ||
let size_with_guard = page_count_with_guard | ||
.checked_mul(page_size) | ||
.expect("unreasonably large stack requested"); | ||
|
||
// Next, there are a couple of approaches to how we allocate the new stack. If it is | ||
// available, we take the most obvious path and use `mmap`. | ||
nagisa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafe { | ||
let new_stack = libc::mmap( | ||
std::ptr::null_mut(), | ||
size_with_guard, | ||
libc::PROT_NONE, | ||
libc::MAP_PRIVATE | libc::MAP_ANON, | ||
-1, // Some implementations assert fd = -1 if MAP_ANON is specified | ||
0, | ||
); | ||
assert_ne!( | ||
new_stack, | ||
libc::MAP_FAILED, | ||
"mmap failed to allocate stack: {}", | ||
std::io::Error::last_os_error() | ||
); | ||
let guard = StackRestoreGuard { | ||
mapping: new_stack as *mut u8, | ||
page_size, | ||
size_with_guard, | ||
old_stack_limit: get_stack_limit(), | ||
}; | ||
// We leave a guard page without read/write access in our allocation. | ||
// TODO we allocated two extra pages for guard pages, but here we only use one? | ||
m-mueller678 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let above_guard_page = new_stack.add(page_size); | ||
#[cfg(not(target_os = "openbsd"))] | ||
let result = libc::mprotect( | ||
above_guard_page, | ||
size_with_guard - page_size, | ||
libc::PROT_READ | libc::PROT_WRITE, | ||
); | ||
#[cfg(target_os = "openbsd")] | ||
let result = if libc::mmap( | ||
above_guard_page, | ||
size_with_guard - page_size, | ||
libc::PROT_READ | libc::PROT_WRITE, | ||
libc::MAP_FIXED | libc::MAP_PRIVATE | libc::MAP_ANON | libc::MAP_STACK, | ||
-1, | ||
0, | ||
) == above_guard_page | ||
{ | ||
0 | ||
} else { | ||
-1 | ||
}; | ||
assert_ne!( | ||
result, | ||
-1, | ||
"mprotect/mmap failed: {}", | ||
std::io::Error::last_os_error() | ||
); | ||
guard | ||
} | ||
} | ||
|
||
// TODO this should return a *mut [u8], but pointer slices only got proper support with Rust 1.79. | ||
pub fn stack_area(&self) -> (*mut u8, usize) { | ||
unsafe { | ||
( | ||
self.mapping.add(self.page_size), | ||
self.size_with_guard - self.page_size, | ||
) | ||
} | ||
} | ||
} | ||
|
||
impl Drop for StackRestoreGuard { | ||
fn drop(&mut self) { | ||
unsafe { | ||
// FIXME: check the error code and decide what to do with it. | ||
// Perhaps a debug_assertion? | ||
libc::munmap(self.mapping as *mut std::ffi::c_void, self.size_with_guard); | ||
} | ||
set_stack_limit(self.old_stack_limit); | ||
} | ||
} | ||
|
||
fn page_size() -> usize { | ||
// FIXME: consider caching the page size. | ||
unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) as usize } | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.