From 0f045407d9af3baf835ef61c62f031d9877e1840 Mon Sep 17 00:00:00 2001 From: friendlyanon Date: Wed, 1 May 2024 16:14:31 +0200 Subject: [PATCH] Implement assembly functions using inline `__asm` This avoids having to rely on MASM. --- wrappers/CMakeLists.txt | 11 +++-------- wrappers/cas.asm | 17 ----------------- wrappers/cas.h | 10 ---------- wrappers/detect486.asm | 28 ---------------------------- wrappers/detect486.c | 26 ++++++++++++++++++++++++++ wrappers/detect486.h | 6 +----- wrappers/kernel32.c | 41 +++++++++++++++++++++++++++-------------- 7 files changed, 57 insertions(+), 82 deletions(-) delete mode 100644 wrappers/cas.asm delete mode 100644 wrappers/cas.h delete mode 100644 wrappers/detect486.asm create mode 100644 wrappers/detect486.c diff --git a/wrappers/CMakeLists.txt b/wrappers/CMakeLists.txt index 402974c..2fa8548 100644 --- a/wrappers/CMakeLists.txt +++ b/wrappers/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13) -project(corkel32 C ASM_MASM) +project(corkel32 C) add_compile_definitions(_WIN32_WINNT=0x0400) @@ -12,12 +12,7 @@ target_compile_definitions(corkdebug PRIVATE _CRT_SECURE_NO_WARNINGS=1) add_library( detect486 OBJECT - detect486.asm -) - -add_library( - cas OBJECT - cas.asm + detect486.c ) include(CheckSymbolExists) @@ -31,7 +26,7 @@ add_library( comdlg32.c corkel32.def ) -target_link_libraries(corkel32 PRIVATE corkdebug detect486 cas) +target_link_libraries(corkel32 PRIVATE corkdebug detect486) target_compile_definitions(corkel32 PRIVATE _CRT_SECURE_NO_WARNINGS=1) # NTDLL => CORNT diff --git a/wrappers/cas.asm b/wrappers/cas.asm deleted file mode 100644 index 6e95593..0000000 --- a/wrappers/cas.asm +++ /dev/null @@ -1,17 +0,0 @@ -.486 -.MODEL FLAT - -.CODE - -OPTION PROLOGUE:NONE -OPTION EPILOGUE:NONE - -_InterlockedCompareExchange_486@12 PROC - mov ecx, DWORD PTR [esp + 4] ; dest - mov edx, DWORD PTR [esp + 8] ; exchange - mov eax, DWORD PTR [esp + 12] ; compare - lock cmpxchg DWORD PTR [ecx], edx - ret 12 -_InterlockedCompareExchange_486@12 ENDP - -END diff --git a/wrappers/cas.h b/wrappers/cas.h deleted file mode 100644 index c2a0ef3..0000000 --- a/wrappers/cas.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef CAS -#define CAS - -#ifndef STDCALL -#define STDCALL __stdcall -#endif - -long STDCALL InterlockedCompareExchange_486(long* dest, long exchange, long compare); - -#endif // CAS diff --git a/wrappers/detect486.asm b/wrappers/detect486.asm deleted file mode 100644 index dbd4686..0000000 --- a/wrappers/detect486.asm +++ /dev/null @@ -1,28 +0,0 @@ -.386 -.MODEL FLAT, STDCALL - -.CODE - -is_cpu_486_or_newer PROC - pushfd - pop eax - mov ebx, eax - xor eax, 40000h ; toggle the AC bit in EFLAGS (only available in 486 or newer) - push eax - popfd - pushfd - pop eax - cmp eax, ebx - jz is_386 - push ebx - popfd - xor eax, eax - inc eax - ret - -is_386: - xor eax, eax - ret -is_cpu_486_or_newer ENDP - -END diff --git a/wrappers/detect486.c b/wrappers/detect486.c new file mode 100644 index 0000000..9bfe773 --- /dev/null +++ b/wrappers/detect486.c @@ -0,0 +1,26 @@ +#include "detect486.h" + +__declspec(naked) int is_cpu_486_or_newer(void) +{ + __asm { + pushfd + pop eax + mov ebx, eax + xor eax, 40000h ; toggle the AC bit in EFLAGS (only available in 486 or newer) + push eax + popfd + pushfd + pop eax + cmp eax, ebx + jz is_386 + push ebx + popfd + xor eax, eax + inc eax + ret + + is_386: + xor eax, eax + ret + } +} diff --git a/wrappers/detect486.h b/wrappers/detect486.h index 2670d23..c5522fe 100644 --- a/wrappers/detect486.h +++ b/wrappers/detect486.h @@ -1,10 +1,6 @@ #ifndef DETECT_486 #define DETECT_486 -#ifndef STDCALL -#define STDCALL __stdcall -#endif - -int STDCALL is_cpu_486_or_newer(void); +int is_cpu_486_or_newer(void); #endif // DETECT_486 diff --git a/wrappers/kernel32.c b/wrappers/kernel32.c index aa3d271..c1c045a 100644 --- a/wrappers/kernel32.c +++ b/wrappers/kernel32.c @@ -1,7 +1,6 @@ #include #include -#include "cas.h" #include "debug.h" #include "detect486.h" @@ -155,22 +154,36 @@ BOOL WINAPI CORKEL32_DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVO return e; } +static char const* InterlockedCompareExchange_name = "InterlockedCompareExchange"; + // Reimplemented -LONG WINAPI CORKEL32_InterlockedCompareExchange(LONG* dest, LONG xchg, LONG compare) +__declspec(naked) LONG WINAPI CORKEL32_InterlockedCompareExchange(LONG* dest, LONG exchange, LONG compare) { - LONG temp; - if (has_cmpxchg) { - return InterlockedCompareExchange_486(dest, xchg, compare); + __asm { + cmp has_cmpxchg, 0 + jz ICE_is_386 + mov edx, DWORD PTR [esp + 4] ; dest + mov ecx, DWORD PTR [esp + 8] ; exchange + mov eax, DWORD PTR [esp + 12] ; compare + lock cmpxchg DWORD PTR [edx], ecx + ret 12 + + ICE_is_386: + push InterlockedCompareExchange_name + push TRACE_FORCE_DONT_PRINT + call Trace + add esp, 8 + + mov edx, DWORD PTR [esp + 4] ; dest + mov eax, DWORD PTR [edx] + cmp eax, DWORD PTR [esp + 12] ; compare + jne ICE_exit + mov ecx, DWORD PTR [esp + 8] ; exchange + mov DWORD PTR [edx], ecx + + ICE_exit: + ret 12 } - - temp = *dest; - Trace(TRACE_FORCE_DONT_PRINT, "InterlockedCompareExchange"); - - if (compare == *dest) { - *dest = xchg; - } - - return temp; } HANDLE WINAPI CORKEL32_CreateToolhelp32Snapshot(DWORD param_0, DWORD param_1)