Skip to content

Commit ec28e43

Browse files
committed
Add null-pointer checks when accessing a TypeSystem's SymbolFile
A type system is not guaranteed to have a symbol file. This patch adds null-pointer checks so we don't crash when trying to access a type system's symbol file. Reviewed By: aprantl, teemperor Differential Revision: https://reviews.llvm.org/D101539
1 parent 6c82b8a commit ec28e43

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,12 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
534534
auto type_def = llvm::dyn_cast<PDBSymbolTypeTypedef>(&type);
535535
assert(type_def);
536536

537+
SymbolFile *symbol_file = m_ast.GetSymbolFile();
538+
if (!symbol_file)
539+
return nullptr;
540+
537541
lldb_private::Type *target_type =
538-
m_ast.GetSymbolFile()->ResolveTypeUID(type_def->getTypeId());
542+
symbol_file->ResolveTypeUID(type_def->getTypeId());
539543
if (!target_type)
540544
return nullptr;
541545

@@ -609,8 +613,13 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
609613
auto arg = arg_enum->getChildAtIndex(arg_idx);
610614
if (!arg)
611615
break;
616+
617+
SymbolFile *symbol_file = m_ast.GetSymbolFile();
618+
if (!symbol_file)
619+
return nullptr;
620+
612621
lldb_private::Type *arg_type =
613-
m_ast.GetSymbolFile()->ResolveTypeUID(arg->getSymIndexId());
622+
symbol_file->ResolveTypeUID(arg->getSymIndexId());
614623
// If there's some error looking up one of the dependent types of this
615624
// function signature, bail.
616625
if (!arg_type)
@@ -621,8 +630,12 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
621630
lldbassert(arg_list.size() <= num_args);
622631

623632
auto pdb_return_type = func_sig->getReturnType();
633+
SymbolFile *symbol_file = m_ast.GetSymbolFile();
634+
if (!symbol_file)
635+
return nullptr;
636+
624637
lldb_private::Type *return_type =
625-
m_ast.GetSymbolFile()->ResolveTypeUID(pdb_return_type->getSymIndexId());
638+
symbol_file->ResolveTypeUID(pdb_return_type->getSymIndexId());
626639
// If there's some error looking up one of the dependent types of this
627640
// function signature, bail.
628641
if (!return_type)
@@ -654,10 +667,13 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
654667
if (uint64_t size = array_type->getLength())
655668
bytes = size;
656669

670+
SymbolFile *symbol_file = m_ast.GetSymbolFile();
671+
if (!symbol_file)
672+
return nullptr;
673+
657674
// If array rank > 0, PDB gives the element type at N=0. So element type
658675
// will parsed in the order N=0, N=1,..., N=rank sequentially.
659-
lldb_private::Type *element_type =
660-
m_ast.GetSymbolFile()->ResolveTypeUID(element_uid);
676+
lldb_private::Type *element_type = symbol_file->ResolveTypeUID(element_uid);
661677
if (!element_type)
662678
return nullptr;
663679

@@ -711,16 +727,20 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
711727
case PDB_SymType::PointerType: {
712728
auto *pointer_type = llvm::dyn_cast<PDBSymbolTypePointer>(&type);
713729
assert(pointer_type);
714-
Type *pointee_type = m_ast.GetSymbolFile()->ResolveTypeUID(
730+
731+
SymbolFile *symbol_file = m_ast.GetSymbolFile();
732+
if (!symbol_file)
733+
return nullptr;
734+
735+
Type *pointee_type = symbol_file->ResolveTypeUID(
715736
pointer_type->getPointeeType()->getSymIndexId());
716737
if (!pointee_type)
717738
return nullptr;
718739

719740
if (pointer_type->isPointerToDataMember() ||
720741
pointer_type->isPointerToMemberFunction()) {
721742
auto class_parent_uid = pointer_type->getRawSymbol().getClassParentId();
722-
auto class_parent_type =
723-
m_ast.GetSymbolFile()->ResolveTypeUID(class_parent_uid);
743+
auto class_parent_type = symbol_file->ResolveTypeUID(class_parent_uid);
724744
assert(class_parent_type);
725745

726746
CompilerType pointer_ast_type;

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9306,11 +9306,11 @@ CompilerType TypeSystemClang::DeclGetFunctionArgumentType(void *opaque_decl,
93069306
std::vector<CompilerDecl> TypeSystemClang::DeclContextFindDeclByName(
93079307
void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
93089308
std::vector<CompilerDecl> found_decls;
9309-
if (opaque_decl_ctx) {
9309+
SymbolFile *symbol_file = GetSymbolFile();
9310+
if (opaque_decl_ctx && symbol_file) {
93109311
DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
93119312
std::set<DeclContext *> searched;
93129313
std::multimap<DeclContext *, DeclContext *> search_queue;
9313-
SymbolFile *symbol_file = GetSymbolFile();
93149314

93159315
for (clang::DeclContext *decl_context = root_decl_ctx;
93169316
decl_context != nullptr && found_decls.empty();
@@ -9404,10 +9404,10 @@ uint32_t TypeSystemClang::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
94049404
clang::DeclContext *child_decl_ctx,
94059405
ConstString *child_name,
94069406
CompilerType *child_type) {
9407-
if (frame_decl_ctx) {
9407+
SymbolFile *symbol_file = GetSymbolFile();
9408+
if (frame_decl_ctx && symbol_file) {
94089409
std::set<DeclContext *> searched;
94099410
std::multimap<DeclContext *, DeclContext *> search_queue;
9410-
SymbolFile *symbol_file = GetSymbolFile();
94119411

94129412
// Get the lookup scope for the decl we're trying to find.
94139413
clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();

lldb/source/Symbol/Type.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,8 @@ ModuleSP Type::GetModule() {
730730
ModuleSP Type::GetExeModule() {
731731
if (m_compiler_type) {
732732
SymbolFile *symbol_file = m_compiler_type.GetTypeSystem()->GetSymbolFile();
733-
return symbol_file->GetObjectFile()->GetModule();
733+
if (symbol_file)
734+
return symbol_file->GetObjectFile()->GetModule();
734735
}
735736
return ModuleSP();
736737
}

lldb/unittests/Symbol/TestTypeSystemClang.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,15 @@ TEST(TestScratchTypeSystemClang, InferSubASTFromLangOpts) {
741741
ScratchTypeSystemClang::IsolatedASTKind::CppModules,
742742
ScratchTypeSystemClang::InferIsolatedASTKindFromLangOpts(lang_opts));
743743
}
744+
745+
TEST_F(TestTypeSystemClang, GetExeModuleWhenMissingSymbolFile) {
746+
CompilerType compiler_type = m_ast->GetBasicTypeFromAST(lldb::eBasicTypeInt);
747+
lldb_private::Type t(0, nullptr, ConstString("MyType"), llvm::None, nullptr,
748+
0, {}, {}, compiler_type,
749+
lldb_private::Type::ResolveState::Full);
750+
// Test that getting the execution module when no type system is present
751+
// is handled gracefully.
752+
ModuleSP module = t.GetExeModule();
753+
EXPECT_EQ(module.get(), nullptr);
754+
}
755+

0 commit comments

Comments
 (0)