Skip to content

Commit

Permalink
Implement InterlockedCompareExchange w/ __asm
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlyanon committed May 1, 2024
1 parent 63c4752 commit 3a96083
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions wrappers/kernel32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3a96083

Please sign in to comment.