Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions crates/wdk-sys/src/test_stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub use wdf::*;
driver_model__driver_type = "UMDF"
))]
use crate::{DRIVER_OBJECT, NTSTATUS, PCUNICODE_STRING};
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
use crate::{ERESOURCE, EX_SPIN_LOCK, KIRQL, LOGICAL, ULONG, ULONG_PTR};

/// Stubbed version of `DriverEntry` Symbol so that test targets will compile
///
Expand All @@ -37,6 +39,147 @@ pub const unsafe extern "system" fn driver_entry_stub(
0
}

/// Stubbed version of `ExInitializeResourceLite` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExInitializeResourceLite(_resource: *mut ERESOURCE) -> NTSTATUS {
crate::STATUS_SUCCESS
}

/// Stubbed version of `ExAcquireResourceSharedLite` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquireResourceSharedLite(
_resource: *mut ERESOURCE,
_wait: crate::BOOLEAN,
) -> crate::BOOLEAN {
1
}

/// Stubbed version of `ExAcquireResourceExclusiveLite` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquireResourceExclusiveLite(
_resource: *mut ERESOURCE,
_wait: crate::BOOLEAN,
) -> crate::BOOLEAN {
1
}

/// Stubbed version of `ExReleaseResourceLite` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExReleaseResourceLite(_resource: *mut ERESOURCE) {}

/// Stubbed version of `ExDeleteResourceLite` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExDeleteResourceLite(_resource: *mut ERESOURCE) -> NTSTATUS {
crate::STATUS_SUCCESS
}

/// Stubbed version of `KeEnterCriticalRegion` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn KeEnterCriticalRegion() {}

/// Stubbed version of `KeLeaveCriticalRegion` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn KeLeaveCriticalRegion() {}

/// Stubbed version of `ExInitializePushLock` so test targets can link
///
/// # Safety
///
/// `push_lock` must point to valid writable push lock storage.
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub unsafe extern "system" fn ExInitializePushLock(push_lock: *mut ULONG_PTR) {
// SAFETY: Test callers pass a valid pointer to push lock storage.
unsafe {
push_lock.write(0);
}
}

/// Stubbed version of `ExAcquirePushLockSharedEx` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquirePushLockSharedEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}

/// Stubbed version of `ExAcquirePushLockExclusiveEx` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquirePushLockExclusiveEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}

/// Stubbed version of `ExReleasePushLockSharedEx` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExReleasePushLockSharedEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}

/// Stubbed version of `ExReleasePushLockExclusiveEx` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExReleasePushLockExclusiveEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}

/// Stubbed version of `ExAcquireSpinLockShared` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquireSpinLockShared(_spin_lock: *mut EX_SPIN_LOCK) -> KIRQL {
0
}

/// Stubbed version of `ExAcquireSpinLockSharedAtDpcLevel` so test targets can
/// link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquireSpinLockSharedAtDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}

/// Stubbed version of `ExReleaseSpinLockShared` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExReleaseSpinLockShared(_spin_lock: *mut EX_SPIN_LOCK, _old_irql: KIRQL) {}

/// Stubbed version of `ExReleaseSpinLockSharedFromDpcLevel` so test targets can
/// link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExReleaseSpinLockSharedFromDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}

/// Stubbed version of `ExAcquireSpinLockExclusive` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquireSpinLockExclusive(_spin_lock: *mut EX_SPIN_LOCK) -> KIRQL {
0
}

/// Stubbed version of `ExAcquireSpinLockExclusiveAtDpcLevel` so test targets
/// can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExAcquireSpinLockExclusiveAtDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}

/// Stubbed version of `ExReleaseSpinLockExclusive` so test targets can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExReleaseSpinLockExclusive(_spin_lock: *mut EX_SPIN_LOCK, _old_irql: KIRQL) {}

/// Stubbed version of `ExReleaseSpinLockExclusiveFromDpcLevel` so test targets
/// can link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExReleaseSpinLockExclusiveFromDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}

/// Stubbed version of `ExTryConvertSharedSpinLockExclusive` so test targets can
/// link
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
#[unsafe(no_mangle)]
pub extern "system" fn ExTryConvertSharedSpinLockExclusive(
_spin_lock: *mut EX_SPIN_LOCK,
) -> LOGICAL {
1
}

#[cfg(any(driver_model__driver_type = "KMDF", driver_model__driver_type = "UMDF"))]
mod wdf {
use crate::ULONG;
Expand Down
6 changes: 6 additions & 0 deletions crates/wdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
no_std
)]

#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
extern crate alloc;

#[cfg(any(
driver_model__driver_type = "WDM",
driver_model__driver_type = "KMDF",
Expand All @@ -36,6 +39,9 @@ mod print;
/// environments.
pub mod fmt;

#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
pub mod sync;

#[cfg(any(driver_model__driver_type = "KMDF", driver_model__driver_type = "UMDF"))]
pub mod wdf;

Expand Down
28 changes: 28 additions & 0 deletions crates/wdk/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Synchronization primitives backed by kernel-mode WDK objects.
//!
//! The wrappers in this module expose Rust guard-based access to common
//! shared/exclusive kernel locks:
//!
//! - [`RwLock`] uses `ERESOURCE` for waitable reader-writer locking at `IRQL <=
//! APC_LEVEL`.
//! - [`PushLock`] uses `EX_PUSH_LOCK` for compact waitable reader-writer
//! locking at `IRQL <= APC_LEVEL`.
//! - [`RwSpinLock`] uses `EX_SPIN_LOCK` for very short non-waiting sections
//! that can run up to `DISPATCH_LEVEL`.

pub use push_lock::*;
pub use rw_lock::*;
pub use rw_spin_lock::*;

mod push_lock;
mod rw_lock;
mod rw_spin_lock;

// Stable Rust does not support negative `Send` impls for these guard types.
// This marker keeps guards from crossing threads, which ensures kernel lock
// release and IRQL restoration happen on the acquiring thread.
type NotSend = core::marker::PhantomData<alloc::rc::Rc<()>>;

const fn not_send() -> NotSend {
core::marker::PhantomData
}
Loading