From d22135ce36cfcc13b9b9d25d3b08a105308708ac Mon Sep 17 00:00:00 2001 From: Cherry <13651622+MolotovCherry@users.noreply.github.com> Date: Mon, 21 Oct 2024 11:40:44 -0700 Subject: [PATCH] Use sptr until strict provenance is stable --- Cargo.lock | 7 +++++++ crates/yabg3nml/Cargo.toml | 2 ++ crates/yabg3nml/src/wapi/enum_windows.rs | 10 ++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c71964..12d26ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1602,6 +1602,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "sptr" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b9b39299b249ad65f3b7e96443bad61c02ca5cd3589f46cb6d610a0fd6c0d6a" + [[package]] name = "syn" version = "1.0.109" @@ -2256,6 +2262,7 @@ dependencies = [ "rand", "sha256", "shared", + "sptr", "tracing", "tracing-appender", "tracing-subscriber", diff --git a/crates/yabg3nml/Cargo.toml b/crates/yabg3nml/Cargo.toml index b329d2a..e0552b9 100644 --- a/crates/yabg3nml/Cargo.toml +++ b/crates/yabg3nml/Cargo.toml @@ -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" diff --git a/crates/yabg3nml/src/wapi/enum_windows.rs b/crates/yabg3nml/src/wapi/enum_windows.rs index c68daa5..d7bb9cb 100644 --- a/crates/yabg3nml/src/wapi/enum_windows.rs +++ b/crates/yabg3nml/src/wapi/enum_windows.rs @@ -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}, @@ -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::(param1.0 as _) }; let result = panic::catch_unwind(AssertUnwindSafe(|| cb(param0)));