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>
This commit is contained in:
@@ -408,12 +408,11 @@ HandleTrap(CONTEXT* context, bool reset_guard_page)
|
||||
{
|
||||
assert(sAlreadyHandlingTrap);
|
||||
|
||||
if (!CheckIfTrapAtAddress(ContextToPC(context))) {
|
||||
return false;
|
||||
void *JmpBuf = RecordTrap(ContextToPC(context), reset_guard_page);
|
||||
if (JmpBuf == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RecordTrap(ContextToPC(context), reset_guard_page);
|
||||
|
||||
// Unwind calls longjmp, so it doesn't run the automatic
|
||||
// sAlreadhHanldingTrap cleanups, so reset it manually before doing
|
||||
// a longjmp.
|
||||
@@ -423,12 +422,13 @@ HandleTrap(CONTEXT* context, bool reset_guard_page)
|
||||
// Reroute the PC to run the Unwind function on the main stack after the
|
||||
// handler exits. This doesn't yet work for stack overflow traps, because
|
||||
// in that case the main thread doesn't have any space left to run.
|
||||
SetContextPC(context, reinterpret_cast<const uint8_t*>(&Unwind));
|
||||
assert(false); // this branch isn't implemented here
|
||||
// SetContextPC(context, reinterpret_cast<const uint8_t*>(&Unwind));
|
||||
#else
|
||||
// For now, just call Unwind directly, rather than redirecting the PC there,
|
||||
// so that it runs on the alternate signal handler stack. To run on the main
|
||||
// stack, reroute the context PC like this:
|
||||
Unwind();
|
||||
Unwind(JmpBuf);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user