Skip to content

Commit 509f5c4

Browse files
author
hgreving
committed
[MC] Fix memory leak when allocating MCInst with bump allocator
Adds the function createMCInst() to MCContext that creates a MCInst using a typed bump alloctor. MCInst contains a SmallVector<MCOperand, 8>. The SmallVector is POD only for <= 8 operands. The default untyped bump pointer allocator of MCContext does not delete the MCInst, so if the SmallVector grows, it's a leak. This fixes https://bugs.llvm.org/show_bug.cgi?id=46900.
1 parent 045e79e commit 509f5c4

File tree

7 files changed

+31
-16
lines changed

7 files changed

+31
-16
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ namespace llvm {
9797
SpecificBumpPtrAllocator<MCSectionMachO> MachOAllocator;
9898
SpecificBumpPtrAllocator<MCSectionWasm> WasmAllocator;
9999
SpecificBumpPtrAllocator<MCSectionXCOFF> XCOFFAllocator;
100+
SpecificBumpPtrAllocator<MCInst> MCInstAllocator;
100101

101102
/// Bindings of names to symbols.
102103
SymbolTable Symbols;
@@ -380,6 +381,11 @@ namespace llvm {
380381

381382
/// @}
382383

384+
/// \name McInst Management
385+
386+
/// Create and return a new MC instruction.
387+
MCInst *createMCInst();
388+
383389
/// \name Symbol Management
384390
/// @{
385391

llvm/lib/MC/MCContext.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void MCContext::reset() {
9090
ELFAllocator.DestroyAll();
9191
MachOAllocator.DestroyAll();
9292
XCOFFAllocator.DestroyAll();
93+
MCInstAllocator.DestroyAll();
9394

9495
MCSubtargetAllocator.DestroyAll();
9596
InlineAsmUsedLabelNames.clear();
@@ -126,6 +127,14 @@ void MCContext::reset() {
126127
HadError = false;
127128
}
128129

130+
//===----------------------------------------------------------------------===//
131+
// MCInst Management
132+
//===----------------------------------------------------------------------===//
133+
134+
MCInst *MCContext::createMCInst() {
135+
return new (MCInstAllocator.Allocate()) MCInst;
136+
}
137+
129138
//===----------------------------------------------------------------------===//
130139
// Symbol Manipulation
131140
//===----------------------------------------------------------------------===//

llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
641641
return true;
642642
return finishBundle(IDLoc, Out);
643643
}
644-
MCInst *SubInst = new (getParser().getContext()) MCInst;
644+
MCInst *SubInst = getParser().getContext().createMCInst();
645645
if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
646646
MatchingInlineAsm)) {
647647
if (InBrackets)

llvm/lib/Target/Hexagon/Disassembler/HexagonDisassembler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
175175
while (Result == Success && !Complete) {
176176
if (Bytes.size() < HEXAGON_INSTR_SIZE)
177177
return MCDisassembler::Fail;
178-
MCInst *Inst = new (getContext()) MCInst;
178+
MCInst *Inst = getContext().createMCInst();
179179
Result = getSingleInstruction(*Inst, MI, Bytes, Address, cs, Complete);
180180
MI.addOperand(MCOperand::createInst(Inst));
181181
Size += HEXAGON_INSTR_SIZE;
@@ -384,8 +384,8 @@ DecodeStatus HexagonDisassembler::getSingleInstruction(MCInst &MI, MCInst &MCB,
384384
break;
385385
}
386386
MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
387-
MCInst *MILow = new (getContext()) MCInst;
388-
MCInst *MIHigh = new (getContext()) MCInst;
387+
MCInst *MILow = getContext().createMCInst();
388+
MCInst *MIHigh = getContext().createMCInst();
389389
auto TmpExtender = CurrentExtender;
390390
CurrentExtender =
391391
nullptr; // constant extenders in duplex must always be in slot 1

llvm/lib/Target/Hexagon/HexagonMCInstLower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
104104
HexagonMCInstrInfo::setOuterLoop(MCB);
105105
return;
106106
}
107-
MCInst *MCI = new (AP.OutContext) MCInst;
107+
MCInst *MCI = AP.OutContext.createMCInst();
108108
MCI->setOpcode(MI->getOpcode());
109109
assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
110110
"MCI opcode should have been set on construction");

llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class HexagonAsmBackend : public MCAsmBackend {
7474

7575
void setExtender(MCContext &Context) const {
7676
if (Extender == nullptr)
77-
const_cast<HexagonAsmBackend *>(this)->Extender = new (Context) MCInst;
77+
const_cast<HexagonAsmBackend *>(this)->Extender = Context.createMCInst();
7878
}
7979

8080
MCInst *takeExtender() const {
@@ -736,7 +736,7 @@ class HexagonAsmBackend : public MCAsmBackend {
736736
auto &Inst = const_cast<MCInst &>(RF.getInst());
737737
while (Size > 0 &&
738738
HexagonMCInstrInfo::bundleSize(Inst) < MaxPacketSize) {
739-
MCInst *Nop = new (Context) MCInst;
739+
MCInst *Nop = Context.createMCInst();
740740
Nop->setOpcode(Hexagon::A2_nop);
741741
Inst.addOperand(MCOperand::createInst(Nop));
742742
Size -= 4;

llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCCompound.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
210210
case Hexagon::A2_tfrsi:
211211
Rt = L.getOperand(0);
212212
compoundOpcode = J4_jumpseti;
213-
CompoundInsn = new (Context) MCInst;
213+
CompoundInsn = Context.createMCInst();
214214
CompoundInsn->setOpcode(compoundOpcode);
215215

216216
CompoundInsn->addOperand(Rt);
@@ -223,7 +223,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
223223
Rs = L.getOperand(1);
224224

225225
compoundOpcode = J4_jumpsetr;
226-
CompoundInsn = new (Context) MCInst;
226+
CompoundInsn = Context.createMCInst();
227227
CompoundInsn->setOpcode(compoundOpcode);
228228
CompoundInsn->addOperand(Rt);
229229
CompoundInsn->addOperand(Rs);
@@ -237,7 +237,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
237237
Rt = L.getOperand(2);
238238

239239
compoundOpcode = cmpeqBitOpcode[getCompoundOp(R)];
240-
CompoundInsn = new (Context) MCInst;
240+
CompoundInsn = Context.createMCInst();
241241
CompoundInsn->setOpcode(compoundOpcode);
242242
CompoundInsn->addOperand(Rs);
243243
CompoundInsn->addOperand(Rt);
@@ -250,7 +250,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
250250
Rt = L.getOperand(2);
251251

252252
compoundOpcode = cmpgtBitOpcode[getCompoundOp(R)];
253-
CompoundInsn = new (Context) MCInst;
253+
CompoundInsn = Context.createMCInst();
254254
CompoundInsn->setOpcode(compoundOpcode);
255255
CompoundInsn->addOperand(Rs);
256256
CompoundInsn->addOperand(Rt);
@@ -263,7 +263,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
263263
Rt = L.getOperand(2);
264264

265265
compoundOpcode = cmpgtuBitOpcode[getCompoundOp(R)];
266-
CompoundInsn = new (Context) MCInst;
266+
CompoundInsn = Context.createMCInst();
267267
CompoundInsn->setOpcode(compoundOpcode);
268268
CompoundInsn->addOperand(Rs);
269269
CompoundInsn->addOperand(Rt);
@@ -281,7 +281,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
281281
compoundOpcode = cmpeqiBitOpcode[getCompoundOp(R)];
282282

283283
Rs = L.getOperand(1);
284-
CompoundInsn = new (Context) MCInst;
284+
CompoundInsn = Context.createMCInst();
285285
CompoundInsn->setOpcode(compoundOpcode);
286286
CompoundInsn->addOperand(Rs);
287287
CompoundInsn->addOperand(L.getOperand(2));
@@ -299,7 +299,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
299299
compoundOpcode = cmpgtiBitOpcode[getCompoundOp(R)];
300300

301301
Rs = L.getOperand(1);
302-
CompoundInsn = new (Context) MCInst;
302+
CompoundInsn = Context.createMCInst();
303303
CompoundInsn->setOpcode(compoundOpcode);
304304
CompoundInsn->addOperand(Rs);
305305
CompoundInsn->addOperand(L.getOperand(2));
@@ -310,7 +310,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
310310
LLVM_DEBUG(dbgs() << "CX: C2_cmpgtui\n");
311311
Rs = L.getOperand(1);
312312
compoundOpcode = cmpgtuiBitOpcode[getCompoundOp(R)];
313-
CompoundInsn = new (Context) MCInst;
313+
CompoundInsn = Context.createMCInst();
314314
CompoundInsn->setOpcode(compoundOpcode);
315315
CompoundInsn->addOperand(Rs);
316316
CompoundInsn->addOperand(L.getOperand(2));
@@ -321,7 +321,7 @@ static MCInst *getCompoundInsn(MCContext &Context, MCInst const &L,
321321
LLVM_DEBUG(dbgs() << "CX: S2_tstbit_i\n");
322322
Rs = L.getOperand(1);
323323
compoundOpcode = tstBitOpcode[getCompoundOp(R)];
324-
CompoundInsn = new (Context) MCInst;
324+
CompoundInsn = Context.createMCInst();
325325
CompoundInsn->setOpcode(compoundOpcode);
326326
CompoundInsn->addOperand(Rs);
327327
CompoundInsn->addOperand(R.getOperand(1));

0 commit comments

Comments
 (0)