Merge branch 'fix/llvm-trap-windows' into feature/llvm-osr

This commit is contained in:
losfair
2019-08-21 15:02:17 -07:00
2 changed files with 61 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#include "object_loader.hh"
#include <iostream>
#include <memory>
#include <setjmp.h>
extern "C" void __register_frame(uint8_t *);
extern "C" void __deregister_frame(uint8_t *);
@ -12,6 +13,54 @@ MemoryManager::~MemoryManager() {
callbacks.dealloc_memory(read_section.base, read_section.size);
callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size);
}
void unwinding_setjmp(jmp_buf stack_out, void (*func)(void *), void *userdata) {
if(setjmp(stack_out)) {
} else {
func(userdata);
}
}
[[noreturn]]
void unwinding_longjmp(jmp_buf stack_in) {
longjmp(stack_in, 42);
}
struct UnwindPoint {
UnwindPoint *prev;
jmp_buf stack;
std::function<void()> *f;
std::unique_ptr<std::exception> exception;
};
static thread_local UnwindPoint *unwind_state = nullptr;
static void unwind_payload(void *_point) {
UnwindPoint *point = (UnwindPoint *) _point;
(*point->f)();
}
void catch_unwind(std::function<void()>&& f) {
UnwindPoint current;
current.prev = unwind_state;
current.f = &f;
unwind_state = &current;
unwinding_setjmp(current.stack, unwind_payload, (void *) &current);
if(current.exception) {
throw *current.exception;
}
}
void unsafe_unwind(std::exception *exception) {
UnwindPoint *state = unwind_state;
if(state) {
state->exception.reset(exception);
unwinding_longjmp(state->stack);
} else {
abort();
}
}
uint8_t *MemoryManager::allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id,

View File

@ -5,6 +5,8 @@
#include <exception>
#include <iostream>
#include <sstream>
#include <setjmp.h>
#include <functional>
#include <llvm/ExecutionEngine/RuntimeDyld.h>
@ -104,11 +106,14 @@ private:
size_t stack_map_size = 0;
};
struct WasmException {
struct WasmException: std::exception {
public:
virtual std::string description() const noexcept = 0;
};
void catch_unwind(std::function<void()>&& f);
[[noreturn]] void unsafe_unwind(std::exception *exception);
struct UncatchableException : WasmException {
public:
virtual std::string description() const noexcept override {
@ -234,27 +239,29 @@ result_t module_load(const uint8_t *mem_ptr, size_t mem_size,
return RESULT_OK;
}
[[noreturn]] void throw_trap(WasmTrap::Type ty) { throw WasmTrap(ty); }
[[noreturn]] void throw_trap(WasmTrap::Type ty) { unsafe_unwind(new WasmTrap(ty)); }
void module_delete(WasmModule *module) { delete module; }
// Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust
// side.
[[noreturn]] void throw_any(size_t data, size_t vtable) {
throw UserException(data, vtable);
unsafe_unwind(new UserException(data, vtable));
}
// Throw a pointer that's assumed to be codegen::BreakpointHandler on the
// rust side.
[[noreturn]] void throw_breakpoint(uintptr_t callback) {
throw BreakpointException(callback);
unsafe_unwind(new BreakpointException(callback));
}
bool invoke_trampoline(trampoline_t trampoline, void *ctx, void *func,
void *params, void *results, WasmTrap::Type *trap_out,
box_any_t *user_error, void *invoke_env) noexcept {
try {
trampoline(ctx, func, params, results);
catch_unwind([trampoline, ctx, func, params, results]() {
trampoline(ctx, func, params, results);
});
return true;
} catch (const WasmTrap &e) {
*trap_out = e.type;