Skip to content

[SIL] Fix use-after-free in SILFunction::print. #29949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,13 @@ void SILType::dump() const {
}

/// Prints the name and type of the given SIL function with the given
/// `PrintOptions`. Mutates `printOptions`, setting `GenericEnv` and
/// `AlternativeTypeNames`.
static void printSILFunctionNameAndType(llvm::raw_ostream &OS,
const SILFunction *function,
PrintOptions &printOptions) {
/// `PrintOptions`. Computes mapping for sugared type names and stores the
/// result in `sugaredTypeNames`.
static void printSILFunctionNameAndType(
llvm::raw_ostream &OS, const SILFunction *function,
llvm::DenseMap<CanType, Identifier> &sugaredTypeNames) {
function->printName(OS);
OS << " : $";
llvm::DenseMap<CanType, Identifier> aliases;
auto genSig = function->getLoweredFunctionType()->getSubstGenericSignature();
auto *genEnv = function->getGenericEnvironment();
// If `genSig` and `genEnv` are both defined, get sugared names of generic
Expand All @@ -470,22 +469,24 @@ static void printSILFunctionNameAndType(llvm::raw_ostream &OS,
continue;
// Otherwise, add sugared name mapping for the type (and its archetype, if
// defined).
aliases[paramTy->getCanonicalType()] = name;
sugaredTypeNames[paramTy->getCanonicalType()] = name;
if (auto *archetypeTy =
genEnv->mapTypeIntoContext(paramTy)->getAs<ArchetypeType>())
aliases[archetypeTy->getCanonicalType()] = name;
sugaredTypeNames[archetypeTy->getCanonicalType()] = name;
}
}
auto printOptions = PrintOptions::printSIL();
printOptions.GenericEnv = genEnv;
printOptions.AlternativeTypeNames = aliases.empty() ? nullptr : &aliases;
printOptions.AlternativeTypeNames =
sugaredTypeNames.empty() ? nullptr : &sugaredTypeNames;
function->getLoweredFunctionType()->print(OS, printOptions);
}

/// Prints the name and type of the given SIL function.
static void printSILFunctionNameAndType(llvm::raw_ostream &OS,
const SILFunction *function) {
auto printOptions = PrintOptions::printSIL();
printSILFunctionNameAndType(OS, function, printOptions);
llvm::DenseMap<CanType, Identifier> sugaredTypeNames;
printSILFunctionNameAndType(OS, function, sugaredTypeNames);
}

namespace {
Expand Down Expand Up @@ -2513,16 +2514,17 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
if (!isExternalDeclaration() && hasOwnership())
OS << "[ossa] ";

auto printOptions = PrintOptions::printSIL();
printSILFunctionNameAndType(OS, this, printOptions);
llvm::DenseMap<CanType, Identifier> sugaredTypeNames;
printSILFunctionNameAndType(OS, this, sugaredTypeNames);

if (!isExternalDeclaration()) {
if (auto eCount = getEntryCount()) {
OS << " !function_entry_count(" << eCount.getValue() << ")";
}
OS << " {\n";

SILPrinter(PrintCtx, printOptions.AlternativeTypeNames).print(this);
SILPrinter(PrintCtx, sugaredTypeNames.empty() ? nullptr : &sugaredTypeNames)
.print(this);
OS << "} // end sil function '" << getName() << '\'';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-swift-frontend -emit-sil %s

// SR-12239: use-after-free in `SILFunction::print`.

func outer<C>(_ x: C) {
func inner<C>(_ x: C) {}
}