Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement InterlockedCompareExchange using CAS #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions wrappers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,48 @@ cmake_minimum_required(VERSION 3.13)

project(corkel32 C)

set(CMAKE_BUILD_TYPE Release)
add_compile_definitions(_WIN32_WINNT=0x0400)

add_library(corkdebug OBJECT
add_library(
corkdebug OBJECT
debug.c
)
link_libraries(corkdebug)
target_compile_definitions(corkdebug PRIVATE _CRT_SECURE_NO_WARNINGS=1)

add_library(
detect486 OBJECT
detect486.c
)

include(CheckSymbolExists)
check_symbol_exists(RtlUnwind Windows.h HAS_RTL_UNWIND)

# KERNEL32 => CORKEL32
add_library(corkel32 SHARED
add_library(
corkel32 MODULE
kernel32.c
advapi32.c
comdlg32.c
corkel32.def
)
target_link_libraries(corkel32 PRIVATE corkdebug detect486)
target_compile_definitions(corkel32 PRIVATE _CRT_SECURE_NO_WARNINGS=1)

# NTDLL => CORNT
add_library(cornt SHARED
add_library(
cornt MODULE
ntdll.c
cornt.def
)
target_link_libraries(cornt PRIVATE corkdebug)
if(HAS_RTL_UNWIND)
target_compile_definitions(cornt PRIVATE HAS_RTL_UNWIND=1)
endif()

# USER32 => CORUSR
add_library(corusr SHARED
add_library(
corusr MODULE
user32.c
corusr.def
)
target_link_libraries(corusr PRIVATE corkdebug)
1 change: 0 additions & 1 deletion wrappers/advapi32.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define _WIN32_WINNT 0x0400
#include <windows.h>

#include "debug.h"
Expand Down
1 change: 0 additions & 1 deletion wrappers/comdlg32.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define _WIN32_WINNT 0x0400
#include <windows.h>

#include "debug.h"
Expand Down
26 changes: 26 additions & 0 deletions wrappers/detect486.c
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 6 additions & 0 deletions wrappers/detect486.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DETECT_486
#define DETECT_486

int is_cpu_486_or_newer(void);

#endif // DETECT_486
50 changes: 39 additions & 11 deletions wrappers/kernel32.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#define _WIN32_WINNT 0x0400

#include <stdio.h>
#include <windows.h>

#include "debug.h"
#include "detect486.h"

static int has_cmpxchg = 0;

DWORD WINAPI CORKEL32_GetLastError()
{
Expand Down Expand Up @@ -153,18 +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 = *dest;

Trace(TRACE_FORCE_DONT_PRINT, "InterlockedCompareExchange");

if (compare == *dest) {
*dest = xchg;
__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
}

return temp;
}

HANDLE WINAPI CORKEL32_CreateToolhelp32Snapshot(DWORD param_0, DWORD param_1)
Expand Down Expand Up @@ -303,3 +322,12 @@ BOOL WINAPI CORKEL32_InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION cr

return TRUE;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH) {
has_cmpxchg = is_cpu_486_or_newer();
}

return TRUE;
}
7 changes: 5 additions & 2 deletions wrappers/ntdll.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <lm.h> // for NTSTATUS

#include "debug.h"

ULONG WINAPI CORNT_RtlNtStatusToDosError(NTSTATUS)
ULONG WINAPI CORNT_RtlNtStatusToDosError(NTSTATUS param_0)
{
Trace(TRACE_UNIMPLEMENTED, "RtlNtStatusToDosError");
// TODO: Stub
return 0;
}

#ifndef HAS_RTL_UNWIND
extern NTAPI RtlUnwind(void* param_0, void* param_1, struct _EXCEPTION_RECORD* param_2, void* param_3);
#endif

VOID NTAPI CORNT_RtlUnwind(void *p0,void *p1,struct _EXCEPTION_RECORD *p2, void *p3)
{
Trace(TRACE_PASSTHROUGH, "RtlUnwind");
Expand Down
1 change: 0 additions & 1 deletion wrappers/user32.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define _WIN32_WINNT 0x0400
#include <windows.h>

#include "debug.h"
Expand Down