Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
87bf2d4
executor: Use non wasmi-specific execution in tests.
jimpo Oct 18, 2019
f37b9d8
executor: Move all runtime execution tests into tests file.
jimpo Oct 18, 2019
36bdc5c
executor: Use test_case macro to easily execute tests with different
jimpo Oct 18, 2019
727ce43
executor: Convert errors to strings with Display, not Debug.
jimpo Oct 21, 2019
1bcb472
node-executor: Rewrite benchmarks with criterion.
jimpo Oct 21, 2019
3908f76
executor: Begin implementation of Wasm runtime.
jimpo Oct 21, 2019
447c31a
executor: Define and implement basic FunctionExecutor.
jimpo Oct 21, 2019
828fbf0
executor: Implement host function trampoline generation.
jimpo Oct 21, 2019
81535c5
executor: Instantiate and link runtime module to env module.
jimpo Oct 21, 2019
a3d2b35
executor: Provide input data during wasmtime execution.
jimpo Oct 21, 2019
a7bb759
executor: Implement SandboxCapabilites::invoke for wasmtime executor.
jimpo Oct 21, 2019
9076ca0
executor: Integrate and test wasmtime execution method.
jimpo Oct 21, 2019
5a903a8
executor: Improve FunctionExecution error messages.
jimpo Oct 21, 2019
a77e9bd
Scope the unsafe blocks to be smaller.
jimpo Oct 23, 2019
c964753
Rename TrampolineState to EnvState.
jimpo Oct 23, 2019
5e0d828
Let EnvState own its own compiler instead of unsafe lifetime cast.
jimpo Oct 24, 2019
7623f12
Refactor out some common wasmi/wasmtime logic.
jimpo Oct 25, 2019
7467e35
Typos and cosmetic changes.
jimpo Oct 25, 2019
b73465c
More trampoline comments.
jimpo Oct 25, 2019
d5b72c6
Cargo.lock update.
jimpo Oct 28, 2019
779d4b4
cli: CLI option for running Substrate with compiled Wasm execution.
jimpo Oct 25, 2019
5ffda81
executor: Switch dependency from fork to official wasmtime repo.
jimpo Oct 28, 2019
70896c0
Quiet down cranelift logs.
jimpo Oct 28, 2019
4b0cdc8
Explicitly catch panics during host calls.
jimpo Oct 30, 2019
28f7c11
Additional checks and clarifications in make_trampoline.
jimpo Oct 30, 2019
f4016dd
Merge branch 'master' into wasmtime-real
jimpo Oct 31, 2019
b76f733
Fixes after merge from master and panic safety for wasmtime
jimpo Oct 31, 2019
0ec6caf
Merge branch 'master' into wasmtime-real
jimpo Nov 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
executor: Implement SandboxCapabilites::invoke for wasmtime executor.
  • Loading branch information
jimpo committed Oct 28, 2019
commit a7bb75918bf64cb1dc543a22ce5a01e37298a1ec
108 changes: 98 additions & 10 deletions core/executor/src/wasmtime/function_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@
use crate::allocator::FreeingBumpHeapAllocator;
use crate::error::{Error, Result};
use crate::sandbox::{self, SandboxCapabilities, SupervisorFuncIndex};
use crate::wasmtime::util::{checked_range, read_memory_into, write_memory_from};
use crate::wasmtime::util::{
checked_range, cranelift_ir_signature, read_memory_into, write_memory_from,
};

use codec::{Decode, Encode};
use cranelift_codegen::ir;
use cranelift_codegen::isa::TargetFrontendConfig;
use log::trace;
use primitives::sandbox as sandbox_primitives;
use wasmtime_jit::Compiler;
use wasmtime_runtime::{Export, VMCallerCheckedAnyfunc, VMContext};
use std::{cmp, mem, ptr};
use wasmtime_environ::translate_signature;
use wasmtime_jit::{ActionError, Compiler};
use wasmtime_runtime::{Export, VMCallerCheckedAnyfunc, VMContext, wasmtime_call_trampoline};
use wasm_interface::{
FunctionContext, MemoryId, Pointer, Result as WResult, Sandbox, WordSize,
FunctionContext, MemoryId, Pointer, Result as WResult, Sandbox, Signature, Value, ValueType,
WordSize,
};

/// Wrapper type for pointer to a Wasm table entry.
Expand Down Expand Up @@ -140,14 +147,48 @@ impl<'a> SandboxCapabilities for FunctionExecutor<'a> {

fn invoke(
&mut self,
_dispatch_thunk: &Self::SupervisorFuncRef,
_invoke_args_ptr: Pointer<u8>,
_invoke_args_len: WordSize,
_state: u32,
_func_idx: SupervisorFuncIndex,
dispatch_thunk: &Self::SupervisorFuncRef,
invoke_args_ptr: Pointer<u8>,
invoke_args_len: WordSize,
state: u32,
func_idx: SupervisorFuncIndex,
) -> Result<i64>
{
unimplemented!()
let func_ptr = unsafe { (*dispatch_thunk.0).func_ptr };
let vmctx = unsafe { (*dispatch_thunk.0).vmctx };

// The following code is based on the wasmtime_jit::Context::invoke.
let value_size = mem::size_of::<VMInvokeArgument>();
let (signature, mut values_vec) = generate_signature_and_args(
&[
Value::I32(u32::from(invoke_args_ptr) as i32),
Value::I32(invoke_args_len as i32),
Value::I32(state as i32),
Value::I32(usize::from(func_idx) as i32),
],
Some(ValueType::I64),
self.compiler.frontend_config(),
);

// Get the trampoline to call for this function.
let exec_code_buf = self.compiler
.get_published_trampoline(func_ptr, &signature, value_size)
.map_err(ActionError::Setup)
.map_err(Error::Wasmtime)?;

// Call the trampoline.
if let Err(message) = unsafe {
wasmtime_call_trampoline(
vmctx,
exec_code_buf,
values_vec.as_mut_ptr() as *mut u8,
)
} {
return Err(Error::FunctionExecution(message));
}

// Load the return value out of `values_vec`.
Ok(unsafe { ptr::read(values_vec.as_ptr() as *const i64) })
}
}

Expand Down Expand Up @@ -298,3 +339,50 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
Ok(instance_idx_or_err_code as u32)
}
}

// The storage for a Wasmtime invocation argument.
#[derive(Debug, Default, Copy, Clone)]
#[repr(C, align(8))]
struct VMInvokeArgument([u8; 8]);

fn generate_signature_and_args(
args: &[Value],
result_type: Option<ValueType>,
frontend_config: TargetFrontendConfig,
) -> (ir::Signature, Vec<VMInvokeArgument>)
{
// This code is based on the wasmtime_jit::Context::invoke.

let param_types = args.iter()
.map(|arg| arg.value_type())
.collect::<Vec<_>>();
let signature = translate_signature(
cranelift_ir_signature(
Signature::new(param_types, result_type),
&frontend_config.default_call_conv
),
frontend_config.pointer_type()
);

let mut values_vec = vec![
VMInvokeArgument::default();
cmp::max(args.len(), result_type.iter().len())
];

// Store the argument values into `values_vec`.
for (index, arg) in args.iter().enumerate() {
unsafe {
let ptr = values_vec.as_mut_ptr().add(index);

match arg {
Value::I32(x) => ptr::write(ptr as *mut i32, *x),
Value::I64(x) => ptr::write(ptr as *mut i64, *x),
Value::F32(x) => ptr::write(ptr as *mut u32, *x),
Value::F64(x) => ptr::write(ptr as *mut u64, *x),
}
}
}

(signature, values_vec)
}

12 changes: 12 additions & 0 deletions core/wasm-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ pub enum Value {
F64(u64),
}

impl Value {
/// Returns the type of this value.
pub fn value_type(&self) -> ValueType {
match self {
Value::I32(_) => ValueType::I32,
Value::I64(_) => ValueType::I64,
Value::F32(_) => ValueType::F32,
Value::F64(_) => ValueType::F64,
}
}
}

/// Provides `Sealed` trait to prevent implementing trait `PointerType` outside of this crate.
mod private {
pub trait Sealed {}
Expand Down