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