-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fix if the hook is not called at the start of the process, the hook may fail #144
base: main
Are you sure you want to change the base?
Conversation
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.
Thanks for the contribution!
It looks like you might have mistakenly included all of the other commits from your other PRs here?
Can you pair this down to only the issue you describe in your pull request description?
b492481
to
c029e71
Compare
@bgianfo, I have rebased to only include this commit. |
…ent operations in Detours, because heap memory API functions may be HOOKed. For example, in AcLayers.dll, there may be lock competition between HOOK heap functions. This is only a temporary circumvention solution, the final solution It should also be ensured that the virtual memory API is not locked and can be called.
… Over-bound-access memory errors.
0f46187
to
5dbb4c8
Compare
5dbb4c8
to
2da209b
Compare
4f64a5d
to
9d41534
Compare
Hey @sonyps5201314 thanks again for the contribution. @dtarditi and I looked at this togeather today, and we decided the change as it is, is very risk to merge since it's so large and invasive. Can you please desribe what the actual race condition / bug this is fixing is so we can work on trying to scope down this change? It's not clear from the description or the commits where the bug you are fixing actually. Is it also possible to introduce a stress test / unit test to validate we have fix the issue and don't regress it in the future? |
…e_starttime_of_a_process
@bgianfo Compile, debug and run the Note: It is recommended to use VS2013 to compile and debug the program of this project, because VS2019 and VS2017 have the bug that the call stack of the program cannot be viewed when the x86 program crashes or deadlocks. It has been reported for almost a year, and the official hasn't repaired it yet. |
…e_starttime_of_a_process
…_not_at_the_starttime_of_a_process
The original commit seems to mostly be related to #70 (though covers a broader problem): any new/delete/malloc/free etc after any In windows, there is a per-heap lock, which new/delete/malloc/free always need to acquire, and in most applications, practically all operations will be on the same default heap - in effect, roughly speaking, there's a per-process lock on heap operations. See https://docs.microsoft.com/en-us/windows/win32/memory/heap-functions
You can reproduce this by:
-- There are also several other bugs fixed in later commits; I'm not came across them in practice, so I'm not able to explain them. -- As for how this relates to 'at the start of the process':
|
Here is a small standalone example: https://gist.github.com/fredemmott/adb9c41d7cff44aecf92bece990f4c3f The explicit lock is just to make it a reliable repro; you can reproduce it less artificially (and less reliably) by replacing the locking threadproc with: while (true) {
delete new int;
} You can replace 'int' with any type. |
An alternative fix for that first issue would be to simply call |
The latest committed code makes Detours use VirtualAlloc/VirtualFree for memory allocation and deallocation, so there is no Heap lock problem you worry about. // After calling DetourUpdateAllOtherThreads, you should call DetourTransactionCommit(Ex) or DetourTransactionAbort as soon as possible.
// In addition to the DetourAttach(Ex)\DetourDetach(Ex) call, other user operations should not be included between them,
// because other user operations may cause CRT lock competition, and at this time CRT lock may be owned by other threads.
// Other threads have been suspended at this time, so that may cause deadlock problems
LONG WINAPI DetourUpdateThreadEx(_In_ HANDLE hThread, _In_opt_ BOOL fCloseThreadHandleOnDestroyDetourThreadObject /*= TRUE*/);
BOOL WINAPI DetourUpdateAllOtherThreads(); and you can read the commented text above for DetourUpdateThreadEx and DetourUpdateAllOtherThreads to understand the precautions for using them. int __cdecl _heap_init (
int mtflag
)
{
#if defined _M_AMD64 || defined _M_IA64
// HEAP_NO_SERIALIZE is incompatible with the LFH heap
mtflag = 1;
#endif /* defined _M_AMD64 || defined _M_IA64 */
// Initialize the "big-block" heap first.
if ( (_crtheap = HeapCreate( mtflag ? 0 : HEAP_NO_SERIALIZE,
BYTES_PER_PAGE, 0 )) == NULL )
return 0; you can know that |
This can generally be avoided by calling
I don't disagree on this, but it is not necessary for many cases.
I'm looking for minimal - and understandable - changes on top of master. This PR fixes several issues (and I understand this is more than the heaplock race), and adds new features. Regardless of reliability for you, that makes it not fit my goals. Splitting it up with clear explanations would also likely aid Microsoft in reviewing and ideally merging. That said, I want to be clear that I appreciate your insight and efforts for the community :) |
…e_starttime_of_a_process
warning C6553: The annotation for function 'DetourTransactionBeginEx' on _Param_(1) does not apply to a value type. warning C6553: The annotation for function 'DetourUpdateThreadEx' on _Param_(2) does not apply to a value type. Flagged By: VS 17.2.6 (CL.exe 14.32.31332.0)
…e_starttime_of_a_process
for example, create a c++ console project, create many threads to call CreateFileW,ReadFile,CloseHandle
and create serveral threads to execute Hook and Unhook CreateFileW and ReadFile function every 100 milliseconds.
it will be failed after some time.