wasmtime/runtime/
memory.rs

1use crate::prelude::*;
2use crate::runtime::vm::VMMemoryImport;
3use crate::store::{StoreData, StoreOpaque, Stored};
4use crate::trampoline::generate_memory_export;
5use crate::Trap;
6use crate::{AsContext, AsContextMut, Engine, MemoryType, StoreContext, StoreContextMut};
7use core::cell::UnsafeCell;
8use core::fmt;
9use core::slice;
10use core::time::Duration;
11
12pub use crate::runtime::vm::WaitResult;
13
14/// Error for out of bounds [`Memory`] access.
15#[derive(Debug)]
16#[non_exhaustive]
17pub struct MemoryAccessError {
18    // Keep struct internals private for future extensibility.
19    _private: (),
20}
21
22impl fmt::Display for MemoryAccessError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "out of bounds memory access")
25    }
26}
27
28#[cfg(feature = "std")]
29impl std::error::Error for MemoryAccessError {}
30
31/// A WebAssembly linear memory.
32///
33/// WebAssembly memories represent a contiguous array of bytes that have a size
34/// that is always a multiple of the WebAssembly page size, currently 64
35/// kilobytes.
36///
37/// WebAssembly memory is used for global data (not to be confused with wasm
38/// `global` items), statics in C/C++/Rust, shadow stack memory, etc. Accessing
39/// wasm memory is generally quite fast.
40///
41/// Memories, like other wasm items, are owned by a [`Store`](crate::Store).
42///
43/// # `Memory` and Safety
44///
45/// Linear memory is a lynchpin of safety for WebAssembly. In Wasmtime there are
46/// safe methods of interacting with a [`Memory`]:
47///
48/// * [`Memory::read`]
49/// * [`Memory::write`]
50/// * [`Memory::data`]
51/// * [`Memory::data_mut`]
52///
53/// Note that all of these consider the entire store context as borrowed for the
54/// duration of the call or the duration of the returned slice. This largely
55/// means that while the function is running you'll be unable to borrow anything
56/// else from the store. This includes getting access to the `T` on
57/// [`Store<T>`](crate::Store), but it also means that you can't recursively
58/// call into WebAssembly for instance.
59///
60/// If you'd like to dip your toes into handling [`Memory`] in a more raw
61/// fashion (e.g. by using raw pointers or raw slices), then there's a few
62/// important points to consider when doing so:
63///
64/// * Any recursive calls into WebAssembly can possibly modify any byte of the
65///   entire memory. This means that whenever wasm is called Rust can't have any
66///   long-lived borrows live across the wasm function call. Slices like `&mut
67///   [u8]` will be violated because they're not actually exclusive at that
68///   point, and slices like `&[u8]` are also violated because their contents
69///   may be mutated.
70///
71/// * WebAssembly memories can grow, and growth may change the base pointer.
72///   This means that even holding a raw pointer to memory over a wasm function
73///   call is also incorrect. Anywhere in the function call the base address of
74///   memory may change. Note that growth can also be requested from the
75///   embedding API as well.
76///
77/// As a general rule of thumb it's recommended to stick to the safe methods of
78/// [`Memory`] if you can. It's not advised to use raw pointers or `unsafe`
79/// operations because of how easy it is to accidentally get things wrong.
80///
81/// Some examples of safely interacting with memory are:
82///
83/// ```rust
84/// use wasmtime::{Memory, Store, MemoryAccessError};
85///
86/// // Memory can be read and written safely with the `Memory::read` and
87/// // `Memory::write` methods.
88/// // An error is returned if the copy did not succeed.
89/// fn safe_examples(mem: Memory, store: &mut Store<()>) -> Result<(), MemoryAccessError> {
90///     let offset = 5;
91///     mem.write(&mut *store, offset, b"hello")?;
92///     let mut buffer = [0u8; 5];
93///     mem.read(&store, offset, &mut buffer)?;
94///     assert_eq!(b"hello", &buffer);
95///
96///     // Note that while this is safe care must be taken because the indexing
97///     // here may panic if the memory isn't large enough.
98///     assert_eq!(&mem.data(&store)[offset..offset + 5], b"hello");
99///     mem.data_mut(&mut *store)[offset..offset + 5].copy_from_slice(b"bye!!");
100///
101///     Ok(())
102/// }
103/// ```
104///
105/// It's worth also, however, covering some examples of **incorrect**,
106/// **unsafe** usages of `Memory`. Do not do these things!
107///
108/// ```rust
109/// # use anyhow::Result;
110/// use wasmtime::{Memory, Store};
111///
112/// // NOTE: All code in this function is not safe to execute and may cause
113/// // segfaults/undefined behavior at runtime. Do not copy/paste these examples
114/// // into production code!
115/// unsafe fn unsafe_examples(mem: Memory, store: &mut Store<()>) -> Result<()> {
116///     // First and foremost, any borrow can be invalidated at any time via the
117///     // `Memory::grow` function. This can relocate memory which causes any
118///     // previous pointer to be possibly invalid now.
119///     unsafe {
120///         let pointer: &u8 = &*mem.data_ptr(&store);
121///         mem.grow(&mut *store, 1)?; // invalidates `pointer`!
122///         // println!("{}", *pointer); // FATAL: use-after-free
123///     }
124///
125///     // Note that the use-after-free also applies to slices, whether they're
126///     // slices of bytes or strings.
127///     unsafe {
128///         let mem_slice = std::slice::from_raw_parts(
129///             mem.data_ptr(&store),
130///             mem.data_size(&store),
131///         );
132///         let slice: &[u8] = &mem_slice[0x100..0x102];
133///         mem.grow(&mut *store, 1)?; // invalidates `slice`!
134///         // println!("{:?}", slice); // FATAL: use-after-free
135///     }
136///
137///     // The `Memory` type may be stored in other locations, so if you hand
138///     // off access to the `Store` then those locations may also call
139///     // `Memory::grow` or similar, so it's not enough to just audit code for
140///     // calls to `Memory::grow`.
141///     unsafe {
142///         let pointer: &u8 = &*mem.data_ptr(&store);
143///         some_other_function(store); // may invalidate `pointer` through use of `store`
144///         // println!("{:?}", pointer); // FATAL: maybe a use-after-free
145///     }
146///
147///     // An especially subtle aspect of accessing a wasm instance's memory is
148///     // that you need to be extremely careful about aliasing. Anyone at any
149///     // time can call `data_unchecked()` or `data_unchecked_mut()`, which
150///     // means you can easily have aliasing mutable references:
151///     unsafe {
152///         let ref1: &u8 = &*mem.data_ptr(&store).add(0x100);
153///         let ref2: &mut u8 = &mut *mem.data_ptr(&store).add(0x100);
154///         // *ref2 = *ref1; // FATAL: violates Rust's aliasing rules
155///     }
156///
157///     Ok(())
158/// }
159/// # fn some_other_function(store: &mut Store<()>) {}
160/// ```
161///
162/// Overall there's some general rules of thumb when unsafely working with
163/// `Memory` and getting raw pointers inside of it:
164///
165/// * If you never have a "long lived" pointer into memory, you're likely in the
166///   clear. Care still needs to be taken in threaded scenarios or when/where
167///   data is read, but you'll be shielded from many classes of issues.
168/// * Long-lived pointers must always respect Rust'a aliasing rules. It's ok for
169///   shared borrows to overlap with each other, but mutable borrows must
170///   overlap with nothing.
171/// * Long-lived pointers are only valid if they're not invalidated for their
172///   lifetime. This means that [`Store`](crate::Store) isn't used to reenter
173///   wasm or the memory itself is never grown or otherwise modified/aliased.
174///
175/// At this point it's worth reiterating again that unsafely working with
176/// `Memory` is pretty tricky and not recommended! It's highly recommended to
177/// use the safe methods to interact with [`Memory`] whenever possible.
178///
179/// ## `Memory` Safety and Threads
180///
181/// Currently the `wasmtime` crate does not implement the wasm threads proposal,
182/// but it is planned to do so. It may be interesting to readers to see how this
183/// affects memory safety and what was previously just discussed as well.
184///
185/// Once threads are added into the mix, all of the above rules still apply.
186/// There's an additional consideration that all reads and writes can happen
187/// concurrently, though. This effectively means that any borrow into wasm
188/// memory are virtually never safe to have.
189///
190/// Mutable pointers are fundamentally unsafe to have in a concurrent scenario
191/// in the face of arbitrary wasm code. Only if you dynamically know for sure
192/// that wasm won't access a region would it be safe to construct a mutable
193/// pointer. Additionally even shared pointers are largely unsafe because their
194/// underlying contents may change, so unless `UnsafeCell` in one form or
195/// another is used everywhere there's no safety.
196///
197/// One important point about concurrency is that while [`Memory::grow`] can
198/// happen concurrently it will never relocate the base pointer. Shared
199/// memories must always have a maximum size and they will be preallocated such
200/// that growth will never relocate the base pointer. The current size of the
201/// memory may still change over time though.
202///
203/// Overall the general rule of thumb for shared memories is that you must
204/// atomically read and write everything. Nothing can be borrowed and everything
205/// must be eagerly copied out. This means that [`Memory::data`] and
206/// [`Memory::data_mut`] won't work in the future (they'll probably return an
207/// error) for shared memories when they're implemented. When possible it's
208/// recommended to use [`Memory::read`] and [`Memory::write`] which will still
209/// be provided.
210#[derive(Copy, Clone, Debug)]
211#[repr(transparent)] // here for the C API
212pub struct Memory(Stored<crate::runtime::vm::ExportMemory>);
213
214impl Memory {
215    /// Creates a new WebAssembly memory given the configuration of `ty`.
216    ///
217    /// The `store` argument will be the owner of the returned [`Memory`]. All
218    /// WebAssembly memory is initialized to zero.
219    ///
220    /// # Panics
221    ///
222    /// This function will panic if the [`Store`](`crate::Store`) has a
223    /// [`ResourceLimiterAsync`](`crate::ResourceLimiterAsync`) (see also:
224    /// [`Store::limiter_async`](`crate::Store::limiter_async`)). When
225    /// using an async resource limiter, use [`Memory::new_async`] instead.
226    ///
227    /// # Examples
228    ///
229    /// ```
230    /// # use wasmtime::*;
231    /// # fn main() -> anyhow::Result<()> {
232    /// let engine = Engine::default();
233    /// let mut store = Store::new(&engine, ());
234    ///
235    /// let memory_ty = MemoryType::new(1, None);
236    /// let memory = Memory::new(&mut store, memory_ty)?;
237    ///
238    /// let module = Module::new(&engine, "(module (memory (import \"\" \"\") 1))")?;
239    /// let instance = Instance::new(&mut store, &module, &[memory.into()])?;
240    /// // ...
241    /// # Ok(())
242    /// # }
243    /// ```
244    pub fn new(mut store: impl AsContextMut, ty: MemoryType) -> Result<Memory> {
245        Self::_new(store.as_context_mut().0, ty)
246    }
247
248    /// Async variant of [`Memory::new`]. You must use this variant with
249    /// [`Store`](`crate::Store`)s which have a
250    /// [`ResourceLimiterAsync`](`crate::ResourceLimiterAsync`).
251    ///
252    /// # Panics
253    ///
254    /// This function will panic when used with a non-async
255    /// [`Store`](`crate::Store`).
256    #[cfg(feature = "async")]
257    pub async fn new_async<T>(
258        mut store: impl AsContextMut<Data = T>,
259        ty: MemoryType,
260    ) -> Result<Memory>
261    where
262        T: Send,
263    {
264        let mut store = store.as_context_mut();
265        assert!(
266            store.0.async_support(),
267            "cannot use `new_async` without enabling async support on the config"
268        );
269        store.on_fiber(|store| Self::_new(store.0, ty)).await?
270    }
271
272    /// Helper function for attaching the memory to a "frankenstein" instance
273    fn _new(store: &mut StoreOpaque, ty: MemoryType) -> Result<Memory> {
274        unsafe {
275            let export = generate_memory_export(store, &ty, None)?;
276            Ok(Memory::from_wasmtime_memory(export, store))
277        }
278    }
279
280    /// Returns the underlying type of this memory.
281    ///
282    /// # Panics
283    ///
284    /// Panics if this memory doesn't belong to `store`.
285    ///
286    /// # Examples
287    ///
288    /// ```
289    /// # use wasmtime::*;
290    /// # fn main() -> anyhow::Result<()> {
291    /// let engine = Engine::default();
292    /// let mut store = Store::new(&engine, ());
293    /// let module = Module::new(&engine, "(module (memory (export \"mem\") 1))")?;
294    /// let instance = Instance::new(&mut store, &module, &[])?;
295    /// let memory = instance.get_memory(&mut store, "mem").unwrap();
296    /// let ty = memory.ty(&store);
297    /// assert_eq!(ty.minimum(), 1);
298    /// # Ok(())
299    /// # }
300    /// ```
301    pub fn ty(&self, store: impl AsContext) -> MemoryType {
302        let store = store.as_context();
303        let ty = &store[self.0].memory;
304        MemoryType::from_wasmtime_memory(&ty)
305    }
306
307    /// Safely reads memory contents at the given offset into a buffer.
308    ///
309    /// The entire buffer will be filled.
310    ///
311    /// If `offset + buffer.len()` exceed the current memory capacity, then the
312    /// buffer is left untouched and a [`MemoryAccessError`] is returned.
313    ///
314    /// # Panics
315    ///
316    /// Panics if this memory doesn't belong to `store`.
317    pub fn read(
318        &self,
319        store: impl AsContext,
320        offset: usize,
321        buffer: &mut [u8],
322    ) -> Result<(), MemoryAccessError> {
323        let store = store.as_context();
324        let slice = self
325            .data(&store)
326            .get(offset..)
327            .and_then(|s| s.get(..buffer.len()))
328            .ok_or(MemoryAccessError { _private: () })?;
329        buffer.copy_from_slice(slice);
330        Ok(())
331    }
332
333    /// Safely writes contents of a buffer to this memory at the given offset.
334    ///
335    /// If the `offset + buffer.len()` exceeds the current memory capacity, then
336    /// none of the buffer is written to memory and a [`MemoryAccessError`] is
337    /// returned.
338    ///
339    /// # Panics
340    ///
341    /// Panics if this memory doesn't belong to `store`.
342    pub fn write(
343        &self,
344        mut store: impl AsContextMut,
345        offset: usize,
346        buffer: &[u8],
347    ) -> Result<(), MemoryAccessError> {
348        let mut context = store.as_context_mut();
349        self.data_mut(&mut context)
350            .get_mut(offset..)
351            .and_then(|s| s.get_mut(..buffer.len()))
352            .ok_or(MemoryAccessError { _private: () })?
353            .copy_from_slice(buffer);
354        Ok(())
355    }
356
357    /// Returns this memory as a native Rust slice.
358    ///
359    /// Note that this method will consider the entire store context provided as
360    /// borrowed for the duration of the lifetime of the returned slice.
361    ///
362    /// # Panics
363    ///
364    /// Panics if this memory doesn't belong to `store`.
365    pub fn data<'a, T: 'a>(&self, store: impl Into<StoreContext<'a, T>>) -> &'a [u8] {
366        unsafe {
367            let store = store.into();
368            let definition = store[self.0].definition.as_ref();
369            debug_assert!(!self.ty(store).is_shared());
370            slice::from_raw_parts(definition.base.as_ptr(), definition.current_length())
371        }
372    }
373
374    /// Returns this memory as a native Rust mutable slice.
375    ///
376    /// Note that this method will consider the entire store context provided as
377    /// borrowed for the duration of the lifetime of the returned slice.
378    ///
379    /// # Panics
380    ///
381    /// Panics if this memory doesn't belong to `store`.
382    pub fn data_mut<'a, T: 'a>(&self, store: impl Into<StoreContextMut<'a, T>>) -> &'a mut [u8] {
383        unsafe {
384            let store = store.into();
385            let definition = store[self.0].definition.as_ref();
386            debug_assert!(!self.ty(store).is_shared());
387            slice::from_raw_parts_mut(definition.base.as_ptr(), definition.current_length())
388        }
389    }
390
391    /// Same as [`Memory::data_mut`], but also returns the `T` from the
392    /// [`StoreContextMut`].
393    ///
394    /// This method can be used when you want to simultaneously work with the
395    /// `T` in the store as well as the memory behind this [`Memory`]. Using
396    /// [`Memory::data_mut`] would consider the entire store borrowed, whereas
397    /// this method allows the Rust compiler to see that the borrow of this
398    /// memory and the borrow of `T` are disjoint.
399    ///
400    /// # Panics
401    ///
402    /// Panics if this memory doesn't belong to `store`.
403    pub fn data_and_store_mut<'a, T: 'a>(
404        &self,
405        store: impl Into<StoreContextMut<'a, T>>,
406    ) -> (&'a mut [u8], &'a mut T) {
407        // Note the unsafety here. Our goal is to simultaneously borrow the
408        // memory and custom data from `store`, and the store it's connected
409        // to. Rust will not let us do that, however, because we must call two
410        // separate methods (both of which borrow the whole `store`) and one of
411        // our borrows is mutable (the custom data).
412        //
413        // This operation, however, is safe because these borrows do not overlap
414        // and in the process of borrowing them mutability doesn't actually
415        // touch anything. This is akin to mutably borrowing two indices in an
416        // array, which is safe so long as the indices are separate.
417        unsafe {
418            let mut store = store.into();
419            let data = &mut *(store.data_mut() as *mut T);
420            (self.data_mut(store), data)
421        }
422    }
423
424    /// Returns the base pointer, in the host's address space, that the memory
425    /// is located at.
426    ///
427    /// For more information and examples see the documentation on the
428    /// [`Memory`] type.
429    ///
430    /// # Panics
431    ///
432    /// Panics if this memory doesn't belong to `store`.
433    pub fn data_ptr(&self, store: impl AsContext) -> *mut u8 {
434        unsafe { store.as_context()[self.0].definition.as_ref().base.as_ptr() }
435    }
436
437    /// Returns the byte length of this memory.
438    ///
439    /// WebAssembly memories are made up of a whole number of pages, so the byte
440    /// size returned will always be a multiple of this memory's page size. Note
441    /// that different Wasm memories may have different page sizes. You can get
442    /// a memory's page size via the [`Memory::page_size`] method.
443    ///
444    /// By default the page size is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or
445    /// `65536`) but [the custom-page-sizes proposal] allows a memory to opt
446    /// into a page size of `1`. Future extensions might allow any power of two
447    /// as a page size.
448    ///
449    /// [the custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
450    ///
451    /// For more information and examples see the documentation on the
452    /// [`Memory`] type.
453    ///
454    /// # Panics
455    ///
456    /// Panics if this memory doesn't belong to `store`.
457    pub fn data_size(&self, store: impl AsContext) -> usize {
458        self.internal_data_size(store.as_context().0)
459    }
460
461    pub(crate) fn internal_data_size(&self, store: &StoreOpaque) -> usize {
462        unsafe { store[self.0].definition.as_ref().current_length() }
463    }
464
465    /// Returns the size, in units of pages, of this Wasm memory.
466    ///
467    /// WebAssembly memories are made up of a whole number of pages, so the byte
468    /// size returned will always be a multiple of this memory's page size. Note
469    /// that different Wasm memories may have different page sizes. You can get
470    /// a memory's page size via the [`Memory::page_size`] method.
471    ///
472    /// By default the page size is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or
473    /// `65536`) but [the custom-page-sizes proposal] allows a memory to opt
474    /// into a page size of `1`. Future extensions might allow any power of two
475    /// as a page size.
476    ///
477    /// [the custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
478    ///
479    /// # Panics
480    ///
481    /// Panics if this memory doesn't belong to `store`.
482    pub fn size(&self, store: impl AsContext) -> u64 {
483        self.internal_size(store.as_context().0)
484    }
485
486    pub(crate) fn internal_size(&self, store: &StoreOpaque) -> u64 {
487        let byte_size = self.internal_data_size(store);
488        let page_size = usize::try_from(self._page_size(store)).unwrap();
489        u64::try_from(byte_size / page_size).unwrap()
490    }
491
492    /// Returns the size of a page, in bytes, for this memory.
493    ///
494    /// WebAssembly memories are made up of a whole number of pages, so the byte
495    /// size (as returned by [`Memory::data_size`]) will always be a multiple of
496    /// their page size. Different Wasm memories may have different page sizes.
497    ///
498    /// By default this is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or `65536`)
499    /// but [the custom-page-sizes proposal] allows opting into a page size of
500    /// `1`. Future extensions might allow any power of two as a page size.
501    ///
502    /// [the custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
503    pub fn page_size(&self, store: impl AsContext) -> u64 {
504        self._page_size(store.as_context().0)
505    }
506
507    pub(crate) fn _page_size(&self, store: &StoreOpaque) -> u64 {
508        store[self.0].memory.page_size()
509    }
510
511    /// Returns the log2 of this memory's page size, in bytes.
512    ///
513    /// WebAssembly memories are made up of a whole number of pages, so the byte
514    /// size (as returned by [`Memory::data_size`]) will always be a multiple of
515    /// their page size. Different Wasm memories may have different page sizes.
516    ///
517    /// By default the page size is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or
518    /// `65536`) but [the custom-page-sizes proposal] allows opting into a page
519    /// size of `1`. Future extensions might allow any power of two as a page
520    /// size.
521    ///
522    /// [the custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
523    pub fn page_size_log2(&self, store: impl AsContext) -> u8 {
524        self._page_size_log2(store.as_context().0)
525    }
526
527    pub(crate) fn _page_size_log2(&self, store: &StoreOpaque) -> u8 {
528        store[self.0].memory.page_size_log2
529    }
530
531    /// Grows this WebAssembly memory by `delta` pages.
532    ///
533    /// This will attempt to add `delta` more pages of memory on to the end of
534    /// this `Memory` instance. If successful this may relocate the memory and
535    /// cause [`Memory::data_ptr`] to return a new value. Additionally any
536    /// unsafely constructed slices into this memory may no longer be valid.
537    ///
538    /// On success returns the number of pages this memory previously had
539    /// before the growth succeeded.
540    ///
541    /// Note that, by default, a WebAssembly memory's page size is 64KiB (aka
542    /// 65536 or 2<sup>16</sup>). The [custom-page-sizes proposal] allows Wasm
543    /// memories to opt into a page size of one byte (and this may be further
544    /// relaxed to any power of two in a future extension).
545    ///
546    /// [custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
547    ///
548    /// # Errors
549    ///
550    /// Returns an error if memory could not be grown, for example if it exceeds
551    /// the maximum limits of this memory. A
552    /// [`ResourceLimiter`](crate::ResourceLimiter) is another example of
553    /// preventing a memory to grow.
554    ///
555    /// # Panics
556    ///
557    /// Panics if this memory doesn't belong to `store`.
558    ///
559    /// This function will panic if the [`Store`](`crate::Store`) has a
560    /// [`ResourceLimiterAsync`](`crate::ResourceLimiterAsync`) (see also:
561    /// [`Store::limiter_async`](`crate::Store::limiter_async`). When using an
562    /// async resource limiter, use [`Memory::grow_async`] instead.
563    ///
564    /// # Examples
565    ///
566    /// ```
567    /// # use wasmtime::*;
568    /// # fn main() -> anyhow::Result<()> {
569    /// let engine = Engine::default();
570    /// let mut store = Store::new(&engine, ());
571    /// let module = Module::new(&engine, "(module (memory (export \"mem\") 1 2))")?;
572    /// let instance = Instance::new(&mut store, &module, &[])?;
573    /// let memory = instance.get_memory(&mut store, "mem").unwrap();
574    ///
575    /// assert_eq!(memory.size(&store), 1);
576    /// assert_eq!(memory.grow(&mut store, 1)?, 1);
577    /// assert_eq!(memory.size(&store), 2);
578    /// assert!(memory.grow(&mut store, 1).is_err());
579    /// assert_eq!(memory.size(&store), 2);
580    /// assert_eq!(memory.grow(&mut store, 0)?, 2);
581    /// # Ok(())
582    /// # }
583    /// ```
584    pub fn grow(&self, mut store: impl AsContextMut, delta: u64) -> Result<u64> {
585        let store = store.as_context_mut().0;
586        let mem = self.wasmtime_memory(store);
587        unsafe {
588            match (*mem).grow(delta, Some(store))? {
589                Some(size) => {
590                    let vm = (*mem).vmmemory();
591                    store[self.0].definition.write(vm);
592                    let page_size = (*mem).page_size();
593                    Ok(u64::try_from(size).unwrap() / page_size)
594                }
595                None => bail!("failed to grow memory by `{}`", delta),
596            }
597        }
598    }
599
600    /// Async variant of [`Memory::grow`]. Required when using a
601    /// [`ResourceLimiterAsync`](`crate::ResourceLimiterAsync`).
602    ///
603    /// # Panics
604    ///
605    /// This function will panic when used with a non-async
606    /// [`Store`](`crate::Store`).
607    #[cfg(feature = "async")]
608    pub async fn grow_async<T>(
609        &self,
610        mut store: impl AsContextMut<Data = T>,
611        delta: u64,
612    ) -> Result<u64>
613    where
614        T: Send,
615    {
616        let mut store = store.as_context_mut();
617        assert!(
618            store.0.async_support(),
619            "cannot use `grow_async` without enabling async support on the config"
620        );
621        store.on_fiber(|store| self.grow(store, delta)).await?
622    }
623
624    fn wasmtime_memory(&self, store: &mut StoreOpaque) -> *mut crate::runtime::vm::Memory {
625        unsafe {
626            let export = &store[self.0];
627            crate::runtime::vm::Instance::from_vmctx(export.vmctx, |handle| {
628                handle.get_defined_memory(export.index)
629            })
630        }
631    }
632
633    pub(crate) unsafe fn from_wasmtime_memory(
634        wasmtime_export: crate::runtime::vm::ExportMemory,
635        store: &mut StoreOpaque,
636    ) -> Memory {
637        Memory(store.store_data_mut().insert(wasmtime_export))
638    }
639
640    pub(crate) fn wasmtime_ty<'a>(&self, store: &'a StoreData) -> &'a wasmtime_environ::Memory {
641        &store[self.0].memory
642    }
643
644    pub(crate) fn vmimport(&self, store: &StoreOpaque) -> crate::runtime::vm::VMMemoryImport {
645        let export = &store[self.0];
646        crate::runtime::vm::VMMemoryImport {
647            from: export.definition.into(),
648            vmctx: export.vmctx.into(),
649            index: export.index,
650        }
651    }
652
653    pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
654        store.store_data().contains(self.0)
655    }
656
657    /// Get a stable hash key for this memory.
658    ///
659    /// Even if the same underlying memory definition is added to the
660    /// `StoreData` multiple times and becomes multiple `wasmtime::Memory`s,
661    /// this hash key will be consistent across all of these memories.
662    #[cfg(feature = "coredump")]
663    pub(crate) fn hash_key(&self, store: &StoreOpaque) -> impl core::hash::Hash + Eq + use<> {
664        store[self.0].definition.as_ptr() as usize
665    }
666}
667
668/// A linear memory. This trait provides an interface for raw memory buffers
669/// which are used by wasmtime, e.g. inside ['Memory']. Such buffers are in
670/// principle not thread safe. By implementing this trait together with
671/// MemoryCreator, one can supply wasmtime with custom allocated host managed
672/// memory.
673///
674/// # Safety
675///
676/// The memory should be page aligned and a multiple of page size.
677/// To prevent possible silent overflows, the memory should be protected by a
678/// guard page.  Additionally the safety concerns explained in ['Memory'], for
679/// accessing the memory apply here as well.
680///
681/// Note that this is a relatively advanced feature and it is recommended to be
682/// familiar with wasmtime runtime code to use it.
683pub unsafe trait LinearMemory: Send + Sync + 'static {
684    /// Returns the number of allocated bytes which are accessible at this time.
685    fn byte_size(&self) -> usize;
686
687    /// Returns byte capacity of this linear memory's current allocation.
688    ///
689    /// Growth up to this value should not relocate the linear memory base
690    /// pointer.
691    fn byte_capacity(&self) -> usize;
692
693    /// Grows this memory to have the `new_size`, in bytes, specified.
694    ///
695    /// Returns `Err` if memory can't be grown by the specified amount
696    /// of bytes. The error may be downcastable to `std::io::Error`.
697    /// Returns `Ok` if memory was grown successfully.
698    fn grow_to(&mut self, new_size: usize) -> Result<()>;
699
700    /// Return the allocated memory as a mutable pointer to u8.
701    fn as_ptr(&self) -> *mut u8;
702}
703
704/// A memory creator. Can be used to provide a memory creator
705/// to wasmtime which supplies host managed memory.
706///
707/// # Safety
708///
709/// This trait is unsafe, as the memory safety depends on proper implementation
710/// of memory management. Memories created by the MemoryCreator should always be
711/// treated as owned by wasmtime instance, and any modification of them outside
712/// of wasmtime invoked routines is unsafe and may lead to corruption.
713///
714/// Note that this is a relatively advanced feature and it is recommended to be
715/// familiar with Wasmtime runtime code to use it.
716pub unsafe trait MemoryCreator: Send + Sync {
717    /// Create a new `LinearMemory` object from the specified parameters.
718    ///
719    /// The type of memory being created is specified by `ty` which indicates
720    /// both the minimum and maximum size, in wasm pages. The minimum and
721    /// maximum sizes, in bytes, are also specified as parameters to avoid
722    /// integer conversion if desired.
723    ///
724    /// The `reserved_size_in_bytes` value indicates the expected size of the
725    /// reservation that is to be made for this memory. If this value is `None`
726    /// than the implementation is free to allocate memory as it sees fit. If
727    /// the value is `Some`, however, then the implementation is expected to
728    /// reserve that many bytes for the memory's allocation, plus the guard
729    /// size at the end. Note that this reservation need only be a virtual
730    /// memory reservation, physical memory does not need to be allocated
731    /// immediately. In this case `grow` should never move the base pointer and
732    /// the maximum size of `ty` is guaranteed to fit within
733    /// `reserved_size_in_bytes`.
734    ///
735    /// The `guard_size_in_bytes` parameter indicates how many bytes of space,
736    /// after the memory allocation, is expected to be unmapped. JIT code will
737    /// elide bounds checks based on the `guard_size_in_bytes` provided, so for
738    /// JIT code to work correctly the memory returned will need to be properly
739    /// guarded with `guard_size_in_bytes` bytes left unmapped after the base
740    /// allocation.
741    ///
742    /// Note that the `reserved_size_in_bytes` and `guard_size_in_bytes` options
743    /// are tuned from the various [`Config`](crate::Config) methods about
744    /// memory sizes/guards. Additionally these two values are guaranteed to be
745    /// multiples of the system page size.
746    ///
747    /// Memory created from this method should be zero filled.
748    fn new_memory(
749        &self,
750        ty: MemoryType,
751        minimum: usize,
752        maximum: Option<usize>,
753        reserved_size_in_bytes: Option<usize>,
754        guard_size_in_bytes: usize,
755    ) -> Result<Box<dyn LinearMemory>, String>;
756}
757
758/// A constructor for externally-created shared memory.
759///
760/// The [threads proposal] adds the concept of "shared memory" to WebAssembly.
761/// This is much the same as a Wasm linear memory (i.e., [`Memory`]), but can be
762/// used concurrently by multiple agents. Because these agents may execute in
763/// different threads, [`SharedMemory`] must be thread-safe.
764///
765/// When the threads proposal is enabled, there are multiple ways to construct
766/// shared memory:
767///  1. for imported shared memory, e.g., `(import "env" "memory" (memory 1 1
768///     shared))`, the user must supply a [`SharedMemory`] with the
769///     externally-created memory as an import to the instance--e.g.,
770///     `shared_memory.into()`.
771///  2. for private or exported shared memory, e.g., `(export "env" "memory"
772///     (memory 1 1 shared))`, Wasmtime will create the memory internally during
773///     instantiation--access using `Instance::get_shared_memory()`.
774///
775/// [threads proposal]:
776///     https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md
777///
778/// # Examples
779///
780/// ```
781/// # use wasmtime::*;
782/// # fn main() -> anyhow::Result<()> {
783/// let mut config = Config::new();
784/// config.wasm_threads(true);
785/// let engine = Engine::new(&config)?;
786/// let mut store = Store::new(&engine, ());
787///
788/// let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, 2))?;
789/// let module = Module::new(&engine, r#"(module (memory (import "" "") 1 2 shared))"#)?;
790/// let instance = Instance::new(&mut store, &module, &[shared_memory.into()])?;
791/// // ...
792/// # Ok(())
793/// # }
794/// ```
795#[derive(Clone)]
796pub struct SharedMemory {
797    vm: crate::runtime::vm::SharedMemory,
798    engine: Engine,
799    page_size_log2: u8,
800}
801
802impl SharedMemory {
803    /// Construct a [`SharedMemory`] by providing both the `minimum` and
804    /// `maximum` number of 64K-sized pages. This call allocates the necessary
805    /// pages on the system.
806    #[cfg(feature = "threads")]
807    pub fn new(engine: &Engine, ty: MemoryType) -> Result<Self> {
808        if !ty.is_shared() {
809            bail!("shared memory must have the `shared` flag enabled on its memory type")
810        }
811        debug_assert!(ty.maximum().is_some());
812
813        let tunables = engine.tunables();
814        let ty = ty.wasmtime_memory();
815        let page_size_log2 = ty.page_size_log2;
816        let memory = crate::runtime::vm::SharedMemory::new(ty, tunables)?;
817
818        Ok(Self {
819            vm: memory,
820            engine: engine.clone(),
821            page_size_log2,
822        })
823    }
824
825    /// Return the type of the shared memory.
826    pub fn ty(&self) -> MemoryType {
827        MemoryType::from_wasmtime_memory(&self.vm.ty())
828    }
829
830    /// Returns the size, in WebAssembly pages, of this wasm memory.
831    pub fn size(&self) -> u64 {
832        let byte_size = u64::try_from(self.data_size()).unwrap();
833        let page_size = u64::from(self.page_size());
834        byte_size / page_size
835    }
836
837    /// Returns the size of a page, in bytes, for this memory.
838    ///
839    /// By default this is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or `65536`)
840    /// but [the custom-page-sizes proposal] allows opting into a page size of
841    /// `1`. Future extensions might allow any power of two as a page size.
842    ///
843    /// [the custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
844    pub fn page_size(&self) -> u32 {
845        debug_assert!(self.page_size_log2 == 0 || self.page_size_log2 == 16);
846        1 << self.page_size_log2
847    }
848
849    /// Returns the byte length of this memory.
850    ///
851    /// The returned value will be a multiple of the wasm page size, 64k.
852    ///
853    /// For more information and examples see the documentation on the
854    /// [`Memory`] type.
855    pub fn data_size(&self) -> usize {
856        self.vm.byte_size()
857    }
858
859    /// Return access to the available portion of the shared memory.
860    ///
861    /// The slice returned represents the region of accessible memory at the
862    /// time that this function was called. The contents of the returned slice
863    /// will reflect concurrent modifications happening on other threads.
864    ///
865    /// # Safety
866    ///
867    /// The returned slice is valid for the entire duration of the lifetime of
868    /// this instance of [`SharedMemory`]. The base pointer of a shared memory
869    /// does not change. This [`SharedMemory`] may grow further after this
870    /// function has been called, but the slice returned will not grow.
871    ///
872    /// Concurrent modifications may be happening to the data returned on other
873    /// threads. The `UnsafeCell<u8>` represents that safe access to the
874    /// contents of the slice is not possible through normal loads and stores.
875    ///
876    /// The memory returned must be accessed safely through the `Atomic*` types
877    /// in the [`std::sync::atomic`] module. Casting to those types must
878    /// currently be done unsafely.
879    pub fn data(&self) -> &[UnsafeCell<u8>] {
880        unsafe {
881            let definition = self.vm.vmmemory_ptr().as_ref();
882            slice::from_raw_parts(definition.base.as_ptr().cast(), definition.current_length())
883        }
884    }
885
886    /// Grows this WebAssembly memory by `delta` pages.
887    ///
888    /// This will attempt to add `delta` more pages of memory on to the end of
889    /// this `Memory` instance. If successful this may relocate the memory and
890    /// cause [`Memory::data_ptr`] to return a new value. Additionally any
891    /// unsafely constructed slices into this memory may no longer be valid.
892    ///
893    /// On success returns the number of pages this memory previously had
894    /// before the growth succeeded.
895    ///
896    /// # Errors
897    ///
898    /// Returns an error if memory could not be grown, for example if it exceeds
899    /// the maximum limits of this memory. A
900    /// [`ResourceLimiter`](crate::ResourceLimiter) is another example of
901    /// preventing a memory to grow.
902    pub fn grow(&self, delta: u64) -> Result<u64> {
903        match self.vm.grow(delta, None)? {
904            Some((old_size, _new_size)) => {
905                // For shared memory, the `VMMemoryDefinition` is updated inside
906                // the locked region.
907                Ok(u64::try_from(old_size).unwrap() / u64::from(self.page_size()))
908            }
909            None => bail!("failed to grow memory by `{}`", delta),
910        }
911    }
912
913    /// Equivalent of the WebAssembly `memory.atomic.notify` instruction for
914    /// this shared memory.
915    ///
916    /// This method allows embedders to notify threads blocked on the specified
917    /// `addr`, an index into wasm linear memory. Threads could include
918    /// wasm threads blocked on a `memory.atomic.wait*` instruction or embedder
919    /// threads blocked on [`SharedMemory::atomic_wait32`], for example.
920    ///
921    /// The `count` argument is the number of threads to wake up.
922    ///
923    /// This function returns the number of threads awoken.
924    ///
925    /// # Errors
926    ///
927    /// This function will return an error if `addr` is not within bounds or
928    /// not aligned to a 4-byte boundary.
929    pub fn atomic_notify(&self, addr: u64, count: u32) -> Result<u32, Trap> {
930        self.vm.atomic_notify(addr, count)
931    }
932
933    /// Equivalent of the WebAssembly `memory.atomic.wait32` instruction for
934    /// this shared memory.
935    ///
936    /// This method allows embedders to block the current thread until notified
937    /// via the `memory.atomic.notify` instruction or the
938    /// [`SharedMemory::atomic_notify`] method, enabling synchronization with
939    /// the wasm guest as desired.
940    ///
941    /// The `expected` argument is the expected 32-bit value to be stored at
942    /// the byte address `addr` specified. The `addr` specified is an index
943    /// into this linear memory.
944    ///
945    /// The optional `timeout` argument is the maximum amount of time to block
946    /// the current thread. If not specified the thread may sleep indefinitely.
947    ///
948    /// This function returns one of three possible values:
949    ///
950    /// * `WaitResult::Ok` - this function, loaded the value at `addr`, found
951    ///   it was equal to `expected`, and then blocked (all as one atomic
952    ///   operation). The thread was then awoken with a `memory.atomic.notify`
953    ///   instruction or the [`SharedMemory::atomic_notify`] method.
954    /// * `WaitResult::Mismatch` - the value at `addr` was loaded but was not
955    ///   equal to `expected` so the thread did not block and immediately
956    ///   returned.
957    /// * `WaitResult::TimedOut` - all the steps of `Ok` happened, except this
958    ///   thread was woken up due to a timeout.
959    ///
960    /// This function will not return due to spurious wakeups.
961    ///
962    /// # Errors
963    ///
964    /// This function will return an error if `addr` is not within bounds or
965    /// not aligned to a 4-byte boundary.
966    pub fn atomic_wait32(
967        &self,
968        addr: u64,
969        expected: u32,
970        timeout: Option<Duration>,
971    ) -> Result<WaitResult, Trap> {
972        self.vm.atomic_wait32(addr, expected, timeout)
973    }
974
975    /// Equivalent of the WebAssembly `memory.atomic.wait64` instruction for
976    /// this shared memory.
977    ///
978    /// For more information see [`SharedMemory::atomic_wait32`].
979    ///
980    /// # Errors
981    ///
982    /// Returns the same error as [`SharedMemory::atomic_wait32`] except that
983    /// the specified address must be 8-byte aligned instead of 4-byte aligned.
984    pub fn atomic_wait64(
985        &self,
986        addr: u64,
987        expected: u64,
988        timeout: Option<Duration>,
989    ) -> Result<WaitResult, Trap> {
990        self.vm.atomic_wait64(addr, expected, timeout)
991    }
992
993    /// Return a reference to the [`Engine`] used to configure the shared
994    /// memory.
995    pub(crate) fn engine(&self) -> &Engine {
996        &self.engine
997    }
998
999    /// Construct a single-memory instance to provide a way to import
1000    /// [`SharedMemory`] into other modules.
1001    pub(crate) fn vmimport(&self, store: &mut StoreOpaque) -> crate::runtime::vm::VMMemoryImport {
1002        let export_memory = generate_memory_export(store, &self.ty(), Some(&self.vm)).unwrap();
1003        VMMemoryImport {
1004            from: export_memory.definition.into(),
1005            vmctx: export_memory.vmctx.into(),
1006            index: export_memory.index,
1007        }
1008    }
1009
1010    /// Create a [`SharedMemory`] from an [`ExportMemory`] definition. This
1011    /// function is available to handle the case in which a Wasm module exports
1012    /// shared memory and the user wants host-side access to it.
1013    pub(crate) unsafe fn from_wasmtime_memory(
1014        wasmtime_export: crate::runtime::vm::ExportMemory,
1015        store: &mut StoreOpaque,
1016    ) -> Self {
1017        #[cfg_attr(not(feature = "threads"), allow(unused_variables, unreachable_code))]
1018        crate::runtime::vm::Instance::from_vmctx(wasmtime_export.vmctx, |handle| {
1019            let memory_index = handle.env_module().memory_index(wasmtime_export.index);
1020            let page_size = handle.memory_page_size(memory_index);
1021            debug_assert!(page_size.is_power_of_two());
1022            let page_size_log2 = u8::try_from(page_size.ilog2()).unwrap();
1023
1024            let memory = handle
1025                .get_defined_memory(wasmtime_export.index)
1026                .as_mut()
1027                .unwrap();
1028            match memory.as_shared_memory() {
1029                Some(mem) => Self {
1030                    vm: mem.clone(),
1031                    engine: store.engine().clone(),
1032                    page_size_log2,
1033                },
1034                None => panic!("unable to convert from a shared memory"),
1035            }
1036        })
1037    }
1038}
1039
1040impl fmt::Debug for SharedMemory {
1041    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1042        f.debug_struct("SharedMemory").finish_non_exhaustive()
1043    }
1044}
1045
1046#[cfg(test)]
1047mod tests {
1048    use crate::*;
1049
1050    // Assert that creating a memory via `Memory::new` respects the limits/tunables
1051    // in `Config`.
1052    #[test]
1053    fn respect_tunables() {
1054        let mut cfg = Config::new();
1055        cfg.memory_reservation(0).memory_guard_size(0);
1056        let mut store = Store::new(&Engine::new(&cfg).unwrap(), ());
1057        let ty = MemoryType::new(1, None);
1058        let mem = Memory::new(&mut store, ty).unwrap();
1059        let store = store.as_context();
1060        let tunables = store.engine().tunables();
1061        assert_eq!(tunables.memory_guard_size, 0);
1062        assert!(!store[mem.0].memory.can_elide_bounds_check(tunables, 12));
1063    }
1064
1065    #[test]
1066    fn hash_key_is_stable_across_duplicate_store_data_entries() -> Result<()> {
1067        let mut store = Store::<()>::default();
1068        let module = Module::new(
1069            store.engine(),
1070            r#"
1071                (module
1072                    (memory (export "m") 1 1)
1073                )
1074            "#,
1075        )?;
1076        let instance = Instance::new(&mut store, &module, &[])?;
1077
1078        // Each time we `get_memory`, we call `Memory::from_wasmtime` which adds
1079        // a new entry to `StoreData`, so `g1` and `g2` will have different
1080        // indices into `StoreData`.
1081        let m1 = instance.get_memory(&mut store, "m").unwrap();
1082        let m2 = instance.get_memory(&mut store, "m").unwrap();
1083
1084        // That said, they really point to the same memory.
1085        assert_eq!(m1.data(&store)[0], 0);
1086        assert_eq!(m2.data(&store)[0], 0);
1087        m1.data_mut(&mut store)[0] = 42;
1088        assert_eq!(m1.data(&mut store)[0], 42);
1089        assert_eq!(m2.data(&mut store)[0], 42);
1090
1091        // And therefore their hash keys are the same.
1092        assert!(m1.hash_key(&store.as_context().0) == m2.hash_key(&store.as_context().0));
1093
1094        // But the hash keys are different from different memories.
1095        let instance2 = Instance::new(&mut store, &module, &[])?;
1096        let m3 = instance2.get_memory(&mut store, "m").unwrap();
1097        assert!(m1.hash_key(&store.as_context().0) != m3.hash_key(&store.as_context().0));
1098
1099        Ok(())
1100    }
1101}