Skip to content

Commit 1c9f4d4

Browse files
authored
[ARM] Avoid reference into modified vector (#93965)
FirstCand is a reference to RepeatedSequenceLocs[0]. However, that vector is being modified a lot throughout the function, including one place that reassigns the whole vector. I'm not sure whether this can really happen in practice, but it doesn't seem unlikely that this could lead to a use-after-free. Avoid this by directly using RepeatedSequenceLocs[0] at the start of the function (as a lot of other places already do) and only creating FirstCand at the end where no more modifications take place.
1 parent 4023f4e commit 1c9f4d4

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8257,10 +8257,8 @@ static bool outliningCandidatesV8_3OpsConsensus(const outliner::Candidate &a,
82578257
std::optional<outliner::OutlinedFunction>
82588258
AArch64InstrInfo::getOutliningCandidateInfo(
82598259
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
8260-
outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
8261-
82628260
unsigned SequenceSize = 0;
8263-
for (auto &MI : FirstCand)
8261+
for (auto &MI : RepeatedSequenceLocs[0])
82648262
SequenceSize += getInstSizeInBytes(MI);
82658263

82668264
unsigned NumBytesToCreateFrame = 0;
@@ -8303,7 +8301,8 @@ AArch64InstrInfo::getOutliningCandidateInfo(
83038301
// Performing a tail call may require extra checks when PAuth is enabled.
83048302
// If PAuth is disabled, set it to zero for uniformity.
83058303
unsigned NumBytesToCheckLRInTCEpilogue = 0;
8306-
if (FirstCand.getMF()
8304+
if (RepeatedSequenceLocs[0]
8305+
.getMF()
83078306
->getInfo<AArch64FunctionInfo>()
83088307
->shouldSignReturnAddress(true)) {
83098308
// One PAC and one AUT instructions
@@ -8475,7 +8474,8 @@ AArch64InstrInfo::getOutliningCandidateInfo(
84758474

84768475
// True if it's possible to fix up each stack instruction in this sequence.
84778476
// Important for frames/call variants that modify the stack.
8478-
bool AllStackInstrsSafe = llvm::all_of(FirstCand, IsSafeToFixup);
8477+
bool AllStackInstrsSafe =
8478+
llvm::all_of(RepeatedSequenceLocs[0], IsSafeToFixup);
84798479

84808480
// If the last instruction in any candidate is a terminator, then we should
84818481
// tail call all of the candidates.
@@ -8625,6 +8625,7 @@ AArch64InstrInfo::getOutliningCandidateInfo(
86258625
if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
86268626
// Check if the range contains a call. These require a save + restore of the
86278627
// link register.
8628+
outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
86288629
bool ModStackToSaveLR = false;
86298630
if (std::any_of(FirstCand.begin(), std::prev(FirstCand.end()),
86308631
[](const MachineInstr &MI) { return MI.isCall(); }))

llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5873,10 +5873,8 @@ static bool isLRAvailable(const TargetRegisterInfo &TRI,
58735873
std::optional<outliner::OutlinedFunction>
58745874
ARMBaseInstrInfo::getOutliningCandidateInfo(
58755875
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
5876-
outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
5877-
58785876
unsigned SequenceSize = 0;
5879-
for (auto &MI : FirstCand)
5877+
for (auto &MI : RepeatedSequenceLocs[0])
58805878
SequenceSize += getInstSizeInBytes(MI);
58815879

58825880
// Properties about candidate MBBs that hold for all of them.
@@ -6071,6 +6069,7 @@ ARMBaseInstrInfo::getOutliningCandidateInfo(
60716069
if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
60726070
// check if the range contains a call. These require a save + restore of
60736071
// the link register.
6072+
outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
60746073
if (std::any_of(FirstCand.begin(), std::prev(FirstCand.end()),
60756074
[](const MachineInstr &MI) { return MI.isCall(); }))
60766075
NumBytesToCreateFrame += Costs.SaveRestoreLROnStack;

0 commit comments

Comments
 (0)