Skip to main content

Module discriminator

Module discriminator 

Source
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

    • FnPtrDiscriminatorSource
    • ptrauth_compute_fn_ptr_type_discriminator_for
    • ptrauth_clone_discriminated_schema_for
  • Low-level API

    • FnPtrTypeDiscriminatorInput
    • compute_fn_ptr_type_discriminator
  • Signature extraction

    • extract_fn_ptr_type
  • Clang-compatible type model

    • ClangDiscTy
    • canonicalize_c_type
    • to_clang_disc_ty
  • Encoding

    • PtrauthEncoder
    • encode_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§

FnPtrTypeDiscriminatorInput
Canonical representation of a function signature used for pointer authentication discriminator generation.
PtrauthEncoder 🔒

Enums§

ClangDiscTy 🔒

Traits§

FnPtrDiscriminatorSource
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.