Skip to content

Commit 5f1b008

Browse files
committed
SIL: fix memory leaks in SILParser
* Leaks due to forwarding values * Leaks due to abort on errors rdar://problem/66931238
1 parent 170d9e9 commit 5f1b008

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ namespace {
189189
TUState(*static_cast<SILParserState *>(P.SIL)),
190190
ParsedTypeCallback([](Type ty) {}) {}
191191

192+
~SILParser();
193+
192194
/// diagnoseProblems - After a function is fully parse, emit any diagnostics
193195
/// for errors and return true if there were any.
194196
bool diagnoseProblems();
@@ -516,6 +518,17 @@ bool SILParser::parseVerbatim(StringRef name) {
516518
return false;
517519
}
518520

521+
SILParser::~SILParser() {
522+
for (auto &Entry : ForwardRefLocalValues) {
523+
if (ValueBase *dummyVal = LocalValues[Entry.first()]) {
524+
dummyVal->replaceAllUsesWith(SILUndef::get(dummyVal->getType(), SILMod, ValueOwnershipKind::None));
525+
SILInstruction::destroy(cast<GlobalAddrInst>(dummyVal));
526+
SILMod.deallocateInst(cast<GlobalAddrInst>(dummyVal));
527+
}
528+
}
529+
}
530+
531+
519532
/// diagnoseProblems - After a function is fully parse, emit any diagnostics
520533
/// for errors and return true if there were any.
521534
bool SILParser::diagnoseProblems() {
@@ -686,7 +699,7 @@ SILValue SILParser::getLocalValue(UnresolvedValueName Name, SILType Type,
686699
P.diagnose(Name.NameLoc, diag::sil_value_use_type_mismatch, Name.Name,
687700
EntryTy.getASTType(), Type.getASTType());
688701
// Make sure to return something of the requested type.
689-
return new (SILMod) GlobalAddrInst(getDebugLoc(B, Loc), Type);
702+
return SILUndef::get(Type, B.getFunction());
690703
}
691704

692705
return SILValue(Entry);
@@ -724,6 +737,8 @@ void SILParser::setLocalValue(ValueBase *Value, StringRef Name,
724737
} else {
725738
// Forward references only live here if they have a single result.
726739
Entry->replaceAllUsesWith(Value);
740+
SILInstruction::destroy(cast<GlobalAddrInst>(Entry));
741+
SILMod.deallocateInst(cast<GlobalAddrInst>(Entry));
727742
}
728743
Entry = Value;
729744
return;

0 commit comments

Comments
 (0)