mirror of
https://github.com/fluencelabs/wasmer
synced 2025-03-31 06:51:04 +00:00
Add implementations for typed func errors to cranelift and llvm
This commit is contained in:
parent
5e1a67d835
commit
c212ba3619
@ -2,12 +2,13 @@ use crate::relocation::{TrapData, TrapSink};
|
|||||||
use crate::trampoline::Trampolines;
|
use crate::trampoline::Trampolines;
|
||||||
use hashbrown::HashSet;
|
use hashbrown::HashSet;
|
||||||
use libc::c_void;
|
use libc::c_void;
|
||||||
use std::{any::Any, cell::Cell, sync::Arc};
|
use std::{any::Any, cell::Cell, ptr::NonNull, sync::Arc};
|
||||||
use wasmer_runtime_core::{
|
use wasmer_runtime_core::{
|
||||||
backend::{ProtectedCaller, Token, UserTrapper},
|
backend::{ProtectedCaller, Token, UserTrapper},
|
||||||
error::RuntimeResult,
|
error::RuntimeResult,
|
||||||
export::Context,
|
export::Context,
|
||||||
module::{ExportIndex, ModuleInfo, ModuleInner},
|
module::{ExportIndex, ModuleInfo, ModuleInner},
|
||||||
|
typed_func::{Wasm, WasmTrapInfo},
|
||||||
types::{FuncIndex, FuncSig, LocalOrImport, SigIndex, Type, Value},
|
types::{FuncIndex, FuncSig, LocalOrImport, SigIndex, Type, Value},
|
||||||
vm::{self, ImportBacking},
|
vm::{self, ImportBacking},
|
||||||
};
|
};
|
||||||
@ -148,6 +149,46 @@ impl ProtectedCaller for Caller {
|
|||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_wasm_trampoline(&self, module: &ModuleInner, sig_index: SigIndex) -> Option<Wasm> {
|
||||||
|
unsafe extern "C" fn invoke(
|
||||||
|
trampoline: unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64),
|
||||||
|
ctx: *mut vm::Ctx,
|
||||||
|
func: NonNull<vm::Func>,
|
||||||
|
args: *const u64,
|
||||||
|
rets: *mut u64,
|
||||||
|
trap_info: *mut WasmTrapInfo,
|
||||||
|
invoke_env: Option<NonNull<c_void>>,
|
||||||
|
) -> bool {
|
||||||
|
let handler_data = &*invoke_env.unwrap().cast().as_ptr();
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
let res = call_protected(handler_data, || unsafe {
|
||||||
|
// Leap of faith.
|
||||||
|
trampoline(ctx, func, args, rets);
|
||||||
|
})
|
||||||
|
.is_ok();
|
||||||
|
|
||||||
|
// the trampoline is called from C on windows
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
let res = call_protected(handler_data, trampoline, ctx, func, args, rets).is_ok();
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
let trampoline = self
|
||||||
|
.trampolines
|
||||||
|
.lookup(sig_index)
|
||||||
|
.expect("that trampoline doesn't exist");
|
||||||
|
|
||||||
|
Some(unsafe {
|
||||||
|
Wasm::from_raw_parts(
|
||||||
|
trampoline,
|
||||||
|
invoke,
|
||||||
|
Some(NonNull::from(&self.handler_data).cast()),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn get_early_trapper(&self) -> Box<dyn UserTrapper> {
|
fn get_early_trapper(&self) -> Box<dyn UserTrapper> {
|
||||||
Box::new(Trapper)
|
Box::new(Trapper)
|
||||||
}
|
}
|
||||||
@ -157,7 +198,7 @@ fn get_func_from_index<'a>(
|
|||||||
module: &'a ModuleInner,
|
module: &'a ModuleInner,
|
||||||
import_backing: &ImportBacking,
|
import_backing: &ImportBacking,
|
||||||
func_index: FuncIndex,
|
func_index: FuncIndex,
|
||||||
) -> (*const vm::Func, Context, &'a FuncSig, SigIndex) {
|
) -> (NonNull<vm::Func>, Context, &'a FuncSig, SigIndex) {
|
||||||
let sig_index = *module
|
let sig_index = *module
|
||||||
.info
|
.info
|
||||||
.func_assoc
|
.func_assoc
|
||||||
@ -170,14 +211,13 @@ fn get_func_from_index<'a>(
|
|||||||
.func_resolver
|
.func_resolver
|
||||||
.get(&module, local_func_index)
|
.get(&module, local_func_index)
|
||||||
.expect("broken invariant, func resolver not synced with module.exports")
|
.expect("broken invariant, func resolver not synced with module.exports")
|
||||||
.cast()
|
.cast(),
|
||||||
.as_ptr() as *const _,
|
|
||||||
Context::Internal,
|
Context::Internal,
|
||||||
),
|
),
|
||||||
LocalOrImport::Import(imported_func_index) => {
|
LocalOrImport::Import(imported_func_index) => {
|
||||||
let imported_func = import_backing.imported_func(imported_func_index);
|
let imported_func = import_backing.imported_func(imported_func_index);
|
||||||
(
|
(
|
||||||
imported_func.func as *const _,
|
NonNull::new(imported_func.func as *mut _).unwrap(),
|
||||||
Context::External(imported_func.vmctx),
|
Context::External(imported_func.vmctx),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use cranelift_codegen::{
|
|||||||
};
|
};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use std::ffi::c_void;
|
use std::ffi::c_void;
|
||||||
use std::{iter, mem};
|
use std::{iter, mem, ptr::NonNull};
|
||||||
use wasmer_runtime_core::{
|
use wasmer_runtime_core::{
|
||||||
backend::sys::{Memory, Protect},
|
backend::sys::{Memory, Protect},
|
||||||
module::{ExportIndex, ModuleInfo},
|
module::{ExportIndex, ModuleInfo},
|
||||||
@ -23,8 +23,7 @@ impl RelocSink for NullRelocSink {
|
|||||||
fn reloc_jt(&mut self, _: u32, _: Reloc, _: ir::JumpTable) {}
|
fn reloc_jt(&mut self, _: u32, _: Reloc, _: ir::JumpTable) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Trampoline =
|
pub type Trampoline = unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64);
|
||||||
unsafe extern "C" fn(*mut vm::Ctx, *const vm::Func, *const u64, *mut u64) -> c_void;
|
|
||||||
|
|
||||||
pub struct Trampolines {
|
pub struct Trampolines {
|
||||||
memory: Memory,
|
memory: Memory,
|
||||||
|
@ -5,14 +5,16 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum
|
||||||
|
{
|
||||||
PROTECT_NONE,
|
PROTECT_NONE,
|
||||||
PROTECT_READ,
|
PROTECT_READ,
|
||||||
PROTECT_READ_WRITE,
|
PROTECT_READ_WRITE,
|
||||||
PROTECT_READ_EXECUTE,
|
PROTECT_READ_EXECUTE,
|
||||||
} mem_protect_t;
|
} mem_protect_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum
|
||||||
|
{
|
||||||
RESULT_OK,
|
RESULT_OK,
|
||||||
RESULT_ALLOCATE_FAILURE,
|
RESULT_ALLOCATE_FAILURE,
|
||||||
RESULT_PROTECT_FAILURE,
|
RESULT_PROTECT_FAILURE,
|
||||||
@ -20,16 +22,17 @@ typedef enum {
|
|||||||
RESULT_OBJECT_LOAD_FAILURE,
|
RESULT_OBJECT_LOAD_FAILURE,
|
||||||
} result_t;
|
} 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 (*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 (*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 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);
|
typedef uintptr_t (*lookup_vm_symbol_t)(const char *name_ptr, size_t length);
|
||||||
typedef void (*fde_visitor_t)(uint8_t *fde);
|
typedef void (*fde_visitor_t)(uint8_t *fde);
|
||||||
typedef result_t (*visit_fde_t)(uint8_t *fde, size_t size, fde_visitor_t visitor);
|
typedef result_t (*visit_fde_t)(uint8_t *fde, size_t size, fde_visitor_t visitor);
|
||||||
|
|
||||||
typedef void (*trampoline_t)(void*, void*, void*, void*);
|
typedef void (*trampoline_t)(void *, void *, void *, void *);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
/* Memory management. */
|
/* Memory management. */
|
||||||
alloc_memory_t alloc_memory;
|
alloc_memory_t alloc_memory;
|
||||||
protect_memory_t protect_memory;
|
protect_memory_t protect_memory;
|
||||||
@ -40,32 +43,40 @@ typedef struct {
|
|||||||
visit_fde_t visit_fde;
|
visit_fde_t visit_fde;
|
||||||
} callbacks_t;
|
} callbacks_t;
|
||||||
|
|
||||||
struct WasmException {
|
struct WasmException
|
||||||
public:
|
{
|
||||||
|
public:
|
||||||
virtual std::string description() const noexcept = 0;
|
virtual std::string description() const noexcept = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UncatchableException : WasmException {
|
struct UncatchableException : WasmException
|
||||||
public:
|
{
|
||||||
virtual std::string description() const noexcept override {
|
public:
|
||||||
|
virtual std::string description() const noexcept override
|
||||||
|
{
|
||||||
return "Uncatchable exception";
|
return "Uncatchable exception";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct UserException : UncatchableException {
|
struct UserException : UncatchableException
|
||||||
public:
|
{
|
||||||
|
public:
|
||||||
UserException(std::string msg) : msg(msg) {}
|
UserException(std::string msg) : msg(msg) {}
|
||||||
|
|
||||||
virtual std::string description() const noexcept override {
|
virtual std::string description() const noexcept override
|
||||||
|
{
|
||||||
return std::string("user exception: ") + msg;
|
return std::string("user exception: ") + msg;
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
|
private:
|
||||||
std::string msg;
|
std::string msg;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WasmTrap : UncatchableException {
|
struct WasmTrap : UncatchableException
|
||||||
public:
|
{
|
||||||
enum Type {
|
public:
|
||||||
|
enum Type
|
||||||
|
{
|
||||||
Unreachable = 0,
|
Unreachable = 0,
|
||||||
IncorrectCallIndirectSignature = 1,
|
IncorrectCallIndirectSignature = 1,
|
||||||
MemoryOutOfBounds = 2,
|
MemoryOutOfBounds = 2,
|
||||||
@ -76,49 +87,54 @@ public:
|
|||||||
|
|
||||||
WasmTrap(Type type) : type(type) {}
|
WasmTrap(Type type) : type(type) {}
|
||||||
|
|
||||||
virtual std::string description() const noexcept override {
|
virtual std::string description() const noexcept override
|
||||||
|
{
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
ss
|
ss
|
||||||
<< "WebAssembly trap:" << '\n'
|
<< "WebAssembly trap:" << '\n'
|
||||||
<< " - type: " << type << '\n';
|
<< " - type: " << type << '\n';
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
Type type;
|
Type type;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend std::ostream& operator<<(std::ostream& out, const Type& ty) {
|
friend std::ostream &operator<<(std::ostream &out, const Type &ty)
|
||||||
switch (ty) {
|
{
|
||||||
case Type::Unreachable:
|
switch (ty)
|
||||||
out << "unreachable";
|
{
|
||||||
break;
|
case Type::Unreachable:
|
||||||
case Type::IncorrectCallIndirectSignature:
|
out << "unreachable";
|
||||||
out << "incorrect call_indirect signature";
|
break;
|
||||||
break;
|
case Type::IncorrectCallIndirectSignature:
|
||||||
case Type::MemoryOutOfBounds:
|
out << "incorrect call_indirect signature";
|
||||||
out << "memory access out-of-bounds";
|
break;
|
||||||
break;
|
case Type::MemoryOutOfBounds:
|
||||||
case Type::CallIndirectOOB:
|
out << "memory access out-of-bounds";
|
||||||
out << "call_indirect out-of-bounds";
|
break;
|
||||||
break;
|
case Type::CallIndirectOOB:
|
||||||
case Type::IllegalArithmetic:
|
out << "call_indirect out-of-bounds";
|
||||||
out << "illegal arithmetic operation";
|
break;
|
||||||
break;
|
case Type::IllegalArithmetic:
|
||||||
case Type::Unknown:
|
out << "illegal arithmetic operation";
|
||||||
default:
|
break;
|
||||||
out << "unknown";
|
case Type::Unknown:
|
||||||
break;
|
default:
|
||||||
|
out << "unknown";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CatchableException : WasmException {
|
struct CatchableException : WasmException
|
||||||
public:
|
{
|
||||||
|
public:
|
||||||
CatchableException(uint32_t type_id, uint32_t value_num) : type_id(type_id), value_num(value_num) {}
|
CatchableException(uint32_t type_id, uint32_t value_num) : type_id(type_id), value_num(value_num) {}
|
||||||
|
|
||||||
virtual std::string description() const noexcept override {
|
virtual std::string description() const noexcept override
|
||||||
|
{
|
||||||
return "catchable exception";
|
return "catchable exception";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,23 +142,26 @@ public:
|
|||||||
uint64_t values[1];
|
uint64_t values[1];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WasmModule {
|
struct WasmModule
|
||||||
public:
|
{
|
||||||
|
public:
|
||||||
WasmModule(
|
WasmModule(
|
||||||
const uint8_t *object_start,
|
const uint8_t *object_start,
|
||||||
size_t object_size,
|
size_t object_size,
|
||||||
callbacks_t callbacks
|
callbacks_t callbacks);
|
||||||
);
|
|
||||||
|
|
||||||
void *get_func(llvm::StringRef name) const;
|
void *get_func(llvm::StringRef name) const;
|
||||||
private:
|
|
||||||
|
private:
|
||||||
std::unique_ptr<llvm::RuntimeDyld::MemoryManager> memory_manager;
|
std::unique_ptr<llvm::RuntimeDyld::MemoryManager> memory_manager;
|
||||||
std::unique_ptr<llvm::object::ObjectFile> object_file;
|
std::unique_ptr<llvm::object::ObjectFile> object_file;
|
||||||
std::unique_ptr<llvm::RuntimeDyld> runtime_dyld;
|
std::unique_ptr<llvm::RuntimeDyld> runtime_dyld;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C"
|
||||||
result_t module_load(const uint8_t* mem_ptr, size_t mem_size, callbacks_t callbacks, WasmModule** module_out) {
|
{
|
||||||
|
result_t module_load(const uint8_t *mem_ptr, size_t mem_size, callbacks_t callbacks, WasmModule **module_out)
|
||||||
|
{
|
||||||
*module_out = new WasmModule(mem_ptr, mem_size, callbacks);
|
*module_out = new WasmModule(mem_ptr, mem_size, callbacks);
|
||||||
|
|
||||||
return RESULT_OK;
|
return RESULT_OK;
|
||||||
@ -152,34 +171,44 @@ extern "C" {
|
|||||||
throw WasmTrap(ty);
|
throw WasmTrap(ty);
|
||||||
}
|
}
|
||||||
|
|
||||||
void module_delete(WasmModule* module) {
|
void module_delete(WasmModule *module)
|
||||||
|
{
|
||||||
delete module;
|
delete module;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool invoke_trampoline(
|
bool invoke_trampoline(
|
||||||
trampoline_t trampoline,
|
trampoline_t trampoline,
|
||||||
void* ctx,
|
void *ctx,
|
||||||
void* func,
|
void *func,
|
||||||
void* params,
|
void *params,
|
||||||
void* results,
|
void *results,
|
||||||
WasmTrap::Type* trap_out
|
WasmTrap::Type *trap_out,
|
||||||
) throw() {
|
void *invoke_env) throw()
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
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;
|
||||||
return false;
|
return false;
|
||||||
} catch(const WasmException& e) {
|
}
|
||||||
|
catch (const WasmException &e)
|
||||||
|
{
|
||||||
*trap_out = WasmTrap::Type::Unknown;
|
*trap_out = WasmTrap::Type::Unknown;
|
||||||
return false;
|
return false;
|
||||||
} catch (...) {
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
*trap_out = WasmTrap::Type::Unknown;
|
*trap_out = WasmTrap::Type::Unknown;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void* get_func_symbol(WasmModule* module, const char* name) {
|
void *get_func_symbol(WasmModule *module, const char *name)
|
||||||
|
{
|
||||||
return module->get_func(llvm::StringRef(name));
|
return module->get_func(llvm::StringRef(name));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,7 +11,7 @@ use libc::{
|
|||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
any::Any,
|
any::Any,
|
||||||
ffi::CString,
|
ffi::{c_void, CString},
|
||||||
mem,
|
mem,
|
||||||
ptr::{self, NonNull},
|
ptr::{self, NonNull},
|
||||||
slice, str,
|
slice, str,
|
||||||
@ -23,6 +23,7 @@ use wasmer_runtime_core::{
|
|||||||
export::Context,
|
export::Context,
|
||||||
module::{ModuleInfo, ModuleInner},
|
module::{ModuleInfo, ModuleInner},
|
||||||
structures::TypedIndex,
|
structures::TypedIndex,
|
||||||
|
typed_func::{Wasm, WasmTrapInfo},
|
||||||
types::{FuncIndex, FuncSig, LocalFuncIndex, LocalOrImport, SigIndex, Type, Value},
|
types::{FuncIndex, FuncSig, LocalFuncIndex, LocalOrImport, SigIndex, Type, Value},
|
||||||
vm::{self, ImportBacking},
|
vm::{self, ImportBacking},
|
||||||
vmcalls,
|
vmcalls,
|
||||||
@ -54,17 +55,6 @@ enum LLVMResult {
|
|||||||
OBJECT_LOAD_FAILURE,
|
OBJECT_LOAD_FAILURE,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[repr(C)]
|
|
||||||
enum WasmTrapType {
|
|
||||||
Unreachable = 0,
|
|
||||||
IncorrectCallIndirectSignature = 1,
|
|
||||||
MemoryOutOfBounds = 2,
|
|
||||||
CallIndirectOOB = 3,
|
|
||||||
IllegalArithmetic = 4,
|
|
||||||
Unknown,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct Callbacks {
|
struct Callbacks {
|
||||||
alloc_memory: extern "C" fn(usize, MemProtect, &mut *mut u8, &mut usize) -> LLVMResult,
|
alloc_memory: extern "C" fn(usize, MemProtect, &mut *mut u8, &mut usize) -> LLVMResult,
|
||||||
@ -87,13 +77,15 @@ extern "C" {
|
|||||||
|
|
||||||
fn throw_trap(ty: i32);
|
fn throw_trap(ty: i32);
|
||||||
|
|
||||||
|
#[allow(improper_ctypes)]
|
||||||
fn invoke_trampoline(
|
fn invoke_trampoline(
|
||||||
trampoline: unsafe extern "C" fn(*mut vm::Ctx, *const vm::Func, *const u64, *mut u64),
|
trampoline: unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64),
|
||||||
vmctx_ptr: *mut vm::Ctx,
|
vmctx_ptr: *mut vm::Ctx,
|
||||||
func_ptr: *const vm::Func,
|
func_ptr: NonNull<vm::Func>,
|
||||||
params: *const u64,
|
params: *const u64,
|
||||||
results: *mut u64,
|
results: *mut u64,
|
||||||
trap_out: *mut WasmTrapType,
|
trap_out: *mut WasmTrapInfo,
|
||||||
|
invoke_env: Option<NonNull<c_void>>,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,7 +352,12 @@ impl ProtectedCaller for LLVMProtectedCaller {
|
|||||||
|
|
||||||
let mut return_vec = vec![0; signature.returns().len()];
|
let mut return_vec = vec![0; signature.returns().len()];
|
||||||
|
|
||||||
let trampoline: unsafe extern "C" fn(*mut vm::Ctx, *const vm::Func, *const u64, *mut u64) = unsafe {
|
let trampoline: unsafe extern "C" fn(
|
||||||
|
*mut vm::Ctx,
|
||||||
|
NonNull<vm::Func>,
|
||||||
|
*const u64,
|
||||||
|
*mut u64,
|
||||||
|
) = unsafe {
|
||||||
let name = if cfg!(target_os = "macos") {
|
let name = if cfg!(target_os = "macos") {
|
||||||
format!("_trmp{}", sig_index.index())
|
format!("_trmp{}", sig_index.index())
|
||||||
} else {
|
} else {
|
||||||
@ -374,7 +371,7 @@ impl ProtectedCaller for LLVMProtectedCaller {
|
|||||||
mem::transmute(symbol)
|
mem::transmute(symbol)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut trap_out = WasmTrapType::Unknown;
|
let mut trap_out = WasmTrapInfo::Unknown;
|
||||||
|
|
||||||
// Here we go.
|
// Here we go.
|
||||||
let success = unsafe {
|
let success = unsafe {
|
||||||
@ -385,6 +382,7 @@ impl ProtectedCaller for LLVMProtectedCaller {
|
|||||||
param_vec.as_ptr(),
|
param_vec.as_ptr(),
|
||||||
return_vec.as_mut_ptr(),
|
return_vec.as_mut_ptr(),
|
||||||
&mut trap_out,
|
&mut trap_out,
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -400,29 +398,35 @@ impl ProtectedCaller for LLVMProtectedCaller {
|
|||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
} else {
|
} else {
|
||||||
Err(match trap_out {
|
Err(RuntimeError::Trap {
|
||||||
WasmTrapType::Unreachable => RuntimeError::Trap {
|
msg: trap_out.to_string().into(),
|
||||||
msg: "unreachable".into(),
|
|
||||||
},
|
|
||||||
WasmTrapType::IncorrectCallIndirectSignature => RuntimeError::Trap {
|
|
||||||
msg: "uncorrect call_indirect signature".into(),
|
|
||||||
},
|
|
||||||
WasmTrapType::MemoryOutOfBounds => RuntimeError::Trap {
|
|
||||||
msg: "memory out-of-bounds access".into(),
|
|
||||||
},
|
|
||||||
WasmTrapType::CallIndirectOOB => RuntimeError::Trap {
|
|
||||||
msg: "call_indirect out-of-bounds".into(),
|
|
||||||
},
|
|
||||||
WasmTrapType::IllegalArithmetic => RuntimeError::Trap {
|
|
||||||
msg: "illegal arithmetic operation".into(),
|
|
||||||
},
|
|
||||||
WasmTrapType::Unknown => RuntimeError::Trap {
|
|
||||||
msg: "unknown trap".into(),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_wasm_trampoline(&self, _module: &ModuleInner, sig_index: SigIndex) -> Option<Wasm> {
|
||||||
|
let trampoline: unsafe extern "C" fn(
|
||||||
|
*mut vm::Ctx,
|
||||||
|
NonNull<vm::Func>,
|
||||||
|
*const u64,
|
||||||
|
*mut u64,
|
||||||
|
) = unsafe {
|
||||||
|
let name = if cfg!(target_os = "macos") {
|
||||||
|
format!("_trmp{}", sig_index.index())
|
||||||
|
} else {
|
||||||
|
format!("trmp{}", sig_index.index())
|
||||||
|
};
|
||||||
|
|
||||||
|
let c_str = CString::new(name).unwrap();
|
||||||
|
let symbol = get_func_symbol(self.module, c_str.as_ptr());
|
||||||
|
assert!(!symbol.is_null());
|
||||||
|
|
||||||
|
mem::transmute(symbol)
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(unsafe { Wasm::from_raw_parts(trampoline, invoke_trampoline, None) })
|
||||||
|
}
|
||||||
|
|
||||||
fn get_early_trapper(&self) -> Box<dyn UserTrapper> {
|
fn get_early_trapper(&self) -> Box<dyn UserTrapper> {
|
||||||
Box::new(Placeholder)
|
Box::new(Placeholder)
|
||||||
}
|
}
|
||||||
@ -438,7 +442,7 @@ fn get_func_from_index<'a>(
|
|||||||
module: &'a ModuleInner,
|
module: &'a ModuleInner,
|
||||||
import_backing: &ImportBacking,
|
import_backing: &ImportBacking,
|
||||||
func_index: FuncIndex,
|
func_index: FuncIndex,
|
||||||
) -> (*const vm::Func, Context, &'a FuncSig, SigIndex) {
|
) -> (NonNull<vm::Func>, Context, &'a FuncSig, SigIndex) {
|
||||||
let sig_index = *module
|
let sig_index = *module
|
||||||
.info
|
.info
|
||||||
.func_assoc
|
.func_assoc
|
||||||
@ -451,14 +455,13 @@ fn get_func_from_index<'a>(
|
|||||||
.func_resolver
|
.func_resolver
|
||||||
.get(&module, local_func_index)
|
.get(&module, local_func_index)
|
||||||
.expect("broken invariant, func resolver not synced with module.exports")
|
.expect("broken invariant, func resolver not synced with module.exports")
|
||||||
.cast()
|
.cast(),
|
||||||
.as_ptr() as *const _,
|
|
||||||
Context::Internal,
|
Context::Internal,
|
||||||
),
|
),
|
||||||
LocalOrImport::Import(imported_func_index) => {
|
LocalOrImport::Import(imported_func_index) => {
|
||||||
let imported_func = import_backing.imported_func(imported_func_index);
|
let imported_func = import_backing.imported_func(imported_func_index);
|
||||||
(
|
(
|
||||||
imported_func.func as *const _,
|
NonNull::new(imported_func.func as *mut _).unwrap(),
|
||||||
Context::External(imported_func.vmctx),
|
Context::External(imported_func.vmctx),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,8 @@ use crate::{
|
|||||||
error::CompileResult,
|
error::CompileResult,
|
||||||
error::RuntimeResult,
|
error::RuntimeResult,
|
||||||
module::ModuleInner,
|
module::ModuleInner,
|
||||||
types::{FuncIndex, LocalFuncIndex, Value},
|
typed_func::Wasm,
|
||||||
|
types::{FuncIndex, LocalFuncIndex, SigIndex, Value},
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -95,6 +96,8 @@ pub trait ProtectedCaller: Send + Sync {
|
|||||||
_: Token,
|
_: Token,
|
||||||
) -> RuntimeResult<Vec<Value>>;
|
) -> RuntimeResult<Vec<Value>>;
|
||||||
|
|
||||||
|
fn get_wasm_trampoline(&self, module: &ModuleInner, sig_index: SigIndex) -> Option<Wasm>;
|
||||||
|
|
||||||
fn get_early_trapper(&self) -> Box<dyn UserTrapper>;
|
fn get_early_trapper(&self) -> Box<dyn UserTrapper>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ use crate::{
|
|||||||
types::{FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, TableIndex, Value},
|
types::{FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, TableIndex, Value},
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
use std::{mem, sync::Arc};
|
use std::{mem, ptr::NonNull, sync::Arc};
|
||||||
|
|
||||||
pub(crate) struct InstanceInner {
|
pub(crate) struct InstanceInner {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -145,20 +145,26 @@ impl Instance {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let func_wasm_inner = self
|
||||||
|
.module
|
||||||
|
.protected_caller
|
||||||
|
.get_wasm_trampoline(&self.module, sig_index)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let func_ptr = match func_index.local_or_import(&self.module.info) {
|
let func_ptr = match func_index.local_or_import(&self.module.info) {
|
||||||
LocalOrImport::Local(local_func_index) => self
|
LocalOrImport::Local(local_func_index) => self
|
||||||
.module
|
.module
|
||||||
.func_resolver
|
.func_resolver
|
||||||
.get(&self.module, local_func_index)
|
.get(&self.module, local_func_index)
|
||||||
.unwrap()
|
.unwrap(),
|
||||||
.as_ptr(),
|
LocalOrImport::Import(import_func_index) => NonNull::new(
|
||||||
LocalOrImport::Import(import_func_index) => {
|
self.inner.import_backing.vm_functions[import_func_index].func as *mut _,
|
||||||
self.inner.import_backing.vm_functions[import_func_index].func
|
)
|
||||||
}
|
.unwrap(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let typed_func: Func<Args, Rets, Wasm> =
|
let typed_func: Func<Args, Rets, Wasm> =
|
||||||
unsafe { Func::from_raw_parts(trampoline, invoke, f as _, ctx, invoke_env) };
|
unsafe { Func::from_raw_parts(func_wasm_inner, func_ptr, ctx) };
|
||||||
|
|
||||||
Ok(typed_func)
|
Ok(typed_func)
|
||||||
} else {
|
} else {
|
||||||
|
@ -23,7 +23,7 @@ mod sig_registry;
|
|||||||
pub mod structures;
|
pub mod structures;
|
||||||
mod sys;
|
mod sys;
|
||||||
pub mod table;
|
pub mod table;
|
||||||
mod typed_func;
|
pub mod typed_func;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod units;
|
pub mod units;
|
||||||
pub mod vm;
|
pub mod vm;
|
||||||
|
@ -4,10 +4,17 @@ use crate::{
|
|||||||
export::{Context, Export, FuncPointer},
|
export::{Context, Export, FuncPointer},
|
||||||
import::IsExport,
|
import::IsExport,
|
||||||
types::{FuncSig, Type, WasmExternType},
|
types::{FuncSig, Type, WasmExternType},
|
||||||
vm::Ctx,
|
vm::{self, Ctx},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
any::Any, cell::UnsafeCell, ffi::c_void, fmt, marker::PhantomData, mem, panic, ptr, sync::Arc,
|
any::Any,
|
||||||
|
cell::UnsafeCell,
|
||||||
|
ffi::c_void,
|
||||||
|
fmt,
|
||||||
|
marker::PhantomData,
|
||||||
|
mem, panic,
|
||||||
|
ptr::{self, NonNull},
|
||||||
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
@ -45,20 +52,36 @@ impl fmt::Display for WasmTrapInfo {
|
|||||||
|
|
||||||
pub trait Kind {}
|
pub trait Kind {}
|
||||||
|
|
||||||
type Trampoline = extern "C" fn(*mut Ctx, *const c_void, *const u64, *mut u64);
|
pub type Trampoline = unsafe extern "C" fn(*mut Ctx, NonNull<vm::Func>, *const u64, *mut u64);
|
||||||
type Invoke = extern "C" fn(
|
pub type Invoke = unsafe extern "C" fn(
|
||||||
Trampoline,
|
Trampoline,
|
||||||
*mut Ctx,
|
*mut Ctx,
|
||||||
*const c_void,
|
NonNull<vm::Func>,
|
||||||
*const u64,
|
*const u64,
|
||||||
*mut u64,
|
*mut u64,
|
||||||
&mut WasmTrapInfo,
|
*mut WasmTrapInfo,
|
||||||
|
Option<NonNull<c_void>>,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
pub struct Wasm {
|
pub struct Wasm {
|
||||||
trampoline: Trampoline,
|
trampoline: Trampoline,
|
||||||
invoke: Invoke,
|
invoke: Invoke,
|
||||||
invoke_env: *mut c_void,
|
invoke_env: Option<NonNull<c_void>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Wasm {
|
||||||
|
pub unsafe fn from_raw_parts(
|
||||||
|
trampoline: Trampoline,
|
||||||
|
invoke: Invoke,
|
||||||
|
invoke_env: Option<NonNull<c_void>>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
trampoline,
|
||||||
|
invoke,
|
||||||
|
invoke_env,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Host(());
|
pub struct Host(());
|
||||||
@ -75,7 +98,7 @@ pub trait WasmTypeList {
|
|||||||
fn types() -> &'static [Type];
|
fn types() -> &'static [Type];
|
||||||
unsafe fn call<Rets>(
|
unsafe fn call<Rets>(
|
||||||
self,
|
self,
|
||||||
f: *const c_void,
|
f: NonNull<vm::Func>,
|
||||||
wasm: Wasm,
|
wasm: Wasm,
|
||||||
ctx: *mut Ctx,
|
ctx: *mut Ctx,
|
||||||
) -> Result<Rets, WasmTrapInfo>
|
) -> Result<Rets, WasmTrapInfo>
|
||||||
@ -88,7 +111,7 @@ where
|
|||||||
Args: WasmTypeList,
|
Args: WasmTypeList,
|
||||||
Rets: WasmTypeList,
|
Rets: WasmTypeList,
|
||||||
{
|
{
|
||||||
fn to_raw(&self) -> *const c_void;
|
fn to_raw(&self) -> NonNull<vm::Func>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait TrapEarly<Rets>
|
pub trait TrapEarly<Rets>
|
||||||
@ -128,7 +151,7 @@ where
|
|||||||
|
|
||||||
pub struct Func<'a, Args = (), Rets = (), Inner: Kind = Wasm> {
|
pub struct Func<'a, Args = (), Rets = (), Inner: Kind = Wasm> {
|
||||||
inner: Inner,
|
inner: Inner,
|
||||||
f: *const c_void,
|
f: NonNull<vm::Func>,
|
||||||
ctx: *mut Ctx,
|
ctx: *mut Ctx,
|
||||||
_phantom: PhantomData<(&'a (), Args, Rets)>,
|
_phantom: PhantomData<(&'a (), Args, Rets)>,
|
||||||
}
|
}
|
||||||
@ -139,18 +162,12 @@ where
|
|||||||
Rets: WasmTypeList,
|
Rets: WasmTypeList,
|
||||||
{
|
{
|
||||||
pub(crate) unsafe fn from_raw_parts(
|
pub(crate) unsafe fn from_raw_parts(
|
||||||
trampoline: Trampoline,
|
inner: Wasm,
|
||||||
invoke: Invoke,
|
f: NonNull<vm::Func>,
|
||||||
f: *const c_void,
|
|
||||||
ctx: *mut Ctx,
|
ctx: *mut Ctx,
|
||||||
invoke_env: *mut c_void,
|
|
||||||
) -> Func<'a, Args, Rets, Wasm> {
|
) -> Func<'a, Args, Rets, Wasm> {
|
||||||
Func {
|
Func {
|
||||||
inner: Wasm {
|
inner,
|
||||||
trampoline,
|
|
||||||
invoke,
|
|
||||||
invoke_env,
|
|
||||||
},
|
|
||||||
f,
|
f,
|
||||||
ctx,
|
ctx,
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
@ -214,7 +231,7 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
|
|||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
unsafe fn call<Rets: WasmTypeList>(
|
unsafe fn call<Rets: WasmTypeList>(
|
||||||
self,
|
self,
|
||||||
f: *const c_void,
|
f: NonNull<vm::Func>,
|
||||||
wasm: Wasm,
|
wasm: Wasm,
|
||||||
ctx: *mut Ctx,
|
ctx: *mut Ctx,
|
||||||
) -> Result<Rets, WasmTrapInfo> {
|
) -> Result<Rets, WasmTrapInfo> {
|
||||||
@ -233,6 +250,7 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
|
|||||||
args.as_ptr(),
|
args.as_ptr(),
|
||||||
rets.as_mut().as_mut_ptr(),
|
rets.as_mut().as_mut_ptr(),
|
||||||
&mut trap,
|
&mut trap,
|
||||||
|
wasm.invoke_env,
|
||||||
) {
|
) {
|
||||||
Ok(Rets::from_ret_array(rets))
|
Ok(Rets::from_ret_array(rets))
|
||||||
} else {
|
} else {
|
||||||
@ -263,6 +281,7 @@ macro_rules! impl_traits {
|
|||||||
type CStruct = $struct_name<$( $x ),*>;
|
type CStruct = $struct_name<$( $x ),*>;
|
||||||
type RetArray = [u64; count_idents!( $( $x ),* )];
|
type RetArray = [u64; count_idents!( $( $x ),* )];
|
||||||
fn from_ret_array(array: Self::RetArray) -> Self {
|
fn from_ret_array(array: Self::RetArray) -> Self {
|
||||||
|
#[allow(non_snake_case)]
|
||||||
let [ $( $x ),* ] = array;
|
let [ $( $x ),* ] = array;
|
||||||
( $( WasmExternType::from_bits($x) ),* )
|
( $( WasmExternType::from_bits($x) ),* )
|
||||||
}
|
}
|
||||||
@ -283,7 +302,7 @@ macro_rules! impl_traits {
|
|||||||
&[$( $x::TYPE, )*]
|
&[$( $x::TYPE, )*]
|
||||||
}
|
}
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
unsafe fn call<Rets: WasmTypeList>(self, f: *const c_void, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, WasmTrapInfo> {
|
unsafe fn call<Rets: WasmTypeList>(self, f: NonNull<vm::Func>, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, WasmTrapInfo> {
|
||||||
// type Trampoline = extern "C" fn(*mut Ctx, *const c_void, *const u64, *mut u64);
|
// type Trampoline = extern "C" fn(*mut Ctx, *const c_void, *const u64, *mut u64);
|
||||||
// type Invoke = extern "C" fn(Trampoline, *mut Ctx, *const c_void, *const u64, *mut u64, &mut WasmTrapInfo) -> bool;
|
// type Invoke = extern "C" fn(Trampoline, *mut Ctx, *const c_void, *const u64, *mut u64, &mut WasmTrapInfo) -> bool;
|
||||||
|
|
||||||
@ -293,7 +312,7 @@ macro_rules! impl_traits {
|
|||||||
let mut rets = Rets::empty_ret_array();
|
let mut rets = Rets::empty_ret_array();
|
||||||
let mut trap = WasmTrapInfo::Unknown;
|
let mut trap = WasmTrapInfo::Unknown;
|
||||||
|
|
||||||
if (wasm.invoke)(wasm.trampoline, ctx, f, args.as_ptr(), rets.as_mut().as_mut_ptr(), &mut trap) {
|
if (wasm.invoke)(wasm.trampoline, ctx, f, args.as_ptr(), rets.as_mut().as_mut_ptr(), &mut trap, wasm.invoke_env) {
|
||||||
Ok(Rets::from_ret_array(rets))
|
Ok(Rets::from_ret_array(rets))
|
||||||
} else {
|
} else {
|
||||||
Err(trap)
|
Err(trap)
|
||||||
@ -309,7 +328,7 @@ macro_rules! impl_traits {
|
|||||||
|
|
||||||
impl< $( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap> ExternalFunction<($( $x ),*), Rets> for FN {
|
impl< $( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap> ExternalFunction<($( $x ),*), Rets> for FN {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
fn to_raw(&self) -> *const c_void {
|
fn to_raw(&self) -> NonNull<vm::Func> {
|
||||||
assert_eq!(mem::size_of::<Self>(), 0, "you cannot use a closure that captures state for `Func`.");
|
assert_eq!(mem::size_of::<Self>(), 0, "you cannot use a closure that captures state for `Func`.");
|
||||||
|
|
||||||
extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap>( ctx: &mut Ctx $( ,$x: $x )* ) -> Rets::CStruct {
|
extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap>( ctx: &mut Ctx $( ,$x: $x )* ) -> Rets::CStruct {
|
||||||
@ -333,7 +352,7 @@ macro_rules! impl_traits {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wrap::<$( $x, )* Rets, Trap, Self> as *const c_void
|
NonNull::new(wrap::<$( $x, )* Rets, Trap, Self> as *mut vm::Func).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,7 +403,7 @@ where
|
|||||||
Inner: Kind,
|
Inner: Kind,
|
||||||
{
|
{
|
||||||
fn to_export(&self) -> Export {
|
fn to_export(&self) -> Export {
|
||||||
let func = unsafe { FuncPointer::new(self.f as _) };
|
let func = unsafe { FuncPointer::new(self.f.as_ptr()) };
|
||||||
let ctx = Context::Internal;
|
let ctx = Context::Internal;
|
||||||
let signature = Arc::new(FuncSig::new(Args::types(), Rets::types()));
|
let signature = Arc::new(FuncSig::new(Args::types(), Rets::types()));
|
||||||
|
|
||||||
|
@ -5,7 +5,10 @@ use wabt::wat2wasm;
|
|||||||
static WAT: &'static str = r#"
|
static WAT: &'static str = r#"
|
||||||
(module
|
(module
|
||||||
(type (;0;) (func (result i32)))
|
(type (;0;) (func (result i32)))
|
||||||
|
(import "env" "do_panic" (func $do_panic (type 0)))
|
||||||
(func $dbz (result i32)
|
(func $dbz (result i32)
|
||||||
|
call $do_panic
|
||||||
|
drop
|
||||||
i32.const 42
|
i32.const 42
|
||||||
i32.const 0
|
i32.const 0
|
||||||
i32.div_u
|
i32.div_u
|
||||||
@ -33,6 +36,10 @@ fn foobar(ctx: &mut Ctx) -> i32 {
|
|||||||
42
|
42
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn do_panic(ctx: &mut Ctx) -> Result<i32, String> {
|
||||||
|
Err("error".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), error::Error> {
|
fn main() -> Result<(), error::Error> {
|
||||||
let wasm = get_wasm();
|
let wasm = get_wasm();
|
||||||
|
|
||||||
@ -46,11 +53,15 @@ fn main() -> Result<(), error::Error> {
|
|||||||
// };
|
// };
|
||||||
|
|
||||||
println!("instantiating");
|
println!("instantiating");
|
||||||
let instance = module.instantiate(&imports! {})?;
|
let instance = module.instantiate(&imports! {
|
||||||
|
"env" => {
|
||||||
|
"do_panic" => Func::new(do_panic),
|
||||||
|
},
|
||||||
|
})?;
|
||||||
|
|
||||||
let foo = instance.dyn_func("dbz")?;
|
let foo: Func<(), i32> = instance.func("dbz")?;
|
||||||
|
|
||||||
let result = foo.call(&[]);
|
let result = foo.call();
|
||||||
|
|
||||||
println!("result: {:?}", result);
|
println!("result: {:?}", result);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user