Skip to content

feat: improve user-mode and kernel-mode driver panic handlers#642

Open
Alan632 wants to merge 18 commits into
microsoft:mainfrom
Alan632:panic_handler
Open

feat: improve user-mode and kernel-mode driver panic handlers#642
Alan632 wants to merge 18 commits into
microsoft:mainfrom
Alan632:panic_handler

Conversation

@Alan632
Copy link
Copy Markdown
Contributor

@Alan632 Alan632 commented Apr 16, 2026

Description

Previous panic handlers would initiate an infinite loop.

This PR adds custom panic handler implementations for both WDM/KMDF and UMDF Rust drivers and focuses on providing best effort information to a debugger.

TODO: Update the UMDF cargo-wdk lib.rs template to include UMDF panic registration. New issue created to track this: Issue 646

Verification

  • Verified with sample-kmdf-driver (kernel mode) by initiating panic and extracting panic/bugcheck info from post mortem memory dump.
  • Verified with sample-umdf-driver (user mode) by initiating panic and viewing output in DebugView.

@Alan632 Alan632 self-assigned this Apr 16, 2026
Copilot AI review requested due to automatic review settings April 16, 2026 00:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds improved panic handling for Windows Drivers Rust (WDR) targets: kernel-mode panics now trigger a bugcheck with source-location metadata, and UMDF drivers can install a std panic hook to emit panic info to the debugger.

Changes:

  • Implement WDM/KMDF panic handler that calls KeBugCheckEx with panic location parameters.
  • Add UMDF-only install_panic_hook() to print panic info (and break into debugger in debug builds).
  • Add wdk-panic build script + dependencies and update UMDF template to install the hook.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
crates/wdk-panic/src/lib.rs Adds kernel bugcheck panic handler + UMDF panic hook installer and updates crate docs/cfgs.
crates/wdk-panic/build.rs Adds build script to emit WDK cfg settings for conditional compilation.
crates/wdk-panic/Cargo.toml Adds wdk dependency and wdk-build build-dependency.
crates/cargo-wdk/templates/umdf/lib.rs.tmp Calls wdk_panic::install_panic_hook() in UMDF DriverEntry.
crates/cargo-wdk/templates/umdf/Cargo.toml.tmp Adds wdk-panic dependency to UMDF template.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/wdk-panic/src/lib.rs
Comment on lines +15 to +19
[dependencies]
wdk.workspace = true

[build-dependencies]
wdk-build.workspace = true
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdk-panic now adds a new public API (install_panic_hook) and changes panic behavior for WDM/KMDF (bugcheck instead of an infinite loop), but the crate version remains 0.4.1. Please bump the crate version (and update the changelog in the release process) so downstream consumers/templates can depend on a version that actually contains this API/behavior.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version bumps happen in release prs


[dependencies]
wdk = "0.4.1"
wdk-panic = "0.4.1"
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UMDF template pins wdk-panic = "0.4.1", but this PR introduces the install_panic_hook API. If a user generates a project and pulls from crates.io, it will fail to compile unless the template is updated to the new wdk-panic version (or the template version is substituted during release).

Suggested change
wdk-panic = "0.4.1"
wdk-panic = "0.5.1"

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update after release.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this not be added now? i think we had infra to not run certain tests in release prs

Copy link
Copy Markdown
Contributor

@gurry gurry May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That logic works only in release-plz PRs so we can't switch to 0.5.1 in this PR. We have to wait until the next release.

Copilot AI review requested due to automatic review settings April 17, 2026 19:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/wdk-panic/src/lib.rs
pub unsafe extern "system" fn driver_entry(
_driver: PDRIVER_OBJECT,
_registry_path: PCUNICODE_STRING,
) -> NTSTATUS {
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdk_panic::install_panic_hook() is only defined under #[cfg(all(not(test), driver_model__driver_type = "UMDF"))]. Calling it unconditionally from driver_entry will fail to compile when building with cfg(test) (e.g., cargo test). Gate the call with #[cfg(not(test))] (or relax the cfg on install_panic_hook) so test builds don’t break.

Suggested change
) -> NTSTATUS {
) -> NTSTATUS {
#[cfg(not(test))]

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings April 17, 2026 20:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/wdk-panic/src/lib.rs Outdated
Comment thread crates/cargo-wdk/templates/umdf/lib.rs.tmp
Comment thread crates/cargo-wdk/templates/umdf/Cargo.toml.tmp
Comment thread crates/wdk-panic/src/lib.rs Outdated
Alan Ngo and others added 2 commits April 17, 2026 15:19
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Alan632 <aln.noda7@gmail.com>
Copilot AI review requested due to automatic review settings April 17, 2026 22:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/wdk-panic/src/lib.rs Outdated
Copilot AI review requested due to automatic review settings April 17, 2026 22:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/wdk-panic/build.rs
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.45%. Comparing base (5748ea2) to head (26798a0).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #642      +/-   ##
==========================================
+ Coverage   78.44%   79.45%   +1.00%     
==========================================
  Files          25       26       +1     
  Lines        5201     5500     +299     
  Branches     5201     5500     +299     
==========================================
+ Hits         4080     4370     +290     
  Misses       1001     1001              
- Partials      120      129       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI review requested due to automatic review settings April 20, 2026 20:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI review requested due to automatic review settings April 20, 2026 22:18
@Alan632 Alan632 marked this pull request as ready for review April 20, 2026 22:20
@Alan632 Alan632 added the enhancement New feature or request label Apr 20, 2026
@Alan632 Alan632 changed the title feat: add panic handler for WDR feat: add panic handler for windows-drivers-rs Apr 20, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/wdk-panic/src/lib.rs Outdated
/// Registers a panic hook for UMDF drivers that prints panic information via
/// [`wdk::println!`] and then aborts the host process.
///
/// Output is routed through [`OutputDebugStringA`], which is received by any
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rustdoc link [OutputDebugStringA] is unqualified and likely resolves to no item in this crate, which will trigger broken_intra_doc_links (docs CI uses RUSTDOCFLAGS=-D warnings). Prefer either fully qualifying it (e.g., wdk_sys::windows::OutputDebugStringA with an appropriate direct dependency) or formatting it as code (backticks) instead of an intra-doc link.

Suggested change
/// Output is routed through [`OutputDebugStringA`], which is received by any
/// Output is routed through `OutputDebugStringA`, which is received by any

Copilot uses AI. Check for mistakes.
#[cfg(all(not(test), driver_model__driver_type = "UMDF"))]
pub fn install_panic_hook() {
std::panic::set_hook(Box::new(|info| {
wdk::println!("[PANIC] {info}");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have a sample output of how this looks like when you have a debugger attached to the umdf driver?

Comment thread crates/wdk-panic/src/lib.rs Outdated

// High, uncommon bugcheck code to avoid collision with OS-defined identifiers.
// Lower 24 bits spell `PNC` (0x50 'P', 0x4E 'N', 0x43 'C') for recognizability.
const BUGCHECK_RUST_CODE: u32 = 0x8050_4E43;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this reads better if we derive it from the ASCII tag instead of spelling the hex literal directly. Something like const RUST_PANIC_BUGCHECK_CODE: u32 = u32::from_be_bytes(*b"RPNC"); keeps the same value but makes the intent much clearer. Also i think starting with 0x8 is not provably necessary.

@wmmc88 wmmc88 changed the title feat: add panic handler for windows-drivers-rs feat: improve user-mode and kernel-mode driver panic handlers Apr 30, 2026
Copy link
Copy Markdown
Contributor

@gurry gurry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just one minor comment on a safety comment.

Comment thread crates/wdk-panic/src/lib.rs Outdated
Comment on lines +66 to +75
// SAFETY: `KeBugCheckEx` is a Windows kernel API exported by `ntoskrnl.exe`
// that is callable at any IRQL and never returns (it halts the system with a
// bugcheck). The parameters are scalar `usize` values recorded in the crash
// dump; `KeBugCheckEx` does not dereference `panic_filename_ptr` — it is stored
// as an opaque value for post-mortem analysis. The FFI signature matches the
// WDK declaration in `wdm.h`. This call is sound because:
// 1. The function is always available in kernel mode (linked via ntoskrnl.lib).
// 2. The calling convention is correct (`extern "system"` maps to the
// appropriate Windows calling convention for the target architecture).
// 3. The `-> !` return type is upheld — `KeBugCheckEx` never returns.
Copy link
Copy Markdown
Contributor

@gurry gurry May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A safety comment only needs to explain that you are calling the function correctly and upholding its pre-conditions, e.g. arguments being passed are valid, lifetimes are not violoated etc. It need not explain that the function is declared correctly which this comment seems to do.

(Assuming the declaration is correct) this function is always safe to call, regardless of arg values. So may be that's what the comment should say.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

i think the only thing worth calling out here is that KeBugCheckEx is callable at every irql

/// wdk_panic::install_panic_hook();
/// ```
#[cfg(all(not(test), driver_model__driver_type = "UMDF"))]
pub fn install_panic_hook() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer term we could automatically insert a call to this function in a UMDF driver through the #[driver_entry] attribute provided by the wdf crate.

Copy link
Copy Markdown
Collaborator

@wmmc88 wmmc88 May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1! there are various versions of attribute macros floating around to help with the friction around bridging the gap between c callbacks and rust functions. this is definitely one of the things we can put in the glue layer


[dependencies]
wdk = "0.4.1"
wdk-panic = "0.4.1"
Copy link
Copy Markdown
Contributor

@gurry gurry May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That logic works only in release-plz PRs so we can't switch to 0.5.1 in this PR. We have to wait until the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants