Is there a way to hook malloc from within the process without risking a race condition? #229
Unanswered
simonvanbernem
asked this question in
Q&A
Replies: 1 comment 2 replies
-
If you're creating the hook in DllMain, this isn't an issue because the loader lock prevents any thread from starting up until your DllMain function completes. This is the safest place to create a hook in. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have an application where I want to hook malloc to do more specialized error checking of allocations than the c-runtime hooks allow for. Detours seems to fit perfectly, and I got it working nicely, but I think there is a race condition looming:
I currently only call DetourUpdateThread on the current thread (which doesn't even seem to be necessary?). But there are other threads running that I haven't created and don't control, which might be calling malloc right when I commit the detours transaction, which could lead to bad things happening, if I understand this correctly.
One solution for discovering all threads in the process (to then call DetourUpdateThread on each) I have seen uses CreateToolhelp32Snapshot, and then iterates through all threads and checks if they belong to the current process. This is "probably fine" I guess, but between iterating the list of threads and committing the transaction, new threads could have spawned in my process, right? And there is no way to tell windows "don't let any new threads be created until I'm done here" right? If those threads somehow made it into malloc, we are back to square one.
That leaves me with the question: Is there a way to hook malloc using detours that 100% guarantees that nothing bad will happen, not relying on hopefully no other (possibly newly created) thread being in malloc right when the transaction is commited?
Beta Was this translation helpful? Give feedback.
All reactions