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