This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by thakis on Nov 18 2022, 11:33 AM.

Details

Summary

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.

Diff Detail

Event Timeline

thakis created this revision.Nov 18 2022, 11:33 AM
Herald added a reviewer: maksfb. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
thakis requested review of this revision.Nov 18 2022, 11:33 AM
thakis edited the summary of this revision. (Show Details)

(ref D129260)

thakis edited the summary of this revision. (Show Details)Nov 18 2022, 11:36 AM
thakis added a reviewer: Restricted Project.
yota9 accepted this revision as: yota9.Nov 18 2022, 11:38 AM

LGTM Thanks.

This revision is now accepted and ready to land.Nov 18 2022, 11:38 AM
This revision was landed with ongoing or failed builds.Nov 18 2022, 11:42 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptNov 18 2022, 11:42 AM