1 //! An `Instance` contains all the runtime state used by execution of a 2 //! wasm module (except its callstack and register state). An 3 //! `InstanceHandle` is a reference-counting handle for an `Instance`. 4 5 use crate::runtime::vm::const_expr::{ConstEvalContext, ConstExprEvaluator}; 6 use crate::runtime::vm::export::Export; 7 use crate::runtime::vm::memory::{Memory, RuntimeMemoryCreator}; 8 use crate::runtime::vm::table::{Table, TableElement, TableElementType}; 9 use crate::runtime::vm::vmcontext::{ 10 VMBuiltinFunctionsArray, VMContext, VMFuncRef, VMFunctionImport, VMGlobalDefinition, 11 VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMOpaqueContext, VMRuntimeLimits, 12 VMTableDefinition, VMTableImport, VMTagDefinition, VMTagImport, 13 }; 14 use crate::runtime::vm::{ 15 ExportFunction, ExportGlobal, ExportMemory, ExportTable, ExportTag, GcStore, Imports, 16 ModuleRuntimeInfo, SendSyncPtr, VMFunctionBody, VMGcRef, VMStore, VMStoreRawPtr, VmPtr, VmSafe, 17 WasmFault, 18 }; 19 use crate::store::{StoreInner, StoreOpaque}; 20 use crate::{prelude::*, StoreContextMut}; 21 use alloc::sync::Arc; 22 use core::alloc::Layout; 23 use core::any::Any; 24 use core::ops::Range; 25 use core::ptr::NonNull; 26 #[cfg(target_has_atomic = "64")] 27 use core::sync::atomic::AtomicU64; 28 use core::{mem, ptr}; 29 use sptr::Strict; 30 #[cfg(feature = "gc")] 31 use wasmtime_environ::ModuleInternedTypeIndex; 32 use wasmtime_environ::{ 33 packed_option::ReservedValue, DataIndex, DefinedGlobalIndex, DefinedMemoryIndex, 34 DefinedTableIndex, DefinedTagIndex, ElemIndex, EntityIndex, EntityRef, EntitySet, FuncIndex, 35 GlobalIndex, HostPtr, MemoryIndex, Module, PrimaryMap, PtrSize, TableIndex, TableInitialValue, 36 TableSegmentElements, TagIndex, Trap, VMOffsets, VMSharedTypeIndex, WasmHeapTopType, 37 VMCONTEXT_MAGIC, 38 }; 39 #[cfg(feature = "wmemcheck")] 40 use wasmtime_wmemcheck::Wmemcheck; 41 42 mod allocator; 43 pub use allocator::*; 44 45 /// The pair of an instance and a raw pointer its associated store. 46 /// 47 /// ### Safety 48 /// 49 /// Getting a borrow of a vmctx's store is one of the fundamental bits of unsafe 50 /// code in Wasmtime. No matter how we architect the runtime, some kind of 51 /// unsafe conversion from a raw vmctx pointer that Wasm is using into a Rust 52 /// struct must happen. 53 /// 54 /// It is our responsibility to ensure that multiple (exclusive) borrows of the 55 /// vmctx's store never exist at the same time. The distinction between the 56 /// `Instance` type (which doesn't expose its underlying vmctx pointer or a way 57 /// to get a borrow of its associated store) and this type (which does) is 58 /// designed to help with that. 59 /// 60 /// Going from a `*mut VMContext` to a `&mut StoreInner<T>` is naturally unsafe 61 /// due to the raw pointer usage, but additionally the `T` type parameter needs 62 /// to be the same `T` that was used to define the `dyn VMStore` trait object 63 /// that was stuffed into the vmctx. 64 /// 65 /// ### Usage 66 /// 67 /// Usage generally looks like: 68 /// 69 /// 1. You get a raw `*mut VMContext` from Wasm 70 /// 71 /// 2. You call `InstanceAndStore::from_vmctx` on that raw pointer 72 /// 73 /// 3. You then call `InstanceAndStore::unpack_mut` (or another helper) to get 74 /// the underlying `&mut Instance` and `&mut dyn VMStore` (or `&mut 75 /// StoreInner<T>`). 76 /// 77 /// 4. You then use whatever `Instance` methods you need to, each of which take 78 /// a store argument as necessary. 79 /// 80 /// In step (4) you no longer need to worry about double exclusive borrows of 81 /// the store, so long as you don't do (1-2) again. Note also that the borrow 82 /// checker prevents repeating step (3) if you never repeat (1-2). In general, 83 /// steps (1-3) should be done in a single, common, internally-unsafe, 84 /// plumbing-code bottleneck and the raw pointer should never be exposed to Rust 85 /// code that does (4) after the `InstanceAndStore` is created. Follow this 86 /// pattern, and everything using the resulting `Instance` and `Store` can be 87 /// safe code (at least, with regards to accessing the store itself). 88 /// 89 /// As an illustrative example, the common plumbing code for our various 90 /// libcalls performs steps (1-3) before calling into each actual libcall 91 /// implementation function that does (4). The plumbing code hides the raw vmctx 92 /// pointer and never gives out access to it to the libcall implementation 93 /// functions, nor does an `Instance` expose its internal vmctx pointer, which 94 /// would allow unsafely repeating steps (1-2). 95 #[repr(transparent)] 96 pub struct InstanceAndStore { 97 instance: Instance, 98 } 99 100 impl InstanceAndStore { 101 /// Converts the provided `*mut VMContext` to an `InstanceAndStore` 102 /// reference and calls the provided closure with it. 103 /// 104 /// This method will move the `vmctx` pointer backwards to point to the 105 /// original `Instance` that precedes it. The closure is provided a 106 /// temporary reference to the `InstanceAndStore` with a constrained 107 /// lifetime to ensure that it doesn't accidentally escape. 108 /// 109 /// # Safety 110 /// 111 /// Callers must validate that the `vmctx` pointer is a valid allocation and 112 /// that it's valid to acquire `&mut InstanceAndStore` at this time. For 113 /// example this can't be called twice on the same `VMContext` to get two 114 /// active mutable borrows to the same `InstanceAndStore`. 115 /// 116 /// See also the safety discussion in this type's documentation. 117 #[inline] 118 pub(crate) unsafe fn from_vmctx<R>( 119 vmctx: NonNull<VMContext>, 120 f: impl for<'a> FnOnce(&'a mut Self) -> R, 121 ) -> R { 122 const _: () = assert!(mem::size_of::<InstanceAndStore>() == mem::size_of::<Instance>()); 123 let mut ptr = vmctx 124 .byte_sub(mem::size_of::<Instance>()) 125 .cast::<InstanceAndStore>(); 126 127 f(ptr.as_mut()) 128 } 129 130 /// Unpacks this `InstanceAndStore` into its underlying `Instance` and `dyn 131 /// VMStore`. 132 #[inline] 133 pub(crate) fn unpack_mut(&mut self) -> (&mut Instance, &mut dyn VMStore) { 134 unsafe { 135 let store = &mut *self.store_ptr(); 136 (&mut self.instance, store) 137 } 138 } 139 140 /// Unpacks this `InstanceAndStore` into its underlying `Instance` and 141 /// `StoreInner<T>`. 142 /// 143 /// # Safety 144 /// 145 /// The `T` must be the same `T` that was used to define this store's 146 /// instance. 147 #[inline] 148 pub(crate) unsafe fn unpack_context_mut<T>( 149 &mut self, 150 ) -> (&mut Instance, StoreContextMut<'_, T>) { 151 let store_ptr = self.store_ptr().cast::<StoreInner<T>>(); 152 (&mut self.instance, StoreContextMut(&mut *store_ptr)) 153 } 154 155 /// Gets a pointer to this instance's `Store` which was originally 156 /// configured on creation. 157 /// 158 /// # Panics 159 /// 160 /// May panic if the originally configured store was `None`. That can happen 161 /// for host functions so host functions can't be queried what their 162 /// original `Store` was since it's just retained as null (since host 163 /// functions are shared amongst threads and don't all share the same 164 /// store). 165 #[inline] 166 fn store_ptr(&self) -> *mut dyn VMStore { 167 self.instance.store.unwrap().0.as_ptr() 168 } 169 } 170 171 /// A type that roughly corresponds to a WebAssembly instance, but is also used 172 /// for host-defined objects. 173 /// 174 /// This structure is is never allocated directly but is instead managed through 175 /// an `InstanceHandle`. This structure ends with a `VMContext` which has a 176 /// dynamic size corresponding to the `module` configured within. Memory 177 /// management of this structure is always externalized. 178 /// 179 /// Instances here can correspond to actual instantiated modules, but it's also 180 /// used ubiquitously for host-defined objects. For example creating a 181 /// host-defined memory will have a `module` that looks like it exports a single 182 /// memory (and similar for other constructs). 183 /// 184 /// This `Instance` type is used as a ubiquitous representation for WebAssembly 185 /// values, whether or not they were created on the host or through a module. 186 #[repr(C)] // ensure that the vmctx field is last. 187 pub struct Instance { 188 /// The runtime info (corresponding to the "compiled module" 189 /// abstraction in higher layers) that is retained and needed for 190 /// lazy initialization. This provides access to the underlying 191 /// Wasm module entities, the compiled JIT code, metadata about 192 /// functions, lazy initialization state, etc. 193 runtime_info: ModuleRuntimeInfo, 194 195 /// WebAssembly linear memory data. 196 /// 197 /// This is where all runtime information about defined linear memories in 198 /// this module lives. 199 /// 200 /// The `MemoryAllocationIndex` was given from our `InstanceAllocator` and 201 /// must be given back to the instance allocator when deallocating each 202 /// memory. 203 memories: PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>, 204 205 /// WebAssembly table data. 206 /// 207 /// Like memories, this is only for defined tables in the module and 208 /// contains all of their runtime state. 209 /// 210 /// The `TableAllocationIndex` was given from our `InstanceAllocator` and 211 /// must be given back to the instance allocator when deallocating each 212 /// table. 213 tables: PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>, 214 215 /// Stores the dropped passive element segments in this instantiation by index. 216 /// If the index is present in the set, the segment has been dropped. 217 dropped_elements: EntitySet<ElemIndex>, 218 219 /// Stores the dropped passive data segments in this instantiation by index. 220 /// If the index is present in the set, the segment has been dropped. 221 dropped_data: EntitySet<DataIndex>, 222 223 /// Hosts can store arbitrary per-instance information here. 224 /// 225 /// Most of the time from Wasmtime this is `Box::new(())`, a noop 226 /// allocation, but some host-defined objects will store their state here. 227 host_state: Box<dyn Any + Send + Sync>, 228 229 /// A pointer to the `vmctx` field at the end of the `Instance`. 230 /// 231 /// If you're looking at this a reasonable question would be "why do we need 232 /// a pointer to ourselves?" because after all the pointer's value is 233 /// trivially derivable from any `&Instance` pointer. The rationale for this 234 /// field's existence is subtle, but it's required for correctness. The 235 /// short version is "this makes miri happy". 236 /// 237 /// The long version of why this field exists is that the rules that MIRI 238 /// uses to ensure pointers are used correctly have various conditions on 239 /// them depend on how pointers are used. More specifically if `*mut T` is 240 /// derived from `&mut T`, then that invalidates all prior pointers drived 241 /// from the `&mut T`. This means that while we liberally want to re-acquire 242 /// a `*mut VMContext` throughout the implementation of `Instance` the 243 /// trivial way, a function `fn vmctx(&mut Instance) -> *mut VMContext` 244 /// would effectively invalidate all prior `*mut VMContext` pointers 245 /// acquired. The purpose of this field is to serve as a sort of 246 /// source-of-truth for where `*mut VMContext` pointers come from. 247 /// 248 /// This field is initialized when the `Instance` is created with the 249 /// original allocation's pointer. That means that the provenance of this 250 /// pointer contains the entire allocation (both instance and `VMContext`). 251 /// This provenance bit is then "carried through" where `fn vmctx` will base 252 /// all returned pointers on this pointer itself. This provides the means of 253 /// never invalidating this pointer throughout MIRI and additionally being 254 /// able to still temporarily have `&mut Instance` methods and such. 255 /// 256 /// It's important to note, though, that this is not here purely for MIRI. 257 /// The careful construction of the `fn vmctx` method has ramifications on 258 /// the LLVM IR generated, for example. A historical CVE on Wasmtime, 259 /// GHSA-ch89-5g45-qwc7, was caused due to relying on undefined behavior. By 260 /// deriving VMContext pointers from this pointer it specifically hints to 261 /// LLVM that trickery is afoot and it properly informs `noalias` and such 262 /// annotations and analysis. More-or-less this pointer is actually loaded 263 /// in LLVM IR which helps defeat otherwise present aliasing optimizations, 264 /// which we want, since writes to this should basically never be optimized 265 /// out. 266 /// 267 /// As a final note it's worth pointing out that the machine code generated 268 /// for accessing `fn vmctx` is still as one would expect. This member isn't 269 /// actually ever loaded at runtime (or at least shouldn't be). Perhaps in 270 /// the future if the memory consumption of this field is a problem we could 271 /// shrink it slightly, but for now one extra pointer per wasm instance 272 /// seems not too bad. 273 vmctx_self_reference: SendSyncPtr<VMContext>, 274 275 // TODO: add support for multiple memories; `wmemcheck_state` corresponds to 276 // memory 0. 277 #[cfg(feature = "wmemcheck")] 278 pub(crate) wmemcheck_state: Option<Wmemcheck>, 279 280 /// Self-pointer back to `Store<T>` and its functions. Not present for 281 /// the brief time that `Store<T>` is itself being created. Also not 282 /// present for some niche uses that are disconnected from stores (e.g. 283 /// cross-thread stuff used in `InstancePre`) 284 store: Option<VMStoreRawPtr>, 285 286 /// Additional context used by compiled wasm code. This field is last, and 287 /// represents a dynamically-sized array that extends beyond the nominal 288 /// end of the struct (similar to a flexible array member). 289 vmctx: VMContext, 290 } 291 292 impl Instance { 293 /// Create an instance at the given memory address. 294 /// 295 /// It is assumed the memory was properly aligned and the 296 /// allocation was `alloc_size` in bytes. 297 unsafe fn new( 298 req: InstanceAllocationRequest, 299 memories: PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>, 300 tables: PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>, 301 memory_tys: &PrimaryMap<MemoryIndex, wasmtime_environ::Memory>, 302 ) -> InstanceHandle { 303 // The allocation must be *at least* the size required of `Instance`. 304 let layout = Self::alloc_layout(req.runtime_info.offsets()); 305 let ptr = alloc::alloc::alloc(layout); 306 if ptr.is_null() { 307 alloc::alloc::handle_alloc_error(layout); 308 } 309 let ptr = ptr.cast::<Instance>(); 310 311 let module = req.runtime_info.env_module(); 312 let dropped_elements = EntitySet::with_capacity(module.passive_elements.len()); 313 let dropped_data = EntitySet::with_capacity(module.passive_data_map.len()); 314 315 #[cfg(not(feature = "wmemcheck"))] 316 let _ = memory_tys; 317 318 ptr::write( 319 ptr, 320 Instance { 321 runtime_info: req.runtime_info.clone(), 322 memories, 323 tables, 324 dropped_elements, 325 dropped_data, 326 host_state: req.host_state, 327 vmctx_self_reference: SendSyncPtr::new(NonNull::new(ptr.add(1).cast()).unwrap()), 328 vmctx: VMContext { 329 _marker: core::marker::PhantomPinned, 330 }, 331 #[cfg(feature = "wmemcheck")] 332 wmemcheck_state: { 333 if req.wmemcheck { 334 let size = memory_tys 335 .iter() 336 .next() 337 .map(|memory| memory.1.limits.min) 338 .unwrap_or(0) 339 * 64 340 * 1024; 341 Some(Wmemcheck::new(size as usize)) 342 } else { 343 None 344 } 345 }, 346 store: None, 347 }, 348 ); 349 350 (*ptr).initialize_vmctx(module, req.runtime_info.offsets(), req.store, req.imports); 351 InstanceHandle { 352 instance: Some(SendSyncPtr::new(NonNull::new(ptr).unwrap())), 353 } 354 } 355 356 /// Converts the provided `*mut VMContext` to an `Instance` pointer and runs 357 /// the provided closure with the instance. 358 /// 359 /// This method will move the `vmctx` pointer backwards to point to the 360 /// original `Instance` that precedes it. The closure is provided a 361 /// temporary version of the `Instance` pointer with a constrained lifetime 362 /// to the closure to ensure it doesn't accidentally escape. 363 /// 364 /// # Unsafety 365 /// 366 /// Callers must validate that the `vmctx` pointer is a valid allocation 367 /// and that it's valid to acquire `&mut Instance` at this time. For example 368 /// this can't be called twice on the same `VMContext` to get two active 369 /// pointers to the same `Instance`. 370 #[inline] 371 pub unsafe fn from_vmctx<R>( 372 vmctx: NonNull<VMContext>, 373 f: impl FnOnce(&mut Instance) -> R, 374 ) -> R { 375 let mut ptr = vmctx 376 .byte_sub(mem::size_of::<Instance>()) 377 .cast::<Instance>(); 378 f(ptr.as_mut()) 379 } 380 381 /// Helper function to access various locations offset from our `*mut 382 /// VMContext` object. 383 /// 384 /// # Safety 385 /// 386 /// This method is unsafe because the `offset` must be within bounds of the 387 /// `VMContext` object trailing this instance. 388 unsafe fn vmctx_plus_offset<T: VmSafe>(&self, offset: impl Into<u32>) -> *const T { 389 self.vmctx() 390 .as_ptr() 391 .byte_add(usize::try_from(offset.into()).unwrap()) 392 .cast() 393 } 394 395 /// Dual of `vmctx_plus_offset`, but for mutability. 396 unsafe fn vmctx_plus_offset_mut<T: VmSafe>(&mut self, offset: impl Into<u32>) -> NonNull<T> { 397 self.vmctx() 398 .byte_add(usize::try_from(offset.into()).unwrap()) 399 .cast() 400 } 401 402 pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> { 403 self.runtime_info.env_module() 404 } 405 406 #[cfg(feature = "gc")] 407 pub(crate) fn runtime_module(&self) -> Option<&crate::Module> { 408 match &self.runtime_info { 409 ModuleRuntimeInfo::Module(m) => Some(m), 410 ModuleRuntimeInfo::Bare(_) => None, 411 } 412 } 413 414 /// Translate a module-level interned type index into an engine-level 415 /// interned type index. 416 #[cfg(feature = "gc")] 417 pub fn engine_type_index(&self, module_index: ModuleInternedTypeIndex) -> VMSharedTypeIndex { 418 self.runtime_info.engine_type_index(module_index) 419 } 420 421 #[inline] 422 fn offsets(&self) -> &VMOffsets<HostPtr> { 423 self.runtime_info.offsets() 424 } 425 426 /// Return the indexed `VMFunctionImport`. 427 fn imported_function(&self, index: FuncIndex) -> &VMFunctionImport { 428 unsafe { &*self.vmctx_plus_offset(self.offsets().vmctx_vmfunction_import(index)) } 429 } 430 431 /// Return the index `VMTableImport`. 432 fn imported_table(&self, index: TableIndex) -> &VMTableImport { 433 unsafe { &*self.vmctx_plus_offset(self.offsets().vmctx_vmtable_import(index)) } 434 } 435 436 /// Return the indexed `VMMemoryImport`. 437 fn imported_memory(&self, index: MemoryIndex) -> &VMMemoryImport { 438 unsafe { &*self.vmctx_plus_offset(self.offsets().vmctx_vmmemory_import(index)) } 439 } 440 441 /// Return the indexed `VMGlobalImport`. 442 fn imported_global(&self, index: GlobalIndex) -> &VMGlobalImport { 443 unsafe { &*self.vmctx_plus_offset(self.offsets().vmctx_vmglobal_import(index)) } 444 } 445 446 /// Return the indexed `VMTagImport`. 447 fn imported_tag(&self, index: TagIndex) -> &VMTagImport { 448 unsafe { &*self.vmctx_plus_offset(self.offsets().vmctx_vmtag_import(index)) } 449 } 450 451 /// Return the indexed `VMTagDefinition`. 452 fn tag_ptr(&mut self, index: DefinedTagIndex) -> NonNull<VMTagDefinition> { 453 unsafe { self.vmctx_plus_offset_mut(self.offsets().vmctx_vmtag_definition(index)) } 454 } 455 456 /// Return the indexed `VMTableDefinition`. 457 #[allow(dead_code)] 458 fn table(&mut self, index: DefinedTableIndex) -> VMTableDefinition { 459 unsafe { self.table_ptr(index).read() } 460 } 461 462 /// Updates the value for a defined table to `VMTableDefinition`. 463 fn set_table(&mut self, index: DefinedTableIndex, table: VMTableDefinition) { 464 unsafe { 465 self.table_ptr(index).write(table); 466 } 467 } 468 469 /// Return the indexed `VMTableDefinition`. 470 fn table_ptr(&mut self, index: DefinedTableIndex) -> NonNull<VMTableDefinition> { 471 unsafe { self.vmctx_plus_offset_mut(self.offsets().vmctx_vmtable_definition(index)) } 472 } 473 474 /// Get a locally defined or imported memory. 475 pub(crate) fn get_memory(&self, index: MemoryIndex) -> VMMemoryDefinition { 476 if let Some(defined_index) = self.env_module().defined_memory_index(index) { 477 self.memory(defined_index) 478 } else { 479 let import = self.imported_memory(index); 480 unsafe { VMMemoryDefinition::load(import.from.as_ptr()) } 481 } 482 } 483 484 /// Get a locally defined or imported memory. 485 #[cfg(feature = "threads")] 486 pub(crate) fn get_runtime_memory(&mut self, index: MemoryIndex) -> &mut Memory { 487 if let Some(defined_index) = self.env_module().defined_memory_index(index) { 488 unsafe { &mut *self.get_defined_memory(defined_index) } 489 } else { 490 let import = self.imported_memory(index); 491 unsafe { 492 let ptr = Instance::from_vmctx(import.vmctx.as_non_null(), |i| { 493 i.get_defined_memory(import.index) 494 }); 495 &mut *ptr 496 } 497 } 498 } 499 500 /// Return the indexed `VMMemoryDefinition`. 501 fn memory(&self, index: DefinedMemoryIndex) -> VMMemoryDefinition { 502 unsafe { VMMemoryDefinition::load(self.memory_ptr(index).as_ptr()) } 503 } 504 505 /// Set the indexed memory to `VMMemoryDefinition`. 506 fn set_memory(&self, index: DefinedMemoryIndex, mem: VMMemoryDefinition) { 507 unsafe { 508 self.memory_ptr(index).write(mem); 509 } 510 } 511 512 /// Return the indexed `VMMemoryDefinition`. 513 fn memory_ptr(&self, index: DefinedMemoryIndex) -> NonNull<VMMemoryDefinition> { 514 let vmptr = unsafe { 515 *self.vmctx_plus_offset::<VmPtr<_>>(self.offsets().vmctx_vmmemory_pointer(index)) 516 }; 517 vmptr.as_non_null() 518 } 519 520 /// Return the indexed `VMGlobalDefinition`. 521 fn global_ptr(&mut self, index: DefinedGlobalIndex) -> NonNull<VMGlobalDefinition> { 522 unsafe { self.vmctx_plus_offset_mut(self.offsets().vmctx_vmglobal_definition(index)) } 523 } 524 525 /// Get a raw pointer to the global at the given index regardless whether it 526 /// is defined locally or imported from another module. 527 /// 528 /// Panics if the index is out of bound or is the reserved value. 529 pub(crate) fn defined_or_imported_global_ptr( 530 &mut self, 531 index: GlobalIndex, 532 ) -> NonNull<VMGlobalDefinition> { 533 if let Some(index) = self.env_module().defined_global_index(index) { 534 self.global_ptr(index) 535 } else { 536 self.imported_global(index).from.as_non_null() 537 } 538 } 539 540 /// Get all globals within this instance. 541 /// 542 /// Returns both import and defined globals. 543 /// 544 /// Returns both exported and non-exported globals. 545 /// 546 /// Gives access to the full globals space. 547 pub fn all_globals<'a>( 548 &'a mut self, 549 ) -> impl ExactSizeIterator<Item = (GlobalIndex, ExportGlobal)> + 'a { 550 let module = self.env_module().clone(); 551 module.globals.keys().map(move |idx| { 552 ( 553 idx, 554 ExportGlobal { 555 definition: self.defined_or_imported_global_ptr(idx), 556 vmctx: Some(self.vmctx()), 557 global: self.env_module().globals[idx], 558 }, 559 ) 560 }) 561 } 562 563 /// Get the globals defined in this instance (not imported). 564 pub fn defined_globals<'a>( 565 &'a mut self, 566 ) -> impl ExactSizeIterator<Item = (DefinedGlobalIndex, ExportGlobal)> + 'a { 567 let module = self.env_module().clone(); 568 module 569 .globals 570 .keys() 571 .skip(module.num_imported_globals) 572 .map(move |global_idx| { 573 let def_idx = module.defined_global_index(global_idx).unwrap(); 574 let global = ExportGlobal { 575 definition: self.global_ptr(def_idx), 576 vmctx: Some(self.vmctx()), 577 global: self.env_module().globals[global_idx], 578 }; 579 (def_idx, global) 580 }) 581 } 582 583 /// Return a pointer to the interrupts structure 584 #[inline] 585 pub fn runtime_limits(&mut self) -> NonNull<Option<VmPtr<VMRuntimeLimits>>> { 586 unsafe { self.vmctx_plus_offset_mut(self.offsets().ptr.vmctx_runtime_limits()) } 587 } 588 589 /// Return a pointer to the global epoch counter used by this instance. 590 #[cfg(target_has_atomic = "64")] 591 pub fn epoch_ptr(&mut self) -> NonNull<Option<VmPtr<AtomicU64>>> { 592 unsafe { self.vmctx_plus_offset_mut(self.offsets().ptr.vmctx_epoch_ptr()) } 593 } 594 595 /// Return a pointer to the GC heap base pointer. 596 pub fn gc_heap_base(&mut self) -> NonNull<Option<VmPtr<u8>>> { 597 unsafe { self.vmctx_plus_offset_mut(self.offsets().ptr.vmctx_gc_heap_base()) } 598 } 599 600 /// Return a pointer to the GC heap bound. 601 pub fn gc_heap_bound(&mut self) -> NonNull<usize> { 602 unsafe { self.vmctx_plus_offset_mut(self.offsets().ptr.vmctx_gc_heap_bound()) } 603 } 604 605 /// Return a pointer to the collector-specific heap data. 606 pub fn gc_heap_data(&mut self) -> NonNull<Option<VmPtr<u8>>> { 607 unsafe { self.vmctx_plus_offset_mut(self.offsets().ptr.vmctx_gc_heap_data()) } 608 } 609 610 pub(crate) unsafe fn set_store(&mut self, store: Option<NonNull<dyn VMStore>>) { 611 self.store = store.map(VMStoreRawPtr); 612 if let Some(mut store) = store { 613 let store = store.as_mut(); 614 self.runtime_limits() 615 .write(Some(store.vmruntime_limits().into())); 616 #[cfg(target_has_atomic = "64")] 617 self.epoch_ptr() 618 .write(Some(NonNull::from(store.engine().epoch_counter()).into())); 619 self.set_gc_heap(store.gc_store_mut().ok()); 620 } else { 621 self.runtime_limits().write(None); 622 #[cfg(target_has_atomic = "64")] 623 self.epoch_ptr().write(None); 624 self.set_gc_heap(None); 625 } 626 } 627 628 unsafe fn set_gc_heap(&mut self, gc_store: Option<&mut GcStore>) { 629 if let Some(gc_store) = gc_store { 630 let heap = gc_store.gc_heap.heap_slice_mut(); 631 self.gc_heap_bound().write(heap.len()); 632 self.gc_heap_base() 633 .write(Some(NonNull::from(heap).cast().into())); 634 self.gc_heap_data() 635 .write(Some(gc_store.gc_heap.vmctx_gc_heap_data().into())); 636 } else { 637 self.gc_heap_bound().write(0); 638 self.gc_heap_base().write(None); 639 self.gc_heap_data().write(None); 640 } 641 } 642 643 pub(crate) unsafe fn set_callee(&mut self, callee: Option<NonNull<VMFunctionBody>>) { 644 let callee = callee.map(|p| VmPtr::from(p)); 645 self.vmctx_plus_offset_mut(self.offsets().ptr.vmctx_callee()) 646 .write(callee); 647 } 648 649 /// Return a reference to the vmctx used by compiled wasm code. 650 #[inline] 651 pub fn vmctx(&self) -> NonNull<VMContext> { 652 // The definition of this method is subtle but intentional. The goal 653 // here is that effectively this should return `&mut self.vmctx`, but 654 // it's not quite so simple. Some more documentation is available on the 655 // `vmctx_self_reference` field, but the general idea is that we're 656 // creating a pointer to return with proper provenance. Provenance is 657 // still in the works in Rust at the time of this writing but the load 658 // of the `self.vmctx_self_reference` field is important here as it 659 // affects how LLVM thinks about aliasing with respect to the returned 660 // pointer. 661 // 662 // The intention of this method is to codegen to machine code as `&mut 663 // self.vmctx`, however. While it doesn't show up like this in LLVM IR 664 // (there's an actual load of the field) it does look like that by the 665 // time the backend runs. (that's magic to me, the backend removing 666 // loads...) 667 // 668 // As a final minor note, strict provenance APIs are not stable on Rust 669 // today so the `sptr` crate is used. This crate provides the extension 670 // trait `Strict` but the method names conflict with the nightly methods 671 // so a different syntax is used to invoke methods here. 672 let addr = &raw const self.vmctx; 673 let ret = Strict::with_addr(self.vmctx_self_reference.as_ptr(), Strict::addr(addr)); 674 NonNull::new(ret).unwrap() 675 } 676 677 fn get_exported_func(&mut self, index: FuncIndex) -> ExportFunction { 678 let func_ref = self.get_func_ref(index).unwrap(); 679 ExportFunction { func_ref } 680 } 681 682 fn get_exported_table(&mut self, index: TableIndex) -> ExportTable { 683 let (definition, vmctx) = 684 if let Some(def_index) = self.env_module().defined_table_index(index) { 685 (self.table_ptr(def_index), self.vmctx()) 686 } else { 687 let import = self.imported_table(index); 688 (import.from.as_non_null(), import.vmctx.as_non_null()) 689 }; 690 ExportTable { 691 definition, 692 vmctx, 693 table: self.env_module().tables[index], 694 } 695 } 696 697 fn get_exported_memory(&mut self, index: MemoryIndex) -> ExportMemory { 698 let (definition, vmctx, def_index) = 699 if let Some(def_index) = self.env_module().defined_memory_index(index) { 700 (self.memory_ptr(def_index), self.vmctx(), def_index) 701 } else { 702 let import = self.imported_memory(index); 703 ( 704 import.from.as_non_null(), 705 import.vmctx.as_non_null(), 706 import.index, 707 ) 708 }; 709 ExportMemory { 710 definition, 711 vmctx, 712 memory: self.env_module().memories[index], 713 index: def_index, 714 } 715 } 716 717 fn get_exported_global(&mut self, index: GlobalIndex) -> ExportGlobal { 718 ExportGlobal { 719 definition: if let Some(def_index) = self.env_module().defined_global_index(index) { 720 self.global_ptr(def_index) 721 } else { 722 self.imported_global(index).from.as_non_null() 723 }, 724 vmctx: Some(self.vmctx()), 725 global: self.env_module().globals[index], 726 } 727 } 728 729 fn get_exported_tag(&mut self, index: TagIndex) -> ExportTag { 730 ExportTag { 731 definition: if let Some(def_index) = self.env_module().defined_tag_index(index) { 732 self.tag_ptr(def_index) 733 } else { 734 self.imported_tag(index).from.as_non_null() 735 }, 736 tag: self.env_module().tags[index], 737 } 738 } 739 740 /// Return an iterator over the exports of this instance. 741 /// 742 /// Specifically, it provides access to the key-value pairs, where the keys 743 /// are export names, and the values are export declarations which can be 744 /// resolved `lookup_by_declaration`. 745 pub fn exports(&self) -> wasmparser::collections::index_map::Iter<String, EntityIndex> { 746 self.env_module().exports.iter() 747 } 748 749 /// Return a reference to the custom state attached to this instance. 750 #[inline] 751 pub fn host_state(&self) -> &dyn Any { 752 &*self.host_state 753 } 754 755 /// Return the table index for the given `VMTableDefinition`. 756 pub unsafe fn table_index(&mut self, table: &VMTableDefinition) -> DefinedTableIndex { 757 let index = DefinedTableIndex::new( 758 usize::try_from( 759 (table as *const VMTableDefinition) 760 .offset_from(self.table_ptr(DefinedTableIndex::new(0)).as_ptr()), 761 ) 762 .unwrap(), 763 ); 764 assert!(index.index() < self.tables.len()); 765 index 766 } 767 768 /// Get the given memory's page size, in bytes. 769 pub(crate) fn memory_page_size(&self, index: MemoryIndex) -> usize { 770 usize::try_from(self.env_module().memories[index].page_size()).unwrap() 771 } 772 773 /// Grow memory by the specified amount of pages. 774 /// 775 /// Returns `None` if memory can't be grown by the specified amount 776 /// of pages. Returns `Some` with the old size in bytes if growth was 777 /// successful. 778 pub(crate) fn memory_grow( 779 &mut self, 780 store: &mut dyn VMStore, 781 index: MemoryIndex, 782 delta: u64, 783 ) -> Result<Option<usize>, Error> { 784 match self.env_module().defined_memory_index(index) { 785 Some(idx) => self.defined_memory_grow(store, idx, delta), 786 None => { 787 let import = self.imported_memory(index); 788 unsafe { 789 Instance::from_vmctx(import.vmctx.as_non_null(), |i| { 790 i.defined_memory_grow(store, import.index, delta) 791 }) 792 } 793 } 794 } 795 } 796 797 fn defined_memory_grow( 798 &mut self, 799 store: &mut dyn VMStore, 800 idx: DefinedMemoryIndex, 801 delta: u64, 802 ) -> Result<Option<usize>, Error> { 803 let memory = &mut self.memories[idx].1; 804 805 let result = unsafe { memory.grow(delta, Some(store)) }; 806 807 // Update the state used by a non-shared Wasm memory in case the base 808 // pointer and/or the length changed. 809 if memory.as_shared_memory().is_none() { 810 let vmmemory = memory.vmmemory(); 811 self.set_memory(idx, vmmemory); 812 } 813 814 result 815 } 816 817 pub(crate) fn table_element_type(&mut self, table_index: TableIndex) -> TableElementType { 818 unsafe { (*self.get_table(table_index)).element_type() } 819 } 820 821 /// Grow table by the specified amount of elements, filling them with 822 /// `init_value`. 823 /// 824 /// Returns `None` if table can't be grown by the specified amount of 825 /// elements, or if `init_value` is the wrong type of table element. 826 pub(crate) fn table_grow( 827 &mut self, 828 store: &mut dyn VMStore, 829 table_index: TableIndex, 830 delta: u64, 831 init_value: TableElement, 832 ) -> Result<Option<usize>, Error> { 833 self.with_defined_table_index_and_instance(table_index, |i, instance| { 834 instance.defined_table_grow(store, i, delta, init_value) 835 }) 836 } 837 838 fn defined_table_grow( 839 &mut self, 840 store: &mut dyn VMStore, 841 table_index: DefinedTableIndex, 842 delta: u64, 843 init_value: TableElement, 844 ) -> Result<Option<usize>, Error> { 845 let table = &mut self 846 .tables 847 .get_mut(table_index) 848 .unwrap_or_else(|| panic!("no table for index {}", table_index.index())) 849 .1; 850 851 let result = unsafe { table.grow(delta, init_value, store) }; 852 853 // Keep the `VMContext` pointers used by compiled Wasm code up to 854 // date. 855 let element = self.tables[table_index].1.vmtable(); 856 self.set_table(table_index, element); 857 858 result 859 } 860 861 fn alloc_layout(offsets: &VMOffsets<HostPtr>) -> Layout { 862 let size = mem::size_of::<Self>() 863 .checked_add(usize::try_from(offsets.size_of_vmctx()).unwrap()) 864 .unwrap(); 865 let align = mem::align_of::<Self>(); 866 Layout::from_size_align(size, align).unwrap() 867 } 868 869 fn type_ids_array(&mut self) -> NonNull<VmPtr<VMSharedTypeIndex>> { 870 unsafe { self.vmctx_plus_offset_mut(self.offsets().ptr.vmctx_type_ids_array()) } 871 } 872 873 /// Construct a new VMFuncRef for the given function 874 /// (imported or defined in this module) and store into the given 875 /// location. Used during lazy initialization. 876 /// 877 /// Note that our current lazy-init scheme actually calls this every 878 /// time the funcref pointer is fetched; this turns out to be better 879 /// than tracking state related to whether it's been initialized 880 /// before, because resetting that state on (re)instantiation is 881 /// very expensive if there are many funcrefs. 882 fn construct_func_ref( 883 &mut self, 884 index: FuncIndex, 885 type_index: VMSharedTypeIndex, 886 into: *mut VMFuncRef, 887 ) { 888 let func_ref = if let Some(def_index) = self.env_module().defined_func_index(index) { 889 VMFuncRef { 890 array_call: self 891 .runtime_info 892 .array_to_wasm_trampoline(def_index) 893 .expect("should have array-to-Wasm trampoline for escaping function") 894 .into(), 895 wasm_call: Some(self.runtime_info.function(def_index).into()), 896 vmctx: VMOpaqueContext::from_vmcontext(self.vmctx()).into(), 897 type_index, 898 } 899 } else { 900 let import = self.imported_function(index); 901 VMFuncRef { 902 array_call: import.array_call, 903 wasm_call: Some(import.wasm_call), 904 vmctx: import.vmctx, 905 type_index, 906 } 907 }; 908 909 // Safety: we have a `&mut self`, so we have exclusive access 910 // to this Instance. 911 unsafe { 912 ptr::write(into, func_ref); 913 } 914 } 915 916 /// Get a `&VMFuncRef` for the given `FuncIndex`. 917 /// 918 /// Returns `None` if the index is the reserved index value. 919 /// 920 /// The returned reference is a stable reference that won't be moved and can 921 /// be passed into JIT code. 922 pub(crate) fn get_func_ref(&mut self, index: FuncIndex) -> Option<NonNull<VMFuncRef>> { 923 if index == FuncIndex::reserved_value() { 924 return None; 925 } 926 927 // Safety: we have a `&mut self`, so we have exclusive access 928 // to this Instance. 929 unsafe { 930 // For now, we eagerly initialize an funcref struct in-place 931 // whenever asked for a reference to it. This is mostly 932 // fine, because in practice each funcref is unlikely to be 933 // requested more than a few times: once-ish for funcref 934 // tables used for call_indirect (the usual compilation 935 // strategy places each function in the table at most once), 936 // and once or a few times when fetching exports via API. 937 // Note that for any case driven by table accesses, the lazy 938 // table init behaves like a higher-level cache layer that 939 // protects this initialization from happening multiple 940 // times, via that particular table at least. 941 // 942 // When `ref.func` becomes more commonly used or if we 943 // otherwise see a use-case where this becomes a hotpath, 944 // we can reconsider by using some state to track 945 // "uninitialized" explicitly, for example by zeroing the 946 // funcrefs (perhaps together with other 947 // zeroed-at-instantiate-time state) or using a separate 948 // is-initialized bitmap. 949 // 950 // We arrived at this design because zeroing memory is 951 // expensive, so it's better for instantiation performance 952 // if we don't have to track "is-initialized" state at 953 // all! 954 let func = &self.env_module().functions[index]; 955 let sig = func.signature.unwrap_engine_type_index(); 956 let func_ref = self 957 .vmctx_plus_offset_mut::<VMFuncRef>(self.offsets().vmctx_func_ref(func.func_ref)); 958 self.construct_func_ref(index, sig, func_ref.as_ptr()); 959 960 Some(func_ref) 961 } 962 } 963 964 /// Get the passive elements segment at the given index. 965 /// 966 /// Returns an empty segment if the index is out of bounds or if the segment 967 /// has been dropped. 968 /// 969 /// The `storage` parameter should always be `None`; it is a bit of a hack 970 /// to work around lifetime issues. 971 pub(crate) fn passive_element_segment<'a>( 972 &self, 973 storage: &'a mut Option<(Arc<wasmtime_environ::Module>, TableSegmentElements)>, 974 elem_index: ElemIndex, 975 ) -> &'a TableSegmentElements { 976 debug_assert!(storage.is_none()); 977 *storage = Some(( 978 // TODO: this `clone()` shouldn't be necessary but is used for now to 979 // inform `rustc` that the lifetime of the elements here are 980 // disconnected from the lifetime of `self`. 981 self.env_module().clone(), 982 // NB: fall back to an expressions-based list of elements which 983 // doesn't have static type information (as opposed to 984 // `TableSegmentElements::Functions`) since we don't know what type 985 // is needed in the caller's context. Let the type be inferred by 986 // how they use the segment. 987 TableSegmentElements::Expressions(Box::new([])), 988 )); 989 let (module, empty) = storage.as_ref().unwrap(); 990 991 match module.passive_elements_map.get(&elem_index) { 992 Some(index) if !self.dropped_elements.contains(elem_index) => { 993 &module.passive_elements[*index] 994 } 995 _ => empty, 996 } 997 } 998 999 /// The `table.init` operation: initializes a portion of a table with a 1000 /// passive element. 1001 /// 1002 /// # Errors 1003 /// 1004 /// Returns a `Trap` error when the range within the table is out of bounds 1005 /// or the range within the passive element is out of bounds. 1006 pub(crate) fn table_init( 1007 &mut self, 1008 store: &mut StoreOpaque, 1009 table_index: TableIndex, 1010 elem_index: ElemIndex, 1011 dst: u64, 1012 src: u64, 1013 len: u64, 1014 ) -> Result<(), Trap> { 1015 let mut storage = None; 1016 let elements = self.passive_element_segment(&mut storage, elem_index); 1017 let mut const_evaluator = ConstExprEvaluator::default(); 1018 self.table_init_segment( 1019 store, 1020 &mut const_evaluator, 1021 table_index, 1022 elements, 1023 dst, 1024 src, 1025 len, 1026 ) 1027 } 1028 1029 pub(crate) fn table_init_segment( 1030 &mut self, 1031 store: &mut StoreOpaque, 1032 const_evaluator: &mut ConstExprEvaluator, 1033 table_index: TableIndex, 1034 elements: &TableSegmentElements, 1035 dst: u64, 1036 src: u64, 1037 len: u64, 1038 ) -> Result<(), Trap> { 1039 // https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-table-init 1040 1041 let table = unsafe { &mut *self.get_table(table_index) }; 1042 let src = usize::try_from(src).map_err(|_| Trap::TableOutOfBounds)?; 1043 let len = usize::try_from(len).map_err(|_| Trap::TableOutOfBounds)?; 1044 let module = self.env_module().clone(); 1045 1046 match elements { 1047 TableSegmentElements::Functions(funcs) => { 1048 let elements = funcs 1049 .get(src..) 1050 .and_then(|s| s.get(..len)) 1051 .ok_or(Trap::TableOutOfBounds)?; 1052 table.init_func(dst, elements.iter().map(|idx| self.get_func_ref(*idx)))?; 1053 } 1054 TableSegmentElements::Expressions(exprs) => { 1055 let exprs = exprs 1056 .get(src..) 1057 .and_then(|s| s.get(..len)) 1058 .ok_or(Trap::TableOutOfBounds)?; 1059 let mut context = ConstEvalContext::new(self); 1060 match module.tables[table_index].ref_type.heap_type.top() { 1061 WasmHeapTopType::Extern => table.init_gc_refs( 1062 dst, 1063 exprs.iter().map(|expr| unsafe { 1064 let raw = const_evaluator 1065 .eval(store, &mut context, expr) 1066 .expect("const expr should be valid"); 1067 VMGcRef::from_raw_u32(raw.get_externref()) 1068 }), 1069 )?, 1070 WasmHeapTopType::Any => table.init_gc_refs( 1071 dst, 1072 exprs.iter().map(|expr| unsafe { 1073 let raw = const_evaluator 1074 .eval(store, &mut context, expr) 1075 .expect("const expr should be valid"); 1076 VMGcRef::from_raw_u32(raw.get_anyref()) 1077 }), 1078 )?, 1079 WasmHeapTopType::Func => table.init_func( 1080 dst, 1081 exprs.iter().map(|expr| unsafe { 1082 NonNull::new( 1083 const_evaluator 1084 .eval(store, &mut context, expr) 1085 .expect("const expr should be valid") 1086 .get_funcref() 1087 .cast(), 1088 ) 1089 }), 1090 )?, 1091 } 1092 } 1093 } 1094 1095 Ok(()) 1096 } 1097 1098 /// Drop an element. 1099 pub(crate) fn elem_drop(&mut self, elem_index: ElemIndex) { 1100 // https://webassembly.github.io/reference-types/core/exec/instructions.html#exec-elem-drop 1101 1102 self.dropped_elements.insert(elem_index); 1103 1104 // Note that we don't check that we actually removed a segment because 1105 // dropping a non-passive segment is a no-op (not a trap). 1106 } 1107 1108 /// Get a locally-defined memory. 1109 pub fn get_defined_memory(&mut self, index: DefinedMemoryIndex) -> *mut Memory { 1110 // SAFETY: the `unsafe` here is projecting from `*mut (A, B)` to 1111 // `*mut A`, which should be a safe operation to do. 1112 unsafe { &raw mut (*self.memories.get_raw_mut(index).unwrap()).1 } 1113 } 1114 1115 /// Do a `memory.copy` 1116 /// 1117 /// # Errors 1118 /// 1119 /// Returns a `Trap` error when the source or destination ranges are out of 1120 /// bounds. 1121 pub(crate) fn memory_copy( 1122 &mut self, 1123 dst_index: MemoryIndex, 1124 dst: u64, 1125 src_index: MemoryIndex, 1126 src: u64, 1127 len: u64, 1128 ) -> Result<(), Trap> { 1129 // https://webassembly.github.io/reference-types/core/exec/instructions.html#exec-memory-copy 1130 1131 let src_mem = self.get_memory(src_index); 1132 let dst_mem = self.get_memory(dst_index); 1133 1134 let src = self.validate_inbounds(src_mem.current_length(), src, len)?; 1135 let dst = self.validate_inbounds(dst_mem.current_length(), dst, len)?; 1136 let len = usize::try_from(len).unwrap(); 1137 1138 // Bounds and casts are checked above, by this point we know that 1139 // everything is safe. 1140 unsafe { 1141 let dst = dst_mem.base.as_ptr().add(dst); 1142 let src = src_mem.base.as_ptr().add(src); 1143 // FIXME audit whether this is safe in the presence of shared memory 1144 // (https://github.com/bytecodealliance/wasmtime/issues/4203). 1145 ptr::copy(src, dst, len); 1146 } 1147 1148 Ok(()) 1149 } 1150 1151 fn validate_inbounds(&self, max: usize, ptr: u64, len: u64) -> Result<usize, Trap> { 1152 let oob = || Trap::MemoryOutOfBounds; 1153 let end = ptr 1154 .checked_add(len) 1155 .and_then(|i| usize::try_from(i).ok()) 1156 .ok_or_else(oob)?; 1157 if end > max { 1158 Err(oob()) 1159 } else { 1160 Ok(ptr.try_into().unwrap()) 1161 } 1162 } 1163 1164 /// Perform the `memory.fill` operation on a locally defined memory. 1165 /// 1166 /// # Errors 1167 /// 1168 /// Returns a `Trap` error if the memory range is out of bounds. 1169 pub(crate) fn memory_fill( 1170 &mut self, 1171 memory_index: MemoryIndex, 1172 dst: u64, 1173 val: u8, 1174 len: u64, 1175 ) -> Result<(), Trap> { 1176 let memory = self.get_memory(memory_index); 1177 let dst = self.validate_inbounds(memory.current_length(), dst, len)?; 1178 let len = usize::try_from(len).unwrap(); 1179 1180 // Bounds and casts are checked above, by this point we know that 1181 // everything is safe. 1182 unsafe { 1183 let dst = memory.base.as_ptr().add(dst); 1184 // FIXME audit whether this is safe in the presence of shared memory 1185 // (https://github.com/bytecodealliance/wasmtime/issues/4203). 1186 ptr::write_bytes(dst, val, len); 1187 } 1188 1189 Ok(()) 1190 } 1191 1192 /// Get the internal storage range of a particular Wasm data segment. 1193 pub(crate) fn wasm_data_range(&self, index: DataIndex) -> Range<u32> { 1194 match self.env_module().passive_data_map.get(&index) { 1195 Some(range) if !self.dropped_data.contains(index) => range.clone(), 1196 _ => 0..0, 1197 } 1198 } 1199 1200 /// Given an internal storage range of a Wasm data segment (or subset of a 1201 /// Wasm data segment), get the data's raw bytes. 1202 pub(crate) fn wasm_data(&self, range: Range<u32>) -> &[u8] { 1203 let start = usize::try_from(range.start).unwrap(); 1204 let end = usize::try_from(range.end).unwrap(); 1205 &self.runtime_info.wasm_data()[start..end] 1206 } 1207 1208 /// Performs the `memory.init` operation. 1209 /// 1210 /// # Errors 1211 /// 1212 /// Returns a `Trap` error if the destination range is out of this module's 1213 /// memory's bounds or if the source range is outside the data segment's 1214 /// bounds. 1215 pub(crate) fn memory_init( 1216 &mut self, 1217 memory_index: MemoryIndex, 1218 data_index: DataIndex, 1219 dst: u64, 1220 src: u32, 1221 len: u32, 1222 ) -> Result<(), Trap> { 1223 let range = self.wasm_data_range(data_index); 1224 self.memory_init_segment(memory_index, range, dst, src, len) 1225 } 1226 1227 pub(crate) fn memory_init_segment( 1228 &mut self, 1229 memory_index: MemoryIndex, 1230 range: Range<u32>, 1231 dst: u64, 1232 src: u32, 1233 len: u32, 1234 ) -> Result<(), Trap> { 1235 // https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-memory-init 1236 1237 let memory = self.get_memory(memory_index); 1238 let data = self.wasm_data(range); 1239 let dst = self.validate_inbounds(memory.current_length(), dst, len.into())?; 1240 let src = self.validate_inbounds(data.len(), src.into(), len.into())?; 1241 let len = len as usize; 1242 1243 unsafe { 1244 let src_start = data.as_ptr().add(src); 1245 let dst_start = memory.base.as_ptr().add(dst); 1246 // FIXME audit whether this is safe in the presence of shared memory 1247 // (https://github.com/bytecodealliance/wasmtime/issues/4203). 1248 ptr::copy_nonoverlapping(src_start, dst_start, len); 1249 } 1250 1251 Ok(()) 1252 } 1253 1254 /// Drop the given data segment, truncating its length to zero. 1255 pub(crate) fn data_drop(&mut self, data_index: DataIndex) { 1256 self.dropped_data.insert(data_index); 1257 1258 // Note that we don't check that we actually removed a segment because 1259 // dropping a non-passive segment is a no-op (not a trap). 1260 } 1261 1262 /// Get a table by index regardless of whether it is locally-defined 1263 /// or an imported, foreign table. Ensure that the given range of 1264 /// elements in the table is lazily initialized. We define this 1265 /// operation all-in-one for safety, to ensure the lazy-init 1266 /// happens. 1267 /// 1268 /// Takes an `Iterator` for the index-range to lazy-initialize, 1269 /// for flexibility. This can be a range, single item, or empty 1270 /// sequence, for example. The iterator should return indices in 1271 /// increasing order, so that the break-at-out-of-bounds behavior 1272 /// works correctly. 1273 pub(crate) fn get_table_with_lazy_init( 1274 &mut self, 1275 table_index: TableIndex, 1276 range: impl Iterator<Item = u64>, 1277 ) -> *mut Table { 1278 self.with_defined_table_index_and_instance(table_index, |idx, instance| { 1279 instance.get_defined_table_with_lazy_init(idx, range) 1280 }) 1281 } 1282 1283 /// Gets the raw runtime table data structure owned by this instance 1284 /// given the provided `idx`. 1285 /// 1286 /// The `range` specified is eagerly initialized for funcref tables. 1287 pub fn get_defined_table_with_lazy_init( 1288 &mut self, 1289 idx: DefinedTableIndex, 1290 range: impl Iterator<Item = u64>, 1291 ) -> *mut Table { 1292 let elt_ty = self.tables[idx].1.element_type(); 1293 1294 if elt_ty == TableElementType::Func { 1295 for i in range { 1296 let value = match self.tables[idx].1.get(None, i) { 1297 Some(value) => value, 1298 None => { 1299 // Out-of-bounds; caller will handle by likely 1300 // throwing a trap. No work to do to lazy-init 1301 // beyond the end. 1302 break; 1303 } 1304 }; 1305 1306 if !value.is_uninit() { 1307 continue; 1308 } 1309 1310 // The table element `i` is uninitialized and is now being 1311 // initialized. This must imply that a `precompiled` list of 1312 // function indices is available for this table. The precompiled 1313 // list is extracted and then it is consulted with `i` to 1314 // determine the function that is going to be initialized. Note 1315 // that `i` may be outside the limits of the static 1316 // initialization so it's a fallible `get` instead of an index. 1317 let module = self.env_module(); 1318 let precomputed = match &module.table_initialization.initial_values[idx] { 1319 TableInitialValue::Null { precomputed } => precomputed, 1320 TableInitialValue::Expr(_) => unreachable!(), 1321 }; 1322 // Panicking here helps catch bugs rather than silently truncating by accident. 1323 let func_index = precomputed.get(usize::try_from(i).unwrap()).cloned(); 1324 let func_ref = func_index.and_then(|func_index| self.get_func_ref(func_index)); 1325 self.tables[idx] 1326 .1 1327 .set(i, TableElement::FuncRef(func_ref)) 1328 .expect("Table type should match and index should be in-bounds"); 1329 } 1330 } 1331 1332 // SAFETY: the `unsafe` here is projecting from `*mut (A, B)` to 1333 // `*mut A`, which should be a safe operation to do. 1334 unsafe { &raw mut (*self.tables.get_raw_mut(idx).unwrap()).1 } 1335 } 1336 1337 /// Get a table by index regardless of whether it is locally-defined or an 1338 /// imported, foreign table. 1339 pub(crate) fn get_table(&mut self, table_index: TableIndex) -> *mut Table { 1340 self.with_defined_table_index_and_instance(table_index, |idx, instance| unsafe { 1341 // SAFETY: the `unsafe` here is projecting from `*mut (A, B)` to 1342 // `*mut A`, which should be a safe operation to do. 1343 &raw mut (*instance.tables.get_raw_mut(idx).unwrap()).1 1344 }) 1345 } 1346 1347 /// Get a locally-defined table. 1348 pub(crate) fn get_defined_table(&mut self, index: DefinedTableIndex) -> *mut Table { 1349 // SAFETY: the `unsafe` here is projecting from `*mut (A, B)` to 1350 // `*mut A`, which should be a safe operation to do. 1351 unsafe { &raw mut (*self.tables.get_raw_mut(index).unwrap()).1 } 1352 } 1353 1354 pub(crate) fn with_defined_table_index_and_instance<R>( 1355 &mut self, 1356 index: TableIndex, 1357 f: impl FnOnce(DefinedTableIndex, &mut Instance) -> R, 1358 ) -> R { 1359 if let Some(defined_table_index) = self.env_module().defined_table_index(index) { 1360 f(defined_table_index, self) 1361 } else { 1362 let import = self.imported_table(index); 1363 unsafe { 1364 Instance::from_vmctx(import.vmctx.as_non_null(), |foreign_instance| { 1365 let foreign_table_def = import.from.as_ptr(); 1366 let foreign_table_index = foreign_instance.table_index(&*foreign_table_def); 1367 f(foreign_table_index, foreign_instance) 1368 }) 1369 } 1370 } 1371 } 1372 1373 /// Initialize the VMContext data associated with this Instance. 1374 /// 1375 /// The `VMContext` memory is assumed to be uninitialized; any field 1376 /// that we need in a certain state will be explicitly written by this 1377 /// function. 1378 unsafe fn initialize_vmctx( 1379 &mut self, 1380 module: &Module, 1381 offsets: &VMOffsets<HostPtr>, 1382 store: StorePtr, 1383 imports: Imports, 1384 ) { 1385 assert!(ptr::eq(module, self.env_module().as_ref())); 1386 1387 self.vmctx_plus_offset_mut(offsets.ptr.vmctx_magic()) 1388 .write(VMCONTEXT_MAGIC); 1389 self.set_callee(None); 1390 self.set_store(store.as_raw()); 1391 1392 // Initialize shared types 1393 let types = NonNull::from(self.runtime_info.type_ids()); 1394 self.type_ids_array().write(types.cast().into()); 1395 1396 // Initialize the built-in functions 1397 static BUILTINS: VMBuiltinFunctionsArray = VMBuiltinFunctionsArray::INIT; 1398 let ptr = BUILTINS.expose_provenance(); 1399 self.vmctx_plus_offset_mut(offsets.ptr.vmctx_builtin_functions()) 1400 .write(VmPtr::from(ptr)); 1401 1402 // Initialize the imports 1403 debug_assert_eq!(imports.functions.len(), module.num_imported_funcs); 1404 ptr::copy_nonoverlapping( 1405 imports.functions.as_ptr(), 1406 self.vmctx_plus_offset_mut(offsets.vmctx_imported_functions_begin()) 1407 .as_ptr(), 1408 imports.functions.len(), 1409 ); 1410 debug_assert_eq!(imports.tables.len(), module.num_imported_tables); 1411 ptr::copy_nonoverlapping( 1412 imports.tables.as_ptr(), 1413 self.vmctx_plus_offset_mut(offsets.vmctx_imported_tables_begin()) 1414 .as_ptr(), 1415 imports.tables.len(), 1416 ); 1417 debug_assert_eq!(imports.memories.len(), module.num_imported_memories); 1418 ptr::copy_nonoverlapping( 1419 imports.memories.as_ptr(), 1420 self.vmctx_plus_offset_mut(offsets.vmctx_imported_memories_begin()) 1421 .as_ptr(), 1422 imports.memories.len(), 1423 ); 1424 debug_assert_eq!(imports.globals.len(), module.num_imported_globals); 1425 ptr::copy_nonoverlapping( 1426 imports.globals.as_ptr(), 1427 self.vmctx_plus_offset_mut(offsets.vmctx_imported_globals_begin()) 1428 .as_ptr(), 1429 imports.globals.len(), 1430 ); 1431 1432 debug_assert_eq!(imports.tags.len(), module.num_imported_tags); 1433 ptr::copy_nonoverlapping( 1434 imports.tags.as_ptr(), 1435 self.vmctx_plus_offset_mut(offsets.vmctx_imported_tags_begin()) 1436 .as_ptr(), 1437 imports.tags.len(), 1438 ); 1439 1440 // N.B.: there is no need to initialize the funcrefs array because we 1441 // eagerly construct each element in it whenever asked for a reference 1442 // to that element. In other words, there is no state needed to track 1443 // the lazy-init, so we don't need to initialize any state now. 1444 1445 // Initialize the defined tables 1446 let mut ptr = self.vmctx_plus_offset_mut(offsets.vmctx_tables_begin()); 1447 for i in 0..module.num_defined_tables() { 1448 ptr.write(self.tables[DefinedTableIndex::new(i)].1.vmtable()); 1449 ptr = ptr.add(1); 1450 } 1451 1452 // Initialize the defined memories. This fills in both the 1453 // `defined_memories` table and the `owned_memories` table at the same 1454 // time. Entries in `defined_memories` hold a pointer to a definition 1455 // (all memories) whereas the `owned_memories` hold the actual 1456 // definitions of memories owned (not shared) in the module. 1457 let mut ptr = self.vmctx_plus_offset_mut(offsets.vmctx_memories_begin()); 1458 let mut owned_ptr = self.vmctx_plus_offset_mut(offsets.vmctx_owned_memories_begin()); 1459 for i in 0..module.num_defined_memories() { 1460 let defined_memory_index = DefinedMemoryIndex::new(i); 1461 let memory_index = module.memory_index(defined_memory_index); 1462 if module.memories[memory_index].shared { 1463 let def_ptr = self.memories[defined_memory_index] 1464 .1 1465 .as_shared_memory() 1466 .unwrap() 1467 .vmmemory_ptr(); 1468 ptr.write(VmPtr::from(def_ptr)); 1469 } else { 1470 owned_ptr.write(self.memories[defined_memory_index].1.vmmemory()); 1471 ptr.write(VmPtr::from(owned_ptr)); 1472 owned_ptr = owned_ptr.add(1); 1473 } 1474 ptr = ptr.add(1); 1475 } 1476 1477 // Zero-initialize the globals so that nothing is uninitialized memory 1478 // after this function returns. The globals are actually initialized 1479 // with their const expression initializers after the instance is fully 1480 // allocated. 1481 for (index, _init) in module.global_initializers.iter() { 1482 self.global_ptr(index).write(VMGlobalDefinition::new()); 1483 } 1484 1485 // Initialize the defined tags 1486 let mut ptr = self.vmctx_plus_offset_mut(offsets.vmctx_tags_begin()); 1487 for i in 0..module.num_defined_tags() { 1488 let defined_index = DefinedTagIndex::new(i); 1489 let tag_index = module.tag_index(defined_index); 1490 let tag = module.tags[tag_index]; 1491 ptr.write(VMTagDefinition::new( 1492 tag.signature.unwrap_engine_type_index(), 1493 )); 1494 ptr = ptr.add(1); 1495 } 1496 } 1497 1498 fn wasm_fault(&self, addr: usize) -> Option<WasmFault> { 1499 let mut fault = None; 1500 for (_, (_, memory)) in self.memories.iter() { 1501 let accessible = memory.wasm_accessible(); 1502 if accessible.start <= addr && addr < accessible.end { 1503 // All linear memories should be disjoint so assert that no 1504 // prior fault has been found. 1505 assert!(fault.is_none()); 1506 fault = Some(WasmFault { 1507 memory_size: memory.byte_size(), 1508 wasm_address: u64::try_from(addr - accessible.start).unwrap(), 1509 }); 1510 } 1511 } 1512 fault 1513 } 1514 } 1515 1516 /// A handle holding an `Instance` of a WebAssembly module. 1517 #[derive(Debug)] 1518 pub struct InstanceHandle { 1519 instance: Option<SendSyncPtr<Instance>>, 1520 } 1521 1522 impl InstanceHandle { 1523 /// Creates an "empty" instance handle which internally has a null pointer 1524 /// to an instance. 1525 pub fn null() -> InstanceHandle { 1526 InstanceHandle { instance: None } 1527 } 1528 1529 /// Return a raw pointer to the vmctx used by compiled wasm code. 1530 #[inline] 1531 pub fn vmctx(&self) -> NonNull<VMContext> { 1532 self.instance().vmctx() 1533 } 1534 1535 /// Return a reference to a module. 1536 pub fn module(&self) -> &Arc<Module> { 1537 self.instance().env_module() 1538 } 1539 1540 /// Lookup a function by index. 1541 pub fn get_exported_func(&mut self, export: FuncIndex) -> ExportFunction { 1542 self.instance_mut().get_exported_func(export) 1543 } 1544 1545 /// Lookup a global by index. 1546 pub fn get_exported_global(&mut self, export: GlobalIndex) -> ExportGlobal { 1547 self.instance_mut().get_exported_global(export) 1548 } 1549 1550 /// Lookup a tag by index. 1551 pub fn get_exported_tag(&mut self, export: TagIndex) -> ExportTag { 1552 self.instance_mut().get_exported_tag(export) 1553 } 1554 1555 /// Lookup a memory by index. 1556 pub fn get_exported_memory(&mut self, export: MemoryIndex) -> ExportMemory { 1557 self.instance_mut().get_exported_memory(export) 1558 } 1559 1560 /// Lookup a table by index. 1561 pub fn get_exported_table(&mut self, export: TableIndex) -> ExportTable { 1562 self.instance_mut().get_exported_table(export) 1563 } 1564 1565 /// Lookup an item with the given index. 1566 pub fn get_export_by_index(&mut self, export: EntityIndex) -> Export { 1567 match export { 1568 EntityIndex::Function(i) => Export::Function(self.get_exported_func(i)), 1569 EntityIndex::Global(i) => Export::Global(self.get_exported_global(i)), 1570 EntityIndex::Table(i) => Export::Table(self.get_exported_table(i)), 1571 EntityIndex::Memory(i) => Export::Memory(self.get_exported_memory(i)), 1572 EntityIndex::Tag(i) => Export::Tag(self.get_exported_tag(i)), 1573 } 1574 } 1575 1576 /// Return an iterator over the exports of this instance. 1577 /// 1578 /// Specifically, it provides access to the key-value pairs, where the keys 1579 /// are export names, and the values are export declarations which can be 1580 /// resolved `lookup_by_declaration`. 1581 pub fn exports(&self) -> wasmparser::collections::index_map::Iter<String, EntityIndex> { 1582 self.instance().exports() 1583 } 1584 1585 /// Return a reference to the custom state attached to this instance. 1586 pub fn host_state(&self) -> &dyn Any { 1587 self.instance().host_state() 1588 } 1589 1590 /// Get a table defined locally within this module. 1591 pub fn get_defined_table(&mut self, index: DefinedTableIndex) -> *mut Table { 1592 self.instance_mut().get_defined_table(index) 1593 } 1594 1595 /// Get a table defined locally within this module, lazily 1596 /// initializing the given range first. 1597 pub fn get_defined_table_with_lazy_init( 1598 &mut self, 1599 index: DefinedTableIndex, 1600 range: impl Iterator<Item = u64>, 1601 ) -> *mut Table { 1602 let index = self.instance().env_module().table_index(index); 1603 self.instance_mut().get_table_with_lazy_init(index, range) 1604 } 1605 1606 /// Get all tables within this instance. 1607 /// 1608 /// Returns both import and defined tables. 1609 /// 1610 /// Returns both exported and non-exported tables. 1611 /// 1612 /// Gives access to the full tables space. 1613 pub fn all_tables<'a>( 1614 &'a mut self, 1615 ) -> impl ExactSizeIterator<Item = (TableIndex, ExportTable)> + 'a { 1616 let indices = (0..self.module().tables.len()) 1617 .map(|i| TableIndex::new(i)) 1618 .collect::<Vec<_>>(); 1619 indices.into_iter().map(|i| (i, self.get_exported_table(i))) 1620 } 1621 1622 /// Return the tables defined in this instance (not imported). 1623 pub fn defined_tables<'a>(&'a mut self) -> impl ExactSizeIterator<Item = ExportTable> + 'a { 1624 let num_imported = self.module().num_imported_tables; 1625 self.all_tables() 1626 .skip(num_imported) 1627 .map(|(_i, table)| table) 1628 } 1629 1630 /// Get all memories within this instance. 1631 /// 1632 /// Returns both import and defined memories. 1633 /// 1634 /// Returns both exported and non-exported memories. 1635 /// 1636 /// Gives access to the full memories space. 1637 pub fn all_memories<'a>( 1638 &'a mut self, 1639 ) -> impl ExactSizeIterator<Item = (MemoryIndex, ExportMemory)> + 'a { 1640 let indices = (0..self.module().memories.len()) 1641 .map(|i| MemoryIndex::new(i)) 1642 .collect::<Vec<_>>(); 1643 indices 1644 .into_iter() 1645 .map(|i| (i, self.get_exported_memory(i))) 1646 } 1647 1648 /// Return the memories defined in this instance (not imported). 1649 pub fn defined_memories<'a>(&'a mut self) -> impl ExactSizeIterator<Item = ExportMemory> + 'a { 1650 let num_imported = self.module().num_imported_memories; 1651 self.all_memories() 1652 .skip(num_imported) 1653 .map(|(_i, memory)| memory) 1654 } 1655 1656 /// Get all globals within this instance. 1657 /// 1658 /// Returns both import and defined globals. 1659 /// 1660 /// Returns both exported and non-exported globals. 1661 /// 1662 /// Gives access to the full globals space. 1663 pub fn all_globals<'a>( 1664 &'a mut self, 1665 ) -> impl ExactSizeIterator<Item = (GlobalIndex, ExportGlobal)> + 'a { 1666 self.instance_mut().all_globals() 1667 } 1668 1669 /// Get the globals defined in this instance (not imported). 1670 pub fn defined_globals<'a>( 1671 &'a mut self, 1672 ) -> impl ExactSizeIterator<Item = (DefinedGlobalIndex, ExportGlobal)> + 'a { 1673 self.instance_mut().defined_globals() 1674 } 1675 1676 /// Return a reference to the contained `Instance`. 1677 #[inline] 1678 pub(crate) fn instance(&self) -> &Instance { 1679 unsafe { &*self.instance.unwrap().as_ptr() } 1680 } 1681 1682 pub(crate) fn instance_mut(&mut self) -> &mut Instance { 1683 unsafe { &mut *self.instance.unwrap().as_ptr() } 1684 } 1685 1686 /// Get this instance's `dyn VMStore` trait object. 1687 /// 1688 /// This should only be used for initializing a vmctx's store pointer. It 1689 /// should never be used to access the store itself. Use `InstanceAndStore` 1690 /// for that instead. 1691 pub fn traitobj(&self, store: &StoreOpaque) -> NonNull<dyn VMStore> { 1692 // By requiring a store argument, we are ensuring that callers aren't 1693 // getting this trait object in order to access the store, since they 1694 // already have access. See `InstanceAndStore` and its documentation for 1695 // details about the store access patterns we want to restrict host code 1696 // to. 1697 let _ = store; 1698 1699 self.instance().store.unwrap().0 1700 } 1701 1702 /// Configure the `*mut dyn Store` internal pointer after-the-fact. 1703 /// 1704 /// This is provided for the original `Store` itself to configure the first 1705 /// self-pointer after the original `Box` has been initialized. 1706 pub unsafe fn set_store(&mut self, store: NonNull<dyn VMStore>) { 1707 self.instance_mut().set_store(Some(store)); 1708 } 1709 1710 /// Returns a clone of this instance. 1711 /// 1712 /// This is unsafe because the returned handle here is just a cheap clone 1713 /// of the internals, there's no lifetime tracking around its validity. 1714 /// You'll need to ensure that the returned handles all go out of scope at 1715 /// the same time. 1716 #[inline] 1717 pub unsafe fn clone(&self) -> InstanceHandle { 1718 InstanceHandle { 1719 instance: self.instance, 1720 } 1721 } 1722 1723 /// Performs post-initialization of an instance after its handle has been 1724 /// created and registered with a store. 1725 /// 1726 /// Failure of this function means that the instance still must persist 1727 /// within the store since failure may indicate partial failure, or some 1728 /// state could be referenced by other instances. 1729 pub fn initialize( 1730 &mut self, 1731 store: &mut StoreOpaque, 1732 module: &Module, 1733 is_bulk_memory: bool, 1734 ) -> Result<()> { 1735 allocator::initialize_instance(store, self.instance_mut(), module, is_bulk_memory) 1736 } 1737 1738 /// Attempts to convert from the host `addr` specified to a WebAssembly 1739 /// based address recorded in `WasmFault`. 1740 /// 1741 /// This method will check all linear memories that this instance contains 1742 /// to see if any of them contain `addr`. If one does then `Some` is 1743 /// returned with metadata about the wasm fault. Otherwise `None` is 1744 /// returned and `addr` doesn't belong to this instance. 1745 pub fn wasm_fault(&self, addr: usize) -> Option<WasmFault> { 1746 self.instance().wasm_fault(addr) 1747 } 1748 } 1749