Skip to content

Commit 5fa7d8e

Browse files
committed
Fix a nullptr deref in GetCanonicalSwiftType.
<rdar://problem/64347693>
1 parent 4d9bda3 commit 5fa7d8e

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

lldb/include/lldb/Symbol/SwiftASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ class SwiftASTContext : public TypeSystemSwift {
560560

561561
~SwiftASTContext();
562562

563+
#ifndef NDEBUG
564+
/// Provided only for unit tests.
565+
SwiftASTContext();
566+
#endif
567+
563568
const std::string &GetDescription() const;
564569

565570
// PluginInterface functions

lldb/source/Symbol/SwiftASTContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,12 @@ static std::string GetClangModulesCacheProperty() {
893893
return path.str();
894894
}
895895

896+
#ifndef NDEBUG
897+
SwiftASTContext::SwiftASTContext() : m_typeref_typesystem(this) {
898+
llvm::dbgs() << "Initialized mock SwiftASTContext\n";
899+
}
900+
#endif
901+
896902
SwiftASTContext::SwiftASTContext(std::string description, llvm::Triple triple,
897903
Target *target)
898904
: TypeSystemSwift(), m_typeref_typesystem(this),

lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ swift::Type GetSwiftType(CompilerType type) {
4444
}
4545

4646
swift::CanType GetCanonicalSwiftType(CompilerType type) {
47+
swift::Type swift_type = nullptr;
4748
auto *ts = type.GetTypeSystem();
4849
if (auto *tr = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(ts))
49-
return tr->GetSwiftType(type)->getCanonicalType();
50+
swift_type = tr->GetSwiftType(type);
5051
if (auto *ast = llvm::dyn_cast_or_null<SwiftASTContext>(ts))
51-
return ast->GetSwiftType(type)->getCanonicalType();
52-
return swift::CanType();
52+
swift_type = ast->GetSwiftType(type);
53+
return swift_type ? swift_type->getCanonicalType() : swift::CanType();
5354
}
5455

5556
static lldb::addr_t

lldb/unittests/Symbol/TestSwiftASTContext.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ struct TestSwiftASTContext : public testing::Test {
6161
};
6262

6363
struct SwiftASTContextTester : public SwiftASTContext {
64+
SwiftASTContextTester() : SwiftASTContext() {}
65+
6466
static std::string GetResourceDir(llvm::StringRef platform_sdk_path,
6567
std::string swift_dir,
6668
std::string swift_stdlib_os_dir,
@@ -184,3 +186,14 @@ TEST_F(TestSwiftASTContext, ResourceDir) {
184186
swift_dir = path::parent_path(abs_paths[9]);
185187
EXPECT_EQ(GetResourceDir("x86_64-apple-macosx", macosx_sdk), swift_dir);
186188
}
189+
190+
TEST_F(TestSwiftASTContext, IsNonTriviallyManagedReferenceType) {
191+
#ifndef NDEBUG
192+
// The mock constructor is only available in asserts mode.
193+
SwiftASTContext::NonTriviallyManagedReferenceStrategy strategy;
194+
SwiftASTContext context;
195+
CompilerType t(&context, nullptr);
196+
EXPECT_FALSE(SwiftASTContext::IsNonTriviallyManagedReferenceType(t, strategy,
197+
nullptr));
198+
#endif
199+
}

0 commit comments

Comments
 (0)