1 //! Memory management for linear memories. 2 //! 3 //! This module implements the runtime data structures that manage linear 4 //! memories for WebAssembly. There's a number of types here each with various 5 //! purposes, and this is the high level relationships between types where an 6 //! arrow here means "builds on top of". 7 //! 8 //! ```text 9 //! ┌─────────────────────┐ 10 //! │ │ 11 //! │ Memory ├─────────────┐ 12 //! │ │ │ 13 //! └──────────┬──────────┘ │ 14 //! │ │ 15 //! │ │ 16 //! ▼ ▼ 17 //! ┌─────────────────────┐ ┌──────────────┐ 18 //! │ │ │ │ 19 //! │ LocalMemory │◄────┤ SharedMemory │ 20 //! │ │ │ │ 21 //! └──────────┬──────────┘ └──────────────┘ 22 //! │ 23 //! │ 24 //! ▼ 25 //! ┌─────────────────────┐ 26 //! │ │ 27 //! │ RuntimeLinearMemory ├─────────────┬───────────────┐ 28 //! │ │ │ │ 29 //! └──────────┬──────────┘ │ │ 30 //! │ │ │ 31 //! │ │ │ 32 //! ▼ ▼ ▼ 33 //! ┌─────────────────────┐ ┌──────────────┐ ┌─────┐ 34 //! │ │ │ │ │ │ 35 //! │ MmapMemory │ │ StaticMemory │ │ ... │ 36 //! │ │ │ │ │ │ 37 //! └─────────────────────┘ └──────────────┘ └─────┘ 38 //! ``` 39 //! 40 //! In more detail: 41 //! 42 //! * `Memory` - the root of what's actually stored in a wasm instance. This 43 //! implements the high-level embedder APIs one would expect from a wasm 44 //! linear memory. 45 //! 46 //! * `SharedMemory` - this is one of the variants of a local memory. A shared 47 //! memory contains `RwLock<LocalMemory>` where all the real bits happen 48 //! within the lock. 49 //! 50 //! * `LocalMemory` - this is an owned allocation of a linear memory which 51 //! maintains low-level state that's shared between `SharedMemory` and the 52 //! instance-local state of `Memory`. One example is that `LocalMemory::grow` 53 //! has most of the logic around memory growth. 54 //! 55 //! * `RuntimeLinearMemory` - this is a trait which `LocalMemory` delegates to. 56 //! This trait is intentionally relatively simple to be exposed in Wasmtime's 57 //! embedder API. This is exposed all the way through `wasmtime::Config` so 58 //! embedders can provide arbitrary implementations. 59 //! 60 //! * `MmapMemory` - this is an implementation of `RuntimeLinearMemory` in terms 61 //! of the platform's mmap primitive. 62 //! 63 //! * `StaticMemory` - this is an implementation of `RuntimeLinearMemory` 64 //! for the pooling allocator where the base pointer is already allocated 65 //! and contents are managed through `MemoryImageSlot`. 66 //! 67 //! Other important types for memories are `MemoryImage` and `MemoryImageSlot` 68 //! which manage CoW state for memories. This is implemented at the 69 //! `LocalMemory` layer. 70 //! 71 //! FIXME: don't have both RuntimeLinearMemory and wasmtime::LinearMemory, they 72 //! should be merged together. 73 //! 74 //! FIXME: don't have both RuntimeMemoryCreator and wasmtime::MemoryCreator, 75 //! they should be merged together. 76 77 use crate::prelude::*; 78 use crate::runtime::vm::vmcontext::VMMemoryDefinition; 79 #[cfg(has_virtual_memory)] 80 use crate::runtime::vm::{HostAlignedByteCount, MmapOffset}; 81 use crate::runtime::vm::{MemoryImage, MemoryImageSlot, SendSyncPtr, VMStore}; 82 use alloc::sync::Arc; 83 use core::{ops::Range, ptr::NonNull}; 84 use wasmtime_environ::Tunables; 85 86 #[cfg(feature = "threads")] 87 use wasmtime_environ::Trap; 88 89 #[cfg(has_virtual_memory)] 90 mod mmap; 91 #[cfg(has_virtual_memory)] 92 pub use self::mmap::MmapMemory; 93 94 mod malloc; 95 pub use self::malloc::MallocMemory; 96 97 #[cfg(feature = "pooling-allocator")] 98 mod static_; 99 #[cfg(feature = "pooling-allocator")] 100 use self::static_::StaticMemory; 101 102 #[cfg(feature = "threads")] 103 mod shared_memory; 104 #[cfg(feature = "threads")] 105 pub use shared_memory::SharedMemory; 106 107 #[cfg(not(feature = "threads"))] 108 mod shared_memory_disabled; 109 #[cfg(not(feature = "threads"))] 110 pub use shared_memory_disabled::SharedMemory; 111 112 /// A memory allocator 113 pub trait RuntimeMemoryCreator: Send + Sync { 114 /// Create new RuntimeLinearMemory 115 fn new_memory( 116 &self, 117 ty: &wasmtime_environ::Memory, 118 tunables: &Tunables, 119 minimum: usize, 120 maximum: Option<usize>, 121 ) -> Result<Box<dyn RuntimeLinearMemory>>; 122 } 123 124 /// A default memory allocator used by Wasmtime 125 pub struct DefaultMemoryCreator; 126 127 impl RuntimeMemoryCreator for DefaultMemoryCreator { 128 /// Create new MmapMemory 129 fn new_memory( 130 &self, 131 ty: &wasmtime_environ::Memory, 132 tunables: &Tunables, 133 minimum: usize, 134 maximum: Option<usize>, 135 ) -> Result<Box<dyn RuntimeLinearMemory>> { 136 #[cfg(has_virtual_memory)] 137 if tunables.signals_based_traps 138 || tunables.memory_guard_size > 0 139 || tunables.memory_reservation > 0 140 || tunables.memory_init_cow 141 { 142 return Ok(Box::new(MmapMemory::new(ty, tunables, minimum, maximum)?)); 143 } 144 145 let _ = maximum; 146 Ok(Box::new(MallocMemory::new(ty, tunables, minimum)?)) 147 } 148 } 149 150 /// A linear memory and its backing storage. 151 pub trait RuntimeLinearMemory: Send + Sync { 152 /// Returns the number bytes that this linear memory can access. 153 fn byte_size(&self) -> usize; 154 155 /// Returns the maximal number of bytes the current allocation can access. 156 /// 157 /// Growth up to this value should not relocate the base pointer. 158 fn byte_capacity(&self) -> usize; 159 160 /// Grow memory to the specified amount of bytes. 161 /// 162 /// Returns an error if memory can't be grown by the specified amount 163 /// of bytes. 164 fn grow_to(&mut self, size: usize) -> Result<()>; 165 166 /// Returns a pointer to the base of this linear memory allocation. 167 /// 168 /// This is either a raw pointer, or a reference to an mmap along with an 169 /// offset within it. 170 fn base(&self) -> MemoryBase; 171 172 /// Get a `VMMemoryDefinition` for this linear memory. 173 fn vmmemory(&self) -> VMMemoryDefinition; 174 175 /// Internal method for Wasmtime when used in conjunction with CoW images. 176 /// This is used to inform the underlying memory that the size of memory has 177 /// changed. 178 /// 179 /// Note that this is hidden and panics by default as embedders using custom 180 /// memory without CoW images shouldn't have to worry about this. 181 #[doc(hidden)] 182 fn set_byte_size(&mut self, len: usize) { 183 let _ = len; 184 panic!("CoW images used with this memory and it doesn't support it"); 185 } 186 } 187 188 /// The base pointer of a memory allocation. 189 #[derive(Clone, Debug)] 190 pub enum MemoryBase { 191 /// A raw pointer into memory. 192 /// 193 /// This may or may not be host-page-aligned. 194 Raw(SendSyncPtr<u8>), 195 196 /// An mmap along with an offset into it. 197 #[cfg(has_virtual_memory)] 198 Mmap(MmapOffset), 199 } 200 201 impl MemoryBase { 202 /// Creates a new `MemoryBase` from a raw pointer. 203 /// 204 /// The pointer must be non-null, and it must be logically `Send + Sync`. 205 pub fn new_raw(ptr: *mut u8) -> Self { 206 Self::Raw(NonNull::new(ptr).expect("pointer is non-null").into()) 207 } 208 209 /// Returns the actual memory address in memory that is represented by this 210 /// base. 211 pub fn as_non_null(&self) -> NonNull<u8> { 212 match self { 213 Self::Raw(ptr) => ptr.as_non_null(), 214 #[cfg(has_virtual_memory)] 215 Self::Mmap(mmap_offset) => mmap_offset.as_non_null(), 216 } 217 } 218 219 /// Same as `as_non_null`, but different return type. 220 pub fn as_mut_ptr(&self) -> *mut u8 { 221 self.as_non_null().as_ptr() 222 } 223 } 224 225 /// Representation of a runtime wasm linear memory. 226 pub enum Memory { 227 Local(LocalMemory), 228 Shared(SharedMemory), 229 } 230 231 impl Memory { 232 /// Create a new dynamic (movable) memory instance for the specified plan. 233 pub fn new_dynamic( 234 ty: &wasmtime_environ::Memory, 235 tunables: &Tunables, 236 creator: &dyn RuntimeMemoryCreator, 237 store: &mut dyn VMStore, 238 memory_image: Option<&Arc<MemoryImage>>, 239 ) -> Result<Self> { 240 let (minimum, maximum) = Self::limit_new(ty, Some(store))?; 241 let allocation = creator.new_memory(ty, tunables, minimum, maximum)?; 242 243 let memory = LocalMemory::new(ty, tunables, allocation, memory_image)?; 244 Ok(if ty.shared { 245 Memory::Shared(SharedMemory::wrap(ty, memory)?) 246 } else { 247 Memory::Local(memory) 248 }) 249 } 250 251 /// Create a new static (immovable) memory instance for the specified plan. 252 #[cfg(feature = "pooling-allocator")] 253 pub fn new_static( 254 ty: &wasmtime_environ::Memory, 255 tunables: &Tunables, 256 base: MemoryBase, 257 base_capacity: usize, 258 memory_image: MemoryImageSlot, 259 store: &mut dyn VMStore, 260 ) -> Result<Self> { 261 let (minimum, maximum) = Self::limit_new(ty, Some(store))?; 262 let pooled_memory = StaticMemory::new(base, base_capacity, minimum, maximum)?; 263 let allocation = Box::new(pooled_memory); 264 265 // Configure some defaults a bit differently for this memory within the 266 // `LocalMemory` structure created, notably we already have 267 // `memory_image` and regardless of configuration settings this memory 268 // can't move its base pointer since it's a fixed allocation. 269 let mut memory = LocalMemory::new(ty, tunables, allocation, None)?; 270 assert!(memory.memory_image.is_none()); 271 memory.memory_image = Some(memory_image); 272 memory.memory_may_move = false; 273 274 Ok(if ty.shared { 275 // FIXME(#4244): not supported with the pooling allocator (which 276 // `new_static` is always used with), see `MemoryPool::validate` as 277 // well). 278 todo!("using shared memory with the pooling allocator is a work in progress"); 279 } else { 280 Memory::Local(memory) 281 }) 282 } 283 284 /// Calls the `store`'s limiter to optionally prevent a memory from being allocated. 285 /// 286 /// Returns a tuple of the minimum size, optional maximum size, and log(page 287 /// size) of the memory, all in bytes. 288 pub(crate) fn limit_new( 289 ty: &wasmtime_environ::Memory, 290 store: Option<&mut dyn VMStore>, 291 ) -> Result<(usize, Option<usize>)> { 292 let page_size = usize::try_from(ty.page_size()).unwrap(); 293 294 // This is the absolute possible maximum that the module can try to 295 // allocate, which is our entire address space minus a wasm page. That 296 // shouldn't ever actually work in terms of an allocation because 297 // presumably the kernel wants *something* for itself, but this is used 298 // to pass to the `store`'s limiter for a requested size 299 // to approximate the scale of the request that the wasm module is 300 // making. This is necessary because the limiter works on `usize` bytes 301 // whereas we're working with possibly-overflowing `u64` calculations 302 // here. To actually faithfully represent the byte requests of modules 303 // we'd have to represent things as `u128`, but that's kinda 304 // overkill for this purpose. 305 let absolute_max = 0usize.wrapping_sub(page_size); 306 307 // If the minimum memory size overflows the size of our own address 308 // space, then we can't satisfy this request, but defer the error to 309 // later so the `store` can be informed that an effective oom is 310 // happening. 311 let minimum = ty 312 .minimum_byte_size() 313 .ok() 314 .and_then(|m| usize::try_from(m).ok()); 315 316 // The plan stores the maximum size in units of wasm pages, but we 317 // use units of bytes. Unlike for the `minimum` size we silently clamp 318 // the effective maximum size to the limits of what we can track. If the 319 // maximum size exceeds `usize` or `u64` then there's no need to further 320 // keep track of it as some sort of runtime limit will kick in long 321 // before we reach the statically declared maximum size. 322 let maximum = ty 323 .maximum_byte_size() 324 .ok() 325 .and_then(|m| usize::try_from(m).ok()); 326 327 // Inform the store's limiter what's about to happen. This will let the 328 // limiter reject anything if necessary, and this also guarantees that 329 // we should call the limiter for all requested memories, even if our 330 // `minimum` calculation overflowed. This means that the `minimum` we're 331 // informing the limiter is lossy and may not be 100% accurate, but for 332 // now the expected uses of limiter means that's ok. 333 if let Some(store) = store { 334 if !store.memory_growing(0, minimum.unwrap_or(absolute_max), maximum)? { 335 bail!( 336 "memory minimum size of {} pages exceeds memory limits", 337 ty.limits.min 338 ); 339 } 340 } 341 342 // At this point we need to actually handle overflows, so bail out with 343 // an error if we made it this far. 344 let minimum = minimum.ok_or_else(|| { 345 format_err!( 346 "memory minimum size of {} pages exceeds memory limits", 347 ty.limits.min 348 ) 349 })?; 350 351 Ok((minimum, maximum)) 352 } 353 354 /// Returns this memory's page size, in bytes. 355 pub fn page_size(&self) -> u64 { 356 match self { 357 Memory::Local(mem) => mem.page_size(), 358 Memory::Shared(mem) => mem.page_size(), 359 } 360 } 361 362 /// Returns the size of this memory, in bytes. 363 pub fn byte_size(&self) -> usize { 364 match self { 365 Memory::Local(mem) => mem.byte_size(), 366 Memory::Shared(mem) => mem.byte_size(), 367 } 368 } 369 370 /// Returns whether or not this memory needs initialization. It 371 /// may not if it already has initial content thanks to a CoW 372 /// mechanism. 373 pub(crate) fn needs_init(&self) -> bool { 374 match self { 375 Memory::Local(mem) => mem.needs_init(), 376 Memory::Shared(mem) => mem.needs_init(), 377 } 378 } 379 380 /// Grow memory by the specified amount of wasm pages. 381 /// 382 /// Returns `None` if memory can't be grown by the specified amount 383 /// of wasm pages. Returns `Some` with the old size of memory, in bytes, on 384 /// successful growth. 385 /// 386 /// # Safety 387 /// 388 /// Resizing the memory can reallocate the memory buffer for dynamic memories. 389 /// An instance's `VMContext` may have pointers to the memory's base and will 390 /// need to be fixed up after growing the memory. 391 /// 392 /// Generally, prefer using `InstanceHandle::memory_grow`, which encapsulates 393 /// this unsafety. 394 /// 395 /// Ensure that the provided Store is not used to get access any Memory 396 /// which lives inside it. 397 pub unsafe fn grow( 398 &mut self, 399 delta_pages: u64, 400 store: Option<&mut dyn VMStore>, 401 ) -> Result<Option<usize>, Error> { 402 let result = match self { 403 Memory::Local(mem) => mem.grow(delta_pages, store)?, 404 Memory::Shared(mem) => mem.grow(delta_pages, store)?, 405 }; 406 match result { 407 Some((old, _new)) => Ok(Some(old)), 408 None => Ok(None), 409 } 410 } 411 412 /// Return a `VMMemoryDefinition` for exposing the memory to compiled wasm code. 413 pub fn vmmemory(&self) -> VMMemoryDefinition { 414 match self { 415 Memory::Local(mem) => mem.vmmemory(), 416 // `vmmemory()` is used for writing the `VMMemoryDefinition` of a 417 // memory into its `VMContext`; this should never be possible for a 418 // shared memory because the only `VMMemoryDefinition` for it should 419 // be stored in its own `def` field. 420 Memory::Shared(_) => unreachable!(), 421 } 422 } 423 424 /// Consume the memory, returning its [`MemoryImageSlot`] if any is present. 425 /// The image should only be present for a subset of memories created with 426 /// [`Memory::new_static()`]. 427 #[cfg(feature = "pooling-allocator")] 428 pub fn unwrap_static_image(self) -> MemoryImageSlot { 429 match self { 430 Memory::Local(mem) => mem.unwrap_static_image(), 431 Memory::Shared(_) => panic!("expected a local memory"), 432 } 433 } 434 435 /// If the [Memory] is a [SharedMemory], unwrap it and return a clone to 436 /// that shared memory. 437 pub fn as_shared_memory(&mut self) -> Option<&mut SharedMemory> { 438 match self { 439 Memory::Local(_) => None, 440 Memory::Shared(mem) => Some(mem), 441 } 442 } 443 444 /// Implementation of `memory.atomic.notify` for all memories. 445 #[cfg(feature = "threads")] 446 pub fn atomic_notify(&mut self, addr: u64, count: u32) -> Result<u32, Trap> { 447 match self.as_shared_memory() { 448 Some(m) => m.atomic_notify(addr, count), 449 None => { 450 validate_atomic_addr(&self.vmmemory(), addr, 4, 4)?; 451 Ok(0) 452 } 453 } 454 } 455 456 /// Implementation of `memory.atomic.wait32` for all memories. 457 #[cfg(feature = "threads")] 458 pub fn atomic_wait32( 459 &mut self, 460 addr: u64, 461 expected: u32, 462 timeout: Option<core::time::Duration>, 463 ) -> Result<crate::WaitResult, Trap> { 464 match self.as_shared_memory() { 465 Some(m) => m.atomic_wait32(addr, expected, timeout), 466 None => { 467 validate_atomic_addr(&self.vmmemory(), addr, 4, 4)?; 468 Err(Trap::AtomicWaitNonSharedMemory) 469 } 470 } 471 } 472 473 /// Implementation of `memory.atomic.wait64` for all memories. 474 #[cfg(feature = "threads")] 475 pub fn atomic_wait64( 476 &mut self, 477 addr: u64, 478 expected: u64, 479 timeout: Option<core::time::Duration>, 480 ) -> Result<crate::WaitResult, Trap> { 481 match self.as_shared_memory() { 482 Some(m) => m.atomic_wait64(addr, expected, timeout), 483 None => { 484 validate_atomic_addr(&self.vmmemory(), addr, 8, 8)?; 485 Err(Trap::AtomicWaitNonSharedMemory) 486 } 487 } 488 } 489 490 /// Returns the range of bytes that WebAssembly should be able to address in 491 /// this linear memory. Note that this includes guard pages which wasm can 492 /// hit. 493 pub fn wasm_accessible(&self) -> Range<usize> { 494 match self { 495 Memory::Local(mem) => mem.wasm_accessible(), 496 Memory::Shared(mem) => mem.wasm_accessible(), 497 } 498 } 499 } 500 501 /// An owned allocation of a wasm linear memory. 502 /// 503 /// This might be part of a `Memory` via `Memory::Local` but it might also be 504 /// the implementation basis for a `SharedMemory` behind an `RwLock` for 505 /// example. 506 pub struct LocalMemory { 507 alloc: Box<dyn RuntimeLinearMemory>, 508 ty: wasmtime_environ::Memory, 509 memory_may_move: bool, 510 memory_guard_size: usize, 511 memory_reservation: usize, 512 513 /// An optional CoW mapping that provides the initial content of this 514 /// memory. 515 memory_image: Option<MemoryImageSlot>, 516 } 517 518 impl LocalMemory { 519 pub fn new( 520 ty: &wasmtime_environ::Memory, 521 tunables: &Tunables, 522 alloc: Box<dyn RuntimeLinearMemory>, 523 memory_image: Option<&Arc<MemoryImage>>, 524 ) -> Result<LocalMemory> { 525 // If a memory image was specified, try to create the MemoryImageSlot on 526 // top of our mmap. 527 let memory_image = match memory_image { 528 #[cfg(has_virtual_memory)] 529 Some(image) => { 530 // We currently don't support memory_image if 531 // `RuntimeLinearMemory::byte_size` is not a multiple of the host page 532 // size. See https://github.com/bytecodealliance/wasmtime/issues/9660. 533 if let Ok(byte_size) = HostAlignedByteCount::new(alloc.byte_size()) { 534 // memory_image is CoW-based so it is expected to be backed 535 // by an mmap. 536 let mmap_base = match alloc.base() { 537 MemoryBase::Mmap(offset) => offset, 538 MemoryBase::Raw { .. } => { 539 unreachable!("memory_image is Some only for mmap-based memories") 540 } 541 }; 542 543 let mut slot = 544 MemoryImageSlot::create(mmap_base, byte_size, alloc.byte_capacity()); 545 // On drop, we will unmap our mmap'd range that this slot 546 // was mapped on top of, so there is no need for the slot to 547 // wipe it with an anonymous mapping first. 548 // 549 // Note that this code would be incorrect if clear-on-drop 550 // were enabled. That's because: 551 // 552 // * In the struct definition, `memory_image` above is listed 553 // after `alloc`. 554 // * Rust drops fields in the order they're defined, so 555 // `memory_image` would be dropped after `alloc`. 556 // * `alloc` can represent either owned memory (i.e. the mmap is 557 // freed on drop) or logically borrowed memory (something else 558 // manages the mmap). 559 // * If `alloc` is borrowed memory, then this isn't an issue. 560 // * But if `alloc` is owned memory, then it would first drop 561 // the mmap, and then `memory_image` would try to remap 562 // part of that same memory as part of clear-on-drop. 563 // 564 // A lot of this really suggests representing the ownership 565 // via Rust lifetimes -- that would be a major refactor, 566 // though. 567 slot.no_clear_on_drop(); 568 slot.instantiate(alloc.byte_size(), Some(image), ty, tunables)?; 569 Some(slot) 570 } else { 571 None 572 } 573 } 574 #[cfg(not(has_virtual_memory))] 575 Some(_) => unreachable!(), 576 None => None, 577 }; 578 Ok(LocalMemory { 579 ty: *ty, 580 alloc, 581 memory_may_move: ty.memory_may_move(tunables), 582 memory_image, 583 memory_guard_size: tunables.memory_guard_size.try_into().unwrap(), 584 memory_reservation: tunables.memory_reservation.try_into().unwrap(), 585 }) 586 } 587 588 pub fn page_size(&self) -> u64 { 589 self.ty.page_size() 590 } 591 592 /// Grows a memory by `delta_pages`. 593 /// 594 /// This performs the necessary checks on the growth before delegating to 595 /// the underlying `grow_to` implementation. 596 /// 597 /// The `store` is used only for error reporting. 598 pub fn grow( 599 &mut self, 600 delta_pages: u64, 601 mut store: Option<&mut dyn VMStore>, 602 ) -> Result<Option<(usize, usize)>, Error> { 603 let old_byte_size = self.alloc.byte_size(); 604 605 // Wasm spec: when growing by 0 pages, always return the current size. 606 if delta_pages == 0 { 607 return Ok(Some((old_byte_size, old_byte_size))); 608 } 609 610 let page_size = usize::try_from(self.page_size()).unwrap(); 611 612 // The largest wasm-page-aligned region of memory is possible to 613 // represent in a `usize`. This will be impossible for the system to 614 // actually allocate. 615 let absolute_max = 0usize.wrapping_sub(page_size); 616 617 // Calculate the byte size of the new allocation. Let it overflow up to 618 // `usize::MAX`, then clamp it down to `absolute_max`. 619 let new_byte_size = usize::try_from(delta_pages) 620 .unwrap_or(usize::MAX) 621 .saturating_mul(page_size) 622 .saturating_add(old_byte_size) 623 .min(absolute_max); 624 625 let maximum = self 626 .ty 627 .maximum_byte_size() 628 .ok() 629 .and_then(|n| usize::try_from(n).ok()); 630 631 // Store limiter gets first chance to reject memory_growing. 632 if let Some(store) = &mut store { 633 if !store.memory_growing(old_byte_size, new_byte_size, maximum)? { 634 return Ok(None); 635 } 636 } 637 638 // Save the original base pointer to assert the invariant that growth up 639 // to the byte capacity never relocates the base pointer. 640 let base_ptr_before = self.alloc.base().as_mut_ptr(); 641 let required_to_not_move_memory = new_byte_size <= self.alloc.byte_capacity(); 642 643 let result = (|| -> Result<()> { 644 // Never exceed maximum, even if limiter permitted it. 645 if let Some(max) = maximum { 646 if new_byte_size > max { 647 bail!("Memory maximum size exceeded"); 648 } 649 } 650 651 // If memory isn't allowed to move then don't let growth happen 652 // beyond the initial capacity 653 if !self.memory_may_move && new_byte_size > self.alloc.byte_capacity() { 654 bail!("Memory maximum size exceeded"); 655 } 656 657 // If we have a CoW image overlay then let it manage accessible 658 // bytes. Once the heap limit is modified inform the underlying 659 // allocation that the size has changed. 660 // 661 // If the growth is going beyond the size of the heap image then 662 // discard it. This should only happen for `MmapMemory` where 663 // `no_clear_on_drop` is set so the destructor doesn't do anything. 664 // For now be maximally sure about this by asserting that memory can 665 // indeed move and that we're on unix. If this wants to run 666 // somewhere else like Windows or with other allocations this may 667 // need adjusting. 668 if let Some(image) = &mut self.memory_image { 669 if new_byte_size <= self.alloc.byte_capacity() { 670 image.set_heap_limit(new_byte_size)?; 671 self.alloc.set_byte_size(new_byte_size); 672 return Ok(()); 673 } 674 assert!(cfg!(unix)); 675 assert!(self.memory_may_move); 676 self.memory_image = None; 677 } 678 679 // And failing all that fall back to the underlying allocation to 680 // grow it. 681 self.alloc.grow_to(new_byte_size) 682 })(); 683 684 match result { 685 Ok(()) => { 686 // On successful growth double-check that the base pointer 687 // didn't move if it shouldn't have. 688 if required_to_not_move_memory { 689 assert_eq!(base_ptr_before, self.alloc.base().as_mut_ptr()); 690 } 691 692 Ok(Some((old_byte_size, new_byte_size))) 693 } 694 Err(e) => { 695 // FIXME: shared memories may not have an associated store to 696 // report the growth failure to but the error should not be 697 // dropped 698 // (https://github.com/bytecodealliance/wasmtime/issues/4240). 699 if let Some(store) = store { 700 store.memory_grow_failed(e)?; 701 } 702 Ok(None) 703 } 704 } 705 } 706 707 pub fn vmmemory(&self) -> VMMemoryDefinition { 708 self.alloc.vmmemory() 709 } 710 711 pub fn byte_size(&self) -> usize { 712 self.alloc.byte_size() 713 } 714 715 pub fn needs_init(&self) -> bool { 716 match &self.memory_image { 717 Some(image) => !image.has_image(), 718 None => true, 719 } 720 } 721 722 pub fn wasm_accessible(&self) -> Range<usize> { 723 let base = self.alloc.base().as_mut_ptr() as usize; 724 // From the base add: 725 // 726 // * max(capacity, reservation) -- all memory is guaranteed to have at 727 // least `memory_reservation`, but capacity may go beyond that. 728 // * memory_guard_size - wasm is allowed to hit the guard page for 729 // sigsegv for example. 730 // 731 // and this computes the range that wasm is allowed to load from and 732 // deterministically trap or succeed. 733 let end = 734 base + self.alloc.byte_capacity().max(self.memory_reservation) + self.memory_guard_size; 735 base..end 736 } 737 738 #[cfg(feature = "pooling-allocator")] 739 pub fn unwrap_static_image(self) -> MemoryImageSlot { 740 self.memory_image.unwrap() 741 } 742 } 743 744 /// In the configurations where bounds checks were elided in JIT code (because 745 /// we are using static memories with virtual memory guard pages) this manual 746 /// check is here so we don't segfault from Rust. For other configurations, 747 /// these checks are required anyways. 748 #[cfg(feature = "threads")] 749 pub fn validate_atomic_addr( 750 def: &VMMemoryDefinition, 751 addr: u64, 752 access_size: u64, 753 access_alignment: u64, 754 ) -> Result<*mut u8, Trap> { 755 debug_assert!(access_alignment.is_power_of_two()); 756 if !(addr % access_alignment == 0) { 757 return Err(Trap::HeapMisaligned); 758 } 759 760 let length = u64::try_from(def.current_length()).unwrap(); 761 if !(addr.saturating_add(access_size) < length) { 762 return Err(Trap::MemoryOutOfBounds); 763 } 764 765 let addr = usize::try_from(addr).unwrap(); 766 Ok(def.base.as_ptr().wrapping_add(addr)) 767 } 768