A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Give core Wasm translation the enclosing component's alias regions (#15173)
Nick Fitzgerald3528e2dbd2b9
diff --git a/crates/cranelift/src/alias_region.rs b/crates/cranelift/src/alias_region.rsindex bfe9447536..f5ea88a052 100644--- a/crates/cranelift/src/alias_region.rs+++ b/crates/cranelift/src/alias_region.rs@@ -106,6 +106,7 @@ enum VmType {reason = "generated uniformly for all VM types via `for_each_vm_type!`")]VMTransmitLocalState,+ HandleTableSlotsArray,}/// A key that uniquely identifies an alias region across an entire compilation.@@ -690,6 +691,7 @@ macro_rules! define_vm_type_alias_region_helpers {// and is marked `#[aggregate]` in `for_each_vm_type!` so that no accessor is// generated for it at all.(@field_ty $pt:expr, VmPtr < $g:ty >) => { $pt };+ (@field_ty $pt:expr, VmPtr < VmPtr < $g:ty >>) => { $pt };(@field_ty $pt:expr, Option < VmPtr < $g:ty >>) => { $pt };(@field_ty $pt:expr, AtomicUsize) => { $pt };(@field_ty $pt:expr, usize) => { $pt };@@ -1153,6 +1155,7 @@ macro_rules! define_vmctx_alias_region_helpers {// pointer type as an `ir::Type`).(@field_ty $pt:expr, u32) => { ir::types::I32 };(@field_ty $pt:expr, VmPtr < $g:ident >) => { $pt };+ (@field_ty $pt:expr, VmPtr < VmPtr < $g:ident >>) => { $pt };(@field_ty $pt:expr, VMSharedTypeIndex) => { ir::types::I32 };(@field_ty $pt:expr, AtomicU64) => { ir::types::I64 };(@field_ty $pt:expr, unsafe extern "C" fn) => { $pt };@@ -1558,6 +1561,16 @@ whereintroducer}+ /// Get the `Field` for an element of the out-of-line array that+ /// `VMStoreContext::handle_table_slots` points at.+ ///+ /// The returned `Field` is relative to the array's base pointer.+ pub fn handle_table_slots_array(&mut self) -> Field<'_, Offsets> {+ let ty = self.pointer_type;+ let flags = ir::MemFlagsData::trusted();+ Field::new(self, VmType::HandleTableSlotsArray, 0, flags, ty)+ }+/// Get the alias region shared by all memories that cross a module boundary/// and whose definition we do not statically know.pub fn public_memory_region(&mut self, func: &mut ir::Function) -> ir::AliasRegion {diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rsindex 3a861810a0..a96cf96bdf 100644--- a/crates/cranelift/src/compiler.rs+++ b/crates/cranelift/src/compiler.rs@@ -445,7 +445,7 @@ impl Compiler {context.func.signature = wasm_call_signature(&*self.isa, ty, &self.tunables);let (namespace, index) = key.into_raw_parts();context.func.name = UserFuncName::User(UserExternalName { namespace, index });- let mut func_env = FuncEnvironment::new(self, translation, types, ty, key, None, 0);+ let mut func_env = FuncEnvironment::new(self, translation, types, ty, key, None, 0, None);compiler.cx.func_translator@@ -491,6 +491,7 @@ impl wasmtime_environ::Compiler for Compiler {key: FuncKey,input: FunctionBodyData<'_>,types: &ModuleTypesBuilder,+ component: Option<wasmtime_environ::EnclosingComponent<'_>>,symbol: &str,) -> Result<CompiledFunctionBody, CompileError> {log::trace!("compiling Wasm function: {key:?} = {symbol:?}");@@ -531,6 +532,7 @@ impl wasmtime_environ::Compiler for Compiler {key,Some(func_index),func_body_offset,+ component,);// The `stack_limit` global value below is the implementation of stackdiff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rsindex 30123dbcae..a6a2d26854 100644--- a/crates/cranelift/src/func_environ.rs+++ b/crates/cranelift/src/func_environ.rs@@ -264,6 +264,16 @@ pub struct FuncEnvironment<'module_environment> {/// Cached alias regions for alias analysis.pub(crate) alias_regions: AliasRegions<VMOffsets<u8>>,++ /// Alias regions for the enclosing component's offsets.+ ///+ /// Present only when this module is being compiled as part of a component.+ #[allow(+ dead_code,+ reason = "read by the fused-adapter fast paths in a follow-up commit"+ )]+ pub(crate) component_regions:+ Option<AliasRegions<wasmtime_environ::component::VMComponentOffsets<u8>>>,}impl<'module_environment> FuncEnvironment<'module_environment> {@@ -275,6 +285,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> {key: FuncKey,func_index: Option<FuncIndex>,func_body_offset: u64,+ component: Option<wasmtime_environ::EnclosingComponent<'_>>,) -> Self {let tunables = compiler.tunables();let builtin_functions = BuiltinFunctions::new(compiler);@@ -291,7 +302,14 @@ impl<'module_environment> FuncEnvironment<'module_environment> {let isa = compiler.isa();let offsets = VMOffsets::new(isa.pointer_bytes(), &translation.module);+ let component_regions = component.map(|c| {+ AliasRegions::new(wasmtime_environ::component::VMComponentOffsets::new(+ isa.pointer_bytes(),+ c.0,+ ))+ });Self {+ component_regions,key,isa,module: &translation.module,diff --git a/crates/environ/src/compile/mod.rs b/crates/environ/src/compile/mod.rsindex a84e38d570..78c5cc1a31 100644--- a/crates/environ/src/compile/mod.rs+++ b/crates/environ/src/compile/mod.rs@@ -71,6 +71,23 @@ impl core::error::Error for CompileError {}}+/// The component a core module is being compiled as part of.+///+/// A newtype, rather than a bare `&Component`, so that the `Compiler` trait can+/// name the parameter without depending on `cfg(feature = "component-model")`.+#[cfg(feature = "component-model")]+#[derive(Clone, Copy)]+pub struct EnclosingComponent<'a>(pub &'a crate::component::Component);++/// Stand-in for `EnclosingComponent` when components are disabled at compile+/// time.+#[cfg(not(feature = "component-model"))]+#[derive(Clone, Copy)]+pub enum EnclosingComponent<'a> {+ #[doc(hidden)]+ Never(core::convert::Infallible, core::marker::PhantomData<&'a ()>),+}+/// Implementation of an incremental compilation's key/value cache store.////// In theory, this could just be Cranelift's `CacheKvStore` trait, but it is not as we want to@@ -247,12 +264,17 @@ pub trait Compiler: Send + Sync {/// The body of the function is available in `data` and configuration/// values are also passed in via `tunables`. Type information in/// `translation` is all relative to `types`.+ ///+ /// `component` is the component this module is being compiled as part of,+ /// if any. Fused adapters need it to compute offsets within the+ /// `VMComponentContext` they will run against.fn compile_function(&self,translation: &ModuleTranslation<'_>,key: FuncKey,data: FunctionBodyData<'_>,types: &ModuleTypesBuilder,+ component: Option<EnclosingComponent<'_>>,symbol: &str,) -> Result<CompiledFunctionBody, CompileError>;diff --git a/crates/environ/src/lib.rs b/crates/environ/src/lib.rsindex f9f0aafc99..2bbeb7c0eb 100644--- a/crates/environ/src/lib.rs+++ b/crates/environ/src/lib.rs@@ -10,6 +10,9 @@#![deny(missing_docs)]#![warn(clippy::cast_sign_loss)]#![no_std]+// `for_each_vm_type!`'s offset generation recurses once per field of a type,+// and some of our `VM*` types have enough fields to exceed the default.+#![recursion_limit = "512"]#[cfg(any(feature = "std", test))]#[macro_use]diff --git a/crates/environ/src/vmctxtypes.rs b/crates/environ/src/vmctxtypes.rsindex d0171280fb..0407abcb52 100644--- a/crates/environ/src/vmctxtypes.rs+++ b/crates/environ/src/vmctxtypes.rs@@ -393,6 +393,9 @@ macro_rules! vmctx_field_size {(($p:expr) VmPtr < $g:ident >) => {u32::from($p)};+ (($p:expr) VmPtr < VmPtr < $g:ident >>) => {+ u32::from($p)+ };// `VMLowering` is a pair of pointers, and is not itself defined by// `for_each_vm_type!`.(($p:expr) VMLowering) => {diff --git a/crates/environ/src/vmoffsets.rs b/crates/environ/src/vmoffsets.rsindex a0e022ca2e..66d8498cd1 100644--- a/crates/environ/src/vmoffsets.rs+++ b/crates/environ/src/vmoffsets.rs@@ -62,6 +62,7 @@ macro_rules! define_vm_type_offsets {// references; and `VMGlobalKind` is a `repr(C, u32)` enum with a `u32`// payload.(@size ($p:expr) VmPtr < $g:ty >) => { u32::from($p) };+ (@size ($p:expr) VmPtr < VmPtr < $g:ty >>) => { u32::from($p) };(@size ($p:expr) Option < VmPtr < $g:ty >>) => { u32::from($p) };(@size ($p:expr) AtomicUsize) => { u32::from($p) };(@size ($p:expr) usize) => { u32::from($p) };@@ -122,6 +123,7 @@ macro_rules! define_vm_type_offsets {// with 64-bit fields must therefore put them first and force their own alignment with// `#[repr(C, align(8))]`, as `VMStoreContext` does.(@align ($p:expr) VmPtr < $g:ty >) => { u32::from($p) };+ (@align ($p:expr) VmPtr < VmPtr < $g:ty >>) => { u32::from($p) };(@align ($p:expr) Option < VmPtr < $g:ty >>) => { u32::from($p) };(@align ($p:expr) AtomicUsize) => { u32::from($p) };(@align ($p:expr) usize) => { u32::from($p) };diff --git a/crates/wasmtime/src/compile.rs b/crates/wasmtime/src/compile.rsindex 6c9c68424d..adba68a130 100644--- a/crates/wasmtime/src/compile.rs+++ b/crates/wasmtime/src/compile.rs@@ -313,7 +313,7 @@ impl<'a> CompileInputs<'a> {let mut ret = CompileInputs { inputs: vec![] };let module_index = StaticModuleIndex::from_u32(0);- ret.collect_inputs_in_translations(types, [(module_index, translation, functions)]);+ ret.collect_inputs_in_translations(types, None, [(module_index, translation, functions)]);ret}@@ -337,7 +337,11 @@ impl<'a> CompileInputs<'a> {let mut ret = CompileInputs { inputs: vec![] };- ret.collect_inputs_in_translations(types.module_types_builder(), module_translations);+ ret.collect_inputs_in_translations(+ types.module_types_builder(),+ Some(wasmtime_environ::EnclosingComponent(&component.component)),+ module_translations,+ );let tunables = engine.tunables();for i in component@@ -471,6 +475,7 @@ impl<'a> CompileInputs<'a> {fn collect_inputs_in_translations(&mut self,types: &'a ModuleTypesBuilder,+ component: Option<wasmtime_environ::EnclosingComponent<'a>>,translations: impl IntoIterator<Item = (StaticModuleIndex,@@ -507,7 +512,14 @@ impl<'a> CompileInputs<'a> {let offset = data.original_position();let start_srcloc = FilePos::new(u32::try_from(offset).unwrap());let function = compiler- .compile_function(translation, key, func_body_data, types, &symbol)+ .compile_function(+ translation,+ key,+ func_body_data,+ types,+ component,+ &symbol,+ ).with_context(|| format!("failed to compile: {symbol}"))?;Ok(CompileOutput {diff --git a/crates/winch/src/compiler.rs b/crates/winch/src/compiler.rsindex 4568341ff4..2bfc949f50 100644--- a/crates/winch/src/compiler.rs+++ b/crates/winch/src/compiler.rs@@ -116,6 +116,7 @@ impl wasmtime_environ::Compiler for Compiler {key: FuncKey,data: FunctionBodyData<'_>,types: &ModuleTypesBuilder,+ _component: Option<wasmtime_environ::EnclosingComponent<'_>>,symbol: &str,) -> Result<CompiledFunctionBody, CompileError> {log::trace!("compiling function: {key:?} = {symbol:?}");@@ -250,12 +251,13 @@ impl wasmtime_environ::Compiler for NoInlineCompiler {key: FuncKey,data: FunctionBodyData<'_>,types: &ModuleTypesBuilder,+ component: Option<wasmtime_environ::EnclosingComponent<'_>>,symbol: &str,) -> Result<CompiledFunctionBody, CompileError> {let input = data.body.clone();let mut body = self.0- .compile_function(translation, key, data, types, symbol)?;+ .compile_function(translation, key, data, types, component, symbol)?;if let Some(c) = self.0.inlining_compiler() {c.finish_compiling(&mut body, Some(input), symbol).map_err(|e| CompileError::Codegen(e.to_string()))?;