From 0278962faad6d8559812f4d3709da93c38255c62 Mon Sep 17 00:00:00 2001 From: Mehdi Date: Sat, 17 Oct 2026 19:45:01 -0500 Subject: [PATCH 1/6] WIP: controller stops poking steady targets (module base cache, throttled checks, toast gate, copy skip) --- hmw-core/src/hider.rs | 43 +++++++++++++++++++++++++++++++++++ hmw-core/src/inject.rs | 43 ++++++++++++++++++++++++++++++++++- hmw-core/src/notifications.rs | 41 +++++++++++++++++++++++++++++++++ hmw-core/src/watcher.rs | 14 +++++++++++- src-tauri/src/lib.rs | 15 ++++++++++++ 5 files changed, 154 insertions(+), 2 deletions(-) diff --git a/hmw-core/src/hider.rs b/hmw-core/src/hider.rs index bda3df0..71a1d48 100644 --- a/hmw-core/src/hider.rs +++ b/hmw-core/src/hider.rs @@ -86,3 +86,46 @@ pub fn check_protection(pid: u32, payload_path: &str) -> Result<()> { )) }) } + +/// How long a steady target goes without a protection health check. +pub const PROTECTION_RECHECK: std::time::Duration = std::time::Duration::from_secs(15); + +/// Decides when `check_protection` is worth a remote thread. A target is +/// checked when its visible window set changed since the last check, or when +/// the last check is older than `PROTECTION_RECHECK`. +#[derive(Debug, Default)] +pub struct ProtectionThrottle { + seen: std::collections::HashMap, +} + +impl ProtectionThrottle { + /// True when `pid` should be checked now; records the check when it is. + pub fn due(&mut self, pid: u32, signature: u64, now: std::time::Instant) -> bool { + match self.seen.get(&pid) { + Some(&(last, at)) if last == signature && now.duration_since(at) < PROTECTION_RECHECK => { + false + } + _ => { + self.seen.insert(pid, (signature, now)); + true + } + } + } + + /// Forget a pid, so its next check is immediate. + pub fn forget(&mut self, pid: u32) { + self.seen.remove(&pid); + } + + /// Drop entries for pids no longer being tracked. + pub fn retain(&mut self, keep: impl Fn(u32) -> bool) { + self.seen.retain(|&pid, _| keep(pid)); + } +} + +/// Order-independent fingerprint of a set of window handles. +pub fn window_signature(hwnds: impl IntoIterator) -> u64 { + hwnds.into_iter().fold(0u64, |acc, h| { + acc.wrapping_add((h as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15).rotate_left(17)) + }) +} diff --git a/hmw-core/src/inject.rs b/hmw-core/src/inject.rs index 5586166..fed8a16 100644 --- a/hmw-core/src/inject.rs +++ b/hmw-core/src/inject.rs @@ -249,8 +249,49 @@ pub fn call_export(pid: u32, payload_path: &str, export: &str, hwnd: isize) -> R } } + // A loaded payload keeps its base for the life of the process. Remember it + // per (pid, start time, path) so steady targets skip the module snapshot. + let started = crate::process::process_started_at(pid); + if let Some(started) = started { + if let Some(base) = cached_base(pid, started, payload_path) { + match run_export(&handle, base, export, hwnd) { + Ok(()) => return Ok(()), + Err(_) => forget_base(pid), + } + } + } let base = ensure_loaded(&handle, pid, payload_path)?; - run_export(&handle, base, export, hwnd) + let result = run_export(&handle, base, export, hwnd); + match (&result, started) { + (Ok(()), Some(started)) => remember_base(pid, started, payload_path, base), + _ => forget_base(pid), + } + result +} + +type BaseCache = std::collections::HashMap; + +fn base_cache() -> std::sync::MutexGuard<'static, BaseCache> { + static CACHE: std::sync::OnceLock> = std::sync::OnceLock::new(); + CACHE + .get_or_init(Default::default) + .lock() + .unwrap_or_else(|p| p.into_inner()) +} + +fn cached_base(pid: u32, started: u64, payload_path: &str) -> Option { + match base_cache().get(&pid) { + Some((s, path, base)) if *s == started && path == payload_path => Some(*base), + _ => None, + } +} + +fn remember_base(pid: u32, started: u64, payload_path: &str, base: isize) { + base_cache().insert(pid, (started, payload_path.to_string(), base)); +} + +fn forget_base(pid: u32) { + base_cache().remove(&pid); } /// Start `export` of the payload at `base` in the target and wait for it. diff --git a/hmw-core/src/notifications.rs b/hmw-core/src/notifications.rs index b6e535e..93e91a4 100644 --- a/hmw-core/src/notifications.rs +++ b/hmw-core/src/notifications.rs @@ -34,6 +34,12 @@ fn staged_payload(payload_path: &str) -> Result { if dest.parent().is_none_or(|dir| !dir.is_dir()) { return Ok(payload_path.to_string()); } + // Steady state: the staged copy is current, so leave the file alone. + if let (Ok(src), Ok(dst)) = (std::fs::metadata(payload_path), std::fs::metadata(&dest)) { + if src.len() == dst.len() && src.modified().ok() == dst.modified().ok() { + return Ok(dest.to_string_lossy().into_owned()); + } + } match std::fs::copy(payload_path, &dest) { Ok(_) => Ok(dest.to_string_lossy().into_owned()), Err(_) if dest.is_file() => Ok(dest.to_string_lossy().into_owned()), @@ -80,9 +86,44 @@ pub fn apply_notification_toasts(hidden: bool, payload_path: &str) -> Vec