Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 992e377

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 672d0f1 commit 992e377

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

bolt/lib/Passes/PAuthGadgetScanner.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ class SrcSafetyAnalysis {
432432
}
433433

434434
SrcState computeNext(const MCInst &Point, const SrcState &Cur) {
435+
if (BC.MIB->isCFI(Point))
436+
return Cur;
437+
435438
SrcStatePrinter P(BC);
436439
LLVM_DEBUG({
437440
dbgs() << " SrcSafetyAnalysis::ComputeNext(";
@@ -675,6 +678,8 @@ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis {
675678
SrcState S = createEntryState();
676679
for (auto &I : BF.instrs()) {
677680
MCInst &Inst = I.second;
681+
if (BC.MIB->isCFI(Inst))
682+
continue;
678683

679684
// If there is a label before this instruction, it is possible that it
680685
// can be jumped-to, thus conservatively resetting S. As an exception,
@@ -952,6 +957,9 @@ class DstSafetyAnalysis {
952957
}
953958

954959
DstState computeNext(const MCInst &Point, const DstState &Cur) {
960+
if (BC.MIB->isCFI(Point))
961+
return Cur;
962+
955963
DstStatePrinter P(BC);
956964
LLVM_DEBUG({
957965
dbgs() << " DstSafetyAnalysis::ComputeNext(";
@@ -1128,6 +1136,8 @@ class CFGUnawareDstSafetyAnalysis : public DstSafetyAnalysis {
11281136
DstState S = createUnsafeState();
11291137
for (auto &I : llvm::reverse(BF.instrs())) {
11301138
MCInst &Inst = I.second;
1139+
if (BC.MIB->isCFI(Inst))
1140+
continue;
11311141

11321142
// If Inst can change the control flow, we cannot be sure that the next
11331143
// instruction (to be executed in analyzed program) is the one processed
@@ -1326,6 +1336,9 @@ void FunctionAnalysisContext::findUnsafeUses(
13261336
});
13271337

13281338
iterateOverInstrs(BF, [&](MCInstReference Inst) {
1339+
if (BC.MIB->isCFI(Inst))
1340+
return;
1341+
13291342
const SrcState &S = Analysis->getStateBefore(Inst);
13301343

13311344
// If non-empty state was never propagated from the entry basic block
@@ -1389,6 +1402,9 @@ void FunctionAnalysisContext::findUnsafeDefs(
13891402
});
13901403

13911404
iterateOverInstrs(BF, [&](MCInstReference Inst) {
1405+
if (BC.MIB->isCFI(Inst))
1406+
return;
1407+
13921408
const DstState &S = Analysis->getStateAfter(Inst);
13931409

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

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

+32
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>
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)