Skip to content

Commit 77a0b64

Browse files
committed
[BOLT][NFC] Rename Pointer Auth DWARF rewriter passes
Original names were "working titles". After initial patches are merged, I'd like to rename these passes to names that reflect their intent better and show their relationship to each other: InsertNegateRAStatePass renamed to PointerAuthCFIFixup, MarkRAStates renamed to PointerAuthCFIAnalyzer.
1 parent 0453667 commit 77a0b64

File tree

14 files changed

+73
-72
lines changed

14 files changed

+73
-72
lines changed

bolt/docs/PacRetDesign.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ negate-ra-state CFIs will become invalid during BasicBlock reordering.
104104
## Solution design
105105

106106
The implementation introduces two new passes:
107-
1. `MarkRAStatesPass`: assigns the RA state to each instruction based on the CFIs
108-
in the input binary
109-
2. `InsertNegateRAStatePass`: reads those assigned instruction RA states after
107+
1. `PointerAuthCFIAnalyzer`: assigns the RA state to each instruction based on
108+
the CFI in the input binary
109+
2. `PointerAuthCFIFixup`: reads those assigned instruction RA states after
110110
optimizations, and emits `DW_CFA_AARCH64_negate_ra_state` CFIs at the correct
111111
places: wherever there is a state change between two consecutive instructions
112112
in the layout order.
@@ -129,7 +129,7 @@ instruction.
129129
This special case is handled by adding an `initialRAState` bool to each BinaryFunction.
130130
If the `Offset` the CFI refers to is zero, we don't store an annotation, but set
131131
the `initialRAState` in `FillCFIInfoFor`. This information is then used in
132-
`MarkRAStates`.
132+
`PointerAuthCFIAnalyzer`.
133133

134134
### Binaries without DWARF info
135135

@@ -146,7 +146,7 @@ In summary:
146146
- pointer auth is used, and we have DWARF CFIs: passes run, and rewrite the
147147
negate-ra-state CFI.
148148

149-
### MarkRAStates pass
149+
### PointerAuthCFIAnalyzer pass
150150

151151
This pass runs before optimizations reorder anything.
152152

@@ -173,9 +173,9 @@ what we have before the pass, and after it.
173173
| autiasp | negate-ra-state | signed |
174174
| ret | | unsigned |
175175

176-
##### Error handling in MarkRAState Pass:
176+
##### Error handling in PointerAuthCFIAnalyzer pass:
177177

178-
Whenever the MarkRAStates pass finds inconsistencies in the current
178+
Whenever the PointerAuthCFIAnalyzer pass finds inconsistencies in the current
179179
BinaryFunction, it marks the function as ignored using `BF.setIgnored()`. BOLT
180180
will not optimize this function but will emit it unchanged in the original section
181181
(`.bolt.org.text`).
@@ -188,16 +188,17 @@ The inconsistencies are as follows:
188188
Users will be informed about the number of ignored functions in the pass, the
189189
exact functions ignored, and the found inconsistency.
190190

191-
### InsertNegateRAStatePass
191+
### PointerAuthCFIFixup
192192

193-
This pass runs after optimizations. It performns the _inverse_ of MarkRAState pa s:
193+
This pass runs after optimizations. It performns the _inverse_ of PointerAuthCFIAnalyzer
194+
pass:
194195
1. it reads the RA state annotations attached to the instructions, and
195196
2. whenever the state changes, it adds a PseudoInstruction that holds an
196197
OpNegateRAState CFI.
197198

198199
##### Covering newly generated instructions:
199200

200-
Some BOLT passes can add new Instructions. In InsertNegateRAStatePass, we have
201+
Some BOLT passes can add new Instructions. In PointerAuthCFIFixup, we have
201202
to know what RA state these have.
202203

203204
> [!important]
@@ -230,7 +231,7 @@ freely. The only special case is function splitting. When a function is split,
230231
the split part becomes a new function in the emitted binary. For unwinding to
231232
work, it needs to "replay" all CFIs that lead up to the split point. BOLT does
232233
this for other CFIs. As negate-ra-state is not read (only stored as an Annotation),
233-
we have to do this manually in InsertNegateRAStatePass. Here, if the split part
234+
we have to do this manually in PointerAuthCFIFixup. Here, if the split part
234235
starts with an instruction that has Signed RA state, we add a negate-ra-state CFI
235236
to indicate this.
236237

bolt/include/bolt/Passes/MarkRAStates.h renamed to bolt/include/bolt/Passes/PointerAuthCFIAnalyzer.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
//===- bolt/Passes/MarkRAStates.cpp ---------------------------------===//
1+
//===- bolt/Passes/PointerAuthCFIAnalyzer.h -------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file implements the MarkRAStates class.
9+
// This file implements the PointerAuthCFIAnalyzer class.
1010
//
1111
//===----------------------------------------------------------------------===//
12-
#ifndef BOLT_PASSES_MARK_RA_STATES
13-
#define BOLT_PASSES_MARK_RA_STATES
12+
#ifndef BOLT_PASSES_POINTER_AUTH_CFI_ANALYZER
13+
#define BOLT_PASSES_POINTER_AUTH_CFI_ANALYZER
1414

1515
#include "bolt/Passes/BinaryPasses.h"
1616
#include <mutex>
1717

1818
namespace llvm {
1919
namespace bolt {
2020

21-
class MarkRAStates : public BinaryFunctionPass {
21+
class PointerAuthCFIAnalyzer : public BinaryFunctionPass {
2222
// setIgnored() is not thread-safe, but the pass is running on functions in
2323
// parallel.
2424
std::mutex IgnoreMutex;
2525

2626
public:
27-
explicit MarkRAStates() : BinaryFunctionPass(false) {}
27+
explicit PointerAuthCFIAnalyzer() : BinaryFunctionPass(false) {}
2828

29-
const char *getName() const override { return "mark-ra-states"; }
29+
const char *getName() const override { return "pointer-auth-cfi-analyzer"; }
3030

3131
/// Pass entry point
3232
Error runOnFunctions(BinaryContext &BC) override;

bolt/include/bolt/Passes/InsertNegateRAStatePass.h renamed to bolt/include/bolt/Passes/PointerAuthCFIFixup.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
//===- bolt/Passes/InsertNegateRAStatePass.h ------------------------------===//
1+
//===- bolt/Passes/PointerAuthCFIFixup.h ----------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file implements the InsertNegateRAStatePass class.
9+
// This file implements the PointerAuthCFIFixup class.
1010
//
1111
//===----------------------------------------------------------------------===//
12-
#ifndef BOLT_PASSES_INSERT_NEGATE_RA_STATE_PASS
13-
#define BOLT_PASSES_INSERT_NEGATE_RA_STATE_PASS
12+
#ifndef BOLT_PASSES_POINTER_AUTH_CFI_FIXUP
13+
#define BOLT_PASSES_POINTER_AUTH_CFI_FIXUP
1414

1515
#include "bolt/Passes/BinaryPasses.h"
1616

1717
namespace llvm {
1818
namespace bolt {
1919

20-
class InsertNegateRAState : public BinaryFunctionPass {
20+
class PointerAuthCFIFixup : public BinaryFunctionPass {
2121
public:
22-
explicit InsertNegateRAState() : BinaryFunctionPass(false) {}
22+
explicit PointerAuthCFIFixup() : BinaryFunctionPass(false) {}
2323

24-
const char *getName() const override { return "insert-negate-ra-state-pass"; }
24+
const char *getName() const override { return "pointer-auth-cfi-fixup"; }
2525

2626
/// Pass entry point
2727
Error runOnFunctions(BinaryContext &BC) override;

bolt/lib/Core/Exceptions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ bool CFIReaderWriter::fillCFIInfoFor(BinaryFunction &Function) const {
572572
if (Function.getBinaryContext().isAArch64()) {
573573
// Support for pointer authentication:
574574
// We need to annotate instructions that modify the RA State, to work
575-
// out the state of each instruction in MarkRAStates Pass.
575+
// out the state of each instruction in PointerAuthCFIAnalyzer Pass.
576576
if (Offset != 0)
577577
Function.setInstModifiesRAState(DW_CFA_remember_state, Offset);
578578
}
@@ -583,7 +583,7 @@ bool CFIReaderWriter::fillCFIInfoFor(BinaryFunction &Function) const {
583583
if (Function.getBinaryContext().isAArch64()) {
584584
// Support for pointer authentication:
585585
// We need to annotate instructions that modify the RA State, to work
586-
// out the state of each instruction in MarkRAStates Pass.
586+
// out the state of each instruction in PointerAuthCFIAnalyzer Pass.
587587
if (Offset != 0)
588588
Function.setInstModifiesRAState(DW_CFA_restore_state, Offset);
589589
}
@@ -652,15 +652,15 @@ bool CFIReaderWriter::fillCFIInfoFor(BinaryFunction &Function) const {
652652
// BasicBlocks, which changes during optimizations. Instead of adding
653653
// OpNegateRAState CFIs, an annotation is added to the instruction, to
654654
// mark that the instruction modifies the RA State. The actual state for
655-
// instructions are worked out in MarkRAStates based on these
655+
// instructions are worked out in PointerAuthCFIAnalyzer based on these
656656
// annotations.
657657
if (Offset != 0)
658658
Function.setInstModifiesRAState(DW_CFA_AARCH64_negate_ra_state,
659659
Offset);
660660
else
661661
// We cannot Annotate an instruction at Offset == 0.
662662
// Instead, we save the initial (Signed) state, and push it to
663-
// MarkRAStates' RAStateStack.
663+
// PointerAuthCFIAnalyzer's RAStateStack.
664664
Function.setInitialRAState(true);
665665
break;
666666
}

bolt/lib/Passes/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ add_llvm_library(LLVMBOLTPasses
1717
IdenticalCodeFolding.cpp
1818
IndirectCallPromotion.cpp
1919
Inliner.cpp
20-
InsertNegateRAStatePass.cpp
2120
Instrumentation.cpp
2221
JTFootprintReduction.cpp
2322
LongJmp.cpp
2423
LoopInversionPass.cpp
2524
LivenessAnalysis.cpp
2625
MCF.cpp
27-
MarkRAStates.cpp
2826
PatchEntries.cpp
2927
PAuthGadgetScanner.cpp
3028
PettisAndHansen.cpp
3129
PLTCall.cpp
30+
PointerAuthCFIAnalyzer.cpp
31+
PointerAuthCFIFixup.cpp
3232
ProfileQualityStats.cpp
3333
RegAnalysis.cpp
3434
RegReAssign.cpp

bolt/lib/Passes/MarkRAStates.cpp renamed to bolt/lib/Passes/PointerAuthCFIAnalyzer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//===- bolt/Passes/MarkRAStates.cpp ---------------------------------===//
1+
//===- bolt/Passes/PointerAuthCFIAnalyzer.cpp -----------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file implements the MarkRAStates class.
9+
// This file implements the PointerAuthCFIAnalyzer class.
1010
// Three CFIs have an influence on the RA State of an instruction:
1111
// - NegateRAState flips the RA State,
1212
// - RememberState pushes the RA State to a stack,
@@ -16,10 +16,10 @@
1616
// the RA State of each instruction, and save it as new MCAnnotations. The new
1717
// annotations are Signing, Signed, Authenticating and Unsigned. After
1818
// optimizations, .cfi_negate_ra_state CFIs are added to the places where the
19-
// state changes in InsertNegateRAStatePass.
19+
// state changes in PointerAuthCFIFixup.
2020
//
2121
//===----------------------------------------------------------------------===//
22-
#include "bolt/Passes/MarkRAStates.h"
22+
#include "bolt/Passes/PointerAuthCFIAnalyzer.h"
2323
#include "bolt/Core/BinaryFunction.h"
2424
#include "bolt/Core/ParallelUtilities.h"
2525
#include <cstdlib>
@@ -31,7 +31,7 @@ using namespace llvm;
3131
namespace llvm {
3232
namespace bolt {
3333

34-
bool MarkRAStates::runOnFunction(BinaryFunction &BF) {
34+
bool PointerAuthCFIAnalyzer::runOnFunction(BinaryFunction &BF) {
3535

3636
BinaryContext &BC = BF.getBinaryContext();
3737

@@ -110,7 +110,7 @@ bool MarkRAStates::runOnFunction(BinaryFunction &BF) {
110110
return true;
111111
}
112112

113-
Error MarkRAStates::runOnFunctions(BinaryContext &BC) {
113+
Error PointerAuthCFIAnalyzer::runOnFunctions(BinaryContext &BC) {
114114
std::atomic<uint64_t> FunctionsIgnored{0};
115115
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
116116
if (!runOnFunction(BF)) {
@@ -132,8 +132,8 @@ Error MarkRAStates::runOnFunctions(BinaryContext &BC) {
132132

133133
ParallelUtilities::runOnEachFunction(
134134
BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun,
135-
SkipPredicate, "MarkRAStates");
136-
BC.outs() << "BOLT-INFO: MarkRAStates ran on " << Total
135+
SkipPredicate, "PointerAuthCFIAnalyzer");
136+
BC.outs() << "BOLT-INFO: PointerAuthCFIAnalyzer ran on " << Total
137137
<< " functions. Ignored " << FunctionsIgnored << " functions "
138138
<< format("(%.2lf%%)", (100.0 * FunctionsIgnored) / Total)
139139
<< " because of CFI inconsistencies\n";

bolt/lib/Passes/InsertNegateRAStatePass.cpp renamed to bolt/lib/Passes/PointerAuthCFIFixup.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
//===- bolt/Passes/InsertNegateRAStatePass.cpp ----------------------------===//
1+
//===- bolt/Passes/PointerAuthCFIFixup.cpp --------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file implements the InsertNegateRAStatePass class. It inserts
9+
// This file implements the PointerAuthCFIFixup class. It inserts
1010
// OpNegateRAState CFIs to places where the state of two consecutive
1111
// instructions are different.
1212
//
1313
//===----------------------------------------------------------------------===//
14-
#include "bolt/Passes/InsertNegateRAStatePass.h"
14+
#include "bolt/Passes/PointerAuthCFIFixup.h"
1515
#include "bolt/Core/BinaryFunction.h"
1616
#include "bolt/Core/ParallelUtilities.h"
1717
#include <cstdlib>
@@ -23,7 +23,7 @@ namespace bolt {
2323

2424
static bool PassFailed = false;
2525

26-
void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
26+
void PointerAuthCFIFixup::runOnFunction(BinaryFunction &BF) {
2727
if (PassFailed)
2828
return;
2929

@@ -35,7 +35,7 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
3535
if (BF.getState() != BinaryFunction::State::CFG &&
3636
BF.getState() != BinaryFunction::State::CFG_Finalized) {
3737
BC.outs() << "BOLT-INFO: no CFG for " << BF.getPrintName()
38-
<< " in InsertNegateRAStatePass\n";
38+
<< " in PointerAuthCFIFixup\n";
3939
return;
4040
}
4141

@@ -74,7 +74,7 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
7474
}
7575
}
7676

77-
void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
77+
void PointerAuthCFIFixup::inferUnknownStates(BinaryFunction &BF) {
7878
BinaryContext &BC = BF.getBinaryContext();
7979

8080
// Fill in missing RAStates in simple cases (inside BBs).
@@ -88,7 +88,7 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
8888
fillUnknownBlocksInCFG(BF);
8989
}
9090

91-
void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
91+
void PointerAuthCFIFixup::coverFunctionFragmentStart(BinaryFunction &BF,
9292
FunctionFragment &FF) {
9393
BinaryContext &BC = BF.getBinaryContext();
9494
if (FF.empty())
@@ -119,7 +119,7 @@ void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
119119
}
120120

121121
std::optional<bool>
122-
InsertNegateRAState::getFirstKnownRAState(BinaryContext &BC,
122+
PointerAuthCFIFixup::getFirstKnownRAState(BinaryContext &BC,
123123
BinaryBasicBlock &BB) {
124124
for (const MCInst &Inst : BB) {
125125
if (BC.MIB->isCFI(Inst))
@@ -131,7 +131,7 @@ InsertNegateRAState::getFirstKnownRAState(BinaryContext &BC,
131131
return std::nullopt;
132132
}
133133

134-
void InsertNegateRAState::fillUnknownStateInBB(BinaryContext &BC,
134+
void PointerAuthCFIFixup::fillUnknownStateInBB(BinaryContext &BC,
135135
BinaryBasicBlock &BB) {
136136

137137
auto First = BB.getFirstNonPseudo();
@@ -173,7 +173,7 @@ void InsertNegateRAState::fillUnknownStateInBB(BinaryContext &BC,
173173
}
174174
}
175175

176-
bool InsertNegateRAState::isUnknownBlock(BinaryContext &BC,
176+
bool PointerAuthCFIFixup::isUnknownBlock(BinaryContext &BC,
177177
BinaryBasicBlock &BB) {
178178
for (const MCInst &Inst : BB) {
179179
if (BC.MIB->isCFI(Inst))
@@ -185,7 +185,7 @@ bool InsertNegateRAState::isUnknownBlock(BinaryContext &BC,
185185
return true;
186186
}
187187

188-
void InsertNegateRAState::markUnknownBlock(BinaryContext &BC,
188+
void PointerAuthCFIFixup::markUnknownBlock(BinaryContext &BC,
189189
BinaryBasicBlock &BB, bool State) {
190190
// If we call this when an Instruction has either kRASigned or kRAUnsigned
191191
// annotation, setRASigned or setRAUnsigned would fail.
@@ -198,7 +198,7 @@ void InsertNegateRAState::markUnknownBlock(BinaryContext &BC,
198198
}
199199
}
200200

201-
std::optional<bool> InsertNegateRAState::getRAStateByCFG(BinaryBasicBlock &BB,
201+
std::optional<bool> PointerAuthCFIFixup::getRAStateByCFG(BinaryBasicBlock &BB,
202202
BinaryFunction &BF) {
203203
BinaryContext &BC = BF.getBinaryContext();
204204

@@ -244,7 +244,7 @@ std::optional<bool> InsertNegateRAState::getRAStateByCFG(BinaryBasicBlock &BB,
244244
return NeighborRAState;
245245
}
246246

247-
void InsertNegateRAState::fillUnknownStubs(BinaryFunction &BF) {
247+
void PointerAuthCFIFixup::fillUnknownStubs(BinaryFunction &BF) {
248248
BinaryContext &BC = BF.getBinaryContext();
249249
bool FirstIter = true;
250250
MCInst PrevInst;
@@ -281,7 +281,7 @@ void InsertNegateRAState::fillUnknownStubs(BinaryFunction &BF) {
281281
}
282282
}
283283
}
284-
void InsertNegateRAState::fillUnknownBlocksInCFG(BinaryFunction &BF) {
284+
void PointerAuthCFIFixup::fillUnknownBlocksInCFG(BinaryFunction &BF) {
285285
BinaryContext &BC = BF.getBinaryContext();
286286

287287
auto fillUnknowns = [&](BinaryFunction &BF) -> std::pair<int, bool> {
@@ -317,7 +317,7 @@ void InsertNegateRAState::fillUnknownBlocksInCFG(BinaryFunction &BF) {
317317
}
318318
}
319319

320-
Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
320+
Error PointerAuthCFIFixup::runOnFunctions(BinaryContext &BC) {
321321
std::atomic<uint64_t> FunctionsModified{0};
322322
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
323323
FunctionsModified++;
@@ -334,7 +334,7 @@ Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
334334

335335
ParallelUtilities::runOnEachFunction(
336336
BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun,
337-
SkipPredicate, "InsertNegateRAStatePass");
337+
SkipPredicate, "PointerAuthCFIFixup");
338338

339339
BC.outs() << "BOLT-INFO: rewritten pac-ret DWARF info in "
340340
<< FunctionsModified << " out of " << BC.getBinaryFunctions().size()

0 commit comments

Comments
 (0)