Skip to content

Commit

Permalink
Check ex handling support
Browse files Browse the repository at this point in the history
  • Loading branch information
kynex7510 committed Nov 3, 2024
1 parent c99b47d commit da9238b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions Include/CTRL/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 10 additions & 6 deletions Source/Exception.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "CTRL/Exception.h"
#include "CTRL/Env.h"

#define TLS_EX_HANDLER_SLOT 16
#define TLS_EX_STACK_SLOT 17
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
26 changes: 19 additions & 7 deletions Tests/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit da9238b

Please sign in to comment.