Skip to content

Commit f65e8c3

Browse files
committed
[bolt] Fix std::prev()-past-begin in veneer handling code
matchLinkerVeneer() returns 3 if `Instruction` and the last two instructions in `[Instructions.begin, Instructions.end())` match the pattern ADRP x16, imm ADD x16, x16, imm BR x16 BinaryContext.cpp used to use --Count; for (auto It = std::prev(Instructions.end()); Count != 0; It = std::prev(It), --Count) { ...use It... } to walk these instructions. The first `--Count` skips the instruction that's in `Instruction` instead of in `Instructions`. The loop then walks over `Instructions`. However, on the last iteration, this calls `std::prev()` on an iterator that points at the container's begin(), which can blow up. Instead, use rbegin(), which sidesteps this issue. Fixes test/AArch64/veneer-gold.s on a macOS host. With this, check-bolt passes on macOS. Differential Revision: https://reviews.llvm.org/D138313
1 parent aaea4e3 commit f65e8c3

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

bolt/lib/Core/BinaryContext.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,7 @@ bool BinaryContext::handleAArch64Veneer(uint64_t Address, bool MatchOnly) {
11911191
MIB->addAnnotation(Instruction, "AArch64Veneer", true);
11921192
Veneer->addInstruction(Offset, std::move(Instruction));
11931193
--Count;
1194-
for (auto It = std::prev(Instructions.end()); Count != 0;
1195-
It = std::prev(It), --Count) {
1194+
for (auto It = Instructions.rbegin(); Count != 0; ++It, --Count) {
11961195
MIB->addAnnotation(It->second, "AArch64Veneer", true);
11971196
Veneer->addInstruction(It->first, std::move(It->second));
11981197
}

0 commit comments

Comments
 (0)