Files
wasmtime/crates/runtime/signalhandlers/Trampolines.cpp
Alex Crichton 05d6c27142 Reduce number of thread locals in trap handling (#852)
* 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>
2020-01-23 14:34:47 -06:00

38 lines
679 B
C++

#include <setjmp.h>
#include "SignalHandlers.hpp"
extern "C"
int WasmtimeCallTrampoline(
void **buf_storage,
void *vmctx,
void *caller_vmctx,
void (*body)(void*, void*, void*),
void *args)
{
jmp_buf buf;
if (setjmp(buf) != 0) {
return 0;
}
*buf_storage = &buf;
body(vmctx, caller_vmctx, args);
return 1;
}
extern "C"
int WasmtimeCall(void **buf_storage, void *vmctx, void *caller_vmctx, void (*body)(void*, void*)) {
jmp_buf buf;
if (setjmp(buf) != 0) {
return 0;
}
*buf_storage = &buf;
body(vmctx, caller_vmctx);
return 1;
}
extern "C"
void Unwind(void *JmpBuf) {
jmp_buf *buf = (jmp_buf*) JmpBuf;
longjmp(*buf, 1);
}