2019-02-26 02:07:22 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2019-02-28 20:31:39 +00:00
|
|
|
#include <llvm/ExecutionEngine/RuntimeDyld.h>
|
2019-03-02 20:57:35 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <exception>
|
2019-02-26 02:07:22 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
PROTECT_NONE,
|
|
|
|
PROTECT_READ,
|
|
|
|
PROTECT_READ_WRITE,
|
|
|
|
PROTECT_READ_EXECUTE,
|
|
|
|
} mem_protect_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
RESULT_OK,
|
|
|
|
RESULT_ALLOCATE_FAILURE,
|
|
|
|
RESULT_PROTECT_FAILURE,
|
|
|
|
RESULT_DEALLOC_FAILURE,
|
|
|
|
RESULT_OBJECT_LOAD_FAILURE,
|
|
|
|
} result_t;
|
|
|
|
|
|
|
|
typedef result_t (*alloc_memory_t)(size_t size, mem_protect_t protect, uint8_t** ptr_out, size_t* size_out);
|
|
|
|
typedef result_t (*protect_memory_t)(uint8_t* ptr, size_t size, mem_protect_t protect);
|
|
|
|
typedef result_t (*dealloc_memory_t)(uint8_t* ptr, size_t size);
|
2019-03-02 01:11:20 +00:00
|
|
|
typedef uintptr_t (*lookup_vm_symbol_t)(const char* name_ptr, size_t length);
|
2019-03-02 18:56:02 +00:00
|
|
|
typedef void (*fde_visitor_t)(uint8_t *fde);
|
|
|
|
typedef result_t (*visit_fde_t)(uint8_t *fde, size_t size, fde_visitor_t visitor);
|
2019-02-26 02:07:22 +00:00
|
|
|
|
2019-03-02 20:57:35 +00:00
|
|
|
typedef void (*trampoline_t)(void*, void*, void*, void*);
|
|
|
|
|
2019-02-26 02:07:22 +00:00
|
|
|
typedef struct {
|
|
|
|
/* Memory management. */
|
|
|
|
alloc_memory_t alloc_memory;
|
|
|
|
protect_memory_t protect_memory;
|
|
|
|
dealloc_memory_t dealloc_memory;
|
|
|
|
|
|
|
|
lookup_vm_symbol_t lookup_vm_symbol;
|
2019-03-02 18:56:02 +00:00
|
|
|
|
|
|
|
visit_fde_t visit_fde;
|
2019-02-26 02:07:22 +00:00
|
|
|
} callbacks_t;
|
|
|
|
|
2019-03-02 22:16:02 +00:00
|
|
|
struct WasmException {
|
2019-03-02 20:57:35 +00:00
|
|
|
public:
|
|
|
|
virtual std::string description() const noexcept = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UncatchableException : WasmException {
|
|
|
|
public:
|
2019-03-02 22:16:02 +00:00
|
|
|
virtual std::string description() const noexcept override {
|
|
|
|
return "Uncatchable exception";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UserException : UncatchableException {
|
|
|
|
public:
|
|
|
|
UserException(std::string msg) : msg(msg) {}
|
|
|
|
|
|
|
|
virtual std::string description() const noexcept override {
|
|
|
|
return std::string("user exception: ") + msg;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::string msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct WasmTrap : UncatchableException {
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
Unreachable = 0,
|
|
|
|
IncorrectCallIndirectSignature = 1,
|
|
|
|
MemoryOutOfBounds = 2,
|
2019-03-04 05:38:35 +00:00
|
|
|
CallIndirectOOB = 3,
|
2019-03-05 03:56:02 +00:00
|
|
|
IllegalArithmetic = 4,
|
2019-03-02 20:57:35 +00:00
|
|
|
Unknown,
|
2019-03-02 22:16:02 +00:00
|
|
|
};
|
2019-03-02 20:57:35 +00:00
|
|
|
|
2019-03-02 22:16:02 +00:00
|
|
|
WasmTrap(Type type) : type(type) {}
|
2019-03-02 20:57:35 +00:00
|
|
|
|
|
|
|
virtual std::string description() const noexcept override {
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss
|
2019-03-02 22:16:02 +00:00
|
|
|
<< "WebAssembly trap:" << '\n'
|
2019-03-02 20:57:35 +00:00
|
|
|
<< " - type: " << type << '\n';
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:16:02 +00:00
|
|
|
Type type;
|
|
|
|
|
2019-03-02 20:57:35 +00:00
|
|
|
private:
|
|
|
|
friend std::ostream& operator<<(std::ostream& out, const Type& ty) {
|
|
|
|
switch (ty) {
|
|
|
|
case Type::Unreachable:
|
|
|
|
out << "unreachable";
|
|
|
|
break;
|
|
|
|
case Type::IncorrectCallIndirectSignature:
|
|
|
|
out << "incorrect call_indirect signature";
|
|
|
|
break;
|
2019-03-02 22:16:02 +00:00
|
|
|
case Type::MemoryOutOfBounds:
|
|
|
|
out << "memory access out-of-bounds";
|
|
|
|
break;
|
2019-03-05 03:56:02 +00:00
|
|
|
case Type::CallIndirectOOB:
|
|
|
|
out << "call_indirect out-of-bounds";
|
|
|
|
break;
|
|
|
|
case Type::IllegalArithmetic:
|
|
|
|
out << "illegal arithmetic operation";
|
|
|
|
break;
|
2019-03-02 20:57:35 +00:00
|
|
|
case Type::Unknown:
|
2019-03-02 22:16:02 +00:00
|
|
|
default:
|
2019-03-02 20:57:35 +00:00
|
|
|
out << "unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CatchableException : WasmException {
|
|
|
|
public:
|
|
|
|
CatchableException(uint32_t type_id, uint32_t value_num) : type_id(type_id), value_num(value_num) {}
|
|
|
|
|
|
|
|
virtual std::string description() const noexcept override {
|
|
|
|
return "catchable exception";
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t type_id, value_num;
|
2019-03-03 01:00:05 +00:00
|
|
|
uint64_t values[1];
|
2019-03-02 20:57:35 +00:00
|
|
|
};
|
|
|
|
|
2019-03-02 22:16:02 +00:00
|
|
|
struct WasmModule {
|
2019-02-28 20:31:39 +00:00
|
|
|
public:
|
|
|
|
WasmModule(
|
|
|
|
const uint8_t *object_start,
|
|
|
|
size_t object_size,
|
2019-03-01 01:20:18 +00:00
|
|
|
callbacks_t callbacks
|
2019-02-28 20:31:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
void *get_func(llvm::StringRef name) const;
|
|
|
|
private:
|
2019-03-02 18:56:02 +00:00
|
|
|
std::unique_ptr<llvm::RuntimeDyld::MemoryManager> memory_manager;
|
2019-02-28 20:31:39 +00:00
|
|
|
std::unique_ptr<llvm::object::ObjectFile> object_file;
|
2019-03-01 01:20:18 +00:00
|
|
|
std::unique_ptr<llvm::RuntimeDyld> runtime_dyld;
|
2019-02-28 20:31:39 +00:00
|
|
|
};
|
|
|
|
|
2019-02-26 02:07:22 +00:00
|
|
|
extern "C" {
|
2019-03-01 01:20:18 +00:00
|
|
|
result_t module_load(const uint8_t* mem_ptr, size_t mem_size, callbacks_t callbacks, WasmModule** module_out) {
|
2019-02-28 20:31:39 +00:00
|
|
|
*module_out = new WasmModule(mem_ptr, mem_size, callbacks);
|
|
|
|
|
2019-02-26 02:07:22 +00:00
|
|
|
return RESULT_OK;
|
|
|
|
}
|
|
|
|
|
2019-03-03 01:00:05 +00:00
|
|
|
[[noreturn]] void throw_trap(WasmTrap::Type ty) {
|
2019-03-02 22:16:02 +00:00
|
|
|
throw WasmTrap(ty);
|
2019-03-02 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 01:20:18 +00:00
|
|
|
void module_delete(WasmModule* module) {
|
|
|
|
delete module;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:08:15 +00:00
|
|
|
bool invoke_trampoline(
|
|
|
|
trampoline_t trampoline,
|
|
|
|
void* ctx,
|
|
|
|
void* func,
|
|
|
|
void* params,
|
|
|
|
void* results,
|
|
|
|
WasmTrap::Type* trap_out
|
|
|
|
) throw() {
|
2019-03-02 20:57:35 +00:00
|
|
|
try {
|
|
|
|
trampoline(ctx, func, params, results);
|
2019-03-03 03:08:15 +00:00
|
|
|
return true;
|
|
|
|
} catch(const WasmTrap& e) {
|
|
|
|
*trap_out = e.type;
|
|
|
|
return false;
|
2019-03-02 20:57:35 +00:00
|
|
|
} catch(const WasmException& e) {
|
2019-03-03 03:08:15 +00:00
|
|
|
*trap_out = WasmTrap::Type::Unknown;
|
|
|
|
return false;
|
2019-03-02 20:57:35 +00:00
|
|
|
} catch (...) {
|
2019-03-03 03:08:15 +00:00
|
|
|
*trap_out = WasmTrap::Type::Unknown;
|
|
|
|
return false;
|
2019-03-02 20:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:31:39 +00:00
|
|
|
void* get_func_symbol(WasmModule* module, const char* name) {
|
|
|
|
return module->get_func(llvm::StringRef(name));
|
2019-02-26 02:07:22 +00:00
|
|
|
}
|
|
|
|
}
|