1
Fork 0

Rollup merge of #128131 - ChrisDenton:stuff, r=workingjubilee

Import `c_void` rather than using the full path

Follow up to #128092. As requested, this imports `c_void` in more places. I also fixed up some imports to use `core` for core types instead of `crate`. While that is not strictly necessary, I think ideally things in `sys/pal` should only depend on itself or core so that the code is less spaghetti. We're far away from that ideal at the moment but I can at least try to slowly move in that direction.

Also this forbids `unsafe_op_in_unsafe_fn` for library/std/src/sys/pal/windows by fixing up the remaining unsafe bits that are just punting their unsafe requirements onto the caller of the `unsafe` function (or definition macro).

<!--
r? workingjubilee
-->
This commit is contained in:
Matthias Krüger 2024-07-24 18:00:40 +02:00 committed by GitHub
commit f3a7c3fd3b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 35 additions and 32 deletions

View file

@ -37,7 +37,7 @@ windows_targets::link!("kernel32.dll" "system" fn GetProcessHeap() -> c::HANDLE)
// Note that `dwBytes` is allowed to be zero, contrary to some other allocators. // Note that `dwBytes` is allowed to be zero, contrary to some other allocators.
// //
// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapalloc // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapalloc
windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap: c::HANDLE, dwflags: u32, dwbytes: usize) -> *mut core::ffi::c_void); windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap: c::HANDLE, dwflags: u32, dwbytes: usize) -> *mut c_void);
// Reallocate a block of memory behind a given pointer `lpMem` from a given heap `hHeap`, // Reallocate a block of memory behind a given pointer `lpMem` from a given heap `hHeap`,
// to a block of at least `dwBytes` bytes, either shrinking the block in place, // to a block of at least `dwBytes` bytes, either shrinking the block in place,
@ -61,9 +61,9 @@ windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap: c::HANDLE, dw
windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc( windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
hheap: c::HANDLE, hheap: c::HANDLE,
dwflags : u32, dwflags : u32,
lpmem: *const core::ffi::c_void, lpmem: *const c_void,
dwbytes: usize dwbytes: usize
) -> *mut core::ffi::c_void); ) -> *mut c_void);
// Free a block of memory behind a given pointer `lpMem` from a given heap `hHeap`. // Free a block of memory behind a given pointer `lpMem` from a given heap `hHeap`.
// Returns a nonzero value if the operation is successful, and zero if the operation fails. // Returns a nonzero value if the operation is successful, and zero if the operation fails.
@ -79,7 +79,7 @@ windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
// Note that `lpMem` is allowed to be null, which will not cause the operation to fail. // Note that `lpMem` is allowed to be null, which will not cause the operation to fail.
// //
// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
windows_targets::link!("kernel32.dll" "system" fn HeapFree(hheap: c::HANDLE, dwflags: u32, lpmem: *const core::ffi::c_void) -> c::BOOL); windows_targets::link!("kernel32.dll" "system" fn HeapFree(hheap: c::HANDLE, dwflags: u32, lpmem: *const c_void) -> c::BOOL);
// Cached handle to the default heap of the current process. // Cached handle to the default heap of the current process.
// Either a non-null handle returned by `GetProcessHeap`, or null when not yet initialized or `GetProcessHeap` failed. // Either a non-null handle returned by `GetProcessHeap`, or null when not yet initialized or `GetProcessHeap` failed.

View file

@ -4,12 +4,10 @@
#![cfg_attr(test, allow(dead_code))] #![cfg_attr(test, allow(dead_code))]
#![unstable(issue = "none", feature = "windows_c")] #![unstable(issue = "none", feature = "windows_c")]
#![allow(clippy::style)] #![allow(clippy::style)]
#![allow(unsafe_op_in_unsafe_fn)]
use crate::ffi::CStr; use core::ffi::{c_uint, c_ulong, c_ushort, c_void, CStr};
use crate::mem; use core::mem;
use crate::os::raw::{c_uint, c_ulong, c_ushort, c_void}; use core::ptr;
use crate::ptr;
pub(super) mod windows_targets; pub(super) mod windows_targets;
@ -136,26 +134,26 @@ compat_fn_with_fallback! {
// >= Win10 1607 // >= Win10 1607
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
pub fn SetThreadDescription(hthread: HANDLE, lpthreaddescription: PCWSTR) -> HRESULT { pub fn SetThreadDescription(hthread: HANDLE, lpthreaddescription: PCWSTR) -> HRESULT {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL }
} }
// >= Win10 1607 // >= Win10 1607
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreaddescription // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreaddescription
pub fn GetThreadDescription(hthread: HANDLE, lpthreaddescription: *mut PWSTR) -> HRESULT { pub fn GetThreadDescription(hthread: HANDLE, lpthreaddescription: *mut PWSTR) -> HRESULT {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL }
} }
// >= Win8 / Server 2012 // >= Win8 / Server 2012
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
#[cfg(target_vendor = "win7")] #[cfg(target_vendor = "win7")]
pub fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime: *mut FILETIME) -> () { pub fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime: *mut FILETIME) -> () {
GetSystemTimeAsFileTime(lpsystemtimeasfiletime) unsafe { GetSystemTimeAsFileTime(lpsystemtimeasfiletime) }
} }
// >= Win11 / Server 2022 // >= Win11 / Server 2022
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a
pub fn GetTempPath2W(bufferlength: u32, buffer: PWSTR) -> u32 { pub fn GetTempPath2W(bufferlength: u32, buffer: PWSTR) -> u32 {
GetTempPathW(bufferlength, buffer) unsafe { GetTempPathW(bufferlength, buffer) }
} }
} }
@ -188,12 +186,12 @@ extern "system" {
compat_fn_optional! { compat_fn_optional! {
crate::sys::compat::load_synch_functions(); crate::sys::compat::load_synch_functions();
pub fn WaitOnAddress( pub fn WaitOnAddress(
address: *const ::core::ffi::c_void, address: *const c_void,
compareaddress: *const ::core::ffi::c_void, compareaddress: *const c_void,
addresssize: usize, addresssize: usize,
dwmilliseconds: u32 dwmilliseconds: u32
) -> BOOL; ) -> BOOL;
pub fn WakeByAddressSingle(address: *const ::core::ffi::c_void); pub fn WakeByAddressSingle(address: *const c_void);
} }
#[cfg(any(target_vendor = "win7", target_vendor = "uwp"))] #[cfg(any(target_vendor = "win7", target_vendor = "uwp"))]
@ -240,7 +238,7 @@ compat_fn_with_fallback! {
shareaccess: FILE_SHARE_MODE, shareaccess: FILE_SHARE_MODE,
createdisposition: NTCREATEFILE_CREATE_DISPOSITION, createdisposition: NTCREATEFILE_CREATE_DISPOSITION,
createoptions: NTCREATEFILE_CREATE_OPTIONS, createoptions: NTCREATEFILE_CREATE_OPTIONS,
eabuffer: *const ::core::ffi::c_void, eabuffer: *const c_void,
ealength: u32 ealength: u32
) -> NTSTATUS { ) -> NTSTATUS {
STATUS_NOT_IMPLEMENTED STATUS_NOT_IMPLEMENTED
@ -250,9 +248,9 @@ compat_fn_with_fallback! {
filehandle: HANDLE, filehandle: HANDLE,
event: HANDLE, event: HANDLE,
apcroutine: PIO_APC_ROUTINE, apcroutine: PIO_APC_ROUTINE,
apccontext: *const core::ffi::c_void, apccontext: *const c_void,
iostatusblock: *mut IO_STATUS_BLOCK, iostatusblock: *mut IO_STATUS_BLOCK,
buffer: *mut core::ffi::c_void, buffer: *mut c_void,
length: u32, length: u32,
byteoffset: *const i64, byteoffset: *const i64,
key: *const u32 key: *const u32
@ -264,9 +262,9 @@ compat_fn_with_fallback! {
filehandle: HANDLE, filehandle: HANDLE,
event: HANDLE, event: HANDLE,
apcroutine: PIO_APC_ROUTINE, apcroutine: PIO_APC_ROUTINE,
apccontext: *const core::ffi::c_void, apccontext: *const c_void,
iostatusblock: *mut IO_STATUS_BLOCK, iostatusblock: *mut IO_STATUS_BLOCK,
buffer: *const core::ffi::c_void, buffer: *const c_void,
length: u32, length: u32,
byteoffset: *const i64, byteoffset: *const i64,
key: *const u32 key: *const u32

View file

@ -158,8 +158,10 @@ macro_rules! compat_fn_with_fallback {
static PTR: AtomicPtr<c_void> = AtomicPtr::new(load as *mut _); static PTR: AtomicPtr<c_void> = AtomicPtr::new(load as *mut _);
unsafe extern "system" fn load($($argname: $argtype),*) -> $rettype { unsafe extern "system" fn load($($argname: $argtype),*) -> $rettype {
let func = load_from_module(Module::new($module)); unsafe {
func($($argname),*) let func = load_from_module(Module::new($module));
func($($argname),*)
}
} }
fn load_from_module(module: Option<Module>) -> F { fn load_from_module(module: Option<Module>) -> F {
@ -182,8 +184,10 @@ macro_rules! compat_fn_with_fallback {
#[inline(always)] #[inline(always)]
pub unsafe fn call($($argname: $argtype),*) -> $rettype { pub unsafe fn call($($argname: $argtype),*) -> $rettype {
let func: F = mem::transmute(PTR.load(Ordering::Relaxed)); unsafe {
func($($argname),*) let func: F = mem::transmute(PTR.load(Ordering::Relaxed));
func($($argname),*)
}
} }
} }
#[allow(unused)] #[allow(unused)]
@ -225,7 +229,7 @@ macro_rules! compat_fn_optional {
} }
#[inline] #[inline]
pub unsafe extern "system" fn $symbol($($argname: $argtype),*) $(-> $rettype)? { pub unsafe extern "system" fn $symbol($($argname: $argtype),*) $(-> $rettype)? {
$symbol::option().unwrap()($($argname),*) unsafe { $symbol::option().unwrap()($($argname),*) }
} }
)+ )+
) )

View file

@ -3,16 +3,17 @@
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use crate::cmp;
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, Read}; use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, Read};
use crate::mem;
use crate::os::windows::io::{ use crate::os::windows::io::{
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle, AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
}; };
use crate::ptr;
use crate::sys::c; use crate::sys::c;
use crate::sys::cvt; use crate::sys::cvt;
use crate::sys_common::{AsInner, FromInner, IntoInner}; use crate::sys_common::{AsInner, FromInner, IntoInner};
use core::cmp;
use core::ffi::c_void;
use core::mem;
use core::ptr;
/// An owned container for `HANDLE` object, closing them on Drop. /// An owned container for `HANDLE` object, closing them on Drop.
/// ///
@ -255,7 +256,7 @@ impl Handle {
None, None,
ptr::null_mut(), ptr::null_mut(),
&mut io_status, &mut io_status,
buf.cast::<core::ffi::c_void>(), buf.cast::<c_void>(),
len, len,
offset.as_ref().map(|n| ptr::from_ref(n).cast::<i64>()).unwrap_or(ptr::null()), offset.as_ref().map(|n| ptr::from_ref(n).cast::<i64>()).unwrap_or(ptr::null()),
ptr::null(), ptr::null(),
@ -305,7 +306,7 @@ impl Handle {
None, None,
ptr::null_mut(), ptr::null_mut(),
&mut io_status, &mut io_status,
buf.as_ptr().cast::<core::ffi::c_void>(), buf.as_ptr().cast::<c_void>(),
len, len,
offset.as_ref().map(|n| ptr::from_ref(n).cast::<i64>()).unwrap_or(ptr::null()), offset.as_ref().map(|n| ptr::from_ref(n).cast::<i64>()).unwrap_or(ptr::null()),
ptr::null(), ptr::null(),

View file

@ -1,5 +1,5 @@
#![allow(missing_docs, nonstandard_style)] #![allow(missing_docs, nonstandard_style)]
#![deny(unsafe_op_in_unsafe_fn)] #![forbid(unsafe_op_in_unsafe_fn)]
use crate::ffi::{OsStr, OsString}; use crate::ffi::{OsStr, OsString};
use crate::io::ErrorKind; use crate::io::ErrorKind;