| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use crate::{Handle, RustBuffer, RustCallStatus, RustCallStatusCode}; |
| 6 |
use std::{mem::ManuallyDrop, ptr::NonNull}; |
| 7 |
|
| 8 |
/// FFIBuffer element |
| 9 |
/// |
| 10 |
/// This is the union of all possible primitive FFI types. |
| 11 |
/// Composite FFI types like `RustBuffer` and `RustCallStatus` are stored using multiple elements. |
| 12 |
#[repr(C)] |
| 13 |
#[derive(Clone, Copy)] |
| 14 |
pub union FfiBufferElement { |
| 15 |
pub u8: u8, |
| 16 |
pub i8: i8, |
| 17 |
pub u16: u16, |
| 18 |
pub i16: i16, |
| 19 |
pub u32: u32, |
| 20 |
pub i32: i32, |
| 21 |
pub u64: u64, |
| 22 |
pub i64: i64, |
| 23 |
pub float: std::ffi::c_float, |
| 24 |
pub double: std::ffi::c_double, |
| 25 |
pub ptr: *const std::ffi::c_void, |
| 26 |
} |
| 27 |
|
| 28 |
impl Default for FfiBufferElement { |
| 29 |
fn default() -> Self { |
| 30 |
Self { u64: 0 } |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/// Serialize a FFI value to a buffer |
| 35 |
/// |
| 36 |
/// This trait allows FFI types to be read from/written to FFIBufferElement slices. |
| 37 |
/// It's similar, to the [crate::Lift::read] and [crate::Lower::write] methods, but implemented on the FFI types rather than Rust types. |
| 38 |
/// It's useful to compare the two: |
| 39 |
/// |
| 40 |
/// - [crate::Lift] and [crate::Lower] are implemented on Rust types like String and user-defined records. |
| 41 |
/// - [FfiSerialize] is implemented on the FFI types like RustBuffer, RustCallStatus, and vtable structs. |
| 42 |
/// - All 3 traits are implemented for simple cases where the FFI type and Rust type are the same, for example numeric types. |
| 43 |
/// - [FfiSerialize] uses FFIBuffer elements rather than u8 elements. Using a union eliminates the need to cast values and creates better alignment. |
| 44 |
/// - [FfiSerialize] uses a constant size to store each type. |
| 45 |
/// |
| 46 |
/// [FfiSerialize] is used to generate alternate forms of the scaffolding functions that simplify work needed to implement the bindings on the other side. |
| 47 |
/// This is currently only used in the gecko-js bindings for Firefox, but could maybe be useful for other external bindings or even some of the builtin bindings like Python/Kotlin. |
| 48 |
/// |
| 49 |
/// The FFI-buffer version of the scaffolding functions: |
| 50 |
/// - Input two pointers to ffi buffers, one to read arguments from and one to write the return value to. |
| 51 |
/// - Rather than inputting an out pointer for `RustCallStatus` it's written to the return buffer after the normal return value. |
| 52 |
/// |
| 53 |
pub trait FfiSerialize: Sized { |
| 54 |
/// Number of elements required to store this FFI type |
| 55 |
const SIZE: usize; |
| 56 |
|
| 57 |
/// Get a value from a ffi buffer |
| 58 |
/// |
| 59 |
/// Note: `buf` should be thought of as `&[FFIBufferElement; Self::SIZE]`, but it can't be spelled out that way |
| 60 |
/// since Rust doesn't support that usage of const generics yet. |
| 61 |
fn get(buf: &[FfiBufferElement]) -> Self; |
| 62 |
|
| 63 |
/// Put a value to a ffi buffer |
| 64 |
/// |
| 65 |
/// Note: `buf` should be thought of as `&[FFIBufferElement; Self::SIZE]`, but it can't be spelled out that way |
| 66 |
/// since Rust doesn't support that usage of const generics yet. |
| 67 |
fn put(buf: &mut [FfiBufferElement], value: Self); |
| 68 |
|
| 69 |
/// Read a value from a ffi buffer ref and advance it |
| 70 |
/// |
| 71 |
/// buf must have a length of at least `Self::Size` |
| 72 |
fn read(buf: &mut &[FfiBufferElement]) -> Self { |
| 73 |
let value = Self::get(buf); |
| 74 |
*buf = &buf[Self::SIZE..]; |
| 75 |
value |
| 76 |
} |
| 77 |
|
| 78 |
/// Write a value to a ffi buffer ref and advance it |
| 79 |
/// |
| 80 |
/// buf must have a length of at least `Self::Size` |
| 81 |
fn write(buf: &mut &mut [FfiBufferElement], value: Self) { |
| 82 |
Self::put(buf, value); |
| 83 |
// Lifetime dance taken from `bytes::BufMut` |
| 84 |
let (_, new_buf) = ::core::mem::take(buf).split_at_mut(Self::SIZE); |
| 85 |
*buf = new_buf; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/// Get the FFI buffer size for list of types |
| 90 |
#[macro_export] |
| 91 |
macro_rules! ffi_buffer_size { |
| 92 |
($($T:ty),* $(,)?) => { |
| 93 |
( |
| 94 |
0 |
| 95 |
$( |
| 96 |
+ <$T as $crate::FfiSerialize>::SIZE |
| 97 |
)* |
| 98 |
) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
macro_rules! define_ffi_serialize_simple_cases { |
| 103 |
($(($name: ident, $T:ty)),* $(,)?) => { |
| 104 |
$( |
| 105 |
impl FfiSerialize for $T { |
| 106 |
const SIZE: usize = 1; |
| 107 |
|
| 108 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 109 |
// Safety: the foreign bindings are responsible for sending us the correct data. |
| 110 |
unsafe { buf[0].$name } |
| 111 |
} |
| 112 |
|
| 113 |
fn put(buf: &mut[FfiBufferElement], value: Self) { |
| 114 |
buf[0].$name = value |
| 115 |
} |
| 116 |
} |
| 117 |
)* |
| 118 |
}; |
| 119 |
} |
| 120 |
|
| 121 |
define_ffi_serialize_simple_cases! { |
| 122 |
(i8, i8), |
| 123 |
(u8, u8), |
| 124 |
(i16, i16), |
| 125 |
(u16, u16), |
| 126 |
(i32, i32), |
| 127 |
(u32, u32), |
| 128 |
(i64, i64), |
| 129 |
(u64, u64), |
| 130 |
(ptr, *const std::ffi::c_void), |
| 131 |
} |
| 132 |
|
| 133 |
impl FfiSerialize for f32 { |
| 134 |
const SIZE: usize = 1; |
| 135 |
|
| 136 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 137 |
// Safety: the foreign bindings are responsible for sending us the correct data. |
| 138 |
unsafe { buf[0].float as Self } |
| 139 |
} |
| 140 |
|
| 141 |
fn put(buf: &mut [FfiBufferElement], value: Self) { |
| 142 |
// Use a cast since it's theoretically possible for float to not be f32 on some systems. |
| 143 |
buf[0].float = value as std::ffi::c_float; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl FfiSerialize for f64 { |
| 148 |
const SIZE: usize = 1; |
| 149 |
|
| 150 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 151 |
// Safety: the foreign bindings are responsible for sending us the correct data. |
| 152 |
unsafe { buf[0].double as Self } |
| 153 |
} |
| 154 |
|
| 155 |
fn put(buf: &mut [FfiBufferElement], value: Self) { |
| 156 |
// Use a cast since it's theoretically possible for double to not be f64 on some systems. |
| 157 |
buf[0].double = value as std::ffi::c_double; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl FfiSerialize for bool { |
| 162 |
const SIZE: usize = 1; |
| 163 |
|
| 164 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 165 |
// Safety: the foreign bindings are responsible for sending us the correct data. |
| 166 |
unsafe { buf[0].i8 == 1 } |
| 167 |
} |
| 168 |
|
| 169 |
fn put(buf: &mut [FfiBufferElement], value: Self) { |
| 170 |
buf[0].i8 = if value { 1 } else { 0 } |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
impl FfiSerialize for () { |
| 175 |
const SIZE: usize = 0; |
| 176 |
|
| 177 |
fn get(_buf: &[FfiBufferElement]) -> Self {} |
| 178 |
|
| 179 |
fn put(_buf: &mut [FfiBufferElement], _value: Self) {} |
| 180 |
} |
| 181 |
|
| 182 |
impl<T> FfiSerialize for NonNull<T> { |
| 183 |
const SIZE: usize = 1; |
| 184 |
|
| 185 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 186 |
// Safety: this relies on the foreign code passing us valid pointers |
| 187 |
unsafe { Self::new_unchecked(buf[0].ptr as *mut T) } |
| 188 |
} |
| 189 |
|
| 190 |
fn put(buf: &mut [FfiBufferElement], value: Self) { |
| 191 |
buf[0].ptr = value.as_ptr() as *const std::ffi::c_void |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
impl FfiSerialize for Handle { |
| 196 |
const SIZE: usize = 1; |
| 197 |
|
| 198 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 199 |
unsafe { Handle::from_raw_unchecked(buf[0].u64) } |
| 200 |
} |
| 201 |
|
| 202 |
fn put(buf: &mut [FfiBufferElement], value: Self) { |
| 203 |
buf[0].u64 = value.as_raw() |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
impl FfiSerialize for RustBuffer { |
| 208 |
const SIZE: usize = 3; |
| 209 |
|
| 210 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 211 |
// Safety: the foreign bindings are responsible for sending us the correct data. |
| 212 |
let (capacity, len, data) = unsafe { (buf[0].u64, buf[1].u64, buf[2].ptr as *mut u8) }; |
| 213 |
unsafe { RustBuffer::from_raw_parts(data, len, capacity) } |
| 214 |
} |
| 215 |
|
| 216 |
fn put(buf: &mut [FfiBufferElement], value: Self) { |
| 217 |
buf[0].u64 = value.capacity; |
| 218 |
buf[1].u64 = value.len; |
| 219 |
buf[2].ptr = value.data as *const std::ffi::c_void; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
impl FfiSerialize for RustCallStatus { |
| 224 |
const SIZE: usize = 4; |
| 225 |
|
| 226 |
fn get(buf: &[FfiBufferElement]) -> Self { |
| 227 |
// Safety: the foreign bindings are responsible for sending us the correct data. |
| 228 |
let code = unsafe { buf[0].i8 }; |
| 229 |
Self { |
| 230 |
code: RustCallStatusCode::try_from(code).unwrap_or(RustCallStatusCode::UnexpectedError), |
| 231 |
error_buf: ManuallyDrop::new(RustBuffer::get(&buf[1..])), |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
fn put(buf: &mut [FfiBufferElement], value: Self) { |
| 236 |
buf[0].i8 = value.code as i8; |
| 237 |
RustBuffer::put(&mut buf[1..], ManuallyDrop::into_inner(value.error_buf)) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#[cfg(test)] |
| 242 |
mod test { |
| 243 |
use super::*; |
| 244 |
use crate::{Handle, RustBuffer, RustCallStatus, RustCallStatusCode}; |
| 245 |
|
| 246 |
#[test] |
| 247 |
fn test_ffi_buffer_size() { |
| 248 |
assert_eq!(ffi_buffer_size!(u8), 1); |
| 249 |
assert_eq!(ffi_buffer_size!(i8), 1); |
| 250 |
assert_eq!(ffi_buffer_size!(u16), 1); |
| 251 |
assert_eq!(ffi_buffer_size!(i16), 1); |
| 252 |
assert_eq!(ffi_buffer_size!(u32), 1); |
| 253 |
assert_eq!(ffi_buffer_size!(i32), 1); |
| 254 |
assert_eq!(ffi_buffer_size!(u64), 1); |
| 255 |
assert_eq!(ffi_buffer_size!(i64), 1); |
| 256 |
assert_eq!(ffi_buffer_size!(f32), 1); |
| 257 |
assert_eq!(ffi_buffer_size!(f64), 1); |
| 258 |
assert_eq!(ffi_buffer_size!(bool), 1); |
| 259 |
assert_eq!(ffi_buffer_size!(*const std::ffi::c_void), 1); |
| 260 |
assert_eq!(ffi_buffer_size!(RustBuffer), 3); |
| 261 |
assert_eq!(ffi_buffer_size!(RustCallStatus), 4); |
| 262 |
assert_eq!(ffi_buffer_size!(Handle), 1); |
| 263 |
assert_eq!(ffi_buffer_size!(()), 0); |
| 264 |
|
| 265 |
assert_eq!(ffi_buffer_size!(u8, f32, bool, Handle, (), RustBuffer), 7); |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
fn test_ffi_serialize() { |
| 270 |
let mut some_data = vec![1, 2, 3]; |
| 271 |
let void_ptr = some_data.as_mut_ptr() as *const std::ffi::c_void; |
| 272 |
let rust_buffer = unsafe { RustBuffer::from_raw_parts(some_data.as_mut_ptr(), 2, 3) }; |
| 273 |
let orig_rust_buffer_data = ( |
| 274 |
rust_buffer.data_pointer(), |
| 275 |
rust_buffer.len(), |
| 276 |
rust_buffer.capacity(), |
| 277 |
); |
| 278 |
let handle = unsafe { Handle::from_raw(101).unwrap() }; |
| 279 |
let rust_call_status = RustCallStatus::default(); |
| 280 |
let rust_call_status_error_buf = &rust_call_status.error_buf; |
| 281 |
let orig_rust_call_status_buffer_data = ( |
| 282 |
rust_call_status_error_buf.data_pointer(), |
| 283 |
rust_call_status_error_buf.len(), |
| 284 |
rust_call_status_error_buf.capacity(), |
| 285 |
); |
| 286 |
let mut buf = [FfiBufferElement::default(); 21]; |
| 287 |
let mut buf_writer = buf.as_mut_slice(); |
| 288 |
<u8 as FfiSerialize>::write(&mut buf_writer, 0); |
| 289 |
<i8 as FfiSerialize>::write(&mut buf_writer, 1); |
| 290 |
<u16 as FfiSerialize>::write(&mut buf_writer, 2); |
| 291 |
<i16 as FfiSerialize>::write(&mut buf_writer, 3); |
| 292 |
<u32 as FfiSerialize>::write(&mut buf_writer, 4); |
| 293 |
<i32 as FfiSerialize>::write(&mut buf_writer, 5); |
| 294 |
<u64 as FfiSerialize>::write(&mut buf_writer, 6); |
| 295 |
<i64 as FfiSerialize>::write(&mut buf_writer, 7); |
| 296 |
<f32 as FfiSerialize>::write(&mut buf_writer, 0.1); |
| 297 |
<f64 as FfiSerialize>::write(&mut buf_writer, 0.2); |
| 298 |
<bool as FfiSerialize>::write(&mut buf_writer, true); |
| 299 |
<*const std::ffi::c_void as FfiSerialize>::write(&mut buf_writer, void_ptr); |
| 300 |
<RustBuffer as FfiSerialize>::write(&mut buf_writer, rust_buffer); |
| 301 |
<RustCallStatus as FfiSerialize>::write(&mut buf_writer, rust_call_status); |
| 302 |
<Handle as FfiSerialize>::write(&mut buf_writer, handle); |
| 303 |
#[allow(clippy::needless_borrows_for_generic_args)] |
| 304 |
<() as FfiSerialize>::write(&mut buf_writer, ()); |
| 305 |
|
| 306 |
let mut buf_reader = buf.as_slice(); |
| 307 |
assert_eq!(<u8 as FfiSerialize>::read(&mut buf_reader), 0); |
| 308 |
assert_eq!(<i8 as FfiSerialize>::read(&mut buf_reader), 1); |
| 309 |
assert_eq!(<u16 as FfiSerialize>::read(&mut buf_reader), 2); |
| 310 |
assert_eq!(<i16 as FfiSerialize>::read(&mut buf_reader), 3); |
| 311 |
assert_eq!(<u32 as FfiSerialize>::read(&mut buf_reader), 4); |
| 312 |
assert_eq!(<i32 as FfiSerialize>::read(&mut buf_reader), 5); |
| 313 |
assert_eq!(<u64 as FfiSerialize>::read(&mut buf_reader), 6); |
| 314 |
assert_eq!(<i64 as FfiSerialize>::read(&mut buf_reader), 7); |
| 315 |
assert_eq!(<f32 as FfiSerialize>::read(&mut buf_reader), 0.1); |
| 316 |
assert_eq!(<f64 as FfiSerialize>::read(&mut buf_reader), 0.2); |
| 317 |
assert!(<bool as FfiSerialize>::read(&mut buf_reader)); |
| 318 |
assert_eq!( |
| 319 |
<*const std::ffi::c_void as FfiSerialize>::read(&mut buf_reader), |
| 320 |
void_ptr |
| 321 |
); |
| 322 |
let rust_buffer2 = <RustBuffer as FfiSerialize>::read(&mut buf_reader); |
| 323 |
assert_eq!( |
| 324 |
( |
| 325 |
rust_buffer2.data_pointer(), |
| 326 |
rust_buffer2.len(), |
| 327 |
rust_buffer2.capacity() |
| 328 |
), |
| 329 |
orig_rust_buffer_data, |
| 330 |
); |
| 331 |
|
| 332 |
let rust_call_status2 = <RustCallStatus as FfiSerialize>::read(&mut buf_reader); |
| 333 |
assert_eq!(rust_call_status2.code, RustCallStatusCode::Success); |
| 334 |
|
| 335 |
let rust_call_status2_error_buf = ManuallyDrop::into_inner(rust_call_status2.error_buf); |
| 336 |
assert_eq!( |
| 337 |
( |
| 338 |
rust_call_status2_error_buf.data_pointer(), |
| 339 |
rust_call_status2_error_buf.len(), |
| 340 |
rust_call_status2_error_buf.capacity(), |
| 341 |
), |
| 342 |
orig_rust_call_status_buffer_data |
| 343 |
); |
| 344 |
assert_eq!(<Handle as FfiSerialize>::read(&mut buf_reader), handle); |
| 345 |
// Ensure that `read` with a unit struct doesn't panic. No need to assert anything, since |
| 346 |
// the return type is (). |
| 347 |
<() as FfiSerialize>::read(&mut buf_reader); |
| 348 |
} |
| 349 |
} |