-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathffi.rs
More file actions
307 lines (277 loc) · 10.3 KB
/
ffi.rs
File metadata and controls
307 lines (277 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//! `#[repr(C)]` types shared across the TDL package / task executor C-FFI boundary.
//!
//! Both sides of the boundary live in the same process and share the same Rust global allocator,
//! so buffers allocated on one side can be reclaimed on the other via [`Box::into_raw`] /
//! [`Box::from_raw`].
use std::{ffi::c_char, fmt, marker::PhantomData, mem::ManuallyDrop, ops::Deref, str::Utf8Error};
/// Borrowed, C-ABI-compatible view of a contiguous slice `&'borrow_lifetime [ElementType]`.
///
/// [`CArray`] implements [`Deref<Target = [ElementType]>`][Deref] so that all slice methods
/// are available directly, without requiring a conversion call.
///
/// # Type Parameters
///
/// * `'borrow_lifetime` - The lifetime of the borrowed slice.
/// * `ElementType` - The type of the element inside the slice.
#[repr(C)]
pub struct CArray<'borrow_lifetime, ElementType> {
pointer: *const ElementType,
length: usize,
_lifetime: PhantomData<&'borrow_lifetime [ElementType]>,
}
// Manual `Copy`/`Clone` impls avoid the auto-derived `ElementType: Copy` / `ElementType: Clone`
// bounds: a borrowed pointer/length pair is always trivially copyable regardless of the element
// type.
impl<ElementType> Copy for CArray<'_, ElementType> {}
impl<ElementType> Clone for CArray<'_, ElementType> {
fn clone(&self) -> Self {
*self
}
}
impl<ElementType> fmt::Debug for CArray<'_, ElementType> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CArray")
.field("pointer", &self.pointer)
.field("length", &self.length)
.finish()
}
}
impl<'borrow_lifetime, ElementType> CArray<'borrow_lifetime, ElementType> {
/// Borrows `slice` as a C-ABI array view.
///
/// The returned [`CArray`] is tied to the lifetime of `slice`; the pointer remains valid as
/// long as the original slice is not moved or dropped.
///
/// # Returns
///
/// The constructed C array from the given slice.
#[must_use]
pub const fn from_slice(slice: &'borrow_lifetime [ElementType]) -> Self {
Self {
pointer: slice.as_ptr(),
length: slice.len(),
_lifetime: PhantomData,
}
}
/// Reconstructs a Rust slice from the raw pointer and length.
///
/// # Returns
///
/// A slice of `length` elements starting at `pointer`.
///
/// # Safety
///
/// The caller must guarantee that:
///
/// * `pointer` points to a single, contiguous allocation of at least `length` elements of
/// `ElementType`, properly initialized.
/// * The memory remains valid and immutable for the returned lifetime.
#[must_use]
pub const fn as_slice(&self) -> &'borrow_lifetime [ElementType] {
unsafe { std::slice::from_raw_parts(self.pointer, self.length) }
}
}
impl<ElementType> Deref for CArray<'_, ElementType> {
type Target = [ElementType];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
/// Borrowed view of a UTF-8 string as a `char`-typed C array.
pub type CCharArray<'borrow_lifetime> = CArray<'borrow_lifetime, c_char>;
impl<'borrow_lifetime> CCharArray<'borrow_lifetime> {
/// Borrows UTF8-encoded string `s` as a C-ABI char array view.
///
/// The returned view is **not** NUL-terminated; both sides of the FFI boundary rely on the
/// explicit `length` field rather than a terminator.
///
/// Equivalent to `std::string_view` in C++.
///
/// # Returns
///
/// The constructed C char array from the given `&str`.
#[must_use]
pub const fn from_utf8(s: &'borrow_lifetime str) -> Self {
Self {
pointer: s.as_ptr().cast::<c_char>(),
length: s.len(),
_lifetime: PhantomData,
}
}
/// Reconstructs a UTF8-encoded Rust `&str` from the raw pointer and length.
///
/// # Returns
///
/// A `&str` view of the underlying C char array on success.
///
/// # Errors
///
/// Returns an error if:
///
/// * Forwards [`str::from_utf8`]'s return values on failure.
pub const fn as_utf8(&self) -> Result<&'borrow_lifetime str, Utf8Error> {
let bytes: &[u8] =
unsafe { std::slice::from_raw_parts(self.pointer.cast::<u8>(), self.length) };
str::from_utf8(bytes)
}
}
/// Borrowed view of a raw byte buffer.
pub type CByteArray<'borrow_lifetime> = CArray<'borrow_lifetime, u8>;
/// Owned, C-ABI-compatible result buffer returned from a TDL task execution.
///
/// The buffer is allocated on the TDL-package side by leaking a `Box<[u8]>` via [`Box::into_raw`]
/// and reclaimed on the executor side via [`Box::from_raw`]. This only works because both sides
/// share the same global allocator, which is true when the package is loaded via `dlopen` into
/// the executor process.
#[repr(C)]
pub struct TaskExecutionResult {
is_error: bool,
pointer: *mut u8,
length: usize,
}
impl TaskExecutionResult {
/// Constructs a successful result wrapping wire-format-encoded output bytes.
#[must_use]
pub fn from_outputs(bytes: Vec<u8>) -> Self {
Self::from_buffer(false, bytes)
}
/// Constructs a failing result wrapping msgpack-encoded [`TdlError`](crate::TdlError) bytes.
#[must_use]
pub fn from_error(bytes: Vec<u8>) -> Self {
Self::from_buffer(true, bytes)
}
/// Reclaims ownership of the leaked buffer and returns it.
///
/// # Returns
///
/// `Ok(bytes)` on success, where `bytes` is the wire-format output payload produced by the
/// user task.
///
/// # Errors
///
/// Returns `Err(bytes)` if the result represented failure, where `bytes` is a msgpack-encoded
/// [`crate::TdlError`] produced inside the TDL package. The caller is responsible for decoding
/// it via [`rmp_serde::from_slice`].
///
/// # Safety
///
/// The caller must guarantee that `self.pointer` / `self.length` originated from a prior call
/// to [`Self::from_outputs`] or [`Self::from_error`] in a component that shares this process's
/// global allocator.
pub fn into_result(self) -> Result<Vec<u8>, Vec<u8>> {
// Prevent the destructor from running after we reconstruct the `Box`.
let this = ManuallyDrop::new(self);
let boxed: Box<[u8]> = unsafe {
Box::from_raw(std::ptr::slice_from_raw_parts_mut(
this.pointer,
this.length,
))
};
let vec = boxed.into_vec();
if this.is_error { Err(vec) } else { Ok(vec) }
}
fn from_buffer(is_error: bool, buffer: Vec<u8>) -> Self {
let boxed: Box<[u8]> = buffer.into_boxed_slice();
let length = boxed.len();
let pointer = Box::into_raw(boxed).cast::<u8>();
Self {
is_error,
pointer,
length,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TdlError;
#[test]
fn c_byte_array_round_trip() {
let data: [u8; 5] = [1, 2, 3, 4, 5];
let view = CByteArray::from_slice(&data);
assert_eq!(view.len(), 5);
assert!(!view.is_empty());
let reconstructed = view.as_slice();
assert_eq!(reconstructed, &data[..]);
}
#[test]
fn c_byte_array_deref_to_slice() {
let data: [u8; 4] = [10, 20, 30, 40];
let view = CByteArray::from_slice(&data);
// Slice methods are available directly via `Deref`.
assert_eq!(view.iter().sum::<u8>(), 100);
assert_eq!(view[1], 20);
assert_eq!(&view[..2], &[10, 20]);
}
#[test]
fn c_byte_array_deserializes_msgpack_via_deref() -> anyhow::Result<()> {
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct Point {
x: i32,
y: i32,
}
let original = Point { x: -7, y: 42 };
let encoded = rmp_serde::to_vec(&original)?;
let view = CByteArray::from_slice(&encoded);
// `&*view` triggers `Deref`, yielding `&[u8]` directly — no `as_slice()` call.
let decoded: Point = rmp_serde::from_slice(&view)?;
assert_eq!(decoded, original);
Ok(())
}
#[test]
fn c_byte_array_empty() {
let data: [u8; 0] = [];
let view = CByteArray::from_slice(&data);
assert_eq!(view.len(), 0);
assert!(view.is_empty());
}
#[test]
fn c_char_array_round_trip() -> anyhow::Result<()> {
let original = "hello, TDL";
let view = CCharArray::from_utf8(original);
assert_eq!(view.len(), original.len());
let reconstructed = view.as_utf8()?;
assert_eq!(reconstructed, original);
Ok(())
}
#[test]
fn c_char_array_invalid_utf8() {
// 0xFF is never valid in any position of a UTF-8 sequence.
let invalid_bytes: &[u8] = &[0x68, 0x65, 0xff, 0x6c, 0x6f];
// Reinterpret as `&[c_char]` to go through `CArray::from_slice` rather than `from_utf8`.
let c_chars: &[std::ffi::c_char] = unsafe {
std::slice::from_raw_parts(invalid_bytes.as_ptr().cast(), invalid_bytes.len())
};
let view = CCharArray::from_slice(c_chars);
assert_eq!(view.len(), 5);
assert!(view.as_utf8().is_err());
}
#[test]
fn task_execution_result_success_round_trip() {
let payload = vec![10u8, 20, 30, 40];
let expected = payload.clone();
let result = TaskExecutionResult::from_outputs(payload);
let reclaimed = result.into_result();
assert_eq!(reclaimed, Ok(expected));
}
#[test]
fn task_execution_result_error_round_trip() -> anyhow::Result<()> {
let error = TdlError::Custom("custom task execution error".to_owned());
let payload = rmp_serde::to_vec(&error)?;
let result = TaskExecutionResult::from_error(payload);
let reclaimed = result.into_result();
if let Err(payload) = reclaimed {
let decoded: TdlError = rmp_serde::from_slice(&payload)?;
assert_eq!(decoded.to_string(), error.to_string());
} else {
panic!("reclaimed payload did not match original");
}
Ok(())
}
#[test]
fn task_execution_result_empty_buffer() {
let result = TaskExecutionResult::from_outputs(Vec::new());
let reclaimed = result.into_result();
assert_eq!(reclaimed, Ok(Vec::new()));
}
}