Skip to content

Commit cd68056

Browse files
authored
[BOLT] Simplify RAState helpers (NFCI) (llvm#162820)
- unify isRAStateSigned and isRAStateUnsigned to a common getRAState, - unify setRASigned and setRAUnsigned into setRAState(MCInst, bool), - update users of these to match the new implementations.
1 parent 2d381bf commit cd68056

File tree

4 files changed

+58
-53
lines changed

4 files changed

+58
-53
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,20 +1371,13 @@ class MCPlusBuilder {
13711371
/// Return true if \p Inst has RestoreState annotation.
13721372
bool hasRestoreState(const MCInst &Inst) const;
13731373

1374-
/// Stores RA Signed annotation on \p Inst.
1375-
void setRASigned(MCInst &Inst) const;
1374+
/// Sets kRASigned or kRAUnsigned annotation on \p Inst.
1375+
/// Fails if \p Inst has either annotation already set.
1376+
void setRAState(MCInst &Inst, bool State) const;
13761377

1377-
/// Return true if \p Inst has Signed RA annotation.
1378-
bool isRASigned(const MCInst &Inst) const;
1379-
1380-
/// Stores RA Unsigned annotation on \p Inst.
1381-
void setRAUnsigned(MCInst &Inst) const;
1382-
1383-
/// Return true if \p Inst has Unsigned RA annotation.
1384-
bool isRAUnsigned(const MCInst &Inst) const;
1385-
1386-
/// Return true if \p Inst doesn't have any annotation related to RA state.
1387-
bool isRAStateUnknown(const MCInst &Inst) const;
1378+
/// Return true if \p Inst has kRASigned annotation, false if it has
1379+
/// kRAUnsigned annotation, and std::nullopt if neither annotation is set.
1380+
std::optional<bool> getRAState(const MCInst &Inst) const;
13881381

13891382
/// Return true if the instruction is a call with an exception handling info.
13901383
virtual bool isInvoke(const MCInst &Inst) const {

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,21 @@ bool MCPlusBuilder::hasRestoreState(const MCInst &Inst) const {
186186
return hasAnnotation(Inst, MCAnnotation::kRestoreState);
187187
}
188188

189-
void MCPlusBuilder::setRASigned(MCInst &Inst) const {
189+
void MCPlusBuilder::setRAState(MCInst &Inst, bool State) const {
190190
assert(!hasAnnotation(Inst, MCAnnotation::kRASigned));
191-
setAnnotationOpValue(Inst, MCAnnotation::kRASigned, true);
192-
}
193-
194-
bool MCPlusBuilder::isRASigned(const MCInst &Inst) const {
195-
return hasAnnotation(Inst, MCAnnotation::kRASigned);
196-
}
197-
198-
void MCPlusBuilder::setRAUnsigned(MCInst &Inst) const {
199191
assert(!hasAnnotation(Inst, MCAnnotation::kRAUnsigned));
200-
setAnnotationOpValue(Inst, MCAnnotation::kRAUnsigned, true);
192+
if (State)
193+
setAnnotationOpValue(Inst, MCAnnotation::kRASigned, true);
194+
else
195+
setAnnotationOpValue(Inst, MCAnnotation::kRAUnsigned, true);
201196
}
202197

203-
bool MCPlusBuilder::isRAUnsigned(const MCInst &Inst) const {
204-
return hasAnnotation(Inst, MCAnnotation::kRAUnsigned);
205-
}
206-
207-
bool MCPlusBuilder::isRAStateUnknown(const MCInst &Inst) const {
208-
return !(isRAUnsigned(Inst) || isRASigned(Inst));
198+
std::optional<bool> MCPlusBuilder::getRAState(const MCInst &Inst) const {
199+
if (hasAnnotation(Inst, MCAnnotation::kRASigned))
200+
return true;
201+
if (hasAnnotation(Inst, MCAnnotation::kRAUnsigned))
202+
return false;
203+
return std::nullopt;
209204
}
210205

211206
std::optional<MCLandingPad> MCPlusBuilder::getEHInfo(const MCInst &Inst) const {

bolt/lib/Passes/InsertNegateRAStatePass.cpp

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ using namespace llvm;
2121
namespace llvm {
2222
namespace bolt {
2323

24+
static bool PassFailed = false;
25+
2426
void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
27+
if (PassFailed)
28+
return;
29+
2530
BinaryContext &BC = BF.getBinaryContext();
2631

2732
if (BF.getState() == BinaryFunction::State::Empty)
@@ -39,26 +44,31 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
3944
for (FunctionFragment &FF : BF.getLayout().fragments()) {
4045
coverFunctionFragmentStart(BF, FF);
4146
bool FirstIter = true;
42-
MCInst PrevInst;
47+
bool PrevRAState = false;
4348
// As this pass runs after function splitting, we should only check
4449
// consecutive instructions inside FunctionFragments.
4550
for (BinaryBasicBlock *BB : FF) {
4651
for (auto It = BB->begin(); It != BB->end(); ++It) {
4752
MCInst &Inst = *It;
4853
if (BC.MIB->isCFI(Inst))
4954
continue;
55+
auto RAState = BC.MIB->getRAState(Inst);
56+
if (!RAState) {
57+
BC.errs() << "BOLT-ERROR: unknown RAState after inferUnknownStates "
58+
<< " in function " << BF.getPrintName() << "\n";
59+
PassFailed = true;
60+
return;
61+
}
5062
if (!FirstIter) {
5163
// Consecutive instructions with different RAState means we need to
5264
// add a OpNegateRAState.
53-
if ((BC.MIB->isRASigned(PrevInst) && BC.MIB->isRAUnsigned(Inst)) ||
54-
(BC.MIB->isRAUnsigned(PrevInst) && BC.MIB->isRASigned(Inst))) {
65+
if (*RAState != PrevRAState)
5566
It = BF.addCFIInstruction(
5667
BB, It, MCCFIInstruction::createNegateRAState(nullptr));
57-
}
5868
} else {
5969
FirstIter = false;
6070
}
61-
PrevInst = *It;
71+
PrevRAState = *RAState;
6272
}
6373
}
6474
}
@@ -81,10 +91,17 @@ void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
8191
});
8292
// If a function is already split in the input, the first FF can also start
8393
// with Signed state. This covers that scenario as well.
84-
if (BC.MIB->isRASigned(*((*FirstNonEmpty)->begin()))) {
85-
BF.addCFIInstruction(*FirstNonEmpty, (*FirstNonEmpty)->begin(),
86-
MCCFIInstruction::createNegateRAState(nullptr));
94+
auto II = (*FirstNonEmpty)->getFirstNonPseudo();
95+
auto RAState = BC.MIB->getRAState(*II);
96+
if (!RAState) {
97+
BC.errs() << "BOLT-ERROR: unknown RAState after inferUnknownStates "
98+
<< " in function " << BF.getPrintName() << "\n";
99+
PassFailed = true;
100+
return;
87101
}
102+
if (*RAState)
103+
BF.addCFIInstruction(*FirstNonEmpty, II,
104+
MCCFIInstruction::createNegateRAState(nullptr));
88105
}
89106

90107
void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
@@ -96,15 +113,21 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
96113
if (BC.MIB->isCFI(Inst))
97114
continue;
98115

99-
if (!FirstIter && BC.MIB->isRAStateUnknown(Inst)) {
100-
if (BC.MIB->isRASigned(PrevInst) || BC.MIB->isPSignOnLR(PrevInst)) {
101-
BC.MIB->setRASigned(Inst);
102-
} else if (BC.MIB->isRAUnsigned(PrevInst) ||
103-
BC.MIB->isPAuthOnLR(PrevInst)) {
104-
BC.MIB->setRAUnsigned(Inst);
116+
auto RAState = BC.MIB->getRAState(Inst);
117+
if (!FirstIter && !RAState) {
118+
if (BC.MIB->isPSignOnLR(PrevInst))
119+
RAState = true;
120+
else if (BC.MIB->isPAuthOnLR(PrevInst))
121+
RAState = false;
122+
else {
123+
auto PrevRAState = BC.MIB->getRAState(PrevInst);
124+
RAState = PrevRAState ? *PrevRAState : false;
105125
}
126+
BC.MIB->setRAState(Inst, *RAState);
106127
} else {
107128
FirstIter = false;
129+
if (!RAState)
130+
BC.MIB->setRAState(Inst, BF.getInitialRAState());
108131
}
109132
PrevInst = Inst;
110133
}
@@ -135,6 +158,8 @@ Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
135158
<< " functions "
136159
<< format("(%.2lf%%).\n", (100.0 * FunctionsModified) /
137160
BC.getBinaryFunctions().size());
161+
if (PassFailed)
162+
return createFatalBOLTError("");
138163
return Error::success();
139164
}
140165

bolt/lib/Passes/MarkRAStates.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ bool MarkRAStates::runOnFunction(BinaryFunction &BF) {
7272
BF.setIgnored();
7373
return false;
7474
}
75-
// The signing instruction itself is unsigned, the next will be
76-
// signed.
77-
BC.MIB->setRAUnsigned(Inst);
7875
} else if (BC.MIB->isPAuthOnLR(Inst)) {
7976
if (!RAState) {
8077
// RA authenticating instructions should only follow signed RA state.
@@ -86,15 +83,10 @@ bool MarkRAStates::runOnFunction(BinaryFunction &BF) {
8683
BF.setIgnored();
8784
return false;
8885
}
89-
// The authenticating instruction itself is signed, but the next will be
90-
// unsigned.
91-
BC.MIB->setRASigned(Inst);
92-
} else if (RAState) {
93-
BC.MIB->setRASigned(Inst);
94-
} else {
95-
BC.MIB->setRAUnsigned(Inst);
9686
}
9787

88+
BC.MIB->setRAState(Inst, RAState);
89+
9890
// Updating RAState. All updates are valid from the next instruction.
9991
// Because the same instruction can have remember and restore, the order
10092
// here is relevant. This is the reason to loop over Annotations instead

0 commit comments

Comments
 (0)