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