Skip to content

Commit 36741fd

Browse files
Mattiwattimrexodia
authored andcommitted
Fix ThreadGetSuspendCount if the suspend count limit is reached
Use a better method of querying the suspend count on Windows >= 8.1 that doesn't involve suspending and resuming
1 parent 1874da8 commit 36741fd

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/dbg/thread.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,28 @@ bool ThreadGetTeb(duint TEBAddress, TEB* Teb)
187187

188188
int ThreadGetSuspendCount(HANDLE Thread)
189189
{
190+
// Query the suspend count. This only works on Windows 8.1 and later
191+
DWORD suspendCount;
192+
if(NT_SUCCESS(NtQueryInformationThread(Thread, ThreadSuspendCount, &suspendCount, sizeof(suspendCount), nullptr)))
193+
{
194+
return suspendCount;
195+
}
196+
190197
//
191198
// Suspend a thread in order to get the previous suspension count
192199
// WARNING: This function is very bad (threads should not be randomly interrupted)
193200
//
194-
int suspendCount = (int)SuspendThread(Thread);
195201

196-
if(suspendCount == -1)
197-
return 0;
202+
// Use NtSuspendThread, because there is no Win32 error for STATUS_SUSPEND_COUNT_EXCEEDED
203+
NTSTATUS status = NtSuspendThread(Thread, &suspendCount);
204+
if(status == STATUS_SUSPEND_COUNT_EXCEEDED)
205+
suspendCount = MAXCHAR; // If the thread is already at the max suspend count, KeSuspendThread raises an exception and never returns the count
206+
else if(!NT_SUCCESS(status))
207+
suspendCount = 0;
198208

199209
// Resume the thread's normal execution
200-
ResumeThread(Thread);
210+
if(NT_SUCCESS(status))
211+
ResumeThread(Thread);
201212

202213
return suspendCount;
203214
}

0 commit comments

Comments
 (0)