Skip to content

Commit

Permalink
add SetUnhandledExceptionFilter support to win32 so that crash dump c…
Browse files Browse the repository at this point in the history
…an work as expected
  • Loading branch information
longhun12346 committed May 6, 2024
1 parent a38c14e commit afb9310
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
44 changes: 36 additions & 8 deletions src/koffi/src/abi_x86.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,37 @@ struct BackRegisters {
int ret_pop;
};

extern "C" uint64_t ForwardCallG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
extern "C" double ForwardCallD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
extern "C" uint64_t ForwardCallRG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
extern "C" float ForwardCallRF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
extern "C" double ForwardCallRD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
extern "C" uint64_t ForwardCallG(const void *func, uint8_t *sp, uint8_t **out_old_sp, Size len);
extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_old_sp, Size len);
extern "C" double ForwardCallD(const void *func, uint8_t *sp, uint8_t **out_old_sp, Size len);
extern "C" uint64_t ForwardCallRG(const void *func, uint8_t *sp, uint8_t **out_old_sp, Size len);
extern "C" float ForwardCallRF(const void *func, uint8_t *sp, uint8_t **out_old_sp, Size len);
extern "C" double ForwardCallRD(const void *func, uint8_t *sp, uint8_t **out_old_sp, Size len);
extern "C" void ForwardCall(const void* func, Size len)
{
__try
{
__asm
{
sub esp, 12
sub esp, len
mov edi, esp
mov esi, ebp
add esi, 24
mov ecx, len
shr ecx, 2
cld
rep movsd
call func
add esp, len
add esp, 12
}
}
__except (UnhandledExceptionFilter(GetExceptionInformation()))
{
ExitProcess(GetExceptionCode());
}
}

extern "C" napi_value CallSwitchStack(Napi::Function *func, size_t argc, napi_value *argv,
uint8_t *old_sp, Span<uint8_t> *new_stack,
Expand Down Expand Up @@ -134,6 +159,8 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
uint32_t *args_ptr = nullptr;
uint32_t *fast_ptr = nullptr;

uint8_t* sp = mem->stack.end();

// Pass return value in register or through memory
if (!AllocStack(func->args_size, 16, &args_ptr)) [[unlikely]]
return false;
Expand Down Expand Up @@ -316,6 +343,7 @@ bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
#undef PUSH_INTEGER_32

new_sp = mem->stack.end();
len = sp - new_sp;

return true;
}
Expand Down Expand Up @@ -355,8 +383,8 @@ void CallData::Execute(const FunctionInfo *func, void *native)

#define PERFORM_CALL(Suffix) \
([&]() { \
auto ret = (func->fast ? ForwardCallR ## Suffix(native, new_sp, &old_sp) \
: ForwardCall ## Suffix(native, new_sp, &old_sp)); \
auto ret = (func->fast ? ForwardCallR ## Suffix(native, new_sp, &old_sp, len) \
: ForwardCall ## Suffix(native, new_sp, &old_sp, len)); \
return ret; \
})()

Expand Down
23 changes: 17 additions & 6 deletions src/koffi/src/abi_x86_asm.asm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public ForwardCallRF
public ForwardCallRD

.model flat, C

.data
extern ForwardCall:proc

.code

; Copy function pointer to EAX, in order to save it through argument forwarding.
Expand All @@ -38,25 +42,32 @@ public ForwardCallRD
prologue macro
endbr32
push ebp
push esi
mov ebp, esp
mov eax, dword ptr [esp+16]
mov eax, dword ptr [esp+20]
mov dword ptr [eax+0], esp
mov eax, dword ptr [esp+8]
mov esp, dword ptr [esp+12]
mov eax, dword ptr [esp+12]
mov esi, dword ptr [esp+24]
mov esp, dword ptr [esp+16]
endm

fastcall macro
mov ecx, dword ptr [esp+0]
mov edx, dword ptr [esp+4]
mov ecx, dword ptr [esp+4]
mov edx, dword ptr [esp+8]
add esp, 16
endm

; Call native function.
; Once done, restore normal stack pointer and return.
; The return value is passed back untouched.
epilogue macro
call eax
sub esp, 8
push esi
push eax
call ForwardCall
add esp, 16
mov esp, ebp
pop esi
pop ebp
ret
endm
Expand Down
1 change: 1 addition & 0 deletions src/koffi/src/call.hh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class alignas(8) CallData {

uint8_t *new_sp;
uint8_t *old_sp;
Size len;

union {
int8_t i8;
Expand Down

0 comments on commit afb9310

Please sign in to comment.