-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panic Strategy #6
Comments
I 100% agree that it is typically not acceptable to trigger a bugcheck like this and I agree that code should always prevent itself from getting to a corrupted or unrecoverable state. However, panicking is a core part of the rust language, and a panic handler is required to compile rust code as it defines what happens when a rust function call triggers a panic (ie. via
I am unsure what you meant by disallow here. AFAIK, there is no mechanism in Rust to prevent a developer from calling |
Here is an example of a panic handler implementation that statically asserts at compile time that the resulting program never panics: https://github.com/lachlansneff/no-panics-whatsoever/blob/main/src/lib.rs
I understand, however asserts are not present in release builds, but panics are. So, maybe a possible strategy here is to ensure that a release binary can never panic? |
My interpretation of what jxy-s is saying is panic is likely overused in the rust language right now, and this could lead to quality issues with spurious BSODs from hidden panic calls. A common example of this would be memory allocation with the rust containers (Vec, etc). Rust has added custom allocators to containers, but it has been considered experimental for quite some time now. I think it would be beneficial to consider options with how to make this a bit safer for developers to avoid some mishaps that are likely going to be very easy to make (e.g. accidentally using the wrong memory allocator). |
For posterity, Rust support has been already been discussed in length for the Linux kernel, I'll quote Linus here from his email:
The concern I have and the reason I raised this topic is in the same vein that Linus raises in the quoted thread. If Microsoft provides this crate with the statement of (from the README), "This repo is a collection of Rust crates that enable developers to develop Windows Drivers in Rust. It is the intention to support both WDM and WDF driver development models." This subject must be considered carefully. And Microsoft should provide ergonomically safe and appropriate facilities to "develop Windows Drivers in Rust". |
I have a related question -- I ended up here because I was wondering whether panics in Rust code in windows kernel space would be fundamentally unsafe because of the kernel APIs not being written with unwinding in mind? Most C code typically isn't. But IIRC windows SEH involves something like unwinding so I wasn't sure if that's true or not. Most of my dev experience is on Linux so out of my element here. |
Windows SEH does not unwind in the way I believe you're expecting it to. It only invokes "Termination Handlers" ( For Rust in the Windows kernel to use This crate currently requires |
This is a bit pedantic but I think it's an important distinction: There's 3 aspects to "playing nicely" with panic:
I think you would want all 3 to be true for it to be safe. |
While having no panics at all obviously solves this issue, when there is a When we encounter an unhandled exception inside a module of a program, we crash that program. In my opinion crashing the kernel in this case is the only sensible option when actually encountering a panic. For debug mode, I personally prefer to print the actual panic info, emit an |
"Panic" in the Windows Kernel (or "bugchecking" the system KeBugCheckEx) is a last resort and should be reserved only for cases where the kernel is in a corrupted and unrecoverable state. It is usually never acceptable to bugcheck the system. I find it unlikely that any Rust crate would ever need to bugcheck the kernel.
Today this library notes a "TODO" on this topic:
windows-drivers-rs/crates/wdk-panic/src/lib.rs
Lines 29 to 33 in cd1fd23
Ultimately the kernel should handle errors cleanly and, by all means necessary, prevent itself from getting into a corrupted or unrecoverable state. I worry that the introduction of a panic handler and the interactions between other crates written for the Windows Kernel could degrade the overall reliability of the kernel.
I'm not certain the best way to approach this problem. My initial reaction is disallow panic handlers when developing against the Windows Kernel and reserve "panics" (bugchecks) for the rare and extreme cases. To achieve this the crates used in the kernel would need to always communicate errors rather than giving up with a panic. Thankfully, Rust has robust and capable error handling patterns with things like
match
andResult
. I'd like to raise this as an open topic to the community to discuss strategies for panic handling for the Windows Kernel.The text was updated successfully, but these errors were encountered: