Skip to content

Commit

Permalink
[3RDPARTY] Update KNSoft.NDK to 1.1.0-beta and adapt other NDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
RatinCN committed Aug 9, 2024
1 parent b68c239 commit eb248b3
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Source/Demo/DeadLock.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ SetHookThread(
return hr;
}

static TEST_DECL(DeadLock)
TEST_FUNC(DeadLock)
{
NTSTATUS Status;
HANDLE hThreads[2];
Expand Down
2 changes: 1 addition & 1 deletion Source/Demo/DelayHook.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ DelayAttachCallback(
_stricmp(Function, g_asEqualRect.Buffer) == 0;
}

static TEST_DECL(DelayHook)
TEST_FUNC(DelayHook)
{
NTSTATUS Status;
HRESULT hr;
Expand Down
4 changes: 2 additions & 2 deletions Source/Demo/Demo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets')" />
<Import Project="..\packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets'))" />
<Error Condition="!Exists('..\packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets'))" />
</Target>
</Project>
9 changes: 9 additions & 0 deletions Source/Demo/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

#include <KNSoft/NDK/UnitTest/UnitTest.inl>

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;
Expand Down
2 changes: 1 addition & 1 deletion Source/Demo/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="KNSoft.NDK" version="1.0.7-beta" targetFramework="native" />
<package id="KNSoft.NDK" version="1.1.0-beta" targetFramework="native" />
</packages>
16 changes: 14 additions & 2 deletions Source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
<PreprocessorDefinitions>MSB_CONFIGURATIONTYPE_UTILITY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>MSB_CONFIGURATION_NAME="$(ConfigurationName)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MSB_MSBUILD_VERSION="$(MSBuildVersion)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MSB_VCTOOLS_VERSION="$(VCToolsVersion)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MSB_PLATFORM_TOOLSET="$(PlatformToolset)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MSB_PLATFORM_TOOLSET_VERSION="$(PlatformToolsetVersion)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MSB_PLATFORM_TARGET_VERSION="$(PlatformTarget)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MSB_TARGET_PLATFORM_VERSION="$(TargetPlatformVersion)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>MSB_TARGET_PLATFORM_DISPLAY_NAME="$(TargetPlatformDisplayName)";%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>

<!-- Use static default libraries -->
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
Expand All @@ -47,9 +59,9 @@
</ClCompile>
</ItemDefinitionGroup>

<!-- Import $(ProjectName).Build.props if exists -->
<!-- Import $(MSBuildProjectName).Build.props if exists -->
<PropertyGroup>
<ProjectNameBuildPropsPath>$(MSBuildProjectDirectory)\$(ProjectName).Build.props</ProjectNameBuildPropsPath>
<ProjectNameBuildPropsPath>$(MSBuildProjectDirectory)\$(MSBuildProjectName).Build.props</ProjectNameBuildPropsPath>
</PropertyGroup>
<Import Project="$(ProjectNameBuildPropsPath)" Condition="Exists('$(ProjectNameBuildPropsPath)')"/>

Expand Down
7 changes: 7 additions & 0 deletions Source/KNSoft.SlimDetours.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>_USE_KNSOFT_NDK;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
</Project>
7 changes: 4 additions & 3 deletions Source/KNSoft.SlimDetours.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,25 @@
<ClCompile Include="SlimDetours\Trampoline.c" />
<ClCompile Include="SlimDetours\Transaction.c" />
<ClCompile Include="SlimDetours\Wrapper.c" />
<None Include="packages.config" />
<None Include="SlimDetours\SlimDetours.NDK.inl" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Detours\src\detours.h" />
<ClInclude Include="Detours\src\detver.h" />
<ClInclude Include="SlimDetours\SlimDetours.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="SlimDetours\SlimDetours.inl" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets" Condition="Exists('packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets')" />
<Import Project="packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets" Condition="Exists('packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\KNSoft.NDK.1.0.7-beta\build\KNSoft.NDK.targets'))" />
<Error Condition="!Exists('packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\KNSoft.NDK.1.1.0-beta\build\KNSoft.NDK.targets'))" />
</Target>
</Project>
3 changes: 3 additions & 0 deletions Source/KNSoft.SlimDetours.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<None Include="Detours\src\uimports.cpp">
<Filter>Detours</Filter>
</None>
<None Include="SlimDetours\SlimDetours.NDK.inl">
<Filter>SlimDetours</Filter>
</None>
<None Include="packages.config" />
</ItemGroup>
</Project>
42 changes: 0 additions & 42 deletions Source/SlimDetours/SlimDetours.NDK.Addendum.inl

This file was deleted.

82 changes: 82 additions & 0 deletions Source/SlimDetours/SlimDetours.NDK.inl
Original file line number Diff line number Diff line change
@@ -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 <KNSoft/NDK/NDK.h>

#elif defined(__REACTOS__)

#define WIN32_NO_STATUS
#include <windef.h>
#include <winbase.h>

#define NTOS_MODE_USER
#include <ndk/exfuncs.h>
#include <ndk/obfuncs.h>
#include <ndk/mmfuncs.h>
#include <ndk/kefuncs.h>
#include <ndk/psfuncs.h>
#include <ndk/rtlfuncs.h>

#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
4 changes: 2 additions & 2 deletions Source/SlimDetours/SlimDetours.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

#pragma once

#include <Windows.h>

#if !defined(_X86_) && !defined(_AMD64_) && !defined(_ARM64_)
#error Unsupported architecture (x86, amd64, arm64)
#endif

#include <Windows.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
28 changes: 2 additions & 26 deletions Source/SlimDetours/SlimDetours.inl
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
#pragma once

#ifndef __REACTOS__

/* Use KNSoft.NDK by default */
#include <KNSoft/NDK/NDK.h>

#else

/* Use ReactOS NDK with addendum on ReactOS */

#define WIN32_NO_STATUS
#include <windef.h>
#include <winbase.h>

#define NTOS_MODE_USER
#include <ndk/exfuncs.h>
#include <ndk/obfuncs.h>
#include <ndk/mmfuncs.h>
#include <ndk/kefuncs.h>
#include <ndk/psfuncs.h>
#include <ndk/rtlfuncs.h>
#include "SlimDetours.NDK.inl"
#include "SlimDetours.h"

#include <suppress.h>

#include "SlimDetours.NDK.Addendum.inl"

#endif

#include "SlimDetours.h"

#if _DEBUG
#define DETOUR_TRACE DbgPrint
#define DETOUR_BREAK() __debugbreak()
Expand Down
2 changes: 1 addition & 1 deletion Source/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="KNSoft.NDK" version="1.0.7-beta" targetFramework="native" />
<package id="KNSoft.NDK" version="1.1.0-beta" targetFramework="native" />
</packages>

0 comments on commit eb248b3

Please sign in to comment.