From 3a96083adab9a0c74bd25000ffd5a5d5e81e2081 Mon Sep 17 00:00:00 2001 From: friendlyanon Date: Wed, 1 May 2024 18:17:50 +0200 Subject: [PATCH] Implement `InterlockedCompareExchange` w/ `__asm` --- wrappers/kernel32.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/wrappers/kernel32.c b/wrappers/kernel32.c index 25e777f..f3be02f 100644 --- a/wrappers/kernel32.c +++ b/wrappers/kernel32.c @@ -154,33 +154,38 @@ BOOL WINAPI CORKEL32_DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVO return e; } -static LONG __declspec(naked) WINAPI InterlockedCompareExchange_486(LONG* dest, LONG xchg, LONG compare) +static char const* InterlockedCompareExchange_name = "InterlockedCompareExchange"; + +// Reimplemented +__declspec(naked) LONG WINAPI CORKEL32_InterlockedCompareExchange(LONG* dest, LONG exchange, LONG compare) { __asm { - mov ecx, DWORD PTR [esp + 4] ; dest - mov edx, DWORD PTR [esp + 8] ; xchg + 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 [ecx], edx + mov ebx, DWORD PTR [edx] + lock cmpxchg DWORD PTR [edx], ecx + mov eax, ebx ret 12 - } -} -// Reimplemented -LONG WINAPI CORKEL32_InterlockedCompareExchange(LONG* dest, LONG xchg, LONG compare) -{ - LONG temp; - if (has_cmpxchg) { - return InterlockedCompareExchange_486(dest, xchg, compare); - } + ICE_is_386: + push InterlockedCompareExchange_name + push TRACE_FORCE_DONT_PRINT + call Trace + add esp, 8 - temp = *dest; - Trace(TRACE_FORCE_DONT_PRINT, "InterlockedCompareExchange"); + mov edx, DWORD PTR [esp + 4] ; dest + mov eax, DWORD PTR [edx] + cmp DWORD PTR [esp + 12], eax ; compare + jne ICE_exit + mov ecx, DWORD PTR [esp + 8] ; exchange + mov DWORD PTR [edx], ecx - if (compare == *dest) { - *dest = xchg; + ICE_exit: + ret 12 } - - return temp; } HANDLE WINAPI CORKEL32_CreateToolhelp32Snapshot(DWORD param_0, DWORD param_1)