Skip to content

Instantly share code, notes, and snippets.

@fredemmott
Last active January 16, 2022 17:54
Show Gist options
  • Save fredemmott/adb9c41d7cff44aecf92bece990f4c3f to your computer and use it in GitHub Desktop.
Save fredemmott/adb9c41d7cff44aecf92bece990f4c3f to your computer and use it in GitHub Desktop.
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <detours.h>
#include <mutex>
std::mutex gStartupMutex;
bool gHeapLocked = false;
DWORD WINAPI HeapLockingThreadProc(LPVOID ignored) {
std::unique_lock lock(gStartupMutex);
HeapLock(GetProcessHeap());
gHeapLocked = true;
while(true) {
Sleep(1000);
}
}
DWORD WINAPI SleepingThreadProc(LPVOID ignored) {
while(true) {
Sleep(1000);
}
}
int main() {
gStartupMutex.lock();
auto getsLock = CreateThread(nullptr, NULL, &HeapLockingThreadProc, nullptr, NULL, nullptr);
auto sleeps = CreateThread(nullptr, NULL, &SleepingThreadProc, nullptr, NULL, nullptr);
gStartupMutex.unlock();
// spinlock to avoid going anywhere near the heap
while (!gHeapLocked) {
YieldProcessor();
}
DetourTransactionBegin();
DetourUpdateThread(getsLock);
DetourUpdateThread(sleeps);
DetourTransactionCommit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment