Skip to content

Commit fdb13cf

Browse files
authored
[BOLT] Fix local out-of-range stub issue in LongJmp (#73918)
If a local stub is out-of-range, at LongJmp we will try to find another local stub first. However, The original implementation do not work as expected and it leads to an infinite loop between replaceTargetWithStub and fixBranches. After this patch, we first convert the target of BB back to the target of the local stub, and then look up for other valid local stubs and so on.
1 parent af999c4 commit fdb13cf

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

bolt/lib/Passes/LongJmp.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,23 @@ LongJmpPass::replaceTargetWithStub(BinaryBasicBlock &BB, MCInst &Inst,
202202
}
203203
} else if (LocalStubsIter != Stubs.end() &&
204204
LocalStubsIter->second.count(TgtBB)) {
205-
// If we are replacing a local stub (because it is now out of range),
206-
// use its target instead of creating a stub to jump to another stub
205+
// The TgtBB and TgtSym now are the local out-of-range stub and its label.
206+
// So, we are attempting to restore BB to its previous state without using
207+
// this stub.
207208
TgtSym = BC.MIB->getTargetSymbol(*TgtBB->begin());
208-
TgtBB = BB.getSuccessor(TgtSym, BI);
209+
assert(TgtSym &&
210+
"First instruction is expected to contain a target symbol.");
211+
BinaryBasicBlock *TgtBBSucc = TgtBB->getSuccessor(TgtSym, BI);
212+
213+
// TgtBB might have no successor. e.g. a stub for a function call.
214+
if (TgtBBSucc) {
215+
BB.replaceSuccessor(TgtBB, TgtBBSucc, BI.Count, BI.MispredictedCount);
216+
assert(TgtBB->getExecutionCount() >= BI.Count &&
217+
"At least equal or greater than the branch count.");
218+
TgtBB->setExecutionCount(TgtBB->getExecutionCount() - BI.Count);
219+
}
220+
221+
TgtBB = TgtBBSucc;
209222
}
210223

211224
BinaryBasicBlock *StubBB = lookupLocalStub(BB, Inst, TgtSym, DotAddress);

0 commit comments

Comments
 (0)