From da9238bf761c8893c5a4bfe337dd2b34a7252947 Mon Sep 17 00:00:00 2001 From: kynex7510 Date: Sun, 3 Nov 2024 22:36:50 +0100 Subject: [PATCH] Check ex handling support --- Include/CTRL/Exception.h | 1 + Source/Exception.c | 16 ++++++++++------ Tests/Main.c | 26 +++++++++++++++++++------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Include/CTRL/Exception.h b/Include/CTRL/Exception.h index fc434e0..636cf05 100644 --- a/Include/CTRL/Exception.h +++ b/Include/CTRL/Exception.h @@ -13,6 +13,7 @@ typedef void(*CTRLExHandlerFn)(ERRF_ExceptionData*); bool ctrlEnableExceptionHandling(void); void ctrlDisableExceptionHandling(void); +bool ctrlExceptionHandlingIsSupported(void); bool ctrlSetExceptionHandler(CTRLExHandlerFn fn, size_t index); bool ctrlClearExceptionHandler(size_t index); diff --git a/Source/Exception.c b/Source/Exception.c index 11be6d4..86c1713 100644 --- a/Source/Exception.c +++ b/Source/Exception.c @@ -1,4 +1,5 @@ #include "CTRL/Exception.h" +#include "CTRL/Env.h" #define TLS_EX_HANDLER_SLOT 16 #define TLS_EX_STACK_SLOT 17 @@ -29,19 +30,22 @@ static void ctrl_disableExHandlingImpl(u32* tls) { } bool ctrlEnableExceptionHandling(void) { - u32* tls = (u32*)getThreadLocalStorage(); - if (ctrl_hasExHandlers(tls)) { - ctrl_enableExHandlingImpl(tls); - return true; + if (ctrlExceptionHandlingIsSupported()) { + u32* tls = (u32*)getThreadLocalStorage(); + if (ctrl_hasExHandlers(tls)) { + ctrl_enableExHandlingImpl(tls); + return true; + } } return false; } void ctrlDisableExceptionHandling(void) { ctrl_disableExHandlingImpl((u32*)getThreadLocalStorage()); } +bool ctrlExceptionHandlingIsSupported(void) { return ctrlDetectEnv() != Env_Citra; } bool ctrlSetExceptionHandler(CTRLExHandlerFn fn, size_t index) { - if (index >= CTRL_MAX_EX_HANDLERS) + if (!ctrlExceptionHandlingIsSupported() || (index >= CTRL_MAX_EX_HANDLERS)) return false; u32* tls = (u32*)getThreadLocalStorage(); @@ -53,7 +57,7 @@ bool ctrlSetExceptionHandler(CTRLExHandlerFn fn, size_t index) { } bool ctrlClearExceptionHandler(size_t index) { - if (index >= CTRL_MAX_EX_HANDLERS) + if (!ctrlExceptionHandlingIsSupported() || (index >= CTRL_MAX_EX_HANDLERS)) return false; u32* tls = (u32*)getThreadLocalStorage(); diff --git a/Tests/Main.c b/Tests/Main.c index 9074e15..58d160d 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -25,14 +25,26 @@ static void exHandler1(ERRF_ExceptionData* info) { static bool test1(void) { printf("=== EXCEPTION HANDLER TEST ===\n"); - ctrlSetExceptionHandler(exHandler1, CTRL_MAX_EX_HANDLERS - 1); - ctrlSetExceptionHandler(exHandler0, 0); - - asm("mov r0, #0"); - asm("ldr r0, [r0]"); + if (ctrlExceptionHandlingIsSupported()) { + if (!ctrlSetExceptionHandler(exHandler1, CTRL_MAX_EX_HANDLERS - 1)) { + printf("FAILED: could not set exception handler\n"); + return false; + } + + if (!ctrlSetExceptionHandler(exHandler0, 0)) { + printf("FAILED: could not set exception handler\n"); + return false; + } + + asm("mov r0, #0"); + asm("ldr r0, [r0]"); + + ctrlDisableExceptionHandling(); + printf("SUCCESS\n"); + } else { + printf("SKIPPED (not supported)\n"); + } - ctrlDisableExceptionHandling(); - printf("SUCCESS\n"); return true; }