Windows-rust-driver-samples
AGENTS.md360 starsChanged 8 months agoApache-2.0View on GitHub
Rust ports of Windows driver samples from the official Windows Driver Samples, using crates from windows-drivers-rs. All driver crates target #![no_std] and compile as cdylib for the Windows kernel. Requires EWDK build environment, Clang (LLVM), and cargo-make. CI enforces all of these — run before submitting PRs: Each driver crate follows this file structure: All WDF framework function calls go through the call_unsafe_wdf_function_binding! macro from wdk-sys. These are always unsafe blocks: WDF structs require their Size field to be set.…
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repositoryOne other file it gives its agents.
- CLAUDE.mdCLAUDE.md
The file
We split this AGENTS.md into the rules it gives an agent
5 rules. About 0 are common, 1 is distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 85
Place init code in #[link_section = "INIT"].
- Very specific: line 90
All WDF framework function calls go through the call_unsafe_wdf_function_binding!
- Very specific: line 131
Every driver crate must include both, gated behind #[cfg(not(test))]:
- Distinctive: line 147
All driver crates enable strict clippy lints:
- Very specific: line 177
Every .rs file starts with:
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Copilot Instructions
Rust ports of Windows driver samples from the official Windows Driver Samples, using crates from windows-drivers-rs. All driver crates target #![no_std] and compile as cdylib for the Windows kernel.
Build Commands
Requires EWDK build environment, Clang (LLVM), and cargo-make.
# Build all workspace members
cargo build --locked
# Build and package drivers (stamps INF, creates CAT file in Package/)
cargo make
# Build a specific driver sample
cargo make --cwd general/echo/kmdf/driver/DriverSync
# Run the echo test app
cargo run --bin echoapp
cargo run --bin echoapp -- -AsyncLint and Formatting
CI enforces all of these — run before submitting PRs:
# Clippy (treats warnings as errors)
cargo clippy --locked --all-targets -- -D warnings
# Clippy with nightly features
cargo +nightly clippy --locked --all-targets --features nightly -- -D warnings
# Rust formatting (requires nightly due to unstable rustfmt options)
cargo +nightly fmt --all -- --check
# TOML formatting
taplo fmt --check --diff
# Unused dependency detection
cargo machete
# Security audit
cargo audit --deny warnings
# Documentation build (warnings are errors via RUSTDOCFLAGS=-D warnings in CI)
cargo doc --lockedArchitecture
Workspace Layout
general/— Functional driver samples (e.g.,echo/kmdf/driver/DriverSyncis a KMDF echo driver)tools/— Diagnostic/verification drivers (e.g.,dv/kmdf/fail_driver_pool_leakintentionally leaks memory for Driver Verifier testing)general/echo/kmdf/exe— User-mode test app (echoapp) for the echo driver
Driver Crate Anatomy
Each driver crate follows this file structure:
lib.rs— Crate root with#![no_std], clippy lints, module declarations, context structs,WDF_*_SIZEconstants, andwdf_declare_context_type!macro invocationsdriver.rs—DriverEntry(exported as#[export_name = "DriverEntry"],extern "system") andEvtDriverDeviceAddcallbackdevice.rs— Device creation, PnP/power callbacksqueue.rs— I/O queue initialization and request handling callbacksbuild.rs— Callswdk_build::configure_wdk_binary_build()*.inx— INF template file for driver installation
Key Crate Dependencies (from windows-drivers-rs)
| Crate | Purpose |
wdk | Safe Rust wrappers and macros (println!, paged_code!, nt_success, wdf::Timer, wdf::SpinLock) |
wdk-sys | Raw FFI bindings to WDK. Use via call_unsafe_wdf_function_binding! macro |
wdk-alloc | WdkAllocator — kernel-compatible global allocator |
wdk-panic | Kernel-compatible panic handler |
wdk-build | Build-time configuration for driver compilation and cargo-make integration |
Key Conventions
Driver Entry Points
DriverEntrymust use#[export_name = "DriverEntry"]andextern "system"calling convention- Place init code in
#[link_section = "INIT"]; place pageable code in#[link_section = "PAGE"]and invokepaged_code!()at function entry - WDF callbacks use
extern "C"calling convention
WDF Function Calls
All WDF framework function calls go through the call_unsafe_wdf_function_binding! macro from wdk-sys. These are always unsafe blocks:
unsafe {
call_unsafe_wdf_function_binding!(
WdfDriverCreate,
driver as PDRIVER_OBJECT,
registry_path,
WDF_NO_OBJECT_ATTRIBUTES,
&raw mut driver_config,
driver_handle_output,
)
}WDF Structure Sizes
WDF structs require their Size field to be set. Use const-evaluated WDF_*_SIZE constants with compile-time truncation assertions:
#[allow(
clippy::cast_possible_truncation,
reason = "size_of::<WDF_DRIVER_CONFIG>() is known to fit in ULONG due to below const assert"
)]
const WDF_DRIVER_CONFIG_SIZE: ULONG = {
const S: usize = core::mem::size_of::<WDF_DRIVER_CONFIG>();
const { assert!(S <= ULONG::MAX as usize) };
S as ULONG
};WDF Object Contexts
Context types are registered using macros from wdf_object_context.rs:
wdf_declare_context_type!(MyContext)— generateswdf_object_get_my_contextaccessorwdf_declare_context_type_with_name!(MyContext, custom_getter_name)— generates a named accessorwdf_get_context_type_info!(MyContext)— retrieves the type info pointer forWDF_OBJECT_ATTRIBUTES.ContextTypeInfo
Kernel Allocator and Panic Handler
Every driver crate must include both, gated behind #[cfg(not(test))]:
#[cfg(not(test))]
extern crate wdk_panic;
#[cfg(not(test))]
use wdk_alloc::WdkAllocator;
#[cfg(not(test))]
#[global_allocator]
static GLOBAL_ALLOCATOR: WdkAllocator = WdkAllocator;Clippy Configuration
All driver crates enable strict clippy lints:
#![deny(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![warn(clippy::cargo)]
#![allow(clippy::missing_safety_doc)]Nightly Feature Gating
Crates expose an optional nightly feature for unstable Rust capabilities. The feature cascades through wdk and wdk-sys:
[features]
nightly = ["wdk/nightly", "wdk-sys/nightly"]Formatting
- Rust: Uses nightly
rustfmtwith unstable options (seerustfmt.toml):imports_granularity = "Crate",group_imports = "StdExternalCrate",hex_literal_case = "Upper", among others - TOML: Uses
taplowith CRLF line endings (taplo.toml)
Rust Flags
-C target-feature=+crt-static is set in .cargo/config.toml and must also be in CI RUSTFLAGS. CI also sets -D warnings to deny all warnings.
Copyright Headers
Every .rs file starts with:
// Copyright (c) Microsoft Corporation.
// License: MIT OR Apache-2.0Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

