Skip to main content

cfi_types/
lib.rs

1//! CFI types for cross-language LLVM CFI support.
2//!
3//! The cfi_types crate provides a new set of C types as user-defined types
4//! using the cfi_encoding attribute and repr(transparent) to be used for
5//! cross-language LLVM CFI support. This new set of C types allows the Rust
6//! compiler to identify and correctly encode C types in extern "C" function
7//! types indirectly called across the FFI boundary when CFI is enabled.
8//!
9//! The use of these types are optional and are recommended for when enforcement
10//! and explicitness of types used across the FFI boundary and no loss of
11//! granularity for cross-language LLVM CFI are preferred.
12//!
13//! Alternatively, the `-Zsanitizer-cfi-normalize-integers` option may be used
14//! with the Clang `-fsanitize-cfi-icall-experimental-normalize-integers` option
15//! for cross-language LLVM CFI support.
16
17#![feature(cfg_sanitizer_cfi)]
18#![feature(cfi_encoding)]
19#![allow(non_camel_case_types)]
20
21/// CFI type equivalent to Rust's core::ffi::c_char type alias.
22#[allow(dead_code)]
23#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "c")]
24#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
25#[repr(transparent)]
26pub struct c_char(pub core::ffi::c_char);
27
28/// CFI type equivalent to Rust's core::ffi::c_int type alias.
29#[allow(dead_code)]
30#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "i")]
31#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
32#[repr(transparent)]
33pub struct c_int(pub core::ffi::c_int);
34
35/// CFI type equivalent to Rust's core::ffi::c_long type alias.
36#[allow(dead_code)]
37#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "l")]
38#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
39#[repr(transparent)]
40pub struct c_long(pub core::ffi::c_long);
41
42/// CFI type equivalent to Rust's core::ffi::c_longlong type alias.
43#[allow(dead_code)]
44#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "x")]
45#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
46#[repr(transparent)]
47pub struct c_longlong(pub core::ffi::c_longlong);
48
49/// CFI type equivalent to Rust's core::ffi::c_schar type alias.
50#[allow(dead_code)]
51#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "a")]
52#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
53#[repr(transparent)]
54pub struct c_schar(pub core::ffi::c_schar);
55
56/// CFI type equivalent to Rust's core::ffi::c_short type alias.
57#[allow(dead_code)]
58#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "s")]
59#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
60#[repr(transparent)]
61pub struct c_short(pub core::ffi::c_short);
62
63/// CFI type equivalent to Rust's core::ffi::c_uchar type alias.
64#[allow(dead_code)]
65#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "h")]
66#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
67#[repr(transparent)]
68pub struct c_uchar(pub core::ffi::c_uchar);
69
70/// CFI type equivalent to Rust's core::ffi::c_uint type alias.
71#[allow(dead_code)]
72#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "j")]
73#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
74#[repr(transparent)]
75pub struct c_uint(pub core::ffi::c_uint);
76
77/// CFI type equivalent to Rust's core::ffi::c_ulong type alias.
78#[allow(dead_code)]
79#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "m")]
80#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
81#[repr(transparent)]
82pub struct c_ulong(pub core::ffi::c_ulong);
83
84/// CFI type equivalent to Rust's core::ffi::c_ulonglong type alias.
85#[allow(dead_code)]
86#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "y")]
87#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
88#[repr(transparent)]
89pub struct c_ulonglong(pub core::ffi::c_ulonglong);
90
91/// CFI type equivalent to Rust's core::ffi::c_ushort type alias.
92#[allow(dead_code)]
93#[cfg_attr(not(sanitizer_cfi_normalize_integers), cfi_encoding = "t")]
94#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
95#[repr(transparent)]
96pub struct c_ushort(pub core::ffi::c_ushort);