A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Parameterize `transfer-borrow-to-owner` on its source table (#15190)
Nick Fitzgeralde3995b803818
diff --git a/crates/cranelift/src/compiler/component.rs b/crates/cranelift/src/compiler/component.rsindex 7d52c21b5a..22839b5af7 100644--- a/crates/cranelift/src/compiler/component.rs+++ b/crates/cranelift/src/compiler/component.rs@@ -616,12 +616,14 @@ impl<'a> TrampolineCompiler<'a> {|_, _| {},);}- Trampoline::ResourceTransferBorrowToOwner => {+ Trampoline::ResourceTransferBorrowToOwner { ty } => {self.translate_libcall(host::resource_transfer_borrow_to_owner,TrapSentinel::NegativeOne,WasmArgs::InRegisters,- |_, _| {},+ |me, params| {+ params.push(me.index_value(*ty));+ },);}Trampoline::PrepareCall { memory } => {@@ -1436,7 +1438,7 @@ impl<'a> TrampolineCompiler<'a> {// directly from guest wasm, so no check is needed.Trampoline::ResourceTransferOwn| Trampoline::ResourceTransferBorrow- | Trampoline::ResourceTransferBorrowToOwner+ | Trampoline::ResourceTransferBorrowToOwner { .. }| Trampoline::PrepareCall { .. }| Trampoline::StartCall { .. }| Trampoline::FutureTransferdiff --git a/crates/environ/src/component.rs b/crates/environ/src/component.rsindex 190b620d31..bdc963e326 100644--- a/crates/environ/src/component.rs+++ b/crates/environ/src/component.rs@@ -113,7 +113,7 @@ macro_rules! foreach_builtin_component_function {// Same as `resource_transfer_borrow` but for when the destination// instance owns the resource, in which case it receives the// representation directly and no destination table is involved.- resource_transfer_borrow_to_owner(vmctx: vmctx, src_idx: u32, src_table: u32) -> u64;+ resource_transfer_borrow_to_owner(vmctx: vmctx, src_table: u32, src_idx: u32) -> u64;enter_sync_call(vmctx: vmctx, callee_async: u32, callee_instance: u32) -> bool;exit_sync_call(vmctx: vmctx) -> bool;diff --git a/crates/environ/src/component/dfg.rs b/crates/environ/src/component/dfg.rsindex a5f2d1360d..27222f0ae0 100644--- a/crates/environ/src/component/dfg.rs+++ b/crates/environ/src/component/dfg.rs@@ -470,7 +470,9 @@ pub enum Trampoline {},ResourceTransferOwn,ResourceTransferBorrow,- ResourceTransferBorrowToOwner,+ ResourceTransferBorrowToOwner {+ ty: TypeResourceTableIndex,+ },PrepareCall {memory: Option<MemoryId>,},@@ -1188,8 +1190,8 @@ impl LinearizeDfg<'_> {},Trampoline::ResourceTransferOwn => info::Trampoline::ResourceTransferOwn,Trampoline::ResourceTransferBorrow => info::Trampoline::ResourceTransferBorrow,- Trampoline::ResourceTransferBorrowToOwner => {- info::Trampoline::ResourceTransferBorrowToOwner+ Trampoline::ResourceTransferBorrowToOwner { ty } => {+ info::Trampoline::ResourceTransferBorrowToOwner { ty: *ty }}Trampoline::PrepareCall { memory } => info::Trampoline::PrepareCall {memory: memory.map(|v| self.runtime_memory(v)),diff --git a/crates/environ/src/component/info.rs b/crates/environ/src/component/info.rsindex d64f869cda..6ec6ca01bc 100644--- a/crates/environ/src/component/info.rs+++ b/crates/environ/src/component/info.rs@@ -1077,7 +1077,10 @@ pub enum Trampoline {/// Same as `ResourceTransferBorrow` but for a destination instance which/// owns the resource and so receives its representation directly, meaning/// no destination table is consulted.- ResourceTransferBorrowToOwner,+ ResourceTransferBorrowToOwner {+ /// The resource table the borrow is transferred out of.+ ty: TypeResourceTableIndex,+ },/// An intrinsic used by FACT-generated modules to prepare a call involving/// an async-lowered import and/or an async-lifted export.@@ -1260,8 +1263,11 @@ impl Trampoline {ErrorContextDrop { .. } => format!("error-context-drop"),ResourceTransferOwn => format!("component-resource-transfer-own"),ResourceTransferBorrow => format!("component-resource-transfer-borrow"),- ResourceTransferBorrowToOwner => {- format!("component-resource-transfer-borrow-to-owner")+ ResourceTransferBorrowToOwner { ty } => {+ format!(+ "component-resource-transfer-borrow-to-owner-{}",+ ty.as_u32()+ )}PrepareCall { .. } => format!("component-prepare-call"),StartCall { .. } => format!("component-start-call"),diff --git a/crates/environ/src/component/thread_transparency.rs b/crates/environ/src/component/thread_transparency.rsindex 47656ed26d..6d47163975 100644--- a/crates/environ/src/component/thread_transparency.rs+++ b/crates/environ/src/component/thread_transparency.rs@@ -304,7 +304,7 @@ fn trampoline_callable(Transcoder { .. }| ResourceTransferOwn| ResourceTransferBorrow- | ResourceTransferBorrowToOwner+ | ResourceTransferBorrowToOwner { .. }| PrepareCall { .. }| StartCall { .. }| FutureTransferdiff --git a/crates/environ/src/component/translate/adapt.rs b/crates/environ/src/component/translate/adapt.rsindex aa60b885cc..5e3ed4ec70 100644--- a/crates/environ/src/component/translate/adapt.rs+++ b/crates/environ/src/component/translate/adapt.rs@@ -325,8 +325,8 @@ fn fact_import_to_core_def(fact::Import::ResourceTransferBorrow => {simple_intrinsic(dfg::Trampoline::ResourceTransferBorrow)}- fact::Import::ResourceTransferBorrowToOwner => {- simple_intrinsic(dfg::Trampoline::ResourceTransferBorrowToOwner)+ fact::Import::ResourceTransferBorrowToOwner { ty } => {+ simple_intrinsic(dfg::Trampoline::ResourceTransferBorrowToOwner { ty: *ty })}fact::Import::PrepareCall { memory } => simple_intrinsic(dfg::Trampoline::PrepareCall {memory: memory.as_ref().map(|v| dfg.memories.push(unwrap_memory(v))),diff --git a/crates/environ/src/fact.rs b/crates/environ/src/fact.rsindex 02c6518e0b..06fb74d23f 100644--- a/crates/environ/src/fact.rs+++ b/crates/environ/src/fact.rs@@ -87,7 +87,7 @@ pub struct Module<'a> {/// Cached versions of imported trampolines for working with resources.imported_resource_transfer_own: Option<FuncIndex>,imported_resource_transfer_borrow: Option<FuncIndex>,- imported_resource_transfer_borrow_to_owner: Option<FuncIndex>,+ imported_resource_transfer_borrow_to_owner: HashMap<TypeResourceTableIndex, FuncIndex>,// Cached versions of imported trampolines for working with the async ABI.imported_start_calls: HashMap<(Option<FuncIndex>, Option<FuncIndex>, Vec<ValType>), FuncIndex>,@@ -301,7 +301,7 @@ impl<'a> Module<'a> {helper_worklist: Vec::new(),imported_resource_transfer_own: None,imported_resource_transfer_borrow: None,- imported_resource_transfer_borrow_to_owner: None,+ imported_resource_transfer_borrow_to_owner: HashMap::new(),imported_start_calls: HashMap::new(),imported_future_transfer: None,imported_stream_transfer: None,@@ -710,14 +710,28 @@ impl<'a> Module<'a> {.resource_owned_by_own_instance(ty, |idx| self.component.defined_resource_instance(idx))}- fn import_resource_transfer_borrow_to_owner(&mut self) -> FuncIndex {- self.import_simple(+ fn import_resource_transfer_borrow_to_owner(+ &mut self,+ ty: TypeResourceTableIndex,+ ) -> FuncIndex {+ self.import_simple_get_and_set("resource",- "transfer-borrow-to-owner",- &[ValType::I32, ValType::I32],+ &format!("transfer-borrow-to-owner{}", ty.as_u32()),&[ValType::I32],- Import::ResourceTransferBorrowToOwner,- |me| &mut me.imported_resource_transfer_borrow_to_owner,+ &[ValType::I32],+ Import::ResourceTransferBorrowToOwner { ty },+ |me| {+ me.imported_resource_transfer_borrow_to_owner+ .get(&ty)+ .copied()+ },+ |me, v| {+ assert!(+ me.imported_resource_transfer_borrow_to_owner+ .insert(ty, v)+ .is_none()+ )+ },)}@@ -942,7 +956,10 @@ pub enum Import {ResourceTransferBorrow,/// Lifts a borrowed resource for a destination which owns the resource and/// therefore receives its representation rather than a handle.- ResourceTransferBorrowToOwner,+ ResourceTransferBorrowToOwner {+ /// The resource table the borrow is being transferred out of.+ ty: TypeResourceTableIndex,+ },/// An intrinsic used by FACT-generated modules to begin a call involving/// an async-lowered import and/or an async-lifted export.PrepareCall {diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rsindex ecbbd75f6f..52db189f93 100644--- a/crates/environ/src/fact/trampoline.rs+++ b/crates/environ/src/fact/trampoline.rs@@ -3499,15 +3499,13 @@ impl<'a, 'b> Compiler<'a, 'b> {_ => panic!("expected an `Borrow`"),};- let (transfer, dst_ty) = if self.module.resource_owned_by_own_instance(dst_ty) {- (self.module.import_resource_transfer_borrow_to_owner(), None)+ if self.module.resource_owned_by_own_instance(dst_ty) {+ let transfer = self.module.import_resource_transfer_borrow_to_owner(src_ty);+ self.translate_handle_types(src, &[], dst, transfer);} else {- (- self.module.import_resource_transfer_borrow(),- Some(dst_ty.as_u32()),- )- };- self.translate_handle(src_ty.as_u32(), src, dst_ty, dst, transfer);+ let transfer = self.module.import_resource_transfer_borrow();+ self.translate_handle(src_ty.as_u32(), src, Some(dst_ty.as_u32()), dst, transfer);+ }}/// Translates the index `src`, which resides in the table `src_ty`, into@@ -3524,6 +3522,22 @@ impl<'a, 'b> Compiler<'a, 'b> {dst_ty: Option<u32>,dst: &Destination,transfer: FuncIndex,+ ) {+ let types: &[u32] = match dst_ty {+ Some(dst_ty) => &[src_ty, dst_ty],+ None => &[src_ty],+ };+ self.translate_handle_types(src, types, dst, transfer);+ }++ /// As [`Self::translate_handle`], except that `types` gives exactly the+ /// type constants to push after the index.+ fn translate_handle_types(+ &mut self,+ src: &Source<'_>,+ types: &[u32],+ dst: &Destination,+ transfer: FuncIndex,) {self.push_dst_addr(dst);match src {@@ -3531,9 +3545,8 @@ impl<'a, 'b> Compiler<'a, 'b> {Source::Stack(stack) => self.stack_get(stack, ValType::I32),Source::Struct(_) | Source::Array(_) => todo!("CM+GC"),}- self.instruction(I32Const(src_ty as i32));- if let Some(dst_ty) = dst_ty {- self.instruction(I32Const(dst_ty as i32));+ for ty in types {+ self.instruction(I32Const(*ty as i32));}self.instruction(Call(transfer.as_u32()));match dst {diff --git a/crates/wasmtime/src/runtime/vm/component/libcalls.rs b/crates/wasmtime/src/runtime/vm/component/libcalls.rsindex 59e596db05..e19b3c70f0 100644--- a/crates/wasmtime/src/runtime/vm/component/libcalls.rs+++ b/crates/wasmtime/src/runtime/vm/component/libcalls.rs@@ -671,8 +671,8 @@ fn resource_transfer_borrow(fn resource_transfer_borrow_to_owner(store: &mut dyn VMStore,instance: Instance,- src_idx: u32,src_table: u32,+ src_idx: u32,) -> Result<u32> {let src_table = TypeResourceTableIndex::from_u32(src_table);instance.resource_transfer_borrow_to_owner(store, src_idx, src_table)