Skip to content

Commit 4f2ee07

Browse files
authored
[BOLT][AArch64] Do not crash on authenticated branch instructions (#129898)
When an indirect branch instruction is decoded, analyzeIndirectBranch method is asked if this is a well-known code pattern. On AArch64, the only special pattern which is detected is Jump Table, emitted as a branch to the sum of a constant base address and a variable offset. Therefore, `Inst.getOpcode()` being one of `AArch64::BRA*` means Inst cannot belong to such Jump Table pattern, thus returning early.
1 parent 8cc6c2e commit 4f2ee07

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,18 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
547547
return false;
548548
}
549549

550+
bool isBRA(const MCInst &Inst) const {
551+
switch (Inst.getOpcode()) {
552+
case AArch64::BRAA:
553+
case AArch64::BRAB:
554+
case AArch64::BRAAZ:
555+
case AArch64::BRABZ:
556+
return true;
557+
default:
558+
return false;
559+
}
560+
}
561+
550562
bool mayLoad(const MCInst &Inst) const override {
551563
return isLDRB(Inst) || isLDRH(Inst) || isLDRW(Inst) || isLDRX(Inst) ||
552564
isLDRQ(Inst) || isLDRD(Inst) || isLDRS(Inst);
@@ -941,6 +953,11 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
941953
DenseMap<const MCInst *, SmallVector<MCInst *, 4>> &UDChain,
942954
const MCExpr *&JumpTable, int64_t &Offset, int64_t &ScaleValue,
943955
MCInst *&PCRelBase) const {
956+
// The only kind of indirect branches we match is jump table, thus ignore
957+
// authenticating branch instructions early.
958+
if (isBRA(Inst))
959+
return false;
960+
944961
// Expect AArch64 BR
945962
assert(Inst.getOpcode() == AArch64::BR && "Unexpected opcode");
946963

bolt/test/AArch64/test-indirect-branch.s

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// REQUIRES: system-linux, asserts
77

8-
// RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
8+
// RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown -mattr=+pauth %s -o %t.o
99
// RUN: %clang %cflags --target=aarch64-unknown-linux %t.o -o %t.exe -Wl,-q
1010
// RUN: llvm-bolt %t.exe -o %t.bolt --print-cfg --strict --debug-only=mcplus \
1111
// RUN: -v=1 2>&1 | FileCheck %s
@@ -73,6 +73,27 @@ test2_0:
7373
test2_1:
7474
ret
7575

76+
// Make sure BOLT does not crash trying to disassemble BRA* instructions.
77+
.globl test_braa
78+
.type test_braa, %function
79+
test_braa:
80+
braa x0, x1
81+
82+
.globl test_brab
83+
.type test_brab, %function
84+
test_brab:
85+
brab x0, x1
86+
87+
.globl test_braaz
88+
.type test_braaz, %function
89+
test_braaz:
90+
braaz x0
91+
92+
.globl test_brabz
93+
.type test_brabz, %function
94+
test_brabz:
95+
brabz x0
96+
7697
.section .rodata,"a",@progbits
7798
datatable:
7899
.word test1_0-datatable

0 commit comments

Comments
 (0)