Skip to content
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: Eliminate possible deadlock related to memory allocation on global heap #232

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,12 @@ _Benign_race_end_
s_pPendingThreads = NULL;
s_ppPendingError = NULL;

if (!HeapLock(GetProcessHeap())) {
s_nPendingError = GetLastError();

return s_nPendingError;
}

// Make sure the trampoline pages are writable.
s_nPendingError = detour_writable_trampoline_regions();

Expand Down Expand Up @@ -1645,10 +1651,19 @@ LONG WINAPI DetourTransactionAbort()
delete t;
t = n;
}

// s_nPendingError should not be overwritten as DetourTransactionCommitEx relies on it.
LONG nReportedError = NO_ERROR;

if (!HeapUnlock(GetProcessHeap())) {
// This may mask a preceding error but we can't do anything about it as we can report a single error.
s_nPendingError = nReportedError = GetLastError();
}

s_pPendingThreads = NULL;
s_nPendingThreadId = 0;

return NO_ERROR;
return nReportedError;
}

LONG WINAPI DetourTransactionCommit()
Expand Down Expand Up @@ -1935,6 +1950,11 @@ typedef ULONG_PTR DETOURS_EIP_TYPE;
delete t;
t = n;
}

if (!HeapUnlock(GetProcessHeap())) {
s_nPendingError = GetLastError();
}

s_pPendingThreads = NULL;
s_nPendingThreadId = 0;

Expand Down