Skip to content

[lldb][TypeSystemClang] Add warning and defensive checks when ASTContext is not fully initialized #110481

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
Oct 1, 2024
Merged
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
39 changes: 35 additions & 4 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@
#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/DumpDataExtractor.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
@@ -697,10 +698,20 @@ void TypeSystemClang::CreateASTContext() {
TargetInfo *target_info = getTargetInfo();
if (target_info)
m_ast_up->InitBuiltinTypes(*target_info);
else if (auto *log = GetLog(LLDBLog::Expressions))
LLDB_LOG(log,
"Failed to initialize builtin ASTContext types for target '{0}'",
m_target_triple);
else {
std::string err =
llvm::formatv(
"Failed to initialize builtin ASTContext types for target '{0}'. "
"Printing variables may behave unexpectedly.",
m_target_triple)
.str();

LLDB_LOG(GetLog(LLDBLog::Expressions), err.c_str());

static std::once_flag s_uninitialized_target_warning;
Debugger::ReportWarning(std::move(err), /*debugger_id=*/std::nullopt,
&s_uninitialized_target_warning);
}

GetASTMap().Insert(m_ast_up.get(), this);

@@ -749,6 +760,10 @@ CompilerType
TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
size_t bit_size) {
ASTContext &ast = getASTContext();

if (!ast.VoidPtrTy)
return {};

switch (encoding) {
case eEncodingInvalid:
if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
@@ -891,6 +906,9 @@ CompilerType TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize(
llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size) {
ASTContext &ast = getASTContext();

if (!ast.VoidPtrTy)
return {};

switch (dw_ate) {
default:
break;
@@ -2335,6 +2353,9 @@ CompilerType TypeSystemClang::GetIntTypeFromBitSize(size_t bit_size,
bool is_signed) {
clang::ASTContext &ast = getASTContext();

if (!ast.VoidPtrTy)
return {};

if (is_signed) {
if (bit_size == ast.getTypeSize(ast.SignedCharTy))
return GetType(ast.SignedCharTy);
@@ -2376,6 +2397,9 @@ CompilerType TypeSystemClang::GetIntTypeFromBitSize(size_t bit_size,
}

CompilerType TypeSystemClang::GetPointerSizedIntType(bool is_signed) {
if (!getASTContext().VoidPtrTy)
return {};

return GetIntTypeFromBitSize(
getASTContext().getTypeSize(getASTContext().VoidPtrTy), is_signed);
}
@@ -7453,6 +7477,13 @@ clang::FieldDecl *TypeSystemClang::AddFieldToRecordType(

clang::Expr *bit_width = nullptr;
if (bitfield_bit_size != 0) {
if (clang_ast.IntTy.isNull()) {
LLDB_LOG(
GetLog(LLDBLog::Expressions),
"{0} failed: builtin ASTContext types have not been initialized");
return nullptr;
}

llvm::APInt bitfield_bit_size_apint(clang_ast.getTypeSize(clang_ast.IntTy),
bitfield_bit_size);
bit_width = new (clang_ast)
32 changes: 32 additions & 0 deletions lldb/unittests/Symbol/TestTypeSystemClang.cpp
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
#include "lldb/Core/Declaration.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/lldb-enumerations.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ExprCXX.h"
@@ -231,6 +232,37 @@ TEST_F(TestTypeSystemClang, TestBuiltinTypeForEncodingAndBitSize) {
VerifyEncodingAndBitSize(*m_ast, eEncodingIEEE754, 64);
}

TEST_F(TestTypeSystemClang, TestBuiltinTypeForEmptyTriple) {
// Test that we can access type-info of builtin Clang AST
// types without crashing even when the target triple is
// empty.

TypeSystemClang ast("empty triple AST", llvm::Triple{});

// This test only makes sense if the builtin ASTContext types were
// not initialized.
ASSERT_TRUE(ast.getASTContext().VoidPtrTy.isNull());

EXPECT_FALSE(ast.GetBuiltinTypeByName(ConstString("int")).IsValid());
EXPECT_FALSE(ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
"char", dwarf::DW_ATE_signed_char, 8)
.IsValid());
EXPECT_FALSE(ast.GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingUint, 8)
.IsValid());
EXPECT_FALSE(ast.GetPointerSizedIntType(/*is_signed=*/false));
EXPECT_FALSE(ast.GetIntTypeFromBitSize(8, /*is_signed=*/false));

CompilerType record_type = ast.CreateRecordType(
nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "Record",
llvm::to_underlying(clang::TagTypeKind::Struct),
lldb::eLanguageTypeC_plus_plus, std::nullopt);
TypeSystemClang::StartTagDeclarationDefinition(record_type);
EXPECT_EQ(ast.AddFieldToRecordType(record_type, "field", record_type,
eAccessPublic, /*bitfield_bit_size=*/8),
nullptr);
TypeSystemClang::CompleteTagDeclarationDefinition(record_type);
}

TEST_F(TestTypeSystemClang, TestDisplayName) {
TypeSystemClang ast("some name", llvm::Triple());
EXPECT_EQ("some name", ast.getDisplayName());
Loading