* Reduce number of thread locals in trap handling This commit refactors the trap handling portion of wasmtime with a few goals in mind. I've been reading around a bit lately and feel that we have a bit too few globals and thread locals floating around rather than handles attached to contexts. I'm hoping that we can reduce the number of thread locals and globals, and this commit is the start of reducing this number. The changes applied in this commit remove the set of thread locals in the `traphandlers` module in favor of one thread local that's managed in a sort of stack discipline. This way each call to `wasmtime_call*` sets up its own stack local state that can be managed and read on that stack frame. Additionally the C++ glue code around `setjmp` and `longjmp` has all been refactored to avoid going back and forth between Rust and C++. Now we'll simply enter C++, go straight into `setjmp`/the call, and then traps will enter Rust only once to both learn if the trap should be acted upon and record information about the trap. Overall the hope here is that context passing between `wasmtime_call*` and the trap handling function will be a bit easier. For example I hope to remove the global `get_trap_registry()` function next in favor of storing a handle to a registry inside each instance, and the `*mut VMContext` can be used to reach the `InstanceHandle` underneath, and this trap registry. * Update crates/runtime/src/traphandlers.rs Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com> Co-authored-by: Sergei Pepyakin <s.pepyakin@gmail.com>
55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
#ifndef signal_handlers_h
|
|
#define signal_handlers_h
|
|
|
|
#include <stdint.h>
|
|
#include <setjmp.h>
|
|
#ifndef __cplusplus
|
|
#include <stdbool.h>
|
|
#endif
|
|
|
|
#include <signal.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// Record the Trap code and wasm bytecode offset in TLS somewhere
|
|
void* RecordTrap(const uint8_t* pc, bool reset_guard_page);
|
|
|
|
#if defined(_WIN32)
|
|
#include <windows.h>
|
|
#include <winternl.h>
|
|
bool InstanceSignalHandler(LPEXCEPTION_POINTERS);
|
|
#elif defined(USE_APPLE_MACH_PORTS)
|
|
bool InstanceSignalHandler(int, siginfo_t *, void *);
|
|
#else
|
|
#include <sys/ucontext.h>
|
|
bool InstanceSignalHandler(int, siginfo_t *, ucontext_t *);
|
|
#endif
|
|
|
|
void Unwind(void*);
|
|
|
|
// This function performs the low-overhead signal handler initialization that we
|
|
// want to do eagerly to ensure a more-deterministic global process state. This
|
|
// is especially relevant for signal handlers since handler ordering depends on
|
|
// installation order: the wasm signal handler must run *before* the other crash
|
|
// handlers and since POSIX signal handlers work LIFO, this function needs to be
|
|
// called at the end of the startup process, after other handlers have been
|
|
// installed. This function can thus be called multiple times, having no effect
|
|
// after the first call.
|
|
int
|
|
EnsureEagerSignalHandlers(void);
|
|
|
|
// Assuming EnsureEagerProcessSignalHandlers() has already been called,
|
|
// this function performs the full installation of signal handlers which must
|
|
// be performed per-thread. This operation may incur some overhead and
|
|
// so should be done only when needed to use wasm.
|
|
int
|
|
EnsureDarwinMachPorts(void);
|
|
|
|
#ifdef __cplusplus
|
|
} // extern "C"
|
|
#endif
|
|
|
|
#endif // signal_handlers_h
|