You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/TechWiki/Update Threads Automatically When Applying Inline Hooks/README.md
+9-184Lines changed: 9 additions & 184 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,20 +19,20 @@ LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread);
19
19
```
20
20
In other words, the caller needs to traverse all threads in the process except itself and pass them to this function, which is complicated and inconvenient to use.
21
21
22
-
But [Detours](https://github.com/microsoft/Detours) updates threads very precisely, it accurately adjusts the PC (Program Counter) in the thread context to the correct position by using [`GetThreadContext`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadcontext) and [`SetThreadContext`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadcontext), see [Detours/src/detours.cpp at 4b8c659f · microsoft/Detours](https://github.com/microsoft/Detours/blob/4b8c659f549b0ab21cf649377c7a84eb708f5e68/src/detours.cpp#L1840) for implementation.
22
+
[Detours](https://github.com/microsoft/Detours) updates threads very precisely, it accurately adjusts the PC (Program Counter) in the thread context to the correct position by using [`GetThreadContext`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadcontext) and [`SetThreadContext`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadcontext), see [Detours/src/detours.cpp at 4b8c659f · microsoft/Detours](https://github.com/microsoft/Detours/blob/4b8c659f549b0ab21cf649377c7a84eb708f5e68/src/detours.cpp#L1840-L1906) for implementation.
23
+
24
+
But [Detours](https://github.com/microsoft/Detours) still has inadequacy in thread updating under x64, see also [PR #344: Improve thread program counter adjustment](https://github.com/microsoft/Detours/pull/344) submitted by me for this.
23
25
24
26
> [!TIP]
25
27
> While its official example "[Using Detours](https://github.com/microsoft/Detours/wiki/Using-Detours)" has code like `DetourUpdateThread(GetCurrentThread())`, such usage is pointless and invalid, and should be used to update all threads in the process except the current thread, see also: [`DetourUpdateThread`](https://github.com/microsoft/Detours/wiki/DetourUpdateThread). But even updating threads in the right way, it also brings a new risk, see [🔗 TechWiki: Avoid Deadlocking on The Heap When Updating Threads](https://github.com/KNSoft/KNSoft.SlimDetours/blob/main/Docs/TechWiki/Avoid%20Deadlocking%20on%20The%20Heap%20When%20Updating%20Threads/README.md).
26
28
27
29
### MinHook
28
30
29
-
[MinHook](https://github.com/TsudaKageyu/minhook) does a better job, it updates threads automatically when set (or unset) hooks, and adjusts PC (Program Counter) in the thread context as accurately as [Detours](https://github.com/microsoft/Detours).
31
+
[MinHook](https://github.com/TsudaKageyu/minhook) does a better job, it calls [CreateToolhelp32Snapshot](https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot) to obtain other threads and updates them automatically when set (or unset) hooks, then adjusts PC (Program Counter) in the thread context as accurately as [Detours](https://github.com/microsoft/Detours).
30
32
31
33
### mhook
32
34
33
-
[mhook](https://github.com/martona/mhook) updates threads automatically when set (or unset) hooks, see [mhook/mhook-lib/mhook.cpp at e58a58ca · martona/mhook](https://github.com/martona/mhook/blob/e58a58ca31dbe14f202b9b26315bff9f7a32598c/mhook-lib/mhook.cpp#L557) for implementation.
34
-
35
-
But the way it updates threads is a bit hacky compared to the others mentioned above, wait 100ms if the thread is exactly in the area where the instruction is about to be modified, try up to 3 times:
35
+
[mhook](https://github.com/martona/mhook) calls [NtQuerySystemInformation](https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntquerysysteminformation) to obtain other threads and updates them automatically when set (or unset) hooks. But the way to update threads is more hacky, wait 100ms if the thread is exactly in the area where the instruction is about to be modified, try up to 3 times, see [mhook/mhook-lib/mhook.cpp at e58a58ca · martona/mhook](https://github.com/martona/mhook/blob/e58a58ca31dbe14f202b9b26315bff9f7a32598c/mhook-lib/mhook.cpp#L557-L631) for implementation:
36
36
```C
37
37
while (GetThreadContext(hThread, &ctx))
38
38
{
@@ -50,189 +50,14 @@ while (GetThreadContext(hThread, &ctx))
50
50
}
51
51
```
52
52
53
-
## SlimDetours implementation
54
-
55
-
[SlimDetours](https://github.com/KNSoft/KNSoft.SlimDetours) takes all of the above advantages into account, traverse all threads of the process at hook (or unhook) time, and then update the thread context in the same way as [Detours](https://github.com/microsoft/Detours).
56
-
57
-
Suspend all threads in the current process except the current thread and return their handles:
[SlimDetours](https://github.com/KNSoft/KNSoft.SlimDetours) has two methods to obtain other threads, calls [NtQuerySystemInformation](https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntquerysysteminformation) when targeting NT5, but use `NtGetNextThread` when targeting NT6+ (default) to significantly improve performance and correctness guarantees.
0 commit comments