Repository navigation
fix: 修复部分游戏启动时的使用 RegOpenKeyExA 读注册表导致退出的错误 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,14 @@ use std::sync::atomic::{AtomicUsize, Ordering}; | |
| use std::sync::Mutex; | ||
|
|
||
| use once_cell::sync::{Lazy, OnceCell}; | ||
| use windows_sys::Win32::System::Registry::{RegCloseKey, RegOpenKeyExA, RegOpenKeyExW, HKEY}; | ||
|
|
||
| use crate::config::Config; | ||
| use crate::iohook::hook_table::{hook_table_apply, null_module, HookSymbol}; | ||
| use crate::iohook::proc_addr; | ||
| use crate::platform::winapi::{self, RegQueryValueExAFn, RegQueryValueExWFn}; | ||
| use crate::util::api::Api; | ||
| use crate::util::win32::handle_from_value; | ||
|
|
||
| const fn predefined_hkey(value: u32) -> usize { | ||
| value as i32 as isize as usize | ||
|
|
@@ -38,6 +40,7 @@ const SOFTWARE_KEY: [u16; 9] = [ | |
| 0, | ||
| ]; | ||
|
|
||
| type RegOpenKeyExAFn = unsafe extern "system" fn(usize, *const c_char, u32, u32, *mut usize) -> u32; | ||
| type RegOpenKeyExWFn = unsafe extern "system" fn(usize, *const u16, u32, u32, *mut usize) -> u32; | ||
| type RegCreateKeyExWFn = unsafe extern "system" fn( | ||
| usize, | ||
|
|
@@ -91,6 +94,7 @@ static KEYS: Lazy<Mutex<Vec<VirtualKey>>> = Lazy::new(|| Mutex::new(Vec::new())) | |
| static HANDLES: Lazy<Mutex<HashMap<usize, usize>>> = Lazy::new(|| Mutex::new(HashMap::new())); | ||
| static API: OnceCell<Api> = OnceCell::new(); | ||
|
|
||
| static ORIG_OPEN_KEY_EX_A: AtomicUsize = AtomicUsize::new(0); | ||
| static ORIG_OPEN_KEY_EX_W: AtomicUsize = AtomicUsize::new(0); | ||
| static ORIG_CREATE_KEY_EX_W: AtomicUsize = AtomicUsize::new(0); | ||
| static ORIG_CLOSE_KEY: AtomicUsize = AtomicUsize::new(0); | ||
|
|
@@ -101,6 +105,7 @@ static ORIG_GET_VALUE_W: AtomicUsize = AtomicUsize::new(0); | |
| static ORIG_QUERY_INFO_KEY_W: AtomicUsize = AtomicUsize::new(0); | ||
| static ORIG_ENUM_VALUE_W: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| static mut ORIG_OPEN_KEY_EX_A_PTR: *const () = ptr::null(); | ||
| static mut ORIG_OPEN_KEY_EX_W_PTR: *const () = ptr::null(); | ||
| static mut ORIG_CREATE_KEY_EX_W_PTR: *const () = ptr::null(); | ||
| static mut ORIG_CLOSE_KEY_PTR: *const () = ptr::null(); | ||
|
|
@@ -198,6 +203,11 @@ pub fn push_key(root: usize, path: &str, values: Vec<RegValue>) { | |
| fn install_hooks(api: &Api) { | ||
| unsafe { | ||
| let symbols = [ | ||
| HookSymbol { | ||
| name: "RegOpenKeyExA", | ||
| patch: hooked_reg_open_key_ex_a as *const (), | ||
| original: ptr::addr_of_mut!(ORIG_OPEN_KEY_EX_A_PTR), | ||
| }, | ||
|
Comment on lines
+206
to
+210
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 中 · 部分安装钩子时返回错误的值 当调用方的 RegOpenKeyExA IAT 条目已被修补,但 RegQueryValueExA 条目未被修补时, 请将打开和查询钩子视为必须成对安装的钩子:对于查询条目无法修补的调用方,应回滚修补,或禁用虚拟键打开功能。 提供给 AI 代理的提示在 `packages/applechu/src/platform/reg_hook.rs` 第 206–210 行:
**部分安装钩子时返回错误的值**
当调用方的 RegOpenKeyExA IAT 条目已被修补,但 RegQueryValueExA 条目未被修补时,`hooked_reg_open_key_ex_a` 会返回一个映射句柄,该句柄由真实的 SOFTWARE 键提供支持;但调用方原生的 `RegQueryValueExA` 会绕过 `hooked_reg_query_value_ex_a`,转而读取真实注册表,而不是虚拟的 OSVersion。
请将打开和查询钩子视为必须成对安装的钩子:对于查询条目无法修补的调用方,应回滚修补,或禁用虚拟键打开功能。Original comment in English🟡 Medium · Partial hook returns wrong values When the caller's RegOpenKeyExA IAT entry is patched but its RegQueryValueExA entry is not, Treat the open and query hooks as a required pair: roll back or disable virtual opens for callers whose query entry could not be patched. Prompt for AI agentsIn `packages/applechu/src/platform/reg_hook.rs` at lines 206-210:
**Partial hook returns wrong values**
When the caller's RegOpenKeyExA IAT entry is patched but its RegQueryValueExA entry is not, `hooked_reg_open_key_ex_a` returns a mapped handle backed by the real SOFTWARE key, but the caller's native `RegQueryValueExA` bypasses `hooked_reg_query_value_ex_a` and reads the real registry instead of virtual OSVersion.
Treat the open and query hooks as a required pair: roll back or disable virtual opens for callers whose query entry could not be patched. |
||
| HookSymbol { | ||
| name: "RegOpenKeyExW", | ||
| patch: hooked_reg_open_key_ex_w as *const (), | ||
|
|
@@ -256,6 +266,7 @@ fn install_hooks(api: &Api) { | |
| fn sync_originals() { | ||
| // SAFETY: 原函数槽由 hook 安装过程写入,随后通过原子变量发布给并发调用者。 | ||
| unsafe { | ||
| ORIG_OPEN_KEY_EX_A.store(ORIG_OPEN_KEY_EX_A_PTR as usize, Ordering::SeqCst); | ||
| ORIG_OPEN_KEY_EX_W.store(ORIG_OPEN_KEY_EX_W_PTR as usize, Ordering::SeqCst); | ||
| ORIG_CREATE_KEY_EX_W.store(ORIG_CREATE_KEY_EX_W_PTR as usize, Ordering::SeqCst); | ||
| ORIG_CLOSE_KEY.store(ORIG_CLOSE_KEY_PTR as usize, Ordering::SeqCst); | ||
|
|
@@ -268,6 +279,37 @@ fn sync_originals() { | |
| } | ||
| } | ||
|
|
||
| unsafe extern "system" fn hooked_reg_open_key_ex_a( | ||
| parent: usize, | ||
| subkey: *const c_char, | ||
| options: u32, | ||
| access: u32, | ||
| out: *mut usize, | ||
| ) -> u32 { | ||
| if out.is_null() { | ||
| return ERROR_INVALID_PARAMETER; | ||
| } | ||
| let name = winapi::cstr_to_string(subkey).unwrap_or_default(); | ||
| if let Some(key_id) = resolve_named_key_id(parent, &name) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 中 · 嵌套打开操作无法找到虚拟键 当调用方先打开 请跟踪由 A 钩子打开的普通句柄对应的注册表路径,并用它解析虚拟后代键;或者以其他方式支持从真实的祖先句柄解析虚拟路径。 提供给 AI 代理的提示在 `packages/applechu/src/platform/reg_hook.rs` 第 293 行:
**嵌套打开操作无法找到虚拟键**
当调用方先打开 `HKLM\SYSTEM`,然后相对于返回的句柄打开 `SEGA` 或更深层的虚拟子键时,`resolve_named_key_id` 无法从 `HKLM\SYSTEM` 返回的真实句柄解析 `SEGA`,而 `key_for_handle` 中也没有该句柄的条目。钩子会回退到真实注册表,因此,逐级打开虚拟路径的调用方无法访问该虚拟路径。
请跟踪由 A 钩子打开的普通句柄对应的注册表路径,并用它解析虚拟后代键;或者以其他方式支持从真实的祖先句柄解析虚拟路径。Original comment in English🟡 Medium · Nested opens miss virtual keys When a caller opens Track the registry path for ordinary handles opened by the A hook and use it to resolve virtual descendants, or otherwise support resolving virtual paths from real ancestor handles. Prompt for AI agentsIn `packages/applechu/src/platform/reg_hook.rs` at line 293:
**Nested opens miss virtual keys**
When a caller opens `HKLM\SYSTEM` first and then opens `SEGA` or a deeper virtual subkey relative to that returned handle, `resolve_named_key_id` cannot resolve `SEGA` from the real handle returned for `HKLM\SYSTEM`, and `key_for_handle` has no entry for that handle. The hook falls through to the real registry, so callers opening the virtual path one component at a time fail to reach it.
Track the registry path for ordinary handles opened by the A hook and use it to resolve virtual descendants, or otherwise support resolving virtual paths from real ancestor handles. |
||
| return open_virtual_key(key_id, out); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 中 · 错误地授予了写入权限 当 ANSI 调用方请求写入权限来打开虚拟键时, 请验证请求的访问权限;如果虚拟键无法满足这些权限,请返回适当的错误。 另请参见 提供给 AI 代理的提示在 `packages/applechu/src/platform/reg_hook.rs` 第 294 行:
**错误地授予了写入权限**
当 ANSI 调用方请求写入权限来打开虚拟键时,`hooked_reg_open_key_ex_a` 会忽略请求的 `access`,而 `open_virtual_key` 会以 `KEY_READ` 权限打开后备键;因此,请求写入权限的调用方虽然收到成功结果,却无法执行该成功结果所承诺允许的写入操作。
请验证请求的访问权限;如果虚拟键无法满足这些权限,请返回适当的错误。
另请参见 `packages/applechu/src/platform/reg_hook.rs:539`。Original comment in English🟡 Medium · Write access is falsely granted When an ANSI caller opens a virtual key requesting write access, Validate the requested access rights and return an appropriate error when the virtual key cannot honor them. Also at Prompt for AI agentsIn `packages/applechu/src/platform/reg_hook.rs` at line 294:
**Write access is falsely granted**
When an ANSI caller opens a virtual key requesting write access, `hooked_reg_open_key_ex_a` ignores the requested `access` and `open_virtual_key` opens the backing key with `KEY_READ`; callers requesting write access therefore receive success but cannot perform the writes that success promises.
Validate the requested access rights and return an appropriate error when the virtual key cannot honor them.
Also at `packages/applechu/src/platform/reg_hook.rs:539`. |
||
| } | ||
| if key_for_handle(parent).is_some() { | ||
| return winapi::ERROR_FILE_NOT_FOUND; | ||
|
Comment on lines
+296
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 中 · ANSI 子键无法继续解析 当游戏打开一个虚拟键,随后请求一个未在 对于无法解析的子键,请保留原始注册表查询行为,并使用一个以该虚拟键实际注册表路径为根的后备句柄。 提供给 AI 代理的提示在 `packages/applechu/src/platform/reg_hook.rs` 第 296–297 行:
**ANSI 子键无法继续解析**
当游戏打开一个虚拟键,随后请求一个未在 `KEYS` 中注册的子键时,只要 `key_for_handle(parent)` 识别出该父键是虚拟键,钩子就会返回 `ERROR_FILE_NOT_FOUND`,而不会尝试调用原始的 `RegOpenKeyExA`;因此,调用方无法打开该句柄下其他本来有效的子键。
对于无法解析的子键,请保留原始注册表查询行为,并使用一个以该虚拟键实际注册表路径为根的后备句柄。Original comment in English🟡 Medium · ANSI child keys stop resolving When a game opens a virtual key and then requests a child key not registered in Preserve the original registry lookup for unresolved child keys, using a backing handle rooted at the virtual key’s actual registry path. Prompt for AI agentsIn `packages/applechu/src/platform/reg_hook.rs` at lines 296-297:
**ANSI child keys stop resolving**
When a game opens a virtual key and then requests a child key not registered in `KEYS`, the hook returns `ERROR_FILE_NOT_FOUND` as soon as `key_for_handle(parent)` recognizes a virtual parent, without trying the original `RegOpenKeyExA`; callers therefore cannot open an otherwise valid child key beneath that handle.
Preserve the original registry lookup for unresolved child keys, using a backing handle rooted at the virtual key’s actual registry path. |
||
| } | ||
| let original_addr = ORIG_OPEN_KEY_EX_A.load(Ordering::SeqCst); | ||
| if original_addr == 0 { | ||
| return RegOpenKeyExA( | ||
| handle_from_value(parent), | ||
| subkey.cast(), | ||
| options, | ||
| access, | ||
| out.cast(), | ||
| ); | ||
| } | ||
| let original: RegOpenKeyExAFn = std::mem::transmute(original_addr); | ||
| original(parent, subkey, options, access, out) | ||
| } | ||
|
|
||
| unsafe extern "system" fn hooked_reg_open_key_ex_w( | ||
| parent: usize, | ||
| subkey: *const u16, | ||
|
|
@@ -492,19 +534,20 @@ unsafe extern "system" fn hooked_reg_enum_value_w( | |
| } | ||
|
|
||
| unsafe fn open_virtual_key(key_id: usize, out: *mut usize) -> u32 { | ||
| let mut token = 0usize; | ||
| let result = original_open_key_ex_w( | ||
| HKEY_LOCAL_MACHINE, | ||
| // Some games import only RegOpenKeyExA. Open the backing handle independently of the W hook. | ||
| let mut token: HKEY = ptr::null_mut(); | ||
| let result = RegOpenKeyExW( | ||
| handle_from_value(HKEY_LOCAL_MACHINE), | ||
| SOFTWARE_KEY.as_ptr(), | ||
| 0, | ||
| KEY_READ, | ||
| &mut token, | ||
| ); | ||
| if result == winapi::ERROR_SUCCESS { | ||
| if let Ok(mut handles) = HANDLES.lock() { | ||
| handles.insert(token, key_id); | ||
| handles.insert(token.addr(), key_id); | ||
| } | ||
| *out = token; | ||
| *out = token.addr(); | ||
| } | ||
| result | ||
| } | ||
|
|
@@ -582,7 +625,11 @@ unsafe fn write_raw_value( | |
|
|
||
| unsafe fn resolve_key_id(parent: usize, subkey: *const u16) -> Option<usize> { | ||
| let subkey = winapi::wide_to_string(subkey).unwrap_or_default(); | ||
| let (root, path) = resolve_candidate(parent, &subkey)?; | ||
| resolve_named_key_id(parent, &subkey) | ||
| } | ||
|
|
||
| fn resolve_named_key_id(parent: usize, subkey: &str) -> Option<usize> { | ||
| let (root, path) = resolve_candidate(parent, subkey)?; | ||
| KEYS.lock() | ||
| .ok()? | ||
| .iter() | ||
|
|
@@ -697,7 +744,7 @@ unsafe fn original_create_key_ex_w( | |
| unsafe fn original_close_key(handle: usize) -> u32 { | ||
| let original_addr = ORIG_CLOSE_KEY.load(Ordering::SeqCst); | ||
| if original_addr == 0 { | ||
| return winapi::ERROR_SUCCESS; | ||
| return RegCloseKey(handle_from_value(handle)); | ||
| } | ||
| let original: RegCloseKeyFn = std::mem::transmute(original_addr); | ||
| original(handle) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 中 · 虚拟键在启动期间无法使用
如果其他游戏线程在钩子安装后、但在
init注册虚拟键之前调用RegOpenKeyExA,hooked_reg_open_key_ex_a将无法找到该虚拟键,并会将请求转发给真实注册表;真实注册表会返回错误,因此调用方无法打开该键。请在安装钩子之前注册虚拟键,或者采取其他措施,确保注册完成前不会有调用进入该钩子。
另请参见
packages/applechu/src/platform/reg_hook.rs:207。提供给 AI 代理的提示
Original comment in English
🟡 Medium · Virtual keys fail during startup
When another game thread calls
RegOpenKeyExAafter the hook is installed but beforeinitregisters the virtual keys,hooked_reg_open_key_ex_acannot find the virtual key and forwards the request to the real registry, which returns an error, so the caller's open fails.Register the virtual keys before installing the hook, or otherwise prevent calls from reaching it until registration completes.
Also at
packages/applechu/src/platform/reg_hook.rs:207.Prompt for AI agents