From d06aac5b67c037426d32fff8f4495e7bb25b78b0 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Wed, 2 Aug 2023 18:46:26 +0200 Subject: [PATCH 1/4] Added proper definition of ucontext for macos/aarch64 to avoid unaligned issue --- lib/vm/src/trap/traphandlers.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index b2845c1732f..e68d3de5eeb 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -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(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); @@ -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, @@ -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( @@ -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"), From 81cffa0a01889b2c8885f1767147588697c841dd Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Wed, 2 Aug 2023 18:56:58 +0200 Subject: [PATCH 2/4] Fixed linter --- lib/vm/src/trap/traphandlers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index e68d3de5eeb..1710091f211 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -38,7 +38,7 @@ static MAGIC: u8 = 0xc0; static DEFAULT_STACK_SIZE: AtomicUsize = AtomicUsize::new(1024 * 1024); -// Current definition of `ucontext_t` in the `libc` crate is incorrect +// 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"))] From f5c2894d64833acc2aa4863bf353fef02ee8288d Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Wed, 2 Aug 2023 20:38:36 +0200 Subject: [PATCH 3/4] Only use libc::ucontext_t for linux and macos non-aarch64 --- lib/vm/src/trap/traphandlers.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index 1710091f211..c2f2759d51c 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -51,7 +51,10 @@ struct ucontext_t { uc_mcontext: libc::mcontext_t, } -#[cfg(not(all(target_arch = "aarch64", target_os = "macos")))] +#[cfg(any( + target_os = "linux", + all(not(target_arch = "aarch64"), target_os = "macos") +))] use libc::ucontext_t; /// Default stack size is 1MB. From bdea4aa12157367ab1bc7cd53277ebb13e7a4daa Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Thu, 3 Aug 2023 11:10:59 +0200 Subject: [PATCH 4/4] Better way on when to use libc::ucontext_t --- lib/vm/src/trap/traphandlers.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index c2f2759d51c..0ab2eae789e 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -51,10 +51,7 @@ struct ucontext_t { uc_mcontext: libc::mcontext_t, } -#[cfg(any( - target_os = "linux", - all(not(target_arch = "aarch64"), target_os = "macos") -))] +#[cfg(all(unix, not(all(target_arch = "aarch64", target_os = "macos"))))] use libc::ucontext_t; /// Default stack size is 1MB.