I wanted to build some sort of API monitor by hooking all ntdll functions using Detours
API. Each hooked function will call the original implementation and than
add notice for this call inside std base data structure.
However, I encountered a scenario of recursive hooking where my hooked function is indirectly calling itself.
Therefore, I tried to use the Tls memory to set a bit per thread that indicate that current function is called from hooked function, so avoid calling the hook again (execute the original function only).
But my recursive hook guard also uses some calls for memory allocation function like NtAllocateVirtualMemory
, and therefore I'm currently avoid hooking those functions.
Perhaps anybody has encountered a similar issue and implemented hook reentrancy guard in a way that doesn't triggered any memory allocation function (which might be impossible since even if you call a new function and your stack memory is insufficient, it should allocate more memory).
thanks