Skip to content

Commit

Permalink
Refactored undocumented Windows structures to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaldaien committed Dec 17, 2024
1 parent d9bbbbf commit b9c609a
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 214 deletions.
3 changes: 3 additions & 0 deletions include/SpecialK/diagnostics/debug_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ using NtTerminateProcess_pfn = NTSTATUS (*)(HANDLE, NTSTATUS);
using RtlExitUserThread_pfn = VOID (NTAPI *)(_In_ NTSTATUS Status);
using SHGetKnownFolderPath_pfn = HRESULT (WINAPI *)(REFKNOWNFOLDERID,DWORD,HANDLE,PWSTR*);

using RtlAcquirePebLock_pfn = void (NTAPI *)(void);
using RtlReleasePebLock_pfn = void (NTAPI *)(void);

extern SHGetKnownFolderPath_pfn SHGetKnownFolderPath_Original;

extern GetCommandLineW_pfn GetCommandLineW_Original;
Expand Down
26 changes: 13 additions & 13 deletions include/SpecialK/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@

#define UNICODE 1

#ifndef NT_SUCCESS
# define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
# define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
# define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
# define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
// STATUS_INVALID_PARAMETER ((NTSTATUS)0xC000000DL)
# define STATUS_NO_SUCH_FILE ((NTSTATUS)0xC000000FL)
# define STATUS_ACCESS_DENIED ((NTSTATUS)0xc0000022L)
# define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L)
# define STATUS_ALERTED ((NTSTATUS)0x00000101L)
# define STATUS_PROCESS_IS_TERMINATING ((NTSTATUS)0xC000010AL)
#endif

#include <Windows.h>
#include <windowsx.h>
#include <excpt.h>
Expand Down Expand Up @@ -16630,19 +16643,6 @@ extern bool __SK_bypass;
extern const wchar_t* SK_VersionStrW;
extern const char* SK_VersionStrA;

#ifndef NT_SUCCESS
# define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
# define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
# define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
# define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
// STATUS_INVALID_PARAMETER ((NTSTATUS)0xC000000DL)
# define STATUS_NO_SUCH_FILE ((NTSTATUS)0xC000000FL)
# define STATUS_ACCESS_DENIED ((NTSTATUS)0xc0000022L)
# define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L)
# define STATUS_ALERTED ((NTSTATUS)0x00000101L)
# define STATUS_PROCESS_IS_TERMINATING ((NTSTATUS)0xC000010AL)
#endif

#ifndef __cpp_lib_format
#define __cpp_lib_format
#endif
Expand Down
122 changes: 117 additions & 5 deletions include/SpecialK/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,15 +730,127 @@ struct SKWG_Thread_Entry
std::wstring name = L"";
};

DWORD WINAPI
SK_DelayExecution (double dMilliseconds, BOOL bAlertable) noexcept;
#define THREAD_CREATE_FLAGS_CREATE_SUSPENDED 0x00000001
#define THREAD_CREATE_FLAGS_SKIP_THREAD_ATTACH 0x00000002
#define THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER 0x00000004
#define THREAD_CREATE_FLAGS_HAS_SECURITY_DESCRIPTOR 0x00000010
#define THREAD_CREATE_FLAGS_ACCESS_CHECK_IN_TARGET 0x00000020
#define THREAD_CREATE_FLAGS_INITIAL_THREAD 0x00000080

using SK_POBJECT_ATTRIBUTES = void*;

using NtCreateThreadEx_pfn = NTSTATUS (NTAPI *)(
_Out_ PHANDLE ThreadHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_opt_ SK_POBJECT_ATTRIBUTES ObjectAttributes,
_In_ HANDLE ProcessHandle,
_In_ PVOID StartRoutine,
_In_opt_ PVOID Argument,
_In_ ULONG CreateFlags,
_In_opt_ ULONG_PTR ZeroBits,
_In_opt_ SIZE_T StackSize,
_In_opt_ SIZE_T MaximumStackSize,
_In_opt_ PVOID AttributeList
);

BOOL
WINAPI
SK_SetProcessAffinityMask (HANDLE hProcess, DWORD_PTR dwProcessAffinityMask);
using GetThreadContext_pfn = BOOL (WINAPI *)(HANDLE,LPCONTEXT);
using SetThreadContext_pfn = BOOL (WINAPI *)(HANDLE,const CONTEXT*);

typedef enum _SK_THREAD_INFORMATION_CLASS {
ThreadBasicInformation,
ThreadTimes,
ThreadPriority,
ThreadBasePriority,
ThreadAffinityMask,
ThreadImpersonationToken,
ThreadDescriptorTableEntry,
ThreadEnableAlignmentFaultFixup,
ThreadEventPair_Reusable,
ThreadQuerySetWin32StartAddress,
ThreadZeroTlsCell,
ThreadPerformanceCount,
ThreadAmILastThread,
ThreadIdealProcessor,
ThreadPriorityBoost,
ThreadSetTlsArrayAddress,
ThreadIsIoPending_,
ThreadHideFromDebugger,
ThreadBreakOnTermination,
ThreadSwitchLegacyState,
ThreadIsTerminated,
ThreadLastSystemCall,
ThreadIoPriority,
ThreadCycleTime,
ThreadPagePriority,
ThreadActualBasePriority,
ThreadTebInformation,
ThreadCSwitchMon,
ThreadCSwitchPmu,
ThreadWow64Context,
ThreadGroupInformation,
ThreadUmsInformation,
ThreadCounterProfiling,
ThreadIdealProcessorEx,
ThreadCpuAccountingInformation,
ThreadSuspendCount,
ThreadHeterogeneousCpuPolicy,
ThreadContainerId,
ThreadNameInformation_,
ThreadSelectedCpuSets,
ThreadSystemThreadInformation,
ThreadActualGroupAffinity,
ThreadDynamicCodePolicyInfo,
ThreadExplicitCaseSensitivity,
ThreadWorkOnBehalfTicket,
ThreadSubsystemInformation,
ThreadDbgkWerReportActive,
ThreadAttachContainer,
ThreadManageWritesToExecutableMemory,
ThreadPowerThrottlingState,
ThreadWorkloadClass,
ThreadCreateStateChange,
ThreadApplyStateChange,
ThreadStrongerBadHandleChecks,
ThreadEffectiveIoPriority,
ThreadEffectivePagePriority,
ThreadUpdateLockOwnership,
ThreadSchedulerSharedDataSlot,
ThreadTebInformationAtomic,
ThreadIndexInformation,
MaxThreadInfoClass,
} SK_THREAD_INFORMATION_CLASS,
*PSK_THREAD_INFORMATION_CLASS;

#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif

using ZwSetInformationThread_pfn = NTSTATUS (NTAPI *)(
_In_ HANDLE ThreadHandle,
_In_ SK_THREAD_INFORMATION_CLASS ThreadInformationClass,
_In_ PVOID ThreadInformation,
_In_ ULONG ThreadInformationLength
);

// The Nt function is the most obvious choice, but some sneaky
// software will hop right over this and call Zw... directly.
//
// --> Thus, don't even bother hooking NtSetInformationThread.
//
using NtSetInformationThread_pfn = NTSTATUS (NTAPI *)(
_In_ HANDLE ThreadHandle,
_In_ SK_THREAD_INFORMATION_CLASS ThreadInformationClass,
_In_ PVOID ThreadInformation,
_In_ ULONG ThreadInformationLength
);

DWORD WINAPI SK_DelayExecution (double dMilliseconds, BOOL bAlertable) noexcept;
BOOL WINAPI SK_SetThreadIOPriority (HANDLE hThread, int ioPriority) noexcept;
BOOL WINAPI SK_SetProcessAffinityMask (HANDLE hProcess, DWORD_PTR dwProcessAffinityMask);

void SK_Widget_InvokeThreadProfiler (void);
void SK_ImGui_RebalanceThreadButton (void);

extern float __SK_Thread_RebalanceEveryNSeconds;

#endif /* __SK__THREAD_H__ */
146 changes: 9 additions & 137 deletions src/diagnostics/debug_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1620,144 +1620,16 @@ OutputDebugStringW_Detour (LPCWSTR lpOutputString)
#endif
}

GetThreadContext_pfn GetThreadContext_Original = nullptr;
SetThreadContext_pfn SetThreadContext_Original = nullptr;

NtCreateThreadEx_pfn NtCreateThreadEx_Original = nullptr;
NtCreateThreadEx_pfn ZwCreateThreadEx_Original = nullptr;
NtSetInformationThread_pfn NtSetInformationThread_Original = nullptr;
ZwSetInformationThread_pfn ZwSetInformationThread_Original = nullptr;

using GetThreadContext_pfn = BOOL (WINAPI *)(HANDLE,LPCONTEXT);
using SetThreadContext_pfn = BOOL (WINAPI *)(HANDLE,const CONTEXT *);

GetThreadContext_pfn GetThreadContext_Original = nullptr;
SetThreadContext_pfn SetThreadContext_Original = nullptr;

enum IO_PRIORITY_HINT : int
{
IoPriorityVeryLow = 0, // Defragging, content indexing and other background I/Os.
IoPriorityLow, // Prefetching for applications.
IoPriorityNormal, // Normal I/Os.
IoPriorityHigh, // Used by filesystems for checkpoint I/O.
IoPriorityCritical, // Used by memory manager. Not available for applications.
MaxIoPriorityTypes
};

typedef enum _SK_THREAD_INFORMATION_CLASS {
ThreadBasicInformation,
ThreadTimes,
ThreadPriority,
ThreadBasePriority,
ThreadAffinityMask,
ThreadImpersonationToken,
ThreadDescriptorTableEntry,
ThreadEnableAlignmentFaultFixup,
ThreadEventPair,
ThreadQuerySetWin32StartAddress,
ThreadZeroTlsCell,
ThreadPerformanceCount,
ThreadAmILastThread,
ThreadIdealProcessor,
ThreadPriorityBoost,
ThreadSetTlsArrayAddress,
ThreadIsIoPending_,
ThreadHideFromDebugger,
ThreadBreakOnTermination,
ThreadSwitchLegacyState,
ThreadIsTerminated,
ThreadLastSystemCall,
ThreadIoPriority,
ThreadCycleTime,
ThreadPagePriority,
ThreadActualBasePriority,
ThreadTebInformation,
ThreadCSwitchMon,
ThreadCSwitchPmu,
ThreadWow64Context,
ThreadGroupInformation,
ThreadUmsInformation,
ThreadCounterProfiling,
ThreadIdealProcessorEx,
ThreadCpuAccountingInformation,
ThreadSuspendCount,
ThreadHeterogeneousCpuPolicy,
ThreadContainerId,
_ThreadNameInformation,
ThreadSelectedCpuSets,
ThreadSystemThreadInformation,
ThreadActualGroupAffinity,
ThreadDynamicCodePolicyInfo,
ThreadExplicitCaseSensitivity,
ThreadWorkOnBehalfTicket,
ThreadSubsystemInformation,
ThreadDbgkWerReportActive,
ThreadAttachContainer,
ThreadManageWritesToExecutableMemory,
ThreadPowerThrottlingState,
} SK_THREAD_INFORMATION_CLASS,
*PSK_THREAD_INFORMATION_CLASS;

#ifndef NT_SUCCESS
# define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif

#define THREAD_CREATE_FLAGS_CREATE_SUSPENDED 0x00000001
#define THREAD_CREATE_FLAGS_SKIP_THREAD_ATTACH 0x00000002
#define THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER 0x00000004
#define THREAD_CREATE_FLAGS_HAS_SECURITY_DESCRIPTOR 0x00000010
#define THREAD_CREATE_FLAGS_ACCESS_CHECK_IN_TARGET 0x00000020
#define THREAD_CREATE_FLAGS_INITIAL_THREAD 0x00000080

using ZwSetInformationThread_pfn = NTSTATUS (NTAPI *)(
_In_ HANDLE ThreadHandle,
_In_ SK_THREAD_INFORMATION_CLASS ThreadInformationClass,
_In_ PVOID ThreadInformation,
_In_ ULONG ThreadInformationLength
);

// The Nt function is the most obvious choice, but some sneaky
// software will hop right over this and call Zw... directly.
//
// --> Thus, don't even bother hooking NtSetInformationThread.
//
using NtSetInformationThread_pfn = NTSTATUS (NTAPI *)(
_In_ HANDLE ThreadHandle,
_In_ SK_THREAD_INFORMATION_CLASS ThreadInformationClass,
_In_ PVOID ThreadInformation,
_In_ ULONG ThreadInformationLength
);

using NtCreateThreadEx_pfn = NTSTATUS (NTAPI *)(
_Out_ PHANDLE ThreadHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_opt_ POBJECT_ATTRIBUTES ObjectAttributes,
_In_ HANDLE ProcessHandle,
_In_ PVOID StartRoutine,
_In_opt_ PVOID Argument,
_In_ ULONG CreateFlags,
_In_opt_ ULONG_PTR ZeroBits,
_In_opt_ SIZE_T StackSize,
_In_opt_ SIZE_T MaximumStackSize,
_In_opt_ PVOID AttributeList
);

NtCreateThreadEx_pfn NtCreateThreadEx_Original = nullptr;
NtCreateThreadEx_pfn ZwCreateThreadEx_Original = nullptr;
NtSetInformationThread_pfn NtSetInformationThread_Original = nullptr;
ZwSetInformationThread_pfn ZwSetInformationThread_Original = nullptr;

typedef void (NTAPI* RtlAcquirePebLock_pfn)(void);
typedef void (NTAPI* RtlReleasePebLock_pfn)(void);

static RtlAcquirePebLock_pfn RtlAcquirePebLock_Original = nullptr;
static RtlReleasePebLock_pfn RtlReleasePebLock_Original = nullptr;

bool
SK_SetThreadIOPriority (HANDLE hThread, int priority)
{
IO_PRIORITY_HINT io_priority =
(IO_PRIORITY_HINT)priority;
static NtSetInformationThread_pfn
NtSetInformationThread =
(NtSetInformationThread_pfn)SK_GetProcAddress (L"NtDll", "NtSetInformationThread");
return
(NT_SUCCESS (NtSetInformationThread(hThread, ThreadIoPriority, &io_priority, sizeof(IO_PRIORITY_HINT))));
}
static RtlAcquirePebLock_pfn RtlAcquirePebLock_Original = nullptr;
static RtlReleasePebLock_pfn RtlReleasePebLock_Original = nullptr;

#define SK_ANTIDEBUG_PARANOIA_STAGE2
#define SK_ANTIDEBUG_PARANOIA_STAGE3
Expand Down Expand Up @@ -4610,7 +4482,7 @@ SK_NtLdr_LockLoaderLock (ULONG Flags, ULONG* State, ULONG_PTR* Cookie)
return STATUS_SUCCESS; // No-Op

static LdrLockLoaderLock_pfn LdrLockLoaderLock =
(LdrLockLoaderLock_pfn)SK_GetProcAddress (L"NtDll.dll",
(LdrLockLoaderLock_pfn)SK_GetProcAddress (L"NtDll.dll",
"LdrLockLoaderLock");

if (! LdrLockLoaderLock)
Expand Down
2 changes: 0 additions & 2 deletions src/input/hid_reports/playstation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ SK_HID_PlayStationDevice::~SK_HID_PlayStationDevice (void)
{
}

extern bool SK_SetThreadIOPriority (HANDLE hThread, int priority);

void SK_HID_FlushPlayStationForceFeedback (void)
{
for (auto& ps_controller : SK_HID_PlayStationControllers)
Expand Down
Loading

0 comments on commit b9c609a

Please sign in to comment.