Browse Source
* Add trampoline compilation support for lowered imports This commit adds support to the component model implementation for compiling trampolines suitable for calling host imports. Currently this is purely just the compilation side of things, modifying the wasmtime-cranelift crate and additionally filling out a new `VMComponentOffsets` type (similar to `VMOffsets`). The actual creation of a `VMComponentContext` is still not performed and will be a subsequent PR. Internally though some tests are actually possible with this where we at least assert that compilation of a component and creation of everything in-memory doesn't panic or trip any assertions, so some tests are added here for that as well. * Fix some test errorspull/4216/head
Alex Crichton
2 years ago
committed by
GitHub
17 changed files with 748 additions and 58 deletions
@ -0,0 +1,170 @@ |
|||
//! Compilation support for the component model.
|
|||
|
|||
use crate::compiler::{Compiler, CompilerContext}; |
|||
use crate::obj::ModuleTextBuilder; |
|||
use crate::CompiledFunction; |
|||
use anyhow::Result; |
|||
use cranelift_codegen::ir::{self, InstBuilder, MemFlags}; |
|||
use cranelift_frontend::FunctionBuilder; |
|||
use object::write::Object; |
|||
use std::any::Any; |
|||
use wasmtime_environ::component::{ |
|||
CanonicalOptions, Component, ComponentCompiler, ComponentTypes, LowerImport, LoweredIndex, |
|||
TrampolineInfo, VMComponentOffsets, |
|||
}; |
|||
use wasmtime_environ::PrimaryMap; |
|||
|
|||
impl ComponentCompiler for Compiler { |
|||
fn compile_lowered_trampoline( |
|||
&self, |
|||
component: &Component, |
|||
lowering: &LowerImport, |
|||
types: &ComponentTypes, |
|||
) -> Result<Box<dyn Any + Send>> { |
|||
let ty = &types[lowering.canonical_abi]; |
|||
let isa = &*self.isa; |
|||
let pointer_type = isa.pointer_type(); |
|||
let offsets = VMComponentOffsets::new(isa.pointer_bytes(), component); |
|||
|
|||
let CompilerContext { |
|||
mut func_translator, |
|||
codegen_context: mut context, |
|||
} = self.take_context(); |
|||
|
|||
context.func = ir::Function::with_name_signature( |
|||
ir::ExternalName::user(0, 0), |
|||
crate::indirect_signature(isa, ty), |
|||
); |
|||
|
|||
let mut builder = FunctionBuilder::new(&mut context.func, func_translator.context()); |
|||
let block0 = builder.create_block(); |
|||
|
|||
// Start off by spilling all the wasm arguments into a stack slot to be
|
|||
// passed to the host function.
|
|||
let (values_vec_ptr_val, values_vec_len) = |
|||
self.wasm_to_host_spill_args(ty, &mut builder, block0); |
|||
let vmctx = builder.func.dfg.block_params(block0)[0]; |
|||
|
|||
// Below this will incrementally build both the signature of the host
|
|||
// function we're calling as well as the list of arguments since the
|
|||
// list is somewhat long.
|
|||
let mut callee_args = Vec::new(); |
|||
let mut host_sig = ir::Signature::new(crate::wasmtime_call_conv(isa)); |
|||
|
|||
let CanonicalOptions { |
|||
memory, |
|||
realloc, |
|||
string_encoding, |
|||
} = lowering.options; |
|||
|
|||
// vmctx: *mut VMComponentContext
|
|||
host_sig.params.push(ir::AbiParam::new(pointer_type)); |
|||
callee_args.push(vmctx); |
|||
|
|||
// data: *mut u8,
|
|||
host_sig.params.push(ir::AbiParam::new(pointer_type)); |
|||
callee_args.push(builder.ins().load( |
|||
pointer_type, |
|||
MemFlags::trusted(), |
|||
vmctx, |
|||
i32::try_from(offsets.lowering_data(lowering.index)).unwrap(), |
|||
)); |
|||
|
|||
// memory: *mut VMMemoryDefinition
|
|||
host_sig.params.push(ir::AbiParam::new(pointer_type)); |
|||
callee_args.push(match memory { |
|||
Some(idx) => builder.ins().load( |
|||
pointer_type, |
|||
MemFlags::trusted(), |
|||
vmctx, |
|||
i32::try_from(offsets.runtime_memory(idx)).unwrap(), |
|||
), |
|||
None => builder.ins().iconst(pointer_type, 0), |
|||
}); |
|||
|
|||
// realloc: *mut VMCallerCheckedAnyfunc
|
|||
host_sig.params.push(ir::AbiParam::new(pointer_type)); |
|||
callee_args.push(match realloc { |
|||
Some(idx) => builder.ins().load( |
|||
pointer_type, |
|||
MemFlags::trusted(), |
|||
vmctx, |
|||
i32::try_from(offsets.runtime_realloc(idx)).unwrap(), |
|||
), |
|||
None => builder.ins().iconst(pointer_type, 0), |
|||
}); |
|||
|
|||
// string_encoding: StringEncoding
|
|||
host_sig.params.push(ir::AbiParam::new(ir::types::I8)); |
|||
callee_args.push( |
|||
builder |
|||
.ins() |
|||
.iconst(ir::types::I8, i64::from(string_encoding as u8)), |
|||
); |
|||
|
|||
// storage: *mut ValRaw
|
|||
host_sig.params.push(ir::AbiParam::new(pointer_type)); |
|||
callee_args.push(values_vec_ptr_val); |
|||
|
|||
// storage_len: usize
|
|||
host_sig.params.push(ir::AbiParam::new(pointer_type)); |
|||
callee_args.push( |
|||
builder |
|||
.ins() |
|||
.iconst(pointer_type, i64::from(values_vec_len)), |
|||
); |
|||
|
|||
// Load host function pointer from the vmcontext and then call that
|
|||
// indirect function pointer with the list of arguments.
|
|||
let host_fn = builder.ins().load( |
|||
pointer_type, |
|||
MemFlags::trusted(), |
|||
vmctx, |
|||
i32::try_from(offsets.lowering_callee(lowering.index)).unwrap(), |
|||
); |
|||
let host_sig = builder.import_signature(host_sig); |
|||
builder.ins().call_indirect(host_sig, host_fn, &callee_args); |
|||
|
|||
// After the host function has returned the results are loaded from
|
|||
// `values_vec_ptr_val` and then returned.
|
|||
self.wasm_to_host_load_results(ty, &mut builder, values_vec_ptr_val); |
|||
|
|||
let func: CompiledFunction = self.finish_trampoline(&mut context, isa)?; |
|||
self.save_context(CompilerContext { |
|||
func_translator, |
|||
codegen_context: context, |
|||
}); |
|||
Ok(Box::new(func)) |
|||
} |
|||
|
|||
fn emit_obj( |
|||
&self, |
|||
trampolines: PrimaryMap<LoweredIndex, Box<dyn Any + Send>>, |
|||
obj: &mut Object<'static>, |
|||
) -> Result<PrimaryMap<LoweredIndex, TrampolineInfo>> { |
|||
let trampolines: PrimaryMap<LoweredIndex, CompiledFunction> = trampolines |
|||
.into_iter() |
|||
.map(|(_, f)| *f.downcast().unwrap()) |
|||
.collect(); |
|||
let module = Default::default(); |
|||
let mut text = ModuleTextBuilder::new(obj, &module, &*self.isa); |
|||
let mut ret = PrimaryMap::new(); |
|||
for (idx, trampoline) in trampolines.iter() { |
|||
let (_symbol, range) = text.append_func( |
|||
false, |
|||
format!("_wasm_component_host_trampoline{}", idx.as_u32()).into_bytes(), |
|||
&trampoline, |
|||
); |
|||
|
|||
let i = ret.push(TrampolineInfo { |
|||
start: u32::try_from(range.start).unwrap(), |
|||
length: u32::try_from(range.end - range.start).unwrap(), |
|||
}); |
|||
assert_eq!(i, idx); |
|||
} |
|||
|
|||
text.finish()?; |
|||
|
|||
Ok(ret) |
|||
} |
|||
} |
@ -0,0 +1,58 @@ |
|||
use crate::component::{Component, ComponentTypes, LowerImport, LoweredIndex}; |
|||
use crate::PrimaryMap; |
|||
use anyhow::Result; |
|||
use object::write::Object; |
|||
use serde::{Deserialize, Serialize}; |
|||
use std::any::Any; |
|||
|
|||
/// Description of where a trampoline is located in the text section of a
|
|||
/// compiled image.
|
|||
#[derive(Serialize, Deserialize)] |
|||
pub struct TrampolineInfo { |
|||
/// The byte offset from the start of the text section where this trampoline
|
|||
/// starts.
|
|||
pub start: u32, |
|||
/// The byte length of this trampoline's function body.
|
|||
pub length: u32, |
|||
} |
|||
|
|||
/// Compilation support necessary for components.
|
|||
pub trait ComponentCompiler: Send + Sync { |
|||
/// Creates a trampoline for a `canon.lower`'d host function.
|
|||
///
|
|||
/// This function will create a suitable trampoline which can be called from
|
|||
/// WebAssembly code and which will then call into host code. The signature
|
|||
/// of this generated trampoline should have the appropriate wasm ABI for
|
|||
/// the `lowering.canonical_abi` type signature (e.g. System-V).
|
|||
///
|
|||
/// The generated trampoline will interpret its first argument as a
|
|||
/// `*mut VMComponentContext` and use the `VMComponentOffsets` for
|
|||
/// `component` to read necessary data (as specified by `lowering.options`)
|
|||
/// and call the host function pointer. Notably the host function pointer
|
|||
/// has the signature `VMLoweringCallee` where many of the arguments are
|
|||
/// loaded from known offsets (for this particular generated trampoline)
|
|||
/// from the `VMComponentContext`.
|
|||
///
|
|||
/// Returns a compiler-specific `Box<dyn Any>` which can be passed later to
|
|||
/// `emit_obj` to crate an elf object.
|
|||
fn compile_lowered_trampoline( |
|||
&self, |
|||
component: &Component, |
|||
lowering: &LowerImport, |
|||
types: &ComponentTypes, |
|||
) -> Result<Box<dyn Any + Send>>; |
|||
|
|||
/// Emits the `trampolines` specified into the in-progress ELF object
|
|||
/// specified by `obj`.
|
|||
///
|
|||
/// Returns a map of trampoline information for where to find them all in
|
|||
/// the text section.
|
|||
///
|
|||
/// Note that this will also prepare unwinding information for all the
|
|||
/// trampolines as necessary.
|
|||
fn emit_obj( |
|||
&self, |
|||
trampolines: PrimaryMap<LoweredIndex, Box<dyn Any + Send>>, |
|||
obj: &mut Object<'static>, |
|||
) -> Result<PrimaryMap<LoweredIndex, TrampolineInfo>>; |
|||
} |
@ -0,0 +1,240 @@ |
|||
// Currently the `VMComponentContext` allocation by field looks like this:
|
|||
//
|
|||
// struct VMComponentContext {
|
|||
// magic: u32,
|
|||
// may_enter: u8,
|
|||
// may_leave: u8,
|
|||
// store: *mut dyn Store,
|
|||
// lowering_anyfuncs: [VMCallerCheckedAnyfunc; component.num_lowerings],
|
|||
// lowerings: [VMLowering; component.num_lowerings],
|
|||
// memories: [*mut VMMemoryDefinition; component.num_memories],
|
|||
// reallocs: [*mut VMCallerCheckedAnyfunc; component.num_reallocs],
|
|||
// }
|
|||
|
|||
use crate::component::{Component, LoweredIndex, RuntimeMemoryIndex, RuntimeReallocIndex}; |
|||
use crate::PtrSize; |
|||
|
|||
/// Equivalent of `VMCONTEXT_MAGIC` except for components.
|
|||
///
|
|||
/// This is stored at the start of all `VMComponentContext` structures adn
|
|||
/// double-checked on `VMComponentContext::from_opaque`.
|
|||
pub const VMCOMPONENT_MAGIC: u32 = u32::from_le_bytes(*b"comp"); |
|||
|
|||
/// Runtime offsets within a `VMComponentContext` for a specific component.
|
|||
#[derive(Debug, Clone, Copy)] |
|||
pub struct VMComponentOffsets<P> { |
|||
/// The host pointer size
|
|||
pub ptr: P, |
|||
|
|||
/// The number of lowered functions this component will be creating.
|
|||
pub num_lowerings: u32, |
|||
/// The number of memories which are recorded in this component for options.
|
|||
pub num_runtime_memories: u32, |
|||
/// The number of reallocs which are recorded in this component for options.
|
|||
pub num_runtime_reallocs: u32, |
|||
|
|||
// precalculated offsets of various member fields
|
|||
magic: u32, |
|||
may_enter: u32, |
|||
may_leave: u32, |
|||
store: u32, |
|||
lowering_anyfuncs: u32, |
|||
lowerings: u32, |
|||
memories: u32, |
|||
reallocs: u32, |
|||
size: u32, |
|||
} |
|||
|
|||
#[inline] |
|||
fn align(offset: u32, align: u32) -> u32 { |
|||
assert!(align.is_power_of_two()); |
|||
(offset + (align - 1)) & !(align - 1) |
|||
} |
|||
|
|||
impl<P: PtrSize> VMComponentOffsets<P> { |
|||
/// Creates a new set of offsets for the `component` specified configured
|
|||
/// additionally for the `ptr` size specified.
|
|||
pub fn new(ptr: P, component: &Component) -> Self { |
|||
let mut ret = Self { |
|||
ptr, |
|||
num_lowerings: component.num_lowerings.try_into().unwrap(), |
|||
num_runtime_memories: component.num_runtime_memories.try_into().unwrap(), |
|||
num_runtime_reallocs: component.num_runtime_reallocs.try_into().unwrap(), |
|||
magic: 0, |
|||
may_enter: 0, |
|||
may_leave: 0, |
|||
store: 0, |
|||
lowering_anyfuncs: 0, |
|||
lowerings: 0, |
|||
memories: 0, |
|||
reallocs: 0, |
|||
size: 0, |
|||
}; |
|||
|
|||
// Convenience functions for checked addition and multiplication.
|
|||
// As side effect this reduces binary size by using only a single
|
|||
// `#[track_caller]` location for each function instead of one for
|
|||
// each individual invocation.
|
|||
#[inline] |
|||
fn cmul(count: u32, size: u8) -> u32 { |
|||
count.checked_mul(u32::from(size)).unwrap() |
|||
} |
|||
|
|||
let mut next_field_offset = 0; |
|||
|
|||
macro_rules! fields { |
|||
(size($field:ident) = $size:expr, $($rest:tt)*) => { |
|||
ret.$field = next_field_offset; |
|||
next_field_offset = next_field_offset.checked_add(u32::from($size)).unwrap(); |
|||
fields!($($rest)*); |
|||
}; |
|||
(align($align:expr), $($rest:tt)*) => { |
|||
next_field_offset = align(next_field_offset, $align); |
|||
fields!($($rest)*); |
|||
}; |
|||
() => {}; |
|||
} |
|||
|
|||
fields! { |
|||
size(magic) = 4u32, |
|||
size(may_enter) = 1u32, |
|||
size(may_leave) = 1u32, |
|||
align(u32::from(ret.ptr.size())), |
|||
size(store) = cmul(2, ret.ptr.size()), |
|||
size(lowering_anyfuncs) = cmul(ret.num_lowerings, ret.ptr.size_of_vmcaller_checked_anyfunc()), |
|||
size(lowerings) = cmul(ret.num_lowerings, ret.ptr.size() * 2), |
|||
size(memories) = cmul(ret.num_runtime_memories, ret.ptr.size()), |
|||
size(reallocs) = cmul(ret.num_runtime_reallocs, ret.ptr.size()), |
|||
} |
|||
|
|||
ret.size = next_field_offset; |
|||
|
|||
// This is required by the implementation of
|
|||
// `VMComponentContext::from_opaque`. If this value changes then this
|
|||
// location needs to be updated.
|
|||
assert_eq!(ret.magic, 0); |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
/// The size, in bytes, of the host pointer.
|
|||
#[inline] |
|||
pub fn pointer_size(&self) -> u8 { |
|||
self.ptr.size() |
|||
} |
|||
|
|||
/// The offset of the `magic` field.
|
|||
#[inline] |
|||
pub fn magic(&self) -> u32 { |
|||
self.magic |
|||
} |
|||
|
|||
/// The offset of the `may_leave` field.
|
|||
#[inline] |
|||
pub fn may_leave(&self) -> u32 { |
|||
self.may_leave |
|||
} |
|||
|
|||
/// The offset of the `may_enter` field.
|
|||
#[inline] |
|||
pub fn may_enter(&self) -> u32 { |
|||
self.may_enter |
|||
} |
|||
|
|||
/// The offset of the `store` field.
|
|||
#[inline] |
|||
pub fn store(&self) -> u32 { |
|||
self.store |
|||
} |
|||
|
|||
/// The offset of the `lowering_anyfuncs` field.
|
|||
#[inline] |
|||
pub fn lowering_anyfuncs(&self) -> u32 { |
|||
self.lowering_anyfuncs |
|||
} |
|||
|
|||
/// The offset of `VMCallerCheckedAnyfunc` for the `index` specified.
|
|||
#[inline] |
|||
pub fn lowering_anyfunc(&self, index: LoweredIndex) -> u32 { |
|||
assert!(index.as_u32() < self.num_lowerings); |
|||
self.lowering_anyfuncs() |
|||
+ index.as_u32() * u32::from(self.ptr.size_of_vmcaller_checked_anyfunc()) |
|||
} |
|||
|
|||
/// The offset of the `lowerings` field.
|
|||
#[inline] |
|||
pub fn lowerings(&self) -> u32 { |
|||
self.lowerings |
|||
} |
|||
|
|||
/// The offset of the `VMLowering` for the `index` specified.
|
|||
#[inline] |
|||
pub fn lowering(&self, index: LoweredIndex) -> u32 { |
|||
assert!(index.as_u32() < self.num_lowerings); |
|||
self.lowerings() + index.as_u32() * u32::from(2 * self.ptr.size()) |
|||
} |
|||
|
|||
/// The offset of the `callee` for the `index` specified.
|
|||
#[inline] |
|||
pub fn lowering_callee(&self, index: LoweredIndex) -> u32 { |
|||
self.lowering(index) + self.lowering_callee_offset() |
|||
} |
|||
|
|||
/// The offset of the `data` for the `index` specified.
|
|||
#[inline] |
|||
pub fn lowering_data(&self, index: LoweredIndex) -> u32 { |
|||
self.lowering(index) + self.lowering_data_offset() |
|||
} |
|||
|
|||
/// The size of the `VMLowering` type
|
|||
#[inline] |
|||
pub fn lowering_size(&self) -> u8 { |
|||
2 * self.ptr.size() |
|||
} |
|||
|
|||
/// The offset of the `callee` field within the `VMLowering` type.
|
|||
#[inline] |
|||
pub fn lowering_callee_offset(&self) -> u32 { |
|||
0 |
|||
} |
|||
|
|||
/// The offset of the `data` field within the `VMLowering` type.
|
|||
#[inline] |
|||
pub fn lowering_data_offset(&self) -> u32 { |
|||
u32::from(self.ptr.size()) |
|||
} |
|||
|
|||
/// The offset of the base of the `runtime_memories` field
|
|||
#[inline] |
|||
pub fn runtime_memories(&self) -> u32 { |
|||
self.memories |
|||
} |
|||
|
|||
/// The offset of the `*mut VMMemoryDefinition` for the runtime index
|
|||
/// provided.
|
|||
#[inline] |
|||
pub fn runtime_memory(&self, index: RuntimeMemoryIndex) -> u32 { |
|||
assert!(index.as_u32() < self.num_runtime_memories); |
|||
self.runtime_memories() + index.as_u32() * u32::from(self.ptr.size()) |
|||
} |
|||
|
|||
/// The offset of the base of the `runtime_reallocs` field
|
|||
#[inline] |
|||
pub fn runtime_reallocs(&self) -> u32 { |
|||
self.reallocs |
|||
} |
|||
|
|||
/// The offset of the `*mut VMCallerCheckedAnyfunc` for the runtime index
|
|||
/// provided.
|
|||
#[inline] |
|||
pub fn runtime_realloc(&self, index: RuntimeReallocIndex) -> u32 { |
|||
assert!(index.as_u32() < self.num_runtime_reallocs); |
|||
self.runtime_reallocs() + index.as_u32() * u32::from(self.ptr.size()) |
|||
} |
|||
|
|||
/// Return the size of the `VMComponentContext` allocation.
|
|||
#[inline] |
|||
pub fn size_of_vmctx(&self) -> u32 { |
|||
self.size |
|||
} |
|||
} |
@ -0,0 +1,80 @@ |
|||
use anyhow::Result; |
|||
use wasmtime::component::Component; |
|||
|
|||
#[test] |
|||
fn can_compile() -> Result<()> { |
|||
let engine = super::engine(); |
|||
let libc = r#" |
|||
(module $libc |
|||
(memory (export "memory") 1) |
|||
(func (export "canonical_abi_realloc") (param i32 i32 i32 i32) (result i32) |
|||
unreachable) |
|||
(func (export "canonical_abi_free") (param i32 i32 i32) |
|||
unreachable) |
|||
) |
|||
(instance $libc (instantiate (module $libc))) |
|||
"#; |
|||
Component::new( |
|||
&engine, |
|||
r#"(component |
|||
(import "" (func $f)) |
|||
(func (canon.lower (func $f))) |
|||
)"#, |
|||
)?; |
|||
Component::new( |
|||
&engine, |
|||
format!( |
|||
r#"(component |
|||
(import "" (func $f (param string))) |
|||
{libc} |
|||
(func (canon.lower (into $libc) (func $f))) |
|||
)"# |
|||
), |
|||
)?; |
|||
Component::new( |
|||
&engine, |
|||
format!( |
|||
r#"(component |
|||
(import "f1" (func $f1 (param string) (result string))) |
|||
{libc} |
|||
(func (canon.lower (into $libc) (func $f1))) |
|||
|
|||
(import "f2" (func $f2 (param u32) (result (list u8)))) |
|||
(instance $libc2 (instantiate (module $libc))) |
|||
(func (canon.lower (into $libc2) (func $f2))) |
|||
|
|||
(func (canon.lower (into $libc2) (func $f1))) |
|||
(func (canon.lower (into $libc) (func $f2))) |
|||
)"# |
|||
), |
|||
)?; |
|||
Component::new( |
|||
&engine, |
|||
format!( |
|||
r#"(component |
|||
(import "log" (func $log (param string))) |
|||
{libc} |
|||
(func $log_lower (canon.lower (into $libc) (func $log))) |
|||
|
|||
(module $logger |
|||
(import "host" "log" (func $log (param i32 i32))) |
|||
(import "libc" "memory" (memory 1)) |
|||
|
|||
(func (export "call") |
|||
i32.const 0 |
|||
i32.const 0 |
|||
call $log) |
|||
) |
|||
(instance $logger (instantiate (module $logger) |
|||
(with "host" (instance (export "log" (func $log_lower)))) |
|||
(with "libc" (instance $libc)) |
|||
)) |
|||
|
|||
(func (export "call") |
|||
(canon.lift (func) (func $logger "call")) |
|||
) |
|||
)"# |
|||
), |
|||
)?; |
|||
Ok(()) |
|||
} |
Loading…
Reference in new issue