Skip to content

[WIP] 2 - FnAbi, llvm.ptrauth.resign and Session API change#159074

Draft
jchlanda wants to merge 3 commits into
rust-lang:mainfrom
jchlanda:jakub/pac_ty_disc_PR_2
Draft

[WIP] 2 - FnAbi, llvm.ptrauth.resign and Session API change#159074
jchlanda wants to merge 3 commits into
rust-lang:mainfrom
jchlanda:jakub/pac_ty_disc_PR_2

Conversation

@jchlanda

@jchlanda jchlanda commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

This patch introduces the following:

  • Extends FnAbi (callconv) with a ptrauth_type_discriminator field. This field is only used when emitting pointer authentication call bundles. It is stored in FnAbi because the call site is not guaranteed to have access to an Instance, so the discriminator cannot always be computed on demand.
  • Adds support for llvm.ptrauth.resign. This intrinsic will be used when support for semantic transmute is added.
  • Performs a minor API redesign as groundwork for allowing call sites to modify schemas in place.

The use of an extended FnAbi is dictated by the need to issue a ptrauth bundle for call-like instructions. At that point, we only have access to the FnAbi struct (Instance is not guaranteed to be available).

Fundamentally, the discriminator originates from the source-level function type. Deriving it at the codegen stage would introduce a layering violation: it would require relying on FnAbi's ArgAbi (for both return values and arguments) to preserve source-level type information. This is not a guarantee provided by these abstractions (and in code comments), as FnAbi represents a lowered calling convention rather than the original function signature.

Computing the discriminator at this stage would therefore create a hard dependency on type information being available where it is not intended to be preserved. Carrying the value explicitly avoids reconstructing source-level information from lowered ABI data and keeps the abstraction boundaries intact.


This is part 2 of a sequence of 8 PRs, that together aim to bring function pointer type discrimination support:

  1. Encoder and hash
  2. FnAbi, llvm.ptrauth.resign and Session API change
  3. FPTR_TYPE_DISCR in ABI Version
  4. Static allocs
  5. Transmutes
  6. Propagate discriminator logic through remaining get_fn_ptr calls sites
  7. Minicore updates to support fn ptr type discriminator tests
  8. Fn ptr type discrimination tests

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 changed the title [WIP] 2 FnAbi, llvm.ptrauth.resign and Session API change [WIP] 2 - FnAbi, llvm.ptrauth.resign and Session API change Jul 10, 2026
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_2 branch 2 times, most recently from 8f37395 to 0792cdd 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_2 branch from 0792cdd to 281565d Compare July 14, 2026 08:51
@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_2 branch from 281565d to 360a700 Compare July 17, 2026 07:01
@rust-log-analyzer

This comment has been minimized.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_2 branch from 360a700 to 089232b Compare July 17, 2026 08:20
This patch 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>.
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_2 branch from 089232b to d85aa6b Compare July 17, 2026 09:47
let discriminator = if self.sess().pointer_authentication_fn_ptr_type_discrimination() {
fn_abi?.ptrauth_type_discriminator
} else {
0

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

Do we need this check here? As far as I can see, we already conditionally set ptrauth_type_discriminator to 0 if pointer_authentication_fn_ptr_type_discrimination() is false in compiler/rustc_ty_utils/src/abi.rs. Maybe we can even rename ptrauth_type_discriminator field in FnAbi to smth like just ptrauth_discriminator and assume it's storing the "effective" discriminator (either 0 or type discriminator)?

Just loudly thinking, I might easily miss smth - I would appreciate your explanation if I've got smth wrong

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.

You are right, we don't need it here, added comment about it though.

Also renaming to ptrauth_discriminator.

),
ptrauth_type_discriminator: if tcx.sess.pointer_authentication_fn_ptr_type_discrimination()
{
compute_fn_ptr_type_discriminator_for(tcx, sig).unwrap_or(0).into()

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

Just in case: do I get it correct that the sequence of PRs is not about "at each point we are expected to have a successful build", it's about ease of review only?

I'm just not seeing compute_fn_ptr_type_discriminator_for defined at this point - was it intentional or should PR content split decisions be reconsidered?

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.

No, you do not. It is intended to be buildable at each stage.

The split is twofold: logical, to show the progression of the implementation, and functional, since each commit is expected to be independently buildable. They are not exactly atomic in the sense that the functionality will not be complete, so if you check out a commit in the middle of the sequence, it should build, but the generated code might not be fully correct.

The function you are referring to was added in the first PR of the series:
https://github.com/rust-lang/rust/pull/159071/changes#diff-40f6b89998ba7d54d3f76fde93944eab9b8ba5a16eb1d763c598e2802a180af7R166

/// Indicates if an unwind may happen across a call to this function.
pub can_unwind: bool,
/// Computed type discriminator for pointer authentication purpose.
pub ptrauth_type_discriminator: u64,

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

Just a future note: when submitting as final non-draft PR for mainline review, it might be worth to provide a summary why a new field in FnAbi is considered at this point the proper solution for our problem and why we really need that. The summary could be placed somewhere in the PR description/PR comments/elsewhere if a more appropriate place exists.

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, in the PR description.

@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_2 branch from d85aa6b to 7708e94 Compare July 20, 2026 12:44
@rust-log-analyzer

This comment has been minimized.

This patch introduces the following:

* Extends `FnAbi` (`callconv`) with a `ptrauth_type_discriminator`
  field. This field is only used when emitting pointer authentication
  call bundles. It is stored in `FnAbi` because the call site is not
  guaranteed to have access to an `Instance`, so the discriminator
  cannot always be computed on demand.
* Adds support for `llvm.ptrauth.resign`. This intrinsic will be used
  when support for semantic transmute is added.
* Performs a minor API redesign as groundwork for allowing call sites to
  modify schemas in place.
@jchlanda
jchlanda force-pushed the jakub/pac_ty_disc_PR_2 branch from 7708e94 to 8ed3f55 Compare July 20, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants