Skip to content

Commit

Permalink
Merge pull request #4120 from wasmerio/fix_macos_aarch64_ucontext
Browse files Browse the repository at this point in the history
Added proper definition of ucontext for macos/aarch64 to avoid unaligned issue
  • Loading branch information
ptitSeb authored Aug 3, 2023
2 parents a3f4dbe + bdea4aa commit 79e92a7
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/vm/src/trap/traphandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ 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(all(unix, not(all(target_arch = "aarch64", target_os = "macos"))))]
use libc::ucontext_t;

/// 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 +232,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 +272,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 +327,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

0 comments on commit 79e92a7

Please sign in to comment.