Skip to content

Commit

Permalink
Update Wintexports to KNSoft.NDK
Browse files Browse the repository at this point in the history
  • Loading branch information
RatinCN committed Apr 21, 2024
1 parent bfd5da9 commit 967aa9b
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Source/SlimDetours/Memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static HANDLE g_hHeap = NULL;
* and [0x00007FF7FFFF0000 ... 0x00007FFFFFFF0000] (32GB) on 64-bit Windows, which is too large to avoid.
* In this case, avoiding 1GB range starting at Ntdll.dll is make sense.
*
* Use MI_ASLR_* provided by Wintexports instead of hard-coded.
* Use MI_ASLR_* provided by KNSoft.NDK instead of hard-coded.
*/

#define SYSTEM_RESERVED_REGION_HIGHEST ((ULONG_PTR)MI_ASLR_HIGHEST_SYSTEM_RANGE_ADDRESS - 1)
Expand Down Expand Up @@ -92,7 +92,7 @@ PVOID detour_memory_alloc(_In_ SIZE_T Size)
{
if (g_hHeap == NULL)
{
g_hHeap = CURRENT_PROCESS_HEAP;
g_hHeap = NtGetProcessHeap();
}
return RtlAllocateHeap(g_hHeap, 0, Size);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SlimDetours/SlimDetours.inl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Wintexports/Wintexports.h>
#include <KNSoft/NDK/NDK.h>

#include <limits.h>

Expand Down
4 changes: 2 additions & 2 deletions Source/SlimDetours/SlimDetours.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
<Import Project="..\packages\KNSoft.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets" Condition="Exists('..\packages\KNSoft.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets')" />
<Import Project="..\packages\KNSoft.NDK.1.0.1-alpha\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.0.1-alpha\build\KNSoft.NDK.targets')" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
Expand Down Expand Up @@ -213,6 +213,6 @@
<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.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets'))" />
<Error Condition="!Exists('..\packages\KNSoft.NDK.1.0.1-alpha\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.0.1-alpha\build\KNSoft.NDK.targets'))" />
</Target>
</Project>
20 changes: 10 additions & 10 deletions Source/SlimDetours/Thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ NTSTATUS detour_thread_suspend(

i = MB_TO_BYTES(1);
_try_alloc:
pSPI = (PSYSTEM_PROCESS_INFORMATION)RtlAllocateHeap(CURRENT_PROCESS_HEAP, 0, i);
pSPI = (PSYSTEM_PROCESS_INFORMATION)RtlAllocateHeap(NtGetProcessHeap(), 0, i);
if (pSPI == NULL)
{
return STATUS_NO_MEMORY;
}
Status = NtQuerySystemInformation(SystemProcessInformation, pSPI, i, &i);
if (!NT_SUCCESS(Status))
{
RtlFreeHeap(CURRENT_PROCESS_HEAP, 0, pSPI);
RtlFreeHeap(NtGetProcessHeap(), 0, pSPI);
if (Status == STATUS_INFO_LENGTH_MISMATCH)
{
goto _try_alloc;
Expand All @@ -40,14 +40,14 @@ NTSTATUS detour_thread_suspend(

/* Find current process and threads */

CurrentPID = (HANDLE)WIE_ReadTEB(ClientId.UniqueProcess);
CurrentPID = (HANDLE)ReadTeb(ClientId.UniqueProcess);
pCurrentSPI = pSPI;
_next_proc:
if (pCurrentSPI->UniqueProcessId != CurrentPID)
{
if (pCurrentSPI->NextEntryOffset == 0)
{
RtlFreeHeap(CURRENT_PROCESS_HEAP, 0, pSPI);
RtlFreeHeap(NtGetProcessHeap(), 0, pSPI);
return STATUS_NOT_FOUND;
} else
{
Expand All @@ -59,16 +59,16 @@ NTSTATUS detour_thread_suspend(

/* Suspend threads and create handle array */

Buffer = (PHANDLE)RtlAllocateHeap(CURRENT_PROCESS_HEAP, 0, pCurrentSPI->NumberOfThreads * sizeof(HANDLE));
Buffer = (PHANDLE)RtlAllocateHeap(NtGetProcessHeap(), 0, pCurrentSPI->NumberOfThreads * sizeof(HANDLE));
if (Buffer == NULL)
{
RtlFreeHeap(CURRENT_PROCESS_HEAP, 0, pSPI);
RtlFreeHeap(NtGetProcessHeap(), 0, pSPI);
return STATUS_NO_MEMORY;
}

InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
SuspendedCount = 0;
CurrentTID = (HANDLE)WIE_ReadTEB(ClientId.UniqueThread);
CurrentTID = (HANDLE)ReadTeb(ClientId.UniqueThread);
for (i = 0; i < pCurrentSPI->NumberOfThreads; i++)
{
if (pSTI[i].ClientId.UniqueThread == CurrentTID ||
Expand All @@ -87,7 +87,7 @@ NTSTATUS detour_thread_suspend(
NtClose(ThreadHandle);
}
}
RtlFreeHeap(CURRENT_PROCESS_HEAP, 0, pSPI);
RtlFreeHeap(NtGetProcessHeap(), 0, pSPI);

/* Return suspended thread handles */

Expand All @@ -97,7 +97,7 @@ NTSTATUS detour_thread_suspend(
*SuspendedHandles = Buffer;
} else
{
RtlFreeHeap(CURRENT_PROCESS_HEAP, 0, Buffer);
RtlFreeHeap(NtGetProcessHeap(), 0, Buffer);
*SuspendedHandles = NULL;
}
return STATUS_SUCCESS;
Expand All @@ -114,7 +114,7 @@ VOID detour_thread_resume(
NtResumeThread(SuspendedHandles[i], NULL);
NtClose(SuspendedHandles[i]);
}
RtlFreeHeap(CURRENT_PROCESS_HEAP, 0, SuspendedHandles);
RtlFreeHeap(NtGetProcessHeap(), 0, SuspendedHandles);
}

NTSTATUS detour_thread_update(_In_ HANDLE ThreadHandle, _In_ PDETOUR_OPERATION PendingOperations)
Expand Down
12 changes: 6 additions & 6 deletions Source/SlimDetours/Transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "SlimDetours.inl"

#pragma comment(lib, "WIE_WinAPI.lib")
#pragma comment(lib, "KNSoft.NDK.WinAPI.lib")

typedef struct _DETOUR_DELAY_ATTACH DETOUR_DELAY_ATTACH, *PDETOUR_DELAY_ATTACH;
typedef VOID(CALLBACK* DETOUR_DELAY_ATTACH_CALLBACK)(
Expand Down Expand Up @@ -46,7 +46,7 @@ NTSTATUS NTAPI SlimDetoursTransactionBegin()
NTSTATUS Status;

// Make sure only one thread can start a transaction.
if (_InterlockedCompareExchangePointer(&s_nPendingThreadId, CURRENT_THREAD_ID, 0) != 0)
if (_InterlockedCompareExchangePointer(&s_nPendingThreadId, NtGetCurrentThreadId(), 0) != 0)
{
return STATUS_TRANSACTIONAL_CONFLICT;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ NTSTATUS NTAPI SlimDetoursTransactionAbort()
SIZE_T sMem;
DWORD dwOld;

if (s_nPendingThreadId != CURRENT_THREAD_ID)
if (s_nPendingThreadId != NtGetCurrentThreadId())
{
return STATUS_TRANSACTIONAL_CONFLICT;
}
Expand Down Expand Up @@ -134,7 +134,7 @@ NTSTATUS NTAPI SlimDetoursTransactionCommit()
BOOL freed = FALSE;
ULONG i;

if (s_nPendingThreadId != CURRENT_THREAD_ID)
if (s_nPendingThreadId != NtGetCurrentThreadId())
{
return STATUS_TRANSACTIONAL_CONFLICT;
}
Expand Down Expand Up @@ -259,7 +259,7 @@ NTSTATUS NTAPI SlimDetoursAttach(_Inout_ PVOID* ppPointer, _In_ PVOID pDetour)
SIZE_T sMem;
DWORD dwOld;

if (s_nPendingThreadId != CURRENT_THREAD_ID)
if (s_nPendingThreadId != NtGetCurrentThreadId())
{
return STATUS_TRANSACTIONAL_CONFLICT;
}
Expand Down Expand Up @@ -458,7 +458,7 @@ NTSTATUS NTAPI SlimDetoursDetach(_Inout_ PVOID* ppPointer, _In_ PVOID pDetour)
SIZE_T sMem;
DWORD dwOld;

if (s_nPendingThreadId != CURRENT_THREAD_ID)
if (s_nPendingThreadId != NtGetCurrentThreadId())
{
return STATUS_TRANSACTIONAL_CONFLICT;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SlimDetours/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.Wintexports" version="1.0.12-alpha" targetFramework="native" />
<package id="KNSoft.NDK" version="1.0.1-alpha" targetFramework="native" />
</packages>
4 changes: 1 addition & 3 deletions Source/Test/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <Wintexports/Wintexports.h>

#pragma comment(lib, "ntdll.lib")
#include <KNSoft/NDK/NDK.h>

#include "../SlimDetours/SlimDetours.h"

Expand Down
10 changes: 2 additions & 8 deletions Source/Test/Test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
<ClCompile>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<OmitDefaultLibName>true</OmitDefaultLibName>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -127,7 +126,6 @@
<ClCompile>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<OmitDefaultLibName>true</OmitDefaultLibName>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -140,7 +138,6 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<OmitDefaultLibName>true</OmitDefaultLibName>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -155,7 +152,6 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<OmitDefaultLibName>true</OmitDefaultLibName>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -168,7 +164,6 @@
<ClCompile>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<OmitDefaultLibName>true</OmitDefaultLibName>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -181,7 +176,6 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<OmitDefaultLibName>true</OmitDefaultLibName>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -198,12 +192,12 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\KNSoft.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets" Condition="Exists('..\packages\KNSoft.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets')" />
<Import Project="..\packages\KNSoft.NDK.1.0.1-alpha\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.0.1-alpha\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.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.Wintexports.1.0.12-alpha\build\KNSoft.Wintexports.targets'))" />
<Error Condition="!Exists('..\packages\KNSoft.NDK.1.0.1-alpha\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.0.1-alpha\build\KNSoft.NDK.targets'))" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion Source/Test/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.Wintexports" version="1.0.12-alpha" targetFramework="native" />
<package id="KNSoft.NDK" version="1.0.1-alpha" targetFramework="native" />
</packages>

0 comments on commit 967aa9b

Please sign in to comment.