Expand description
Function pointer type discrimination for pointer authentication. This module implements Rust’s equivalent of Clang’s function pointer type discriminator computation used in pointer authentication.
Compatibility with Clang is a primary goal. The discriminator produced for a given external “C” function type must match the value computed by Clang so that function pointers can be exchanged safely between Rust and C code while preserving pointer authentication semantics.
The implementation mirrors Clang’s behavior in
ASTContext::encodeTypeForFunctionPointerAuth, ensuring that identical
C-compatible function types produce identical discriminators. See:
https://clang.llvm.org/doxygen/ASTContext_8cpp.html#abb1375e068e807917527842d05cadea3.
§Overview
The computation is structured into three conceptual stages:
§1. Type normalization and lowering
Rust types are converted into a language-independent representation
(ClangDiscTy) that mirrors the type categories used by Clang when computing
function pointer discriminators. This includes canonicalization such as
treating all pointer-like types uniformly and mapping Rust constructs onto
their closest C equivalents.
One notable exception is C _Complex. Rust has no corresponding native type,
so there is no canonical Rust representation to map onto Clang’s _Complex
type category. Rather than infer one (for example, by treating (f32, f32)
or (f64, f64) as complex numbers), this implementation leaves such
representation choices to users and does not provide dedicated _Complex
encoding.
§2. Type encoding
The lowered representation is serialized into a byte stream using rules
intended to match Clang’s implementation in:
encodeTypeForFunctionPointerAuth. The resulting encoding describes the
function signature in a target-independent form suitable for hashing.
§3. Discriminator hashing
The encoded byte stream is hashed using LLVM’s stable SipHash-2-4 based
discriminator algorithm. The implementation here is a direct translation
of LLVM/Clang’s logic and must remain bit-for-bit compatible. See:
https://github.com/llvm/llvm-project/blob/main/third-party/siphash/include/siphash/SipHash.h.
Defined in llvm_siphash.rs.
§Module structure
-
High-level API
FnPtrDiscriminatorSourceptrauth_compute_fn_ptr_type_discriminator_forptrauth_clone_discriminated_schema_for
-
Low-level API
FnPtrTypeDiscriminatorInputcompute_fn_ptr_type_discriminator
-
Signature extraction
extract_fn_ptr_type
-
Clang-compatible type model
ClangDiscTycanonicalize_c_typeto_clang_disc_ty
-
Encoding
PtrauthEncoderencode_ty
§Compatibility requirements
Any changes to the encoding or hashing logic should be validated against Clang’s discriminator computation. Divergence from Clang will result in incompatible pointer authentication values across language boundaries.
This implementation intentionally approximates Clang’s behavior for extern “C” function types only. It does NOT attempt to model full type system rules.
Structs§
- FnPtr
Type Discriminator Input - Canonical representation of a function signature used for pointer authentication discriminator generation.
- Ptrauth
Encoder 🔒
Enums§
Traits§
- FnPtr
Discriminator Source - Types that can serve as a source for function pointer type discrimination.
Functions§
- canonicalize_
c_ 🔒type - compute_
fn_ 🔒ptr_ type_ discriminator - Computes the Clang-compatible function pointer type discriminator.
- encode_
ty 🔒 - Encodes a ClangDiscTy into the discriminator byte stream.
- extract_
fn_ 🔒ptr_ type - Unwraps optional function pointers and normalizes the type.
- ptrauth_
clone_ discriminated_ schema_ for - Clones a pointer authentication schema and updates its constant discriminator.
- ptrauth_
compute_ fn_ ptr_ type_ discriminator_ for - Computes the function pointer type discriminator directly from a supported source.
- to_
clang_ 🔒disc_ ty - Lowers a Rust type into a Clang-compatible discriminator type.