rustc_sanitizers/cfi/typeid/mod.rs
1//! Type metadata identifiers for LLVM Control Flow Integrity (CFI) and cross-language LLVM CFI
2//! support for the Rust compiler.
3//!
4//! For more information about LLVM CFI and cross-language LLVM CFI support for the Rust compiler,
5//! see design document in the tracking issue #89653.
6
7use bitflags::bitflags;
8use rustc_middle::ty::{Instance, Ty, TyCtxt};
9use rustc_target::callconv::FnAbi;
10
11bitflags! {
12 /// Options for typeid_for_fnabi.
13 #[derive(Clone, Copy, Debug)]
14 pub struct TypeIdOptions: u32 {
15 /// Generalizes pointers for compatibility with Clang
16 /// `-fsanitize-cfi-icall-generalize-pointers` option for cross-language LLVM CFI and KCFI
17 /// support.
18 const GENERALIZE_POINTERS = 1;
19 /// Generalizes repr(C) user-defined type for extern function types with the "C" calling
20 /// convention (or extern types) for cross-language LLVM CFI and KCFI support.
21 const GENERALIZE_REPR_C = 2;
22 /// Normalizes integers for compatibility with Clang
23 /// `-fsanitize-cfi-icall-experimental-normalize-integers` option for cross-language LLVM
24 /// CFI and KCFI support.
25 const NORMALIZE_INTEGERS = 4;
26 /// Do not perform self type erasure for attaching a secondary type id to methods with their
27 /// concrete self so they can be used as function pointers.
28 ///
29 /// (This applies to typeid_for_instance only and should be used to attach a secondary type
30 /// id to methods during their declaration/definition so they match the type ids returned by
31 /// either typeid_for_instance or typeid_for_fnabi at call sites during code generation for
32 /// type membership tests when methods are used as function pointers.)
33 const USE_CONCRETE_SELF = 8;
34 }
35}
36
37pub mod itanium_cxx_abi;
38
39/// Returns a type metadata identifier for the specified FnAbi.
40pub fn typeid_for_fnabi<'tcx>(
41 tcx: TyCtxt<'tcx>,
42 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
43 options: TypeIdOptions,
44) -> String {
45 itanium_cxx_abi::typeid_for_fnabi(tcx, fn_abi, options)
46}
47
48/// Returns a type metadata identifier for the specified Instance.
49pub fn typeid_for_instance<'tcx>(
50 tcx: TyCtxt<'tcx>,
51 instance: Instance<'tcx>,
52 options: TypeIdOptions,
53) -> String {
54 itanium_cxx_abi::typeid_for_instance(tcx, instance, options)
55}