Skip to content

Commit 3b73dda

Browse files
committed
[BOLT] Print stats in MarkRAStates, InsertNegateRAState
- print statistics on the output about function getting Ignored in MarkRAStates, and on rewriting OpNegateRAState in InsertNegateRAStatePass. - improve error messages in MarkRAStates
1 parent 910c091 commit 3b73dda

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

bolt/include/bolt/Passes/InsertNegateRAStatePass.h

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

3030
private:
31+
uint64_t FunctionsModified{0};
3132
/// Because states are tracked as MCAnnotations on individual instructions,
3233
/// newly inserted instructions do not have a state associated with them.
3334
/// New states are "inherited" from the last known state.

bolt/include/bolt/Passes/MarkRAStates.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class MarkRAStates : public BinaryFunctionPass {
2525

2626
/// Pass entry point
2727
Error runOnFunctions(BinaryContext &BC) override;
28-
void runOnFunction(BinaryFunction &BF);
28+
bool runOnFunction(BinaryFunction &BF);
29+
30+
private:
31+
uint64_t FunctionsIgnored{0};
2932
};
3033

3134
} // namespace bolt

bolt/lib/Passes/InsertNegateRAStatePass.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
115115

116116
Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
117117
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
118-
if (BF.containedNegateRAState()) {
118+
if (BF.containedNegateRAState() && !BF.isIgnored()) {
119119
// We can skip functions which did not include negate-ra-state CFIs. This
120120
// includes code using pac-ret hardening as well, if the binary is
121121
// compiled with `-fno-exceptions -fno-unwind-tables
122122
// -fno-asynchronous-unwind-tables`
123+
FunctionsModified++;
123124
runOnFunction(BF);
124125
}
125126
};
@@ -128,6 +129,11 @@ Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
128129
BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun, nullptr,
129130
"InsertNegateRAStatePass");
130131

132+
BC.outs() << "BOLT-INFO: rewritten pac-ret DWARF info in "
133+
<< FunctionsModified << " out of " << BC.getBinaryFunctions().size()
134+
<< " functions "
135+
<< format("(%.2lf%%).\n", (100.0 * FunctionsModified) /
136+
BC.getBinaryFunctions().size());
131137
return Error::success();
132138
}
133139

bolt/lib/Passes/MarkRAStates.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ using namespace llvm;
3131
namespace llvm {
3232
namespace bolt {
3333

34-
void MarkRAStates::runOnFunction(BinaryFunction &BF) {
35-
36-
if (BF.isIgnored())
37-
return;
34+
bool MarkRAStates::runOnFunction(BinaryFunction &BF) {
3835

3936
BinaryContext &BC = BF.getBinaryContext();
4037

@@ -49,10 +46,9 @@ void MarkRAStates::runOnFunction(BinaryFunction &BF) {
4946
// annotation.
5047
BF.setIgnored();
5148
BC.outs() << "BOLT-INFO: inconsistent RAStates in function "
52-
<< BF.getPrintName() << "\n";
53-
BC.outs()
54-
<< "BOLT-INFO: ptr sign/auth inst without .cfi_negate_ra_state\n";
55-
return;
49+
<< BF.getPrintName()
50+
<< ": ptr sign/auth inst without .cfi_negate_ra_state\n";
51+
return false;
5652
}
5753
}
5854
}
@@ -72,11 +68,10 @@ void MarkRAStates::runOnFunction(BinaryFunction &BF) {
7268
if (RAState) {
7369
// RA signing instructions should only follow unsigned RA state.
7470
BC.outs() << "BOLT-INFO: inconsistent RAStates in function "
75-
<< BF.getPrintName() << "\n";
76-
BC.outs() << "BOLT-INFO: ptr signing inst encountered in Signed RA "
77-
"state.\n";
71+
<< BF.getPrintName()
72+
<< ": ptr signing inst encountered in Signed RA state\n";
7873
BF.setIgnored();
79-
return;
74+
return false;
8075
}
8176
// The signing instruction itself is unsinged, but the next will be
8277
// signed.
@@ -85,11 +80,11 @@ void MarkRAStates::runOnFunction(BinaryFunction &BF) {
8580
if (!RAState) {
8681
// RA authenticating instructions should only follow signed RA state.
8782
BC.outs() << "BOLT-INFO: inconsistent RAStates in function "
88-
<< BF.getPrintName() << "\n";
89-
BC.outs() << "BOLT-INFO: ptr authenticating inst encountered in "
90-
"Unsigned RA state.\n";
83+
<< BF.getPrintName()
84+
<< ": ptr authenticating inst encountered in Unsigned RA "
85+
"state\n";
9186
BF.setIgnored();
92-
return;
87+
return false;
9388
}
9489
// The authenticating instruction itself is signed, but the next will be
9590
// unsigned.
@@ -120,23 +115,36 @@ void MarkRAStates::runOnFunction(BinaryFunction &BF) {
120115
}
121116
}
122117
}
118+
return true;
123119
}
124120

125121
Error MarkRAStates::runOnFunctions(BinaryContext &BC) {
126122
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
127-
if (BF.containedNegateRAState()) {
123+
if (BF.containedNegateRAState() && !BF.isIgnored()) {
128124
// We can skip functions which did not include negate-ra-state CFIs. This
129125
// includes code using pac-ret hardening as well, if the binary is
130126
// compiled with `-fno-exceptions -fno-unwind-tables
131127
// -fno-asynchronous-unwind-tables`
132-
runOnFunction(BF);
128+
if (!runOnFunction(BF)) {
129+
FunctionsIgnored++;
130+
}
133131
}
134132
};
135133

136134
ParallelUtilities::runOnEachFunction(
137135
BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun, nullptr,
138136
"MarkRAStates");
139137

138+
int Total = llvm::count_if(
139+
BC.getBinaryFunctions(),
140+
[&](std::pair<const unsigned long, BinaryFunction> &P) {
141+
return P.second.containedNegateRAState() && !P.second.isIgnored();
142+
});
143+
BC.outs() << "BOLT-INFO: MarkRAStates ran on " << Total
144+
<< " functions. Ignored " << FunctionsIgnored << " functions "
145+
<< format("(%.2lf%%)", (100.0 * FunctionsIgnored) / Total)
146+
<< " because of CFI inconsistencies.\n";
147+
140148
return Error::success();
141149
}
142150

bolt/test/AArch64/negate-ra-state-incorrect.s

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
1111
# RUN: llvm-bolt %t.exe -o %t.exe.bolt --no-threads | FileCheck %s --check-prefix=CHECK-BOLT
1212

13-
# CHECK-BOLT: BOLT-INFO: inconsistent RAStates in function foo
14-
# CHECK-BOLT-NEXT: BOLT-INFO: ptr authenticating inst encountered in Unsigned RA state.
15-
16-
# CHECK-BOLT: BOLT-INFO: inconsistent RAStates in function bar
17-
# CHECK-BOLT-NEXT: BOLT-INFO: ptr signing inst encountered in Signed RA state
18-
19-
# CHECK-BOLT: BOLT-INFO: inconsistent RAStates in function baz
20-
# CHECK-BOLT-NEXT: BOLT-INFO: ptr sign/auth inst without .cfi_negate_ra_state
13+
# CHECK-BOLT: BOLT-INFO: inconsistent RAStates in function foo: ptr authenticating inst encountered in Unsigned RA state
14+
# CHECK-BOLT: BOLT-INFO: inconsistent RAStates in function bar: ptr signing inst encountered in Signed RA state
15+
# CHECK-BOLT: BOLT-INFO: inconsistent RAStates in function baz: ptr sign/auth inst without .cfi_negate_ra_state
2116

2217
# Check that the incorrect functions got ignored, so they are not in the new .text section
2318
# RUN: llvm-objdump %t.exe.bolt -d -j .text | FileCheck %s --check-prefix=CHECK-OBJDUMP

0 commit comments

Comments
 (0)