Skip to content

Commit

Permalink
Remove windows-shared-memory-equality feature (#364)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrobinson authored Oct 11, 2024
1 parent 5b7a6ed commit 2bcf4dd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 91 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 1 addition & 9 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OsIpcSharedMemory>,
Expand Down
6 changes: 0 additions & 6 deletions src/platform/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
73 changes: 16 additions & 57 deletions src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -344,7 +338,7 @@ fn move_handle_to_process(

#[derive(Debug)]
struct WinHandle {
h: HANDLE,
handle: HANDLE,
}

unsafe impl Send for WinHandle {}
Expand All @@ -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());
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -1095,7 +1058,6 @@ pub struct OsIpcReceiver {
reader: RefCell<MessageReader>,
}

#[cfg(feature = "windows-shared-memory-equality")]
impl PartialEq for OsIpcReceiver {
fn eq(&self, other: &OsIpcReceiver) -> bool {
self.reader.borrow().handle == other.reader.borrow().handle
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
Expand Down
18 changes: 2 additions & 16 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -108,12 +103,7 @@ pub fn get_channel_name_arg(which: &str) -> Option<String> {

// 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)
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 2bcf4dd

Please sign in to comment.