Skip to content

Commit 6bbf7bc

Browse files
committed
add kernel sync primitives
1 parent 0e3499d commit 6bbf7bc

10 files changed

Lines changed: 1966 additions & 9 deletions

File tree

crates/wdk-sys/src/test_stubs.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub use wdf::*;
1616
driver_model__driver_type = "UMDF"
1717
))]
1818
use crate::{DRIVER_OBJECT, NTSTATUS, PCUNICODE_STRING};
19+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
20+
use crate::{ERESOURCE, EX_SPIN_LOCK, KIRQL, LOGICAL, ULONG, ULONG_PTR};
1921

2022
/// Stubbed version of `DriverEntry` Symbol so that test targets will compile
2123
///
@@ -37,6 +39,145 @@ pub const unsafe extern "system" fn driver_entry_stub(
3739
0
3840
}
3941

42+
/// Stubbed version of `ExInitializeResourceLite` so test targets can link
43+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
44+
#[unsafe(no_mangle)]
45+
pub extern "system" fn ExInitializeResourceLite(_resource: *mut ERESOURCE) -> NTSTATUS {
46+
crate::STATUS_SUCCESS
47+
}
48+
49+
/// Stubbed version of `ExAcquireResourceSharedLite` so test targets can link
50+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
51+
#[unsafe(no_mangle)]
52+
pub extern "system" fn ExAcquireResourceSharedLite(
53+
_resource: *mut ERESOURCE,
54+
_wait: crate::BOOLEAN,
55+
) -> crate::BOOLEAN {
56+
1
57+
}
58+
59+
/// Stubbed version of `ExAcquireResourceExclusiveLite` so test targets can link
60+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
61+
#[unsafe(no_mangle)]
62+
pub extern "system" fn ExAcquireResourceExclusiveLite(
63+
_resource: *mut ERESOURCE,
64+
_wait: crate::BOOLEAN,
65+
) -> crate::BOOLEAN {
66+
1
67+
}
68+
69+
/// Stubbed version of `ExReleaseResourceLite` so test targets can link
70+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
71+
#[unsafe(no_mangle)]
72+
pub extern "system" fn ExReleaseResourceLite(_resource: *mut ERESOURCE) {}
73+
74+
/// Stubbed version of `ExDeleteResourceLite` so test targets can link
75+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
76+
#[unsafe(no_mangle)]
77+
pub extern "system" fn ExDeleteResourceLite(_resource: *mut ERESOURCE) -> NTSTATUS {
78+
crate::STATUS_SUCCESS
79+
}
80+
81+
/// Stubbed version of `KeEnterCriticalRegion` so test targets can link
82+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
83+
#[unsafe(no_mangle)]
84+
pub extern "system" fn KeEnterCriticalRegion() {}
85+
86+
/// Stubbed version of `KeLeaveCriticalRegion` so test targets can link
87+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
88+
#[unsafe(no_mangle)]
89+
pub extern "system" fn KeLeaveCriticalRegion() {}
90+
91+
/// Stubbed version of `ExInitializePushLock` so test targets can link
92+
///
93+
/// # Safety
94+
///
95+
/// `push_lock` must point to valid writable push lock storage.
96+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
97+
#[unsafe(no_mangle)]
98+
pub unsafe extern "C" fn ExInitializePushLock(push_lock: *mut ULONG_PTR) {
99+
// SAFETY: Test callers pass a valid pointer to push lock storage.
100+
unsafe {
101+
push_lock.write(0);
102+
}
103+
}
104+
105+
/// Stubbed version of `ExAcquirePushLockSharedEx` so test targets can link
106+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
107+
#[unsafe(no_mangle)]
108+
pub extern "C" fn ExAcquirePushLockSharedEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}
109+
110+
/// Stubbed version of `ExAcquirePushLockExclusiveEx` so test targets can link
111+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
112+
#[unsafe(no_mangle)]
113+
pub extern "C" fn ExAcquirePushLockExclusiveEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}
114+
115+
/// Stubbed version of `ExReleasePushLockSharedEx` so test targets can link
116+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
117+
#[unsafe(no_mangle)]
118+
pub extern "C" fn ExReleasePushLockSharedEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}
119+
120+
/// Stubbed version of `ExReleasePushLockExclusiveEx` so test targets can link
121+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
122+
#[unsafe(no_mangle)]
123+
pub extern "C" fn ExReleasePushLockExclusiveEx(_push_lock: *mut ULONG_PTR, _flags: ULONG) {}
124+
125+
/// Stubbed version of `ExAcquireSpinLockShared` so test targets can link
126+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
127+
#[unsafe(no_mangle)]
128+
pub extern "C" fn ExAcquireSpinLockShared(_spin_lock: *mut EX_SPIN_LOCK) -> KIRQL {
129+
0
130+
}
131+
132+
/// Stubbed version of `ExAcquireSpinLockSharedAtDpcLevel` so test targets can
133+
/// link
134+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
135+
#[unsafe(no_mangle)]
136+
pub extern "C" fn ExAcquireSpinLockSharedAtDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}
137+
138+
/// Stubbed version of `ExReleaseSpinLockShared` so test targets can link
139+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
140+
#[unsafe(no_mangle)]
141+
pub extern "C" fn ExReleaseSpinLockShared(_spin_lock: *mut EX_SPIN_LOCK, _old_irql: KIRQL) {}
142+
143+
/// Stubbed version of `ExReleaseSpinLockSharedFromDpcLevel` so test targets can
144+
/// link
145+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
146+
#[unsafe(no_mangle)]
147+
pub extern "C" fn ExReleaseSpinLockSharedFromDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}
148+
149+
/// Stubbed version of `ExAcquireSpinLockExclusive` so test targets can link
150+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
151+
#[unsafe(no_mangle)]
152+
pub extern "C" fn ExAcquireSpinLockExclusive(_spin_lock: *mut EX_SPIN_LOCK) -> KIRQL {
153+
0
154+
}
155+
156+
/// Stubbed version of `ExAcquireSpinLockExclusiveAtDpcLevel` so test targets
157+
/// can link
158+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
159+
#[unsafe(no_mangle)]
160+
pub extern "C" fn ExAcquireSpinLockExclusiveAtDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}
161+
162+
/// Stubbed version of `ExReleaseSpinLockExclusive` so test targets can link
163+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
164+
#[unsafe(no_mangle)]
165+
pub extern "C" fn ExReleaseSpinLockExclusive(_spin_lock: *mut EX_SPIN_LOCK, _old_irql: KIRQL) {}
166+
167+
/// Stubbed version of `ExReleaseSpinLockExclusiveFromDpcLevel` so test targets
168+
/// can link
169+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
170+
#[unsafe(no_mangle)]
171+
pub extern "C" fn ExReleaseSpinLockExclusiveFromDpcLevel(_spin_lock: *mut EX_SPIN_LOCK) {}
172+
173+
/// Stubbed version of `ExTryConvertSharedSpinLockExclusive` so test targets can
174+
/// link
175+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
176+
#[unsafe(no_mangle)]
177+
pub extern "C" fn ExTryConvertSharedSpinLockExclusive(_spin_lock: *mut EX_SPIN_LOCK) -> LOGICAL {
178+
1
179+
}
180+
40181
#[cfg(any(driver_model__driver_type = "KMDF", driver_model__driver_type = "UMDF"))]
41182
mod wdf {
42183
use crate::ULONG;

crates/wdk/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
no_std
1111
)]
1212

13+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
14+
extern crate alloc;
15+
1316
#[cfg(any(
1417
driver_model__driver_type = "WDM",
1518
driver_model__driver_type = "KMDF",
@@ -36,6 +39,9 @@ mod print;
3639
/// environments.
3740
pub mod fmt;
3841

42+
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
43+
pub mod sync;
44+
3945
#[cfg(any(driver_model__driver_type = "KMDF", driver_model__driver_type = "UMDF"))]
4046
pub mod wdf;
4147

crates/wdk/src/sync/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! Synchronization primitives backed by kernel-mode WDK objects.
2+
//!
3+
//! The wrappers in this module expose Rust guard-based access to common
4+
//! shared/exclusive kernel locks:
5+
//!
6+
//! - [`RwLock`] uses `ERESOURCE` for waitable reader-writer locking at `IRQL <=
7+
//! APC_LEVEL`.
8+
//! - [`PushLock`] uses `EX_PUSH_LOCK` for compact waitable reader-writer
9+
//! locking at `IRQL <= APC_LEVEL`.
10+
//! - [`RwSpinLock`] uses `EX_SPIN_LOCK` for very short non-waiting sections
11+
//! that can run up to `DISPATCH_LEVEL`.
12+
13+
pub use push_lock::*;
14+
pub use rw_lock::*;
15+
pub use rw_spin_lock::*;
16+
17+
mod push_lock;
18+
mod rw_lock;
19+
mod rw_spin_lock;
20+
21+
// Stable Rust does not support negative `Send` impls for these guard types.
22+
// This marker keeps guards from crossing threads, which ensures kernel lock
23+
// release and IRQL restoration happen on the acquiring thread.
24+
type NotSend = core::marker::PhantomData<alloc::rc::Rc<()>>;
25+
26+
const fn not_send() -> NotSend {
27+
core::marker::PhantomData
28+
}

0 commit comments

Comments
 (0)