microseh 1.0.1

Structured Exception Handling (SEH) for Rust
docs.rs failed to build microseh-1.0.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: microseh-1.1.2

MicroSEH is a tiny library that implements Structured Exception Handling (SEH) in Rust and can catch and handle hardware exceptions.

Why?

Hardware exceptions are a very powerful tool for specific use cases. One such use case is to detect and handle illegal instructions at runtime.

Implementation

It turns out that implementing SEH in pure Rust has its own issues (as seen in this article from NAMAZSO)

This library uses a different, simpler approach, which is to use a C stub that calls back into Rust, wrapping the call in a __try __except block.

Usage

Add this to your Cargo.toml:

[dependencies]

microseh = "1.0"

Minimal Example: Dereference a null pointer without crashing the program, and return the handled exception.

fn guarded() -> Result<(), Box<dyn Error>> {
    microseh::try_seh(|| unsafe {
        std::ptr::read_volatile::<i32>(0 as _);
    })?;
}

Accessing Exception Data: You can obtain the address and register dump of an exception.

if let Err(ex) = microseh::try_seh(|| unsafe {
    // *debatable coding choices go here*
}) {
    println!("address: {:x}", ex.address());
    println!("rax: {:x}", ex.registers().rax());
}

For additional examples and practical use cases, please visit the examples directory!

Portability

SEH is an extension to the C language developed by Microsoft, and it is exclusively available on Windows when using Microsoft Visual C++ (MSVC).

MicroSEH is compatible with and has been tested on Windows platforms with the following architectures: x86, x86_64 and aarch64.