From b43e712d27974403f77f4bf139ce484e759ae1d3 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Wed, 8 Sep 2021 10:16:51 +0200 Subject: [PATCH 1/2] fix(signals) Fix MacOS signal handling, especialy for M1 machines (for #2553) --- lib/vm/src/trap/handlers.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/vm/src/trap/handlers.c b/lib/vm/src/trap/handlers.c index 6ae17f72dff..fdba0032ff0 100644 --- a/lib/vm/src/trap/handlers.c +++ b/lib/vm/src/trap/handlers.c @@ -2,16 +2,25 @@ // Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md #include - +#include +#if defined(CFG_TARGET_OS_MACOS) +#include +#include +#include +#endif // Note that `sigsetjmp` and `siglongjmp` are used here where possible to // explicitly pass a 0 argument to `sigsetjmp` that we don't need to preserve // the process signal mask. This should make this call a bit faster b/c it // doesn't need to touch the kernel signal handling routines. // In case of macOS, stackoverflow -#if defined(CFG_TARGET_OS_WINDOWS) || defined(CFG_TARGET_OS_MACOS) +#if defined(CFG_TARGET_OS_WINDOWS) #define platform_setjmp(buf) setjmp(buf) #define platform_longjmp(buf, arg) longjmp(buf, arg) #define platform_jmp_buf jmp_buf +#elif defined(CFG_TARGET_OS_MACOS) +#define platform_setjmp(buf) sigsetjmp(buf, 1) +#define platform_longjmp(buf, arg) siglongjmp(buf, arg) +#define platform_jmp_buf sigjmp_buf #else #define platform_setjmp(buf) sigsetjmp(buf, 0) #define platform_longjmp(buf, arg) siglongjmp(buf, arg) @@ -22,6 +31,15 @@ int wasmer_register_setjmp( void **buf_storage, void (*body)(void*), void *payload) { + #if 0 && defined(CFG_TARGET_OS_MACOS) + // Enable this block to ba able to debug Segfault with lldb + // This will mask the EXC_BAD_ACCESS from lldb... + static int allow_bad_access = 0; + if(!allow_bad_access) { + allow_bad_access = 1; + task_set_exception_ports(mach_task_self(), EXC_MASK_BAD_ACCESS, MACH_PORT_NULL, EXCEPTION_DEFAULT, 0); + } + #endif platform_jmp_buf buf; if (platform_setjmp(buf) != 0) { return 0; From 3e520b671a91d109777809da221c57c77fc6df55 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 8 Sep 2021 12:12:55 +0200 Subject: [PATCH 2/2] Added extra comment --- lib/vm/src/trap/handlers.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/vm/src/trap/handlers.c b/lib/vm/src/trap/handlers.c index fdba0032ff0..ddee4b0c2f2 100644 --- a/lib/vm/src/trap/handlers.c +++ b/lib/vm/src/trap/handlers.c @@ -18,6 +18,8 @@ #define platform_longjmp(buf, arg) longjmp(buf, arg) #define platform_jmp_buf jmp_buf #elif defined(CFG_TARGET_OS_MACOS) +// TODO: This is not the most performant, since it adds overhead when calling functions +// https://github.com/wasmerio/wasmer/issues/2562 #define platform_setjmp(buf) sigsetjmp(buf, 1) #define platform_longjmp(buf, arg) siglongjmp(buf, arg) #define platform_jmp_buf sigjmp_buf