Skip to content

Commit 4d3c451

Browse files
committed
Fix an issue with removing hooks that could lead to hooked functions being called without a proper original function to forward to.
1 parent cd15b23 commit 4d3c451

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/hooks.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
#include <unordered_map>
77

88
namespace {
9-
std::unordered_map<intptr_t, intptr_t> g_hooksToOriginal;
9+
struct HookInfo {
10+
intptr_t target;
11+
intptr_t original;
12+
intptr_t hook;
13+
};
14+
std::unordered_map<intptr_t, HookInfo> g_hooksToOriginal;
1015
}
1116

1217
namespace vrperfkit {
@@ -39,15 +44,24 @@ namespace vrperfkit {
3944
return;
4045
}
4146

42-
g_hooksToOriginal[reinterpret_cast<intptr_t>(detour)] = reinterpret_cast<intptr_t>(pOriginal);
47+
g_hooksToOriginal[reinterpret_cast<intptr_t>(detour)] = HookInfo {
48+
reinterpret_cast<intptr_t>(pTarget),
49+
reinterpret_cast<intptr_t>(pOriginal),
50+
reinterpret_cast<intptr_t>(detour),
51+
};
4352
}
4453

4554
void RemoveHook(void *detour) {
4655
auto entry = g_hooksToOriginal.find(reinterpret_cast<intptr_t>(detour));
4756
if (entry != g_hooksToOriginal.end()) {
48-
void *target = reinterpret_cast<void *>(entry->second);
49-
MH_DisableHook(target);
50-
MH_RemoveHook(target);
57+
void *target = reinterpret_cast<void *>(entry->second.target);
58+
LOG_INFO << "Removing hook to " << target;
59+
if (MH_STATUS status; (status = MH_DisableHook(target)) != MH_OK) {
60+
LOG_ERROR << "Error when disabling hook to " << target << ": " << status;
61+
}
62+
if (MH_STATUS status; (status = MH_RemoveHook(target)) != MH_OK) {
63+
LOG_ERROR << "Error when removing hook to " << target << ": " << status;
64+
}
5165
g_hooksToOriginal.erase(entry);
5266
}
5367
}
@@ -60,7 +74,11 @@ namespace vrperfkit {
6074
return;
6175
}
6276

63-
g_hooksToOriginal[reinterpret_cast<intptr_t>(detour)] = reinterpret_cast<intptr_t>(pOriginal);
77+
g_hooksToOriginal[reinterpret_cast<intptr_t>(detour)] = HookInfo {
78+
reinterpret_cast<intptr_t>(target),
79+
reinterpret_cast<intptr_t>(pOriginal),
80+
reinterpret_cast<intptr_t>(detour),
81+
};
6482
}
6583

6684
void InstallHookInDll(const std::string &name, HMODULE module, void *detour) {
@@ -71,7 +89,7 @@ namespace vrperfkit {
7189
}
7290

7391
intptr_t HookToOriginal(intptr_t hook) {
74-
return g_hooksToOriginal[hook];
92+
return g_hooksToOriginal[hook].original;
7593
}
7694
}
7795
}

src/openvr/openvr_hooks.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ namespace vrperfkit {
9494
hooks::RemoveHook((void*)IVRCompositor008Hook_Submit);
9595
hooks::RemoveHook((void*)IVRCompositor007Hook_Submit);
9696
hooks::RemoveHook((void*)IVRSystemHook_GetRecommendedRenderTargetSize);
97+
hooks::RemoveHook((void*)IVRCompositorHook_WaitGetPoses);
98+
hooks::RemoveHook((void*)IVRCompositorHook_PostPresentHandoff);
9799
g_compositorVersion = 0;
98100
g_systemVersion = 0;
99101
}

0 commit comments

Comments
 (0)