Skip to content

Commit 21acf0e

Browse files
committed
Don't create a Module SwiftASTContext when the stdlib is missing
A Module SwiftASTContext that cannot access the stdlib module will crash when performing simple actions, like looking up a type. This patch tries to make the Target/Module overrides of CreateInstance look more like each other, w.r.t error handling in particular. rdar://57695158, rdar://64828733
1 parent f9957f0 commit 21acf0e

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

lldb/source/Symbol/SwiftASTContext.cpp

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,13 +1556,21 @@ static llvm::Optional<StringRef> GetDSYMBundle(Module &module) {
15561556
return dsym;
15571557
}
15581558

1559+
/// Detect whether a Swift module was "imported" by DWARFImporter.
1560+
/// All this *really* means is that it couldn't be loaded through any
1561+
/// other mechanism.
1562+
static bool IsDWARFImported(swift::ModuleDecl &module) {
1563+
return std::any_of(module.getFiles().begin(), module.getFiles().end(),
1564+
[](swift::FileUnit *file_unit) {
1565+
return (file_unit->getKind() ==
1566+
swift::FileUnitKind::DWARFModule);
1567+
});
1568+
}
1569+
15591570
lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
15601571
Module &module,
15611572
Target *target,
15621573
bool fallback) {
1563-
std::vector<std::string> module_search_paths;
1564-
std::vector<std::pair<std::string, bool>> framework_search_paths;
1565-
15661574
if (!SwiftASTContextSupportsLanguage(language))
15671575
return lldb::TypeSystemSP();
15681576

@@ -1576,16 +1584,33 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
15761584
module.GetDescription(ss, eDescriptionLevelBrief);
15771585
ss << '"' << ')';
15781586
}
1587+
std::vector<std::string> module_search_paths;
1588+
std::vector<std::pair<std::string, bool>> framework_search_paths;
1589+
1590+
LOG_PRINTF(LIBLLDB_LOG_TYPES, "(Module)");
1591+
1592+
auto logError = [&](const char *message) {
1593+
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Failed to create module context - %s",
1594+
message);
1595+
};
15791596

15801597
ArchSpec arch = module.GetArchitecture();
1598+
if (!arch.IsValid()) {
1599+
logError("invalid module architecture");
1600+
return TypeSystemSP();
1601+
}
15811602

15821603
ObjectFile *objfile = module.GetObjectFile();
1583-
if (!objfile)
1584-
return {};
1604+
if (!objfile) {
1605+
logError("no object file for module");
1606+
return TypeSystemSP();
1607+
}
15851608

15861609
ArchSpec object_arch = objfile->GetArchitecture();
1587-
if (!object_arch.IsValid())
1588-
return {};
1610+
if (!object_arch.IsValid()) {
1611+
logError("invalid objfile architecture");
1612+
return TypeSystemSP();
1613+
}
15891614

15901615
lldb::CompUnitSP main_compile_unit_sp = module.GetCompileUnitAtIndex(0);
15911616

@@ -1641,9 +1666,6 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16411666
swift_ast_sp->GetLanguageOptions().EnableAccessControl = false;
16421667
swift_ast_sp->GetLanguageOptions().EnableTargetOSChecking = false;
16431668

1644-
if (!arch.IsValid())
1645-
return TypeSystemSP();
1646-
16471669
swift_ast_sp->SetTriple(triple, &module);
16481670

16491671
bool set_triple = false;
@@ -1654,7 +1676,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16541676
std::string target_triple;
16551677

16561678
if (sym_file) {
1657-
bool got_serialized_options;
1679+
bool got_serialized_options = false;
16581680
llvm::SmallString<0> error;
16591681
llvm::raw_svector_ostream errs(error);
16601682
if (DeserializeAllCompilerFlags(*swift_ast_sp, module, m_description, errs,
@@ -1784,6 +1806,20 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
17841806
swift_ast_sp->LogConfiguration();
17851807
}
17861808
}
1809+
1810+
if (swift_ast_sp->HasFatalErrors()) {
1811+
logError(swift_ast_sp->GetFatalErrors().AsCString());
1812+
return {};
1813+
}
1814+
1815+
const bool can_create = true;
1816+
swift::ModuleDecl *stdlib =
1817+
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
1818+
if (!stdlib || IsDWARFImported(*stdlib)) {
1819+
logError("couldn't load the Swift stdlib");
1820+
return {};
1821+
}
1822+
17871823
return swift_ast_sp;
17881824
}
17891825

@@ -1840,17 +1876,6 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
18401876
return ModuleSP();
18411877
}
18421878

1843-
/// Detect whether a Swift module was "imported" by DWARFImporter.
1844-
/// All this *really* means is that it couldn't be loaded through any
1845-
/// other mechanism.
1846-
static bool IsDWARFImported(swift::ModuleDecl &module) {
1847-
return std::any_of(module.getFiles().begin(), module.getFiles().end(),
1848-
[](swift::FileUnit *file_unit) {
1849-
return (file_unit->getKind() ==
1850-
swift::FileUnitKind::DWARFModule);
1851-
});
1852-
}
1853-
18541879
lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
18551880
Target &target,
18561881
const char *extra_options) {

0 commit comments

Comments
 (0)