Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
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
Scope the unsafe blocks to be smaller.
  • Loading branch information
jimpo committed Oct 28, 2019
commit a77e9bd7ae3935b008ec0d2faf6645c4af4bba6f
89 changes: 52 additions & 37 deletions core/executor/src/wasmtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::wasmtime::util::{cranelift_ir_signature, read_memory_into, write_memo
use crate::{Externalities, RuntimeVersion};

use codec::Decode;
use cranelift_codegen::ir;
use cranelift_codegen::isa::TargetIsa;
use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_frontend::FunctionBuilderContext;
Expand Down Expand Up @@ -165,29 +166,27 @@ fn call_method(
.map_err(ActionError::Setup)
.map_err(Error::Wasmtime)?;

let args = unsafe {
// Ideally there would be a way to set the heap pages during instantiation rather than
// growing the memory after the fact. Current this may require an additional mmap and copy.
// However, the wasmtime API doesn't support modifying the size of memory on instantiation
// at this time.
grow_memory(&mut instance, heap_pages)?;

// Initialize the function executor state.
let executor_state = FunctionExecutorState::new(
// Unsafely extend the reference lifetime to static. This is necessary because the
// host state must be `Any`. This is OK as we will drop this object either before
// exiting the function in the happy case or in the worst case on the next runtime
// call, which also resets the host state. Thus this reference can never actually
// outlive the Context.
mem::transmute::<_, &'static mut Compiler>(context.compiler_mut()),
get_heap_base(&instance)?,
);
reset_host_state(context, Some(executor_state))?;
// Ideally there would be a way to set the heap pages during instantiation rather than
// growing the memory after the fact. Current this may require an additional mmap and copy.
// However, the wasmtime API doesn't support modifying the size of memory on instantiation
// at this time.
grow_memory(&mut instance, heap_pages)?;

// Initialize the function executor state.
let executor_state = FunctionExecutorState::new(
// Unsafely extend the reference lifetime to static. This is necessary because the
// host state must be `Any`. This is OK as we will drop this object either before
// exiting the function in the happy case or in the worst case on the next runtime
// call, which also resets the host state. Thus this reference can never actually
// outlive the Context.
unsafe { mem::transmute::<_, &'static mut Compiler>(context.compiler_mut()) },
get_heap_base(&instance)?,
);
reset_host_state(context, Some(executor_state))?;

// Write the input data into guest memory.
let (data_ptr, data_len) = inject_input_data(context, &mut instance, data)?;
[RuntimeValue::I32(u32::from(data_ptr) as i32), RuntimeValue::I32(data_len as i32)]
};
// Write the input data into guest memory.
let (data_ptr, data_len) = inject_input_data(context, &mut instance, data)?;
let args = [RuntimeValue::I32(u32::from(data_ptr) as i32), RuntimeValue::I32(data_len as i32)];

// Invoke the function in the runtime.
let outcome = externalities::set_and_run_with_externalities(ext, || {
Expand Down Expand Up @@ -217,7 +216,7 @@ fn call_method(

// Read the output data from guest memory.
let mut output = vec![0; output_len];
let memory = unsafe { get_memory_mut(&mut instance)? };
let memory = get_memory_mut(&mut instance)?;
read_memory_into(memory, output_ptr, &mut output)?;
Ok(output)
}
Expand Down Expand Up @@ -290,11 +289,16 @@ fn clear_globals(global_exports: &mut HashMap<String, Option<Export>>) {
global_exports.remove("__indirect_function_table");
}

unsafe fn grow_memory(instance: &mut InstanceHandle, pages: u32) -> Result<()> {
let memory_index = match instance.lookup_immutable("memory") {
Some(Export::Memory { definition, vmctx: _, memory: _ }) =>
instance.memory_index(&*definition),
_ => return Err(Error::InvalidMemoryReference),
fn grow_memory(instance: &mut InstanceHandle, pages: u32) -> Result<()> {
// This is safe to wrap in an unsafe block as:
// - The result of the `lookup_immutable` call is not mutated
// - The definition pointer is returned by a lookup on a valid instance
let memory_index = unsafe {
match instance.lookup_immutable("memory") {
Some(Export::Memory { definition, vmctx: _, memory: _ }) =>
instance.memory_index(&*definition),
_ => return Err(Error::InvalidMemoryReference),
}
};
instance.memory_grow(memory_index, pages)
.map(|_| ())
Expand All @@ -318,7 +322,7 @@ fn reset_host_state(context: &mut Context, executor_state: Option<FunctionExecut
Ok(trampoline_state.reset_trap())
}

unsafe fn inject_input_data(
fn inject_input_data(
context: &mut Context,
instance: &mut InstanceHandle,
data: &[u8],
Expand All @@ -336,22 +340,33 @@ unsafe fn inject_input_data(
Ok((data_ptr, data_len))
}

unsafe fn get_memory_mut(instance: &mut InstanceHandle) -> Result<&mut [u8]> {
fn get_memory_mut(instance: &mut InstanceHandle) -> Result<&mut [u8]> {
match instance.lookup("memory") {
Some(Export::Memory { definition, vmctx: _, memory: _ }) =>
// This is safe to wrap in an unsafe block as:
// - The definition pointer is returned by a lookup on a valid instance and thus points to
// a valid memory definition
Some(Export::Memory { definition, vmctx: _, memory: _ }) => unsafe {
Ok(std::slice::from_raw_parts_mut(
(*definition).base,
(*definition).current_length,
)),
))
},
_ => Err(Error::InvalidMemoryReference),
}
}

unsafe fn get_heap_base(instance: &InstanceHandle) -> Result<u32> {
match instance.lookup_immutable("__heap_base") {
Some(Export::Global { definition, vmctx: _, global: _ }) =>
Ok(*(*definition).as_u32()),
_ => return Err(Error::HeapBaseNotFoundOrInvalid),
fn get_heap_base(instance: &InstanceHandle) -> Result<u32> {
// This is safe to wrap in an unsafe block as:
// - The result of the `lookup_immutable` call is not mutated
// - The definition pointer is returned by a lookup on a valid instance
// - The defined value is checked to be an I32, which can be read safely as a u32
unsafe {
match instance.lookup_immutable("__heap_base") {
Some(Export::Global { definition, vmctx: _, global })
if global.ty == ir::types::I32 =>
Ok(*(*definition).as_u32()),
_ => return Err(Error::HeapBaseNotFoundOrInvalid),
}
}
}

Expand Down