1 #![stable(feature = "", since = "1.30.0")]
2 #![allow(non_camel_case_types)]
4 //! Utilities related to foreign function interface (FFI) bindings.
7 use crate::marker::PhantomData;
8 use crate::ops::{Deref, DerefMut};
10 /// Equivalent to C's `void` type when used as a [pointer].
12 /// In essence, `*const c_void` is equivalent to C's `const void*`
13 /// and `*mut c_void` is equivalent to C's `void*`. That said, this is
14 /// *not* the same as C's `void` return type, which is Rust's `()` type.
16 /// To model pointers to opaque types in FFI, until `extern type` is
17 /// stabilized, it is recommended to use a newtype wrapper around an empty
18 /// byte array. See the [Nomicon] for details.
20 /// One could use `std::os::raw::c_void` if they want to support old Rust
21 /// compiler down to 1.1.0. After Rust 1.30.0, it was re-exported by
22 /// this definition. For more information, please read [RFC 2521].
24 /// [Nomicon]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
25 /// [RFC 2521]: https://github.com/rust-lang/rfcs/blob/master/text/2521-c_void-reunification.md
26 // N.B., for LLVM to recognize the void pointer type and by extension
27 // functions like malloc(), we need to have it represented as i8* in
28 // LLVM bitcode. The enum used here ensures this and prevents misuse
29 // of the "raw" type by only having private variants. We need two
30 // variants, because the compiler complains about the repr attribute
31 // otherwise and we need at least one variant as otherwise the enum
32 // would be uninhabited and at least dereferencing such pointers would
35 #[stable(feature = "core_c_void", since = "1.30.0")]
38 feature = "c_void_variant",
39 reason = "temporary implementation detail",
45 feature = "c_void_variant",
46 reason = "temporary implementation detail",
53 #[stable(feature = "std_debug", since = "1.16.0")]
54 impl fmt::Debug for c_void {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 f.debug_struct("c_void").finish()
60 /// Basic implementation of a `va_list`.
61 // The name is WIP, using `VaListImpl` for now.
63 all(not(target_arch = "aarch64"), not(target_arch = "powerpc"), not(target_arch = "x86_64")),
64 all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")),
65 target_arch = "wasm32",
66 target_arch = "asmjs",
71 feature = "c_variadic",
72 reason = "the `c_variadic` feature has not been properly tested on \
73 all supported platforms",
77 pub struct VaListImpl<'f> {
80 // Invariant over `'f`, so each `VaListImpl<'f>` object is tied to
81 // the region of the function it's defined in
82 _marker: PhantomData<&'f mut &'f c_void>,
86 all(not(target_arch = "aarch64"), not(target_arch = "powerpc"), not(target_arch = "x86_64")),
87 all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")),
88 target_arch = "wasm32",
89 target_arch = "asmjs",
93 feature = "c_variadic",
94 reason = "the `c_variadic` feature has not been properly tested on \
95 all supported platforms",
98 impl<'f> fmt::Debug for VaListImpl<'f> {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 write!(f, "va_list* {:p}", self.ptr)
104 /// AArch64 ABI implementation of a `va_list`. See the
105 /// [AArch64 Procedure Call Standard] for more details.
107 /// [AArch64 Procedure Call Standard]:
108 /// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055b/IHI0055B_aapcs64.pdf
110 target_arch = "aarch64",
111 not(any(target_os = "macos", target_os = "ios")),
117 feature = "c_variadic",
118 reason = "the `c_variadic` feature has not been properly tested on \
119 all supported platforms",
123 pub struct VaListImpl<'f> {
129 _marker: PhantomData<&'f mut &'f c_void>,
132 /// PowerPC ABI implementation of a `va_list`.
133 #[cfg(all(target_arch = "powerpc", not(windows)))]
137 feature = "c_variadic",
138 reason = "the `c_variadic` feature has not been properly tested on \
139 all supported platforms",
143 pub struct VaListImpl<'f> {
147 overflow_arg_area: *mut c_void,
148 reg_save_area: *mut c_void,
149 _marker: PhantomData<&'f mut &'f c_void>,
152 /// x86_64 ABI implementation of a `va_list`.
153 #[cfg(all(target_arch = "x86_64", not(windows)))]
157 feature = "c_variadic",
158 reason = "the `c_variadic` feature has not been properly tested on \
159 all supported platforms",
163 pub struct VaListImpl<'f> {
166 overflow_arg_area: *mut c_void,
167 reg_save_area: *mut c_void,
168 _marker: PhantomData<&'f mut &'f c_void>,
171 /// A wrapper for a `va_list`
175 feature = "c_variadic",
176 reason = "the `c_variadic` feature has not been properly tested on \
177 all supported platforms",
180 pub struct VaList<'a, 'f: 'a> {
183 not(target_arch = "aarch64"),
184 not(target_arch = "powerpc"),
185 not(target_arch = "x86_64")
187 all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")),
188 target_arch = "wasm32",
189 target_arch = "asmjs",
192 inner: VaListImpl<'f>,
195 any(target_arch = "aarch64", target_arch = "powerpc", target_arch = "x86_64"),
196 any(not(target_arch = "aarch64"), not(any(target_os = "macos", target_os = "ios"))),
197 not(target_arch = "wasm32"),
198 not(target_arch = "asmjs"),
201 inner: &'a mut VaListImpl<'f>,
203 _marker: PhantomData<&'a mut VaListImpl<'f>>,
207 all(not(target_arch = "aarch64"), not(target_arch = "powerpc"), not(target_arch = "x86_64")),
208 all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")),
209 target_arch = "wasm32",
210 target_arch = "asmjs",
214 feature = "c_variadic",
215 reason = "the `c_variadic` feature has not been properly tested on \
216 all supported platforms",
219 impl<'f> VaListImpl<'f> {
220 /// Convert a `VaListImpl` into a `VaList` that is binary-compatible with C's `va_list`.
222 pub fn as_va_list<'a>(&'a mut self) -> VaList<'a, 'f> {
223 VaList { inner: VaListImpl { ..*self }, _marker: PhantomData }
228 any(target_arch = "aarch64", target_arch = "powerpc", target_arch = "x86_64"),
229 any(not(target_arch = "aarch64"), not(any(target_os = "macos", target_os = "ios"))),
230 not(target_arch = "wasm32"),
231 not(target_arch = "asmjs"),
235 feature = "c_variadic",
236 reason = "the `c_variadic` feature has not been properly tested on \
237 all supported platforms",
240 impl<'f> VaListImpl<'f> {
241 /// Convert a `VaListImpl` into a `VaList` that is binary-compatible with C's `va_list`.
243 pub fn as_va_list<'a>(&'a mut self) -> VaList<'a, 'f> {
244 VaList { inner: self, _marker: PhantomData }
249 feature = "c_variadic",
250 reason = "the `c_variadic` feature has not been properly tested on \
251 all supported platforms",
254 impl<'a, 'f: 'a> Deref for VaList<'a, 'f> {
255 type Target = VaListImpl<'f>;
258 fn deref(&self) -> &VaListImpl<'f> {
264 feature = "c_variadic",
265 reason = "the `c_variadic` feature has not been properly tested on \
266 all supported platforms",
269 impl<'a, 'f: 'a> DerefMut for VaList<'a, 'f> {
271 fn deref_mut(&mut self) -> &mut VaListImpl<'f> {
276 // The VaArgSafe trait needs to be used in public interfaces, however, the trait
277 // itself must not be allowed to be used outside this module. Allowing users to
278 // implement the trait for a new type (thereby allowing the va_arg intrinsic to
279 // be used on a new type) is likely to cause undefined behavior.
281 // FIXME(dlrobertson): In order to use the VaArgSafe trait in a public interface
282 // but also ensure it cannot be used elsewhere, the trait needs to be public
283 // within a private module. Once RFC 2145 has been implemented look into
286 /// Trait which permits the allowed types to be used with [super::VaListImpl::arg].
288 feature = "c_variadic",
289 reason = "the `c_variadic` feature has not been properly tested on \
290 all supported platforms",
293 pub trait VaArgSafe {}
296 macro_rules! impl_va_arg_safe {
299 #[unstable(feature = "c_variadic",
300 reason = "the `c_variadic` feature has not been properly tested on \
301 all supported platforms",
303 impl sealed_trait::VaArgSafe for $t {}
308 impl_va_arg_safe! {i8, i16, i32, i64, usize}
309 impl_va_arg_safe! {u8, u16, u32, u64, isize}
310 impl_va_arg_safe! {f64}
313 feature = "c_variadic",
314 reason = "the `c_variadic` feature has not been properly tested on \
315 all supported platforms",
318 impl<T> sealed_trait::VaArgSafe for *mut T {}
320 feature = "c_variadic",
321 reason = "the `c_variadic` feature has not been properly tested on \
322 all supported platforms",
325 impl<T> sealed_trait::VaArgSafe for *const T {}
328 feature = "c_variadic",
329 reason = "the `c_variadic` feature has not been properly tested on \
330 all supported platforms",
333 impl<'f> VaListImpl<'f> {
334 /// Advance to the next arg.
336 pub unsafe fn arg<T: sealed_trait::VaArgSafe>(&mut self) -> T {
337 // SAFETY: the caller must uphold the safety contract for `va_arg`.
338 unsafe { va_arg(self) }
341 /// Copies the `va_list` at the current location.
342 pub unsafe fn with_copy<F, R>(&self, f: F) -> R
344 F: for<'copy> FnOnce(VaList<'copy, 'f>) -> R,
346 let mut ap = self.clone();
347 let ret = f(ap.as_va_list());
348 // SAFETY: the caller must uphold the safety contract for `va_end`.
357 feature = "c_variadic",
358 reason = "the `c_variadic` feature has not been properly tested on \
359 all supported platforms",
362 impl<'f> Clone for VaListImpl<'f> {
364 fn clone(&self) -> Self {
365 let mut dest = crate::mem::MaybeUninit::uninit();
366 // SAFETY: we write to the `MaybeUninit`, thus it is initialized and `assume_init` is legal
368 va_copy(dest.as_mut_ptr(), self);
375 feature = "c_variadic",
376 reason = "the `c_variadic` feature has not been properly tested on \
377 all supported platforms",
380 impl<'f> Drop for VaListImpl<'f> {
382 // FIXME: this should call `va_end`, but there's no clean way to
383 // guarantee that `drop` always gets inlined into its caller,
384 // so the `va_end` would get directly called from the same function as
385 // the corresponding `va_copy`. `man va_end` states that C requires this,
386 // and LLVM basically follows the C semantics, so we need to make sure
387 // that `va_end` is always called from the same function as `va_copy`.
388 // For more details, see https://github.com/rust-lang/rust/pull/59625
389 // and https://llvm.org/docs/LangRef.html#llvm-va-end-intrinsic.
391 // This works for now, since `va_end` is a no-op on all current LLVM targets.
395 extern "rust-intrinsic" {
396 /// Destroy the arglist `ap` after initialization with `va_start` or
398 fn va_end(ap: &mut VaListImpl<'_>);
400 /// Copies the current location of arglist `src` to the arglist `dst`.
401 fn va_copy<'f>(dest: *mut VaListImpl<'f>, src: &VaListImpl<'f>);
403 /// Loads an argument of type `T` from the `va_list` `ap` and increment the
404 /// argument `ap` points to.
405 fn va_arg<T: sealed_trait::VaArgSafe>(ap: &mut VaListImpl<'_>) -> T;