Skip to content

Commit 43eecd1

Browse files
committed
[BOLT] Add assert wehn calling insertBTI on empty BBs
BOLT may generate empty BBs, e.g. around function splitting, to hold temporary labels. If they are the target of a new indirect branch, the BTI should be inserted into the first "real" BasicBlock.
1 parent a12f44e commit 43eecd1

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,6 +2838,10 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
28382838

28392839
void insertBTI(BinaryBasicBlock &BB, MCInst &Call) const override {
28402840
auto II = BB.getFirstNonPseudo();
2841+
assert(II != BB.end() &&
2842+
"insertBTI should only be called on non-empty BasicBlocks");
2843+
// Make sure there is no crash in non-assertion builds when calling on empty
2844+
// BBs.
28412845
if (II != BB.end()) {
28422846
if (isBTIVariantCoveringCall(Call, *II))
28432847
return;

bolt/unittests/Core/MCPlusBuilder.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ TEST_P(MCPlusBuilderTester, AArch64_BTI) {
198198
ASSERT_TRUE(BC->MIB->isImplicitBTIC(*II));
199199
}
200200

201+
TEST_P(MCPlusBuilderTester, AArch64_insertBTI_empty) {
202+
if (GetParam() != Triple::aarch64)
203+
GTEST_SKIP();
204+
BinaryFunction *BF = BC->createInjectedBinaryFunction("BF", true);
205+
std::unique_ptr<BinaryBasicBlock> BB = BF->createBasicBlock();
206+
MCInst CallInst = MCInstBuilder(AArch64::BR).addReg(AArch64::X16);
207+
#ifndef NDEBUG
208+
ASSERT_DEATH(BC->MIB->insertBTI(*BB, CallInst),
209+
"insertBTI should only be called on non-empty BasicBlocks");
210+
#else
211+
BC->MIB->insertBTI(*BB, CallInst);
212+
ASSERT(BB->size() == 0);
213+
#endif
214+
}
201215
TEST_P(MCPlusBuilderTester, AArch64_insertBTI_0) {
202216
if (GetParam() != Triple::aarch64)
203217
GTEST_SKIP();

0 commit comments

Comments
 (0)