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
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2019-02-26 02:07:22 +00:00
|
|
|
PROTECT_NONE,
|
|
|
|
PROTECT_READ,
|
|
|
|
PROTECT_READ_WRITE,
|
|
|
|
PROTECT_READ_EXECUTE,
|
|
|
|
} mem_protect_t;
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2019-02-26 02:07:22 +00:00
|
|
|
RESULT_OK,
|
|
|
|
RESULT_ALLOCATE_FAILURE,
|
|
|
|
RESULT_PROTECT_FAILURE,
|
|
|
|
RESULT_DEALLOC_FAILURE,
|
|
|
|
RESULT_OBJECT_LOAD_FAILURE,
|
|
|
|
} result_t;
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
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);
|
|
|
|
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-04-09 22:53:01 +00:00
|
|
|
typedef void (*trampoline_t)(void *, void *, void *, void *);
|
2019-03-02 20:57:35 +00:00
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2019-02-26 02:07:22 +00:00
|
|
|
/* 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-04-22 22:06:40 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t data, vtable;
|
|
|
|
} box_any_t;
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
struct WasmException
|
|
|
|
{
|
|
|
|
public:
|
2019-03-02 20:57:35 +00:00
|
|
|
virtual std::string description() const noexcept = 0;
|
|
|
|
};
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
struct UncatchableException : WasmException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual std::string description() const noexcept override
|
|
|
|
{
|
2019-03-02 22:16:02 +00:00
|
|
|
return "Uncatchable exception";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
struct UserException : UncatchableException
|
|
|
|
{
|
|
|
|
public:
|
2019-04-22 22:06:40 +00:00
|
|
|
UserException(size_t data, size_t vtable) : error_data({ data, vtable }) {}
|
2019-03-02 22:16:02 +00:00
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
virtual std::string description() const noexcept override
|
|
|
|
{
|
2019-04-10 21:17:10 +00:00
|
|
|
return "user exception";
|
2019-03-02 22:16:02 +00:00
|
|
|
}
|
2019-04-09 22:53:01 +00:00
|
|
|
|
2019-04-10 21:17:10 +00:00
|
|
|
// The parts of a `Box<dyn Any>`.
|
2019-04-22 22:06:40 +00:00
|
|
|
box_any_t error_data;
|
2019-03-02 22:16:02 +00:00
|
|
|
};
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
struct WasmTrap : UncatchableException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Type
|
|
|
|
{
|
2019-03-02 22:16:02 +00:00
|
|
|
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
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
virtual std::string description() const noexcept override
|
|
|
|
{
|
2019-03-02 20:57:35 +00:00
|
|
|
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';
|
2019-04-09 22:53:01 +00:00
|
|
|
|
2019-03-02 20:57:35 +00:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:16:02 +00:00
|
|
|
Type type;
|
|
|
|
|
2019-04-09 22:53:01 +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;
|
|
|
|
case Type::MemoryOutOfBounds:
|
|
|
|
out << "memory access out-of-bounds";
|
|
|
|
break;
|
|
|
|
case Type::CallIndirectOOB:
|
|
|
|
out << "call_indirect out-of-bounds";
|
|
|
|
break;
|
|
|
|
case Type::IllegalArithmetic:
|
|
|
|
out << "illegal arithmetic operation";
|
|
|
|
break;
|
|
|
|
case Type::Unknown:
|
|
|
|
default:
|
|
|
|
out << "unknown";
|
|
|
|
break;
|
2019-03-02 20:57:35 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
struct CatchableException : WasmException
|
|
|
|
{
|
|
|
|
public:
|
2019-03-02 20:57:35 +00:00
|
|
|
CatchableException(uint32_t type_id, uint32_t value_num) : type_id(type_id), value_num(value_num) {}
|
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
virtual std::string description() const noexcept override
|
|
|
|
{
|
2019-03-02 20:57:35 +00:00
|
|
|
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-04-09 22:53:01 +00:00
|
|
|
struct WasmModule
|
|
|
|
{
|
|
|
|
public:
|
2019-02-28 20:31:39 +00:00
|
|
|
WasmModule(
|
|
|
|
const uint8_t *object_start,
|
|
|
|
size_t object_size,
|
2019-04-09 22:53:01 +00:00
|
|
|
callbacks_t callbacks);
|
2019-02-28 20:31:39 +00:00
|
|
|
|
|
|
|
void *get_func(llvm::StringRef name) const;
|
2019-04-09 22:53:01 +00:00
|
|
|
|
2019-04-19 20:54:48 +00:00
|
|
|
bool _init_failed = false;
|
2019-04-09 22:53:01 +00:00
|
|
|
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-04-09 22:53:01 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
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-04-19 20:54:48 +00:00
|
|
|
if ((*module_out)->_init_failed) {
|
|
|
|
return RESULT_OBJECT_LOAD_FAILURE;
|
|
|
|
}
|
|
|
|
|
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-04-09 22:53:01 +00:00
|
|
|
void module_delete(WasmModule *module)
|
|
|
|
{
|
2019-03-01 01:20:18 +00:00
|
|
|
delete module;
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:17:10 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:08:15 +00:00
|
|
|
bool invoke_trampoline(
|
|
|
|
trampoline_t trampoline,
|
2019-04-09 22:53:01 +00:00
|
|
|
void *ctx,
|
|
|
|
void *func,
|
|
|
|
void *params,
|
|
|
|
void *results,
|
|
|
|
WasmTrap::Type *trap_out,
|
2019-04-22 22:06:40 +00:00
|
|
|
box_any_t *user_error,
|
2019-04-09 22:53:01 +00:00
|
|
|
void *invoke_env) throw()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-03-02 20:57:35 +00:00
|
|
|
trampoline(ctx, func, params, results);
|
2019-03-03 03:08:15 +00:00
|
|
|
return true;
|
2019-04-09 22:53:01 +00:00
|
|
|
}
|
|
|
|
catch (const WasmTrap &e)
|
|
|
|
{
|
2019-03-03 03:08:15 +00:00
|
|
|
*trap_out = e.type;
|
|
|
|
return false;
|
2019-04-09 22:53:01 +00:00
|
|
|
}
|
2019-04-22 22:06:40 +00:00
|
|
|
catch (const UserException &e)
|
|
|
|
{
|
|
|
|
*user_error = e.error_data;
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-09 22:53:01 +00:00
|
|
|
catch (const WasmException &e)
|
|
|
|
{
|
2019-03-03 03:08:15 +00:00
|
|
|
*trap_out = WasmTrap::Type::Unknown;
|
|
|
|
return false;
|
2019-04-09 22:53:01 +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-04-09 22:53:01 +00:00
|
|
|
void *get_func_symbol(WasmModule *module, const char *name)
|
|
|
|
{
|
2019-02-28 20:31:39 +00:00
|
|
|
return module->get_func(llvm::StringRef(name));
|
2019-02-26 02:07:22 +00:00
|
|
|
}
|
|
|
|
}
|