Skip to content

[PAC] Function pointer type discrimination (4/6) - #159081

Draft
jchlanda wants to merge 10 commits into
rust-lang:mainfrom
jchlanda:jakub/pac_ty_disc_PR_4
Draft

jchlanda wants to merge 10 commits into
rust-lang:mainfrom
jchlanda:jakub/pac_ty_disc_PR_4

Conversation

@jchlanda

@jchlanda jchlanda commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Implements function pointer type discrimination behind -Zpointer-authentication=+function-pointer-type-discrimination on aarch64-unknown-linux-pauthtest.

The model:

  • An extern "C" or extern "system" function pointer is signed with the discriminator of the function it points to.
  • Casts, transmutes, unions and copies through untyped memory never resign.
  • A call authenticates with the discriminator of the function pointer type it is called through, which matches whenever the call is not UB.

The commits:

  1. Sign function pointers held in constants.
  2. Type discrimination itself: per-instance signing, extern_weak symbols signed with their FnAbi discriminator, the FPTR_TYPE_DISCR bit (11) in the PAuth ABI version, and removal of the error that rejected the flag.
  3. The pauthtest doc no longer asks for a patched libc.
  4. Support for C _Complex.

This is step 4 of a series implementing function pointer type discrimination for aarch64-unknown-linux-pauthtest, using the pointee-signature model discussed in rust-lang/unsafe-code-guidelines#624.
It stays a draft until that discussion concludes.
Each PR contains the commits of the previous ones.

  1. Minicore updates
  2. Record the PAuth discriminator in FnAbi
  3. Discriminator encoder: ABI-compatibility rules, f16/f128, _Complex
  4. Function pointer type discrimination
  5. Explicit pointer authentication intrinsics
  6. #[russtc_ptrauth_c_discriminator(N)]

Already merged: #159071 (encoder and hash).
Folded into step 4: #159075, #159084.
Closed: #159082 (resign on transmute, dropped), #159087 (tests moved into each step).


Useful links:

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 10, 2026
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch 2 times, most recently from 9052b2a to 5b223de Compare July 14, 2026 07:27
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch from 5b223de to 45f422b Compare July 14, 2026 08:53
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch from 45f422b to 8659b72 Compare July 17, 2026 07:04
@rust-log-analyzer

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch 2 times, most recently from 8392cdf to 5399226 Compare July 17, 2026 09:48
/// emitted at that offset.
///
/// Offsets are relative to the beginning of the allocation.
pub(crate) struct FnPtrDiscriminatorAtOffset {

@kovdan01 kovdan01 Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the established naming convention in rust for such kind of things? Should this be plural (discriminators)? Especially given the fact that we already have collect_fn_ptr_discriminators

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, FnPtrDiscriminatorsAtOffset it is.

let variant = def.non_enum_variant();

let Some((_, field)) = variant.fields.iter_enumerated().next() else {
return;

@kovdan01 kovdan01 Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please refer me to tests covering these early-return paths (from this case and from the below cases)? The logic by itself looks sound, but it definitely deserves its own test coverage (which I'm not sure I can correctly identify by myself in the new test suite from PR 8)

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, this is actually redundant. I initially though it would serve for something like:

#[repr(transparent)]
struct Wrapper(extern "C" fn(i32));

impl Sync for Wrapper {}

static T_WRAPPED_FN_PTR: Wrapper = Wrapper(g);

in https://github.com/jchlanda/rust/blob/fb279532dbf253c1f78dd02a3268bfdde8d6d7ad/tests/codegen-llvm/pauth/pauth-fn-ptr-type-discrimination-struct-members.rs#L1, but turns out struct arm handles it just fine. Removing.

While at it, I added a test case for Tupel which was missing (T_TUPLE, T_TUPLE_2 and test_8_tuple_members in the same file). Tests added to the 8th PR.

Thanks!

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch 2 times, most recently from ef6b34d to e08ffe7 Compare July 20, 2026 15:19
.as_ref()
.and_then(|m| m.map.get(&Size::from_bytes(offset as u64)));

// Init/fini entries must not participate in function pointer type discrimination.

@kovdan01 kovdan01 Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe worth slightly re-phrasing this - now it might be understood as "init/fini pointers are zero-discriminated", while in reality they receive their own special constant discriminator

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// FIXME: should we cache `const_alloc_to_llvm` to avoid repeating this for the
// same `ConstAllocation`?
let cv = const_alloc_to_llvm(self, alloc.inner(), IsStatic::No, IsInitOrFini::No);
let cv = const_alloc_to_llvm(self, alloc.inner(), IsStatic::No, IsInitOrFini::No, None);

@kovdan01 kovdan01 Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why fn_ptr_discriminators is guaranteed to be None here? Is ConstAllocation something which is guaranteed not to contain nested function pointers?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked at this code again, and I think this is incorrect. Type discrimination should also be applied when computing addresses for statics; it just didn't come up in the existing tests.

From a quick look, this seems a little more involved because we don't have the necessary type information when calling static_addr_of. We'll likely need to either extend its API or compute the discriminator map at the call sites that still have access to the Rust type.

I've added an in-code FIXME to capture this, and if you're okay with it, I'd like to address it in a follow-up PR.

alloc: &Allocation,
is_static: IsStatic,
is_init_fini: IsInitOrFini,
fn_ptr_discriminators: Option<&FnPtrDiscriminatorsAtOffset>,

@kovdan01 kovdan01 Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'm not sure of a particular suggestion, but just as a thought - it might be worth somehow prefixing such things with ptrauth_ in order to (a) indicate that it's ptrauth-specific for readers wondering what's it all about (b) make it easier to grep ptrauth-related stuff.

If just adding prefix right now, the identifier would become too long though. So maybe the rest of the name could be somehow reconsidered.

I would appreciate if you could do a pass over all newly added data structures/variables/argument names which exist in "generic" non-ptrauth-specific context and ensure if in all such places it's clear that the corresponding item is ptrauth-specific

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For new fields that are extending already existing data structures, I'll add the ptrauth_ prefix.

I'll also do a follow-up PR to take another look at session.rs, as it doesn't really belong in this patch series. At that point I'll also review the names of the data structures/functions we're introducing. I agree that public-facing identifiers should be prefixed, but for local, throwaway-style types/funcs I'm less convinced. Since they're context-specific, they should be easier to reason about without additional prefixes.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch 2 times, most recently from f77edea to 58601d5 Compare July 24, 2026 15:18
@rust-log-analyzer

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch from 58601d5 to 286014f Compare July 24, 2026 15:56
@jchlanda jchlanda changed the title [WIP] 4 - Static allocs [PAC] Static allocs (4/8) Jul 24, 2026
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch from 91ffd1d to 3ae948f Compare October 8, 2026 13:05
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch from 3ae948f to b266f9d Compare October 8, 2026 14:24
@rust-bors

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch from b266f9d to 377e8ee Compare October 16, 2026 11:54
@rustbot

rustbot commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-bors

This comment has been minimized.

@jchlanda
jchlanda marked this pull request as draft November 17, 2026 12:01
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 17, 2026
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_4 branch from 377e8ee to ea9e93d Compare November 20, 2026 12:42
@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Nov 20, 2026
This patch brings:
* adds `FnAbi::ptrauth_discriminator`
* `is_ptrauth_signed_abi` unified point of control for pauth-supported
  ABIs
* `ptrauth_signing_discriminator_for` - discriminator for given pointer
  under the schema (None - not supported ABI, constant, or calculated for
  pointer type)
And a test for it.

`compute_fn_ptr_type_discriminator` was split into the encoding and the
hash (both public) for the test use.
…rules

Under type discrimination a fn pointer is signed with a discriminator of
a pointee and authenticated with a discriminator of the function pointer
that it is called through. In order to make a call through
ABI-compatible function pointers work we must make sure that they
produce the same discriminator. ABI-compatible types must generate
the same discriminator.

In order to do that this patch canonicalizes types by their ABI
representation before encoding:
* `repr(transparent)` wrappers as their non-1-ZST field;
* two-variant niche enums (`Option<&T>`, `Option<NonZero<_>>`, `Option<fn()>`)
  as the payload;
* size-0, alignment-1 types as `()` (C's void);
* `Box<T>` as a pointer, and pattern types as their base type;
* `!` as `()`.

`discriminator-abi-compat.rs` checks three groups of signatures:
* ABI-compatible types, must have the same discriminator (a failure is a
  soundness bug)
* types that are not ABI-compatible but share the discriminator because
  Clang's encoding is coarser (a failure breaks C interoperability)
* distinct types - distinct discriminators
Clang's `encodeTypeForFunctionPointerAuth` uses Itanium type codes:
* `_Float16` is `DF16_`
* `f128` is the C type with its representation: `long double` (`e`) on
  AArch64 Linux, where `long double` is IEEE quad precision, and
  `__float128` (`g`) elsewhere.
@rust-log-analyzer

This comment has been minimized.

Match `core::num::Complex<T>` (the `complex` lang item) against C
`_Complex T` for float and integer `T`.
A function pointers read from a constant (for example a `const` item
used by a value, or a field of a constant aggregate), were emitted
unsigned: `from_const` and `from_const_alloc` lowered pointers without a
schema.

This aligns constants lowering to, already implemented, statics.
Enable `-Zpointer-authentication=+function-pointer-type-discrimination`.

Function pointers are signed with the discriminators of the functions
they point to, calls authenticate with the discriminators of the type
they are made through.

Pointers are never implicitly resigned, transmutes, `as` casts, union
puns etc are plain bit blasts.

ABI-compatible calls authenticate fine, because the encoder honours the
compatibility rules and encodes ABI-compatible types with the same
discriminators. Calls through any other signatures are UB and will fail
to authenticate.

This patch brings:
* Set `FPTR_TYPE_DISCR` (bit 11) in the PAuth ABI version and drop the
  "not supported" error.
* `maybe_sign_fn_ptr` signs with the discriminator of the instance.
* The address of an `extern_weak` function is called through the
  function pointer type of its static.
* Init/fini entries keep their own schema, which is not type
  discriminated.
The `libc` changes for the target are upstream, so `libc` no longer needs
patching. `cc-rs` no longer needs a fork either: `cc` 1.4.3 supports the
target, but `bootstrap` still pins 1.2.62, so the doc describes the pin bump.
With 1.4.3, `bootstrap` finds the sysroot through `PAUTHTEST_SYSROOT` and
`PAUTHTEST_RESOURCE_DIR`, so the doc lists those as well.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-run-make Area: port run-make Makefiles to rmake.rs A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-minicore` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants