1 #![stable(feature = "", since = "1.30.0")]
3 #![allow(non_camel_case_types)]
5 //! Utilities related to FFI bindings.
8 use crate::marker::PhantomData;
9 use crate::ops::{Deref, DerefMut};
11 /// Equivalent to C's `void` type when used as a [pointer].
13 /// In essence, `*const c_void` is equivalent to C's `const void*`
14 /// and `*mut c_void` is equivalent to C's `void*`. That said, this is
15 /// *not* the same as C's `void` return type, which is Rust's `()` type.
17 /// To model pointers to opaque types in FFI, until `extern type` is
18 /// stabilized, it is recommended to use a newtype wrapper around an empty
19 /// byte array. See the [Nomicon] for details.
21 /// [pointer]: ../../std/primitive.pointer.html
22 /// [Nomicon]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
23 // N.B., for LLVM to recognize the void pointer type and by extension
24 // functions like malloc(), we need to have it represented as i8* in
25 // LLVM bitcode. The enum used here ensures this and prevents misuse
26 // of the "raw" type by only having private variants. We need two
27 // variants, because the compiler complains about the repr attribute
28 // otherwise and we need at least one variant as otherwise the enum
29 // would be uninhabited and at least dereferencing such pointers would
32 #[stable(feature = "raw_os", since = "1.1.0")]
34 #[unstable(feature = "c_void_variant", reason = "temporary implementation detail",
36 #[doc(hidden)] __variant1,
37 #[unstable(feature = "c_void_variant", reason = "temporary implementation detail",
39 #[doc(hidden)] __variant2,
42 #[stable(feature = "std_debug", since = "1.16.0")]
43 impl fmt::Debug for c_void {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 /// Basic implementation of a `va_list`.
50 // The name is WIP, using `VaListImpl` for now.
51 #[cfg(any(all(not(target_arch = "aarch64"), not(target_arch = "powerpc"),
52 not(target_arch = "x86_64"), not(target_arch = "asmjs")),
53 all(target_arch = "aarch64", target_os = "ios"),
56 #[unstable(feature = "c_variadic",
57 reason = "the `c_variadic` feature has not been properly tested on \
58 all supported platforms",
61 pub struct VaListImpl<'f> {
63 _marker: PhantomData<&'f c_void>,
66 #[cfg(any(all(not(target_arch = "aarch64"), not(target_arch = "powerpc"),
67 not(target_arch = "x86_64"), not(target_arch = "asmjs")),
68 all(target_arch = "aarch64", target_os = "ios"),
70 #[unstable(feature = "c_variadic",
71 reason = "the `c_variadic` feature has not been properly tested on \
72 all supported platforms",
74 impl<'f> fmt::Debug for VaListImpl<'f> {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 write!(f, "va_list* {:p}", self.ptr)
80 /// AArch64 ABI implementation of a `va_list`. See the
81 /// [AArch64 Procedure Call Standard] for more details.
83 /// [AArch64 Procedure Call Standard]:
84 /// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055b/IHI0055B_aapcs64.pdf
85 #[cfg(all(target_arch = "aarch64", not(target_os = "ios"), not(windows)))]
88 #[unstable(feature = "c_variadic",
89 reason = "the `c_variadic` feature has not been properly tested on \
90 all supported platforms",
93 pub struct VaListImpl<'f> {
99 _marker: PhantomData<&'f c_void>,
102 /// PowerPC ABI implementation of a `va_list`.
103 #[cfg(all(target_arch = "powerpc", not(windows)))]
106 #[unstable(feature = "c_variadic",
107 reason = "the `c_variadic` feature has not been properly tested on \
108 all supported platforms",
111 pub struct VaListImpl<'f> {
115 overflow_arg_area: *mut c_void,
116 reg_save_area: *mut c_void,
117 _marker: PhantomData<&'f c_void>,
120 /// x86_64 ABI implementation of a `va_list`.
121 #[cfg(all(target_arch = "x86_64", not(windows)))]
124 #[unstable(feature = "c_variadic",
125 reason = "the `c_variadic` feature has not been properly tested on \
126 all supported platforms",
129 pub struct VaListImpl<'f> {
132 overflow_arg_area: *mut c_void,
133 reg_save_area: *mut c_void,
134 _marker: PhantomData<&'f c_void>,
137 /// asm.js ABI implementation of a `va_list`.
138 // asm.js uses the PNaCl ABI, which specifies that a `va_list` is
139 // an array of 4 32-bit integers, according to the old PNaCl docs at
140 // https://web.archive.org/web/20130518054430/https://www.chromium.org/nativeclient/pnacl/bitcode-abi#TOC-Derived-Types
141 // and clang does the same in `CreatePNaClABIBuiltinVaListDecl` from `lib/AST/ASTContext.cpp`
142 #[cfg(all(target_arch = "asmjs", not(windows)))]
144 #[unstable(feature = "c_variadic",
145 reason = "the `c_variadic` feature has not been properly tested on \
146 all supported platforms",
149 pub struct VaListImpl<'f> {
150 inner: [crate::mem::MaybeUninit<i32>; 4],
151 _marker: PhantomData<&'f c_void>,
154 #[cfg(all(target_arch = "asmjs", not(windows)))]
155 #[unstable(feature = "c_variadic",
156 reason = "the `c_variadic` feature has not been properly tested on \
157 all supported platforms",
159 impl<'f> fmt::Debug for VaListImpl<'f> {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162 write!(f, "va_list* [{:#x}, {:#x}, {:#x}, {:#x}]",
163 self.inner[0].read(), self.inner[1].read(),
164 self.inner[2].read(), self.inner[3].read())
169 /// A wrapper for a `va_list`
172 #[unstable(feature = "c_variadic",
173 reason = "the `c_variadic` feature has not been properly tested on \
174 all supported platforms",
176 pub struct VaList<'a, 'f: 'a> {
177 #[cfg(any(all(not(target_arch = "aarch64"), not(target_arch = "powerpc"),
178 not(target_arch = "x86_64"), not(target_arch = "asmjs")),
179 all(target_arch = "aarch64", target_os = "ios"),
181 inner: VaListImpl<'f>,
183 #[cfg(all(any(target_arch = "aarch64", target_arch = "powerpc",
184 target_arch = "x86_64", target_arch = "asmjs"),
185 any(not(target_arch = "aarch64"), not(target_os = "ios")),
187 inner: &'a mut VaListImpl<'f>,
189 _marker: PhantomData<&'a mut VaListImpl<'f>>,
192 #[cfg(any(all(not(target_arch = "aarch64"), not(target_arch = "powerpc"),
193 not(target_arch = "x86_64"), not(target_arch = "asmjs")),
194 all(target_arch = "aarch64", target_os = "ios"),
196 #[unstable(feature = "c_variadic",
197 reason = "the `c_variadic` feature has not been properly tested on \
198 all supported platforms",
200 impl<'f> VaListImpl<'f> {
201 /// Convert a `VaListImpl` into a `VaList` that is binary-compatible with C's `va_list`.
203 pub fn as_va_list<'a>(&'a mut self) -> VaList<'a, 'f> {
205 inner: VaListImpl { ..*self },
206 _marker: PhantomData,
211 #[cfg(all(any(target_arch = "aarch64", target_arch = "powerpc",
212 target_arch = "x86_64", target_arch = "asmjs"),
213 any(not(target_arch = "aarch64"), not(target_os = "ios")),
215 #[unstable(feature = "c_variadic",
216 reason = "the `c_variadic` feature has not been properly tested on \
217 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> {
225 _marker: PhantomData,
230 #[unstable(feature = "c_variadic",
231 reason = "the `c_variadic` feature has not been properly tested on \
232 all supported platforms",
234 impl<'a, 'f: 'a> Deref for VaList<'a, 'f> {
235 type Target = VaListImpl<'f>;
238 fn deref(&self) -> &VaListImpl<'f> {
243 #[unstable(feature = "c_variadic",
244 reason = "the `c_variadic` feature has not been properly tested on \
245 all supported platforms",
247 impl<'a, 'f: 'a> DerefMut for VaList<'a, 'f> {
249 fn deref_mut(&mut self) -> &mut VaListImpl<'f> {
254 // The VaArgSafe trait needs to be used in public interfaces, however, the trait
255 // itself must not be allowed to be used outside this module. Allowing users to
256 // implement the trait for a new type (thereby allowing the va_arg intrinsic to
257 // be used on a new type) is likely to cause undefined behavior.
259 // FIXME(dlrobertson): In order to use the VaArgSafe trait in a public interface
260 // but also ensure it cannot be used elsewhere, the trait needs to be public
261 // within a private module. Once RFC 2145 has been implemented look into
264 /// Trait which whitelists the allowed types to be used with [VaList::arg]
266 /// [VaList::va_arg]: struct.VaList.html#method.arg
267 #[unstable(feature = "c_variadic",
268 reason = "the `c_variadic` feature has not been properly tested on \
269 all supported platforms",
271 pub trait VaArgSafe {}
274 macro_rules! impl_va_arg_safe {
277 #[unstable(feature = "c_variadic",
278 reason = "the `c_variadic` feature has not been properly tested on \
279 all supported platforms",
281 impl sealed_trait::VaArgSafe for $t {}
286 impl_va_arg_safe!{i8, i16, i32, i64, usize}
287 impl_va_arg_safe!{u8, u16, u32, u64, isize}
288 impl_va_arg_safe!{f64}
290 #[unstable(feature = "c_variadic",
291 reason = "the `c_variadic` feature has not been properly tested on \
292 all supported platforms",
294 impl<T> sealed_trait::VaArgSafe for *mut T {}
295 #[unstable(feature = "c_variadic",
296 reason = "the `c_variadic` feature has not been properly tested on \
297 all supported platforms",
299 impl<T> sealed_trait::VaArgSafe for *const T {}
301 #[unstable(feature = "c_variadic",
302 reason = "the `c_variadic` feature has not been properly tested on \
303 all supported platforms",
305 impl<'f> VaListImpl<'f> {
306 /// Advance to the next arg.
308 pub unsafe fn arg<T: sealed_trait::VaArgSafe>(&mut self) -> T {
312 /// Copies the `va_list` at the current location.
313 pub unsafe fn with_copy<F, R>(&self, f: F) -> R
314 where F: for<'copy> FnOnce(VaList<'copy, 'f>) -> R {
315 let mut ap = self.clone();
316 let ret = f(ap.as_va_list());
322 #[unstable(feature = "c_variadic",
323 reason = "the `c_variadic` feature has not been properly tested on \
324 all supported platforms",
326 impl<'f> Clone for VaListImpl<'f> {
328 fn clone(&self) -> Self {
329 let mut dest = crate::mem::MaybeUninit::uninit();
331 va_copy(dest.as_mut_ptr(), self);
337 #[unstable(feature = "c_variadic",
338 reason = "the `c_variadic` feature has not been properly tested on \
339 all supported platforms",
341 impl<'f> Drop for VaListImpl<'f> {
343 // FIXME: this should call `va_end`, but there's no clean way to
344 // guarantee that `drop` always gets inlined into its caller,
345 // so the `va_end` would get directly called from the same function as
346 // the corresponding `va_copy`. `man va_end` states that C requires this,
347 // and LLVM basically follows the C semantics, so we need to make sure
348 // that `va_end` is always called from the same function as `va_copy`.
349 // For more details, see https://github.com/rust-lang/rust/pull/59625
350 // and https://llvm.org/docs/LangRef.html#llvm-va-end-intrinsic.
352 // This works for now, since `va_end` is a no-op on all current LLVM targets.
356 extern "rust-intrinsic" {
357 /// Destroy the arglist `ap` after initialization with `va_start` or
359 fn va_end(ap: &mut VaListImpl<'_>);
361 /// Copies the current location of arglist `src` to the arglist `dst`.
362 fn va_copy<'f>(dest: *mut VaListImpl<'f>, src: &VaListImpl<'f>);
364 /// Loads an argument of type `T` from the `va_list` `ap` and increment the
365 /// argument `ap` points to.
366 fn va_arg<T: sealed_trait::VaArgSafe>(ap: &mut VaListImpl<'_>) -> T;