|
|
|
|
@@ -1,12 +1,10 @@
|
|
|
|
|
//! Support for a calling of an imported function.
|
|
|
|
|
|
|
|
|
|
use super::create_handle::create_handle;
|
|
|
|
|
use super::trap::TrapSink;
|
|
|
|
|
use crate::{Callable, FuncType, Store, Trap, Val};
|
|
|
|
|
use anyhow::{bail, Result};
|
|
|
|
|
use std::any::Any;
|
|
|
|
|
use std::cmp;
|
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
use std::panic::{self, AssertUnwindSafe};
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
use wasmtime_environ::entity::{EntityRef, PrimaryMap};
|
|
|
|
|
@@ -14,7 +12,7 @@ use wasmtime_environ::ir::types;
|
|
|
|
|
use wasmtime_environ::isa::TargetIsa;
|
|
|
|
|
use wasmtime_environ::wasm::{DefinedFuncIndex, FuncIndex};
|
|
|
|
|
use wasmtime_environ::{
|
|
|
|
|
ir, settings, CompiledFunction, CompiledFunctionUnwindInfo, Export, Module, TrapInformation,
|
|
|
|
|
ir, settings, CompiledFunction, CompiledFunctionUnwindInfo, Export, Module,
|
|
|
|
|
};
|
|
|
|
|
use wasmtime_jit::trampoline::ir::{
|
|
|
|
|
ExternalName, Function, InstBuilder, MemFlags, StackSlotData, StackSlotKind,
|
|
|
|
|
@@ -23,46 +21,17 @@ use wasmtime_jit::trampoline::{
|
|
|
|
|
binemit, pretty_error, Context, FunctionBuilder, FunctionBuilderContext,
|
|
|
|
|
};
|
|
|
|
|
use wasmtime_jit::{native, CodeMemory};
|
|
|
|
|
use wasmtime_runtime::{
|
|
|
|
|
get_mut_trap_registry, InstanceHandle, TrapRegistrationGuard, VMContext, VMFunctionBody,
|
|
|
|
|
};
|
|
|
|
|
use wasmtime_runtime::{InstanceHandle, VMContext, VMFunctionBody};
|
|
|
|
|
|
|
|
|
|
struct TrampolineState {
|
|
|
|
|
func: Rc<dyn Callable + 'static>,
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
code_memory: CodeMemory,
|
|
|
|
|
trap_registration_guards: Vec<TrapRegistrationGuard>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TrampolineState {
|
|
|
|
|
fn new(
|
|
|
|
|
func: Rc<dyn Callable + 'static>,
|
|
|
|
|
code_memory: CodeMemory,
|
|
|
|
|
func_addr: *const VMFunctionBody,
|
|
|
|
|
func_traps: &[TrapInformation],
|
|
|
|
|
) -> Self {
|
|
|
|
|
let mut trap_registry = get_mut_trap_registry();
|
|
|
|
|
let mut trap_registration_guards = Vec::new();
|
|
|
|
|
for trap_desc in func_traps.iter() {
|
|
|
|
|
let func_addr = func_addr as *const u8 as usize;
|
|
|
|
|
let offset = usize::try_from(trap_desc.code_offset).unwrap();
|
|
|
|
|
let trap_addr = func_addr + offset;
|
|
|
|
|
let guard =
|
|
|
|
|
trap_registry.register_trap(trap_addr, trap_desc.source_loc, trap_desc.trap_code);
|
|
|
|
|
trap_registration_guards.push(guard);
|
|
|
|
|
}
|
|
|
|
|
TrampolineState {
|
|
|
|
|
func,
|
|
|
|
|
code_memory,
|
|
|
|
|
trap_registration_guards,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for TrampolineState {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// We must deregister traps before freeing the code memory.
|
|
|
|
|
self.trap_registration_guards.clear();
|
|
|
|
|
fn new(func: Rc<dyn Callable + 'static>, code_memory: CodeMemory) -> Self {
|
|
|
|
|
TrampolineState { func, code_memory }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -152,7 +121,7 @@ fn make_trampoline(
|
|
|
|
|
fn_builder_ctx: &mut FunctionBuilderContext,
|
|
|
|
|
call_id: u32,
|
|
|
|
|
signature: &ir::Signature,
|
|
|
|
|
) -> (*const VMFunctionBody, Vec<TrapInformation>) {
|
|
|
|
|
) -> *const VMFunctionBody {
|
|
|
|
|
// Mostly reverse copy of the similar method from wasmtime's
|
|
|
|
|
// wasmtime-jit/src/compiler.rs.
|
|
|
|
|
let pointer_type = isa.pointer_type();
|
|
|
|
|
@@ -250,7 +219,7 @@ fn make_trampoline(
|
|
|
|
|
|
|
|
|
|
let mut code_buf: Vec<u8> = Vec::new();
|
|
|
|
|
let mut reloc_sink = binemit::TrampolineRelocSink {};
|
|
|
|
|
let mut trap_sink = TrapSink::new();
|
|
|
|
|
let mut trap_sink = binemit::NullTrapSink {};
|
|
|
|
|
let mut stackmap_sink = binemit::NullStackmapSink {};
|
|
|
|
|
context
|
|
|
|
|
.compile_and_emit(
|
|
|
|
|
@@ -265,17 +234,14 @@ fn make_trampoline(
|
|
|
|
|
|
|
|
|
|
let unwind_info = CompiledFunctionUnwindInfo::new(isa, &context);
|
|
|
|
|
|
|
|
|
|
let traps = trap_sink.traps;
|
|
|
|
|
|
|
|
|
|
let addr = code_memory
|
|
|
|
|
code_memory
|
|
|
|
|
.allocate_for_function(&CompiledFunction {
|
|
|
|
|
body: code_buf,
|
|
|
|
|
jt_offsets: context.func.jt_offsets,
|
|
|
|
|
unwind_info,
|
|
|
|
|
})
|
|
|
|
|
.expect("allocate_for_function")
|
|
|
|
|
.as_ptr();
|
|
|
|
|
(addr, traps)
|
|
|
|
|
.as_ptr()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_handle_with_function(
|
|
|
|
|
@@ -306,7 +272,7 @@ pub fn create_handle_with_function(
|
|
|
|
|
module
|
|
|
|
|
.exports
|
|
|
|
|
.insert("trampoline".to_string(), Export::Function(func_id));
|
|
|
|
|
let (trampoline, traps) = make_trampoline(
|
|
|
|
|
let trampoline = make_trampoline(
|
|
|
|
|
isa.as_ref(),
|
|
|
|
|
&mut code_memory,
|
|
|
|
|
&mut fn_builder_ctx,
|
|
|
|
|
@@ -317,7 +283,7 @@ pub fn create_handle_with_function(
|
|
|
|
|
|
|
|
|
|
finished_functions.push(trampoline);
|
|
|
|
|
|
|
|
|
|
let trampoline_state = TrampolineState::new(func.clone(), code_memory, trampoline, &traps);
|
|
|
|
|
let trampoline_state = TrampolineState::new(func.clone(), code_memory);
|
|
|
|
|
|
|
|
|
|
create_handle(
|
|
|
|
|
module,
|
|
|
|
|
|