Skip to content

Commit 707e0bb

Browse files
authored
Lock down windows-core internals (#3129)
1 parent 472b563 commit 707e0bb

File tree

11 files changed

+52
-115
lines changed

11 files changed

+52
-115
lines changed

crates/libs/core/src/com_object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<T: ComObjectInner> ComObject<T> {
165165
/// Gets a borrowed reference to an interface that is implemented by `T`.
166166
///
167167
/// The returned reference does not have an additional reference count.
168-
/// You can AddRef it by calling [`Self::to_owned`].
168+
/// You can AddRef it by calling [`InterfaceRef::to_owned`].
169169
#[inline(always)]
170170
pub fn as_interface<I: Interface>(&self) -> InterfaceRef<'_, I>
171171
where

crates/libs/core/src/event.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use core::ffi::c_void;
23
use core::marker::PhantomData;
34
use core::mem::{size_of, transmute_copy};
45
use core::ptr::null_mut;
@@ -212,7 +213,7 @@ impl<T: Interface> Drop for Array<T> {
212213
unsafe {
213214
if !self.is_empty() && (*self.buffer).0.release() == 0 {
214215
core::ptr::drop_in_place(self.as_mut_slice());
215-
imp::heap_free(self.buffer as _)
216+
heap_free(self.buffer as _)
216217
}
217218
}
218219
}
@@ -230,7 +231,7 @@ impl<T: Interface> Buffer<T> {
230231
Ok(null_mut())
231232
} else {
232233
let alloc_size = size_of::<Self>() + len * size_of::<Delegate<T>>();
233-
let header = imp::heap_alloc(alloc_size)? as *mut Self;
234+
let header = heap_alloc(alloc_size)? as *mut Self;
234235
unsafe {
235236
header.write(Self(imp::RefCount::new(1), PhantomData));
236237
}
@@ -285,3 +286,30 @@ impl<T: Interface> Delegate<T> {
285286
}
286287
}
287288
}
289+
290+
/// Allocate memory of size `bytes` using `malloc` - the `Event` implementation does not
291+
/// need to use any particular allocator so `HeapAlloc` need not be used.
292+
fn heap_alloc(bytes: usize) -> crate::Result<*mut c_void> {
293+
let ptr: *mut c_void = unsafe {
294+
extern "C" {
295+
fn malloc(bytes: usize) -> *mut c_void;
296+
}
297+
298+
malloc(bytes)
299+
};
300+
301+
if ptr.is_null() {
302+
Err(Error::from_hresult(imp::E_OUTOFMEMORY))
303+
} else {
304+
Ok(ptr)
305+
}
306+
}
307+
308+
/// Free memory allocated by `heap_alloc`.
309+
unsafe fn heap_free(ptr: *mut c_void) {
310+
extern "C" {
311+
fn free(ptr: *mut c_void);
312+
}
313+
314+
free(ptr);
315+
}

crates/libs/core/src/imp/bindings.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes
1111
windows_targets::link!("kernel32.dll" "system" fn EncodePointer(ptr : *const core::ffi::c_void) -> *mut core::ffi::c_void);
1212
windows_targets::link!("kernel32.dll" "system" fn FreeLibrary(hlibmodule : HMODULE) -> BOOL);
1313
windows_targets::link!("kernel32.dll" "system" fn GetProcAddress(hmodule : HMODULE, lpprocname : PCSTR) -> FARPROC);
14-
windows_targets::link!("kernel32.dll" "system" fn GetProcessHeap() -> HANDLE);
15-
windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap : HANDLE, dwflags : HEAP_FLAGS, dwbytes : usize) -> *mut core::ffi::c_void);
16-
windows_targets::link!("kernel32.dll" "system" fn HeapFree(hheap : HANDLE, dwflags : HEAP_FLAGS, lpmem : *const core::ffi::c_void) -> BOOL);
1714
windows_targets::link!("kernel32.dll" "system" fn LoadLibraryExA(lplibfilename : PCSTR, hfile : HANDLE, dwflags : LOAD_LIBRARY_FLAGS) -> HMODULE);
1815
windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL);
1916
windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT);
@@ -337,7 +334,6 @@ impl GUID {
337334
}
338335
}
339336
pub type HANDLE = *mut core::ffi::c_void;
340-
pub type HEAP_FLAGS = u32;
341337
pub type HMODULE = *mut core::ffi::c_void;
342338
pub type HRESULT = i32;
343339
#[repr(C)]

crates/libs/core/src/imp/delay_load.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

crates/libs/core/src/imp/factory_cache.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::mem::{forget, transmute, transmute_copy};
66
use core::ptr::null_mut;
77
use core::sync::atomic::{AtomicPtr, Ordering};
88

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

156+
unsafe fn delay_load<T>(library: crate::PCSTR, function: crate::PCSTR) -> Option<T> {
157+
let library = LoadLibraryExA(
158+
library.0,
159+
core::ptr::null_mut(),
160+
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
161+
);
162+
163+
if library.is_null() {
164+
return None;
165+
}
166+
167+
let address = GetProcAddress(library, function.0);
168+
169+
if address.is_some() {
170+
return Some(core::mem::transmute_copy(&address));
171+
}
172+
173+
FreeLibrary(library);
174+
None
175+
}
176+
157177
type DllGetActivationFactory =
158178
extern "system" fn(name: *mut c_void, factory: *mut *mut c_void) -> crate::HRESULT;
159179

crates/libs/core/src/imp/heap.rs

Lines changed: 0 additions & 58 deletions
This file was deleted.

crates/libs/core/src/imp/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
mod bindings;
22
mod can_into;
33
mod com_bindings;
4-
mod delay_load;
54
mod factory_cache;
65
mod generic_factory;
7-
mod heap;
86
mod ref_count;
97
mod sha1;
108
mod waiter;
@@ -13,25 +11,13 @@ mod weak_ref_count;
1311
pub use bindings::*;
1412
pub use can_into::*;
1513
pub use com_bindings::*;
16-
pub use delay_load::*;
1714
pub use factory_cache::*;
1815
pub use generic_factory::*;
19-
pub use heap::*;
2016
pub use ref_count::*;
2117
pub use sha1::*;
2218
pub use waiter::*;
2319
pub use weak_ref_count::*;
2420

25-
pub fn wide_trim_end(mut wide: &[u16]) -> &[u16] {
26-
while let Some(last) = wide.last() {
27-
match last {
28-
32 | 9..=13 => wide = &wide[..wide.len() - 1],
29-
_ => break,
30-
}
31-
}
32-
wide
33-
}
34-
3521
#[doc(hidden)]
3622
#[macro_export]
3723
macro_rules! interface_hierarchy {

crates/libs/core/src/imp/ref_count.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use core::sync::atomic::{fence, AtomicI32, Ordering};
22

3-
#[doc(hidden)]
43
#[repr(transparent)]
54
#[derive(Default)]
65
pub struct RefCount(pub(crate) AtomicI32);

crates/libs/core/src/imp/waiter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
22

3-
#[doc(hidden)]
43
pub struct Waiter(HANDLE);
54
pub struct WaiterSignaler(HANDLE);
65

crates/libs/core/src/imp/weak_ref_count.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use core::mem::{transmute, transmute_copy};
55
use core::ptr::null_mut;
66
use core::sync::atomic::{AtomicIsize, Ordering};
77

8-
#[doc(hidden)]
98
#[repr(transparent)]
109
#[derive(Default)]
1110
pub struct WeakRefCount(AtomicIsize);

0 commit comments

Comments
 (0)