Skip to main content

core/ffi/
mod.rs

1//! Platform-specific types, as defined by C.
2//!
3//! Code that interacts via FFI will almost certainly be using the
4//! base types provided by C, which aren't nearly as nicely defined
5//! as Rust's primitive types. This module provides types which will
6//! match those defined by C, so that code that interacts with C will
7//! refer to the correct types.
8
9#![stable(feature = "core_ffi", since = "1.30.0")]
10#![allow(non_camel_case_types)]
11
12#[doc(inline)]
13#[stable(feature = "core_c_str", since = "1.64.0")]
14pub use self::c_str::CStr;
15#[doc(inline)]
16#[stable(feature = "cstr_from_bytes_until_nul", since = "1.69.0")]
17pub use self::c_str::FromBytesUntilNulError;
18#[doc(inline)]
19#[stable(feature = "core_c_str", since = "1.64.0")]
20pub use self::c_str::FromBytesWithNulError;
21use crate::fmt;
22
23#[stable(feature = "c_str_module", since = "1.88.0")]
24pub mod c_str;
25
26mod va_list;
27#[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")]
28pub use self::va_list::{VaArgSafe, VaList};
29
30mod primitives;
31#[stable(feature = "core_ffi_c", since = "1.64.0")]
32pub use self::primitives::{
33    c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,
34    c_ulong, c_ulonglong, c_ushort,
35};
36#[unstable(feature = "c_size_t", issue = "88345")]
37pub use self::primitives::{c_ptrdiff_t, c_size_t, c_ssize_t};
38
39// N.B., for LLVM to recognize the void pointer type and by extension
40//     functions like malloc(), we need to have it represented as i8* in
41//     LLVM bitcode. The enum used here ensures this and prevents misuse
42//     of the "raw" type by only having private variants. We need two
43//     variants, because the compiler complains about the repr attribute
44//     otherwise and we need at least one variant as otherwise the enum
45//     would be uninhabited and at least dereferencing such pointers would
46//     be UB.
47#[doc = include_str!("c_void.md")]
48#[lang = "c_void"]
49#[repr(u8)]
50#[stable(feature = "core_c_void", since = "1.30.0")]
51#[ferrocene::prevalidated]
52pub enum c_void {
53    #[unstable(
54        feature = "c_void_variant",
55        reason = "temporary implementation detail",
56        issue = "none"
57    )]
58    #[doc(hidden)]
59    __variant1,
60    #[unstable(
61        feature = "c_void_variant",
62        reason = "temporary implementation detail",
63        issue = "none"
64    )]
65    #[doc(hidden)]
66    __variant2,
67}
68
69#[stable(feature = "std_debug", since = "1.16.0")]
70impl fmt::Debug for c_void {
71    #[ferrocene::prevalidated]
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        f.debug_struct("c_void").finish()
74    }
75}
76
77// Link the MSVC default lib
78#[cfg(all(windows, target_env = "msvc"))]
79#[link(
80    name = "/defaultlib:msvcrt",
81    modifiers = "+verbatim",
82    cfg(not(target_feature = "crt-static"))
83)]
84#[link(name = "/defaultlib:libcmt", modifiers = "+verbatim", cfg(target_feature = "crt-static"))]
85unsafe extern "C" {}
86
87// Used by rustc for checking the definitions of other function with the same symbol names
88//
89// See the `invalid_runtime_symbols_definitions` lint.
90mod runtime_symbols {
91    use crate::ffi::{c_char, c_int, c_void};
92
93    unsafe extern "C" {
94        #[rustc_canonical_symbol]
95        fn memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void;
96
97        #[rustc_canonical_symbol]
98        fn memmove(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void;
99
100        #[rustc_canonical_symbol]
101        fn memset(s: *mut c_void, c: c_int, n: usize) -> *mut c_void;
102
103        #[rustc_canonical_symbol]
104        fn memcmp(s1: *const c_void, s2: *const c_void, n: usize) -> c_int;
105
106        #[rustc_canonical_symbol]
107        fn bcmp(s1: *const c_void, s2: *const c_void, n: usize) -> c_int;
108
109        #[rustc_canonical_symbol]
110        fn strlen(s: *const c_char) -> usize;
111    }
112}