mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-23 21:51:32 +00:00
Merge branch 'fix/llvm-trap-windows' into feature/llvm-osr
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
#include "object_loader.hh"
|
#include "object_loader.hh"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <setjmp.h>
|
||||||
|
|
||||||
extern "C" void __register_frame(uint8_t *);
|
extern "C" void __register_frame(uint8_t *);
|
||||||
extern "C" void __deregister_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(read_section.base, read_section.size);
|
||||||
callbacks.dealloc_memory(readwrite_section.base, readwrite_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 = ¤t;
|
||||||
|
|
||||||
|
unwinding_setjmp(current.stack, unwind_payload, (void *) ¤t);
|
||||||
|
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,
|
uint8_t *MemoryManager::allocateCodeSection(uintptr_t size, unsigned alignment,
|
||||||
unsigned section_id,
|
unsigned section_id,
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include <llvm/ExecutionEngine/RuntimeDyld.h>
|
#include <llvm/ExecutionEngine/RuntimeDyld.h>
|
||||||
|
|
||||||
@ -104,11 +106,14 @@ private:
|
|||||||
size_t stack_map_size = 0;
|
size_t stack_map_size = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WasmException {
|
struct WasmException: std::exception {
|
||||||
public:
|
public:
|
||||||
virtual std::string description() const noexcept = 0;
|
virtual std::string description() const noexcept = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void catch_unwind(std::function<void()>&& f);
|
||||||
|
[[noreturn]] void unsafe_unwind(std::exception *exception);
|
||||||
|
|
||||||
struct UncatchableException : WasmException {
|
struct UncatchableException : WasmException {
|
||||||
public:
|
public:
|
||||||
virtual std::string description() const noexcept override {
|
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;
|
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; }
|
void module_delete(WasmModule *module) { delete module; }
|
||||||
|
|
||||||
// Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust
|
// Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust
|
||||||
// side.
|
// side.
|
||||||
[[noreturn]] void throw_any(size_t data, size_t vtable) {
|
[[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
|
// Throw a pointer that's assumed to be codegen::BreakpointHandler on the
|
||||||
// rust side.
|
// rust side.
|
||||||
[[noreturn]] void throw_breakpoint(uintptr_t callback) {
|
[[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,
|
bool invoke_trampoline(trampoline_t trampoline, void *ctx, void *func,
|
||||||
void *params, void *results, WasmTrap::Type *trap_out,
|
void *params, void *results, WasmTrap::Type *trap_out,
|
||||||
box_any_t *user_error, void *invoke_env) noexcept {
|
box_any_t *user_error, void *invoke_env) noexcept {
|
||||||
try {
|
try {
|
||||||
|
catch_unwind([trampoline, ctx, func, params, results]() {
|
||||||
trampoline(ctx, func, params, results);
|
trampoline(ctx, func, params, results);
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (const WasmTrap &e) {
|
} catch (const WasmTrap &e) {
|
||||||
*trap_out = e.type;
|
*trap_out = e.type;
|
||||||
|
Reference in New Issue
Block a user