Skip to content

Commit

Permalink
Use sptr until strict provenance is stable
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Oct 21, 2024
1 parent 369165c commit d22135c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/yabg3nml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ tracing-appender = "0.2.3"
pelite = "0.10.0"
widestring = "1.1.0"
rand = "0.8.5"
# todo: remove sptr when strict provenance apis are stable
sptr = "0.3.2"

[dependencies.argh]
git = "https://github.com/google/argh"
Expand Down
10 changes: 6 additions & 4 deletions crates/yabg3nml/src/wapi/enum_windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::panic::{self, AssertUnwindSafe};

use eyre::Result;
// TODO: remove when strict provenance apis are stable
use sptr::{from_exposed_addr_mut, Strict};
use tracing::{error, trace_span};
use windows::Win32::{
Foundation::{BOOL, FALSE, HWND, LPARAM, TRUE},
Expand All @@ -15,16 +17,16 @@ pub fn EnumWindowsRs(cb: impl FnMut(HWND) -> Result<()> + Send + Sync) {
let _guard = span.enter();

let mut cb: UserCallback = Box::new(cb);
// TODO: Use strict provenance apis when stable
_ = unsafe { EnumWindows(Some(enum_cb), LPARAM(&raw mut cb as isize)) };
// TODO: Use strict provenance apis when stable and remove sptr
_ = unsafe { EnumWindows(Some(enum_cb), LPARAM((&raw mut cb).expose_addr() as _)) };
}

extern "system" fn enum_cb(param0: HWND, param1: LPARAM) -> BOOL {
let span = trace_span!("enum_cb");
let _guard = span.enter();

// TODO: Use strict provenance apis when stable
let cb = unsafe { &mut *(param1.0 as *mut UserCallback) };
// TODO: Use strict provenance apis when stable and remove sptr
let cb = unsafe { &mut *from_exposed_addr_mut::<UserCallback>(param1.0 as _) };

let result = panic::catch_unwind(AssertUnwindSafe(|| cb(param0)));

Expand Down

0 comments on commit d22135c

Please sign in to comment.