1 use crate::component::matching::InstanceType; 2 use crate::component::resources::{HostResourceData, HostResourceIndex, HostResourceTables}; 3 use crate::component::{Instance, ResourceType}; 4 use crate::prelude::*; 5 use crate::runtime::vm::component::{ 6 CallContexts, ComponentInstance, InstanceFlags, ResourceTable, ResourceTables, 7 }; 8 use crate::runtime::vm::{VMFuncRef, VMMemoryDefinition}; 9 use crate::store::{StoreId, StoreOpaque}; 10 use crate::{FuncType, StoreContextMut}; 11 use alloc::sync::Arc; 12 use core::pin::Pin; 13 use core::ptr::NonNull; 14 use wasmtime_environ::component::{ 15 CanonicalOptions, CanonicalOptionsDataModel, ComponentTypes, OptionsIndex, StringEncoding, 16 TypeResourceTableIndex, 17 }; 18 19 /// Runtime representation of canonical ABI options in the component model. 20 /// 21 /// This structure packages up the runtime representation of each option from 22 /// memories to reallocs to string encodings. Note that this is a "standalone" 23 /// structure which has raw pointers internally. This allows it to be created 24 /// out of thin air for a host function import, for example. The `store_id` 25 /// field, however, is what is used to pair this set of options with a store 26 /// reference to actually use the pointers. 27 #[derive(Copy, Clone)] 28 pub struct Options { 29 /// The store from which this options originated. 30 store_id: StoreId, 31 32 /// An optional pointer for the memory that this set of options is referring 33 /// to. This option is not required to be specified in the canonical ABI 34 /// hence the `Option`. 35 /// 36 /// Note that this pointer cannot be safely dereferenced unless a store, 37 /// verified with `self.store_id`, has the appropriate borrow available. 38 memory: Option<NonNull<VMMemoryDefinition>>, 39 40 /// Similar to `memory` but corresponds to the `canonical_abi_realloc` 41 /// function. 42 /// 43 /// Safely using this pointer has the same restrictions as `memory` above. 44 realloc: Option<NonNull<VMFuncRef>>, 45 46 /// The encoding used for strings, if found. 47 /// 48 /// This defaults to utf-8 but can be changed if necessary. 49 string_encoding: StringEncoding, 50 51 /// Whether or not this the async option was set when lowering. 52 async_: bool, 53 54 #[cfg(feature = "component-model-async")] 55 callback: Option<NonNull<VMFuncRef>>, 56 } 57 58 // The `Options` structure stores raw pointers but they're never used unless a 59 // `Store` is available so this should be threadsafe and largely inherit the 60 // thread-safety story of `Store<T>` itself. 61 unsafe impl Send for Options {} 62 unsafe impl Sync for Options {} 63 64 impl Options { 65 // FIXME(#4311): prevent a ctor where the memory is memory64 66 67 /// Creates a new [`Options`] from the given [`OptionsIndex`] belonging to 68 /// the specified [`Instance`] 69 /// 70 /// # Panics 71 /// 72 /// Panics if `instance` is not owned by `store` or if `index` is not valid 73 /// for `instance`'s component. 74 pub fn new_index(store: &StoreOpaque, instance: Instance, index: OptionsIndex) -> Options { 75 let instance = instance.id().get(store); 76 let CanonicalOptions { 77 string_encoding, 78 async_, 79 callback, 80 ref data_model, 81 .. 82 } = instance.component().env_component().options[index]; 83 let (memory, realloc) = match data_model { 84 CanonicalOptionsDataModel::Gc { .. } => (None, None), 85 CanonicalOptionsDataModel::LinearMemory(o) => (o.memory, o.realloc), 86 }; 87 let memory = memory.map(|i| NonNull::new(instance.runtime_memory(i)).unwrap()); 88 let realloc = realloc.map(|i| instance.runtime_realloc(i)); 89 let callback = callback.map(|i| instance.runtime_callback(i)); 90 let _ = callback; 91 92 Options { 93 store_id: store.id(), 94 memory, 95 realloc, 96 string_encoding, 97 async_, 98 #[cfg(feature = "component-model-async")] 99 callback, 100 } 101 } 102 103 fn realloc<'a, T>( 104 &self, 105 store: &'a mut StoreContextMut<'_, T>, 106 realloc_ty: &FuncType, 107 old: usize, 108 old_size: usize, 109 old_align: u32, 110 new_size: usize, 111 ) -> Result<(&'a mut [u8], usize)> { 112 self.store_id.assert_belongs_to(store.0.id()); 113 114 let realloc = self.realloc.unwrap(); 115 116 let params = ( 117 u32::try_from(old)?, 118 u32::try_from(old_size)?, 119 old_align, 120 u32::try_from(new_size)?, 121 ); 122 123 type ReallocFunc = crate::TypedFunc<(u32, u32, u32, u32), u32>; 124 125 // Invoke the wasm malloc function using its raw and statically known 126 // signature. 127 let result = unsafe { ReallocFunc::call_raw(store, realloc_ty, realloc, params)? }; 128 129 if result % old_align != 0 { 130 bail!("realloc return: result not aligned"); 131 } 132 let result = usize::try_from(result)?; 133 134 let memory = self.memory_mut(store.0); 135 136 let result_slice = match memory.get_mut(result..).and_then(|s| s.get_mut(..new_size)) { 137 Some(end) => end, 138 None => bail!("realloc return: beyond end of memory"), 139 }; 140 141 Ok((result_slice, result)) 142 } 143 144 /// Asserts that this function has an associated memory attached to it and 145 /// then returns the slice of memory tied to the lifetime of the provided 146 /// store. 147 pub fn memory<'a>(&self, store: &'a StoreOpaque) -> &'a [u8] { 148 self.store_id.assert_belongs_to(store.id()); 149 150 // The unsafety here is intended to be encapsulated by the two 151 // preceding assertions. Namely we assert that the `store` is the same 152 // as the original store of this `Options`, meaning that we safely have 153 // either a shared reference or a mutable reference (as below) which 154 // means it's safe to view the memory (aka it's not a different store 155 // where our original store is on some other thread or something like 156 // that). 157 // 158 // Additionally the memory itself is asserted to be present as memory 159 // is an optional configuration in canonical ABI options. 160 unsafe { 161 let memory = self.memory.unwrap().as_ref(); 162 core::slice::from_raw_parts(memory.base.as_ptr(), memory.current_length()) 163 } 164 } 165 166 /// Same as above, just `_mut` 167 pub fn memory_mut<'a>(&self, store: &'a mut StoreOpaque) -> &'a mut [u8] { 168 self.store_id.assert_belongs_to(store.id()); 169 170 // See comments in `memory` about the unsafety 171 unsafe { 172 let memory = self.memory.unwrap().as_ref(); 173 core::slice::from_raw_parts_mut(memory.base.as_ptr(), memory.current_length()) 174 } 175 } 176 177 /// Returns the underlying encoding used for strings in this 178 /// lifting/lowering. 179 pub fn string_encoding(&self) -> StringEncoding { 180 self.string_encoding 181 } 182 183 /// Returns the id of the store that this `Options` is connected to. 184 pub fn store_id(&self) -> StoreId { 185 self.store_id 186 } 187 188 /// Returns whether this lifting or lowering uses the async ABI. 189 pub fn async_(&self) -> bool { 190 self.async_ 191 } 192 193 #[cfg(feature = "component-model-async")] 194 pub(crate) fn callback(&self) -> Option<NonNull<VMFuncRef>> { 195 self.callback 196 } 197 198 #[cfg(feature = "component-model-async")] 199 pub(crate) fn memory_raw(&self) -> Option<NonNull<VMMemoryDefinition>> { 200 self.memory 201 } 202 } 203 204 /// A helper structure which is a "package" of the context used during lowering 205 /// values into a component (or storing them into memory). 206 /// 207 /// This type is used by the `Lower` trait extensively and contains any 208 /// contextual information necessary related to the context in which the 209 /// lowering is happening. 210 #[doc(hidden)] 211 pub struct LowerContext<'a, T: 'static> { 212 /// Lowering may involve invoking memory allocation functions so part of the 213 /// context here is carrying access to the entire store that wasm is 214 /// executing within. This store serves as proof-of-ability to actually 215 /// execute wasm safely. 216 pub store: StoreContextMut<'a, T>, 217 218 /// Lowering always happens into a function that's been `canon lift`'d or 219 /// `canon lower`'d, both of which specify a set of options for the 220 /// canonical ABI. For example details like string encoding are contained 221 /// here along with which memory pointers are relative to or what the memory 222 /// allocation function is. 223 pub options: &'a Options, 224 225 /// Lowering happens within the context of a component instance and this 226 /// field stores the type information of that component instance. This is 227 /// used for type lookups and general type queries during the 228 /// lifting/lowering process. 229 pub types: &'a ComponentTypes, 230 231 /// Index of the component instance that's being lowered into. 232 instance: Instance, 233 } 234 235 #[doc(hidden)] 236 impl<'a, T: 'static> LowerContext<'a, T> { 237 /// Creates a new lowering context from the specified parameters. 238 pub fn new( 239 store: StoreContextMut<'a, T>, 240 options: &'a Options, 241 types: &'a ComponentTypes, 242 instance: Instance, 243 ) -> LowerContext<'a, T> { 244 LowerContext { 245 store, 246 options, 247 types, 248 instance, 249 } 250 } 251 252 /// Returns the `&ComponentInstance` that's being lowered into. 253 pub fn instance(&self) -> &ComponentInstance { 254 self.instance.id().get(self.store.0) 255 } 256 257 /// Returns the `&mut ComponentInstance` that's being lowered into. 258 pub fn instance_mut(&mut self) -> Pin<&mut ComponentInstance> { 259 self.instance.id().get_mut(self.store.0) 260 } 261 262 /// Returns a view into memory as a mutable slice of bytes. 263 /// 264 /// # Panics 265 /// 266 /// This will panic if memory has not been configured for this lowering 267 /// (e.g. it wasn't present during the specification of canonical options). 268 pub fn as_slice_mut(&mut self) -> &mut [u8] { 269 self.options.memory_mut(self.store.0) 270 } 271 272 /// Invokes the memory allocation function (which is style after `realloc`) 273 /// with the specified parameters. 274 /// 275 /// # Panics 276 /// 277 /// This will panic if realloc hasn't been configured for this lowering via 278 /// its canonical options. 279 pub fn realloc( 280 &mut self, 281 old: usize, 282 old_size: usize, 283 old_align: u32, 284 new_size: usize, 285 ) -> Result<usize> { 286 let realloc_func_ty = Arc::clone(self.instance().component().realloc_func_ty()); 287 self.options 288 .realloc( 289 &mut self.store, 290 &realloc_func_ty, 291 old, 292 old_size, 293 old_align, 294 new_size, 295 ) 296 .map(|(_, ptr)| ptr) 297 } 298 299 /// Returns a fixed mutable slice of memory `N` bytes large starting at 300 /// offset `N`, panicking on out-of-bounds. 301 /// 302 /// It should be previously verified that `offset` is in-bounds via 303 /// bounds-checks. 304 /// 305 /// # Panics 306 /// 307 /// This will panic if memory has not been configured for this lowering 308 /// (e.g. it wasn't present during the specification of canonical options). 309 pub fn get<const N: usize>(&mut self, offset: usize) -> &mut [u8; N] { 310 // FIXME: this bounds check shouldn't actually be necessary, all 311 // callers of `ComponentType::store` have already performed a bounds 312 // check so we're guaranteed that `offset..offset+N` is in-bounds. That 313 // being said we at least should do bounds checks in debug mode and 314 // it's not clear to me how to easily structure this so that it's 315 // "statically obvious" the bounds check isn't necessary. 316 // 317 // For now I figure we can leave in this bounds check and if it becomes 318 // an issue we can optimize further later, probably with judicious use 319 // of `unsafe`. 320 self.as_slice_mut()[offset..].first_chunk_mut().unwrap() 321 } 322 323 /// Lowers an `own` resource into the guest, converting the `rep` specified 324 /// into a guest-local index. 325 /// 326 /// The `ty` provided is which table to put this into. 327 pub fn guest_resource_lower_own( 328 &mut self, 329 ty: TypeResourceTableIndex, 330 rep: u32, 331 ) -> Result<u32> { 332 self.resource_tables().guest_resource_lower_own(rep, ty) 333 } 334 335 /// Lowers a `borrow` resource into the guest, converting the `rep` to a 336 /// guest-local index in the `ty` table specified. 337 pub fn guest_resource_lower_borrow( 338 &mut self, 339 ty: TypeResourceTableIndex, 340 rep: u32, 341 ) -> Result<u32> { 342 // Implement `lower_borrow`'s special case here where if a borrow is 343 // inserted into a table owned by the instance which implemented the 344 // original resource then no borrow tracking is employed and instead the 345 // `rep` is returned "raw". 346 // 347 // This check is performed by comparing the owning instance of `ty` 348 // against the owning instance of the resource that `ty` is working 349 // with. 350 if self.instance().resource_owned_by_own_instance(ty) { 351 return Ok(rep); 352 } 353 self.resource_tables().guest_resource_lower_borrow(rep, ty) 354 } 355 356 /// Lifts a host-owned `own` resource at the `idx` specified into the 357 /// representation of that resource. 358 pub fn host_resource_lift_own(&mut self, idx: HostResourceIndex) -> Result<u32> { 359 self.resource_tables().host_resource_lift_own(idx) 360 } 361 362 /// Lifts a host-owned `borrow` resource at the `idx` specified into the 363 /// representation of that resource. 364 pub fn host_resource_lift_borrow(&mut self, idx: HostResourceIndex) -> Result<u32> { 365 self.resource_tables().host_resource_lift_borrow(idx) 366 } 367 368 /// Lowers a resource into the host-owned table, returning the index it was 369 /// inserted at. 370 /// 371 /// Note that this is a special case for `Resource<T>`. Most of the time a 372 /// host value shouldn't be lowered with a lowering context. 373 pub fn host_resource_lower_own( 374 &mut self, 375 rep: u32, 376 dtor: Option<NonNull<VMFuncRef>>, 377 flags: Option<InstanceFlags>, 378 ) -> Result<HostResourceIndex> { 379 self.resource_tables() 380 .host_resource_lower_own(rep, dtor, flags) 381 } 382 383 /// Returns the underlying resource type for the `ty` table specified. 384 pub fn resource_type(&self, ty: TypeResourceTableIndex) -> ResourceType { 385 self.instance_type().resource_type(ty) 386 } 387 388 /// Returns the instance type information corresponding to the instance that 389 /// this context is lowering into. 390 pub fn instance_type(&self) -> InstanceType<'_> { 391 InstanceType::new(self.instance()) 392 } 393 394 fn resource_tables(&mut self) -> HostResourceTables<'_> { 395 let (calls, host_table, host_resource_data, instance) = self 396 .store 397 .0 398 .component_resource_state_with_instance(self.instance); 399 HostResourceTables::from_parts( 400 ResourceTables { 401 host_table: Some(host_table), 402 calls, 403 guest: Some(instance.guest_tables()), 404 }, 405 host_resource_data, 406 ) 407 } 408 409 /// See [`HostResourceTables::enter_call`]. 410 #[inline] 411 pub fn enter_call(&mut self) { 412 self.resource_tables().enter_call() 413 } 414 415 /// See [`HostResourceTables::exit_call`]. 416 #[inline] 417 pub fn exit_call(&mut self) -> Result<()> { 418 self.resource_tables().exit_call() 419 } 420 } 421 422 /// Contextual information used when lifting a type from a component into the 423 /// host. 424 /// 425 /// This structure is the analogue of `LowerContext` except used during lifting 426 /// operations (or loading from memory). 427 #[doc(hidden)] 428 pub struct LiftContext<'a> { 429 /// Like lowering, lifting always has options configured. 430 pub options: &'a Options, 431 432 /// Instance type information, like with lowering. 433 pub types: &'a Arc<ComponentTypes>, 434 435 memory: Option<&'a [u8]>, 436 437 instance: Pin<&'a mut ComponentInstance>, 438 instance_handle: Instance, 439 440 host_table: &'a mut ResourceTable, 441 host_resource_data: &'a mut HostResourceData, 442 443 calls: &'a mut CallContexts, 444 } 445 446 #[doc(hidden)] 447 impl<'a> LiftContext<'a> { 448 /// Creates a new lifting context given the provided context. 449 #[inline] 450 pub fn new( 451 store: &'a mut StoreOpaque, 452 options: &'a Options, 453 instance_handle: Instance, 454 ) -> LiftContext<'a> { 455 // From `&mut StoreOpaque` provided the goal here is to project out 456 // three different disjoint fields owned by the store: memory, 457 // `CallContexts`, and `ResourceTable`. There's no native API for that 458 // so it's hacked around a bit. This unsafe pointer cast could be fixed 459 // with more methods in more places, but it doesn't seem worth doing it 460 // at this time. 461 let memory = options 462 .memory 463 .map(|_| options.memory(unsafe { &*(store as *const StoreOpaque) })); 464 let (calls, host_table, host_resource_data, instance) = 465 store.component_resource_state_with_instance(instance_handle); 466 let (component, instance) = instance.component_and_self(); 467 468 LiftContext { 469 memory, 470 options, 471 types: component.types(), 472 instance, 473 instance_handle, 474 calls, 475 host_table, 476 host_resource_data, 477 } 478 } 479 480 /// Returns the entire contents of linear memory for this set of lifting 481 /// options. 482 /// 483 /// # Panics 484 /// 485 /// This will panic if memory has not been configured for this lifting 486 /// operation. 487 pub fn memory(&self) -> &'a [u8] { 488 self.memory.unwrap() 489 } 490 491 /// Returns an identifier for the store from which this `LiftContext` was 492 /// created. 493 pub fn store_id(&self) -> StoreId { 494 self.options.store_id 495 } 496 497 /// Returns the component instance that is being lifted from. 498 pub fn instance_mut(&mut self) -> Pin<&mut ComponentInstance> { 499 self.instance.as_mut() 500 } 501 /// Returns the component instance that is being lifted from. 502 pub fn instance_handle(&self) -> Instance { 503 self.instance_handle 504 } 505 506 /// Lifts an `own` resource from the guest at the `idx` specified into its 507 /// representation. 508 /// 509 /// Additionally returns a destructor/instance flags to go along with the 510 /// representation so the host knows how to destroy this resource. 511 pub fn guest_resource_lift_own( 512 &mut self, 513 ty: TypeResourceTableIndex, 514 idx: u32, 515 ) -> Result<(u32, Option<NonNull<VMFuncRef>>, Option<InstanceFlags>)> { 516 let idx = self.resource_tables().guest_resource_lift_own(idx, ty)?; 517 let (dtor, flags) = self.instance.dtor_and_flags(ty); 518 Ok((idx, dtor, flags)) 519 } 520 521 /// Lifts a `borrow` resource from the guest at the `idx` specified. 522 pub fn guest_resource_lift_borrow( 523 &mut self, 524 ty: TypeResourceTableIndex, 525 idx: u32, 526 ) -> Result<u32> { 527 self.resource_tables().guest_resource_lift_borrow(idx, ty) 528 } 529 530 /// Lowers a resource into the host-owned table, returning the index it was 531 /// inserted at. 532 pub fn host_resource_lower_own( 533 &mut self, 534 rep: u32, 535 dtor: Option<NonNull<VMFuncRef>>, 536 flags: Option<InstanceFlags>, 537 ) -> Result<HostResourceIndex> { 538 self.resource_tables() 539 .host_resource_lower_own(rep, dtor, flags) 540 } 541 542 /// Lowers a resource into the host-owned table, returning the index it was 543 /// inserted at. 544 pub fn host_resource_lower_borrow(&mut self, rep: u32) -> Result<HostResourceIndex> { 545 self.resource_tables().host_resource_lower_borrow(rep) 546 } 547 548 /// Returns the underlying type of the resource table specified by `ty`. 549 pub fn resource_type(&self, ty: TypeResourceTableIndex) -> ResourceType { 550 self.instance_type().resource_type(ty) 551 } 552 553 /// Returns instance type information for the component instance that is 554 /// being lifted from. 555 pub fn instance_type(&self) -> InstanceType<'_> { 556 InstanceType::new(&self.instance) 557 } 558 559 fn resource_tables(&mut self) -> HostResourceTables<'_> { 560 HostResourceTables::from_parts( 561 ResourceTables { 562 host_table: Some(self.host_table), 563 calls: self.calls, 564 // Note that the unsafety here should be valid given the contract of 565 // `LiftContext::new`. 566 guest: Some(self.instance.as_mut().guest_tables()), 567 }, 568 self.host_resource_data, 569 ) 570 } 571 572 /// See [`HostResourceTables::enter_call`]. 573 #[inline] 574 pub fn enter_call(&mut self) { 575 self.resource_tables().enter_call() 576 } 577 578 /// See [`HostResourceTables::exit_call`]. 579 #[inline] 580 pub fn exit_call(&mut self) -> Result<()> { 581 self.resource_tables().exit_call() 582 } 583 } 584