Skip to content

Commit

Permalink
merge pull request #16 from sonodima/chore/unify-docsrs-windows-checks
Browse files Browse the repository at this point in the history
unify docsrs windows checks
  • Loading branch information
sonodima authored Oct 3, 2024
2 parents 51a9edd + 8701eb0 commit 8f37031
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 52 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ std = []
default-target = "x86_64-pc-windows-msvc"
rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]
targets = ["i686-pc-windows-msvc", "x86_64-pc-windows-msvc"]
targets = [
"i686-pc-windows-msvc",
"x86_64-pc-windows-msvc",
"aarch64-pc-windows-msvc",
]
17 changes: 11 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#[cfg(not(docsrs))]
#[cfg(all(windows, not(docsrs)))]
extern crate cc;

#[cfg(not(docsrs))]
#[cfg(all(windows, not(docsrs)))]
fn main() {
if std::env::var("HOST").unwrap().contains("gnu") { return; }
cc::Build::new().file("src/stub.c").compile("sehstub");
// TODO: this is a hack to allow this crate to build on docs.rs.
// https://github.com/sonodima/microseh/pull/11#issuecomment-2385633164
if !std::env::var("HOST").unwrap_or_default().contains("gnu") {
cc::Build::new().file("src/stub.c").compile("sehstub");
}
}

#[cfg(docsrs)]
fn main() {}
#[cfg(any(not(windows), docsrs))]
fn main() {
println!("cargo:warning=building for a non-supported platform, exception handling will not be available");
}
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,10 @@ pub use exception::Exception;

type HandledProc = unsafe extern "system" fn(*mut c_void);

const MS_SUCCEEDED: u32 = 0x0;
const MS_CATCHED: u32 = 0x1;
const MS_DISABLED: u32 = 0x2;

#[cfg(not(docsrs))]
extern "C" {
#[link_name = "HandlerStub"]
fn handler_stub(proc: HandledProc, closure: *mut c_void, exception: *mut Exception) -> u32;
}

#[cfg(docsrs)]
unsafe fn handler_stub(
_proc: HandledProc,
_closure: *mut c_void,
_exception: *mut Exception,
) -> u32 {
MS_DISABLED
}

#[inline(always)]
unsafe extern "system" fn handled_proc<F>(closure: *mut c_void)
where
F: FnMut(),
Expand All @@ -40,22 +26,35 @@ where
}
}

#[cfg(all(windows, not(docsrs)))]
extern "C" {
#[link_name = "HandlerStub"]
fn handler_stub(proc: HandledProc, closure: *mut c_void, exception: *mut Exception) -> u32;
}

#[cfg(all(windows, not(docsrs)))]
fn do_execute_proc<F>(mut closure: F) -> Result<(), Exception>
where
F: FnMut(),
{
let mut exception = Exception::empty();
let closure = &mut closure as *mut _ as *mut c_void;

unsafe {
match handler_stub(handled_proc::<F>, closure, &mut exception) {
MS_CATCHED => Err(exception),
MS_DISABLED => panic!("exception handling is not supported in this build of microseh"),
/* MS_SUCCEEDED */ _ => Ok(()),
}
match unsafe { handler_stub(handled_proc::<F>, closure, &mut exception) } {
MS_SUCCEEDED => Ok(()),
MS_CATCHED => Err(exception),
_ => unreachable!(),
}
}

#[cfg(any(not(windows), docsrs))]
fn do_execute_proc<F>(closure: F) -> Result<(), Exception>
where
F: FnMut(),
{
panic!("exception handling is not available in this build of microseh")
}

/// Executes the provided closure in a context where exceptions are handled, catching any\
/// hardware exceptions that occur.
///
Expand Down Expand Up @@ -95,6 +94,7 @@ where
///
/// If exception handling is disabled in the build, which occurs when the library is\
/// not built on Windows with Microsoft Visual C++.
#[inline(always)]
pub fn try_seh<F, R>(mut closure: F) -> Result<R, Exception>
where
F: FnMut() -> R,
Expand Down
26 changes: 3 additions & 23 deletions src/stub.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <stdint.h>

#define MS_SUCCEEDED 0x0
#define MS_CATCHED 0x1
#define MS_DISABLED 0x2

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define MS_SUCCEEDED 0x0
#define MS_CATCHED 0x1

#define TG_ARCH_X86 1
#define TG_ARCH_X64 2
#define TG_ARCH_ARM64 3
Expand Down Expand Up @@ -152,20 +149,3 @@ uint32_t HandlerStub(_In_ PHANDLED_PROC HandledProc, _In_ PVOID Closure, _Inout_

return Result;
}

#else // _WIN32

#pragma message("WARNING: Exception handling is disabled! This configuration is intended for use by docs.rs only")

// We are not compiling with Windows MSVC, so we implement a dummy HandlerStub to
// fix docs.rs compilation.
uint32_t HandlerStub(void* HandledProc, void* Closure, void* Exception)
{
(void)HandledProc;
(void)Closure;
(void)Exception;

return MS_DISABLED;
}

#endif // _WIN32

0 comments on commit 8f37031

Please sign in to comment.