Skip to content

Commit ce6d8fd

Browse files
ehussgitbot
authored and
gitbot
committed
panic_abort: Apply unsafe_op_in_unsafe_fn
1 parent 27b44a0 commit ce6d8fd

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

panic_abort/src/android.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ type SetAbortMessageType = unsafe extern "C" fn(*const libc::c_char) -> ();
1616
// Weakly resolve the symbol for android_set_abort_message. This function is only available
1717
// for API >= 21.
1818
pub(crate) unsafe fn android_set_abort_message(payload: &mut dyn PanicPayload) {
19-
let func_addr =
19+
let func_addr = unsafe {
2020
libc::dlsym(libc::RTLD_DEFAULT, ANDROID_SET_ABORT_MESSAGE.as_ptr() as *const libc::c_char)
21-
as usize;
21+
as usize
22+
};
2223
if func_addr == 0 {
2324
return;
2425
}
@@ -37,13 +38,14 @@ pub(crate) unsafe fn android_set_abort_message(payload: &mut dyn PanicPayload) {
3738

3839
// Allocate a new buffer to append the null byte.
3940
let size = msg.len() + 1usize;
40-
let buf = libc::malloc(size) as *mut libc::c_char;
41+
let buf = unsafe { libc::malloc(size) as *mut libc::c_char };
4142
if buf.is_null() {
4243
return; // allocation failure
4344
}
44-
copy_nonoverlapping(msg.as_ptr(), buf as *mut u8, msg.len());
45-
buf.add(msg.len()).write(0);
46-
47-
let func = transmute::<usize, SetAbortMessageType>(func_addr);
48-
func(buf);
45+
unsafe {
46+
copy_nonoverlapping(msg.as_ptr(), buf as *mut u8, msg.len());
47+
buf.add(msg.len()).write(0);
48+
let func = transmute::<usize, SetAbortMessageType>(func_addr);
49+
func(buf);
50+
}
4951
}

panic_abort/src/lib.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(staged_api)]
1616
#![feature(rustc_attrs)]
1717
#![allow(internal_features)]
18+
#![deny(unsafe_op_in_unsafe_fn)]
1819

1920
#[cfg(target_os = "android")]
2021
mod android;
@@ -36,16 +37,22 @@ pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Sen
3637
pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
3738
// Android has the ability to attach a message as part of the abort.
3839
#[cfg(target_os = "android")]
39-
android::android_set_abort_message(_payload);
40+
unsafe {
41+
android::android_set_abort_message(_payload);
42+
}
4043
#[cfg(target_os = "zkvm")]
41-
zkvm::zkvm_set_abort_message(_payload);
44+
unsafe {
45+
zkvm::zkvm_set_abort_message(_payload);
46+
}
4247

43-
abort();
48+
unsafe {
49+
abort();
50+
}
4451

4552
cfg_if::cfg_if! {
4653
if #[cfg(any(unix, target_os = "solid_asp3"))] {
4754
unsafe fn abort() -> ! {
48-
libc::abort();
55+
unsafe { libc::abort(); }
4956
}
5057
} else if #[cfg(any(target_os = "hermit",
5158
all(target_vendor = "fortanix", target_env = "sgx"),
@@ -57,7 +64,7 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
5764
unsafe extern "C" {
5865
pub fn __rust_abort() -> !;
5966
}
60-
__rust_abort();
67+
unsafe { __rust_abort(); }
6168
}
6269
} else if #[cfg(all(windows, not(miri)))] {
6370
// On Windows, use the processor-specific __fastfail mechanism. In Windows 8
@@ -75,11 +82,17 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
7582
const FAST_FAIL_FATAL_APP_EXIT: usize = 7;
7683
cfg_if::cfg_if! {
7784
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
78-
core::arch::asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
85+
unsafe {
86+
core::arch::asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
87+
}
7988
} else if #[cfg(all(target_arch = "arm", target_feature = "thumb-mode"))] {
80-
core::arch::asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
89+
unsafe {
90+
core::arch::asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
91+
}
8192
} else if #[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))] {
82-
core::arch::asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
93+
unsafe {
94+
core::arch::asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
95+
}
8396
} else {
8497
core::intrinsics::abort();
8598
}
@@ -93,7 +106,7 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
93106
}
94107

95108
unsafe fn abort() -> ! {
96-
teeos::TEE_Panic(1);
109+
unsafe { teeos::TEE_Panic(1); }
97110
}
98111
} else {
99112
unsafe fn abort() -> ! {

panic_abort/src/zkvm.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ pub(crate) unsafe fn zkvm_set_abort_message(payload: &mut dyn PanicPayload) {
2020
fn sys_panic(msg_ptr: *const u8, len: usize) -> !;
2121
}
2222

23-
sys_panic(msg.as_ptr(), msg.len());
23+
unsafe {
24+
sys_panic(msg.as_ptr(), msg.len());
25+
}
2426
}

0 commit comments

Comments
 (0)