Skip to main content

wasmtime/runtime/component/
store.rs

1use crate::prelude::*;
2use crate::runtime::component::concurrent::ConcurrentState;
3use crate::runtime::component::{HostResourceData, Instance};
4use crate::runtime::vm;
5use crate::runtime::vm::component::{
6    CallContext, ComponentInstance, HandleTable, OwnedComponentInstance,
7};
8use crate::store::{StoreData, StoreId, StoreOpaque};
9use crate::{AsContext, AsContextMut, Engine, Store, StoreContextMut};
10use core::pin::Pin;
11use wasmtime_environ::component::RuntimeComponentInstanceIndex;
12use wasmtime_environ::prelude::TryPrimaryMap;
13
14#[cfg(feature = "component-model-async")]
15use crate::{
16    component::ResourceTable,
17    runtime::vm::{VMStore, component::InstanceState},
18};
19
20/// Default amount of fuel allowed for all guest-to-host calls in the component
21/// model.
22///
23/// This is the maximal amount of data which will be copied from the guest to
24/// the host by default. This is set large enough as to not be hit all that
25/// often in theory but also small enough such that if left unconfigured on a
26/// host doesn't mean that it's automatically susceptible to DoS for example.
27const DEFAULT_HOSTCALL_FUEL: usize = 128 << 20;
28
29/// Extensions to `Store` which are only relevant for component-related
30/// information.
31pub struct ComponentStoreData {
32    /// All component instances, in a similar manner to how core wasm instances
33    /// are managed.
34    instances: TryPrimaryMap<ComponentInstanceId, Option<OwnedComponentInstance>>,
35
36    /// Whether an instance belonging to this store has trapped.
37    trapped: bool,
38
39    /// Total number of component instances in this store, used to track
40    /// resources in the instance allocator.
41    num_component_instances: usize,
42
43    /// Runtime state for components used in the handling of resources, borrow,
44    /// and calls. These also interact with the `ResourceAny` type and its
45    /// internal representation.
46    component_host_table: HandleTable,
47    host_resource_data: HostResourceData,
48
49    /// Metadata/tasks/etc related to component-model-async and concurrency
50    /// support.
51    task_state: ComponentTaskState,
52
53    /// Fuel to be used for each time the guest calls the host or transfers data
54    /// to the host.
55    ///
56    /// Caps the size of the allocations made on the host to this amount
57    /// effectively.
58    hostcall_fuel: usize,
59}
60
61/// State tracking for tasks within components.
62pub enum ComponentTaskState {
63    /// Used when `Config::concurrency_support` is disabled. Here there are no
64    /// async tasks but there's still state for borrows that needs managing.
65    NotConcurrent(ComponentTasksNotConcurrent),
66
67    /// Used when `Config::concurrency_support` is enabled and has
68    /// full state for all async tasks.
69    Concurrent(ConcurrentState),
70}
71
72#[derive(Copy, Clone, Debug, PartialEq, Eq)]
73pub struct ComponentInstanceId(u32);
74wasmtime_environ::entity_impl!(ComponentInstanceId);
75
76#[derive(Debug, Copy, Clone, PartialEq, Eq)]
77pub struct RuntimeInstance {
78    pub instance: ComponentInstanceId,
79    pub index: RuntimeComponentInstanceIndex,
80}
81
82impl ComponentStoreData {
83    pub fn new(engine: &Engine) -> ComponentStoreData {
84        ComponentStoreData {
85            instances: Default::default(),
86            trapped: false,
87            num_component_instances: 0,
88            component_host_table: Default::default(),
89            host_resource_data: Default::default(),
90            task_state: if engine.tunables().concurrency_support {
91                #[cfg(feature = "component-model-async")]
92                {
93                    ComponentTaskState::Concurrent(Default::default())
94                }
95                #[cfg(not(feature = "component-model-async"))]
96                {
97                    // This should be validated in `Config` where if
98                    // `concurrency_support` is enabled but compile time support
99                    // isn't available then an `Engine` isn't creatable.
100                    unreachable!()
101                }
102            } else {
103                ComponentTaskState::NotConcurrent(Default::default())
104            },
105            hostcall_fuel: DEFAULT_HOSTCALL_FUEL,
106        }
107    }
108
109    /// Hook used just before a `Store` is dropped to dispose of anything
110    /// necessary.
111    ///
112    /// Used at this time to deallocate fibers related to concurrency support.
113    pub fn run_manual_drop_routines<T>(store: StoreContextMut<T>) {
114        // We need to drop the fibers of each component instance before
115        // attempting to drop the instances themselves since the fibers may need
116        // to be resumed and allowed to exit cleanly before we yank the state
117        // out from under them.
118        //
119        // This will also drop any futures which might use a `&Accessor` fields
120        // in their `Drop::drop` implementations, in which case they'll need to
121        // be called from with in the context of a `tls::set` closure.
122        #[cfg(feature = "component-model-async")]
123        if store.0.component_data().task_state.is_concurrent() {
124            ComponentStoreData::drop_fibers_and_futures(store.0);
125        }
126        #[cfg(not(feature = "component-model-async"))]
127        let _ = store;
128    }
129
130    pub fn next_component_instance_id(&self) -> ComponentInstanceId {
131        self.instances.next_key()
132    }
133
134    #[cfg(feature = "component-model-async")]
135    pub(crate) fn drop_fibers_and_futures(store: &mut dyn VMStore) {
136        let mut fibers = Vec::new();
137        let mut futures = Vec::new();
138        store
139            .concurrent_state_mut()
140            .take_fibers_and_futures(&mut fibers, &mut futures);
141
142        for mut fiber in fibers {
143            fiber.dispose(store);
144        }
145
146        crate::component::concurrent::tls::set(store, move || drop(futures));
147    }
148
149    #[cfg(feature = "component-model-async")]
150    pub(crate) fn assert_instance_states_empty(&mut self) {
151        for (_, instance) in self.instances.iter_mut() {
152            let Some(instance) = instance.as_mut() else {
153                continue;
154            };
155
156            assert!(instance.get_mut().instance_states().0.iter_mut().all(
157                |(_, state): (_, &mut InstanceState)| state.handle_table().is_empty()
158                    && state.concurrent_state().pending_is_empty()
159            ));
160        }
161    }
162
163    pub fn decrement_allocator_resources(&mut self, allocator: &dyn vm::InstanceAllocator) {
164        for _ in 0..self.num_component_instances {
165            allocator.decrement_component_instance_count();
166        }
167    }
168}
169
170/// A type used to represent an allocated `ComponentInstance` located within a
171/// store.
172///
173/// This type is held in various locations as a "safe index" into a store. This
174/// encapsulates a `StoreId` which owns the instance as well as the index within
175/// the store's list of which instance it's pointing to.
176///
177/// This type can notably be used to index into a `StoreOpaque` to project out
178/// the `ComponentInstance` that is associated with this id.
179#[repr(C)] // used by reference in the C API
180#[derive(Copy, Clone, Debug, PartialEq, Eq)]
181pub struct StoreComponentInstanceId {
182    store_id: StoreId,
183    instance: ComponentInstanceId,
184}
185
186impl StoreComponentInstanceId {
187    pub(crate) fn new(
188        store_id: StoreId,
189        instance: ComponentInstanceId,
190    ) -> StoreComponentInstanceId {
191        StoreComponentInstanceId { store_id, instance }
192    }
193
194    #[inline]
195    pub fn assert_belongs_to(&self, store: StoreId) {
196        self.store_id.assert_belongs_to(store)
197    }
198
199    #[inline]
200    pub(crate) fn store_id(&self) -> StoreId {
201        self.store_id
202    }
203
204    #[inline]
205    pub(crate) fn instance(&self) -> ComponentInstanceId {
206        self.instance
207    }
208
209    /// Looks up the `vm::ComponentInstance` within `store` that this id points
210    /// to.
211    ///
212    /// # Panics
213    ///
214    /// Panics if `self` does not belong to `store`.
215    pub(crate) fn get<'a>(&self, store: &'a StoreOpaque) -> &'a ComponentInstance {
216        self.assert_belongs_to(store.id());
217        store.component_instance(self.instance)
218    }
219
220    /// Mutable version of `get` above.
221    ///
222    /// # Panics
223    ///
224    /// Panics if `self` does not belong to `store`.
225    pub(crate) fn get_mut<'a>(&self, store: &'a mut StoreOpaque) -> Pin<&'a mut ComponentInstance> {
226        self.from_data_get_mut(store.store_data_mut())
227    }
228
229    /// Return a mutable `ComponentInstance` and a `ModuleRegistry`
230    /// from the store.
231    ///
232    /// # Panics
233    ///
234    /// Panics if `self` does not belong to `store`.
235    #[cfg(feature = "component-model-async")]
236    pub(crate) fn get_mut_and_registry<'a>(
237        &self,
238        store: &'a mut StoreOpaque,
239    ) -> (
240        Pin<&'a mut ComponentInstance>,
241        &'a crate::module::ModuleRegistry,
242    ) {
243        let (store_data, registry) = store.store_data_mut_and_registry();
244        let instance = self.from_data_get_mut(store_data);
245        (instance, registry)
246    }
247
248    /// Same as `get_mut`, but borrows less of a store.
249    fn from_data_get_mut<'a>(&self, store: &'a mut StoreData) -> Pin<&'a mut ComponentInstance> {
250        self.assert_belongs_to(store.id());
251        store.component_instance_mut(self.instance)
252    }
253}
254
255impl StoreData {
256    pub(crate) fn push_component_instance(
257        &mut self,
258        data: OwnedComponentInstance,
259    ) -> Result<ComponentInstanceId, OutOfMemory> {
260        let expected = data.get().id();
261        let ret = self.components.instances.push(Some(data))?;
262        assert_eq!(expected, ret);
263        Ok(ret)
264    }
265
266    pub(crate) fn component_instance(&self, id: ComponentInstanceId) -> &ComponentInstance {
267        self.components.instances[id].as_ref().unwrap().get()
268    }
269
270    pub(crate) fn component_instance_mut(
271        &mut self,
272        id: ComponentInstanceId,
273    ) -> Pin<&mut ComponentInstance> {
274        self.components.instances[id].as_mut().unwrap().get_mut()
275    }
276}
277
278impl StoreOpaque {
279    pub(crate) fn trapped(&self) -> bool {
280        self.store_data().components.trapped
281    }
282
283    pub(crate) fn set_trapped(&mut self) {
284        self.store_data_mut().components.trapped = true;
285    }
286
287    pub(crate) fn component_data(&self) -> &ComponentStoreData {
288        &self.store_data().components
289    }
290
291    pub(crate) fn component_data_mut(&mut self) -> &mut ComponentStoreData {
292        &mut self.store_data_mut().components
293    }
294
295    pub(crate) fn component_task_state_mut(&mut self) -> &mut ComponentTaskState {
296        &mut self.component_data_mut().task_state
297    }
298
299    pub(crate) fn push_component_instance(&mut self, instance: Instance) {
300        // We don't actually need the instance itself right now, but it seems
301        // like something we will almost certainly eventually want to keep
302        // around, so force callers to provide it.
303        let _ = instance;
304
305        self.component_data_mut().num_component_instances += 1;
306    }
307
308    pub(crate) fn component_instance(&self, id: ComponentInstanceId) -> &ComponentInstance {
309        self.store_data().component_instance(id)
310    }
311
312    #[cfg(feature = "component-model-async")]
313    pub(crate) fn component_instance_mut(
314        &mut self,
315        id: ComponentInstanceId,
316    ) -> Pin<&mut ComponentInstance> {
317        self.store_data_mut().component_instance_mut(id)
318    }
319
320    #[cfg(feature = "component-model-async")]
321    pub(crate) fn concurrent_state_mut(&mut self) -> &mut ConcurrentState {
322        debug_assert!(self.concurrency_support());
323        self.component_data_mut().task_state.concurrent_state_mut()
324    }
325
326    #[inline]
327    #[cfg(feature = "component-model-async")]
328    pub(crate) fn concurrency_support(&self) -> bool {
329        let support = self.component_data().task_state.is_concurrent();
330        debug_assert_eq!(support, self.engine().tunables().concurrency_support);
331        support
332    }
333
334    pub(crate) fn lift_context_parts(
335        &mut self,
336        instance: Instance,
337    ) -> (
338        &mut ComponentTaskState,
339        &mut HandleTable,
340        &mut HostResourceData,
341        Pin<&mut ComponentInstance>,
342    ) {
343        let instance = instance.id();
344        instance.assert_belongs_to(self.id());
345        let data = self.component_data_mut();
346        (
347            &mut data.task_state,
348            &mut data.component_host_table,
349            &mut data.host_resource_data,
350            data.instances[instance.instance]
351                .as_mut()
352                .unwrap()
353                .get_mut(),
354        )
355    }
356
357    pub(crate) fn component_resource_tables(
358        &mut self,
359        instance: Option<Instance>,
360    ) -> vm::component::ResourceTables<'_> {
361        self.component_resource_tables_and_host_resource_data(instance)
362            .0
363    }
364
365    pub(crate) fn component_resource_tables_and_host_resource_data(
366        &mut self,
367        instance: Option<Instance>,
368    ) -> (
369        vm::component::ResourceTables<'_>,
370        &mut crate::component::HostResourceData,
371    ) {
372        let store_id = self.id();
373        let data = self.component_data_mut();
374        let guest = instance.map(|i| {
375            let i = i.id();
376            i.assert_belongs_to(store_id);
377            data.instances[i.instance]
378                .as_mut()
379                .unwrap()
380                .get_mut()
381                .instance_states()
382        });
383
384        (
385            vm::component::ResourceTables {
386                host_table: &mut data.component_host_table,
387                task_state: &mut data.task_state,
388                guest,
389            },
390            &mut data.host_resource_data,
391        )
392    }
393
394    pub(crate) fn enter_call_not_concurrent(&mut self) -> Result<()> {
395        let state = match &mut self.component_data_mut().task_state {
396            ComponentTaskState::NotConcurrent(state) => state,
397            ComponentTaskState::Concurrent(_) => unreachable!(),
398        };
399        state.scopes.push(CallContext::default())?;
400        Ok(())
401    }
402
403    pub(crate) fn exit_call_not_concurrent(&mut self) {
404        let state = match &mut self.component_data_mut().task_state {
405            ComponentTaskState::NotConcurrent(state) => state,
406            ComponentTaskState::Concurrent(_) => unreachable!(),
407        };
408        state.scopes.pop();
409    }
410
411    pub(crate) fn hostcall_fuel(&self) -> usize {
412        self.component_data().hostcall_fuel
413    }
414
415    pub(crate) fn set_hostcall_fuel(&mut self, fuel: usize) {
416        self.component_data_mut().hostcall_fuel = fuel;
417    }
418
419    #[cfg(feature = "component-model-async")]
420    fn concurrent_resource_table(&mut self) -> Option<&mut ResourceTable> {
421        if self.concurrency_support() {
422            Some(self.concurrent_state_mut().table())
423        } else {
424            None
425        }
426    }
427}
428
429impl<T> Store<T> {
430    /// Returns the amount of "hostcall fuel" used for guest-to-host component
431    /// calls.
432    ///
433    /// This is either the default amount if it hasn't been configured or
434    /// returns the last value passed to [`Store::set_hostcall_fuel`].
435    ///
436    /// See [`Store::set_hostcall_fuel`] `for more details.
437    pub fn hostcall_fuel(&self) -> usize {
438        self.as_context().0.hostcall_fuel()
439    }
440
441    /// Sets the amount of "hostcall fuel" used for guest-to-host component
442    /// calls.
443    ///
444    /// Whenever the guest calls the host it often wants to transfer some data
445    /// as well, such as strings or lists. This configured fuel value can be
446    /// used to limit the amount of data that the host allocates on behalf of
447    /// the guest. This is a DoS mitigation mechanism to prevent a malicious
448    /// guest from causing the host to allocate an unbounded amount of memory
449    /// for example.
450    ///
451    /// Fuel is considered distinct for each host call. The host is responsible
452    /// for ensuring it retains a proper amount of data between host calls if
453    /// applicable. The `fuel` provided here will be the initial value for each
454    /// time the guest calls the host.
455    ///
456    /// The `fuel` value here should roughly corresponds to the maximal number
457    /// of bytes that the guest may transfer to the host in one call.
458    ///
459    /// Note that data transferred from the host to the guest is not limited
460    /// because it's already resident on the host itself. Only data from the
461    /// guest to the host is limited.
462    ///
463    /// The default value for this is 128 MiB.
464    pub fn set_hostcall_fuel(&mut self, fuel: usize) {
465        self.as_context_mut().set_hostcall_fuel(fuel)
466    }
467
468    /// Returns the underlying [`ResourceTable`] that the implementation of
469    /// concurrency in the component model is using.
470    ///
471    /// Returns `None` if [`Config::concurrency_support`] is disabled.
472    ///
473    /// [`Config::concurrency_support`]: crate::Config::concurrency_support
474    #[cfg(feature = "component-model-async")]
475    pub fn concurrent_resource_table(&mut self) -> Option<&mut ResourceTable> {
476        self.as_context_mut().0.concurrent_resource_table()
477    }
478}
479
480impl<T> StoreContextMut<'_, T> {
481    /// See [`Store::hostcall_fuel`].
482    pub fn hostcall_fuel(&self) -> usize {
483        self.0.hostcall_fuel()
484    }
485
486    /// See [`Store::set_hostcall_fuel`].
487    pub fn set_hostcall_fuel(&mut self, fuel: usize) {
488        self.0.set_hostcall_fuel(fuel)
489    }
490
491    /// See [`Store::concurrent_resource_table`].
492    #[cfg(feature = "component-model-async")]
493    pub fn concurrent_resource_table(&mut self) -> Option<&mut ResourceTable> {
494        self.0.concurrent_resource_table()
495    }
496}
497
498#[derive(Default)]
499pub struct ComponentTasksNotConcurrent {
500    scopes: TryVec<CallContext>,
501}
502
503impl ComponentTaskState {
504    pub fn call_context(&mut self, id: u32) -> Result<&mut CallContext> {
505        match self {
506            ComponentTaskState::NotConcurrent(state) => Ok(&mut state.scopes[id as usize]),
507            ComponentTaskState::Concurrent(state) => state.call_context(id),
508        }
509    }
510
511    pub fn current_call_context_scope_id(&self) -> Result<u32> {
512        match self {
513            ComponentTaskState::NotConcurrent(state) => Ok(u32::try_from(state.scopes.len() - 1)?),
514            ComponentTaskState::Concurrent(state) => state.current_call_context_scope_id(),
515        }
516    }
517
518    pub fn concurrent_state_mut(&mut self) -> &mut ConcurrentState {
519        match self {
520            ComponentTaskState::Concurrent(state) => state,
521            ComponentTaskState::NotConcurrent(_) => {
522                panic!("expected concurrent state to be present")
523            }
524        }
525    }
526
527    #[cfg(feature = "component-model-async")]
528    fn is_concurrent(&self) -> bool {
529        match self {
530            ComponentTaskState::Concurrent(_) => true,
531            ComponentTaskState::NotConcurrent(_) => false,
532        }
533    }
534}