Skip to content

Commit

Permalink
fix: CreateThread on RS2-RS3
Browse files Browse the repository at this point in the history
RS2 actually already contains the ABI break for `CreateThread` similar
to RS4.

This commit fixes the names and dispatching to reflect this discovery.
  • Loading branch information
trungnt2910 committed Nov 5, 2024
1 parent f89d66f commit ac93445
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lxmonika/include/pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,14 @@ typedef NTSTATUS PS_PICO_CREATE_THREAD_TH1(
_In_ PPS_PICO_THREAD_ATTRIBUTES ThreadAttributes,
_Outptr_ PHANDLE ThreadHandle
);
typedef NTSTATUS PS_PICO_CREATE_THREAD_RS4(
typedef NTSTATUS PS_PICO_CREATE_THREAD_RS2(
_In_ PPS_PICO_THREAD_ATTRIBUTES ThreadAttributes,
_In_opt_ PPS_PICO_CREATE_INFO CreateInfo,
_Outptr_ PHANDLE ThreadHandle
);

#if (NTDDI_VERSION >= NTDDI_RS4)
typedef PS_PICO_CREATE_THREAD_RS4 PS_PICO_CREATE_THREAD;
#if (NTDDI_VERSION >= NTDDI_RS2)
typedef PS_PICO_CREATE_THREAD_RS2 PS_PICO_CREATE_THREAD;
#else
typedef PS_PICO_CREATE_THREAD_TH1 PS_PICO_CREATE_THREAD;
#endif
Expand Down
13 changes: 12 additions & 1 deletion lxmonika/src/monika.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,20 @@ MaRegisterPicoProviderEx(

// Keep the potentially larger size, hoping that some drivers
// might know that they are outdated.
if (dwAbiVersion >= NTDDI_WIN10_RS4)
if (dwAbiVersion >= NTDDI_WIN10_RS2)
{
// This is the ABI for RS4.
memcpy(PicoRoutines, &MapRoutines[uProviderIndex], PicoRoutines->Size);
// RS3 or lower still uses the CreateProcess callback from TH1.
if (dwAbiVersion < NTDDI_WIN10_RS4)
{
if (PicoRoutines->Size >=
(FIELD_OFFSET(PS_PICO_ROUTINES, CreateProcess)
+ sizeof(PS_PICO_ROUTINES::CreateProcess)))
{
PicoRoutines->CreateProcess = MapRoutinesTh1[uProviderIndex].CreateProcess;
}
}
}
else // NTDDI_WIN10
{
Expand Down
19 changes: 9 additions & 10 deletions lxmonika/src/monika_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,16 @@ MapCreateProcess(
{
MapRanAbiCheck = TRUE;

Logger::LogTrace("Running ABI check before initial dispatch.");
Logger::LogTrace("Running ABI check before initial CreateProcess dispatch.");

// It would be nice if we could run this during initialization.
// However, starting processes when the system has not finished initializing
// is not a fun thing to do and has caused BSODs during testing.
// Instead, we wait until everything is ready and a provider starts requesting
// Pico processes.
// We are not doing anything similar for the threads, since one would need
// a working Pico process to create a Pico thread (theoretically).
// By the time the thread callbacks are called, we should have established
// our version.
//
// For threads, the ABI change occurred at RS2 instead of RS4.
// Fortunately, we can detect RS2 through parameter struct sizes alone.

// Still use the RS4 callback.
PS_PICO_CREATE_INFO psTempInfo = CreateInfo != NULL ?
Expand All @@ -286,7 +285,7 @@ MapCreateProcess(
if (hdlProcess == NULL)
{
Logger::LogTrace("Call succeeded but output parameter is wrong.");
Logger::LogTrace("Old ABI detected.", (PVOID)status);
Logger::LogTrace("Old ABI detected.");

// This is actually RS3 or lower.
// The handle is in psTempInfo.
Expand Down Expand Up @@ -315,12 +314,12 @@ MapCreateProcess(
if (!NT_SUCCESS(status))
{
Logger::LogTrace("TH1 variant also failed, status=", (PVOID)status);
Logger::LogTrace("Invalidating ABI check results.", (PVOID)status);
Logger::LogTrace("Invalidating ABI check results.");
MapRanAbiCheck = FALSE;
}
else
{
Logger::LogTrace("Old ABI detected.", (PVOID)status);
Logger::LogTrace("Old ABI detected.");
}
}
}
Expand Down Expand Up @@ -466,9 +465,9 @@ MapCreateThread(
NTSTATUS status;
HANDLE hdlThread = NULL;

if (MA_SYSTEM_AT_LEAST(NTDDI_WIN10_RS4))
if (MA_SYSTEM_AT_LEAST(NTDDI_WIN10_RS2))
{
status = ((PS_PICO_CREATE_THREAD_RS4*)MapOriginalRoutines.CreateThread)(
status = ((PS_PICO_CREATE_THREAD_RS2*)MapOriginalRoutines.CreateThread)(
ThreadAttributes, CreateInfo, &hdlThread);
}
else // TH1
Expand Down

0 comments on commit ac93445

Please sign in to comment.