chore: Move unsafe operations in unsafe functions to unsafe blocks (#36017)

Signed-off-by: DK Liao <dklassic@gmail.com>
This commit is contained in:
DK Liao 2025-03-18 15:19:35 +09:00 committed by GitHub
parent eb3c48f9d3
commit bcdd34e2aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 57 deletions

View file

@ -62,7 +62,7 @@ fn check_kern_return(kret: mach2::kern_return::kern_return_t) -> Result<(), ()>
#[allow(unsafe_code)]
unsafe fn suspend_thread(thread_id: MonitoredThreadId) -> Result<(), ()> {
check_kern_return(mach2::thread_act::thread_suspend(thread_id))
check_kern_return(unsafe { mach2::thread_act::thread_suspend(thread_id) })
}
#[allow(unsafe_code)]
@ -71,12 +71,14 @@ unsafe fn get_registers(thread_id: MonitoredThreadId) -> Result<Registers, ()> {
{
let mut state = mach2::structs::x86_thread_state64_t::new();
let mut state_count = mach2::structs::x86_thread_state64_t::count();
let kret = mach2::thread_act::thread_get_state(
thread_id,
mach2::thread_status::x86_THREAD_STATE64,
(&mut state) as *mut _ as *mut _,
&mut state_count,
);
let kret = unsafe {
mach2::thread_act::thread_get_state(
thread_id,
mach2::thread_status::x86_THREAD_STATE64,
(&mut state) as *mut _ as *mut _,
&mut state_count,
)
};
check_kern_return(kret)?;
Ok(Registers {
instruction_ptr: state.__rip as Address,
@ -88,12 +90,14 @@ unsafe fn get_registers(thread_id: MonitoredThreadId) -> Result<Registers, ()> {
{
let mut state = mach2::structs::arm_thread_state64_t::new();
let mut state_count = mach2::structs::arm_thread_state64_t::count();
let kret = mach2::thread_act::thread_get_state(
thread_id,
mach2::thread_status::ARM_THREAD_STATE64,
(&mut state) as *mut _ as *mut _,
&mut state_count,
);
let kret = unsafe {
mach2::thread_act::thread_get_state(
thread_id,
mach2::thread_status::ARM_THREAD_STATE64,
(&mut state) as *mut _ as *mut _,
&mut state_count,
)
};
check_kern_return(kret)?;
Ok(Registers {
instruction_ptr: state.__pc as Address,
@ -104,7 +108,7 @@ unsafe fn get_registers(thread_id: MonitoredThreadId) -> Result<Registers, ()> {
}
#[allow(unsafe_code)]
unsafe fn resume_thread(thread_id: MonitoredThreadId) -> Result<(), ()> {
check_kern_return(mach2::thread_act::thread_resume(thread_id))
check_kern_return(unsafe { mach2::thread_act::thread_resume(thread_id) })
}
#[allow(unsafe_code)]
@ -112,35 +116,36 @@ unsafe fn frame_pointer_stack_walk(regs: Registers) -> NativeStack {
// Note: this function will only work with code build with:
// --dev,
// or --with-frame-pointer.
let pthread_t = libc::pthread_self();
let stackaddr = libc::pthread_get_stackaddr_np(pthread_t);
let stacksize = libc::pthread_get_stacksize_np(pthread_t);
let mut native_stack = NativeStack::new();
let pc = regs.instruction_ptr as *mut std::ffi::c_void;
let stack = regs.stack_ptr as *mut std::ffi::c_void;
let _ = native_stack.process_register(pc, stack);
let mut current = regs.frame_ptr as *mut *mut std::ffi::c_void;
while !current.is_null() {
if (current as usize) < stackaddr as usize {
// Reached the end of the stack.
break;
unsafe {
let pthread_t = libc::pthread_self();
let stackaddr = libc::pthread_get_stackaddr_np(pthread_t);
let stacksize = libc::pthread_get_stacksize_np(pthread_t);
let pc = regs.instruction_ptr as *mut std::ffi::c_void;
let stack = regs.stack_ptr as *mut std::ffi::c_void;
let _ = native_stack.process_register(pc, stack);
let mut current = regs.frame_ptr as *mut *mut std::ffi::c_void;
while !current.is_null() {
if (current as usize) < stackaddr as usize {
// Reached the end of the stack.
break;
}
if current as usize >= stackaddr.add(stacksize * 8) as usize {
// Reached the beginning of the stack.
// Assumining 64 bit mac(see the stacksize * 8).
break;
}
let next = *current as *mut *mut std::ffi::c_void;
let pc = current.add(1);
let stack = current.add(2);
if let Err(()) = native_stack.process_register(*pc, *stack) {
break;
}
if (next <= current) || (next as usize & 3 != 0) {
break;
}
current = next;
}
if current as usize >= stackaddr.add(stacksize * 8) as usize {
// Reached the beginning of the stack.
// Assumining 64 bit mac(see the stacksize * 8).
break;
}
let next = *current as *mut *mut std::ffi::c_void;
let pc = current.add(1);
let stack = current.add(2);
if let Err(()) = native_stack.process_register(*pc, *stack) {
break;
}
if (next <= current) || (next as usize & 3 != 0) {
break;
}
current = next;
}
native_stack
}