There are many ways to hook Windows APIs. Inline-hook is one of them. That is, to replace the first 5 bytes of the function with a long jump instruction. There are talks and implementations, to ensure the thread safety of the program, generally they will suspend all the threads while patching. That is, do something like this:
threads = get_threads();
for (thread in threads) {
suspend(thread);
}
I am very confused, I don't think it's safe. Because threads can be started or stopped on the fly, while you are in the looping, things changed. Isn't it (Q1) ?
Since Windows SP2, Windows APIs are hot-patchable. You can also enable hotpatching on your own image by passing the /hotpatch flag(since VS2008). That is, the compiler ensures that first instruction of a function is at least two bytes. And put 5 or 6 bytes padding before the function. The padding bytes can be replaced with a long jump instruction to redirect to the new version of this function, and the two bytes instruction can be replaced with a short jump instruction to redirect to the long jump instruction before it.
Q2: It seems that I don't needed to suspend all the threads while replacing the first two bytes instruction (Although I don't know how to suspend all the threads at all) of a hot-patchable function, is it ?
Q3: Do I need any atomic operation or memory fence or volatile or something to ensure the replacing is thread-safe ? If so, does compiler ensure the address of each function is aligned ?