From 8f6aafe85973d7a1b23240a4c1195ff656bdb9a1 Mon Sep 17 00:00:00 2001 From: Ratin Gao Date: Thu, 25 Jan 2024 00:32:28 +0800 Subject: [PATCH] Minor improvements --- Source/SlimDetours/Disasm.c | 29 +++++++++-------------------- Source/SlimDetours/Memory.c | 7 +++++-- Source/SlimDetours/SlimDetours.h | 1 - Source/SlimDetours/SlimDetours.inl | 2 +- Source/SlimDetours/Thread.c | 2 -- Source/SlimDetours/Transaction.c | 2 +- Source/Test/Main.cpp | 13 +++++-------- Source/Test/Test.vcxproj | 12 ++++++------ 8 files changed, 27 insertions(+), 41 deletions(-) diff --git a/Source/SlimDetours/Disasm.c b/Source/SlimDetours/Disasm.c index 35cb4042..ded927fe 100644 --- a/Source/SlimDetours/Disasm.c +++ b/Source/SlimDetours/Disasm.c @@ -1579,21 +1579,6 @@ pp is like VEX but only instructions with 0 are defined } } -PVOID NTAPI SlimDetoursCopyInstruction( - _In_opt_ PVOID pDst, - _Inout_opt_ PVOID* ppDstPool, - _In_ PVOID pSrc, - _Out_opt_ PVOID* ppTarget, - _Out_opt_ LONG* plExtra) -{ - UNREFERENCED_PARAMETER(ppDstPool); // x86 & x64 don't use a constant pool. - - DETOUR_DISASM Disasm; - - detour_disasm_init(&Disasm, (PBYTE*)ppTarget, plExtra); - return CopyInstruction(&Disasm, (PBYTE)pDst, (PBYTE)pSrc); -} - #endif // defined(_M_X64) || defined(_M_IX86) #if defined(_M_ARM64) @@ -2324,23 +2309,27 @@ static PBYTE CopyInstruction(_In_ PDETOUR_DISASM pDisasm, _In_opt_ PBYTE pDst, _ return pSrc + 4; } +#endif // defined(_M_ARM64) + PVOID NTAPI SlimDetoursCopyInstruction( _In_opt_ PVOID pDst, - _Inout_opt_ PVOID* ppDstPool, _In_ PVOID pSrc, _Out_opt_ PVOID* ppTarget, _Out_opt_ LONG* plExtra) { - UNREFERENCED_PARAMETER(ppDstPool); - DETOUR_DISASM Disasm; +#if defined(_M_X64) || defined(_M_IX86) + detour_disasm_init(&Disasm, (PBYTE*)ppTarget, plExtra); + return (PVOID)CopyInstruction(&Disasm, (PBYTE)pDst, (PBYTE)pSrc); +#elif defined(_M_ARM64) detour_disasm_init(&Disasm); return (PVOID)CopyInstruction(&Disasm, (PBYTE)pDst, (PBYTE)pSrc, (PBYTE*)ppTarget, plExtra); +#else + return NULL; +#endif } - -#endif // defined(_M_ARM64) diff --git a/Source/SlimDetours/Memory.c b/Source/SlimDetours/Memory.c index a0571804..6f1f276a 100644 --- a/Source/SlimDetours/Memory.c +++ b/Source/SlimDetours/Memory.c @@ -79,13 +79,16 @@ VOID detour_memory_init() } #endif - g_hHeap = RtlCreateHeap(HEAP_NO_SERIALIZE | HEAP_GROWABLE, NULL, 0, 0, NULL, NULL); + if (g_hHeap == NULL) + { + g_hHeap = RtlCreateHeap(HEAP_NO_SERIALIZE | HEAP_GROWABLE, NULL, 0, 0, NULL, NULL); + } } _Must_inspect_result_ _Ret_maybenull_ _Post_writable_byte_size_(Size) -PVOID detour_memory_alloc(_In_range_(>, 0) SIZE_T Size) +PVOID detour_memory_alloc(_In_ SIZE_T Size) { if (g_hHeap == NULL) { diff --git a/Source/SlimDetours/SlimDetours.h b/Source/SlimDetours/SlimDetours.h index c0ae0a85..52bd7a5d 100644 --- a/Source/SlimDetours/SlimDetours.h +++ b/Source/SlimDetours/SlimDetours.h @@ -52,7 +52,6 @@ NTSTATUS NTAPI SlimDetoursDelayAttach( PVOID NTAPI SlimDetoursCodeFromPointer(_In_ PVOID pPointer); PVOID NTAPI SlimDetoursCopyInstruction( _In_opt_ PVOID pDst, - _Inout_opt_ PVOID* ppDstPool, _In_ PVOID pSrc, _Out_opt_ PVOID* ppTarget, _Out_opt_ LONG* plExtra); diff --git a/Source/SlimDetours/SlimDetours.inl b/Source/SlimDetours/SlimDetours.inl index ccc250d5..7baa7229 100644 --- a/Source/SlimDetours/SlimDetours.inl +++ b/Source/SlimDetours/SlimDetours.inl @@ -124,7 +124,7 @@ VOID detour_memory_init(); _Must_inspect_result_ _Ret_maybenull_ _Post_writable_byte_size_(Size) -PVOID detour_memory_alloc(_In_range_(> , 0) SIZE_T Size); +PVOID detour_memory_alloc(_In_ SIZE_T Size); BOOL detour_memory_free(_Frees_ptr_ PVOID BaseAddress); diff --git a/Source/SlimDetours/Thread.c b/Source/SlimDetours/Thread.c index 410440a4..8c3ea74e 100644 --- a/Source/SlimDetours/Thread.c +++ b/Source/SlimDetours/Thread.c @@ -139,7 +139,6 @@ NTSTATUS detour_thread_update(_In_ HANDLE ThreadHandle, _In_ PDETOUR_OPERATION P if (cxt.CONTEXT_PC >= (ULONG_PTR)o->pTrampoline && cxt.CONTEXT_PC < ((ULONG_PTR)o->pTrampoline + sizeof(o->pTrampoline))) { - cxt.CONTEXT_PC = (ULONG_PTR)o->pbTarget + detour_align_from_trampoline(o->pTrampoline, (BYTE)(cxt.CONTEXT_PC - (ULONG_PTR)o->pTrampoline)); bUpdateContext = TRUE; @@ -149,7 +148,6 @@ NTSTATUS detour_thread_update(_In_ HANDLE ThreadHandle, _In_ PDETOUR_OPERATION P if (cxt.CONTEXT_PC >= (ULONG_PTR)o->pbTarget && cxt.CONTEXT_PC < ((ULONG_PTR)o->pbTarget + o->pTrampoline->cbRestore)) { - cxt.CONTEXT_PC = (ULONG_PTR)o->pTrampoline + detour_align_from_target(o->pTrampoline, (BYTE)(cxt.CONTEXT_PC - (ULONG_PTR)o->pbTarget)); bUpdateContext = TRUE; diff --git a/Source/SlimDetours/Transaction.c b/Source/SlimDetours/Transaction.c index 48d77725..8e731e89 100644 --- a/Source/SlimDetours/Transaction.c +++ b/Source/SlimDetours/Transaction.c @@ -323,7 +323,7 @@ NTSTATUS NTAPI SlimDetoursAttach(_Inout_ PVOID* ppPointer, _In_ PVOID pDetour) LONG lExtra = 0; DETOUR_TRACE(" SlimDetoursCopyInstruction(%p,%p)\n", pbTrampoline, pbSrc); - pbSrc = (PBYTE)SlimDetoursCopyInstruction(pbTrampoline, (PVOID*)&pbPool, pbSrc, NULL, &lExtra); + pbSrc = (PBYTE)SlimDetoursCopyInstruction(pbTrampoline, pbSrc, NULL, &lExtra); DETOUR_TRACE(" SlimDetoursCopyInstruction() = %p (%d bytes)\n", pbSrc, (int)(pbSrc - pbOp)); pbTrampoline += (pbSrc - pbOp) + lExtra; cbTarget = PtrOffset(pbTarget, pbSrc); diff --git a/Source/Test/Main.cpp b/Source/Test/Main.cpp index 554a0dff..e749da78 100644 --- a/Source/Test/Main.cpp +++ b/Source/Test/Main.cpp @@ -49,11 +49,7 @@ static VOID CALLBACK Delay_attach_proc( } } -int WINAPI wWinMain( - _In_ HINSTANCE hInstance, - _In_opt_ HINSTANCE hPrevInstance, - _In_ LPWSTR lpCmdLine, - _In_ int nShowCmd) +int wmain() { NTSTATUS Status; PVOID User32Base; @@ -61,14 +57,15 @@ int WINAPI wWinMain( #if 1 // Test delay attach FN_MessageBoxW* pfnMessageBoxW; - if (!NT_SUCCESS(SlimDetoursDelayAttach((PVOID*)&g_pfnMessageBoxW, + Status = SlimDetoursDelayAttach((PVOID*)&g_pfnMessageBoxW, Hooked_MessageBoxW, g_usUser32.Buffer, g_asMessageBoxW.Buffer, Delay_attach_proc, - NULL))) + NULL); + if (!NT_SUCCESS(Status)) { - return STATUS_UNSUCCESSFUL; + return Status; } if (!NT_SUCCESS(LdrLoadDll(NULL, NULL, &g_usUser32, &User32Base)) || diff --git a/Source/Test/Test.vcxproj b/Source/Test/Test.vcxproj index 12cdb4ec..034453b9 100644 --- a/Source/Test/Test.vcxproj +++ b/Source/Test/Test.vcxproj @@ -119,7 +119,7 @@ true - Windows + Console @@ -130,7 +130,7 @@ true - Windows + Console @@ -143,7 +143,7 @@ true - Windows + Console true true @@ -158,7 +158,7 @@ true - Windows + Console true true @@ -171,7 +171,7 @@ true - Windows + Console @@ -184,7 +184,7 @@ true - Windows + Console true true