Repository navigation
fix: 修复部分游戏启动时的使用 RegOpenKeyExA 读注册表导致退出的错误 - #7
Conversation
- 接管 RegOpenKeyExA 并复用虚拟键逻辑,让现有的RegQueryValueExA Hook 能读到虚拟的 OSVersion。
审查者指南实现拦截 RegOpenKeyExA,并将其接入现有的虚拟键查找和句柄生命周期,使 ANSI 调用方能够访问虚拟注册表值。
提示和命令与 Sourcery 互动
自定义你的使用体验访问你的控制面板,以便:
获取帮助Original review guide in EnglishReviewer's GuideImplementationIntercept RegOpenKeyExA and connect it to the existing virtual-key lookup and handle lifecycle so ANSI callers can access virtual registry values.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
fix this issues #6 |
There was a problem hiding this comment.
你好——我发现了 5 个问题
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="206" />
<code_context>
fn install_hooks(api: &Api) {
unsafe {
let symbols = [
+ HookSymbol {
+ name: "RegOpenKeyExA",
+ patch: hooked_reg_open_key_ex_a as *const (),
</code_context>
<issue_to_address>
**Virtual keys fail during startup**
When another game thread calls `RegOpenKeyExA` after the hook is installed but before `init` registers the virtual keys, `hooked_reg_open_key_ex_a` cannot 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`.
</issue_to_address>
### Comment 2
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="293" />
<code_context>
+ 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) {
+ return open_virtual_key(key_id, out);
+ }
</code_context>
<issue_to_address>
**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.
</issue_to_address>
### Comment 3
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="206-210" />
<code_context>
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),
+ },
HookSymbol {
name: "RegOpenKeyExW",
</code_context>
<issue_to_address>
**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.
</issue_to_address>
### Comment 4
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="294" />
<code_context>
+ }
+ let name = winapi::cstr_to_string(subkey).unwrap_or_default();
+ if let Some(key_id) = resolve_named_key_id(parent, &name) {
+ return open_virtual_key(key_id, out);
+ }
+ if key_for_handle(parent).is_some() {
</code_context>
<issue_to_address>
**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`.
</issue_to_address>
### Comment 5
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="296-297" />
<code_context>
+ if let Some(key_id) = resolve_named_key_id(parent, &name) {
+ return open_virtual_key(key_id, out);
+ }
+ if key_for_handle(parent).is_some() {
+ return winapi::ERROR_FILE_NOT_FOUND;
+ }
+ let original_addr = ORIG_OPEN_KEY_EX_A.load(Ordering::SeqCst);
</code_context>
<issue_to_address>
**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.
</issue_to_address>Sourcery 评估
待批准。 请先处理 5 项发现。
阻塞性发现:packages/applechu/src/platform/reg_hook.rs:206、packages/applechu/src/platform/reg_hook.rs:293、packages/applechu/src/platform/reg_hook.rs:210、packages/applechu/src/platform/reg_hook.rs:294、packages/applechu/src/platform/reg_hook.rs:297
Original comment in English
Hey - I've found 5 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="206" />
<code_context>
fn install_hooks(api: &Api) {
unsafe {
let symbols = [
+ HookSymbol {
+ name: "RegOpenKeyExA",
+ patch: hooked_reg_open_key_ex_a as *const (),
</code_context>
<issue_to_address>
**Virtual keys fail during startup**
When another game thread calls `RegOpenKeyExA` after the hook is installed but before `init` registers the virtual keys, `hooked_reg_open_key_ex_a` cannot 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`.
</issue_to_address>
### Comment 2
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="293" />
<code_context>
+ 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) {
+ return open_virtual_key(key_id, out);
+ }
</code_context>
<issue_to_address>
**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.
</issue_to_address>
### Comment 3
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="206-210" />
<code_context>
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),
+ },
HookSymbol {
name: "RegOpenKeyExW",
</code_context>
<issue_to_address>
**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.
</issue_to_address>
### Comment 4
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="294" />
<code_context>
+ }
+ let name = winapi::cstr_to_string(subkey).unwrap_or_default();
+ if let Some(key_id) = resolve_named_key_id(parent, &name) {
+ return open_virtual_key(key_id, out);
+ }
+ if key_for_handle(parent).is_some() {
</code_context>
<issue_to_address>
**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`.
</issue_to_address>
### Comment 5
<location path="packages/applechu/src/platform/reg_hook.rs" line_range="296-297" />
<code_context>
+ if let Some(key_id) = resolve_named_key_id(parent, &name) {
+ return open_virtual_key(key_id, out);
+ }
+ if key_for_handle(parent).is_some() {
+ return winapi::ERROR_FILE_NOT_FOUND;
+ }
+ let original_addr = ORIG_OPEN_KEY_EX_A.load(Ordering::SeqCst);
</code_context>
<issue_to_address>
**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.
</issue_to_address>Sourcery assessment
Approval pending. 5 findings to address first.
Blocking findings: packages/applechu/src/platform/reg_hook.rs:206, packages/applechu/src/platform/reg_hook.rs:293, packages/applechu/src/platform/reg_hook.rs:210, packages/applechu/src/platform/reg_hook.rs:294, packages/applechu/src/platform/reg_hook.rs:297
| fn install_hooks(api: &Api) { | ||
| unsafe { | ||
| let symbols = [ | ||
| HookSymbol { |
There was a problem hiding this comment.
🟡 中 · 虚拟键在启动期间无法使用
如果其他游戏线程在钩子安装后、但在 init 注册虚拟键之前调用 RegOpenKeyExA,hooked_reg_open_key_ex_a 将无法找到该虚拟键,并会将请求转发给真实注册表;真实注册表会返回错误,因此调用方无法打开该键。
请在安装钩子之前注册虚拟键,或者采取其他措施,确保注册完成前不会有调用进入该钩子。
另请参见 packages/applechu/src/platform/reg_hook.rs:207。
提供给 AI 代理的提示
在 `packages/applechu/src/platform/reg_hook.rs` 第 206 行:
**虚拟键在启动期间无法使用**
如果其他游戏线程在钩子安装后、但在 `init` 注册虚拟键之前调用 `RegOpenKeyExA`,`hooked_reg_open_key_ex_a` 将无法找到该虚拟键,并会将请求转发给真实注册表;真实注册表会返回错误,因此调用方无法打开该键。
请在安装钩子之前注册虚拟键,或者采取其他措施,确保注册完成前不会有调用进入该钩子。
另请参见 `packages/applechu/src/platform/reg_hook.rs:207`。Original comment in English
🟡 Medium · Virtual keys fail during startup
When another game thread calls RegOpenKeyExA after the hook is installed but before init registers the virtual keys, hooked_reg_open_key_ex_a cannot 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
In `packages/applechu/src/platform/reg_hook.rs` at line 206:
**Virtual keys fail during startup**
When another game thread calls `RegOpenKeyExA` after the hook is installed but before `init` registers the virtual keys, `hooked_reg_open_key_ex_a` cannot 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`.| 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.
🟡 中 · 嵌套打开操作无法找到虚拟键
当调用方先打开 HKLM\SYSTEM,然后相对于返回的句柄打开 SEGA 或更深层的虚拟子键时,resolve_named_key_id 无法从 HKLM\SYSTEM 返回的真实句柄解析 SEGA,而 key_for_handle 中也没有该句柄的条目。钩子会回退到真实注册表,因此,逐级打开虚拟路径的调用方无法访问该虚拟路径。
请跟踪由 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 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.
Prompt for AI agents
In `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.| HookSymbol { | ||
| name: "RegOpenKeyExA", | ||
| patch: hooked_reg_open_key_ex_a as *const (), | ||
| original: ptr::addr_of_mut!(ORIG_OPEN_KEY_EX_A_PTR), | ||
| }, |
There was a problem hiding this comment.
🟡 中 · 部分安装钩子时返回错误的值
当调用方的 RegOpenKeyExA IAT 条目已被修补,但 RegQueryValueExA 条目未被修补时,hooked_reg_open_key_ex_a 会返回一个映射句柄,该句柄由真实的 SOFTWARE 键提供支持;但调用方原生的 RegQueryValueExA 会绕过 hooked_reg_query_value_ex_a,转而读取真实注册表,而不是虚拟的 OSVersion。
请将打开和查询钩子视为必须成对安装的钩子:对于查询条目无法修补的调用方,应回滚修补,或禁用虚拟键打开功能。
提供给 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, 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.
Prompt for AI agents
In `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.| } | ||
| let name = winapi::cstr_to_string(subkey).unwrap_or_default(); | ||
| if let Some(key_id) = resolve_named_key_id(parent, &name) { | ||
| return open_virtual_key(key_id, out); |
There was a problem hiding this comment.
🟡 中 · 错误地授予了写入权限
当 ANSI 调用方请求写入权限来打开虚拟键时,hooked_reg_open_key_ex_a 会忽略请求的 access,而 open_virtual_key 会以 KEY_READ 权限打开后备键;因此,请求写入权限的调用方虽然收到成功结果,却无法执行该成功结果所承诺允许的写入操作。
请验证请求的访问权限;如果虚拟键无法满足这些权限,请返回适当的错误。
另请参见 packages/applechu/src/platform/reg_hook.rs:539。
提供给 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, 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.
Prompt for AI agents
In `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; |
There was a problem hiding this comment.
🟡 中 · ANSI 子键无法继续解析
当游戏打开一个虚拟键,随后请求一个未在 KEYS 中注册的子键时,只要 key_for_handle(parent) 识别出该父键是虚拟键,钩子就会返回 ERROR_FILE_NOT_FOUND,而不会尝试调用原始的 RegOpenKeyExA;因此,调用方无法打开该句柄下其他本来有效的子键。
对于无法解析的子键,请保留原始注册表查询行为,并使用一个以该虚拟键实际注册表路径为根的后备句柄。
提供给 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 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.
Prompt for AI agents
In `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.
Sourcery 总结
在虚拟注册表层支持以 ANSI 编码打开注册表项,以便现有的操作系统版本虚拟化功能适用于更多游戏。
错误修复:
增强功能:
Original summary in English
Summary by Sourcery
Support ANSI registry key opening in the virtual registry layer so existing OS version virtualization works across more games.
Bug Fixes:
Enhancements: