Before
parent: tbd commit: e432306
1 | //! Giterated ABI |
2 | //! # ABI |
3 | //! |
4 | //! ## Value ABI |
5 | //! |
6 | //! At its core, the Giterated Runtime uses the `extern "C"` ABI. What that means is likely platform specific, and doesn't matter. |
7 | //! You are intended to compile the Giterated Runtime and Plugins for your local machine, all with a similar idea of what |
8 | //! your "local machine" is. |
9 | //! |
10 | //! Values are passed using the `FFI` type. There are four categories of value that the `FFI` type enables you to pass: |
11 | //! |
12 | //! | `FFI` Type Category | Placed Backing? | Owned? | |
13 | //! |---------------------|-----------------|--------| |
14 | //! | Slice | Heap/Stack | No | |
15 | //! | Referenced Slice | Stack | No | |
16 | //! | Referenced Value | No | No | |
17 | //! | Owned Value | Heap | Yes | |
18 | //! |
19 | //! For an FFI type to have a "placed backing" is for it to have some data structure beyond the data it represents, placed |
20 | //! somewhere in memory. Some types only require stack placement while some offer both stack and heap placement. |
21 | //! |
22 | //! Stack-placed values can be shared by `PinnedRef` and `PinnedMut`, and thus can only be owned by the caller. |
23 | //! |
24 | //! Heap-placed values can be shared by `Owned`, `PinnedRef`, and `PinnedMut`. They can be owned by any one consumer, |
25 | //! When the handle with ownership is `Drop`'d by the sole consumer, it will free the object using the associated `Drop` callback. |
26 | //! |
27 | //! ### Safety Intents |
28 | //! |
29 | //! This API is designed to simplify interaction with FFI values, and provide a static ABI for those values to be passed. It |
30 | //! is key to enabling ownership across FFI while ensuring associated dropping and allocation freeing logic is ran. |
31 | //! |
32 | //! The contract the developer has to follow is made simpler by this system, and it allows for generic code to be written to |
33 | //! interact with FFI-given values and pass values using FFI. |
34 | //! |
35 | //! ### Stability Guarantees |
36 | //! |
37 | //! There are no plans to guarantee stability until 1.0.0. At that point you can expect the ABI to remain stable until the major version |
38 | //! is incremented again. There will be an appropriate deprecation process and changeover period. |
39 | //! |
40 | //! ### Memory Representation |
41 | //! |
42 | //! Please check out the source code, sorry if you needed that from the docs! |
43 | //! |
44 | //! ## Object, Operation, Setting, Value, Plugin, and Runtime ABIs |
45 | //! |
46 | //! The Giterated Runtime uses vtables to accomplish the goal of ensuring maximum compatibility. For every object that is shared |
47 | //! between plugins, a vtable is used to allow each plugin to provide their own code for interacting with the object. |
48 | //! |
49 | //! When objects switch "runtime domains" (e.g. host -> plugin, plugin -> plugin, plugin -> host), their vtable is swapped out |
50 | //! for the new runtime domain's own vtables. |
51 | //! |
52 | //! ### Untyped "Objects" (see above header for list) |
53 | //! |
54 | //! Untyped objects, in memory, are represented by a data pointer and a vtable pointer. Exactly like Rust traits. However, to |
55 | //! prevent small compilation differences and other random garbage from making the interface not perfectly compatible we use |
56 | //! the local plugin's idea of the vtable for the object at all times. An object that the plugin does not have a vtable for cannot |
57 | //! be relevant to the plugin. |
58 | //! |
59 | //! It is important that the object's base representation in memory remain unchanged between major versions, but the vtables that provide methods for |
60 | //! that object may be grown. The methods that operate on that object may be changed in an non-breaking fashion, and bugs can be |
61 | //! fixed. |
62 | //! |
63 | //! ## Futures ABI |
64 | //! |
65 | //! The Giterated Runtime has an async runtime that allows for futures to be shared and awaited across FFI boundaries while only |
66 | //! executing the future within the context of the Plugin who is running the underlying future. |
67 | //! |
68 | //! Futures are spawned onto the `RuntimeState` with the `RuntimeFuturesExt` trait. This takes a Rust future, boxes it, and |
69 | //! provides a `RuntimeFuture` handle that can be used to drive the underlying Rust future locally. The `RuntimeFuture` handle |
70 | //! is thread-safe and can be shared with the callee and `.await`'d directly like any other future. |
71 | //! |
72 | //! ### RuntimeFuture |
73 | //! |
74 | //! The `RuntimeFuture` mixes a vtable with data to allow for any caller to drive a spawned future. It contains: |
75 | //! |
76 | //! - A `poll_fn` which is used to poll the future for `Ready`-ness |
77 | //! - A `wake_fn` which is used to wake the callee to poll for (expected) `Ready`-ness, it is populated when the `RuntimeFuture` is `await`'d. |
78 | //! |
79 | //! When the `RuntimeFuture` is polled, it causes the inner future to also be polled. We provide the inner future with a waker |
80 | //! that triggers the `RuntimeFuture`'s waker so it is polled again. Breaking character to point out how freaking cool that is. |
81 | //! |
82 | //! `RuntimeFuture`s drop the associated inner future as they drop. |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | use ; |
91 | pub use ; |
92 | use HeapPlacable; |
93 | |
94 | use |
95 | , | PhantomData
96 | , |
97 | , |
98 | ; |
99 | |
100 | use ; |
101 | use ; |
102 | |
103 | |
104 | |
105 | pub use crate Ffi; |
106 | pub use crate StackPinned; |
107 | pub use crate::*; |
108 | pub use crate::; |
109 | |
110 | |
111 | /// Slice Reference |
112 | /// Heap or Stack Placed |
113 | pub type FfiSliceRef<T> = ; |
114 | |
115 | /// Mutable Slice Reference |
116 | /// Heap or Stack Placed |
117 | pub type FfiSliceMut<T> = ; |
118 | |
119 | /// Value Reference |
120 | /// Heap or Stack Placed |
121 | pub type FfiValueRef<T> = ; |
122 | |
123 | /// Mutable Value Reference |
124 | /// Heap or Stack Placed |
125 | pub type FfiValueMut<T> = ; |
126 | |
127 | /// Owned Value |
128 | /// Heap Placed |
129 | pub type FfiValue<T> = ; |
130 | |
131 | /// Owned Slice |
132 | /// Heap Placed |
133 | pub type FfiSlice<T> = ; |
134 | |
135 | |
136 | use crate::; |
137 | |
138 | pub type FfiValueUntyped = ; |
139 | pub type FfiValueRefUntyped = ; |
140 | |
141 | |
142 | /// A value passed over FFI, following the Giterated ABI. |
143 | /// |
144 | /// The function of the [`Ffi`] type is to take an arbitrary pointer and send it over FFI. |
145 | /// Both the caller and callee **must** have the same understanding of what the pointer represents. |
146 | /// The [`Ffi`] type is also used to encode ownership information. |
147 | /// |
148 | /// # The Pointer |
149 | /// The pointer contained within the [`Ffi`] is transmuted based on the provided `ABI` on the |
150 | /// [`Ffi`] type signature. |
151 | |
152 | |
153 | inner: *const (), |
154 | _type_marker: , |
155 | _abi_marker: , |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | unsafe |
162 | |
163 | |
164 | |
165 | |
166 | type Target = ; |
167 | |
168 | |
169 | |
170 | let inner: *const = unsafe ; |
171 | let backing = unsafe ; |
172 | |
173 | unsafe |
174 | from_raw_parts |
175 | backing.slice as *mut T, |
176 | usize try_from .unwrap_unchecked, |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | let inner: *mut = unsafe ; |
185 | let backing = unsafe ; |
186 | |
187 | unsafe |
188 | from_raw_parts_mut |
189 | backing.slice as *mut T, |
190 | usize try_from .unwrap_unchecked, |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | type Target = ; |
200 | |
201 | |
202 | |
203 | let inner: *const = unsafe ; |
204 | |
205 | let backing = unsafe ; |
206 | |
207 | unsafe |
208 | from_raw_parts |
209 | backing.slice as *const T, |
210 | usize try_from .unwrap_unchecked, |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | type Target = T; |
220 | |
221 | |
222 | |
223 | let inner: *const T = unsafe ; |
224 | |
225 | match unsafe |
226 | Some => val, |
227 | _ => unreachable!, |
228 | |
229 | |
230 | |
231 | |
232 | |
233 | type Target = T; |
234 | |
235 | |
236 | let inner: *mut T = unsafe ; |
237 | |
238 | unsafe |
239 | |
240 | |
241 | |
242 | |
243 | let inner: *mut T = unsafe ; |
244 | |
245 | unsafe |
246 | |
247 | |
248 | |
249 | |
250 | |
251 | T: Display, |
252 | |
253 | |
254 | unsafe .fmt |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | let value = Box new |
261 | value, |
262 | drop_fn: free, |
263 | ; |
264 | |
265 | FfiValue |
266 | inner: Box into_raw as _, |
267 | _type_marker: PhantomData, |
268 | _abi_marker: PhantomData, |
269 | |
270 | |
271 | |
272 | |
273 | unsafe |
274 | |
275 | |
276 | |
277 | // This all boils down to moving `T` out of the `FfiValue` and dropping the backing |
278 | // storage for said `FfiValue`. Despite the use of unsafe this is exactly how moving |
279 | // a value onto the stack works. |
280 | |
281 | let inner = self.inner as *mut T; |
282 | let mut move_target: = zeroed; |
283 | |
284 | unsafe |
285 | |
286 | let inner_descriptor: *mut = unsafe ; |
287 | |
288 | unsafe ; |
289 | |
290 | unsafe |
291 | |
292 | |
293 | |
294 | |
295 | type Target = T; |
296 | |
297 | |
298 | |
299 | let inner: *const T = unsafe ; |
300 | |
301 | unsafe |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | let inner: *mut T = unsafe ; |
308 | |
309 | unsafe |
310 | |
311 | |
312 | |
313 | |
314 | use ; |
315 | |
316 | use crate::; |
317 | |
318 | |
319 | |
320 | pub(super) value: T, |
321 | pub(super) drop_fn: unsafe extern "C" fn , |
322 | |
323 | |
324 | |
325 | pub(crate) count: u64, |
326 | pub(crate) slice: *const (), |
327 | _marker: , |
328 | |
329 | |
330 | |
331 | /// Creates a new slice backing from a raw slice pointer and a count. |
332 | /// |
333 | /// # SAFETY |
334 | /// |
335 | /// `slice` **must** refer to a valid slice, with a length greater than or equal to the |
336 | /// value provided as `count`. |
337 | |
338 | pub unsafe |
339 | Self |
340 | count, |
341 | slice, |
342 | _marker: PhantomData, |
343 | |
344 | |
345 | |
346 | /// Creates a new slice backing from an [`FfiSlice`]. |
347 | /// |
348 | /// # SAFETY |
349 | /// |
350 | /// The resultant [`SliceBacking`] **must not** outlive the backing [`FfiSlice`]. |
351 | |
352 | pub unsafe |
353 | let heap_backing: *const = unsafe ; |
354 | |
355 | let heap_backing = unsafe ; |
356 | |
357 | Self |
358 | count: heap_backing.count, |
359 | slice: heap_backing.slice, |
360 | _marker: PhantomData, |
361 | |
362 | |
363 | |
364 | |
365 | |
366 | |
367 | use PhantomData; |
368 | |
369 | use crate:: |
370 | , Ffi, FfiSlice, FfiSliceMut, FfiSliceRef, FfiValue, FfiValueMut, | SliceBacking
371 | FfiValueRef, |
372 | ; |
373 | |
374 | |
375 | |
376 | _lifetime: , |
377 | slice: , |
378 | |
379 | |
380 | |
381 | |
382 | |
383 | FfiSliceRef |
384 | inner: &self.slice as *const _ as *const , |
385 | _type_marker: PhantomData, |
386 | _abi_marker: PhantomData, |
387 | |
388 | |
389 | |
390 | |
391 | |
392 | FfiSliceMut |
393 | inner: &mut self.slice as *mut _ as *mut , |
394 | _type_marker: PhantomData, |
395 | _abi_marker: PhantomData, |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | /// Creates a stack pinned slice guard from a borrowed slice. |
402 | /// |
403 | /// # SAFETY |
404 | /// This function itself isn't "unsafe" but other code will become unsafe if the `slice` |
405 | /// becomes invalid or moves. You'd have to violate safety rules somewhere else to do that, |
406 | /// though. |
407 | |
408 | pub unsafe |
409 | Self |
410 | _lifetime: PhantomData, |
411 | slice: from_raw |
412 | u64 try_from .unwrap, |
413 | slice.as_ptr as *const , |
414 | , |
415 | |
416 | |
417 | |
418 | |
419 | |
420 | value_ref: &'v T, |
421 | |
422 | |
423 | |
424 | /// Grants a reference to the pinned value. |
425 | /// |
426 | /// # SAFETY |
427 | /// - The granted reference **must not** outlive the lifetime of `&self`. |
428 | /// - There **must not** be a mutable reference created or mutable dereference performed during the lifetime of the [`FfiValueRef`]. |
429 | |
430 | pub unsafe |
431 | Ffi |
432 | inner: self.value_ref as *const _ as *const , |
433 | _type_marker: PhantomData, |
434 | _abi_marker: PhantomData, |
435 | |
436 | |
437 | |
438 | |
439 | |
440 | |
441 | pub |
442 | Self |
443 | |
444 | |
445 | |
446 | |
447 | _lifetime: , |
448 | slice: , |
449 | |
450 | |
451 | |
452 | /// Creates a pin guard from a heap placed slice. |
453 | /// |
454 | /// # SAFETY |
455 | /// The `slice` **must not** be moved and **must not** have a mutable reference given during the lifetime |
456 | /// of the returned [`HeapPinnedSlice`] guard. |
457 | |
458 | pub unsafe |
459 | Self |
460 | _lifetime: PhantomData, |
461 | slice: from_heap, |
462 | |
463 | |
464 | |
465 | pub unsafe |
466 | FfiSliceRef |
467 | inner: &self.slice as *const _ as *const , |
468 | _type_marker: PhantomData, |
469 | _abi_marker: PhantomData, |
470 | |
471 | |
472 | |
473 | pub unsafe |
474 | FfiSliceMut |
475 | inner: &mut self.slice as *mut _ as *mut , |
476 | _type_marker: PhantomData, |
477 | _abi_marker: PhantomData, |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | value: &'v , |
485 | |
486 | |
487 | |
488 | |
489 | pub unsafe |
490 | Self |
491 | |
492 | |
493 | |
494 | pub unsafe |
495 | FfiValueRef |
496 | inner: self.value.inner, |
497 | _type_marker: PhantomData, |
498 | _abi_marker: PhantomData, |
499 | |
500 | |
501 | |
502 | |
503 | pub unsafe |
504 | FfiValueMut |
505 | inner: self.value.inner, |
506 | _type_marker: PhantomData, |
507 | _abi_marker: PhantomData, |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | ; |
515 | |
516 | ; |
517 | |
518 | ; |
519 | |
520 | ; |
521 | |
522 | ; |
523 | |
524 | ; |
525 | |
526 | |
527 | |
528 | type Pinned: ?Sized + 'p; |
529 | |
530 | ; |
531 | |
532 | |
533 | |
534 | type Pinned = ; |
535 | |
536 | |
537 | |
538 | unsafe |
539 | |
540 | |
541 | |
542 | |
543 | type Pinned = ; |
544 | |
545 | |
546 | |
547 | from_raw |
548 | |
549 | |
550 |