agentleFS
MicrosoftKnown publishermicrosoft/Windows-rust-driver-samples/AGENTS.md

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.

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
  1. Very specific:

    Place init code in #[link_section = "INIT"].

    line 85
  2. Very specific:

    All WDF framework function calls go through the call_unsafe_wdf_function_binding!

    line 90
  3. Very specific:

    Every driver crate must include both, gated behind #[cfg(not(test))]:

    line 131
  4. Distinctive:

    All driver crates enable strict clippy lints:

    line 147
  5. Very specific:

    Every .rs file starts with:

    line 177

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

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 -- -Async

Lint 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 --locked

Architecture

Workspace Layout

  • general/ — Functional driver samples (e.g., echo/kmdf/driver/DriverSync is a KMDF echo driver)
  • tools/ — Diagnostic/verification drivers (e.g., dv/kmdf/fail_driver_pool_leak intentionally 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_*_SIZE constants, and wdf_declare_context_type! macro invocations
  • driver.rs — DriverEntry (exported as #[export_name = "DriverEntry"], extern "system") and EvtDriverDeviceAdd callback
  • device.rs — Device creation, PnP/power callbacks
  • queue.rs — I/O queue initialization and request handling callbacks
  • build.rs — Calls wdk_build::configure_wdk_binary_build()
  • *.inx — INF template file for driver installation

Key Crate Dependencies (from windows-drivers-rs)

CratePurpose
wdkSafe Rust wrappers and macros (println!, paged_code!, nt_success, wdf::Timer, wdf::SpinLock)
wdk-sysRaw FFI bindings to WDK. Use via call_unsafe_wdf_function_binding! macro
wdk-allocWdkAllocator — kernel-compatible global allocator
wdk-panicKernel-compatible panic handler
wdk-buildBuild-time configuration for driver compilation and cargo-make integration

Key Conventions

Driver Entry Points

  • DriverEntry must use #[export_name = "DriverEntry"] and extern "system" calling convention
  • Place init code in #[link_section = "INIT"]; place pageable code in #[link_section = "PAGE"] and invoke paged_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) — generates wdf_object_get_my_context accessor
  • wdf_declare_context_type_with_name!(MyContext, custom_getter_name) — generates a named accessor
  • wdf_get_context_type_info!(MyContext) — retrieves the type info pointer for WDF_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 rustfmt with unstable options (see rustfmt.toml): imports_granularity = "Crate", group_imports = "StdExternalCrate", hex_literal_case = "Upper", among others
  • TOML: Uses taplo with 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.0

Download now (ZIP)

Does 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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.