Fix stale removal comments (persistent, not destroy) + add deterministic install test

- The remove_* comments still described the superseded "disable -> drain -> destroy"
  flow; the code keeps hooks alive (persistent) and re-enables on re-install. Updated
  the comments to match, and corrected the XInput note (its detours return synthesized
  state and never call the trampoline, so destroying its vector is safe -- unlike the
  trampoline-calling present/MKB/focus-cursor hooks).
- hook_install_test: a fast, single-threaded contract test for hook_install.hpp --
  install_inline creates the hook once and reuses the SAME trampoline across 50
  install/remove cycles (never freed -> no stale-detour UAF), toggling enable/disable
  cleanly. Fills the guard the removed (flaky, concurrency-bound) reproducer left, with
  no threads so it can't flake on SafetyHook's enable/disable atomicity.

x64 23/23, x86 3/3.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-23 11:51:48 +02:00
parent 8a183902ad
commit f843c56f5b
8 changed files with 139 additions and 26 deletions

View File

@@ -45,6 +45,14 @@ add_executable(detour_gate_test detour_gate_test.cpp)
target_include_directories(detour_gate_test PRIVATE ${CMAKE_SOURCE_DIR}/hook/src)
add_test(NAME detour_gate_test COMMAND detour_gate_test)
# Deterministic contract test for the persistent inline-hook model (hook/src/hook_install.hpp):
# install_inline creates a hook once and re-enables it on re-install, reusing the trampoline (never
# freed) so a stale detour can't UAF. Single-threaded, so it can't flake on enable/disable atomicity.
add_executable(hook_install_test hook_install_test.cpp)
target_include_directories(hook_install_test PRIVATE ${CMAKE_SOURCE_DIR}/hook/src)
target_link_libraries(hook_install_test PRIVATE safetyhook::safetyhook)
add_test(NAME hook_install_test COMMAND hook_install_test)
# Unit test for the MKB event ring (SPSC push/pop, wrap-around, full/empty).
add_executable(mkb_ring_test mkb_ring_test.cpp)
target_link_libraries(mkb_ring_test PRIVATE coop_common)
@@ -284,6 +292,7 @@ coop_output_subdir(tests
dinput_hook_test
audio_ring_test
detour_gate_test
hook_install_test
mkb_ring_test
mkb_map_test
audio_mix_test