Skip to content

Commit aa45ad9

Browse files
committed
Review fixes
1 parent 28fa1eb commit aa45ad9

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

bolt/lib/Passes/InsertNegateRAStatePass.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ InsertNegateRAState::getFirstKnownRAState(BinaryContext &BC,
131131
return std::nullopt;
132132
}
133133

134+
bool InsertNegateRAState::isUnknownBlock(BinaryContext &BC,
135+
BinaryBasicBlock &BB) {
136+
std::optional<bool> FirstRAState = getFirstKnownRAState(BC, BB);
137+
return !FirstRAState.has_value();
138+
}
139+
134140
void InsertNegateRAState::fillUnknownStateInBB(BinaryContext &BC,
135141
BinaryBasicBlock &BB) {
136142

@@ -174,18 +180,6 @@ void InsertNegateRAState::fillUnknownStateInBB(BinaryContext &BC,
174180
}
175181
}
176182

177-
bool InsertNegateRAState::isUnknownBlock(BinaryContext &BC,
178-
BinaryBasicBlock &BB) {
179-
for (const MCInst &Inst : BB) {
180-
if (BC.MIB->isCFI(Inst))
181-
continue;
182-
std::optional<bool> RAState = BC.MIB->getRAState(Inst);
183-
if (RAState.has_value())
184-
return false;
185-
}
186-
return true;
187-
}
188-
189183
void InsertNegateRAState::markUnknownBlock(BinaryContext &BC,
190184
BinaryBasicBlock &BB, bool State) {
191185
// If we call this when an Instruction has either kRASigned or kRAUnsigned
@@ -205,7 +199,13 @@ void InsertNegateRAState::fillUnknownStubs(BinaryFunction &BF) {
205199
MCInst PrevInst;
206200
for (FunctionFragment &FF : BF.getLayout().fragments()) {
207201
for (BinaryBasicBlock *BB : FF) {
208-
if (!FirstIter && isUnknownBlock(BC, *BB)) {
202+
if (FirstIter) {
203+
FirstIter = false;
204+
if (isUnknownBlock(BC, *BB))
205+
// If the first BasicBlock is unknown, the function's entry RAState
206+
// should be used.
207+
markUnknownBlock(BC, *BB, BF.getInitialRAState());
208+
} else if (isUnknownBlock(BC, *BB)) {
209209
// As explained in issue #160989, the unwind info is incorrect for
210210
// stubs. Indicating the correct RAState without the rest of the unwind
211211
// info being correct is not useful. Instead, we copy the RAState from
@@ -224,13 +224,6 @@ void InsertNegateRAState::fillUnknownStubs(BinaryFunction &BF) {
224224
PrevRAState = false;
225225
markUnknownBlock(BC, *BB, *PrevRAState);
226226
}
227-
if (FirstIter) {
228-
FirstIter = false;
229-
if (isUnknownBlock(BC, *BB))
230-
// If the first BasicBlock is unknown, the function's entry RAState
231-
// should be used.
232-
markUnknownBlock(BC, *BB, BF.getInitialRAState());
233-
}
234227
// This function iterates on BasicBlocks, so the PrevInst has to be
235228
// updated to the last instruction of the current BasicBlock. If the
236229
// BasicBlock is empty, or only has PseudoInstructions, PrevInst will not

bolt/unittests/Passes/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ set(LLVM_LINK_COMPONENTS
22
DebugInfoDWARF
33
Object
44
MC
5-
${LLVM_TARGETS_TO_BUILD}
5+
${BOLT_TARGETS_TO_BUILD}
66
)
77

88
add_bolt_unittest(PassTests

bolt/unittests/Passes/InsertNegateRAState.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,19 @@ TEST_P(PassTester, fillUnknownStateInBBTest) {
216216
Error E = PassManager->runPasses();
217217
EXPECT_FALSE(E);
218218

219+
BF->dump();
220+
219221
auto CFILoc = findCFIOffsets(*BF);
220222
EXPECT_EQ(CFILoc.size(), 1u);
221223
EXPECT_EQ(CFILoc[0], 4);
224+
// Check that the pass set Unknown and Unknown1 to signed.
225+
// begin() is the CFI, begin() + 1 is Unknown, begin() + 2 is Unknown1.
226+
std::optional<bool> RAState = BC->MIB->getRAState(*(BB2->begin() + 1));
227+
EXPECT_TRUE(RAState.has_value());
228+
EXPECT_TRUE(*RAState);
229+
std::optional<bool> RAState1 = BC->MIB->getRAState(*(BB2->begin() + 2));
230+
EXPECT_TRUE(RAState1.has_value());
231+
EXPECT_TRUE(*RAState1);
222232
}
223233

224234
TEST_P(PassTester, fillUnknownStubs) {
@@ -314,9 +324,9 @@ TEST_P(PassTester, fillUnknownStubsEmpty) {
314324

315325
// Check that BOLT added an RAState to BB2.
316326
std::optional<bool> RAState = BC->MIB->getRAState(*(BB2->begin()));
317-
ASSERT_TRUE(RAState.has_value());
327+
EXPECT_TRUE(RAState.has_value());
318328
// BB2 should be set to BF.initialRAState (false).
319-
ASSERT_FALSE(*RAState);
329+
EXPECT_FALSE(*RAState);
320330
}
321331

322332
#ifdef AARCH64_AVAILABLE

0 commit comments

Comments
 (0)