Skip to content

Commit aabdb23

Browse files
committed
Fix unneccessary stack copy
1 parent 0ac91c0 commit aabdb23

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ debug = 2
1313

1414
[workspace.dependencies]
1515
cfg-if = "1.0"
16-
crash-context = { version = "0.6", path = "crash-context" }
16+
crash-context = "0.6"
1717
libc = "0.2"
1818
mach2 = "0.4"
1919
parking_lot = "0.12"

crash-context/src/linux.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ cfg_if::cfg_if! {
171171
pub uc_mcontext: mcontext_t,
172172
}
173173

174-
#[repr(C, align(16))]
175-
#[derive(Clone)]
176-
pub struct Reserved([u8; 4096]);
177-
178174
// Note you might see this defined in C with `unsigned long` or
179175
// `unsigned long long` and think, WTF, those aren't the same! Except
180176
// `long` means either 32-bit _or_ 64-bit depending on the data model,
@@ -190,7 +186,9 @@ cfg_if::cfg_if! {
190186
pub sp: u64,
191187
pub pc: u64,
192188
pub pstate: u64,
193-
//pub __reserved: Reserved,
189+
// Note that u128 is ABI safe on aarch64, this is actually a
190+
// `long double` in C which Rust doesn't have native support
191+
pub __reserved: [u128; 256],
194192
}
195193

196194
/// Magic value written by the kernel and our custom getcontext
@@ -267,9 +265,9 @@ mod test {
267265
#[cfg(not(target_env = "musl"))]
268266
#[test]
269267
fn matches_libc() {
270-
// assert_eq!(
271-
// std::mem::size_of::<libc::ucontext_t>(),
272-
// std::mem::size_of::<super::ucontext_t>()
273-
// );
268+
assert_eq!(
269+
std::mem::size_of::<libc::ucontext_t>(),
270+
std::mem::size_of::<super::ucontext_t>()
271+
);
274272
}
275273
}

crash-handler/src/linux/state.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Error, Signal};
2-
use std::{mem, ptr};
2+
use std::{mem, ops::DerefMut, ptr};
33

44
// std::cmp::max is not const :(
55
const fn get_stack_size() -> usize {
@@ -402,8 +402,8 @@ unsafe extern "C" fn signal_handler(
402402

403403
/// The size of `CrashContext` can be too big w.r.t the size of alternatate stack
404404
/// for `signal_handler`. Keep the crash context as a .bss field.
405-
static CRASH_CONTEXT: parking_lot::Mutex<mem::MaybeUninit<crash_context::CrashContext>> =
406-
parking_lot::const_mutex(mem::MaybeUninit::uninit());
405+
static CRASH_CONTEXT: parking_lot::Mutex<crash_context::CrashContext> =
406+
parking_lot::const_mutex(unsafe { mem::zeroed() });
407407

408408
pub(super) struct HandlerInner {
409409
handler: Box<dyn crate::CrashEvent>,
@@ -435,12 +435,11 @@ impl HandlerInner {
435435
// Allow ourselves to be dumped, if that is what the user handler wishes to do
436436
let _set_dumpable = SetDumpable::new(self.dump_process);
437437
debug_print!("set dumpable");
438-
let mut crash_ctx = CRASH_CONTEXT.lock();
438+
let mut cc = CRASH_CONTEXT.lock();
439439

440440
{
441-
*crash_ctx = mem::MaybeUninit::zeroed();
441+
ptr::write_bytes(cc.deref_mut(), 0, 1);
442442
debug_print!("zeroed crashctx");
443-
let cc = &mut *crash_ctx.as_mut_ptr();
444443

445444
ptr::copy_nonoverlapping(nix_info, &mut cc.siginfo, 1);
446445
debug_print!("copied siginfo");
@@ -451,24 +450,23 @@ impl HandlerInner {
451450

452451
cfg_if::cfg_if! {
453452
if #[cfg(target_arch = "aarch64")] {
454-
// let fp_ptr = uc_ptr.uc_mcontext.__reserved.as_ptr().cast::<crash_context::fpsimd_context>();
453+
let fp_ptr = uc_ptr.uc_mcontext.__reserved.as_ptr().cast::<crash_context::fpsimd_context>();
455454

456-
// if (*fp_ptr).head.magic == crash_context::FPSIMD_MAGIC {
457-
// ptr::copy_nonoverlapping(fp_ptr, &mut cc.float_state, 1);
458-
// }
455+
if (*fp_ptr).head.magic == crash_context::FPSIMD_MAGIC {
456+
ptr::copy_nonoverlapping(fp_ptr, &mut cc.float_state, 1);
457+
}
459458
} else if #[cfg(not(target_arch = "arm"))] {
460-
// if !uc_ptr.uc_mcontext.fpregs.is_null() {
461-
// ptr::copy_nonoverlapping(uc_ptr.uc_mcontext.fpregs, ((&mut cc.float_state) as *mut crash_context::fpregset_t).cast(), 1);
462-
463-
// }
459+
if !uc_ptr.uc_mcontext.fpregs.is_null() {
460+
ptr::copy_nonoverlapping(uc_ptr.uc_mcontext.fpregs, ((&mut cc.float_state) as *mut crash_context::fpregset_t).cast(), 1);
461+
}
464462
}
465463
}
466464

467465
cc.pid = std::process::id() as i32;
468466
cc.tid = libc::syscall(libc::SYS_gettid) as i32;
469467
}
470468

471-
self.handler.on_crash(&*crash_ctx.as_ptr())
469+
self.handler.on_crash(&cc)
472470
}
473471
}
474472
}

0 commit comments

Comments
 (0)