Skip to content

Commit 3756bf8

Browse files

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

src/SlimDetours/Disassembler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,9 +1058,9 @@ Invalid(
10581058
UNREFERENCED_PARAMETER(pDisasm);
10591059
UNREFERENCED_PARAMETER(pEntry);
10601060
UNREFERENCED_PARAMETER(pbDst);
1061+
UNREFERENCED_PARAMETER(pbSrc);
10611062

1062-
ASSERT(!"Invalid Instruction");
1063-
return pbSrc + 1;
1063+
return NULL;
10641064
}
10651065

10661066
static

src/SlimDetours/Thread.c

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
#include "SlimDetours.inl"
88

99
#define THREAD_ACCESS (THREAD_QUERY_LIMITED_INFORMATION | THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT)
10-
#define INITIAL_THREAD_CAPACITY 128
10+
11+
static HANDLE s_Handles[32];
1112

1213
NTSTATUS
1314
detour_thread_suspend(
1415
_Outptr_result_maybenull_ PHANDLE* SuspendedHandles,
1516
_Out_ PULONG SuspendedHandleCount)
1617
{
1718
NTSTATUS Status;
18-
PHANDLE Buffer = NULL;
19-
ULONG BufferCapacity = 0;
19+
PHANDLE Buffer = s_Handles;
20+
ULONG BufferCapacity = ARRAYSIZE(s_Handles);
2021
ULONG SuspendedCount = 0;
22+
BOOL CurrentThreadSkipped = FALSE;
2123
HANDLE CurrentTID = (HANDLE)(ULONG_PTR)NtCurrentThreadId();
2224
BOOL ClosePrevThread = FALSE;
2325
HANDLE ThreadHandle = NULL;
2426
while (TRUE)
2527
{
26-
HANDLE hNextThread;
27-
Status = NtGetNextThread(NtCurrentProcess(), ThreadHandle, THREAD_ACCESS, 0, 0, &hNextThread);
28+
HANDLE NextThreadHandle;
29+
Status = NtGetNextThread(NtCurrentProcess(), ThreadHandle, THREAD_ACCESS, 0, 0, &NextThreadHandle);
2830
if (ClosePrevThread)
2931
{
3032
NtClose(ThreadHandle);
@@ -39,25 +41,29 @@ detour_thread_suspend(
3941
break;
4042
}
4143

42-
ThreadHandle = hNextThread;
44+
ThreadHandle = NextThreadHandle;
4345
ClosePrevThread = TRUE;
4446

45-
THREAD_BASIC_INFORMATION BasicInformation;
46-
if (!NT_SUCCESS(NtQueryInformationThread(
47-
ThreadHandle,
48-
ThreadBasicInformation,
49-
&BasicInformation,
50-
sizeof(BasicInformation),
51-
NULL
52-
)))
47+
if (!CurrentThreadSkipped)
5348
{
54-
continue;
55-
}
49+
THREAD_BASIC_INFORMATION BasicInformation;
50+
if (!NT_SUCCESS(NtQueryInformationThread(
51+
ThreadHandle,
52+
ThreadBasicInformation,
53+
&BasicInformation,
54+
sizeof(BasicInformation),
55+
NULL
56+
)))
57+
{
58+
continue;
59+
}
5660

57-
/* Skip the current thread */
58-
if (BasicInformation.ClientId.UniqueThread == CurrentTID)
59-
{
60-
continue;
61+
/* Skip the current thread */
62+
if (BasicInformation.ClientId.UniqueThread == CurrentTID)
63+
{
64+
CurrentThreadSkipped = TRUE;
65+
continue;
66+
}
6167
}
6268

6369
if (!NT_SUCCESS(NtSuspendThread(ThreadHandle, NULL)))
@@ -68,18 +74,19 @@ detour_thread_suspend(
6874
ClosePrevThread = FALSE;
6975

7076
Status = STATUS_SUCCESS;
71-
if (Buffer == NULL)
77+
if (SuspendedCount >= BufferCapacity)
7278
{
73-
BufferCapacity = INITIAL_THREAD_CAPACITY;
74-
Buffer = (PHANDLE)detour_memory_alloc(BufferCapacity * sizeof(HANDLE));
75-
if (Buffer == NULL)
79+
BufferCapacity *= 2;
80+
81+
PHANDLE p;
82+
if (Buffer == s_Handles)
7683
{
77-
Status = STATUS_NO_MEMORY;
84+
p = (PHANDLE)detour_memory_alloc(BufferCapacity * sizeof(HANDLE));
85+
} else
86+
{
87+
p = (PHANDLE)detour_memory_realloc(Buffer, BufferCapacity * sizeof(HANDLE));
7888
}
79-
} else if (SuspendedCount >= BufferCapacity)
80-
{
81-
BufferCapacity *= 2;
82-
LPHANDLE p = (PHANDLE)detour_memory_realloc(Buffer, BufferCapacity * sizeof(HANDLE));
89+
8390
if (p)
8491
{
8592
Buffer = p;
@@ -106,17 +113,20 @@ detour_thread_suspend(
106113
Buffer[SuspendedCount++] = ThreadHandle;
107114
}
108115

109-
if (!NT_SUCCESS(Status) && Buffer != NULL)
116+
if (!NT_SUCCESS(Status))
110117
{
111118
for (UINT i = 0; i < SuspendedCount; ++i)
112119
{
113120
NtResumeThread(Buffer[i], NULL);
114121
NtClose(Buffer[i]);
115122
}
116123

117-
detour_memory_free(Buffer);
118-
Buffer = NULL;
124+
if (Buffer != s_Handles)
125+
{
126+
detour_memory_free(Buffer);
127+
}
119128

129+
Buffer = NULL;
120130
SuspendedCount = 0;
121131
}
122132

@@ -138,7 +148,11 @@ detour_thread_resume(
138148
NtResumeThread(SuspendedHandles[i], NULL);
139149
NtClose(SuspendedHandles[i]);
140150
}
141-
detour_memory_free(SuspendedHandles);
151+
152+
if (SuspendedHandles != s_Handles)
153+
{
154+
detour_memory_free(SuspendedHandles);
155+
}
142156
}
143157

144158
NTSTATUS

src/SlimDetours/Transaction.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,13 @@ SlimDetoursAttach(
414414

415415
DETOUR_TRACE(" SlimDetoursCopyInstruction(%p,%p)\n", pbTrampoline, pbSrc);
416416
pbSrc = (PBYTE)SlimDetoursCopyInstruction(pbTrampoline, pbSrc, NULL, &lExtra);
417+
if (pbSrc == NULL)
418+
{
419+
Status = STATUS_ILLEGAL_INSTRUCTION;
420+
DETOUR_BREAK();
421+
goto fail;
422+
}
423+
417424
DETOUR_TRACE(" SlimDetoursCopyInstruction() = %p (%d bytes)\n", pbSrc, (int)(pbSrc - pbOp));
418425
pbTrampoline += (pbSrc - pbOp) + lExtra;
419426
cbTarget = PtrOffset(pbTarget, pbSrc);

0 commit comments

Comments
 (0)