Skip to content

Commit 7a71b56

Browse files
committed
[BOLT] Gadget scanner: make use of C++17 features and LLVM helpers
Perform trivial syntactical cleanups: * make use of structured binding declarations * use LLVM utility functions when appropriate * omit braces around single expression inside single-line LLVM_DEBUG() This patch is NFC aside from minor debug output changes.
1 parent 9ef4b06 commit 7a71b56

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

bolt/lib/Passes/PAuthGadgetScanner.cpp

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class TrackedRegisters {
8888
TrackedRegisters(ArrayRef<MCPhysReg> RegsToTrack)
8989
: Registers(RegsToTrack),
9090
RegToIndexMapping(getMappingSize(RegsToTrack), NoIndex) {
91-
for (unsigned I = 0; I < RegsToTrack.size(); ++I)
92-
RegToIndexMapping[RegsToTrack[I]] = I;
91+
for (auto [MappedIndex, Reg] : llvm::enumerate(RegsToTrack))
92+
RegToIndexMapping[Reg] = MappedIndex;
9393
}
9494

9595
ArrayRef<MCPhysReg> getRegisters() const { return Registers; }
@@ -203,9 +203,9 @@ struct SrcState {
203203

204204
SafeToDerefRegs &= StateIn.SafeToDerefRegs;
205205
TrustedRegs &= StateIn.TrustedRegs;
206-
for (unsigned I = 0; I < LastInstWritingReg.size(); ++I)
207-
for (const MCInst *J : StateIn.LastInstWritingReg[I])
208-
LastInstWritingReg[I].insert(J);
206+
for (auto [ThisSet, OtherSet] :
207+
llvm::zip_equal(LastInstWritingReg, StateIn.LastInstWritingReg))
208+
ThisSet.insert_range(OtherSet);
209209
return *this;
210210
}
211211

@@ -224,11 +224,9 @@ struct SrcState {
224224
static void printInstsShort(raw_ostream &OS,
225225
ArrayRef<SetOfRelatedInsts> Insts) {
226226
OS << "Insts: ";
227-
for (unsigned I = 0; I < Insts.size(); ++I) {
228-
auto &Set = Insts[I];
227+
for (auto [I, PtrSet] : llvm::enumerate(Insts)) {
229228
OS << "[" << I << "](";
230-
for (const MCInst *MCInstP : Set)
231-
OS << MCInstP << " ";
229+
interleave(PtrSet, OS, " ");
232230
OS << ")";
233231
}
234232
}
@@ -416,8 +414,9 @@ class SrcSafetyAnalysis {
416414
// ... an address can be updated in a safe manner, producing the result
417415
// which is as trusted as the input address.
418416
if (auto DstAndSrc = BC.MIB->analyzeAddressArithmeticsForPtrAuth(Point)) {
419-
if (Cur.SafeToDerefRegs[DstAndSrc->second])
420-
Regs.push_back(DstAndSrc->first);
417+
auto [DstReg, SrcReg] = *DstAndSrc;
418+
if (Cur.SafeToDerefRegs[SrcReg])
419+
Regs.push_back(DstReg);
421420
}
422421

423422
// Make sure explicit checker sequence keeps register safe-to-dereference
@@ -469,8 +468,9 @@ class SrcSafetyAnalysis {
469468
// ... an address can be updated in a safe manner, producing the result
470469
// which is as trusted as the input address.
471470
if (auto DstAndSrc = BC.MIB->analyzeAddressArithmeticsForPtrAuth(Point)) {
472-
if (Cur.TrustedRegs[DstAndSrc->second])
473-
Regs.push_back(DstAndSrc->first);
471+
auto [DstReg, SrcReg] = *DstAndSrc;
472+
if (Cur.TrustedRegs[SrcReg])
473+
Regs.push_back(DstReg);
474474
}
475475

476476
return Regs;
@@ -858,9 +858,9 @@ struct DstState {
858858
return (*this = StateIn);
859859

860860
CannotEscapeUnchecked &= StateIn.CannotEscapeUnchecked;
861-
for (unsigned I = 0; I < FirstInstLeakingReg.size(); ++I)
862-
for (const MCInst *J : StateIn.FirstInstLeakingReg[I])
863-
FirstInstLeakingReg[I].insert(J);
861+
for (auto [ThisSet, OtherSet] :
862+
llvm::zip_equal(FirstInstLeakingReg, StateIn.FirstInstLeakingReg))
863+
ThisSet.insert_range(OtherSet);
864864
return *this;
865865
}
866866

@@ -1025,8 +1025,7 @@ class DstSafetyAnalysis {
10251025

10261026
// ... an address can be updated in a safe manner, or
10271027
if (auto DstAndSrc = BC.MIB->analyzeAddressArithmeticsForPtrAuth(Inst)) {
1028-
MCPhysReg DstReg, SrcReg;
1029-
std::tie(DstReg, SrcReg) = *DstAndSrc;
1028+
auto [DstReg, SrcReg] = *DstAndSrc;
10301029
// Note that *all* registers containing the derived values must be safe,
10311030
// both source and destination ones. No temporaries are supported at now.
10321031
if (Cur.CannotEscapeUnchecked[SrcReg] &&
@@ -1065,7 +1064,7 @@ class DstSafetyAnalysis {
10651064
// If this instruction terminates the program immediately, no
10661065
// authentication oracles are possible past this point.
10671066
if (BC.MIB->isTrap(Point)) {
1068-
LLVM_DEBUG({ traceInst(BC, "Trap instruction found", Point); });
1067+
LLVM_DEBUG(traceInst(BC, "Trap instruction found", Point));
10691068
DstState Next(NumRegs, RegsToTrackInstsFor.getNumTrackedRegisters());
10701069
Next.CannotEscapeUnchecked.set();
10711070
return Next;
@@ -1243,7 +1242,7 @@ class CFGUnawareDstSafetyAnalysis : public DstSafetyAnalysis,
12431242
// starting to analyze Inst.
12441243
if (BC.MIB->isCall(Inst) || BC.MIB->isBranch(Inst) ||
12451244
BC.MIB->isReturn(Inst)) {
1246-
LLVM_DEBUG({ traceInst(BC, "Control flow instruction", Inst); });
1245+
LLVM_DEBUG(traceInst(BC, "Control flow instruction", Inst));
12471246
S = createUnsafeState();
12481247
}
12491248

@@ -1381,12 +1380,12 @@ shouldReportUnsafeTailCall(const BinaryContext &BC, const BinaryFunction &BF,
13811380
// such libc, ignore tail calls performed by ELF entry function.
13821381
if (BC.StartFunctionAddress &&
13831382
*BC.StartFunctionAddress == Inst.getFunction()->getAddress()) {
1384-
LLVM_DEBUG({ dbgs() << " Skipping tail call in ELF entry function.\n"; });
1383+
LLVM_DEBUG(dbgs() << " Skipping tail call in ELF entry function.\n");
13851384
return std::nullopt;
13861385
}
13871386

13881387
if (BC.MIB->isSafeJumpTableBranchForPtrAuth(Inst)) {
1389-
LLVM_DEBUG({ dbgs() << " Safe jump table detected, skipping.\n"; });
1388+
LLVM_DEBUG(dbgs() << " Safe jump table detected, skipping.\n");
13901389
return std::nullopt;
13911390
}
13921391

@@ -1421,7 +1420,7 @@ shouldReportCallGadget(const BinaryContext &BC, const MCInstReference &Inst,
14211420
return std::nullopt;
14221421

14231422
if (BC.MIB->isSafeJumpTableBranchForPtrAuth(Inst)) {
1424-
LLVM_DEBUG({ dbgs() << " Safe jump table detected, skipping.\n"; });
1423+
LLVM_DEBUG(dbgs() << " Safe jump table detected, skipping.\n");
14251424
return std::nullopt;
14261425
}
14271426

@@ -1466,7 +1465,7 @@ shouldReportAuthOracle(const BinaryContext &BC, const MCInstReference &Inst,
14661465
});
14671466

14681467
if (S.empty()) {
1469-
LLVM_DEBUG({ dbgs() << " DstState is empty!\n"; });
1468+
LLVM_DEBUG(dbgs() << " DstState is empty!\n");
14701469
return make_generic_report(
14711470
Inst, "Warning: no state computed for an authentication instruction "
14721471
"(possibly unreachable)");
@@ -1493,7 +1492,7 @@ collectRegsToTrack(ArrayRef<PartialReport<MCPhysReg>> Reports) {
14931492
void FunctionAnalysisContext::findUnsafeUses(
14941493
SmallVector<PartialReport<MCPhysReg>> &Reports) {
14951494
auto Analysis = SrcSafetyAnalysis::create(BF, AllocatorId, {});
1496-
LLVM_DEBUG({ dbgs() << "Running src register safety analysis...\n"; });
1495+
LLVM_DEBUG(dbgs() << "Running src register safety analysis...\n");
14971496
Analysis->run();
14981497
LLVM_DEBUG({
14991498
dbgs() << "After src register safety analysis:\n";
@@ -1545,8 +1544,7 @@ void FunctionAnalysisContext::findUnsafeUses(
15451544

15461545
const SrcState &S = Analysis->getStateBefore(Inst);
15471546
if (S.empty()) {
1548-
LLVM_DEBUG(
1549-
{ traceInst(BC, "Instruction has no state, skipping", Inst); });
1547+
LLVM_DEBUG(traceInst(BC, "Instruction has no state, skipping", Inst));
15501548
assert(UnreachableBBReported && "Should be reported at least once");
15511549
(void)UnreachableBBReported;
15521550
return;
@@ -1573,8 +1571,7 @@ void FunctionAnalysisContext::augmentUnsafeUseReports(
15731571
SmallVector<MCPhysReg> RegsToTrack = collectRegsToTrack(Reports);
15741572
// Re-compute the analysis with register tracking.
15751573
auto Analysis = SrcSafetyAnalysis::create(BF, AllocatorId, RegsToTrack);
1576-
LLVM_DEBUG(
1577-
{ dbgs() << "\nRunning detailed src register safety analysis...\n"; });
1574+
LLVM_DEBUG(dbgs() << "\nRunning detailed src register safety analysis...\n");
15781575
Analysis->run();
15791576
LLVM_DEBUG({
15801577
dbgs() << "After detailed src register safety analysis:\n";
@@ -1584,7 +1581,7 @@ void FunctionAnalysisContext::augmentUnsafeUseReports(
15841581
// Augment gadget reports.
15851582
for (auto &Report : Reports) {
15861583
MCInstReference Location = Report.Issue->Location;
1587-
LLVM_DEBUG({ traceInst(BC, "Attaching clobbering info to", Location); });
1584+
LLVM_DEBUG(traceInst(BC, "Attaching clobbering info to", Location));
15881585
assert(Report.RequestedDetails &&
15891586
"Should be removed by handleSimpleReports");
15901587
auto DetailedInfo =
@@ -1602,7 +1599,7 @@ void FunctionAnalysisContext::findUnsafeDefs(
16021599
return;
16031600

16041601
auto Analysis = DstSafetyAnalysis::create(BF, AllocatorId, {});
1605-
LLVM_DEBUG({ dbgs() << "Running dst register safety analysis...\n"; });
1602+
LLVM_DEBUG(dbgs() << "Running dst register safety analysis...\n");
16061603
Analysis->run();
16071604
LLVM_DEBUG({
16081605
dbgs() << "After dst register safety analysis:\n";
@@ -1625,8 +1622,7 @@ void FunctionAnalysisContext::augmentUnsafeDefReports(
16251622
SmallVector<MCPhysReg> RegsToTrack = collectRegsToTrack(Reports);
16261623
// Re-compute the analysis with register tracking.
16271624
auto Analysis = DstSafetyAnalysis::create(BF, AllocatorId, RegsToTrack);
1628-
LLVM_DEBUG(
1629-
{ dbgs() << "\nRunning detailed dst register safety analysis...\n"; });
1625+
LLVM_DEBUG(dbgs() << "\nRunning detailed dst register safety analysis...\n");
16301626
Analysis->run();
16311627
LLVM_DEBUG({
16321628
dbgs() << "After detailed dst register safety analysis:\n";
@@ -1636,7 +1632,7 @@ void FunctionAnalysisContext::augmentUnsafeDefReports(
16361632
// Augment gadget reports.
16371633
for (auto &Report : Reports) {
16381634
MCInstReference Location = Report.Issue->Location;
1639-
LLVM_DEBUG({ traceInst(BC, "Attaching leakage info to", Location); });
1635+
LLVM_DEBUG(traceInst(BC, "Attaching leakage info to", Location));
16401636
assert(Report.RequestedDetails &&
16411637
"Should be removed by handleSimpleReports");
16421638
auto DetailedInfo = std::make_shared<LeakageInfo>(
@@ -1769,8 +1765,7 @@ static void printRelatedInstrs(raw_ostream &OS, const MCInstReference Location,
17691765
// Sort the references to make output deterministic.
17701766
SmallVector<MCInstReference> RI(RelatedInstrs);
17711767
llvm::sort(RI);
1772-
for (unsigned I = 0; I < RI.size(); ++I) {
1773-
MCInstReference InstRef = RI[I];
1768+
for (auto [I, InstRef] : llvm::enumerate(RI)) {
17741769
OS << " " << (I + 1) << ". ";
17751770
BC.printInstruction(OS, InstRef, getAddress(InstRef), &BF);
17761771
};

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ clobber:
177177
// CHECK-EMPTY:
178178
// CHECK-NEXT: Running detailed src register safety analysis...
179179
// CHECK-NEXT: SrcSafetyAnalysis::ComputeNext( mov w30, #0x0, src-state<SafeToDerefRegs: LR W30 W30_HI , TrustedRegs: LR W30 W30_HI , Insts: [0]()>)
180-
// CHECK-NEXT: .. result: (src-state<SafeToDerefRegs: W30_HI , TrustedRegs: W30_HI , Insts: [0](0x{{[0-9a-f]+}} )>)
181-
// CHECK-NEXT: SrcSafetyAnalysis::ComputeNext( ret x30, src-state<SafeToDerefRegs: W30_HI , TrustedRegs: W30_HI , Insts: [0](0x{{[0-9a-f]+}} )>)
182-
// CHECK-NEXT: .. result: (src-state<SafeToDerefRegs: W30_HI , TrustedRegs: W30_HI , Insts: [0](0x{{[0-9a-f]+}} )>)
180+
// CHECK-NEXT: .. result: (src-state<SafeToDerefRegs: W30_HI , TrustedRegs: W30_HI , Insts: [0](0x{{[0-9a-f]+}})>)
181+
// CHECK-NEXT: SrcSafetyAnalysis::ComputeNext( ret x30, src-state<SafeToDerefRegs: W30_HI , TrustedRegs: W30_HI , Insts: [0](0x{{[0-9a-f]+}})>)
182+
// CHECK-NEXT: .. result: (src-state<SafeToDerefRegs: W30_HI , TrustedRegs: W30_HI , Insts: [0](0x{{[0-9a-f]+}})>)
183183
// CHECK-NEXT: After detailed src register safety analysis:
184184
// CHECK-NEXT: Binary Function "clobber" {
185185
// ...
@@ -189,7 +189,7 @@ clobber:
189189
// Iterating over the reports and attaching clobbering info:
190190

191191
// CHECK-EMPTY:
192-
// CHECK-NEXT: Attaching clobbering info to: 00000000: ret # DataflowSrcSafetyAnalysis: src-state<SafeToDerefRegs: BitVector, TrustedRegs: BitVector, Insts: [0](0x{{[0-9a-f]+}} )>
192+
// CHECK-NEXT: Attaching clobbering info to: 00000000: ret # DataflowSrcSafetyAnalysis: src-state<SafeToDerefRegs: BitVector, TrustedRegs: BitVector, Insts: [0](0x{{[0-9a-f]+}})>
193193

194194
.globl nocfg
195195
.type nocfg,@function
@@ -315,7 +315,7 @@ auth_oracle:
315315
// AUTH-ORACLES-NEXT: DstSafetyAnalysis::ComputeNext( ret x30, dst-state<CannotEscapeUnchecked: , Insts: [0]()>)
316316
// AUTH-ORACLES-NEXT: .. result: (dst-state<CannotEscapeUnchecked: LR W30 W30_HI , Insts: [0]()>)
317317
// AUTH-ORACLES-NEXT: DstSafetyAnalysis::ComputeNext( autia x0, x1, dst-state<CannotEscapeUnchecked: LR W30 W30_HI , Insts: [0]()>)
318-
// AUTH-ORACLES-NEXT: .. result: (dst-state<CannotEscapeUnchecked: LR W30 W30_HI , Insts: [0](0x{{[0-9a-f]+}} )>)
318+
// AUTH-ORACLES-NEXT: .. result: (dst-state<CannotEscapeUnchecked: LR W30 W30_HI , Insts: [0](0x{{[0-9a-f]+}})>)
319319
// AUTH-ORACLES-NEXT: After detailed dst register safety analysis:
320320
// AUTH-ORACLES-NEXT: Binary Function "auth_oracle" {
321321
// AUTH-ORACLES-NEXT: Number : 4
@@ -325,14 +325,14 @@ auth_oracle:
325325
// AUTH-ORACLES-NEXT: }
326326
// AUTH-ORACLES-NEXT: [[BB0]] (2 instructions, align : 1)
327327
// AUTH-ORACLES-NEXT: Entry Point
328-
// AUTH-ORACLES-NEXT: 00000000: autia x0, x1 # DataflowDstSafetyAnalysis: dst-state<CannotEscapeUnchecked: BitVector, Insts: [0](0x{{[0-9a-f]+}} )>
328+
// AUTH-ORACLES-NEXT: 00000000: autia x0, x1 # DataflowDstSafetyAnalysis: dst-state<CannotEscapeUnchecked: BitVector, Insts: [0](0x{{[0-9a-f]+}})>
329329
// AUTH-ORACLES-NEXT: 00000004: ret # DataflowDstSafetyAnalysis: dst-state<CannotEscapeUnchecked: BitVector, Insts: [0]()>
330330
// AUTH-ORACLES-EMPTY:
331331
// AUTH-ORACLES-NEXT: DWARF CFI Instructions:
332332
// AUTH-ORACLES-NEXT: <empty>
333333
// AUTH-ORACLES-NEXT: End of Function "auth_oracle"
334334
// AUTH-ORACLES-EMPTY:
335-
// AUTH-ORACLES-NEXT: Attaching leakage info to: 00000000: autia x0, x1 # DataflowDstSafetyAnalysis: dst-state<CannotEscapeUnchecked: BitVector, Insts: [0](0x{{[0-9a-f]+}} )>
335+
// AUTH-ORACLES-NEXT: Attaching leakage info to: 00000000: autia x0, x1 # DataflowDstSafetyAnalysis: dst-state<CannotEscapeUnchecked: BitVector, Insts: [0](0x{{[0-9a-f]+}})>
336336

337337
// Gadget scanner should not crash on CFI instructions, including when debug-printing them.
338338
// Note that the particular debug output is not checked, but BOLT should be

0 commit comments

Comments
 (0)