Skip to main content

wasmtime/runtime/store/
data.rs

1use crate::module::ModuleRegistry;
2use crate::prelude::*;
3use crate::runtime::vm::{self, VMStore};
4use crate::store::StoreOpaque;
5use crate::{Engine, StoreContext, StoreContextMut};
6use core::num::NonZeroU64;
7use core::ops::{Index, IndexMut};
8use core::pin::Pin;
9
10// This is defined here, in a private submodule, so we can explicitly reexport
11// it only as `pub(crate)`. This avoids a ton of
12// crate-private-type-in-public-interface errors that aren't really too
13// interesting to deal with.
14#[derive(Copy, Clone, Debug, PartialEq, Eq)]
15pub struct InstanceId(u32);
16wasmtime_environ::entity_impl!(InstanceId);
17
18pub struct StoreData {
19    id: StoreId,
20    #[cfg(feature = "component-model")]
21    pub(crate) components: crate::component::ComponentStoreData,
22}
23
24impl StoreData {
25    pub fn new(engine: &Engine) -> Result<StoreData> {
26        #[cfg(not(feature = "component-model"))]
27        let _ = engine;
28        Ok(StoreData {
29            id: StoreId::allocate(),
30            #[cfg(feature = "component-model")]
31            components: crate::component::ComponentStoreData::new(engine)?,
32        })
33    }
34
35    pub fn id(&self) -> StoreId {
36        self.id
37    }
38
39    pub fn run_manual_drop_routines<T>(store: StoreContextMut<T>) {
40        #[cfg(feature = "component-model")]
41        crate::component::ComponentStoreData::run_manual_drop_routines(store);
42        #[cfg(not(feature = "component-model"))]
43        let _ = store;
44    }
45
46    pub fn decrement_allocator_resources(&mut self, allocator: &dyn vm::InstanceAllocator) {
47        #[cfg(feature = "component-model")]
48        self.components.decrement_allocator_resources(allocator);
49        #[cfg(not(feature = "component-model"))]
50        let _ = allocator;
51    }
52
53    /// Publishes any JIT-visible buffers this data owns into
54    /// `vm_store_context`.
55    ///
56    /// # Safety
57    ///
58    /// `self` must outlive `vm_store_context` and must not move while it is in
59    /// use.
60    pub unsafe fn publish_to_vm_store_context(
61        &mut self,
62        vm_store_context: &mut vm::VMStoreContext,
63    ) {
64        // SAFETY: forwarded from this function's own contract.
65        #[cfg(feature = "component-model")]
66        unsafe {
67            self.components.publish_lazy_lenders(vm_store_context);
68        }
69        #[cfg(not(feature = "component-model"))]
70        let _ = vm_store_context;
71    }
72}
73
74// forward StoreOpaque => StoreData
75impl<I> Index<I> for StoreOpaque
76where
77    StoreData: Index<I>,
78{
79    type Output = <StoreData as Index<I>>::Output;
80
81    #[inline]
82    fn index(&self, index: I) -> &Self::Output {
83        self.store_data.index(index)
84    }
85}
86
87impl<I> IndexMut<I> for StoreOpaque
88where
89    StoreData: IndexMut<I>,
90{
91    #[inline]
92    fn index_mut(&mut self, index: I) -> &mut Self::Output {
93        self.store_data.index_mut(index)
94    }
95}
96
97// forward StoreContext => StoreOpaque
98impl<I, T> Index<I> for StoreContext<'_, T>
99where
100    StoreOpaque: Index<I>,
101{
102    type Output = <StoreOpaque as Index<I>>::Output;
103
104    #[inline]
105    fn index(&self, index: I) -> &Self::Output {
106        self.0.index(index)
107    }
108}
109
110// forward StoreContextMut => StoreOpaque
111impl<I, T> Index<I> for StoreContextMut<'_, T>
112where
113    StoreOpaque: Index<I>,
114{
115    type Output = <StoreOpaque as Index<I>>::Output;
116
117    #[inline]
118    fn index(&self, index: I) -> &Self::Output {
119        self.0.index(index)
120    }
121}
122
123impl<I, T> IndexMut<I> for StoreContextMut<'_, T>
124where
125    StoreOpaque: IndexMut<I>,
126{
127    #[inline]
128    fn index_mut(&mut self, index: I) -> &mut Self::Output {
129        self.0.index_mut(index)
130    }
131}
132
133// forward dyn VMStore => StoreOpaque
134impl<I> Index<I> for dyn VMStore + '_
135where
136    StoreOpaque: Index<I>,
137{
138    type Output = <StoreOpaque as Index<I>>::Output;
139
140    fn index(&self, index: I) -> &Self::Output {
141        (**self).index(index)
142    }
143}
144
145impl<I> IndexMut<I> for dyn VMStore + '_
146where
147    StoreOpaque: IndexMut<I>,
148{
149    fn index_mut(&mut self, index: I) -> &mut Self::Output {
150        (**self).index_mut(index)
151    }
152}
153
154/// A unique identifier to get attached to a store.
155///
156/// This identifier is embedded into the `Stored<T>` structure and is used to
157/// identify the original store that items come from. For example a `Memory` is
158/// owned by a `Store` and will embed a `StoreId` internally to say which store
159/// it came from. Comparisons with this value are how panics are generated for
160/// mismatching the item that a store belongs to.
161#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
162#[repr(transparent)] // NB: relied on in the C API
163pub struct StoreId(NonZeroU64);
164
165impl StoreId {
166    /// Allocates a new unique identifier for a store that has never before been
167    /// used in this process.
168    pub fn allocate() -> StoreId {
169        // When 64-bit atomics are allowed then allow 2^63 stores at which point
170        // we start panicking to prevent overflow.
171        //
172        // If a store is created once per microsecond then this will last the
173        // current process for 584,540 years before overflowing.
174        const OVERFLOW_THRESHOLD: u64 = 1 << 63;
175
176        #[cfg(target_has_atomic = "64")]
177        let id = {
178            use core::sync::atomic::{AtomicU64, Ordering::Relaxed};
179
180            // Note the usage of `Relaxed` ordering here which should be ok
181            // since we're only looking for atomicity on this counter and this
182            // otherwise isn't used to synchronize memory stored anywhere else.
183            static NEXT_ID: AtomicU64 = AtomicU64::new(0);
184            let id = NEXT_ID.fetch_add(1, Relaxed);
185            if id > OVERFLOW_THRESHOLD {
186                NEXT_ID.store(OVERFLOW_THRESHOLD, Relaxed);
187                panic!("store id allocator overflow");
188            }
189            id
190        };
191
192        // When 64-bit atomics are not allowed use a `RwLock<u64>`. This is
193        // already used elsewhere in Wasmtime and currently has the
194        // implementation of panic-on-contention, but it's at least no worse
195        // than what wasmtime had before and is at least correct and UB-free.
196        #[cfg(not(target_has_atomic = "64"))]
197        let id = {
198            use crate::sync::RwLock;
199            static NEXT_ID: RwLock<u64> = RwLock::new(0);
200
201            let mut lock = NEXT_ID.write();
202            if *lock > OVERFLOW_THRESHOLD {
203                panic!("store id allocator overflow");
204            }
205            let ret = *lock;
206            *lock += 1;
207            ret
208        };
209
210        StoreId(NonZeroU64::new(id + 1).unwrap())
211    }
212
213    #[inline]
214    pub fn assert_belongs_to(&self, store: StoreId) {
215        if *self == store {
216            return;
217        }
218        store_id_mismatch();
219    }
220
221    /// Raw accessor for the C API.
222    pub fn as_raw(&self) -> NonZeroU64 {
223        self.0
224    }
225
226    /// Raw constructor for the C API.
227    pub fn from_raw(id: NonZeroU64) -> StoreId {
228        StoreId(id)
229    }
230}
231
232#[cold]
233fn store_id_mismatch() {
234    panic!("object used with the wrong store");
235}
236
237/// A type used to represent an allocated `vm::Instance` located within a store.
238///
239/// This type is held in various locations as a "safe index" into a store. This
240/// encapsulates a `StoreId` which owns the instance as well as the index within
241/// the store's list of which instance it's pointing to.
242///
243/// This type can notably be used to index into a `StoreOpaque` to project out
244/// the `vm::Instance` that is associated with this id.
245#[repr(C)] // used by reference in the C API
246#[derive(Copy, Clone, Debug, PartialEq, Eq)]
247pub struct StoreInstanceId {
248    store_id: StoreId,
249    instance: InstanceId,
250}
251
252impl StoreInstanceId {
253    pub(crate) fn new(store_id: StoreId, instance: InstanceId) -> StoreInstanceId {
254        StoreInstanceId { store_id, instance }
255    }
256
257    #[inline]
258    pub fn assert_belongs_to(&self, store: StoreId) {
259        self.store_id.assert_belongs_to(store)
260    }
261
262    #[inline]
263    pub fn store_id(&self) -> StoreId {
264        self.store_id
265    }
266
267    #[inline]
268    pub(crate) fn instance(&self) -> InstanceId {
269        self.instance
270    }
271
272    /// Looks up the `vm::Instance` within `store` that this id points to.
273    ///
274    /// # Panics
275    ///
276    /// Panics if `self` does not belong to `store`.
277    #[inline]
278    pub(crate) fn get<'a>(&self, store: &'a StoreOpaque) -> &'a vm::Instance {
279        self.assert_belongs_to(store.id());
280        store.instance(self.instance)
281    }
282
283    /// Mutable version of `get` above.
284    ///
285    /// # Panics
286    ///
287    /// Panics if `self` does not belong to `store`.
288    #[inline]
289    pub(crate) fn get_mut<'a>(&self, store: &'a mut StoreOpaque) -> Pin<&'a mut vm::Instance> {
290        self.assert_belongs_to(store.id());
291        store.instance_mut(self.instance)
292    }
293
294    /// Get both an instance handle and a borrow to the module
295    /// registry in the store.
296    ///
297    /// # Panics
298    ///
299    /// Panics if `self` does not belong to `store`.
300    #[inline]
301    pub(crate) fn get_mut_and_module_registry<'a>(
302        &self,
303        store: &'a mut StoreOpaque,
304    ) -> (Pin<&'a mut vm::Instance>, &'a ModuleRegistry) {
305        self.assert_belongs_to(store.id());
306        store.instance_and_module_registry_mut(self.instance)
307    }
308}
309
310impl Index<StoreInstanceId> for StoreOpaque {
311    type Output = vm::Instance;
312
313    #[inline]
314    fn index(&self, id: StoreInstanceId) -> &Self::Output {
315        id.get(self)
316    }
317}