Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[PAC] Add explicit pointer authentication intrinsics (5/6) - #166255

Draft
jchlanda wants to merge 11 commits into
rust-lang:mainfrom
jchlanda:jakub/pac_ty_disc_PR_9
Draft

jchlanda wants to merge 11 commits into
rust-lang:mainfrom
jchlanda:jakub/pac_ty_disc_PR_9

Conversation

@jchlanda

@jchlanda jchlanda commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

With function pointer type discrimination, C signs a code address for the type of the slot that holds it, and Rust signs it for the function it points to. Where the two differ (void * from dlsym, generic callback types, void * user data), the address has to be converted explicitly at the language boundary. This adds unstable intrinsics for that:

  • ptrauth_c_cast::<Src, Dst>(src): authenticates src with the discriminator C uses for Src and resigns it for Dst.
  • ptrauth_fn_ptr_discriminator::<T>(): the discriminator C uses for T.
  • ptrauth_strip(p): removes the signature.

All three are no-ops without type discrimination.

Tests: codegen tests for each intrinsic, and a C/Rust run-make interop test.


This is step 5 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-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 20, 2026
@jchlanda

Copy link
Copy Markdown
Contributor Author

r? @davidtwco

@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

davidtwco is currently at their maximum review capacity.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

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.
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_9 branch from 10ff43a to e905f3a Compare December 3, 2026 12:10
@rust-log-analyzer

This comment has been minimized.

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

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,
* types that are not ABI-compatible but share the discriminator (to
  match Clang's behaviour),
* 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.
Match `core::num::Complex<T>` against C `_Complex T` for float and
integer `T`.
We used to have `scalar_to_backend` and `scalar_to_backend_with_pac`,
the latter taking a `schema` and designed for function pointers signing.
This was error prone, using plain `scalar_to_backend` would result with
a pointer not being signed.

Instead, make `scalar_to_backend` call
`scalar_to_backend_with_ptrauth_schema` with the default schema.

Renames `scalar_to_backend_with_pac` to
`scalar_to_backend_with_ptrauth_schema`, which is used when a pointer
must not be signed with a default schema (init/fini).
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.

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.
* Type discrimination codegen-llvm tests.
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.
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_9 branch from e905f3a to 17fb0a7 Compare December 3, 2026 12:27
@rust-log-analyzer

This comment has been minimized.

C signs based on the type of the slot holding the pointer (not the
pointee type) and allows resigning on type-casts. A pointer crossing
language boundary that was not signed using its pointee type needs an
explicit resign on the Rust side.
The same is the case if Rust is to share type-erased pointers with C
(they would need a resign).

This patch makes it possible with:
* `ptrauth_c_cast::<Src, Dst>(src)`: resigns a pointer in `src` from the
  discriminator of `Src` to that of `Dst` (Rust equivalent of the C type
  cast).
* `ptrauth_fn_ptr_discriminator::<F>()`: the discriminator of a function
  pointer type, evaluated at compile time (Clang's
  `__builtin_ptrauth_type_discriminator`).
* `ptrauth_strip(ptr)`: the address without its signature (used for
  comparison or printing raw addresses).
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_9 branch from 17fb0a7 to a6fe86c Compare December 3, 2026 14:11

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-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. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants