Skip to content

Commit

Permalink
Lock down windows-core internals (#3129)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jun 26, 2024
1 parent 472b563 commit 707e0bb
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 115 deletions.
2 changes: 1 addition & 1 deletion crates/libs/core/src/com_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<T: ComObjectInner> ComObject<T> {
/// Gets a borrowed reference to an interface that is implemented by `T`.
///
/// The returned reference does not have an additional reference count.
/// You can AddRef it by calling [`Self::to_owned`].
/// You can AddRef it by calling [`InterfaceRef::to_owned`].
#[inline(always)]
pub fn as_interface<I: Interface>(&self) -> InterfaceRef<'_, I>
where
Expand Down
32 changes: 30 additions & 2 deletions crates/libs/core/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use core::ffi::c_void;
use core::marker::PhantomData;
use core::mem::{size_of, transmute_copy};
use core::ptr::null_mut;
Expand Down Expand Up @@ -212,7 +213,7 @@ impl<T: Interface> Drop for Array<T> {
unsafe {
if !self.is_empty() && (*self.buffer).0.release() == 0 {
core::ptr::drop_in_place(self.as_mut_slice());
imp::heap_free(self.buffer as _)
heap_free(self.buffer as _)
}
}
}
Expand All @@ -230,7 +231,7 @@ impl<T: Interface> Buffer<T> {
Ok(null_mut())
} else {
let alloc_size = size_of::<Self>() + len * size_of::<Delegate<T>>();
let header = imp::heap_alloc(alloc_size)? as *mut Self;
let header = heap_alloc(alloc_size)? as *mut Self;
unsafe {
header.write(Self(imp::RefCount::new(1), PhantomData));
}
Expand Down Expand Up @@ -285,3 +286,30 @@ impl<T: Interface> Delegate<T> {
}
}
}

/// Allocate memory of size `bytes` using `malloc` - the `Event` implementation does not
/// need to use any particular allocator so `HeapAlloc` need not be used.
fn heap_alloc(bytes: usize) -> crate::Result<*mut c_void> {
let ptr: *mut c_void = unsafe {
extern "C" {
fn malloc(bytes: usize) -> *mut c_void;
}

malloc(bytes)
};

if ptr.is_null() {
Err(Error::from_hresult(imp::E_OUTOFMEMORY))
} else {
Ok(ptr)
}
}

/// Free memory allocated by `heap_alloc`.
unsafe fn heap_free(ptr: *mut c_void) {
extern "C" {
fn free(ptr: *mut c_void);
}

free(ptr);
}
4 changes: 0 additions & 4 deletions crates/libs/core/src/imp/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes
windows_targets::link!("kernel32.dll" "system" fn EncodePointer(ptr : *const core::ffi::c_void) -> *mut core::ffi::c_void);
windows_targets::link!("kernel32.dll" "system" fn FreeLibrary(hlibmodule : HMODULE) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn GetProcAddress(hmodule : HMODULE, lpprocname : PCSTR) -> FARPROC);
windows_targets::link!("kernel32.dll" "system" fn GetProcessHeap() -> HANDLE);
windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap : HANDLE, dwflags : HEAP_FLAGS, dwbytes : usize) -> *mut core::ffi::c_void);
windows_targets::link!("kernel32.dll" "system" fn HeapFree(hheap : HANDLE, dwflags : HEAP_FLAGS, lpmem : *const core::ffi::c_void) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn LoadLibraryExA(lplibfilename : PCSTR, hfile : HANDLE, dwflags : LOAD_LIBRARY_FLAGS) -> HMODULE);
windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT);
Expand Down Expand Up @@ -337,7 +334,6 @@ impl GUID {
}
}
pub type HANDLE = *mut core::ffi::c_void;
pub type HEAP_FLAGS = u32;
pub type HMODULE = *mut core::ffi::c_void;
pub type HRESULT = i32;
#[repr(C)]
Expand Down
29 changes: 0 additions & 29 deletions crates/libs/core/src/imp/delay_load.rs

This file was deleted.

22 changes: 21 additions & 1 deletion crates/libs/core/src/imp/factory_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use core::mem::{forget, transmute, transmute_copy};
use core::ptr::null_mut;
use core::sync::atomic::{AtomicPtr, Ordering};

#[doc(hidden)]
pub struct FactoryCache<C, I> {
shared: AtomicPtr<c_void>,
_c: PhantomData<C>,
Expand Down Expand Up @@ -154,6 +153,27 @@ unsafe fn get_activation_factory(
function(transmute_copy(name), &mut abi).and_then(|| crate::Type::from_abi(abi))
}

unsafe fn delay_load<T>(library: crate::PCSTR, function: crate::PCSTR) -> Option<T> {
let library = LoadLibraryExA(
library.0,
core::ptr::null_mut(),
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
);

if library.is_null() {
return None;
}

let address = GetProcAddress(library, function.0);

if address.is_some() {
return Some(core::mem::transmute_copy(&address));
}

FreeLibrary(library);
None
}

type DllGetActivationFactory =
extern "system" fn(name: *mut c_void, factory: *mut *mut c_void) -> crate::HRESULT;

Expand Down
58 changes: 0 additions & 58 deletions crates/libs/core/src/imp/heap.rs

This file was deleted.

14 changes: 0 additions & 14 deletions crates/libs/core/src/imp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod bindings;
mod can_into;
mod com_bindings;
mod delay_load;
mod factory_cache;
mod generic_factory;
mod heap;
mod ref_count;
mod sha1;
mod waiter;
Expand All @@ -13,25 +11,13 @@ mod weak_ref_count;
pub use bindings::*;
pub use can_into::*;
pub use com_bindings::*;
pub use delay_load::*;
pub use factory_cache::*;
pub use generic_factory::*;
pub use heap::*;
pub use ref_count::*;
pub use sha1::*;
pub use waiter::*;
pub use weak_ref_count::*;

pub fn wide_trim_end(mut wide: &[u16]) -> &[u16] {
while let Some(last) = wide.last() {
match last {
32 | 9..=13 => wide = &wide[..wide.len() - 1],
_ => break,
}
}
wide
}

#[doc(hidden)]
#[macro_export]
macro_rules! interface_hierarchy {
Expand Down
1 change: 0 additions & 1 deletion crates/libs/core/src/imp/ref_count.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::sync::atomic::{fence, AtomicI32, Ordering};

#[doc(hidden)]
#[repr(transparent)]
#[derive(Default)]
pub struct RefCount(pub(crate) AtomicI32);
Expand Down
1 change: 0 additions & 1 deletion crates/libs/core/src/imp/waiter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::*;

#[doc(hidden)]
pub struct Waiter(HANDLE);
pub struct WaiterSignaler(HANDLE);

Expand Down
1 change: 0 additions & 1 deletion crates/libs/core/src/imp/weak_ref_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use core::mem::{transmute, transmute_copy};
use core::ptr::null_mut;
use core::sync::atomic::{AtomicIsize, Ordering};

#[doc(hidden)]
#[repr(transparent)]
#[derive(Default)]
pub struct WeakRefCount(AtomicIsize);
Expand Down
3 changes: 0 additions & 3 deletions crates/tools/bindings/src/core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
Windows.Win32.System.LibraryLoader.GetProcAddress
Windows.Win32.System.LibraryLoader.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
Windows.Win32.System.LibraryLoader.LoadLibraryExA
Windows.Win32.System.Memory.GetProcessHeap
Windows.Win32.System.Memory.HeapAlloc
Windows.Win32.System.Memory.HeapFree
Windows.Win32.System.Threading.CreateEventW
Windows.Win32.System.Threading.SetEvent
Windows.Win32.System.Threading.WaitForSingleObject
Expand Down

0 comments on commit 707e0bb

Please sign in to comment.