Skip to content

Commit 8b5b732

Browse files
committed
[BOLT][NFC] Simplify RAState tracking
- Remove 'Authenticating' and 'Signing' MCAnnotations. - The same logic can be done using only 'Signed' and 'Unsigned' annotations. - To check if an instruction is signing or authenticating, we can use the PSignOnLR, and PAuthOnLR functions.
1 parent ad2f029 commit 8b5b732

File tree

6 files changed

+10
-70
lines changed

6 files changed

+10
-70
lines changed

bolt/include/bolt/Core/MCPlus.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ class MCAnnotation {
7272
kLabel, /// MCSymbol pointing to this instruction.
7373
kSize, /// Size of the instruction.
7474
kDynamicBranch, /// Jit instruction patched at runtime.
75-
kSigning, /// Inst is a signing instruction (paciasp, etc.).
7675
kSigned, /// Inst is in a range where RA is signed.
77-
kAuthenticating, /// Authenticating inst (e.g. autiasp).
7876
kUnsigned, /// Inst is in a range where RA is unsigned.
7977
kRememberState, /// Inst has rememberState CFI.
8078
kRestoreState, /// Inst has restoreState CFI.

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,18 +1361,6 @@ class MCPlusBuilder {
13611361
/// Return true if \p Inst has Signed RA annotation.
13621362
bool isRASigned(const MCInst &Inst) const;
13631363

1364-
/// Stores RA Signing annotation on \p Inst.
1365-
void setRASigning(MCInst &Inst) const;
1366-
1367-
/// Return true if \p Inst has Signing RA annotation.
1368-
bool isRASigning(const MCInst &Inst) const;
1369-
1370-
/// Stores Authenticating annotation on \p Inst.
1371-
void setAuthenticating(MCInst &Inst) const;
1372-
1373-
/// Return true if \p Inst has Authenticating annotation.
1374-
bool isAuthenticating(const MCInst &Inst) const;
1375-
13761364
/// Stores RA Unsigned annotation on \p Inst.
13771365
void setRAUnsigned(MCInst &Inst) const;
13781366

bolt/include/bolt/Passes/InsertNegateRAStatePass.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ class InsertNegateRAState : public BinaryFunctionPass {
2828
void runOnFunction(BinaryFunction &BF);
2929

3030
private:
31-
/// Loops over all instructions and adds OpNegateRAState CFI
32-
/// after any pointer signing or authenticating instructions,
33-
/// which operate on the LR, except fused pauth + ret instructions
34-
/// (such as RETAA). Normal pauth and psign instructions are "special cases",
35-
/// meaning they always need an OpNegateRAState CFI after them.
36-
/// Fused pauth + ret instructions are not, they work as any other
37-
/// instruction.
38-
/// Returns true, if any OpNegateRAState CFIs were added.
39-
bool addNegateRAStateAfterPSignOrPAuth(BinaryFunction &BF);
4031
/// Because states are tracked as MCAnnotations on individual instructions,
4132
/// newly inserted instructions do not have a state associated with them.
4233
/// New states are "inherited" from the last known state.

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,6 @@ bool MCPlusBuilder::isRASigned(const MCInst &Inst) const {
195195
return hasAnnotation(Inst, MCAnnotation::kSigned);
196196
}
197197

198-
void MCPlusBuilder::setRASigning(MCInst &Inst) const {
199-
assert(!hasAnnotation(Inst, MCAnnotation::kSigning));
200-
setAnnotationOpValue(Inst, MCAnnotation::kSigning, true);
201-
}
202-
203-
bool MCPlusBuilder::isRASigning(const MCInst &Inst) const {
204-
return hasAnnotation(Inst, MCAnnotation::kSigning);
205-
}
206-
207-
void MCPlusBuilder::setAuthenticating(MCInst &Inst) const {
208-
assert(!hasAnnotation(Inst, MCAnnotation::kAuthenticating));
209-
setAnnotationOpValue(Inst, MCAnnotation::kAuthenticating, true);
210-
}
211-
212-
bool MCPlusBuilder::isAuthenticating(const MCInst &Inst) const {
213-
return hasAnnotation(Inst, MCAnnotation::kAuthenticating);
214-
}
215-
216198
void MCPlusBuilder::setRAUnsigned(MCInst &Inst) const {
217199
assert(!hasAnnotation(Inst, MCAnnotation::kUnsigned));
218200
setAnnotationOpValue(Inst, MCAnnotation::kUnsigned, true);
@@ -223,8 +205,7 @@ bool MCPlusBuilder::isRAUnsigned(const MCInst &Inst) const {
223205
}
224206

225207
bool MCPlusBuilder::isRAStateUnknown(const MCInst &Inst) const {
226-
return !(isRAUnsigned(Inst) || isRASigned(Inst) || isRASigning(Inst) ||
227-
isAuthenticating(Inst));
208+
return !(isRAUnsigned(Inst) || isRASigned(Inst));
228209
}
229210

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

bolt/lib/Passes/InsertNegateRAStatePass.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
3434
return;
3535
}
3636

37-
// Attach .cfi_negate_ra_state to the "trivial" cases first.
38-
addNegateRAStateAfterPSignOrPAuth(BF);
39-
4037
inferUnknownStates(BF);
4138

4239
for (FunctionFragment &FF : BF.getLayout().fragments()) {
@@ -67,24 +64,6 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
6764
}
6865
}
6966

70-
bool InsertNegateRAState::addNegateRAStateAfterPSignOrPAuth(
71-
BinaryFunction &BF) {
72-
BinaryContext &BC = BF.getBinaryContext();
73-
bool FoundAny = false;
74-
for (BinaryBasicBlock &BB : BF) {
75-
for (auto Iter = BB.begin(); Iter != BB.end(); ++Iter) {
76-
MCInst &Inst = *Iter;
77-
if (BC.MIB->isPSignOnLR(Inst) ||
78-
(BC.MIB->isPAuthOnLR(Inst) && !BC.MIB->isPAuthAndRet(Inst))) {
79-
Iter = BF.addCFIInstruction(
80-
&BB, Iter + 1, MCCFIInstruction::createNegateRAState(nullptr));
81-
FoundAny = true;
82-
}
83-
}
84-
}
85-
return FoundAny;
86-
}
87-
8867
void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
8968
FunctionFragment &FF) {
9069
BinaryContext &BC = BF.getBinaryContext();
@@ -102,8 +81,7 @@ void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
10281
});
10382
// If a function is already split in the input, the first FF can also start
10483
// with Signed state. This covers that scenario as well.
105-
if (BC.MIB->isRASigned(*((*FirstNonEmpty)->begin())) ||
106-
BC.MIB->isAuthenticating(*((*FirstNonEmpty)->begin()))) {
84+
if (BC.MIB->isRASigned(*((*FirstNonEmpty)->begin()))) {
10785
BF.addCFIInstruction(*FirstNonEmpty, (*FirstNonEmpty)->begin(),
10886
MCCFIInstruction::createNegateRAState(nullptr));
10987
}
@@ -121,10 +99,10 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
12199
continue;
122100

123101
if (!FirstIter && BC.MIB->isRAStateUnknown(Inst)) {
124-
if (BC.MIB->isRASigned(PrevInst) || BC.MIB->isRASigning(PrevInst)) {
102+
if (BC.MIB->isRASigned(PrevInst) || BC.MIB->isPSignOnLR(PrevInst)) {
125103
BC.MIB->setRASigned(Inst);
126104
} else if (BC.MIB->isRAUnsigned(PrevInst) ||
127-
BC.MIB->isAuthenticating(PrevInst)) {
105+
BC.MIB->isPAuthOnLR(PrevInst)) {
128106
BC.MIB->setRAUnsigned(Inst);
129107
}
130108
} else {

bolt/lib/Passes/MarkRAStates.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ void MarkRAStates::runOnFunction(BinaryFunction &BF) {
7878
BF.setIgnored();
7979
return;
8080
}
81-
BC.MIB->setRASigning(Inst);
81+
// The signing instruction itself is unsinged, but the next will be
82+
// signed.
83+
BC.MIB->setRAUnsigned(Inst);
8284
} else if (BC.MIB->isPAuthOnLR(Inst)) {
8385
if (!RAState) {
8486
// RA authenticating instructions should only follow signed RA state.
@@ -89,7 +91,9 @@ void MarkRAStates::runOnFunction(BinaryFunction &BF) {
8991
BF.setIgnored();
9092
return;
9193
}
92-
BC.MIB->setAuthenticating(Inst);
94+
// The authenticating instruction itself is signed, but the next will be
95+
// unsigned.
96+
BC.MIB->setRASigned(Inst);
9397
} else if (RAState) {
9498
BC.MIB->setRASigned(Inst);
9599
} else {

0 commit comments

Comments
 (0)