-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove reliance on sptr and exposing provenance
- Loading branch information
1 parent
d6c2682
commit 68d978b
Showing
4 changed files
with
62 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,84 @@ | ||
use std::panic::{self, AssertUnwindSafe}; | ||
use std::{ | ||
mem, | ||
panic::{self, AssertUnwindSafe}, | ||
sync::Mutex, | ||
}; | ||
|
||
use eyre::Result; | ||
// TODO: remove when strict provenance apis are stable | ||
use sptr::{from_exposed_addr_mut, Strict}; | ||
use shared::utils::SuperLock; | ||
use tracing::{error, trace_span}; | ||
use windows::Win32::{ | ||
Foundation::{BOOL, FALSE, HWND, LPARAM, TRUE}, | ||
Foundation::{BOOL, HWND, LPARAM}, | ||
UI::WindowsAndMessaging::EnumWindows, | ||
}; | ||
|
||
type UserCallback<'a> = Box<dyn FnMut(HWND) -> Result<()> + Send + Sync + 'a>; | ||
|
||
struct FfiCb(UserCallback<'static>); | ||
|
||
trait StaticFfiCbMethods { | ||
unsafe fn set_cb(&self, cb: UserCallback); | ||
unsafe fn call(&self, hwnd: HWND) -> Result<()>; | ||
fn drop(&self); | ||
} | ||
|
||
impl StaticFfiCbMethods for Mutex<Option<FfiCb>> { | ||
/// SAFETY: | ||
/// Caller promises to drop before end of scope where original closure was created | ||
/// this is because closure may have captures, this is !'static | ||
unsafe fn set_cb(&self, cb: UserCallback) { | ||
let _static = unsafe { mem::transmute::<UserCallback, UserCallback<'static>>(cb) }; | ||
*self.super_lock() = Some(FfiCb(_static)); | ||
} | ||
|
||
/// SAFETY: | ||
/// This must be called in scope where closure captures are still valid | ||
unsafe fn call(&self, hwnd: HWND) -> Result<()> { | ||
if let Some(cb) = &mut *self.super_lock() { | ||
cb.0(hwnd) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn drop(&self) { | ||
*self.super_lock() = None; | ||
} | ||
} | ||
|
||
static CB: Mutex<Option<FfiCb>> = Mutex::new(None); | ||
|
||
#[allow(non_snake_case)] | ||
pub fn EnumWindowsRs(cb: impl FnMut(HWND) -> Result<()> + Send + Sync) { | ||
let span = trace_span!("EnumWindowsRs"); | ||
let _guard = span.enter(); | ||
|
||
let mut cb: UserCallback = Box::new(cb); | ||
// TODO: Use strict provenance apis when stable and remove sptr | ||
_ = unsafe { EnumWindows(Some(enum_cb), LPARAM((&raw mut cb).expose_addr() as _)) }; | ||
unsafe { | ||
CB.set_cb(Box::new(cb)); | ||
} | ||
|
||
_ = unsafe { EnumWindows(Some(enum_cb), None) }; | ||
|
||
CB.drop(); | ||
} | ||
|
||
extern "system" fn enum_cb(param0: HWND, param1: LPARAM) -> BOOL { | ||
extern "system" fn enum_cb(param0: HWND, _: LPARAM) -> BOOL { | ||
let span = trace_span!("enum_cb"); | ||
let _guard = span.enter(); | ||
|
||
// 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))); | ||
let result = panic::catch_unwind(AssertUnwindSafe(|| unsafe { CB.call(param0) })); | ||
|
||
match result { | ||
// no panic and cb returned Ok | ||
Ok(Ok(_)) => TRUE, | ||
Ok(Ok(_)) => true.into(), | ||
|
||
// no panic and callback returned Err | ||
Ok(Err(err)) => { | ||
error!("{err}"); | ||
FALSE | ||
false.into() | ||
} | ||
|
||
// panic | ||
Err(_) => FALSE, | ||
Err(_) => false.into(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[toolchain] | ||
channel = "nightly-2024-11-17" | ||
channel = "nightly-2024-12-17" | ||
components = ["rustfmt", "clippy"] | ||
profile = "minimal" |