From e6aa690e759c5d5805dfe3777af5c65732c34fc4 Mon Sep 17 00:00:00 2001 From: TianZer <41182852+TianZerL@users.noreply.github.com> Date: Sat, 27 Apr 2024 21:10:40 +0800 Subject: [PATCH] fix possible crashes in DLL under Windows (#98) --- ruapu.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ruapu.h b/ruapu.h index a1a4488..0b5ff35 100644 --- a/ruapu.h +++ b/ruapu.h @@ -38,11 +38,28 @@ static int ruapu_detect_isa(const void* some_inst) return 0; } #else // WINAPI_FAMILY == WINAPI_FAMILY_APP +typedef const void* ruapu_some_inst; +#if defined (_MSC_VER) // MSVC +static int ruapu_detect_isa(const void* some_inst) +{ + int g_ruapu_sigill_caught = 0; + + __try + { + ((void (*)())some_inst)(); + } + __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION ? + EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) + { + g_ruapu_sigill_caught = 1; + } + + return g_ruapu_sigill_caught ? 0 : 1; +} +#else static int g_ruapu_sigill_caught = 0; static jmp_buf g_ruapu_jmpbuf; -typedef const void* ruapu_some_inst; - static LONG CALLBACK ruapu_catch_sigill(struct _EXCEPTION_POINTERS* ExceptionInfo) { if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION) @@ -69,6 +86,7 @@ static int ruapu_detect_isa(const void* some_inst) return g_ruapu_sigill_caught ? 0 : 1; } +#endif #endif // WINAPI_FAMILY == WINAPI_FAMILY_APP #elif defined __ANDROID__ || defined __linux__ || defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __DragonFly__ || defined __sun__ @@ -111,7 +129,7 @@ static int ruapu_detect_isa(ruapu_some_inst some_inst) return g_ruapu_sigill_caught ? 0 : 1; } -#elif defined __SYTERKIT__ +#elif defined __SYTERKIT__ typedef void (*ruapu_some_inst)(); #include @@ -519,7 +537,7 @@ void ruapu_init() g_ruapu_isa_supported[j] = 0; #elif defined __openrisc__ ruapu_detect_openrisc_isa(); -#else +#else // initialize g_ruapu_isa_map for baremetal here, default all zero // there is still ruapu_some_XYZ() functions available // but you have to work out your own signal handling