Skip to content

Commit 25fda06

Browse files
committed
[BOLT] Gadget scanner: do not crash on debug-printing CFI instructions
Some instruction-printing code used under LLVM_DEBUG does not handle CFI instructions well. While CFI instructions seem to be harmless for the correctness of the analysis results, they do not convey any useful information to the analysis either, so skip them early.
1 parent bd11638 commit 25fda06

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

bolt/lib/Passes/PAuthGadgetScanner.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ class SrcSafetyAnalysis {
430430
}
431431

432432
SrcState computeNext(const MCInst &Point, const SrcState &Cur) {
433+
if (BC.MIB->isCFI(Point))
434+
return Cur;
435+
433436
SrcStatePrinter P(BC);
434437
LLVM_DEBUG({
435438
dbgs() << " SrcSafetyAnalysis::ComputeNext(";
@@ -704,6 +707,8 @@ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis,
704707
SrcState S = createEntryState();
705708
for (auto &I : BF.instrs()) {
706709
MCInst &Inst = I.second;
710+
if (BC.MIB->isCFI(Inst))
711+
continue;
707712

708713
// If there is a label before this instruction, it is possible that it
709714
// can be jumped-to, thus conservatively resetting S. As an exception,
@@ -985,6 +990,9 @@ class DstSafetyAnalysis {
985990
}
986991

987992
DstState computeNext(const MCInst &Point, const DstState &Cur) {
993+
if (BC.MIB->isCFI(Point))
994+
return Cur;
995+
988996
DstStatePrinter P(BC);
989997
LLVM_DEBUG({
990998
dbgs() << " DstSafetyAnalysis::ComputeNext(";
@@ -1156,6 +1164,8 @@ class CFGUnawareDstSafetyAnalysis : public DstSafetyAnalysis,
11561164
DstState S = createUnsafeState();
11571165
for (auto &I : llvm::reverse(BF.instrs())) {
11581166
MCInst &Inst = I.second;
1167+
if (BC.MIB->isCFI(Inst))
1168+
continue;
11591169

11601170
// If Inst can change the control flow, we cannot be sure that the next
11611171
// instruction (to be executed in analyzed program) is the one processed
@@ -1346,6 +1356,9 @@ void FunctionAnalysisContext::findUnsafeUses(
13461356
});
13471357

13481358
iterateOverInstrs(BF, [&](MCInstReference Inst) {
1359+
if (BC.MIB->isCFI(Inst))
1360+
return;
1361+
13491362
const SrcState &S = Analysis->getStateBefore(Inst);
13501363

13511364
// If non-empty state was never propagated from the entry basic block
@@ -1409,6 +1422,9 @@ void FunctionAnalysisContext::findUnsafeDefs(
14091422
});
14101423

14111424
iterateOverInstrs(BF, [&](MCInstReference Inst) {
1425+
if (BC.MIB->isCFI(Inst))
1426+
return;
1427+
14121428
const DstState &S = Analysis->getStateAfter(Inst);
14131429

14141430
if (auto Report = shouldReportAuthOracle(BC, Inst, S))

bolt/test/binary-analysis/AArch64/gs-pauth-debug-output.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,38 @@ auth_oracle:
329329
// PAUTH-EMPTY:
330330
// PAUTH-NEXT: Attaching leakage info to: 00000000: autia x0, x1 # DataflowDstSafetyAnalysis: dst-state<CannotEscapeUnchecked: BitVector, Insts: [0](0x{{[0-9a-f]+}} )>
331331

332+
// Gadget scanner should not crash on CFI instructions, including when debug-printing them.
333+
// Note that the particular debug output is not checked, but BOLT should be
334+
// compiled with assertions enabled to support -debug-only argument.
335+
336+
.globl cfi_inst_df
337+
.type cfi_inst_df,@function
338+
cfi_inst_df:
339+
.cfi_startproc
340+
sub sp, sp, #16
341+
.cfi_def_cfa_offset 16
342+
add sp, sp, #16
343+
.cfi_def_cfa_offset 0
344+
ret
345+
.size cfi_inst_df, .-cfi_inst_df
346+
.cfi_endproc
347+
348+
.globl cfi_inst_nocfg
349+
.type cfi_inst_nocfg,@function
350+
cfi_inst_nocfg:
351+
.cfi_startproc
352+
sub sp, sp, #16
353+
.cfi_def_cfa_offset 16
354+
355+
adr x0, 1f
356+
br x0
357+
1:
358+
add sp, sp, #16
359+
.cfi_def_cfa_offset 0
360+
ret
361+
.size cfi_inst_nocfg, .-cfi_inst_nocfg
362+
.cfi_endproc
363+
332364
// CHECK-LABEL:Analyzing function main, AllocatorId = 1
333365
.globl main
334366
.type main,@function

0 commit comments

Comments
 (0)