From eb248b3e7d6fdc9093d2c642067afda5b7d613f6 Mon Sep 17 00:00:00 2001 From: Ratin Gao Date: Fri, 9 Aug 2024 19:40:58 +0800 Subject: [PATCH] [3RDPARTY] Update KNSoft.NDK to 1.1.0-beta and adapt other NDKs --- Source/Demo/DeadLock.c | 2 +- Source/Demo/DelayHook.c | 2 +- Source/Demo/Demo.vcxproj | 4 +- Source/Demo/Main.c | 9 ++ Source/Demo/packages.config | 2 +- Source/Directory.Build.props | 16 +++- Source/KNSoft.SlimDetours.Build.props | 7 ++ Source/KNSoft.SlimDetours.vcxproj | 7 +- Source/KNSoft.SlimDetours.vcxproj.filters | 3 + .../SlimDetours/SlimDetours.NDK.Addendum.inl | 42 ---------- Source/SlimDetours/SlimDetours.NDK.inl | 82 +++++++++++++++++++ Source/SlimDetours/SlimDetours.h | 4 +- Source/SlimDetours/SlimDetours.inl | 28 +------ Source/packages.config | 2 +- 14 files changed, 129 insertions(+), 81 deletions(-) create mode 100644 Source/KNSoft.SlimDetours.Build.props delete mode 100644 Source/SlimDetours/SlimDetours.NDK.Addendum.inl create mode 100644 Source/SlimDetours/SlimDetours.NDK.inl diff --git a/Source/Demo/DeadLock.c b/Source/Demo/DeadLock.c index 8cdb790..daec5ec 100644 --- a/Source/Demo/DeadLock.c +++ b/Source/Demo/DeadLock.c @@ -83,7 +83,7 @@ SetHookThread( return hr; } -static TEST_DECL(DeadLock) +TEST_FUNC(DeadLock) { NTSTATUS Status; HANDLE hThreads[2]; diff --git a/Source/Demo/DelayHook.c b/Source/Demo/DelayHook.c index 2c9cb05..79fac34 100644 --- a/Source/Demo/DelayHook.c +++ b/Source/Demo/DelayHook.c @@ -30,7 +30,7 @@ DelayAttachCallback( _stricmp(Function, g_asEqualRect.Buffer) == 0; } -static TEST_DECL(DelayHook) +TEST_FUNC(DelayHook) { NTSTATUS Status; HRESULT hr; diff --git a/Source/Demo/Demo.vcxproj b/Source/Demo/Demo.vcxproj index 10de480..7e86bd1 100644 --- a/Source/Demo/Demo.vcxproj +++ b/Source/Demo/Demo.vcxproj @@ -188,12 +188,12 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + \ No newline at end of file diff --git a/Source/Demo/Main.c b/Source/Demo/Main.c index 00d63a3..9b1b4ee 100644 --- a/Source/Demo/Main.c +++ b/Source/Demo/Main.c @@ -2,6 +2,15 @@ #include +TEST_DECL_FUNC(DeadLock); +TEST_DECL_FUNC(DelayHook); + +CONST UNITTEST_ENTRY UnitTestList[] = { + TEST_DECL_ENTRY(DeadLock), + TEST_DECL_ENTRY(DelayHook), + { 0 } +}; + #define DEMO_SWITCH_ENGINE L"-Engine=" FN_EqualRect* g_pfnEqualRect = NULL; diff --git a/Source/Demo/packages.config b/Source/Demo/packages.config index 927bf47..ac12d07 100644 --- a/Source/Demo/packages.config +++ b/Source/Demo/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/Source/Directory.Build.props b/Source/Directory.Build.props index 4f030c7..679a729 100644 --- a/Source/Directory.Build.props +++ b/Source/Directory.Build.props @@ -34,6 +34,18 @@ MSB_CONFIGURATIONTYPE_UTILITY;%(PreprocessorDefinitions) + + + MSB_CONFIGURATION_NAME="$(ConfigurationName)";%(PreprocessorDefinitions) + MSB_MSBUILD_VERSION="$(MSBuildVersion)";%(PreprocessorDefinitions) + MSB_VCTOOLS_VERSION="$(VCToolsVersion)";%(PreprocessorDefinitions) + MSB_PLATFORM_TOOLSET="$(PlatformToolset)";%(PreprocessorDefinitions) + MSB_PLATFORM_TOOLSET_VERSION="$(PlatformToolsetVersion)";%(PreprocessorDefinitions) + MSB_PLATFORM_TARGET_VERSION="$(PlatformTarget)";%(PreprocessorDefinitions) + MSB_TARGET_PLATFORM_VERSION="$(TargetPlatformVersion)";%(PreprocessorDefinitions) + MSB_TARGET_PLATFORM_DISPLAY_NAME="$(TargetPlatformDisplayName)";%(PreprocessorDefinitions) + + @@ -47,9 +59,9 @@ - + - $(MSBuildProjectDirectory)\$(ProjectName).Build.props + $(MSBuildProjectDirectory)\$(MSBuildProjectName).Build.props diff --git a/Source/KNSoft.SlimDetours.Build.props b/Source/KNSoft.SlimDetours.Build.props new file mode 100644 index 0000000..e09c73b --- /dev/null +++ b/Source/KNSoft.SlimDetours.Build.props @@ -0,0 +1,7 @@ + + + + _USE_KNSOFT_NDK;%(PreprocessorDefinitions) + + + \ No newline at end of file diff --git a/Source/KNSoft.SlimDetours.vcxproj b/Source/KNSoft.SlimDetours.vcxproj index 9f50b89..6b5cfa1 100644 --- a/Source/KNSoft.SlimDetours.vcxproj +++ b/Source/KNSoft.SlimDetours.vcxproj @@ -207,6 +207,8 @@ + + @@ -214,17 +216,16 @@ - - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + \ No newline at end of file diff --git a/Source/KNSoft.SlimDetours.vcxproj.filters b/Source/KNSoft.SlimDetours.vcxproj.filters index c1d4ffe..b852570 100644 --- a/Source/KNSoft.SlimDetours.vcxproj.filters +++ b/Source/KNSoft.SlimDetours.vcxproj.filters @@ -79,6 +79,9 @@ Detours + + SlimDetours + \ No newline at end of file diff --git a/Source/SlimDetours/SlimDetours.NDK.Addendum.inl b/Source/SlimDetours/SlimDetours.NDK.Addendum.inl deleted file mode 100644 index 40515ea..0000000 --- a/Source/SlimDetours/SlimDetours.NDK.Addendum.inl +++ /dev/null @@ -1,42 +0,0 @@ -/* Addendum to ReactOS NDK and helper macros on KNSoft.NDK */ - -#pragma once - -#if defined(_X86_) -#define CONTEXT_PC Eip -#elif defined(_AMD64_) -#define CONTEXT_PC Rip -#elif defined(_ARM64_) -#define CONTEXT_PC Pc -#endif - -#define Add2Ptr(P,I) ((PVOID)((PUCHAR)(P) + (I))) -#define PtrOffset(B,O) ((ULONG)((ULONG_PTR)(O) - (ULONG_PTR)(B))) - -#define KB_TO_BYTES(x) ((x) * 1024UL) -#define MB_TO_KB(x) ((x) * 1024UL) -#define MB_TO_BYTES(x) (KB_TO_BYTES(MB_TO_KB(x))) -#define GB_TO_MB(x) ((x) * 1024UL) -#define GB_TO_BYTES(x) (MB_TO_BYTES(GB_TO_MB(x))) - -#define MM_LOWEST_USER_ADDRESS ((PVOID)0x10000) - -#if defined(_WIN64) - -/* [0x00007FF7FFFF0000 ... 0x00007FFFFFFF0000], 32G */ - -#define MI_ASLR_BITMAP_SIZE 0x10000 -#define MI_ASLR_HIGHEST_SYSTEM_RANGE_ADDRESS ((PVOID)0x00007FFFFFFF0000ULL) - -#else - -/* [0x50000000 ... 0x78000000], 640M */ - -#define MI_ASLR_BITMAP_SIZE 0x500 -#define MI_ASLR_HIGHEST_SYSTEM_RANGE_ADDRESS ((PVOID)0x78000000UL) - -#endif - -#define NtGetCurrentProcessId() (NtCurrentTeb()->ClientId.UniqueProcess) -#define NtGetCurrentThreadId() (NtCurrentTeb()->ClientId.UniqueThread) -#define NtGetProcessHeap() (NtCurrentPeb()->ProcessHeap) diff --git a/Source/SlimDetours/SlimDetours.NDK.inl b/Source/SlimDetours/SlimDetours.NDK.inl new file mode 100644 index 0000000..fa5e14a --- /dev/null +++ b/Source/SlimDetours/SlimDetours.NDK.inl @@ -0,0 +1,82 @@ +/* + * Adapt to NDKs to access low-level Windows NT APIs + * + * SlimDetours uses KNSoft.NDK by default, and also support other NDKs. + * + * KNSoft.NDK + * Used when macro `_USE_KNSOFT_NDK` is defined, this is the default behavior on offical project. + * + * ReactOS NDK + * Used when macro `__REACTOS__` is defined, can be built with ReactOS. + * + * Other NDKs + * Include other NDKs (e.g. phnt) header before SlimDetours, they should provide what we need. + */ + +#pragma once + +#if defined(_USE_KNSOFT_NDK) + +#include + +#elif defined(__REACTOS__) + +#define WIN32_NO_STATUS +#include +#include + +#define NTOS_MODE_USER +#include +#include +#include +#include +#include +#include + +#endif + +/* Add KNSoft.NDK specific stuff */ +#ifndef _USE_KNSOFT_NDK + +#define PAGE_SIZE 0x1000 +#define MM_ALLOCATION_GRANULARITY 0x10000 + +#if defined(_X86_) +#define CONTEXT_PC Eip +#elif defined(_AMD64_) +#define CONTEXT_PC Rip +#elif defined(_ARM64_) +#define CONTEXT_PC Pc +#endif + +#define Add2Ptr(P,I) ((PVOID)((PUCHAR)(P) + (I))) +#define PtrOffset(B,O) ((ULONG)((ULONG_PTR)(O) - (ULONG_PTR)(B))) + +#define KB_TO_BYTES(x) ((x) * 1024UL) +#define MB_TO_KB(x) ((x) * 1024UL) +#define MB_TO_BYTES(x) (KB_TO_BYTES(MB_TO_KB(x))) +#define GB_TO_MB(x) ((x) * 1024UL) +#define GB_TO_BYTES(x) (MB_TO_BYTES(GB_TO_MB(x))) + +#define MM_LOWEST_USER_ADDRESS ((PVOID)0x10000) + +#if defined(_WIN64) + +/* [0x00007FF7FFFF0000 ... 0x00007FFFFFFF0000], 32G */ +#define MI_ASLR_BITMAP_SIZE 0x10000 +#define MI_ASLR_HIGHEST_SYSTEM_RANGE_ADDRESS ((PVOID)0x00007FFFFFFF0000ULL) + +#else + +/* [0x50000000 ... 0x78000000], 640M */ +#define MI_ASLR_BITMAP_SIZE 0x500 +#define MI_ASLR_HIGHEST_SYSTEM_RANGE_ADDRESS ((PVOID)0x78000000UL) + +#endif + +#define NtGetCurrentProcessId() (NtCurrentTeb()->ClientId.UniqueProcess) +#define NtGetCurrentThreadId() (NtCurrentTeb()->ClientId.UniqueThread) +#define NtGetProcessHeap() (NtCurrentPeb()->ProcessHeap) +#define NtGetNtdllBase() (CONTAINING_RECORD(NtCurrentPeb()->Ldr->InInitializationOrderModuleList.Flink, LDR_DATA_TABLE_ENTRY, InInitializationOrderModuleList)->DllBase) + +#endif diff --git a/Source/SlimDetours/SlimDetours.h b/Source/SlimDetours/SlimDetours.h index 328df43..a5598ae 100644 --- a/Source/SlimDetours/SlimDetours.h +++ b/Source/SlimDetours/SlimDetours.h @@ -12,12 +12,12 @@ #pragma once +#include + #if !defined(_X86_) && !defined(_AMD64_) && !defined(_ARM64_) #error Unsupported architecture (x86, amd64, arm64) #endif -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/Source/SlimDetours/SlimDetours.inl b/Source/SlimDetours/SlimDetours.inl index 2e3eb32..83018cb 100644 --- a/Source/SlimDetours/SlimDetours.inl +++ b/Source/SlimDetours/SlimDetours.inl @@ -1,34 +1,10 @@ #pragma once -#ifndef __REACTOS__ - -/* Use KNSoft.NDK by default */ -#include - -#else - -/* Use ReactOS NDK with addendum on ReactOS */ - -#define WIN32_NO_STATUS -#include -#include - -#define NTOS_MODE_USER -#include -#include -#include -#include -#include -#include +#include "SlimDetours.NDK.inl" +#include "SlimDetours.h" #include -#include "SlimDetours.NDK.Addendum.inl" - -#endif - -#include "SlimDetours.h" - #if _DEBUG #define DETOUR_TRACE DbgPrint #define DETOUR_BREAK() __debugbreak() diff --git a/Source/packages.config b/Source/packages.config index 927bf47..ac12d07 100644 --- a/Source/packages.config +++ b/Source/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file