Skip to content

Commit 4e186f2

Browse files
authored
[LoongArch] Fix assertion failure for annotate tablejump (#140907)
Fix a use-after-free issue related to annotateTableJump in the LoongArch target. Previously, `LoongArchPreRAExpandPseudo::annotateTableJump()` recorded a reference to a MachineOperand representing a jump table index. However, later optimizations such as the `BranchFolder` pass may delete the instruction containing this operand, leaving a dangling reference. This led to an assertion failure in `LoongArchAsmPrinter::emitJumpTableInfo()` when trying to access a freed MachineOperand via `getIndex()`. The fix avoids holding a reference to the MachineOperand. Instead, we extract and store the jump table index at the time of annotation. During `emitJumpTableInfo()`, we verify whether the recorded index still exists in the MachineFunction's jump table. If not, we skip emission for that entry. Fixes #140904
1 parent bd8578c commit 4e186f2

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,28 +265,32 @@ void LoongArchAsmPrinter::emitJumpTableInfo() {
265265

266266
assert(TM.getTargetTriple().isOSBinFormatELF());
267267

268-
unsigned Size = getDataLayout().getPointerSize();
269268
auto *LAFI = MF->getInfo<LoongArchMachineFunctionInfo>();
270269
unsigned EntrySize = LAFI->getJumpInfoSize();
270+
auto JTI = MF->getJumpTableInfo();
271271

272-
if (0 == EntrySize)
272+
if (!JTI || 0 == EntrySize)
273273
return;
274274

275+
unsigned Size = getDataLayout().getPointerSize();
276+
auto JT = JTI->getJumpTables();
277+
275278
// Emit an additional section to store the correlation info as pairs of
276279
// addresses, each pair contains the address of a jump instruction (jr) and
277280
// the address of the jump table.
278281
OutStreamer->switchSection(MMI->getContext().getELFSection(
279282
".discard.tablejump_annotate", ELF::SHT_PROGBITS, 0));
280283

281284
for (unsigned Idx = 0; Idx < EntrySize; ++Idx) {
285+
int JTIIdx = LAFI->getJumpInfoJTIIndex(Idx);
286+
if (JT[JTIIdx].MBBs.empty())
287+
continue;
282288
OutStreamer->emitValue(
283289
MCSymbolRefExpr::create(LAFI->getJumpInfoJrMI(Idx)->getPreInstrSymbol(),
284290
OutContext),
285291
Size);
286292
OutStreamer->emitValue(
287-
MCSymbolRefExpr::create(
288-
GetJTISymbol(LAFI->getJumpInfoJTIMO(Idx)->getIndex()), OutContext),
289-
Size);
293+
MCSymbolRefExpr::create(GetJTISymbol(JTIIdx), OutContext), Size);
290294
}
291295
}
292296

llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ void LoongArchPreRAExpandPseudo::annotateTableJump(
636636
if (MO.isJTI()) {
637637
MBBI->setPreInstrSymbol(
638638
*MF, MF->getContext().createNamedTempSymbol("jrtb_"));
639-
MF->getInfo<LoongArchMachineFunctionInfo>()->setJumpInfo(&*MBBI, &MO);
639+
MF->getInfo<LoongArchMachineFunctionInfo>()->setJumpInfo(
640+
&*MBBI, MO.getIndex());
640641
IsFound = true;
641642
return;
642643
}

llvm/lib/Target/LoongArch/LoongArchMachineFunctionInfo.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LoongArchMachineFunctionInfo : public MachineFunctionInfo {
4141

4242
/// Pairs of `jr` instructions and corresponding JTI operands, used for the
4343
/// `annotate-tablejump` option.
44-
SmallVector<std::pair<MachineInstr *, MachineOperand *>, 4> JumpInfos;
44+
SmallVector<std::pair<MachineInstr *, int>, 4> JumpInfos;
4545

4646
public:
4747
LoongArchMachineFunctionInfo(const Function &F,
@@ -76,14 +76,12 @@ class LoongArchMachineFunctionInfo : public MachineFunctionInfo {
7676
return is_contained(SExt32Registers, Reg);
7777
}
7878

79-
void setJumpInfo(MachineInstr *JrMI, MachineOperand *JTIMO) {
80-
JumpInfos.push_back(std::make_pair(JrMI, JTIMO));
79+
void setJumpInfo(MachineInstr *JrMI, int JTIIdx) {
80+
JumpInfos.push_back(std::make_pair(JrMI, JTIIdx));
8181
}
8282
unsigned getJumpInfoSize() { return JumpInfos.size(); }
8383
MachineInstr *getJumpInfoJrMI(unsigned Idx) { return JumpInfos[Idx].first; }
84-
MachineOperand *getJumpInfoJTIMO(unsigned Idx) {
85-
return JumpInfos[Idx].second;
86-
}
84+
int getJumpInfoJTIIndex(unsigned Idx) { return JumpInfos[Idx].second; }
8785
};
8886

8987
} // end namespace llvm

0 commit comments

Comments
 (0)