Skip to content

Commit

Permalink
Merge #2560
Browse files Browse the repository at this point in the history
2560: fix(signals) Fix MacOS signal handling, especialy for M1 machines (fo… r=syrusakbary a=ptitSeb

…r #2553)

# Description
Fixed signal handling on macOS. need to use `siglongjmp` when restauring context after the signal is processed (with signal mask handling)
This fix the crash of some of the tests on m1 macs

Co-authored-by: ptitSeb <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
3 people authored Sep 8, 2021
2 parents 0069ecd + 3e520b6 commit 43456a2
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/vm/src/trap/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@
// Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md

#include <setjmp.h>

#include <stdio.h>
#if defined(CFG_TARGET_OS_MACOS)
#include <mach/task.h>
#include <mach/mach_init.h>
#include <mach/mach_port.h>
#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)
// 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
#else
#define platform_setjmp(buf) sigsetjmp(buf, 0)
#define platform_longjmp(buf, arg) siglongjmp(buf, arg)
Expand All @@ -22,6 +33,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;
Expand Down

0 comments on commit 43456a2

Please sign in to comment.