Skip to content

Rollup of 3 pull requests #127757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4572ed6
std: deny(unsafe_op_in_unsafe_fn) but allow sites
workingjubilee Jul 14, 2024
87d850d
std: Unsafe-wrap HashMap::get_many_unchecked_mut
workingjubilee Jul 14, 2024
ce35265
std: Unsafe-wrap OSStr{,ing}::from_encoded_bytes_unchecked
workingjubilee Jul 14, 2024
83a0fe5
std: Directly call unsafe {un,}setenv in env
workingjubilee Jul 15, 2024
df353a0
std: Unsafe-wrap std::io
workingjubilee Jul 15, 2024
64fb236
std: Unsafe-wrap in Wtf8 impl
workingjubilee Jul 15, 2024
e324602
std: Unsafe-wrap std::sync
workingjubilee Jul 15, 2024
1d1cae1
Remove NonZeroDWORD
ChrisDenton Jul 14, 2024
91ba4eb
Remove LARGE_INTEGER
ChrisDenton Jul 14, 2024
65da4af
Remove LONG
ChrisDenton Jul 14, 2024
b107cfa
Remove UINT
ChrisDenton Jul 14, 2024
e70cc28
Remove LPWSTR
ChrisDenton Jul 14, 2024
f2cc943
Remove USHORT
ChrisDenton Jul 14, 2024
5b700a7
Remove CHAR
ChrisDenton Jul 14, 2024
286c327
Remove SIZE_T
ChrisDenton Jul 14, 2024
8052fb8
Remove LPCVOID
ChrisDenton Jul 14, 2024
1b7cf3a
Remove LPOVERLAPPED
ChrisDenton Jul 14, 2024
351f1f3
Remove LPSECURITY_ATTRIBUTES
ChrisDenton Jul 14, 2024
84dd7e4
Remove LPVOID
ChrisDenton Jul 14, 2024
21f69b5
Remove PSRWLOCK
ChrisDenton Jul 14, 2024
d8d7c5c
Remove ULONG
ChrisDenton Jul 14, 2024
e2b062c
Remove DWORD
ChrisDenton Jul 14, 2024
8a1ce3d
Make normalization regex less exact
ChrisDenton Jul 14, 2024
ffe8fc2
Don't re-export `c_int` from `c`
ChrisDenton Jul 14, 2024
816d90a
Fix Windows 7
ChrisDenton Jul 15, 2024
2402e84
Make pal/windows default to deny unsafe in unsafe
ChrisDenton Jul 15, 2024
3411a02
Make os/windows default to deny unsafe in unsafe
ChrisDenton Jul 15, 2024
7e16d5f
Move safety comment outside unsafe block
ChrisDenton Jul 15, 2024
64495b5
Rollup merge of #127712 - ChrisDenton:raw-types, r=workingjubilee
workingjubilee Jul 15, 2024
99c5302
Rollup merge of #127744 - workingjubilee:deny-unsafe-op-in-std, r=jhp…
workingjubilee Jul 15, 2024
476d399
Rollup merge of #127750 - ChrisDenton:safe-unsafe-unsafe, r=workingju…
workingjubilee Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ where
K: Borrow<Q>,
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.
Expand Down
14 changes: 4 additions & 10 deletions library/std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,8 @@ impl Error for VarError {
#[rustc_deprecated_safe_2024]
#[stable(feature = "env", since = "1.0.0")]
pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(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}")
})
}
Expand Down Expand Up @@ -433,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<K: AsRef<OsStr>>(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}"))
}

Expand Down
4 changes: 2 additions & 2 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>) -> Self {
OsString { inner: Buf::from_encoded_bytes_unchecked(bytes) }
OsString { inner: unsafe { Buf::from_encoded_bytes_unchecked(bytes) } }
}

/// Converts to an [`OsStr`] slice.
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 5 additions & 3 deletions library/std/src/io/buffered/bufwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ impl<W: ?Sized + Write> BufWriter<W> {
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]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
7 changes: 5 additions & 2 deletions library/std/src/io/error/repr_bitpacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<SimpleMessage>().as_ptr()),
TAG_SIMPLE_MESSAGE => {
// SAFETY: per tag
unsafe { ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().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),
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ pub(crate) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize
where
F: FnOnce(&mut Vec<u8>) -> Result<usize>,
{
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 {
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions library/std/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
28 changes: 18 additions & 10 deletions library/std/src/os/windows/io/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@ fn stdio_handle(raw: RawHandle) -> RawHandle {
impl FromRawHandle for fs::File {
#[inline]
unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
let handle = handle as sys::c::HANDLE;
fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
OwnedHandle::from_raw_handle(handle),
)))
unsafe {
let handle = handle as sys::c::HANDLE;
fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
OwnedHandle::from_raw_handle(handle),
)))
}
}
}

Expand Down Expand Up @@ -260,24 +262,30 @@ impl AsRawSocket for net::UdpSocket {
impl FromRawSocket for net::TcpStream {
#[inline]
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream {
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock))
unsafe {
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock))
}
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawSocket for net::TcpListener {
#[inline]
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener {
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock))
unsafe {
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock))
}
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawSocket for net::UdpSocket {
#[inline]
unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket {
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock))
unsafe {
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock))
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions library/std/src/os/windows/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl BorrowedSocket<'_> {
#[stable(feature = "io_safety", since = "1.63.0")]
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
assert!(socket != sys::c::INVALID_SOCKET as RawSocket);
Self { socket, _phantom: PhantomData }
unsafe { Self { socket, _phantom: PhantomData } }
}
}

Expand Down Expand Up @@ -201,8 +201,10 @@ impl IntoRawSocket for OwnedSocket {
impl FromRawSocket for OwnedSocket {
#[inline]
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
Self { socket }
unsafe {
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
Self { socket }
}
}
}

Expand Down
1 change: 1 addition & 0 deletions library/std/src/os/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(cfg(windows))]
#![deny(unsafe_op_in_unsafe_fn)]

pub mod ffi;
pub mod fs;
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
#[stable(feature = "process_extensions", since = "1.2.0")]
impl FromRawHandle for process::Stdio {
unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
let handle = sys::handle::Handle::from_raw_handle(handle as *mut _);
let handle = unsafe { sys::handle::Handle::from_raw_handle(handle as *mut _) };
let io = sys::process::Stdio::Handle(handle);
process::Stdio::from_inner(io)
}
Expand Down Expand Up @@ -407,7 +407,7 @@ impl CommandExt for process::Command {
attribute: usize,
value: T,
) -> &mut process::Command {
self.as_inner_mut().raw_attribute(attribute, value);
unsafe { self.as_inner_mut().raw_attribute(attribute, value) };
self
}
}
Expand Down
22 changes: 9 additions & 13 deletions library/std/src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,29 +385,25 @@ fn test_interior_nul_in_env_value_is_error() {
#[cfg(windows)]
fn test_creation_flags() {
use crate::os::windows::process::CommandExt;
use crate::sys::c::{BOOL, DWORD, INFINITE};
use crate::sys::c::{BOOL, INFINITE};
#[repr(C)]
struct DEBUG_EVENT {
pub event_code: DWORD,
pub process_id: DWORD,
pub thread_id: DWORD,
pub event_code: u32,
pub process_id: u32,
pub thread_id: u32,
// This is a union in the real struct, but we don't
// need this data for the purposes of this test.
pub _junk: [u8; 164],
}

extern "system" {
fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL;
fn ContinueDebugEvent(
dwProcessId: DWORD,
dwThreadId: DWORD,
dwContinueStatus: DWORD,
) -> BOOL;
fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: u32) -> BOOL;
fn ContinueDebugEvent(dwProcessId: u32, dwThreadId: u32, dwContinueStatus: u32) -> BOOL;
}

const DEBUG_PROCESS: DWORD = 1;
const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
const DBG_EXCEPTION_NOT_HANDLED: DWORD = 0x80010001;
const DEBUG_PROCESS: u32 = 1;
const EXIT_PROCESS_DEBUG_EVENT: u32 = 5;
const DBG_EXCEPTION_NOT_HANDLED: u32 = 0x80010001;

let mut child =
Command::new("cmd").creation_flags(DEBUG_PROCESS).stdin(Stdio::piped()).spawn().unwrap();
Expand Down
22 changes: 13 additions & 9 deletions library/std/src/sync/mpmc/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ impl<T> Channel<T> {
return Err(msg);
}

let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);

// 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<T> = &*(token.array.slot as *const Slot<T>);
slot.msg.get().write(MaybeUninit::new(msg));
slot.stamp.store(token.array.stamp, Ordering::Release);
}

// Wake a sleeping receiver.
self.receivers.notify();
Expand Down Expand Up @@ -291,11 +292,14 @@ impl<T> Channel<T> {
return Err(());
}

let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);

// 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<T> = &*(token.array.slot as *const Slot<T>);

let msg = slot.msg.get().read().assume_init();
slot.stamp.store(token.array.stamp, Ordering::Release);
msg
};

// Wake a sleeping sender.
self.senders.notify();
Expand Down Expand Up @@ -471,7 +475,7 @@ impl<T> Channel<T> {
false
};

self.discard_all_messages(tail);
unsafe { self.discard_all_messages(tail) };
disconnected
}

Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sync/mpmc/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<C> Sender<C> {
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) });
}
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<C> Receiver<C> {
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) });
}
}
}
Expand Down
Loading
Loading