From ac0fd279184583d682bb8c0f735cd4b1ca56c903 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 14 Jul 2024 16:43:50 -0700 Subject: [PATCH 1/7] std: deny(unsafe_op_in_unsafe_fn) but allow sites This provides a list of locations to hunt down issues in. --- std/src/collections/hash/map.rs | 2 ++ std/src/env.rs | 1 + std/src/ffi/mod.rs | 1 + std/src/io/mod.rs | 1 + std/src/lib.rs | 3 ++- std/src/os/mod.rs | 1 + std/src/sync/mod.rs | 1 + std/src/sys/mod.rs | 2 ++ std/src/sys_common/mod.rs | 1 + 9 files changed, 12 insertions(+), 1 deletion(-) diff --git a/std/src/collections/hash/map.rs b/std/src/collections/hash/map.rs index fcd1c307b5af6..955f4013ab652 100644 --- a/std/src/collections/hash/map.rs +++ b/std/src/collections/hash/map.rs @@ -1,3 +1,5 @@ +#![allow(unsafe_op_in_unsafe_fn)] + #[cfg(test)] mod tests; diff --git a/std/src/env.rs b/std/src/env.rs index 2f35e721610e0..b47b6531d5380 100644 --- a/std/src/env.rs +++ b/std/src/env.rs @@ -9,6 +9,7 @@ //! and those without will return a [`String`]. #![stable(feature = "env", since = "1.0.0")] +#![allow(unsafe_op_in_unsafe_fn)] #[cfg(test)] mod tests; diff --git a/std/src/ffi/mod.rs b/std/src/ffi/mod.rs index f45fd77e8b167..bb64f07844b11 100644 --- a/std/src/ffi/mod.rs +++ b/std/src/ffi/mod.rs @@ -160,6 +160,7 @@ //! [`from_wide`]: crate::os::windows::ffi::OsStringExt::from_wide "os::windows::ffi::OsStringExt::from_wide" #![stable(feature = "rust1", since = "1.0.0")] +#![allow(unsafe_op_in_unsafe_fn)] #[unstable(feature = "c_str_module", issue = "112134")] pub mod c_str; diff --git a/std/src/io/mod.rs b/std/src/io/mod.rs index b464461277a0f..d69c98ce79e69 100644 --- a/std/src/io/mod.rs +++ b/std/src/io/mod.rs @@ -293,6 +293,7 @@ //! [`Arc`]: crate::sync::Arc #![stable(feature = "rust1", since = "1.0.0")] +#![allow(unsafe_op_in_unsafe_fn)] #[cfg(test)] mod tests; diff --git a/std/src/lib.rs b/std/src/lib.rs index 66aeb35aceec0..d4d68c2068d8a 100644 --- a/std/src/lib.rs +++ b/std/src/lib.rs @@ -252,6 +252,7 @@ #![allow(internal_features)] #![deny(rustc::existing_doc_keyword)] #![deny(fuzzy_provenance_casts)] +#![deny(unsafe_op_in_unsafe_fn)] #![allow(rustdoc::redundant_explicit_links)] // Ensure that std can be linked against panic_abort despite compiled with `-C panic=unwind` #![deny(ffi_unwind_calls)] @@ -664,7 +665,7 @@ pub mod alloc; mod panicking; #[path = "../../backtrace/src/lib.rs"] -#[allow(dead_code, unused_attributes, fuzzy_provenance_casts)] +#[allow(dead_code, unused_attributes, fuzzy_provenance_casts, unsafe_op_in_unsafe_fn)] mod backtrace_rs; // Re-export macros defined in core. diff --git a/std/src/os/mod.rs b/std/src/os/mod.rs index 455fdd3c34d77..020a8b324f410 100644 --- a/std/src/os/mod.rs +++ b/std/src/os/mod.rs @@ -2,6 +2,7 @@ #![stable(feature = "os", since = "1.0.0")] #![allow(missing_docs, nonstandard_style, missing_debug_implementations)] +#![allow(unsafe_op_in_unsafe_fn)] pub mod raw; diff --git a/std/src/sync/mod.rs b/std/src/sync/mod.rs index 9a38c42f43a02..5cabdc3a9c7ec 100644 --- a/std/src/sync/mod.rs +++ b/std/src/sync/mod.rs @@ -157,6 +157,7 @@ //! [`RwLock`]: crate::sync::RwLock #![stable(feature = "rust1", since = "1.0.0")] +#![allow(unsafe_op_in_unsafe_fn)] #[stable(feature = "rust1", since = "1.0.0")] pub use alloc_crate::sync::{Arc, Weak}; diff --git a/std/src/sys/mod.rs b/std/src/sys/mod.rs index 4092e6080d548..e50758ce00d8b 100644 --- a/std/src/sys/mod.rs +++ b/std/src/sys/mod.rs @@ -1,3 +1,5 @@ +#![allow(unsafe_op_in_unsafe_fn)] + /// The PAL (platform abstraction layer) contains platform-specific abstractions /// for implementing the features in the other submodules, e.g. UNIX file /// descriptors. diff --git a/std/src/sys_common/mod.rs b/std/src/sys_common/mod.rs index 60ee405ecaaa2..fc4d5592981fc 100644 --- a/std/src/sys_common/mod.rs +++ b/std/src/sys_common/mod.rs @@ -16,6 +16,7 @@ #![allow(missing_docs)] #![allow(missing_debug_implementations)] +#![allow(unsafe_op_in_unsafe_fn)] #[cfg(test)] mod tests; From 4679f9a6cff7886dd3a53067920aaf88ccd53931 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 14 Jul 2024 16:49:02 -0700 Subject: [PATCH 2/7] std: Unsafe-wrap HashMap::get_many_unchecked_mut --- std/src/collections/hash/map.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/std/src/collections/hash/map.rs b/std/src/collections/hash/map.rs index 955f4013ab652..1f6a3e904795a 100644 --- a/std/src/collections/hash/map.rs +++ b/std/src/collections/hash/map.rs @@ -1,5 +1,3 @@ -#![allow(unsafe_op_in_unsafe_fn)] - #[cfg(test)] mod tests; @@ -1020,7 +1018,7 @@ where K: Borrow, Q: Hash + Eq, { - self.base.get_many_unchecked_mut(ks) + unsafe { self.base.get_many_unchecked_mut(ks) } } /// Returns `true` if the map contains a value for the specified key. From 8c75111da55f118007205b7d10d7be67a5c29949 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 14 Jul 2024 16:59:12 -0700 Subject: [PATCH 3/7] std: Unsafe-wrap OSStr{,ing}::from_encoded_bytes_unchecked --- std/src/ffi/mod.rs | 1 - std/src/ffi/os_str.rs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/std/src/ffi/mod.rs b/std/src/ffi/mod.rs index bb64f07844b11..f45fd77e8b167 100644 --- a/std/src/ffi/mod.rs +++ b/std/src/ffi/mod.rs @@ -160,7 +160,6 @@ //! [`from_wide`]: crate::os::windows::ffi::OsStringExt::from_wide "os::windows::ffi::OsStringExt::from_wide" #![stable(feature = "rust1", since = "1.0.0")] -#![allow(unsafe_op_in_unsafe_fn)] #[unstable(feature = "c_str_module", issue = "112134")] pub mod c_str; diff --git a/std/src/ffi/os_str.rs b/std/src/ffi/os_str.rs index 4a417c84a30a5..f9dba08da4c3c 100644 --- a/std/src/ffi/os_str.rs +++ b/std/src/ffi/os_str.rs @@ -184,7 +184,7 @@ impl OsString { #[inline] #[stable(feature = "os_str_bytes", since = "1.74.0")] pub unsafe fn from_encoded_bytes_unchecked(bytes: Vec) -> Self { - OsString { inner: Buf::from_encoded_bytes_unchecked(bytes) } + OsString { inner: unsafe { Buf::from_encoded_bytes_unchecked(bytes) } } } /// Converts to an [`OsStr`] slice. @@ -813,7 +813,7 @@ impl OsStr { #[inline] #[stable(feature = "os_str_bytes", since = "1.74.0")] pub unsafe fn from_encoded_bytes_unchecked(bytes: &[u8]) -> &Self { - Self::from_inner(Slice::from_encoded_bytes_unchecked(bytes)) + Self::from_inner(unsafe { Slice::from_encoded_bytes_unchecked(bytes) }) } #[inline] From 91b7331a8259b374e90082cfb86ae98f4d54d274 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 14 Jul 2024 17:07:19 -0700 Subject: [PATCH 4/7] std: Directly call unsafe {un,}setenv in env --- std/src/env.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/std/src/env.rs b/std/src/env.rs index b47b6531d5380..36add02d68c5c 100644 --- a/std/src/env.rs +++ b/std/src/env.rs @@ -9,7 +9,6 @@ //! and those without will return a [`String`]. #![stable(feature = "env", since = "1.0.0")] -#![allow(unsafe_op_in_unsafe_fn)] #[cfg(test)] mod tests; @@ -367,11 +366,8 @@ impl Error for VarError { #[rustc_deprecated_safe_2024] #[stable(feature = "env", since = "1.0.0")] pub unsafe fn set_var, V: AsRef>(key: K, value: V) { - _set_var(key.as_ref(), value.as_ref()) -} - -unsafe fn _set_var(key: &OsStr, value: &OsStr) { - os_imp::setenv(key, value).unwrap_or_else(|e| { + let (key, value) = (key.as_ref(), value.as_ref()); + unsafe { os_imp::setenv(key, value) }.unwrap_or_else(|e| { panic!("failed to set environment variable `{key:?}` to `{value:?}`: {e}") }) } @@ -434,11 +430,8 @@ unsafe fn _set_var(key: &OsStr, value: &OsStr) { #[rustc_deprecated_safe_2024] #[stable(feature = "env", since = "1.0.0")] pub unsafe fn remove_var>(key: K) { - _remove_var(key.as_ref()) -} - -unsafe fn _remove_var(key: &OsStr) { - os_imp::unsetenv(key) + let key = key.as_ref(); + unsafe { os_imp::unsetenv(key) } .unwrap_or_else(|e| panic!("failed to remove environment variable `{key:?}`: {e}")) } From 8c3a9c1c93949d449a852cc9856e47527777fac4 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 14 Jul 2024 17:14:55 -0700 Subject: [PATCH 5/7] std: Unsafe-wrap std::io --- std/src/io/buffered/bufwriter.rs | 8 +++++--- std/src/io/cursor.rs | 2 +- std/src/io/error/repr_bitpacked.rs | 7 +++++-- std/src/io/mod.rs | 5 ++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/std/src/io/buffered/bufwriter.rs b/std/src/io/buffered/bufwriter.rs index 1768bb05ddbcb..a8680e9b6ead1 100644 --- a/std/src/io/buffered/bufwriter.rs +++ b/std/src/io/buffered/bufwriter.rs @@ -433,9 +433,11 @@ impl BufWriter { let old_len = self.buf.len(); let buf_len = buf.len(); let src = buf.as_ptr(); - let dst = self.buf.as_mut_ptr().add(old_len); - ptr::copy_nonoverlapping(src, dst, buf_len); - self.buf.set_len(old_len + buf_len); + unsafe { + let dst = self.buf.as_mut_ptr().add(old_len); + ptr::copy_nonoverlapping(src, dst, buf_len); + self.buf.set_len(old_len + buf_len); + } } #[inline] diff --git a/std/src/io/cursor.rs b/std/src/io/cursor.rs index a1a8b2a3505c7..2ed64a40495ef 100644 --- a/std/src/io/cursor.rs +++ b/std/src/io/cursor.rs @@ -482,7 +482,7 @@ where A: Allocator, { debug_assert!(vec.capacity() >= pos + buf.len()); - vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()); + unsafe { vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()) }; pos + buf.len() } diff --git a/std/src/io/error/repr_bitpacked.rs b/std/src/io/error/repr_bitpacked.rs index a5cefe2292be4..fbb74967df3f1 100644 --- a/std/src/io/error/repr_bitpacked.rs +++ b/std/src/io/error/repr_bitpacked.rs @@ -267,11 +267,14 @@ where // Using this rather than unwrap meaningfully improves the code // for callers which only care about one variant (usually // `Custom`) - core::hint::unreachable_unchecked(); + unsafe { core::hint::unreachable_unchecked() }; }); ErrorData::Simple(kind) } - TAG_SIMPLE_MESSAGE => ErrorData::SimpleMessage(&*ptr.cast::().as_ptr()), + TAG_SIMPLE_MESSAGE => { + // SAFETY: per tag + unsafe { ErrorData::SimpleMessage(&*ptr.cast::().as_ptr()) } + } TAG_CUSTOM => { // It would be correct for us to use `ptr::byte_sub` here (see the // comment above the `wrapping_add` call in `new_custom` for why), diff --git a/std/src/io/mod.rs b/std/src/io/mod.rs index d69c98ce79e69..1345a30361e28 100644 --- a/std/src/io/mod.rs +++ b/std/src/io/mod.rs @@ -293,7 +293,6 @@ //! [`Arc`]: crate::sync::Arc #![stable(feature = "rust1", since = "1.0.0")] -#![allow(unsafe_op_in_unsafe_fn)] #[cfg(test)] mod tests; @@ -383,11 +382,11 @@ pub(crate) unsafe fn append_to_string(buf: &mut String, f: F) -> Result) -> Result, { - let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() }; + let mut g = Guard { len: buf.len(), buf: unsafe { buf.as_mut_vec() } }; let ret = f(g.buf); // SAFETY: the caller promises to only append data to `buf` - let appended = g.buf.get_unchecked(g.len..); + let appended = unsafe { g.buf.get_unchecked(g.len..) }; if str::from_utf8(appended).is_err() { ret.and_then(|_| Err(Error::INVALID_UTF8)) } else { From e8fa3ef2c2430b339fde99e08ade92488bb06654 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 14 Jul 2024 17:39:40 -0700 Subject: [PATCH 6/7] std: Unsafe-wrap in Wtf8 impl --- std/src/sys_common/mod.rs | 1 - std/src/sys_common/wtf8.rs | 14 ++++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/std/src/sys_common/mod.rs b/std/src/sys_common/mod.rs index fc4d5592981fc..60ee405ecaaa2 100644 --- a/std/src/sys_common/mod.rs +++ b/std/src/sys_common/mod.rs @@ -16,7 +16,6 @@ #![allow(missing_docs)] #![allow(missing_debug_implementations)] -#![allow(unsafe_op_in_unsafe_fn)] #[cfg(test)] mod tests; diff --git a/std/src/sys_common/wtf8.rs b/std/src/sys_common/wtf8.rs index 117a3e23044ea..6aeeb6259285d 100644 --- a/std/src/sys_common/wtf8.rs +++ b/std/src/sys_common/wtf8.rs @@ -602,7 +602,8 @@ impl Wtf8 { /// marked unsafe. #[inline] pub unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 { - mem::transmute(value) + // SAFETY: start with &[u8], end with fancy &[u8] + unsafe { &*(value as *const [u8] as *const Wtf8) } } /// Creates a mutable WTF-8 slice from a mutable WTF-8 byte slice. @@ -611,7 +612,8 @@ impl Wtf8 { /// marked unsafe. #[inline] unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Wtf8 { - mem::transmute(value) + // SAFETY: start with &mut [u8], end with fancy &mut [u8] + unsafe { &mut *(value as *mut [u8] as *mut Wtf8) } } /// Returns the length, in WTF-8 bytes. @@ -942,8 +944,12 @@ pub fn check_utf8_boundary(slice: &Wtf8, index: usize) { /// Copied from core::str::raw::slice_unchecked #[inline] pub unsafe fn slice_unchecked(s: &Wtf8, begin: usize, end: usize) -> &Wtf8 { - // memory layout of a &[u8] and &Wtf8 are the same - Wtf8::from_bytes_unchecked(slice::from_raw_parts(s.bytes.as_ptr().add(begin), end - begin)) + // SAFETY: memory layout of a &[u8] and &Wtf8 are the same + unsafe { + let len = end - begin; + let start = s.as_bytes().as_ptr().add(begin); + Wtf8::from_bytes_unchecked(slice::from_raw_parts(start, len)) + } } /// Copied from core::str::raw::slice_error_fail From 5ff7b404d5e72280934fd237d9065841c13d7c10 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 14 Jul 2024 17:59:37 -0700 Subject: [PATCH 7/7] std: Unsafe-wrap std::sync --- std/src/sync/mod.rs | 1 - std/src/sync/mpmc/array.rs | 22 ++++++++++++-------- std/src/sync/mpmc/counter.rs | 4 ++-- std/src/sync/mpmc/list.rs | 38 +++++++++++++++++++--------------- std/src/sync/mpmc/zero.rs | 20 +++++++++++------- std/src/sync/once_lock.rs | 4 ++-- std/src/sync/reentrant_lock.rs | 4 +++- std/src/sync/rwlock.rs | 2 +- 8 files changed, 54 insertions(+), 41 deletions(-) diff --git a/std/src/sync/mod.rs b/std/src/sync/mod.rs index 5cabdc3a9c7ec..9a38c42f43a02 100644 --- a/std/src/sync/mod.rs +++ b/std/src/sync/mod.rs @@ -157,7 +157,6 @@ //! [`RwLock`]: crate::sync::RwLock #![stable(feature = "rust1", since = "1.0.0")] -#![allow(unsafe_op_in_unsafe_fn)] #[stable(feature = "rust1", since = "1.0.0")] pub use alloc_crate::sync::{Arc, Weak}; diff --git a/std/src/sync/mpmc/array.rs b/std/src/sync/mpmc/array.rs index 492e21d9bdb63..185319add745f 100644 --- a/std/src/sync/mpmc/array.rs +++ b/std/src/sync/mpmc/array.rs @@ -200,11 +200,12 @@ impl Channel { return Err(msg); } - let slot: &Slot = &*(token.array.slot as *const Slot); - // Write the message into the slot and update the stamp. - slot.msg.get().write(MaybeUninit::new(msg)); - slot.stamp.store(token.array.stamp, Ordering::Release); + unsafe { + let slot: &Slot = &*(token.array.slot as *const Slot); + slot.msg.get().write(MaybeUninit::new(msg)); + slot.stamp.store(token.array.stamp, Ordering::Release); + } // Wake a sleeping receiver. self.receivers.notify(); @@ -291,11 +292,14 @@ impl Channel { return Err(()); } - let slot: &Slot = &*(token.array.slot as *const Slot); - // Read the message from the slot and update the stamp. - let msg = slot.msg.get().read().assume_init(); - slot.stamp.store(token.array.stamp, Ordering::Release); + let msg = unsafe { + let slot: &Slot = &*(token.array.slot as *const Slot); + + let msg = slot.msg.get().read().assume_init(); + slot.stamp.store(token.array.stamp, Ordering::Release); + msg + }; // Wake a sleeping sender. self.senders.notify(); @@ -471,7 +475,7 @@ impl Channel { false }; - self.discard_all_messages(tail); + unsafe { self.discard_all_messages(tail) }; disconnected } diff --git a/std/src/sync/mpmc/counter.rs b/std/src/sync/mpmc/counter.rs index a5a6bdc67f13f..3478cf41dc9d2 100644 --- a/std/src/sync/mpmc/counter.rs +++ b/std/src/sync/mpmc/counter.rs @@ -63,7 +63,7 @@ impl Sender { disconnect(&self.counter().chan); if self.counter().destroy.swap(true, Ordering::AcqRel) { - drop(Box::from_raw(self.counter)); + drop(unsafe { Box::from_raw(self.counter) }); } } } @@ -116,7 +116,7 @@ impl Receiver { disconnect(&self.counter().chan); if self.counter().destroy.swap(true, Ordering::AcqRel) { - drop(Box::from_raw(self.counter)); + drop(unsafe { Box::from_raw(self.counter) }); } } } diff --git a/std/src/sync/mpmc/list.rs b/std/src/sync/mpmc/list.rs index 9e7148c716cda..edac7a0cb1835 100644 --- a/std/src/sync/mpmc/list.rs +++ b/std/src/sync/mpmc/list.rs @@ -91,7 +91,7 @@ impl Block { // It is not necessary to set the `DESTROY` bit in the last slot because that slot has // begun destruction of the block. for i in start..BLOCK_CAP - 1 { - let slot = (*this).slots.get_unchecked(i); + let slot = unsafe { (*this).slots.get_unchecked(i) }; // Mark the `DESTROY` bit if a thread is still using the slot. if slot.state.load(Ordering::Acquire) & READ == 0 @@ -103,7 +103,7 @@ impl Block { } // No thread is using the block, now it is safe to destroy it. - drop(Box::from_raw(this)); + drop(unsafe { Box::from_raw(this) }); } } @@ -265,9 +265,11 @@ impl Channel { // Write the message into the slot. let block = token.list.block as *mut Block; let offset = token.list.offset; - let slot = (*block).slots.get_unchecked(offset); - slot.msg.get().write(MaybeUninit::new(msg)); - slot.state.fetch_or(WRITE, Ordering::Release); + unsafe { + let slot = (*block).slots.get_unchecked(offset); + slot.msg.get().write(MaybeUninit::new(msg)); + slot.state.fetch_or(WRITE, Ordering::Release); + } // Wake a sleeping receiver. self.receivers.notify(); @@ -369,19 +371,21 @@ impl Channel { // Read the message. let block = token.list.block as *mut Block; let offset = token.list.offset; - let slot = (*block).slots.get_unchecked(offset); - slot.wait_write(); - let msg = slot.msg.get().read().assume_init(); - - // Destroy the block if we've reached the end, or if another thread wanted to destroy but - // couldn't because we were busy reading from the slot. - if offset + 1 == BLOCK_CAP { - Block::destroy(block, 0); - } else if slot.state.fetch_or(READ, Ordering::AcqRel) & DESTROY != 0 { - Block::destroy(block, offset + 1); - } + unsafe { + let slot = (*block).slots.get_unchecked(offset); + slot.wait_write(); + let msg = slot.msg.get().read().assume_init(); + + // Destroy the block if we've reached the end, or if another thread wanted to destroy but + // couldn't because we were busy reading from the slot. + if offset + 1 == BLOCK_CAP { + Block::destroy(block, 0); + } else if slot.state.fetch_or(READ, Ordering::AcqRel) & DESTROY != 0 { + Block::destroy(block, offset + 1); + } - Ok(msg) + Ok(msg) + } } /// Attempts to send a message into the channel. diff --git a/std/src/sync/mpmc/zero.rs b/std/src/sync/mpmc/zero.rs index 1b82713edc748..6d1c9d64e7a7a 100644 --- a/std/src/sync/mpmc/zero.rs +++ b/std/src/sync/mpmc/zero.rs @@ -103,9 +103,11 @@ impl Channel { return Err(msg); } - let packet = &*(token.zero.0 as *const Packet); - packet.msg.get().write(Some(msg)); - packet.ready.store(true, Ordering::Release); + unsafe { + let packet = &*(token.zero.0 as *const Packet); + packet.msg.get().write(Some(msg)); + packet.ready.store(true, Ordering::Release); + } Ok(()) } @@ -116,22 +118,24 @@ impl Channel { return Err(()); } - let packet = &*(token.zero.0 as *const Packet); + let packet = unsafe { &*(token.zero.0 as *const Packet) }; if packet.on_stack { // The message has been in the packet from the beginning, so there is no need to wait // for it. However, after reading the message, we need to set `ready` to `true` in // order to signal that the packet can be destroyed. - let msg = packet.msg.get().replace(None).unwrap(); + let msg = unsafe { packet.msg.get().replace(None) }.unwrap(); packet.ready.store(true, Ordering::Release); Ok(msg) } else { // Wait until the message becomes available, then read it and destroy the // heap-allocated packet. packet.wait_ready(); - let msg = packet.msg.get().replace(None).unwrap(); - drop(Box::from_raw(token.zero.0 as *mut Packet)); - Ok(msg) + unsafe { + let msg = packet.msg.get().replace(None).unwrap(); + drop(Box::from_raw(token.zero.0 as *mut Packet)); + Ok(msg) + } } } diff --git a/std/src/sync/once_lock.rs b/std/src/sync/once_lock.rs index fe243550606f3..94955beaf37b7 100644 --- a/std/src/sync/once_lock.rs +++ b/std/src/sync/once_lock.rs @@ -502,7 +502,7 @@ impl OnceLock { #[inline] unsafe fn get_unchecked(&self) -> &T { debug_assert!(self.is_initialized()); - (&*self.value.get()).assume_init_ref() + unsafe { (&*self.value.get()).assume_init_ref() } } /// # Safety @@ -511,7 +511,7 @@ impl OnceLock { #[inline] unsafe fn get_unchecked_mut(&mut self) -> &mut T { debug_assert!(self.is_initialized()); - (&mut *self.value.get()).assume_init_mut() + unsafe { (&mut *self.value.get()).assume_init_mut() } } } diff --git a/std/src/sync/reentrant_lock.rs b/std/src/sync/reentrant_lock.rs index f7fe8eb1c7fd5..042c439394e06 100644 --- a/std/src/sync/reentrant_lock.rs +++ b/std/src/sync/reentrant_lock.rs @@ -244,7 +244,9 @@ impl ReentrantLock { } unsafe fn increment_lock_count(&self) -> Option<()> { - *self.lock_count.get() = (*self.lock_count.get()).checked_add(1)?; + unsafe { + *self.lock_count.get() = (*self.lock_count.get()).checked_add(1)?; + } Some(()) } } diff --git a/std/src/sync/rwlock.rs b/std/src/sync/rwlock.rs index e0a8a7603d71a..a4ec52a4abe63 100644 --- a/std/src/sync/rwlock.rs +++ b/std/src/sync/rwlock.rs @@ -578,7 +578,7 @@ impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> { // successfully called from the same thread before instantiating this object. unsafe fn new(lock: &'rwlock RwLock) -> LockResult> { poison::map_result(lock.poison.borrow(), |()| RwLockReadGuard { - data: NonNull::new_unchecked(lock.data.get()), + data: unsafe { NonNull::new_unchecked(lock.data.get()) }, inner_lock: &lock.inner, }) }