From 2bcf4dddd3a3ed7c48292ccf7b06443ceed6a5a7 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Fri, 11 Oct 2024 06:18:59 -0700 Subject: [PATCH] Remove `windows-shared-memory-equality` feature (#364) This feature is useful because it allows compiling for versions of Windows older than Windows 10. Windows 10 was released about a decade ago, so I think it's fine to remove support for older versions of Windows. For example Windows 7 is no longer supported as of January 2020 and it seems that less than 3% of users are using it. --- .github/workflows/main.yml | 4 +- Cargo.toml | 1 - src/ipc.rs | 10 +---- src/platform/test.rs | 6 --- src/platform/windows/mod.rs | 73 ++++++++----------------------------- src/test.rs | 18 +-------- 6 files changed, 21 insertions(+), 91 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3f64e135..93020fa7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,9 +24,9 @@ jobs: - { target: i686-pc-windows-msvc, os: windows-latest } features: ["", "force-inprocess", "async"] include: - - features: "windows-shared-memory-equality" + - features: "" platform: { target: x86_64-pc-windows-msvc, os: windows-latest } - - features: "windows-shared-memory-equality" + - features: "" platform: { target: i686-pc-windows-msvc, os: windows-latest } - features: "memfd" platform: { target: x86_64-unknown-linux-gnu, os: ubuntu-latest } diff --git a/Cargo.toml b/Cargo.toml index 4ebfff07..ee1edf06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ force-inprocess = [] memfd = ["sc"] async = ["futures", "futures-test"] win32-trace = [] -windows-shared-memory-equality = ["windows/Win32_System_LibraryLoader"] [dependencies] bincode = "1" diff --git a/src/ipc.rs b/src/ipc.rs index aa2d7124..266ef3b7 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -541,17 +541,9 @@ impl IpcReceiverSet { /// let shmem = IpcSharedMemory::from_bytes(&data); /// tx.send(shmem.clone()).unwrap(); /// # let rx_shmem = rx.recv().unwrap(); -/// # #[cfg(any(not(target_os = "windows"), all(target_os = "windows", feature = "windows-shared-memory-equality")))] /// # assert_eq!(shmem, rx_shmem); /// ``` -#[derive(Clone, Debug)] -#[cfg_attr( - any( - not(target_os = "windows"), - all(target_os = "windows", feature = "windows-shared-memory-equality") - ), - derive(PartialEq) -)] +#[derive(Clone, Debug, PartialEq)] pub struct IpcSharedMemory { /// None represents no data (empty slice) os_shared_memory: Option, diff --git a/src/platform/test.rs b/src/platform/test.rs index 45d8e77c..ecf04840 100644 --- a/src/platform/test.rs +++ b/src/platform/test.rs @@ -7,12 +7,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// Most of this file won't compile without `windows-shared-memory-equality` feature on Windows since `PartialEq` won't be implemented for `IpcSharedMemory`. -#![cfg(any( - not(target_os = "windows"), - all(target_os = "windows", feature = "windows-shared-memory-equality") -))] - use crate::platform::OsIpcSharedMemory; use crate::platform::{self, OsIpcChannel, OsIpcReceiverSet}; use std::collections::HashMap; diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index de63d3a6..d2ac4db3 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -30,10 +30,10 @@ use windows::{ core::{Error as WinError, PCSTR}, Win32::{ Foundation::{ - CloseHandle, DuplicateHandle, GetLastError, DUPLICATE_CLOSE_SOURCE, - DUPLICATE_HANDLE_OPTIONS, DUPLICATE_SAME_ACCESS, ERROR_BROKEN_PIPE, - ERROR_IO_INCOMPLETE, ERROR_IO_PENDING, ERROR_NOT_FOUND, ERROR_NO_DATA, - ERROR_PIPE_CONNECTED, HANDLE, INVALID_HANDLE_VALUE, WAIT_TIMEOUT, + CloseHandle, CompareObjectHandles, DuplicateHandle, GetLastError, + DUPLICATE_CLOSE_SOURCE, DUPLICATE_HANDLE_OPTIONS, DUPLICATE_SAME_ACCESS, + ERROR_BROKEN_PIPE, ERROR_IO_INCOMPLETE, ERROR_IO_PENDING, ERROR_NOT_FOUND, + ERROR_NO_DATA, ERROR_PIPE_CONNECTED, HANDLE, INVALID_HANDLE_VALUE, WAIT_TIMEOUT, }, Storage::FileSystem::{ CreateFileA, ReadFile, WriteFile, FILE_ATTRIBUTE_NORMAL, FILE_FLAG_OVERLAPPED, @@ -60,12 +60,6 @@ use windows::{ }, }; -#[cfg(feature = "windows-shared-memory-equality")] -use windows::Win32::{ - Foundation::BOOL, - System::LibraryLoader::{GetProcAddress, LoadLibraryA}, -}; - mod aliased_cell; use self::aliased_cell::AliasedCell; @@ -344,7 +338,7 @@ fn move_handle_to_process( #[derive(Debug)] struct WinHandle { - h: HANDLE, + handle: HANDLE, } unsafe impl Send for WinHandle {} @@ -354,7 +348,7 @@ impl Drop for WinHandle { fn drop(&mut self) { unsafe { if self.is_valid() { - let result = CloseHandle(self.h); + let result = CloseHandle(self.handle); assert!(result.is_ok() || thread::panicking()); } } @@ -364,69 +358,38 @@ impl Drop for WinHandle { impl Default for WinHandle { fn default() -> WinHandle { WinHandle { - h: INVALID_HANDLE_VALUE, + handle: INVALID_HANDLE_VALUE, } } } -const WINDOWS_APP_MODULE_NAME: &str = "api-ms-win-core-handle-l1-1-0"; -const COMPARE_OBJECT_HANDLES_FUNCTION_NAME: &str = "CompareObjectHandles"; - -lazy_static! { - static ref WINDOWS_APP_MODULE_NAME_CSTRING: CString = - CString::new(WINDOWS_APP_MODULE_NAME).unwrap(); - static ref COMPARE_OBJECT_HANDLES_FUNCTION_NAME_CSTRING: CString = - CString::new(COMPARE_OBJECT_HANDLES_FUNCTION_NAME).unwrap(); -} - -#[cfg(feature = "windows-shared-memory-equality")] impl PartialEq for WinHandle { fn eq(&self, other: &WinHandle) -> bool { - unsafe { - // Calling LoadLibraryA every time seems to be ok since libraries are refcounted and multiple calls won't produce multiple instances. - let module_handle = LoadLibraryA(PCSTR::from_raw( - WINDOWS_APP_MODULE_NAME_CSTRING.as_ptr() as *const u8, - )) - .unwrap_or_else(|e| panic!("Error loading library {}. {}", WINDOWS_APP_MODULE_NAME, e)); - let proc = GetProcAddress( - module_handle, - PCSTR::from_raw(COMPARE_OBJECT_HANDLES_FUNCTION_NAME_CSTRING.as_ptr() as *const u8), - ) - .unwrap_or_else(|| { - panic!( - "Error calling GetProcAddress to use {}. {:?}", - COMPARE_OBJECT_HANDLES_FUNCTION_NAME, - WinError::from_win32() - ) - }); - let compare_object_handles: unsafe extern "stdcall" fn(HANDLE, HANDLE) -> BOOL = - std::mem::transmute(proc); - compare_object_handles(self.h, other.h).into() - } + unsafe { CompareObjectHandles(self.handle, other.handle).into() } } } impl WinHandle { - fn new(h: HANDLE) -> WinHandle { - WinHandle { h } + fn new(handle: HANDLE) -> WinHandle { + WinHandle { handle } } fn invalid() -> WinHandle { WinHandle { - h: INVALID_HANDLE_VALUE, + handle: INVALID_HANDLE_VALUE, } } fn is_valid(&self) -> bool { - self.h != INVALID_HANDLE_VALUE + self.handle != INVALID_HANDLE_VALUE } fn as_raw(&self) -> HANDLE { - self.h + self.handle } fn take_raw(&mut self) -> HANDLE { - mem::replace(&mut self.h, INVALID_HANDLE_VALUE) + mem::replace(&mut self.handle, INVALID_HANDLE_VALUE) } fn take(&mut self) -> WinHandle { @@ -1095,7 +1058,6 @@ pub struct OsIpcReceiver { reader: RefCell, } -#[cfg(feature = "windows-shared-memory-equality")] impl PartialEq for OsIpcReceiver { fn eq(&self, other: &OsIpcReceiver) -> bool { self.reader.borrow().handle == other.reader.borrow().handle @@ -1268,8 +1230,7 @@ impl OsIpcReceiver { } } -#[derive(Debug)] -#[cfg_attr(feature = "windows-shared-memory-equality", derive(PartialEq))] +#[derive(Debug, PartialEq)] pub struct OsIpcSender { handle: WinHandle, // Make sure this is `!Sync`, to match `mpsc::Sender`; and to discourage sharing references. @@ -1817,7 +1778,6 @@ impl Clone for OsIpcSharedMemory { } } -#[cfg(feature = "windows-shared-memory-equality")] impl PartialEq for OsIpcSharedMemory { fn eq(&self, other: &OsIpcSharedMemory) -> bool { self.handle == other.handle @@ -1932,8 +1892,7 @@ pub enum OsIpcChannel { Receiver(OsIpcReceiver), } -#[derive(Debug)] -#[cfg_attr(feature = "windows-shared-memory-equality", derive(PartialEq))] +#[derive(Debug, PartialEq)] pub struct OsOpaqueIpcChannel { handle: WinHandle, } diff --git a/src/test.rs b/src/test.rs index 21604a38..bbeb5dfd 100644 --- a/src/test.rs +++ b/src/test.rs @@ -17,12 +17,7 @@ use std::cell::RefCell; #[cfg(not(any(feature = "force-inprocess", target_os = "android", target_os = "ios")))] use std::env; use std::iter; -#[cfg(not(any( - feature = "force-inprocess", - target_os = "android", - target_os = "ios", - all(target_os = "windows", not(feature = "windows-shared-memory-equality")) -)))] +#[cfg(not(any(feature = "force-inprocess", target_os = "android", target_os = "ios",)))] use std::process::{self, Command, Stdio}; #[cfg(not(any( feature = "force-inprocess", @@ -108,12 +103,7 @@ pub fn get_channel_name_arg(which: &str) -> Option { // Helper to get a channel_name argument passed in; used for the // cross-process spawn server tests. -#[cfg(not(any( - feature = "force-inprocess", - target_os = "android", - target_os = "ios", - all(target_os = "windows", not(feature = "windows-shared-memory-equality")) -)))] +#[cfg(not(any(feature = "force-inprocess", target_os = "android", target_os = "ios",)))] pub fn spawn_server(test_name: &str, server_args: &[(&str, &str)]) -> process::Child { Command::new(env::current_exe().unwrap()) .arg(test_name) @@ -484,10 +474,6 @@ fn shared_memory_slice() { } #[test] -#[cfg(any( - not(target_os = "windows"), - all(target_os = "windows", feature = "windows-shared-memory-equality") -))] fn shared_memory_object_equality() { let person = ("Patrick Walton".to_owned(), 29); let person_and_shared_memory = (person, IpcSharedMemory::from_byte(0xba, 1024 * 1024));