use std::io;
use std::os::windows::io::{
AsHandle, AsRawHandle as _, BorrowedHandle, FromRawHandle as _, OwnedHandle,
};
use windows_sys::Win32::Foundation::{
DUPLICATE_CLOSE_SOURCE, DuplicateHandle, HANDLE, INVALID_HANDLE_VALUE,
};
use windows_sys::Win32::System::Memory::{
CreateFileMappingW, FILE_MAP_READ, FILE_MAP_WRITE, MEMORY_BASIC_INFORMATION,
MEMORY_MAPPED_VIEW_ADDRESS, MapViewOfFile, PAGE_READWRITE, SEC_COMMIT, UnmapViewOfFile,
VirtualQuery,
};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
use super::{Backing, MAX_PAGE, Mapping};
struct View {
address: MEMORY_MAPPED_VIEW_ADDRESS,
len: usize,
}
impl View {
fn of(section: BorrowedHandle<'_>, access: u32) -> io::Result<Self> {
let address = unsafe { MapViewOfFile(section.as_raw_handle(), access, 0, 0, 0) };
if address.Value.is_null() {
return Err(io::Error::last_os_error());
}
let view = Self { address, len: 0 };
let mut region = MEMORY_BASIC_INFORMATION {
BaseAddress: std::ptr::null_mut(),
AllocationBase: std::ptr::null_mut(),
AllocationProtect: 0,
PartitionId: 0,
RegionSize: 0,
State: 0,
Protect: 0,
Type: 0,
};
let written = unsafe {
VirtualQuery(
view.address.Value,
&raw mut region,
size_of::<MEMORY_BASIC_INFORMATION>(),
)
};
if written == 0 {
return Err(io::Error::last_os_error());
}
Ok(Self {
len: region.RegionSize,
..view
})
}
fn bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.address.Value.cast::<u8>(), self.len) }
}
fn bytes_mut(&mut self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.address.Value.cast::<u8>(), self.len) }
}
}
impl Drop for View {
fn drop(&mut self) {
unsafe { UnmapViewOfFile(self.address) };
}
}
pub fn from_bytes(name: &str, bytes: &[u8]) -> io::Result<OwnedHandle> {
let _ = name;
let size = u64::try_from(bytes.len().max(1)).map_err(io::Error::other)?;
let section = unsafe {
CreateFileMappingW(
INVALID_HANDLE_VALUE,
std::ptr::null(),
PAGE_READWRITE | SEC_COMMIT,
u32::try_from(size >> 32).unwrap_or(u32::MAX),
u32::try_from(size & u64::from(u32::MAX)).unwrap_or(u32::MAX),
std::ptr::null(),
)
};
if section.is_null() {
return Err(io::Error::last_os_error());
}
let section = unsafe { OwnedHandle::from_raw_handle(section) };
{
let mut view = View::of(section.as_handle(), FILE_MAP_WRITE)?;
view.bytes_mut()
.get_mut(..bytes.len())
.ok_or_else(|| io::Error::other("a section shorter than requested"))?
.copy_from_slice(bytes);
}
read_only(section)
}
fn read_only(section: OwnedHandle) -> io::Result<OwnedHandle> {
let mut narrowed: HANDLE = std::ptr::null_mut();
let source = section.as_raw_handle();
std::mem::forget(section);
let duplicated = unsafe {
DuplicateHandle(
GetCurrentProcess(),
source,
GetCurrentProcess(),
&raw mut narrowed,
FILE_MAP_READ,
0,
DUPLICATE_CLOSE_SOURCE,
)
};
if duplicated == 0 {
return Err(io::Error::last_os_error());
}
Ok(unsafe { OwnedHandle::from_raw_handle(narrowed) })
}
impl Mapping {
pub fn map_sealed(
section: &impl AsHandle,
immutable: bool,
min_len: usize,
max_len: usize,
) -> io::Result<Self> {
if !immutable {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"a writable shared buffer needs sealed memory, which only Linux has",
));
}
let view = View::of(section.as_handle(), FILE_MAP_READ)?;
let size = view.len;
let len = size.min(max_len);
if size < min_len || len == 0 || size.saturating_sub(max_len) >= MAX_PAGE {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"a shared buffer's size is out of range",
));
}
let copied = view.bytes().get(..len).unwrap_or_default().to_vec();
Ok(Self {
backing: Backing::Copied(copied),
len,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_section_round_trips_and_its_handle_cannot_write() {
let section = from_bytes("t", b"hello").unwrap();
let bytes = Mapping::map_sealed(§ion, true, 5, 5).unwrap();
assert_eq!(&bytes[..], b"hello");
assert!(
View::of(section.as_handle(), FILE_MAP_WRITE).is_err(),
"the handle a reader gets maps the section read-only"
);
assert!(Mapping::map_sealed(§ion, true, 0, 0).is_err());
assert!(Mapping::map_sealed(§ion, true, 1 << 30, 1 << 30).is_err());
assert!(Mapping::map_sealed(§ion, false, 5, 5).is_err());
}
}