Skip to content

Commit 053632d

Browse files
committed
[AArch64][PAC] Refine authenticated pointer check methods
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 85184b4 commit 053632d

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;
@@ -174,7 +178,7 @@ MachineBasicBlock &llvm::AArch64PAuth::checkAuthenticatedRegister(
174178
return MBB;
175179
case AuthCheckMethod::DummyLoad:
176180
BuildMI(MBB, MBBI, DL, TII->get(AArch64::LDRWui), getWRegFromXReg(TmpReg))
177-
.addReg(AArch64::LR)
181+
.addReg(AuthenticatedReg)
178182
.addImm(0)
179183
.addMemOperand(createCheckMemOperand(MF, Subtarget));
180184
return MBB;
@@ -250,6 +254,10 @@ unsigned llvm::AArch64PAuth::getCheckerSizeInBytes(AuthCheckMethod Method) {
250254

251255
bool AArch64PointerAuth::checkAuthenticatedLR(
252256
MachineBasicBlock::iterator TI) const {
257+
const AArch64FunctionInfo *MFnI = TI->getMF()->getInfo<AArch64FunctionInfo>();
258+
AArch64PACKey::ID KeyId =
259+
MFnI->shouldSignWithBKey() ? AArch64PACKey::IB : AArch64PACKey::IA;
260+
253261
AuthCheckMethod Method = Subtarget->getAuthenticatedLRCheckMethod();
254262

255263
if (Method == AuthCheckMethod::None)
@@ -290,7 +298,7 @@ bool AArch64PointerAuth::checkAuthenticatedLR(
290298
"More than a single register is used by TCRETURN");
291299

292300
checkAuthenticatedRegister(TI, Method, AArch64::LR, TmpReg, /*UseIKey=*/true,
293-
BrkOperand);
301+
BrkOperandForKey(KeyId));
294302

295303
return true;
296304
}

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)