Skip to content
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

Added proper definition of ucontext for macos/aarch64 to avoid unaligned issue #4120

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Changes from 3 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
25 changes: 22 additions & 3 deletions lib/vm/src/trap/traphandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ static MAGIC: u8 = 0xc0;

static DEFAULT_STACK_SIZE: AtomicUsize = AtomicUsize::new(1024 * 1024);

// Current definition of `ucontext_t` in the `libc` crate is incorrect
// on aarch64-apple-drawin so it's defined here with a more accurate definition.
#[repr(C)]
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
struct ucontext_t {
uc_onstack: libc::c_int,
uc_sigmask: libc::sigset_t,
uc_stack: libc::stack_t,
uc_link: *mut libc::ucontext_t,
uc_mcsize: usize,
uc_mcontext: libc::mcontext_t,
}

#[cfg(any(
target_os = "linux",
all(not(target_arch = "aarch64"), target_os = "macos")
))]
use libc::ucontext_t;
ptitSeb marked this conversation as resolved.
Show resolved Hide resolved

/// Default stack size is 1MB.
pub fn set_stack_size(size: usize) {
DEFAULT_STACK_SIZE.store(size.max(8 * 1024).min(100 * 1024 * 1024), Ordering::Relaxed);
Expand Down Expand Up @@ -216,7 +235,7 @@ cfg_if::cfg_if! {
}
_ => None,
};
let ucontext = &mut *(context as *mut libc::ucontext_t);
let ucontext = &mut *(context as *mut ucontext_t);
let (pc, sp) = get_pc_sp(ucontext);
let handled = TrapHandlerContext::handle_trap(
pc,
Expand Down Expand Up @@ -256,7 +275,7 @@ cfg_if::cfg_if! {
}
}

unsafe fn get_pc_sp(context: &libc::ucontext_t) -> (usize, usize) {
unsafe fn get_pc_sp(context: &ucontext_t) -> (usize, usize) {
let (pc, sp);
cfg_if::cfg_if! {
if #[cfg(all(
Expand Down Expand Up @@ -311,7 +330,7 @@ cfg_if::cfg_if! {
(pc, sp)
}

unsafe fn update_context(context: &mut libc::ucontext_t, regs: TrapHandlerRegs) {
unsafe fn update_context(context: &mut ucontext_t, regs: TrapHandlerRegs) {
cfg_if::cfg_if! {
if #[cfg(all(
any(target_os = "linux", target_os = "android"),
Expand Down