Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f49474e

Browse filesBrowse files
authored
Rollup merge of rust-lang#136986 - ehuss:library-unsafe-fun, r=Noratrieb
Apply unsafe_op_in_unsafe_fn to the standard library This applies unsafe_op_in_unsafe_fn to the standard library in preparation for updating to Rust 2024. Closes rust-lang#127747 (I think?) cc ``@workingjubilee`` I have been testing a variety of targets, and I feel like they are all pretty much covered. I'll continue doing some testing async, but I don't expect to catch any more.
2 parents 27d76de + 5f7a207 commit f49474e
Copy full SHA for f49474e

File tree

Expand file treeCollapse file tree

24 files changed

+181
-139
lines changed
Filter options
Expand file treeCollapse file tree

24 files changed

+181
-139
lines changed

‎core/src/alloc/global.rs

Copy file name to clipboardExpand all lines: core/src/alloc/global.rs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use crate::{cmp, ptr};
7070
/// {
7171
/// return null_mut();
7272
/// };
73-
/// self.arena.get().cast::<u8>().add(allocated)
73+
/// unsafe { self.arena.get().cast::<u8>().add(allocated) }
7474
/// }
7575
/// unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
7676
/// }

‎core/src/hint.rs

Copy file name to clipboardExpand all lines: core/src/hint.rs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use crate::{intrinsics, ub_checks};
5252
/// // Safety: `divisor` can't be zero because of `prepare_inputs`,
5353
/// // but the compiler does not know about this. We *promise*
5454
/// // that we always call `prepare_inputs`.
55-
/// std::hint::unreachable_unchecked()
55+
/// unsafe { std::hint::unreachable_unchecked() }
5656
/// }
5757
/// // The compiler would normally introduce a check here that prevents
5858
/// // a division by zero. However, if `divisor` was zero, the branch

‎core/src/intrinsics/mod.rs

Copy file name to clipboardExpand all lines: core/src/intrinsics/mod.rs
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,12 +1703,12 @@ pub const fn forget<T: ?Sized>(_: T) {
17031703
/// ```
17041704
/// struct R<'a>(&'a i32);
17051705
/// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
1706-
/// std::mem::transmute::<R<'b>, R<'static>>(r)
1706+
/// unsafe { std::mem::transmute::<R<'b>, R<'static>>(r) }
17071707
/// }
17081708
///
17091709
/// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
17101710
/// -> &'b mut R<'c> {
1711-
/// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
1711+
/// unsafe { std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r) }
17121712
/// }
17131713
/// ```
17141714
///
@@ -4498,11 +4498,11 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
44984498
///
44994499
/// // SAFETY: Our precondition ensures the source is aligned and valid,
45004500
/// // and `Vec::with_capacity` ensures that we have usable space to write them.
4501-
/// ptr::copy(ptr, dst.as_mut_ptr(), elts);
4501+
/// unsafe { ptr::copy(ptr, dst.as_mut_ptr(), elts); }
45024502
///
45034503
/// // SAFETY: We created it with this much capacity earlier,
45044504
/// // and the previous `copy` has initialized these elements.
4505-
/// dst.set_len(elts);
4505+
/// unsafe { dst.set_len(elts); }
45064506
/// dst
45074507
/// }
45084508
/// ```

‎core/src/mem/maybe_uninit.rs

Copy file name to clipboardExpand all lines: core/src/mem/maybe_uninit.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ use crate::{fmt, intrinsics, ptr, slice};
9898
///
9999
/// unsafe fn make_vec(out: *mut Vec<i32>) {
100100
/// // `write` does not drop the old contents, which is important.
101-
/// out.write(vec![1, 2, 3]);
101+
/// unsafe { out.write(vec![1, 2, 3]); }
102102
/// }
103103
///
104104
/// let mut v = MaybeUninit::uninit();
@@ -844,7 +844,7 @@ impl<T> MaybeUninit<T> {
844844
/// # #![allow(unexpected_cfgs)]
845845
/// use std::mem::MaybeUninit;
846846
///
847-
/// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 1024]) { *buf = [0; 1024] }
847+
/// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 1024]) { unsafe { *buf = [0; 1024] } }
848848
/// # #[cfg(FALSE)]
849849
/// extern "C" {
850850
/// /// Initializes *all* the bytes of the input buffer.

‎core/src/mem/transmutability.rs

Copy file name to clipboardExpand all lines: core/src/mem/transmutability.rs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::marker::{ConstParamTy_, UnsizedConstParamTy};
3232
/// src: ManuallyDrop::new(src),
3333
/// };
3434
///
35-
/// let dst = transmute.dst;
35+
/// let dst = unsafe { transmute.dst };
3636
///
3737
/// ManuallyDrop::into_inner(dst)
3838
/// }

‎core/src/ptr/const_ptr.rs

Copy file name to clipboardExpand all lines: core/src/ptr/const_ptr.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,13 +724,13 @@ impl<T: ?Sized> *const T {
724724
/// that their safety preconditions are met:
725725
/// ```rust
726726
/// # #![feature(ptr_sub_ptr)]
727-
/// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool {
727+
/// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool { unsafe {
728728
/// ptr.sub_ptr(origin) == count
729729
/// # &&
730730
/// origin.add(count) == ptr
731731
/// # &&
732732
/// ptr.sub(count) == origin
733-
/// # }
733+
/// # } }
734734
/// ```
735735
///
736736
/// # Safety

‎core/src/ptr/mut_ptr.rs

Copy file name to clipboardExpand all lines: core/src/ptr/mut_ptr.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,13 +896,13 @@ impl<T: ?Sized> *mut T {
896896
/// that their safety preconditions are met:
897897
/// ```rust
898898
/// # #![feature(ptr_sub_ptr)]
899-
/// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool {
899+
/// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool { unsafe {
900900
/// ptr.sub_ptr(origin) == count
901901
/// # &&
902902
/// origin.add(count) == ptr
903903
/// # &&
904904
/// ptr.sub(count) == origin
905-
/// # }
905+
/// # } }
906906
/// ```
907907
///
908908
/// # Safety

‎core/src/ptr/non_null.rs

Copy file name to clipboardExpand all lines: core/src/ptr/non_null.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,13 +857,13 @@ impl<T: ?Sized> NonNull<T> {
857857
/// that their safety preconditions are met:
858858
/// ```rust
859859
/// # #![feature(ptr_sub_ptr)]
860-
/// # unsafe fn blah(ptr: std::ptr::NonNull<u32>, origin: std::ptr::NonNull<u32>, count: usize) -> bool {
860+
/// # unsafe fn blah(ptr: std::ptr::NonNull<u32>, origin: std::ptr::NonNull<u32>, count: usize) -> bool { unsafe {
861861
/// ptr.sub_ptr(origin) == count
862862
/// # &&
863863
/// origin.add(count) == ptr
864864
/// # &&
865865
/// ptr.sub(count) == origin
866-
/// # }
866+
/// # } }
867867
/// ```
868868
///
869869
/// # Safety

‎panic_abort/src/android.rs

Copy file name to clipboardExpand all lines: panic_abort/src/android.rs
+10-8Lines changed: 10 additions & 8 deletions
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

Copy file name to clipboardExpand all lines: panic_abort/src/lib.rs
+22-9Lines changed: 22 additions & 9 deletions
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

Copy file name to clipboardExpand all lines: panic_abort/src/zkvm.rs
+3-1Lines changed: 3 additions & 1 deletion
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
}

‎panic_unwind/src/emcc.rs

Copy file name to clipboardExpand all lines: panic_unwind/src/emcc.rs
+32-28Lines changed: 32 additions & 28 deletions
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

Copy file name to clipboardExpand all lines: panic_unwind/src/gcc.rs
+21-19Lines changed: 21 additions & 19 deletions
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

Copy file name to clipboardExpand all lines: panic_unwind/src/hermit.rs
+6-2Lines changed: 6 additions & 2 deletions
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
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.