Skip to content

Commit 7d879bc

Browse files
authored
[AArch64][PAC] Refine authenticated pointer check methods (#74074)
Align the values of the immediate operand of BRK instruction with those used by the existing arm64e implementation. Make AuthCheckMethod::DummyLoad use the requested register instead of LR.
1 parent 04c1cce commit 7d879bc

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

llvm/lib/Target/AArch64/AArch64PointerAuth.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "AArch64InstrInfo.h"
1313
#include "AArch64MachineFunctionInfo.h"
1414
#include "AArch64Subtarget.h"
15+
#include "Utils/AArch64BaseInfo.h"
1516
#include "llvm/CodeGen/MachineBasicBlock.h"
1617
#include "llvm/CodeGen/MachineInstrBuilder.h"
1718
#include "llvm/CodeGen/MachineModuleInfo.h"
@@ -35,7 +36,10 @@ class AArch64PointerAuth : public MachineFunctionPass {
3536

3637
private:
3738
/// An immediate operand passed to BRK instruction, if it is ever emitted.
38-
const unsigned BrkOperand = 0xc471;
39+
static unsigned BrkOperandForKey(AArch64PACKey::ID KeyId) {
40+
const unsigned BrkOperandBase = 0xc470;
41+
return BrkOperandBase + KeyId;
42+
}
3943

4044
const AArch64Subtarget *Subtarget = nullptr;
4145
const AArch64InstrInfo *TII = nullptr;
@@ -245,7 +249,7 @@ MachineBasicBlock &llvm::AArch64PAuth::checkAuthenticatedRegister(
245249
return MBB;
246250
case AuthCheckMethod::DummyLoad:
247251
BuildMI(MBB, MBBI, DL, TII->get(AArch64::LDRWui), getWRegFromXReg(TmpReg))
248-
.addReg(AArch64::LR)
252+
.addReg(AuthenticatedReg)
249253
.addImm(0)
250254
.addMemOperand(createCheckMemOperand(MF, Subtarget));
251255
return MBB;
@@ -321,6 +325,10 @@ unsigned llvm::AArch64PAuth::getCheckerSizeInBytes(AuthCheckMethod Method) {
321325

322326
bool AArch64PointerAuth::checkAuthenticatedLR(
323327
MachineBasicBlock::iterator TI) const {
328+
const AArch64FunctionInfo *MFnI = TI->getMF()->getInfo<AArch64FunctionInfo>();
329+
AArch64PACKey::ID KeyId =
330+
MFnI->shouldSignWithBKey() ? AArch64PACKey::IB : AArch64PACKey::IA;
331+
324332
AuthCheckMethod Method = Subtarget->getAuthenticatedLRCheckMethod();
325333

326334
if (Method == AuthCheckMethod::None)
@@ -361,7 +369,7 @@ bool AArch64PointerAuth::checkAuthenticatedLR(
361369
"More than a single register is used by TCRETURN");
362370

363371
checkAuthenticatedRegister(TI, Method, AArch64::LR, TmpReg, /*UseIKey=*/true,
364-
BrkOperand);
372+
BrkOperandForKey(KeyId));
365373

366374
return true;
367375
}

llvm/test/CodeGen/AArch64/sign-return-address-tailcall.ll

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ define i32 @tailcall_direct() "sign-return-address"="non-leaf" {
2323
;
2424
; COMMON-NEXT: b callee
2525
; BRK-NEXT: .[[FAIL]]:
26-
; BRK-NEXT: brk #0xc471
26+
; BRK-NEXT: brk #0xc470
2727
tail call void asm sideeffect "", "~{lr}"()
2828
%call = tail call i32 @callee()
2929
ret i32 %call
@@ -48,7 +48,7 @@ define i32 @tailcall_indirect(ptr %fptr) "sign-return-address"="non-leaf" {
4848
;
4949
; COMMON-NEXT: br x0
5050
; BRK-NEXT: .[[FAIL]]:
51-
; BRK-NEXT: brk #0xc471
51+
; BRK-NEXT: brk #0xc470
5252
tail call void asm sideeffect "", "~{lr}"()
5353
%call = tail call i32 %fptr()
5454
ret i32 %call
@@ -89,7 +89,7 @@ define i32 @tailcall_direct_noframe_sign_all() "sign-return-address"="all" {
8989
;
9090
; COMMON-NEXT: b callee
9191
; BRK-NEXT: .[[FAIL]]:
92-
; BRK-NEXT: brk #0xc471
92+
; BRK-NEXT: brk #0xc470
9393
%call = tail call i32 @callee()
9494
ret i32 %call
9595
}
@@ -113,9 +113,20 @@ define i32 @tailcall_indirect_noframe_sign_all(ptr %fptr) "sign-return-address"=
113113
;
114114
; COMMON-NEXT: br x0
115115
; BRK-NEXT: .[[FAIL]]:
116-
; BRK-NEXT: brk #0xc471
116+
; BRK-NEXT: brk #0xc470
117117
%call = tail call i32 %fptr()
118118
ret i32 %call
119119
}
120120

121+
define i32 @tailcall_ib_key() "sign-return-address"="all" "sign-return-address-key"="b_key" {
122+
; COMMON-LABEL: tailcall_ib_key:
123+
;
124+
; COMMON: b callee
125+
; BRK-NEXT: .{{LBB.*}}:
126+
; BRK-NEXT: brk #0xc471
127+
tail call void asm sideeffect "", "~{lr}"()
128+
%call = tail call i32 @callee()
129+
ret i32 %call
130+
}
131+
121132
declare i32 @callee()

0 commit comments

Comments
 (0)