wasmtime_environ/builtin.rs
1/// Helper macro to iterate over all builtin functions and their signatures.
2#[macro_export]
3macro_rules! foreach_builtin_function {
4 ($mac:ident) => {
5 $mac! {
6 // Returns an index for wasm's `memory.grow` builtin function.
7 memory32_grow(vmctx: vmctx, delta: u64, index: u32) -> pointer;
8 // Returns an index for wasm's `table.copy` when both tables are locally
9 // defined.
10 table_copy(vmctx: vmctx, dst_index: u32, src_index: u32, dst: u64, src: u64, len: u64) -> bool;
11 // Returns an index for wasm's `table.init`.
12 table_init(vmctx: vmctx, table: u32, elem: u32, dst: u64, src: u64, len: u64) -> bool;
13 // Returns an index for wasm's `elem.drop`.
14 elem_drop(vmctx: vmctx, elem: u32);
15 // Returns an index for wasm's `memory.copy`
16 memory_copy(vmctx: vmctx, dst_index: u32, dst: u64, src_index: u32, src: u64, len: u64) -> bool;
17 // Returns an index for wasm's `memory.fill` instruction.
18 memory_fill(vmctx: vmctx, memory: u32, dst: u64, val: u32, len: u64) -> bool;
19 // Returns an index for wasm's `memory.init` instruction.
20 memory_init(vmctx: vmctx, memory: u32, data: u32, dst: u64, src: u32, len: u32) -> bool;
21 // Returns a value for wasm's `ref.func` instruction.
22 ref_func(vmctx: vmctx, func: u32) -> pointer;
23 // Returns an index for wasm's `data.drop` instruction.
24 data_drop(vmctx: vmctx, data: u32);
25 // Returns a table entry after lazily initializing it.
26 table_get_lazy_init_func_ref(vmctx: vmctx, table: u32, index: u64) -> pointer;
27 // Returns an index for Wasm's `table.grow` instruction for `funcref`s.
28 table_grow_func_ref(vmctx: vmctx, table: u32, delta: u64, init: pointer) -> pointer;
29 // Returns an index for Wasm's `table.fill` instruction for `funcref`s.
30 table_fill_func_ref(vmctx: vmctx, table: u32, dst: u64, val: pointer, len: u64) -> bool;
31 // Returns an index for wasm's `memory.atomic.notify` instruction.
32 #[cfg(feature = "threads")]
33 memory_atomic_notify(vmctx: vmctx, memory: u32, addr: u64, count: u32) -> u64;
34 // Returns an index for wasm's `memory.atomic.wait32` instruction.
35 #[cfg(feature = "threads")]
36 memory_atomic_wait32(vmctx: vmctx, memory: u32, addr: u64, expected: u32, timeout: u64) -> u64;
37 // Returns an index for wasm's `memory.atomic.wait64` instruction.
38 #[cfg(feature = "threads")]
39 memory_atomic_wait64(vmctx: vmctx, memory: u32, addr: u64, expected: u64, timeout: u64) -> u64;
40 // Invoked when fuel has run out while executing a function.
41 out_of_gas(vmctx: vmctx) -> bool;
42 // Invoked when we reach a new epoch.
43 #[cfg(target_has_atomic = "64")]
44 new_epoch(vmctx: vmctx) -> u64;
45 // Invoked before malloc returns.
46 #[cfg(feature = "wmemcheck")]
47 check_malloc(vmctx: vmctx, addr: u32, len: u32) -> bool;
48 // Invoked before the free returns.
49 #[cfg(feature = "wmemcheck")]
50 check_free(vmctx: vmctx, addr: u32) -> bool;
51 // Invoked before a load is executed.
52 #[cfg(feature = "wmemcheck")]
53 check_load(vmctx: vmctx, num_bytes: u32, addr: u32, offset: u32) -> bool;
54 // Invoked before a store is executed.
55 #[cfg(feature = "wmemcheck")]
56 check_store(vmctx: vmctx, num_bytes: u32, addr: u32, offset: u32) -> bool;
57 // Invoked after malloc is called.
58 #[cfg(feature = "wmemcheck")]
59 malloc_start(vmctx: vmctx);
60 // Invoked after free is called.
61 #[cfg(feature = "wmemcheck")]
62 free_start(vmctx: vmctx);
63 // Invoked when wasm stack pointer is updated.
64 #[cfg(feature = "wmemcheck")]
65 update_stack_pointer(vmctx: vmctx, value: u32);
66 // Invoked before memory.grow is called.
67 #[cfg(feature = "wmemcheck")]
68 update_mem_size(vmctx: vmctx, num_bytes: u32);
69
70 // Drop a non-stack GC reference (eg an overwritten table entry)
71 // once it will no longer be used again. (Note: `val` is not of type
72 // `reference` because it needn't appear in any stack maps, as it
73 // must not be live after this call.)
74 #[cfg(feature = "gc-drc")]
75 drop_gc_ref(vmctx: vmctx, val: u32);
76
77 // Do a GC, treating the optional `root` as a GC root and returning
78 // the updated `root` (so that, in the case of moving collectors,
79 // callers have a valid version of `root` again).
80 #[cfg(feature = "gc-drc")]
81 gc(vmctx: vmctx, root: u32) -> u64;
82
83 // Allocate a new, uninitialized GC object and return a reference to
84 // it.
85 #[cfg(feature = "gc-drc")]
86 gc_alloc_raw(
87 vmctx: vmctx,
88 kind: u32,
89 module_interned_type_index: u32,
90 size: u32,
91 align: u32
92 ) -> u64;
93
94 // Intern a `funcref` into the GC heap, returning its
95 // `FuncRefTableId`.
96 //
97 // This libcall may not GC.
98 #[cfg(feature = "gc")]
99 intern_func_ref_for_gc_heap(
100 vmctx: vmctx,
101 func_ref: pointer
102 ) -> u64;
103
104 // Get the raw `VMFuncRef` pointer associated with a
105 // `FuncRefTableId` from an earlier `intern_func_ref_for_gc_heap`
106 // call.
107 //
108 // This libcall may not GC.
109 //
110 // Passes in the `ModuleInternedTypeIndex` of the funcref's expected
111 // type, or `ModuleInternedTypeIndex::reserved_value()` if we are
112 // getting the function reference as an untyped `funcref` rather
113 // than a typed `(ref $ty)`.
114 //
115 // TODO: We will want to eventually expose the table directly to
116 // Wasm code, so that it doesn't need to make a libcall to go from
117 // id to `VMFuncRef`. That will be a little tricky: it will also
118 // require updating the pointer to the slab in the `VMContext` (or
119 // `VMRuntimeLimits` or wherever we put it) when the slab is
120 // resized.
121 #[cfg(feature = "gc")]
122 get_interned_func_ref(
123 vmctx: vmctx,
124 func_ref_id: u32,
125 module_interned_type_index: u32
126 ) -> pointer;
127
128 // Builtin implementation of the `array.new_data` instruction.
129 #[cfg(feature = "gc")]
130 array_new_data(
131 vmctx: vmctx,
132 array_interned_type_index: u32,
133 data_index: u32,
134 data_offset: u32,
135 len: u32
136 ) -> u64;
137
138 // Builtin implementation of the `array.new_elem` instruction.
139 #[cfg(feature = "gc")]
140 array_new_elem(
141 vmctx: vmctx,
142 array_interned_type_index: u32,
143 elem_index: u32,
144 elem_offset: u32,
145 len: u32
146 ) -> u64;
147
148 // Builtin implementation of the `array.copy` instruction.
149 #[cfg(feature = "gc")]
150 array_copy(
151 vmctx: vmctx,
152 dst_array: u32,
153 dst_index: u32,
154 src_array: u32,
155 src_index: u32,
156 len: u32
157 ) -> bool;
158
159 // Builtin implementation of the `array.init_data` instruction.
160 #[cfg(feature = "gc")]
161 array_init_data(
162 vmctx: vmctx,
163 array_interned_type_index: u32,
164 array: u32,
165 dst_index: u32,
166 data_index: u32,
167 data_offset: u32,
168 len: u32
169 ) -> bool;
170
171 // Builtin implementation of the `array.init_elem` instruction.
172 #[cfg(feature = "gc")]
173 array_init_elem(
174 vmctx: vmctx,
175 array_interned_type_index: u32,
176 array: u32,
177 dst: u32,
178 elem_index: u32,
179 src: u32,
180 len: u32
181 ) -> bool;
182
183 // Returns whether `actual_engine_type` is a subtype of
184 // `expected_engine_type`.
185 #[cfg(feature = "gc")]
186 is_subtype(
187 vmctx: vmctx,
188 actual_engine_type: u32,
189 expected_engine_type: u32
190 ) -> u32;
191
192 // Returns an index for Wasm's `table.grow` instruction for GC references.
193 #[cfg(feature = "gc")]
194 table_grow_gc_ref(vmctx: vmctx, table: u32, delta: u64, init: u32) -> pointer;
195
196 // Returns an index for Wasm's `table.fill` instruction for GC references.
197 #[cfg(feature = "gc")]
198 table_fill_gc_ref(vmctx: vmctx, table: u32, dst: u64, val: u32, len: u64) -> bool;
199
200 // Raises an unconditional trap with the specified code.
201 //
202 // This is used when signals-based-traps are disabled for backends
203 // when an illegal instruction can't be executed for example.
204 trap(vmctx: vmctx, code: u8);
205
206 // Raises an unconditional trap where the trap information must have
207 // been previously filled in.
208 raise(vmctx: vmctx);
209 }
210 };
211}
212
213/// Helper macro to define a builtin type such as `BuiltinFunctionIndex` and
214/// `ComponentBuiltinFunctionIndex` using the iterator macro, e.g.
215/// `foreach_builtin_function`, as the way to generate accessor methods.
216macro_rules! declare_builtin_index {
217 ($index_name:ident, $iter:ident) => {
218 /// An index type for builtin functions.
219 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
220 pub struct $index_name(u32);
221
222 impl $index_name {
223 /// Create a new builtin from its raw index
224 pub const fn from_u32(i: u32) -> Self {
225 assert!(i < Self::len());
226 Self(i)
227 }
228
229 /// Return the index as an u32 number.
230 pub const fn index(&self) -> u32 {
231 self.0
232 }
233
234 $iter!(declare_builtin_index_constructors);
235 }
236 };
237}
238
239/// Helper macro used by the above macro.
240macro_rules! declare_builtin_index_constructors {
241 (
242 $(
243 $( #[$attr:meta] )*
244 $name:ident( $( $pname:ident: $param:ident ),* ) $( -> $result:ident )?;
245 )*
246 ) => {
247 declare_builtin_index_constructors!(
248 @indices;
249 0;
250 $( $( #[$attr] )* $name; )*
251 );
252
253 /// Returns a symbol name for this builtin.
254 pub fn name(&self) -> &'static str {
255 $(
256 if *self == Self::$name() {
257 return stringify!($name);
258 }
259 )*
260 unreachable!()
261 }
262 };
263
264 // Base case: no more indices to declare, so define the total number of
265 // function indices.
266 (
267 @indices;
268 $len:expr;
269 ) => {
270 /// Returns the total number of builtin functions.
271 pub const fn len() -> u32 {
272 $len
273 }
274 };
275
276 // Recursive case: declare the next index, and then keep declaring the rest of
277 // the indices.
278 (
279 @indices;
280 $index:expr;
281 $( #[$this_attr:meta] )*
282 $this_name:ident;
283 $(
284 $( #[$rest_attr:meta] )*
285 $rest_name:ident;
286 )*
287 ) => {
288 #[allow(missing_docs, reason = "macro-generated")]
289 pub const fn $this_name() -> Self {
290 Self($index)
291 }
292
293 declare_builtin_index_constructors!(
294 @indices;
295 ($index + 1);
296 $( $( #[$rest_attr] )* $rest_name; )*
297 );
298 }
299}
300
301// Define `struct BuiltinFunctionIndex`
302declare_builtin_index!(BuiltinFunctionIndex, foreach_builtin_function);
303
304/// Return value of [`BuiltinFunctionIndex::trap_sentinel`].
305pub enum TrapSentinel {
306 /// A falsy or zero value indicates a trap.
307 Falsy,
308 /// The value `-2` indicates a trap (used for growth-related builtins).
309 NegativeTwo,
310 /// The value `-1` indicates a trap .
311 NegativeOne,
312 /// Any negative value indicates a trap.
313 Negative,
314}
315
316impl BuiltinFunctionIndex {
317 /// Describes the return value of this builtin and what represents a trap.
318 ///
319 /// Libcalls don't raise traps themselves and instead delegate to compilers
320 /// to do so. This means that some return values of libcalls indicate a trap
321 /// is happening and this is represented with sentinel values. This function
322 /// returns the description of the sentinel value which indicates a trap, if
323 /// any. If `None` is returned from this function then this builtin cannot
324 /// generate a trap.
325 #[allow(unreachable_code, unused_macro_rules, reason = "macro-generated code")]
326 pub fn trap_sentinel(&self) -> Option<TrapSentinel> {
327 macro_rules! trap_sentinel {
328 (
329 $(
330 $( #[$attr:meta] )*
331 $name:ident( $( $pname:ident: $param:ident ),* ) $( -> $result:ident )?;
332 )*
333 ) => {{
334 $(
335 $(#[$attr])*
336 if *self == BuiltinFunctionIndex::$name() {
337 let mut _ret = None;
338 $(_ret = Some(trap_sentinel!(@get $name $result));)?
339 return _ret;
340 }
341 )*
342
343 None
344 }};
345
346 // Growth-related functions return -2 as a sentinel.
347 (@get memory32_grow pointer) => (TrapSentinel::NegativeTwo);
348 (@get table_grow_func_ref pointer) => (TrapSentinel::NegativeTwo);
349 (@get table_grow_gc_ref pointer) => (TrapSentinel::NegativeTwo);
350
351 // Atomics-related functions return a negative value indicating trap
352 // indicate a trap.
353 (@get memory_atomic_notify u64) => (TrapSentinel::Negative);
354 (@get memory_atomic_wait32 u64) => (TrapSentinel::Negative);
355 (@get memory_atomic_wait64 u64) => (TrapSentinel::Negative);
356
357 // GC-related functions return a 64-bit value which is negative to
358 // indicate a trap.
359 (@get gc u64) => (TrapSentinel::Negative);
360 (@get gc_alloc_raw u64) => (TrapSentinel::Negative);
361 (@get array_new_data u64) => (TrapSentinel::Negative);
362 (@get array_new_elem u64) => (TrapSentinel::Negative);
363
364 // The final epoch represents a trap
365 (@get new_epoch u64) => (TrapSentinel::NegativeOne);
366
367 // These libcalls can't trap
368 (@get ref_func pointer) => (return None);
369 (@get table_get_lazy_init_func_ref pointer) => (return None);
370 (@get get_interned_func_ref pointer) => (return None);
371 (@get intern_func_ref_for_gc_heap u64) => (return None);
372 (@get is_subtype u32) => (return None);
373
374 // Bool-returning functions use `false` as an indicator of a trap.
375 (@get $name:ident bool) => (TrapSentinel::Falsy);
376
377 (@get $name:ident $ret:ident) => (
378 compile_error!(concat!("no trap sentinel registered for ", stringify!($name)))
379 )
380 }
381
382 foreach_builtin_function!(trap_sentinel)
383 }
384}