Skip to content

Commit 48e410e

Browse files
committed
Lazy load ntdll functions on UWP
1 parent d0ee190 commit 48e410e

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

library/std/src/sys/windows/c.rs

+58
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub use windows_sys::*;
1919
pub type DWORD = c_ulong;
2020
pub type NonZeroDWORD = NonZero_c_ulong;
2121
pub type LARGE_INTEGER = c_longlong;
22+
#[cfg_attr(target_vendor = "uwp", allow(unused))]
2223
pub type LONG = c_long;
2324
pub type UINT = c_uint;
2425
pub type WCHAR = u16;
@@ -50,6 +51,9 @@ pub type CONDITION_VARIABLE = RTL_CONDITION_VARIABLE;
5051
pub type SRWLOCK = RTL_SRWLOCK;
5152
pub type INIT_ONCE = RTL_RUN_ONCE;
5253

54+
#[cfg(target_vendor = "uwp")]
55+
pub const STATUS_NOT_IMPLEMENTED: NTSTATUS = 0xC0000002_u32 as _;
56+
5357
pub const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE { Ptr: ptr::null_mut() };
5458
pub const SRWLOCK_INIT: SRWLOCK = SRWLOCK { Ptr: ptr::null_mut() };
5559
pub const INIT_ONCE_STATIC_INIT: INIT_ONCE = INIT_ONCE { Ptr: ptr::null_mut() };
@@ -267,6 +271,8 @@ pub unsafe fn getaddrinfo(
267271
windows_sys::getaddrinfo(node.cast::<u8>(), service.cast::<u8>(), hints, res)
268272
}
269273

274+
cfg_if::cfg_if! {
275+
if #[cfg(not(target_vendor = "uwp"))] {
270276
pub unsafe fn NtReadFile(
271277
filehandle: BorrowedHandle<'_>,
272278
event: HANDLE,
@@ -313,6 +319,8 @@ pub unsafe fn NtWriteFile(
313319
key.map(|k| k as *const u32).unwrap_or(ptr::null()),
314320
)
315321
}
322+
}
323+
}
316324

317325
// Functions that aren't available on every version of Windows that we support,
318326
// but we still use them and just provide some form of a fallback implementation.
@@ -376,4 +384,54 @@ compat_fn_with_fallback! {
376384
) -> NTSTATUS {
377385
panic!("keyed events not available")
378386
}
387+
388+
// These functions are available on UWP when lazily loaded. They will fail WACK if loaded statically.
389+
#[cfg(target_vendor = "uwp")]
390+
pub fn NtCreateFile(
391+
filehandle: *mut HANDLE,
392+
desiredaccess: FILE_ACCESS_RIGHTS,
393+
objectattributes: *const OBJECT_ATTRIBUTES,
394+
iostatusblock: *mut IO_STATUS_BLOCK,
395+
allocationsize: *const i64,
396+
fileattributes: FILE_FLAGS_AND_ATTRIBUTES,
397+
shareaccess: FILE_SHARE_MODE,
398+
createdisposition: NTCREATEFILE_CREATE_DISPOSITION,
399+
createoptions: NTCREATEFILE_CREATE_OPTIONS,
400+
eabuffer: *const ::core::ffi::c_void,
401+
ealength: u32
402+
) -> NTSTATUS {
403+
STATUS_NOT_IMPLEMENTED
404+
}
405+
#[cfg(target_vendor = "uwp")]
406+
pub fn NtReadFile(
407+
filehandle: BorrowedHandle<'_>,
408+
event: HANDLE,
409+
apcroutine: PIO_APC_ROUTINE,
410+
apccontext: *mut c_void,
411+
iostatusblock: &mut IO_STATUS_BLOCK,
412+
buffer: *mut crate::mem::MaybeUninit<u8>,
413+
length: ULONG,
414+
byteoffset: Option<&LARGE_INTEGER>,
415+
key: Option<&ULONG>
416+
) -> NTSTATUS {
417+
STATUS_NOT_IMPLEMENTED
418+
}
419+
#[cfg(target_vendor = "uwp")]
420+
pub fn NtWriteFile(
421+
filehandle: BorrowedHandle<'_>,
422+
event: HANDLE,
423+
apcroutine: PIO_APC_ROUTINE,
424+
apccontext: *mut c_void,
425+
iostatusblock: &mut IO_STATUS_BLOCK,
426+
buffer: *const u8,
427+
length: ULONG,
428+
byteoffset: Option<&LARGE_INTEGER>,
429+
key: Option<&ULONG>
430+
) -> NTSTATUS {
431+
STATUS_NOT_IMPLEMENTED
432+
}
433+
#[cfg(target_vendor = "uwp")]
434+
pub fn RtlNtStatusToDosError(Status: NTSTATUS) -> ULONG {
435+
Status as ULONG
436+
}
379437
}

library/std/src/sys/windows/rand.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::ffi::c_void;
2-
use crate::io;
31
use crate::mem;
42
use crate::ptr;
53
use crate::sys::c;
@@ -25,6 +23,9 @@ pub fn hashmap_random_keys() -> (u64, u64) {
2523
#[cfg(not(target_vendor = "uwp"))]
2624
#[inline(never)]
2725
fn fallback_rng() -> (u64, u64) {
26+
use crate::ffi::c_void;
27+
use crate::io;
28+
2829
let mut v = (0, 0);
2930
let ret = unsafe {
3031
c::RtlGenRandom(&mut v as *mut _ as *mut c_void, mem::size_of_val(&v) as c::ULONG)

0 commit comments

Comments
 (0)