Skip to main content

wasmtime_environ/
component.rs

1//! Support for the component model in Wasmtime.
2//!
3//! This module contains all of the internal type definitions used by Wasmtime
4//! to process the component model. Despite everything being `pub` here this is
5//! not the public interface of Wasmtime to the component model. Instead this is
6//! the internal support to mirror the core wasm support that Wasmtime already
7//! implements.
8//!
9//! Some main items contained within here are:
10//!
11//! * Type hierarchy information for the component model
12//! * Translation of a component into Wasmtime's representation
13//! * Type information about a component used at runtime
14//!
15//! This module also contains a lot of Serialize/Deserialize types which are
16//! encoded in the final compiled image for a component.
17//!
18//! Note that this entire module is gated behind the `component-model` Cargo
19//! feature.
20
21/// Canonical ABI-defined constant for the maximum number of "flat" parameters
22/// to a wasm function, or the maximum number of parameters a core wasm function
23/// will take for just the parameters used. Over this number the heap is used
24/// for transferring parameters.
25pub const MAX_FLAT_PARAMS: usize = 16;
26
27/// Similar to `MAX_FLAT_PARAMS`, but used for async-lowered imports instead of
28/// sync ones.
29pub const MAX_FLAT_ASYNC_PARAMS: usize = 4;
30
31/// Canonical ABI-defined constant for the maximum number of "flat" results.
32/// This number of results are returned directly from wasm and otherwise results
33/// are transferred through memory.
34pub const MAX_FLAT_RESULTS: usize = 1;
35
36/// Sentinel value in `result_count_or_max_if_async` as part of the
37/// `prepare_call` libcall which indicates that preparation is being done for an
38/// async function that produces no result, aka there is no return pointer.
39pub const PREPARE_ASYNC_NO_RESULT: u32 = u32::MAX;
40
41/// Sentinel value in `result_count_or_max_if_async` as part of the
42/// `prepare_call` libcall which indicates that preparation is being done for an
43/// async function that produces at least one result, aka there is a return
44/// pointer.
45pub const PREPARE_ASYNC_WITH_RESULT: u32 = u32::MAX - 1;
46
47/// Bit flag for indicating async-lifted exports
48///
49/// This flag may be passed to the `start-call` built-in function to indicate
50/// that the callee is an async-lifted export.
51pub const START_FLAG_ASYNC_CALLEE: i32 = 1 << 0;
52
53/// Bit flag for indicating async-lowered imports
54///
55/// This flag may be passed to the `start-call` built-in function to indicate
56/// that the caller is an async-lowered import, in which case the call's status
57/// is returned rather than blocking until the callee produces its results.
58pub const START_FLAG_ASYNC_CALLER: i32 = 1 << 1;
59
60mod artifacts;
61mod info;
62mod intrinsic;
63mod names;
64mod types;
65mod vmcomponent_offsets;
66pub use self::artifacts::*;
67pub use self::info::*;
68pub use self::intrinsic::*;
69pub use self::names::*;
70pub use self::types::*;
71pub use self::vmcomponent_offsets::*;
72
73#[cfg(feature = "compile")]
74mod compiler;
75#[cfg(feature = "compile")]
76pub mod dfg;
77#[cfg(feature = "compile")]
78mod same_vmctx;
79#[cfg(feature = "compile")]
80mod thread_transparency;
81#[cfg(feature = "compile")]
82mod translate;
83#[cfg(feature = "compile")]
84mod types_builder;
85#[cfg(feature = "compile")]
86pub use self::compiler::*;
87#[cfg(feature = "compile")]
88pub use self::same_vmctx::analyze_same_vmctx_imports;
89#[cfg(feature = "compile")]
90pub use self::thread_transparency::transparent_adapters;
91#[cfg(feature = "compile")]
92pub use self::translate::*;
93#[cfg(feature = "compile")]
94pub use self::types_builder::*;
95
96/// Helper macro, like `foreach_transcoder`, to iterate over builtins for
97/// components unrelated to transcoding.
98#[macro_export]
99macro_rules! foreach_builtin_component_function {
100    ($mac:ident) => {
101        $mac! {
102            resource_new32(vmctx: vmctx, caller_instance: u32, resource: u32, rep: u32) -> u64;
103            resource_rep32(vmctx: vmctx, caller_instance: u32, resource: u32, idx: u32) -> u64;
104
105            // Returns an `Option<u32>` where `None` is "no destructor needed"
106            // and `Some(val)` is "run the destructor on this rep". The option
107            // is encoded as a 64-bit integer where the low bit is Some/None
108            // and bits 1-33 are the payload.
109            resource_drop(vmctx: vmctx, caller_instance: u32, resource: u32, idx: u32) -> u64;
110
111            resource_transfer_own(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
112            resource_transfer_borrow(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
113
114            enter_sync_call(vmctx: vmctx, callee_async: u32, callee_instance: u32) -> bool;
115            exit_sync_call(vmctx: vmctx) -> bool;
116
117            #[cfg(feature = "component-model-async")]
118            backpressure_modify(vmctx: vmctx, caller_instance: u32, increment: u8) -> bool;
119            #[cfg(feature = "component-model-async")]
120            task_return(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, storage: ptr_u8, storage_len: size) -> bool;
121            #[cfg(feature = "component-model-async")]
122            task_cancel(vmctx: vmctx, caller_instance: u32) -> bool;
123            #[cfg(feature = "component-model-async")]
124            waitable_set_new(vmctx: vmctx, caller_instance: u32) -> u64;
125            // FIXME(#4311): `payload` needs to be a `u64` for memory64
126            #[cfg(feature = "component-model-async")]
127            waitable_set_wait(vmctx: vmctx, caller_instance: u32, options: u32, set: u32, payload: u32) -> u64;
128            // FIXME(#4311): `payload` needs to be a `u64` for memory64
129            #[cfg(feature = "component-model-async")]
130            waitable_set_poll(vmctx: vmctx, caller_instance: u32, options: u32, set: u32, payload: u32) -> u64;
131            #[cfg(feature = "component-model-async")]
132            waitable_set_drop(vmctx: vmctx, caller_instance: u32, set: u32) -> bool;
133            #[cfg(feature = "component-model-async")]
134            waitable_join(vmctx: vmctx, caller_instance: u32, set: u32, waitable: u32) -> bool;
135            #[cfg(feature = "component-model-async")]
136            subtask_drop(vmctx: vmctx, caller_instance: u32, task_id: u32) -> bool;
137            #[cfg(feature = "component-model-async")]
138            subtask_cancel(vmctx: vmctx, caller_instance: u32, async_: u8, task_id: u32) -> u64;
139            #[cfg(feature = "component-model-async")]
140            prepare_call(
141                vmctx: vmctx,
142                memory: ptr_u8,
143                start: ptr_u8,
144                return_: ptr_u8,
145                caller_instance: u32,
146                callee_instance: u32,
147                task_return_type: u32,
148                callee_async: u32,
149                string_encoding: u32,
150                result_count_or_max_if_async: u32,
151                storage: ptr_u8,
152                storage_len: size
153            ) -> bool;
154            #[cfg(feature = "component-model-async")]
155            start_call(vmctx: vmctx, callback: ptr_u8, post_return: ptr_u8, storage: ptr_u8, storage_len: size, callee: ptr_u8, param_count: u32, result_count: u32, flags: u32) -> bool;
156            #[cfg(feature = "component-model-async")]
157            future_new(vmctx: vmctx, caller_instance: u32, ty: u32) -> u64;
158            // FIXME(#4311): `address` needs to be a `u64` for memory64
159            #[cfg(feature = "component-model-async")]
160            future_write(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, future: u32, address: u32) -> u64;
161            // FIXME(#4311): `address` needs to be a `u64` for memory64
162            #[cfg(feature = "component-model-async")]
163            future_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, future: u32, address: u32) -> u64;
164            #[cfg(feature = "component-model-async")]
165            future_cancel_write(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, writer: u32) -> u64;
166            #[cfg(feature = "component-model-async")]
167            future_cancel_read(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, reader: u32) -> u64;
168            #[cfg(feature = "component-model-async")]
169            future_drop_writable(vmctx: vmctx, caller_instance: u32, ty: u32, writer: u32) -> bool;
170            #[cfg(feature = "component-model-async")]
171            future_drop_readable(vmctx: vmctx, caller_instance: u32, ty: u32, reader: u32) -> bool;
172            #[cfg(feature = "component-model-async")]
173            stream_new(vmctx: vmctx, caller_instance: u32, ty: u32) -> u64;
174            // FIXME(#4311): `address` and `count` need to be `u64`s for memory64
175            #[cfg(feature = "component-model-async")]
176            stream_write(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, stream: u32, address: u32, count: u32) -> u64;
177            // FIXME(#4311): `address` and `count` need to be `u64`s for memory64
178            #[cfg(feature = "component-model-async")]
179            stream_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, stream: u32, address: u32, count: u32) -> u64;
180            #[cfg(feature = "component-model-async")]
181            stream_cancel_write(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, writer: u32) -> u64;
182            #[cfg(feature = "component-model-async")]
183            stream_cancel_read(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, reader: u32) -> u64;
184            #[cfg(feature = "component-model-async")]
185            stream_drop_writable(vmctx: vmctx, caller_instance: u32, ty: u32, writer: u32) -> bool;
186            #[cfg(feature = "component-model-async")]
187            stream_drop_readable(vmctx: vmctx, caller_instance: u32, ty: u32, reader: u32) -> bool;
188            // FIXME(#4311): `address` and `count` need to be `u64`s for memory64
189            #[cfg(feature = "component-model-async")]
190            flat_stream_write(vmctx: vmctx, caller_instance: u32, ty: u32, options:u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
191            // FIXME(#4311): `address` and `count` need to be `u64`s for memory64
192            #[cfg(feature = "component-model-async")]
193            flat_stream_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
194            // FIXME(#4311): `debug_msg_address` and `debug_msg_len` need to be `u64`s for memory64
195            #[cfg(feature = "component-model-async")]
196            error_context_new(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, debug_msg_address: u32, debug_msg_len: u32) -> u64;
197            // FIXME(#4311): `debug_msg_address` needs to be a `u64` for memory64
198            #[cfg(feature = "component-model-async")]
199            error_context_debug_message(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, err_ctx_handle: u32, debug_msg_address: u32) -> bool;
200            #[cfg(feature = "component-model-async")]
201            error_context_drop(vmctx: vmctx, caller_instance: u32, ty: u32, err_ctx_handle: u32) -> bool;
202            #[cfg(feature = "component-model-async")]
203            future_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
204            #[cfg(feature = "component-model-async")]
205            stream_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
206            #[cfg(feature = "component-model-async")]
207            error_context_transfer(vmctx: vmctx, src_idx: u32, src_table: u32, dst_table: u32) -> u64;
208            #[cfg(feature = "component-model-async")]
209            thread_index(vmctx: vmctx) -> u64;
210            #[cfg(feature = "component-model-async")]
211            thread_new_indirect(vmctx: vmctx, caller_instance: u32, func_ty_id: u32, func_table_idx: u32, func_idx: u32, context: u32) -> u64;
212            #[cfg(feature = "component-model-async")]
213            thread_resume_later(vmctx: vmctx, caller_instance: u32, thread_idx: u32) -> bool;
214            #[cfg(feature = "component-model-async")]
215            thread_suspend(vmctx: vmctx, caller_instance: u32) -> u32;
216            #[cfg(feature = "component-model-async")]
217            thread_yield(vmctx: vmctx, caller_instance: u32) -> u32;
218            #[cfg(feature = "component-model-async")]
219            thread_suspend_then_resume(vmctx: vmctx, caller_instance: u32, thread_idx: u32) -> u32;
220            #[cfg(feature = "component-model-async")]
221            thread_yield_then_resume(vmctx: vmctx, caller_instance: u32, thread_idx: u32) -> u32;
222            #[cfg(feature = "component-model-async")]
223            thread_suspend_then_promote(vmctx: vmctx, caller_instance: u32, thread_idx: u32) -> u32;
224            #[cfg(feature = "component-model-async")]
225            thread_yield_then_promote(vmctx: vmctx, caller_instance: u32, thread_idx: u32) -> u32;
226
227            utf8_to_utf8(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;
228            utf16_to_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> bool;
229            latin1_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;
230            latin1_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> bool;
231            utf8_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> size;
232            utf16_to_utf8(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u8, dst_len: size, first_pass: u32, ret2: ptr_size) -> size;
233            latin1_to_utf8(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u8, dst_len: size, first_pass: u32, ret2: ptr_size) -> size;
234            utf16_to_compact_probably_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> size;
235            utf8_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8, ret2: ptr_size) -> size;
236            utf16_to_latin1(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u8, ret2: ptr_size) -> size;
237            utf8_to_compact_utf16(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;
238            utf16_to_compact_utf16(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u16, dst_len: size, bytes_so_far: size) -> size;
239        }
240    };
241}
242
243// Define `struct ComponentBuiltinFunctionIndex`
244declare_builtin_index! {
245    /// An index type for component builtin functions.
246    pub struct ComponentBuiltinFunctionIndex: foreach_builtin_component_function;
247}