Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Avoid accidentally quadratic re-export chain lookups in component analyses (#14611)

Nick Fitzgerald9d199bc0502c
diff --git a/crates/environ/src/component/same_vmctx.rs b/crates/environ/src/component/same_vmctx.rs
index 72be541d76..9ff7e27935 100644
--- a/crates/environ/src/component/same_vmctx.rs
+++ b/crates/environ/src/component/same_vmctx.rs
@@ -31,7 +31,9 @@ use crate::component::dfg::{
};
use crate::prelude::*;
use crate::union_find::UnionFind;
-use crate::{EntityIndex, EntityRef, FuncIndex, PrimaryMap, SecondaryMap, StaticModuleIndex};
+use crate::{
+ EntityIndex, EntityRef, FuncIndex, ImportPositions, PrimaryMap, SecondaryMap, StaticModuleIndex,
+};
use core::mem;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
@@ -239,11 +241,17 @@ pub fn analyze_same_vmctx_imports(
static_modules: &mut PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
) {
let mut builder = SameVmctxBuilder::default();
+ let mut keys = VmctxKeys::new(static_modules);
// Scratch space, reused across the calls below.
- let mut keys = Vec::new();
+ let mut func_keys = Vec::new();
let mut stack = Vec::new();
+ // Resolve every core instance's arguments, in order.
+ for (id, instance) in dfg.instances.iter() {
+ keys.push(dfg, id, instance);
+ }
+
// Observe every core module instantiation that the component itself
// performs.
for effect in dfg.side_effects.iter() {
@@ -251,18 +259,30 @@ pub fn analyze_same_vmctx_imports(
continue;
};
- let Instance::Static(module, args) = &dfg.instances[*id] else {
+ let Instance::Static(module, _) = &dfg.instances[*id] else {
// A module imported from the host is not one we are compiling.
continue;
};
- observe_instantiation(&mut keys, &mut builder, dfg, static_modules, *module, args);
+ observe_instantiation(
+ &mut func_keys,
+ &mut builder,
+ static_modules,
+ *module,
+ keys.args[*id].iter().copied(),
+ );
}
// Adapter modules do not appear in `side_effects`; they are instantiated
// lazily, at most once each, as their adapters are referenced.
for (_, (module, args)) in dfg.adapter_modules.iter() {
- observe_instantiation(&mut keys, &mut builder, dfg, static_modules, *module, args);
+ observe_instantiation(
+ &mut func_keys,
+ &mut builder,
+ static_modules,
+ *module,
+ args.iter().map(|arg| keys.vmctx_key(dfg, arg)),
+ );
}
// The host may instantiate an exported module with anything at all.
@@ -283,30 +303,30 @@ pub fn analyze_same_vmctx_imports(
}
}
-/// Observe an instantiation of `module` with the given positional arguments.
+/// Observe an instantiation of `module`, where `args` yields the `vmctx` key
+/// of each of its positional arguments.
///
-/// `keys` is scratch space, reused across calls to avoid reallocating a vector
-/// per instantiation. Its contents on entry are ignored.
+/// `func_keys` is scratch space, reused across calls to avoid reallocating a
+/// vector per instantiation. Its contents on entry are ignored.
fn observe_instantiation(
- keys: &mut Vec<Option<VmctxKey>>,
+ func_keys: &mut Vec<Option<VmctxKey>>,
builder: &mut SameVmctxBuilder,
- dfg: &ComponentDfg,
static_modules: &PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
module: StaticModuleIndex,
- args: &[CoreDef],
+ args: impl Iterator<Item = Option<VmctxKey>>,
) {
let translation = &static_modules[module];
- keys.clear();
- keys.resize(translation.module.num_imported_funcs, None);
+ func_keys.clear();
+ func_keys.resize(translation.module.num_imported_funcs, None);
- for (position, arg) in args.iter().enumerate() {
+ for (position, key) in args.enumerate() {
let Some(EntityIndex::Function(func)) = translation.module.import_index(position) else {
continue;
};
- keys[func.index()] = vmctx_key(dfg, static_modules, arg);
+ func_keys[func.index()] = key;
}
- builder.observe_instantiation(module, keys);
+ builder.observe_instantiation(module, func_keys);
}
/// Observe every static module reachable from `export`, any of which the host
@@ -335,24 +355,48 @@ fn observe_exported_modules<'a>(
}
}
-/// The `vmctx` that `def`'s `VMFuncRef` carries, when we can see it
-/// statically.
-fn vmctx_key(
- dfg: &ComponentDfg,
- static_modules: &PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
- def: &CoreDef,
-) -> Option<VmctxKey> {
- // A module may import a function and re-export it, in which case the
- // `vmctx` belongs to whichever instance actually defines the function, so
- // follow such chains back to the definition. This mirrors
- // `translate::resolve_core_export`.
- let mut def = def;
+/// The `vmctx` key of every core instance's arguments, memoized so that
+/// resolving a re-export takes a single step rather than walking the whole
+/// chain of re-exports back to the definition.
+struct VmctxKeys<'a> {
+ /// The `vmctx` key of each instance's arguments, by import position.
+ ///
+ /// Empty for instances of host modules, which have no import positions
+ /// that we can see into.
+ args: PrimaryMap<InstanceId, Vec<Option<VmctxKey>>>,
- // The instance most recently looked at. The walk strictly decreases
- // through this, which is what makes it terminate.
- let mut previous: Option<InstanceId> = None;
+ /// The static modules being instantiated.
+ static_modules: &'a PrimaryMap<StaticModuleIndex, ModuleTranslation<'a>>,
- loop {
+ /// The position of each import of each static module.
+ import_positions: PrimaryMap<StaticModuleIndex, ImportPositions>,
+}
+
+impl<'a> VmctxKeys<'a> {
+ fn new(static_modules: &'a PrimaryMap<StaticModuleIndex, ModuleTranslation<'a>>) -> Self {
+ VmctxKeys {
+ args: PrimaryMap::new(),
+ static_modules,
+ import_positions: static_modules
+ .values()
+ .map(|translation| translation.module.import_positions())
+ .collect(),
+ }
+ }
+
+ /// Resolve the arguments of the next instance, `id`.
+ fn push(&mut self, dfg: &ComponentDfg, id: InstanceId, instance: &Instance) {
+ let keys = match instance {
+ Instance::Static(_, args) => args.iter().map(|arg| self.vmctx_key(dfg, arg)).collect(),
+ Instance::Import(..) => Vec::new(),
+ };
+ let pushed = self.args.push(keys);
+ debug_assert_eq!(pushed, id);
+ }
+
+ /// The `vmctx` that `def`'s `VMFuncRef` carries, when we can see it
+ /// statically.
+ fn vmctx_key(&self, dfg: &ComponentDfg, def: &CoreDef) -> Option<VmctxKey> {
// NB: deliberately exhaustive so that new variants must be classified
// here.
let export = match def {
@@ -362,7 +406,7 @@ fn vmctx_key(
return Some(VmctxKey::Component);
}
- CoreDef::Adapter(id) => return adapter_vmctx_key(dfg, static_modules, *id),
+ CoreDef::Adapter(id) => return adapter_vmctx_key(dfg, self.static_modules, *id),
// Not a function, so it never satisfies a function import. Be
// conservative anyway.
@@ -371,14 +415,7 @@ fn vmctx_key(
CoreDef::Export(export) => export,
};
- if previous.is_some_and(|p| export.instance.index() >= p.index()) {
- // Unreachable, since an instantiation's arguments are exports of
- // earlier instances. Give up rather than loop forever.
- return None;
- }
- previous = Some(export.instance);
-
- let Instance::Static(module, args) = &dfg.instances[export.instance] else {
+ let Instance::Static(module, _) = &dfg.instances[export.instance] else {
// An instance of a host module, whose exports we cannot see into.
return None;
};
@@ -386,23 +423,18 @@ fn vmctx_key(
let ExportItem::Index(index) = &export.item else {
// Names are only used for instances of modules whose shape is not
// statically known, which the arm above filtered out.
- return None;
+ unreachable!()
};
- let module = &static_modules[*module].module;
-
// The common case: this instance's module defines the function, so
// the function's context is this instance's context.
- if !module.is_imported(*index) {
+ let Some(position) = self.import_positions[*module].get(*index) else {
return Some(VmctxKey::CoreInstance(export.instance));
- }
+ };
- // Otherwise it is a re-export of one of the module's imports, so keep
- // walking through whichever argument satisfied that import.
- let position = module
- .import_position(*index)
- .expect("imported entities always have an associated import initializer");
- def = &args[position];
+ // Otherwise it is a re-export of one of the module's imports, so it has
+ // the same context as whichever argument satisfied that import.
+ *self.args.get(export.instance)?.get(position)?
}
}
diff --git a/crates/environ/src/component/translate.rs b/crates/environ/src/component/translate.rs
index 69492d9e43..88d2933fe4 100644
--- a/crates/environ/src/component/translate.rs
+++ b/crates/environ/src/component/translate.rs
@@ -4,9 +4,9 @@ use crate::component::*;
use crate::prelude::*;
use crate::{
DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, EngineOrModuleTypeIndex,
- EntityIndex, FactInlineIntrinsic, FuncKey, KnownEntity, KnownGlobal, ModuleEnvironment,
- ModuleInternedTypeIndex, ModuleTranslation, ModuleTypesBuilder, PrimaryMap, ScopeVec, TagIndex,
- Tunables, TypeConvert, WasmHeapType, WasmResult, WasmValType,
+ EntityIndex, FactInlineIntrinsic, FuncKey, ImportPositions, KnownEntity, KnownGlobal,
+ ModuleEnvironment, ModuleInternedTypeIndex, ModuleTranslation, ModuleTypesBuilder, PrimaryMap,
+ ScopeVec, TagIndex, Tunables, TypeConvert, WasmHeapType, WasmResult, WasmValType,
};
use core::str::FromStr;
use cranelift_entity::{EntityRef, SecondaryMap};
@@ -563,7 +563,7 @@ impl<'a, 'data> Translator<'a, 'data> {
// First, abstract interpret the initializers to create a map from each
// static module to its abstract set of instantiations.
let mut instantiations = SecondaryMap::<StaticModuleIndex, AbstractInstantiations>::new();
- let mut instances = StaticInstances::new();
+ let mut instances = StaticInstances::new(&self.static_modules);
for init in &translation.component.initializers {
match init {
GlobalInitializer::InstantiateModule(instantiation, _) => match instantiation {
@@ -592,12 +592,7 @@ impl<'a, 'data> Translator<'a, 'data> {
// is a property of the whole component and not of a single module's
// instantiations; see `ModuleTranslation::known_imported_globals` for
// details.
- let ambiguous = ambiguous_entities(
- &self.static_modules,
- translation,
- &instantiations,
- &instances,
- );
+ let ambiguous = ambiguous_entities(translation, &instantiations, &instances);
// Fourth, record which of each module's own defined entities all of
// their importers agree on, which lets those modules use a precise alias
@@ -654,12 +649,7 @@ impl<'a, 'data> Translator<'a, 'data> {
macro_rules! record_known_entity {
($variant:ident, $imported:expr, $defined_index:ident, $known:ident, $wrap:expr) => {{
let Some((arg_module, EntityIndex::$variant(arg_entity))) =
- unambiguous_entity(
- &self.static_modules,
- &instances,
- &ambiguous.entities,
- arg,
- )
+ unambiguous_entity(&instances, &ambiguous.entities, arg)
else {
continue;
};
@@ -733,7 +723,7 @@ impl<'a, 'data> Translator<'a, 'data> {
// inlinable direct call!
CoreDef::Export(export) => {
let Some((arg_module, arg_entity)) =
- resolve_core_export(&self.static_modules, &instances, export)
+ resolve_core_export(&instances, export)
else {
// Either an instance of a dynamic module that
// is not part of this component, or a
@@ -1945,13 +1935,67 @@ mod pre_inlining {
}
use pre_inlining::PreInliningComponentTypes;
-/// A map from each runtime instance to the static module it is an instance of
-/// and the arguments it was instantiated with, when we statically know them.
-///
-/// `None` for instances of modules that are not part of this component, and
-/// whose shape we therefore cannot see into.
-type StaticInstances<'a> =
- PrimaryMap<RuntimeInstanceIndex, Option<(StaticModuleIndex, &'a [CoreDef])>>;
+/// A map from each runtime instance to the static module it is an instance of,
+/// along with the definition that each of its arguments resolves to, when we
+/// statically know them.
+struct StaticInstances {
+ /// The position of each import of each static module.
+ import_positions: PrimaryMap<StaticModuleIndex, ImportPositions>,
+
+ /// Each instance's module and what each of its arguments resolves to, by
+ /// import position, as computed by `resolve_core_export`.
+ ///
+ /// `None` for instances of modules that are not part of this component,
+ /// and whose shape we therefore cannot see into.
+ instances: PrimaryMap<
+ RuntimeInstanceIndex,
+ Option<(
+ StaticModuleIndex,
+ Vec<Option<(StaticModuleIndex, EntityIndex)>>,
+ )>,
+ >,
+}
+
+impl StaticInstances {
+ fn new(static_modules: &PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>) -> Self {
+ StaticInstances {
+ import_positions: static_modules
+ .values()
+ .map(|translation| translation.module.import_positions())
+ .collect(),
+ instances: PrimaryMap::new(),
+ }
+ }
+
+ /// Push the next runtime instance.
+ ///
+ /// It must be an instance of the given static module with the given
+ /// arguments, or `None` for an instance of a module that is not part of
+ /// this component.
+ fn push(&mut self, instance: Option<(StaticModuleIndex, &[CoreDef])>) {
+ let instance = instance.map(|(module, args)| {
+ let next = self.instances.next_key();
+ let args = args
+ .iter()
+ .map(|arg| match arg {
+ CoreDef::Export(export) => {
+ // An instantiation's arguments are always exports of
+ // instances created before the instance being
+ // instantiated.
+ assert!(export.instance < next);
+ resolve_core_export(self, export)
+ }
+
+ CoreDef::InstanceFlags(_)
+ | CoreDef::Trampoline(_)
+ | CoreDef::UnsafeIntrinsic(_) => None,
+ })
+ .collect();
+ (module, args)
+ });
+ self.instances.push(instance);
+ }
+}
/// Every entity whose identity is not statically known to everything that can
/// access it.
@@ -1989,72 +2033,41 @@ fn component_flags(def: &CoreDef) -> Option<KnownGlobal> {
/// Therefore a returned `Some((module, entity))` always satisfies
/// `!static_modules[module].module.is_imported(entity)`.
fn resolve_core_export(
- static_modules: &PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
- instances: &StaticInstances<'_>,
+ instances: &StaticInstances,
export: &CoreExport<EntityIndex>,
) -> Option<(StaticModuleIndex, EntityIndex)> {
- let mut instance = export.instance;
- let mut item = &export.item;
-
- loop {
- // This can be an instance of a dynamic module that is not part of this
- // component, rather than a statically-known module inside of it.
- let (module, args) = instances[instance]?;
-
- let index = match item {
- ExportItem::Index(index) => *index,
- // Names are only used for instances of modules whose shape we don't
- // statically know, which we already filtered out.
- ExportItem::Name(_) => return None,
- };
+ let (module, args) = instances.instances[export.instance].as_ref()?;
- // The common case: this instance's module defines the entity itself, so
- // we've bottomed out at its canonical identity.
- if !static_modules[module].module.is_imported(index) {
- return Some((module, index));
- }
+ let ExportItem::Index(index) = export.item else {
+ // Names are only used for instances of modules whose shape we don't
+ // statically know, which we already filtered out.
+ unreachable!()
+ };
- // Otherwise this is a re-export of one of the module's imports, so keep
- // walking through whichever argument satisfied that import.
- let position = static_modules[module]
- .module
- .import_position(index)
- .expect("imported entities always have an associated import initializer");
- match &args[position] {
- CoreDef::Export(next) => {
- // An instantiation's arguments are always exports of instances
- // created before the instance being instantiated: `LinearizeDfg`
- // builds the argument `CoreDef`s before assigning the new
- // instance's `RuntimeInstanceIndex`, and would panic building an
- // export of an instance it had not linearized yet. So this walk
- // strictly decreases and must terminate.
- assert!(next.instance < instance);
- instance = next.instance;
- item = &next.item;
- }
+ match instances.import_positions[*module].get(index) {
+ // The common case: this instance's module defines the entity itself,
+ // so we've bottomed out at its canonical identity.
+ None => Some((*module, index)),
- // The chain bottoms out in something that is not an export of
- // another instance in this component, so there is no defining module
- // for us to name.
- CoreDef::InstanceFlags(_) | CoreDef::Trampoline(_) | CoreDef::UnsafeIntrinsic(_) => {
- return None;
- }
- }
+ // Otherwise this is a re-export of one of the module's imports, which
+ // resolves to the same thing as whichever argument satisfied that
+ // import. `StaticInstances::push` already resolved that argument all
+ // the way back to its definition.
+ Some(position) => args[position],
}
}
/// Same as `resolve_core_export`, but for a `CoreDef` that must additionally be
/// unambiguous.
fn unambiguous_entity(
- static_modules: &PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
- instances: &StaticInstances<'_>,
+ instances: &StaticInstances,
ambiguous: &HashSet<(StaticModuleIndex, EntityIndex)>,
def: &CoreDef,
) -> Option<(StaticModuleIndex, EntityIndex)> {
let CoreDef::Export(export) = def else {
return None;
};
- let entity = resolve_core_export(static_modules, instances, export)?;
+ let entity = resolve_core_export(instances, export)?;
if ambiguous.contains(&entity) {
return None;
}
@@ -2089,16 +2102,15 @@ fn unambiguous_entity(
/// every other module that can reach that definition, and not just the
/// re-exporter.
fn ambiguous_entities(
- static_modules: &PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
translation: &ComponentTranslation,
instantiations: &SecondaryMap<StaticModuleIndex, dfg::AbstractInstantiations<'_>>,
- instances: &StaticInstances<'_>,
+ instances: &StaticInstances,
) -> Ambiguous {
let mut ambiguous = Ambiguous::default();
let mut mark = |def: &CoreDef| match def {
CoreDef::Export(export) => {
- if let Some(entity) = resolve_core_export(static_modules, instances, export) {
+ if let Some(entity) = resolve_core_export(instances, export) {
ambiguous.entities.insert(entity);
}
}
diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs
index 9db128df5b..f15583615a 100644
--- a/crates/environ/src/module.rs
+++ b/crates/environ/src/module.rs
@@ -295,6 +295,35 @@ pub enum Initializer {
},
}
+/// The position of each of a module's imports, suitable for indexing an
+/// instantiation's argument list.
+#[derive(Debug, Default)]
+pub struct ImportPositions {
+ functions: Vec<u32>,
+ tables: Vec<u32>,
+ memories: Vec<u32>,
+ globals: Vec<u32>,
+ tags: Vec<u32>,
+}
+
+impl ImportPositions {
+ /// Get the position of the import that defines the given entity.
+ ///
+ /// Returns `None` when the module defines the entity itself, rather than
+ /// importing it.
+ pub fn get(&self, entity: EntityIndex) -> Option<usize> {
+ let (positions, i) = match entity {
+ EntityIndex::Function(i) => (&self.functions, i.index()),
+ EntityIndex::Table(i) => (&self.tables, i.index()),
+ EntityIndex::Memory(i) => (&self.memories, i.index()),
+ EntityIndex::Global(i) => (&self.globals, i.index()),
+ EntityIndex::Tag(i) => (&self.tags, i.index()),
+ };
+ let position = *positions.get(i)?;
+ Some(usize::try_from(position).unwrap())
+ }
+}
+
impl Module {
/// Allocates the module data structures.
pub fn new(module_index: StaticModuleIndex) -> Self {
@@ -530,22 +559,24 @@ impl Module {
}
}
- /// Get the position of the import that defines the given entity, suitable
- /// for indexing an instantiation's argument list.
- ///
- /// Returns `None` when this module defines the entity itself, rather than
- /// importing it.
- ///
- /// Note that this has to scan the initializers: imports of different kinds
- /// are interleaved in declaration order, so an import's position is not
- /// recoverable from the `num_imported_*` counts alone.
- pub fn import_position(&self, entity: EntityIndex) -> Option<usize> {
- if !self.is_imported(entity) {
- return None;
+ /// Compute the position of every import, suitable for indexing an
+ /// instantiation's argument list.
+ pub fn import_positions(&self) -> ImportPositions {
+ let mut positions = ImportPositions::default();
+ for (position, initializer) in self.initializers.iter().enumerate() {
+ let Initializer::Import { index, .. } = initializer;
+ let position = u32::try_from(position).unwrap();
+ let (vec, i) = match *index {
+ EntityIndex::Function(i) => (&mut positions.functions, i.index()),
+ EntityIndex::Table(i) => (&mut positions.tables, i.index()),
+ EntityIndex::Memory(i) => (&mut positions.memories, i.index()),
+ EntityIndex::Global(i) => (&mut positions.globals, i.index()),
+ EntityIndex::Tag(i) => (&mut positions.tags, i.index()),
+ };
+ debug_assert_eq!(vec.len(), i);
+ vec.push(position);
}
- self.initializers.iter().position(|i| match i {
- Initializer::Import { index, .. } => *index == entity,
- })
+ positions
}
/// Returns the type of an item based on its index
diff --git a/tests/all/component_model/nested.rs b/tests/all/component_model/nested.rs
index 9e4b84f9f1..dea7f6a5c0 100644
--- a/tests/all/component_model/nested.rs
+++ b/tests/all/component_model/nested.rs
@@ -124,3 +124,42 @@ fn thread_options_through_inner() -> Result<()> {
assert_eq!(result.to_str(&store)?, "42");
Ok(())
}
+
+#[test]
+fn issue_14506_long_reexport_chain_compiles_quickly() -> Result<()> {
+ const FUNCS: usize = 500;
+ const CHAIN: usize = 500;
+
+ let mut wat = String::from(
+ " (component\n\
+ (core module $D\n",
+ );
+ for i in 0..FUNCS {
+ wat.push_str(&format!(" (func (export \"f{i}\"))\n"));
+ }
+ wat.push_str(
+ " )\n\
+ (core module $M\n",
+ );
+ for i in 0..FUNCS {
+ wat.push_str(&format!(" (import \"\" \"f{i}\" (func))\n"));
+ }
+ for i in 0..FUNCS {
+ wat.push_str(&format!(" (export \"f{i}\" (func {i}))\n"));
+ }
+ wat.push_str(
+ " )\n\
+ (core instance $i0 (instantiate $D))\n",
+ );
+ for i in 1..=CHAIN {
+ let prev = i - 1;
+ wat.push_str(&format!(
+ " (core instance $i{i} (instantiate $M (with \"\" (instance $i{prev}))))\n"
+ ));
+ }
+ wat.push_str(" )\n");
+
+ let engine = super::engine();
+ Component::new(&engine, wat)?;
+ Ok(())
+}