LLVM 24.0.0git
AArch64PointerAuth.h
Go to the documentation of this file.
1//===-- AArch64PointerAuth.h -- Harden code using PAuth ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_TARGET_AARCH64_AARCH64POINTERAUTH_H
10#define LLVM_LIB_TARGET_AARCH64_AARCH64POINTERAUTH_H
11
13
14namespace llvm {
15namespace AArch64PAuth {
16
17/// PAuth key to be used with function pointers in .init_array and .fini_array.
19
20/// Constant discriminator to be used with function pointers in .init_array and
21/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
22constexpr unsigned InitFiniPointerConstantDiscriminator = 0xD9D4;
23
24/// Controls whether, for PAUTH_EPILOGUE_ENTRY_SP, we use the hint-space
25/// AUTI[AB]1716 instruction to authenticate the LR value, or whether we use
26/// an AUTI[AB] LR, X16 instruction instead.
27constexpr bool authenticatesLRViaX17(bool BranchProtectionPAuthLR,
28 bool HasPAuth) {
29 return BranchProtectionPAuthLR || !HasPAuth;
30}
31
32/// Variants of check performed on an authenticated pointer.
33///
34/// In cases such as authenticating the LR value when performing a tail call
35/// or when re-signing a signed pointer with a different signing schema,
36/// a failed authentication may not generate an exception on its own and may
37/// create an authentication or signing oracle if not checked explicitly.
38///
39/// A number of check methods modify control flow in a similar way by
40/// rewriting the code
41///
42/// ```
43/// <authenticate LR>
44/// <more instructions>
45/// ```
46///
47/// as follows:
48///
49/// ```
50/// <authenticate LR>
51/// <method-specific checker>
52/// on_fail:
53/// brk <code>
54/// on_success:
55/// <more instructions>
56///
57/// ```
58enum class AuthCheckMethod {
59 /// Do not check the value at all
61
62 /// Perform a load to a temporary register
64
65 /// Check by comparing bits 62 and 61 of the authenticated address.
66 ///
67 /// This method modifies control flow and inserts the following checker:
68 ///
69 /// ```
70 /// eor Xtmp, Xn, Xn, lsl #1
71 /// tbz Xtmp, #62, on_success
72 /// ```
74
75 /// Check by comparing the authenticated value with an XPAC-ed one without
76 /// using PAuth instructions not encoded as HINT. Can only be applied to LR.
77 ///
78 /// This method modifies control flow and inserts the following checker:
79 ///
80 /// ```
81 /// mov Xtmp, LR
82 /// xpaclri ; encoded as "hint #7"
83 /// ; Note: at this point, the LR register contains the address as if
84 /// ; the authentication succeeded and the temporary register contains the
85 /// ; *real* result of authentication.
86 /// cmp Xtmp, LR
87 /// b.eq on_success
88 /// ```
90
91 /// Similar to XPACHint but using Armv8.3-only XPAC instruction, thus
92 /// not restricted to LR:
93 /// ```
94 /// mov Xtmp, Xn
95 /// xpac(i|d) Xn
96 /// cmp Xtmp, Xn
97 /// b.eq on_success
98 /// ```
100};
101
102/// Ways to check pointer authentication auth/resign failures.
104
105/// Control the emission of .cfi_set_ra_state, which replaces the
106/// deprecated .cfi_negate_ra_state_with_pc [1].
107///
108/// The latter is fundamentally unable to express some program orders [2], as
109/// the dwarf 'program' reads functions in a linear scan of their addresses to
110/// reconstruct the state of the frame, whereas control flow may enter and exit
111/// such regions arbitrarily (such as in hot-cold-split, and shrinkwrapped
112/// fucntions), and thus the negate-based cfi is unable to encode the address of
113/// the signing instruction in all program orders.
114///
115/// Since .cfi_negate_ra_state is still sufficient for describing
116/// ptrauth-returns=pauth, we default to using the new CFI only for PAuth_LR, as
117/// DW_CFA_AARCH64_negate_ra_state has a smaller encoding than
118/// DW_CFA_AARCH64_set_ra_state.
119///
120/// 1: https://github.com/ARM-software/abi-aa/pull/346
121/// 2: https://github.com/ARM-software/abi-aa/issues/327
122enum class SetRAStateMode {
123 Never, // Always use .cfi_negate_ra_state(_with_pc)
124 PAuthLR, // Use .cfi_set_ra_state only for PAuth_LR
125 Always, // Use .cfi_set_ra_state for both PAuth and PAuth_LR
126};
127
128/// Returns the number of bytes added by checkAuthenticatedRegister.
130
131} // end namespace AArch64PAuth
132} // end namespace llvm
133
134#endif
PtrauthCheckMode
Ways to check pointer authentication auth/resign failures.
unsigned getCheckerSizeInBytes(AuthCheckMethod Method)
Returns the number of bytes added by checkAuthenticatedRegister.
constexpr bool authenticatesLRViaX17(bool BranchProtectionPAuthLR, bool HasPAuth)
Controls whether, for PAUTH_EPILOGUE_ENTRY_SP, we use the hint-space AUTI[AB]1716 instruction to auth...
constexpr AArch64PACKey::ID InitFiniKey
PAuth key to be used with function pointers in .init_array and .fini_array.
AuthCheckMethod
Variants of check performed on an authenticated pointer.
@ XPACHint
Check by comparing the authenticated value with an XPAC-ed one without using PAuth instructions not e...
@ DummyLoad
Perform a load to a temporary register.
@ HighBitsNoTBI
Check by comparing bits 62 and 61 of the authenticated address.
@ None
Do not check the value at all.
@ XPAC
Similar to XPACHint but using Armv8.3-only XPAC instruction, thus not restricted to LR:
constexpr unsigned InitFiniPointerConstantDiscriminator
Constant discriminator to be used with function pointers in .init_array and .fini_array.
SetRAStateMode
Control the emission of .cfi_set_ra_state, which replaces the deprecated .cfi_negate_ra_state_with_pc...
This is an optimization pass for GlobalISel generic memory operations.