Skip to content

Commit 618d945

Browse files
ehussgitbot
authored and
gitbot
committed
panic_unwind: Apply unsafe_op_in_unsafe_fn
1 parent ce6d8fd commit 618d945

File tree

6 files changed

+110
-89
lines changed

6 files changed

+110
-89
lines changed

panic_unwind/src/emcc.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -71,42 +71,46 @@ pub(crate) unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
7171
ptr: *mut u8,
7272
is_rust_panic: bool,
7373
}
74-
let catch_data = &*(ptr as *mut CatchData);
74+
unsafe {
75+
let catch_data = &*(ptr as *mut CatchData);
7576

76-
let adjusted_ptr = __cxa_begin_catch(catch_data.ptr as *mut libc::c_void) as *mut Exception;
77-
if !catch_data.is_rust_panic {
78-
super::__rust_foreign_exception();
79-
}
77+
let adjusted_ptr = __cxa_begin_catch(catch_data.ptr as *mut libc::c_void) as *mut Exception;
78+
if !catch_data.is_rust_panic {
79+
super::__rust_foreign_exception();
80+
}
8081

81-
let canary = (&raw const (*adjusted_ptr).canary).read();
82-
if !ptr::eq(canary, &EXCEPTION_TYPE_INFO) {
83-
super::__rust_foreign_exception();
84-
}
82+
let canary = (&raw const (*adjusted_ptr).canary).read();
83+
if !ptr::eq(canary, &EXCEPTION_TYPE_INFO) {
84+
super::__rust_foreign_exception();
85+
}
8586

86-
let was_caught = (*adjusted_ptr).caught.swap(true, Ordering::Relaxed);
87-
if was_caught {
88-
// Since cleanup() isn't allowed to panic, we just abort instead.
89-
intrinsics::abort();
87+
let was_caught = (*adjusted_ptr).caught.swap(true, Ordering::Relaxed);
88+
if was_caught {
89+
// Since cleanup() isn't allowed to panic, we just abort instead.
90+
intrinsics::abort();
91+
}
92+
let out = (*adjusted_ptr).data.take().unwrap();
93+
__cxa_end_catch();
94+
out
9095
}
91-
let out = (*adjusted_ptr).data.take().unwrap();
92-
__cxa_end_catch();
93-
out
9496
}
9597

9698
pub(crate) unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
97-
let exception = __cxa_allocate_exception(mem::size_of::<Exception>()) as *mut Exception;
98-
if exception.is_null() {
99-
return uw::_URC_FATAL_PHASE1_ERROR as u32;
99+
unsafe {
100+
let exception = __cxa_allocate_exception(mem::size_of::<Exception>()) as *mut Exception;
101+
if exception.is_null() {
102+
return uw::_URC_FATAL_PHASE1_ERROR as u32;
103+
}
104+
ptr::write(
105+
exception,
106+
Exception {
107+
canary: &EXCEPTION_TYPE_INFO,
108+
caught: AtomicBool::new(false),
109+
data: Some(data),
110+
},
111+
);
112+
__cxa_throw(exception as *mut _, &EXCEPTION_TYPE_INFO, exception_cleanup);
100113
}
101-
ptr::write(
102-
exception,
103-
Exception {
104-
canary: &EXCEPTION_TYPE_INFO,
105-
caught: AtomicBool::new(false),
106-
data: Some(data),
107-
},
108-
);
109-
__cxa_throw(exception as *mut _, &EXCEPTION_TYPE_INFO, exception_cleanup);
110114
}
111115

112116
extern "C" fn exception_cleanup(ptr: *mut libc::c_void) -> *mut libc::c_void {

panic_unwind/src/gcc.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
6969
cause: data,
7070
});
7171
let exception_param = Box::into_raw(exception) as *mut uw::_Unwind_Exception;
72-
return uw::_Unwind_RaiseException(exception_param) as u32;
72+
return unsafe { uw::_Unwind_RaiseException(exception_param) as u32 };
7373

7474
extern "C" fn exception_cleanup(
7575
_unwind_code: uw::_Unwind_Reason_Code,
@@ -83,26 +83,28 @@ pub(crate) unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
8383
}
8484

8585
pub(crate) unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
86-
let exception = ptr as *mut uw::_Unwind_Exception;
87-
if (*exception).exception_class != RUST_EXCEPTION_CLASS {
88-
uw::_Unwind_DeleteException(exception);
89-
super::__rust_foreign_exception();
90-
}
86+
unsafe {
87+
let exception = ptr as *mut uw::_Unwind_Exception;
88+
if (*exception).exception_class != RUST_EXCEPTION_CLASS {
89+
uw::_Unwind_DeleteException(exception);
90+
super::__rust_foreign_exception();
91+
}
9192

92-
let exception = exception.cast::<Exception>();
93-
// Just access the canary field, avoid accessing the entire `Exception` as
94-
// it can be a foreign Rust exception.
95-
let canary = (&raw const (*exception).canary).read();
96-
if !ptr::eq(canary, &CANARY) {
97-
// A foreign Rust exception, treat it slightly differently from other
98-
// foreign exceptions, because call into `_Unwind_DeleteException` will
99-
// call into `__rust_drop_panic` which produces a confusing
100-
// "Rust panic must be rethrown" message.
101-
super::__rust_foreign_exception();
102-
}
93+
let exception = exception.cast::<Exception>();
94+
// Just access the canary field, avoid accessing the entire `Exception` as
95+
// it can be a foreign Rust exception.
96+
let canary = (&raw const (*exception).canary).read();
97+
if !ptr::eq(canary, &CANARY) {
98+
// A foreign Rust exception, treat it slightly differently from other
99+
// foreign exceptions, because call into `_Unwind_DeleteException` will
100+
// call into `__rust_drop_panic` which produces a confusing
101+
// "Rust panic must be rethrown" message.
102+
super::__rust_foreign_exception();
103+
}
103104

104-
let exception = Box::from_raw(exception as *mut Exception);
105-
exception.cause
105+
let exception = Box::from_raw(exception as *mut Exception);
106+
exception.cause
107+
}
106108
}
107109

108110
// Rust's exception class identifier. This is used by personality routines to

panic_unwind/src/hermit.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ pub(crate) unsafe fn cleanup(_ptr: *mut u8) -> Box<dyn Any + Send> {
99
unsafe extern "C" {
1010
fn __rust_abort() -> !;
1111
}
12-
__rust_abort();
12+
unsafe {
13+
__rust_abort();
14+
}
1315
}
1416

1517
pub(crate) unsafe fn panic(_data: Box<dyn Any + Send>) -> u32 {
1618
unsafe extern "C" {
1719
fn __rust_abort() -> !;
1820
}
19-
__rust_abort();
21+
unsafe {
22+
__rust_abort();
23+
}
2024
}

panic_unwind/src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#![allow(internal_features)]
2828
#![cfg_attr(not(bootstrap), feature(cfg_emscripten_wasm_eh))]
2929
#![warn(unreachable_pub)]
30+
#![deny(unsafe_op_in_unsafe_fn)]
3031

3132
use alloc::boxed::Box;
3233
use core::any::Any;
@@ -87,14 +88,16 @@ unsafe extern "C" {
8788
#[rustc_std_internal_symbol]
8889
#[allow(improper_ctypes_definitions)]
8990
pub unsafe extern "C" fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static) {
90-
Box::into_raw(imp::cleanup(payload))
91+
unsafe { Box::into_raw(imp::cleanup(payload)) }
9192
}
9293

9394
// Entry point for raising an exception, just delegates to the platform-specific
9495
// implementation.
9596
#[rustc_std_internal_symbol]
9697
pub unsafe fn __rust_start_panic(payload: &mut dyn PanicPayload) -> u32 {
97-
let payload = Box::from_raw(payload.take_box());
98+
unsafe {
99+
let payload = Box::from_raw(payload.take_box());
98100

99-
imp::panic(payload)
101+
imp::panic(payload)
102+
}
100103
}

panic_unwind/src/miri.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub(crate) unsafe fn panic(payload: Box<dyn Any + Send>) -> u32 {
1616
// The payload we pass to `miri_start_unwind` will be exactly the argument we get
1717
// in `cleanup` below. So we just box it up once, to get something pointer-sized.
1818
let payload_box: Payload = Box::new(payload);
19-
miri_start_unwind(Box::into_raw(payload_box) as *mut u8)
19+
unsafe { miri_start_unwind(Box::into_raw(payload_box) as *mut u8) }
2020
}
2121

2222
pub(crate) unsafe fn cleanup(payload_box: *mut u8) -> Box<dyn Any + Send> {
2323
// Recover the underlying `Box`.
24-
let payload_box: Payload = Box::from_raw(payload_box as *mut _);
24+
let payload_box: Payload = unsafe { Box::from_raw(payload_box as *mut _) };
2525
*payload_box
2626
}

panic_unwind/src/seh.rs

+43-35
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,11 @@ static mut TYPE_DESCRIPTOR: _TypeDescriptor = _TypeDescriptor {
268268
macro_rules! define_cleanup {
269269
($abi:tt $abi2:tt) => {
270270
unsafe extern $abi fn exception_cleanup(e: *mut Exception) {
271-
if let Exception { data: Some(b), .. } = e.read() {
272-
drop(b);
273-
super::__rust_drop_panic();
271+
unsafe {
272+
if let Exception { data: Some(b), .. } = e.read() {
273+
drop(b);
274+
super::__rust_drop_panic();
275+
}
274276
}
275277
}
276278
unsafe extern $abi2 fn exception_copy(
@@ -322,45 +324,51 @@ pub(crate) unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
322324
//
323325
// In any case, we basically need to do something like this until we can
324326
// express more operations in statics (and we may never be able to).
325-
atomic_store_seqcst(
326-
(&raw mut THROW_INFO.pmfnUnwind).cast(),
327-
ptr_t::new(exception_cleanup as *mut u8).raw(),
328-
);
329-
atomic_store_seqcst(
330-
(&raw mut THROW_INFO.pCatchableTypeArray).cast(),
331-
ptr_t::new((&raw mut CATCHABLE_TYPE_ARRAY).cast()).raw(),
332-
);
333-
atomic_store_seqcst(
334-
(&raw mut CATCHABLE_TYPE_ARRAY.arrayOfCatchableTypes[0]).cast(),
335-
ptr_t::new((&raw mut CATCHABLE_TYPE).cast()).raw(),
336-
);
337-
atomic_store_seqcst(
338-
(&raw mut CATCHABLE_TYPE.pType).cast(),
339-
ptr_t::new((&raw mut TYPE_DESCRIPTOR).cast()).raw(),
340-
);
341-
atomic_store_seqcst(
342-
(&raw mut CATCHABLE_TYPE.copyFunction).cast(),
343-
ptr_t::new(exception_copy as *mut u8).raw(),
344-
);
327+
unsafe {
328+
atomic_store_seqcst(
329+
(&raw mut THROW_INFO.pmfnUnwind).cast(),
330+
ptr_t::new(exception_cleanup as *mut u8).raw(),
331+
);
332+
atomic_store_seqcst(
333+
(&raw mut THROW_INFO.pCatchableTypeArray).cast(),
334+
ptr_t::new((&raw mut CATCHABLE_TYPE_ARRAY).cast()).raw(),
335+
);
336+
atomic_store_seqcst(
337+
(&raw mut CATCHABLE_TYPE_ARRAY.arrayOfCatchableTypes[0]).cast(),
338+
ptr_t::new((&raw mut CATCHABLE_TYPE).cast()).raw(),
339+
);
340+
atomic_store_seqcst(
341+
(&raw mut CATCHABLE_TYPE.pType).cast(),
342+
ptr_t::new((&raw mut TYPE_DESCRIPTOR).cast()).raw(),
343+
);
344+
atomic_store_seqcst(
345+
(&raw mut CATCHABLE_TYPE.copyFunction).cast(),
346+
ptr_t::new(exception_copy as *mut u8).raw(),
347+
);
348+
}
345349

346350
unsafe extern "system-unwind" {
347351
fn _CxxThrowException(pExceptionObject: *mut c_void, pThrowInfo: *mut u8) -> !;
348352
}
349353

350-
_CxxThrowException(throw_ptr, (&raw mut THROW_INFO) as *mut _);
354+
unsafe {
355+
_CxxThrowException(throw_ptr, (&raw mut THROW_INFO) as *mut _);
356+
}
351357
}
352358

353359
pub(crate) unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> {
354-
// A null payload here means that we got here from the catch (...) of
355-
// __rust_try. This happens when a non-Rust foreign exception is caught.
356-
if payload.is_null() {
357-
super::__rust_foreign_exception();
358-
}
359-
let exception = payload as *mut Exception;
360-
let canary = (&raw const (*exception).canary).read();
361-
if !core::ptr::eq(canary, &raw const TYPE_DESCRIPTOR) {
362-
// A foreign Rust exception.
363-
super::__rust_foreign_exception();
360+
unsafe {
361+
// A null payload here means that we got here from the catch (...) of
362+
// __rust_try. This happens when a non-Rust foreign exception is caught.
363+
if payload.is_null() {
364+
super::__rust_foreign_exception();
365+
}
366+
let exception = payload as *mut Exception;
367+
let canary = (&raw const (*exception).canary).read();
368+
if !core::ptr::eq(canary, &raw const TYPE_DESCRIPTOR) {
369+
// A foreign Rust exception.
370+
super::__rust_foreign_exception();
371+
}
372+
(*exception).data.take().unwrap()
364373
}
365-
(*exception).data.take().unwrap()
366374
}

0 commit comments

Comments
 (0)