Skip to content

[Mangler] Include verification errors in the crash log #80496

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 2 commits into from
Apr 7, 2025
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
11 changes: 11 additions & 0 deletions include/swift/Basic/PrettyStackTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ class PrettyStackTraceSwiftVersion : public llvm::PrettyStackTraceEntry {
void print(llvm::raw_ostream &OS) const override;
};

/// Aborts the program, printing a given message to a PrettyStackTrace frame
/// before exiting.
[[noreturn]]
void abortWithPrettyStackTraceMessage(
llvm::function_ref<void(llvm::raw_ostream &)> message);

/// Aborts the program, printing a given message to a PrettyStackTrace frame
/// before exiting.
[[noreturn]]
void abortWithPrettyStackTraceMessage(llvm::StringRef message);

} // end namespace swift

#endif // SWIFT_BASIC_PRETTYSTACKTRACE_H
15 changes: 6 additions & 9 deletions lib/AST/ASTScopePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/STLExtras.h"
#include "llvm/Support/Compiler.h"
#include <algorithm>
Expand Down Expand Up @@ -71,15 +72,11 @@ void ASTScopeImpl::dumpOneScopeMapLocation(

void ASTScopeImpl::abortWithVerificationError(
llvm::function_ref<void(llvm::raw_ostream &)> messageFn) const {
llvm::SmallString<0> errorStr;
llvm::raw_svector_ostream out(errorStr);

out << "ASTScopeImpl verification error in source file '"
<< getSourceFile()->getFilename() << "':\n";
messageFn(out);

llvm::PrettyStackTraceString trace(errorStr.c_str());
abort();
abortWithPrettyStackTraceMessage([&](auto &out) {
out << "ASTScopeImpl verification error in source file '"
<< getSourceFile()->getFilename() << "':\n";
messageFn(out);
});
}

#pragma mark printing
Expand Down
20 changes: 12 additions & 8 deletions lib/Basic/Mangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "swift/Basic/Mangler.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Demangling/Demangler.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Demangling/Punycode.h"
Expand Down Expand Up @@ -201,22 +202,25 @@ void Mangler::verify(StringRef nameStr, ManglingFlavor Flavor) {
Demangler Dem;
NodePointer Root = Dem.demangleSymbol(nameStr);
if (!Root || treeContains(Root, Node::Kind::Suffix)) {
llvm::errs() << "Can't demangle: " << nameStr << '\n';
abort();
abortWithPrettyStackTraceMessage([&](auto &out) {
out << "Can't demangle: " << nameStr;
});
}
auto mangling = mangleNode(Root, Flavor);
if (!mangling.isSuccess()) {
llvm::errs() << "Can't remangle: " << nameStr << '\n';
abort();
abortWithPrettyStackTraceMessage([&](auto &out) {
out << "Can't remangle: " << nameStr;
});
}
std::string Remangled = mangling.result();
if (Remangled == nameStr)
return;

llvm::errs() << "Remangling failed:\n"
"original = " << nameStr << "\n"
"remangled = " << Remangled << "\n";
abort();
abortWithPrettyStackTraceMessage([&](auto &out) {
out << "Remangling failed:\n";
out << "original = " << nameStr << "\n";
out << "remangled = " << Remangled;
});
}

void Mangler::appendIdentifier(StringRef ident, bool allowRawIdentifiers) {
Expand Down
16 changes: 16 additions & 0 deletions lib/Basic/PrettyStackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/QuotedString.h"
#include "swift/Basic/Version.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"

Expand All @@ -38,3 +39,18 @@ void PrettyStackTraceFileContents::print(llvm::raw_ostream &out) const {
void PrettyStackTraceSwiftVersion::print(llvm::raw_ostream &out) const {
out << version::getSwiftFullVersion() << '\n';
}

void swift::abortWithPrettyStackTraceMessage(
llvm::function_ref<void(llvm::raw_ostream &)> message) {
llvm::SmallString<0> errorStr;
llvm::raw_svector_ostream out(errorStr);
message(out);
llvm::PrettyStackTraceString trace(errorStr.c_str());
abort();
}

void swift::abortWithPrettyStackTraceMessage(StringRef message) {
auto messageStr = message.str();
llvm::PrettyStackTraceString trace(messageStr.c_str());
abort();
}
31 changes: 14 additions & 17 deletions lib/Serialization/ModuleFileSharedCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/AST/Module.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Parse/ParseVersion.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Strings.h"
Expand Down Expand Up @@ -697,23 +698,19 @@ std::string ModuleFileSharedCore::Dependency::getPrettyPrintedPath() const {
}

void ModuleFileSharedCore::fatal(llvm::Error error) const {
llvm::SmallString<0> errorStr;
llvm::raw_svector_ostream out(errorStr);

out << "*** DESERIALIZATION FAILURE ***\n";
out << "*** If any module named here was modified in the SDK, please delete the ***\n";
out << "*** new swiftmodule files from the SDK and keep only swiftinterfaces. ***\n";
outputDiagnosticInfo(out);
out << "\n";
if (error) {
handleAllErrors(std::move(error), [&](const llvm::ErrorInfoBase &ei) {
ei.log(out);
out << "\n";
});
}

llvm::PrettyStackTraceString trace(errorStr.c_str());
abort();
abortWithPrettyStackTraceMessage([&](auto &out) {
out << "*** DESERIALIZATION FAILURE ***\n";
out << "*** If any module named here was modified in the SDK, please delete the ***\n";
out << "*** new swiftmodule files from the SDK and keep only swiftinterfaces. ***\n";
outputDiagnosticInfo(out);
out << "\n";
if (error) {
handleAllErrors(std::move(error), [&](const llvm::ErrorInfoBase &ei) {
ei.log(out);
out << "\n";
});
}
});
}

void ModuleFileSharedCore::outputDiagnosticInfo(llvm::raw_ostream &os) const {
Expand Down
7 changes: 3 additions & 4 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "swift/Basic/FileSystem.h"
#include "swift/Basic/LLVMExtras.h"
#include "swift/Basic/PathRemapper.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/Version.h"
#include "swift/ClangImporter/ClangImporter.h"
Expand Down Expand Up @@ -5314,8 +5315,7 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
SWIFT_DEFER {
// This is important enough to leave on in Release builds.
if (initialOffset == Out.GetCurrentBitNo()) {
llvm::PrettyStackTraceString message("failed to serialize anything");
abort();
abortWithPrettyStackTraceMessage("failed to serialize anything");
}
};

Expand Down Expand Up @@ -6141,8 +6141,7 @@ void Serializer::writeASTBlockEntity(Type ty) {
SWIFT_DEFER {
// This is important enough to leave on in Release builds.
if (initialOffset == Out.GetCurrentBitNo()) {
llvm::PrettyStackTraceString message("failed to serialize anything");
abort();
abortWithPrettyStackTraceMessage("failed to serialize anything");
}
};

Expand Down