[PATCH] D138313: [bolt] Fix std::prev()-past-begin in veneer handling code

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 18 11:33:56 PST 2022


thakis created this revision.
thakis added a reviewer: rafauler.
Herald added subscribers: treapster, ayermolo, kristof.beyls.
Herald added a reviewer: Amir.
Herald added a reviewer: maksfb.
Herald added a project: All.
thakis requested review of this revision.

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.


https://reviews.llvm.org/D138313

Files:
  bolt/lib/Core/BinaryContext.cpp


Index: bolt/lib/Core/BinaryContext.cpp
===================================================================
--- bolt/lib/Core/BinaryContext.cpp
+++ bolt/lib/Core/BinaryContext.cpp
@@ -1191,8 +1191,7 @@
     MIB->addAnnotation(Instruction, "AArch64Veneer", true);
     Veneer->addInstruction(Offset, std::move(Instruction));
     --Count;
-    for (auto It = std::prev(Instructions.end()); Count != 0;
-         It = std::prev(It), --Count) {
+    for (auto It = Instructions.rbegin(); Count != 0; ++It, --Count) {
       MIB->addAnnotation(It->second, "AArch64Veneer", true);
       Veneer->addInstruction(It->first, std::move(It->second));
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138313.476537.patch
Type: text/x-patch
Size: 647 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221118/1c63bd8a/attachment.bin>


More information about the llvm-commits mailing list