Skip to content

Commit a5f3a2a

Browse files
authored
[lldb][TypeSystemClang] Add warning and defensive checks when ASTContext is not fully initialized (llvm#110481)
As this comment around target initialization implies: ``` // This can be NULL if we don't know anything about the architecture or if // the target for an architecture isn't enabled in the llvm/clang that we // built ``` There are cases where we might fail to call `InitBuiltinTypes` when creating the backing `ASTContext` for a `TypeSystemClang`. If that happens, the builtins `QualType`s, e.g., `VoidPtrTy`/`IntTy`/etc., are not initialized and dereferencing them as we do in `GetBuiltinTypeForEncodingAndBitSize` (and other places) will lead to nullptr-dereferences. Example backtrace: ``` (lldb) run Assertion failed: (!isNull() && "Cannot retrieve a NULL type pointer"), function getCommonPtr, file Type.h, line 958. Process 2680 stopped * thread rust-lang#15, name = '<lldb.process.internal-state(pid=2712)>', stop reason = hit program assert frame rust-lang#4: 0x000000010cdf3cdc liblldb.20.0.0git.dylib`DWARFASTParserClang::ExtractIntFromFormValue(lldb_private::CompilerType const&, lldb_private::plugin::dwarf::DWARFFormValue const&) const (.cold.1) + liblldb.20.0.0git.dylib`DWARFASTParserClang::ParseObjCMethod(lldb_private::ObjCLanguage::MethodName const&, lldb_private::plugin::dwarf::DWARFDIE const&, lldb_private::CompilerType, ParsedDWARFTypeAttributes , bool) (.cold.1): -> 0x10cdf3cdc <+0>: stp x29, x30, [sp, #-0x10]! 0x10cdf3ce0 <+4>: mov x29, sp 0x10cdf3ce4 <+8>: adrp x0, 545 0x10cdf3ce8 <+12>: add x0, x0, #0xa25 ; "ParseObjCMethod" Target 0: (lldb) stopped. (lldb) bt * thread rust-lang#15, name = '<lldb.process.internal-state(pid=2712)>', stop reason = hit program assert frame #0: 0x0000000180d08600 libsystem_kernel.dylib`__pthread_kill + 8 frame #1: 0x0000000180d40f50 libsystem_pthread.dylib`pthread_kill + 288 frame rust-lang#2: 0x0000000180c4d908 libsystem_c.dylib`abort + 128 frame rust-lang#3: 0x0000000180c4cc1c libsystem_c.dylib`__assert_rtn + 284 * frame rust-lang#4: 0x000000010cdf3cdc liblldb.20.0.0git.dylib`DWARFASTParserClang::ExtractIntFromFormValue(lldb_private::CompilerType const&, lldb_private::plugin::dwarf::DWARFFormValue const&) const (.cold.1) + frame rust-lang#5: 0x0000000109d30acc liblldb.20.0.0git.dylib`lldb_private::TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding, unsigned long) + 1188 frame rust-lang#6: 0x0000000109aaaed4 liblldb.20.0.0git.dylib`DynamicLoaderMacOS::NotifyBreakpointHit(void*, lldb_private::StoppointCallbackContext*, unsigned long long, unsigned long long) + 384 ``` This patch adds a one-time user-visible warning for when we fail to initialize the AST to indicate that initialization went wrong for the given target. Additionally, we add checks for whether one of the `ASTContext` `QualType`s is invalid before dereferencing any builtin types. The warning would look as follows: ``` (lldb) target create "a.out" Current executable set to 'a.out' (arm64). (lldb) b main warning: Failed to initialize builtin ASTContext types for target 'some-unknown-triple'. Printing variables may behave unexpectedly. Breakpoint 1: where = a.out`main + 8 at stepping.cpp:5:14, address = 0x0000000100003f90 ``` rdar://134869779
1 parent 47861fa commit a5f3a2a

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

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

+35-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
5555
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
5656
#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
57+
#include "lldb/Core/Debugger.h"
5758
#include "lldb/Core/DumpDataExtractor.h"
5859
#include "lldb/Core/Module.h"
5960
#include "lldb/Core/PluginManager.h"
@@ -697,10 +698,20 @@ void TypeSystemClang::CreateASTContext() {
697698
TargetInfo *target_info = getTargetInfo();
698699
if (target_info)
699700
m_ast_up->InitBuiltinTypes(*target_info);
700-
else if (auto *log = GetLog(LLDBLog::Expressions))
701-
LLDB_LOG(log,
702-
"Failed to initialize builtin ASTContext types for target '{0}'",
703-
m_target_triple);
701+
else {
702+
std::string err =
703+
llvm::formatv(
704+
"Failed to initialize builtin ASTContext types for target '{0}'. "
705+
"Printing variables may behave unexpectedly.",
706+
m_target_triple)
707+
.str();
708+
709+
LLDB_LOG(GetLog(LLDBLog::Expressions), err.c_str());
710+
711+
static std::once_flag s_uninitialized_target_warning;
712+
Debugger::ReportWarning(std::move(err), /*debugger_id=*/std::nullopt,
713+
&s_uninitialized_target_warning);
714+
}
704715

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

@@ -749,6 +760,10 @@ CompilerType
749760
TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
750761
size_t bit_size) {
751762
ASTContext &ast = getASTContext();
763+
764+
if (!ast.VoidPtrTy)
765+
return {};
766+
752767
switch (encoding) {
753768
case eEncodingInvalid:
754769
if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
@@ -891,6 +906,9 @@ CompilerType TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize(
891906
llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size) {
892907
ASTContext &ast = getASTContext();
893908

909+
if (!ast.VoidPtrTy)
910+
return {};
911+
894912
switch (dw_ate) {
895913
default:
896914
break;
@@ -2335,6 +2353,9 @@ CompilerType TypeSystemClang::GetIntTypeFromBitSize(size_t bit_size,
23352353
bool is_signed) {
23362354
clang::ASTContext &ast = getASTContext();
23372355

2356+
if (!ast.VoidPtrTy)
2357+
return {};
2358+
23382359
if (is_signed) {
23392360
if (bit_size == ast.getTypeSize(ast.SignedCharTy))
23402361
return GetType(ast.SignedCharTy);
@@ -2376,6 +2397,9 @@ CompilerType TypeSystemClang::GetIntTypeFromBitSize(size_t bit_size,
23762397
}
23772398

23782399
CompilerType TypeSystemClang::GetPointerSizedIntType(bool is_signed) {
2400+
if (!getASTContext().VoidPtrTy)
2401+
return {};
2402+
23792403
return GetIntTypeFromBitSize(
23802404
getASTContext().getTypeSize(getASTContext().VoidPtrTy), is_signed);
23812405
}
@@ -7453,6 +7477,13 @@ clang::FieldDecl *TypeSystemClang::AddFieldToRecordType(
74537477

74547478
clang::Expr *bit_width = nullptr;
74557479
if (bitfield_bit_size != 0) {
7480+
if (clang_ast.IntTy.isNull()) {
7481+
LLDB_LOG(
7482+
GetLog(LLDBLog::Expressions),
7483+
"{0} failed: builtin ASTContext types have not been initialized");
7484+
return nullptr;
7485+
}
7486+
74567487
llvm::APInt bitfield_bit_size_apint(clang_ast.getTypeSize(clang_ast.IntTy),
74577488
bitfield_bit_size);
74587489
bit_width = new (clang_ast)

lldb/unittests/Symbol/TestTypeSystemClang.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lldb/Core/Declaration.h"
1414
#include "lldb/Host/FileSystem.h"
1515
#include "lldb/Host/HostInfo.h"
16+
#include "lldb/lldb-enumerations.h"
1617
#include "clang/AST/DeclCXX.h"
1718
#include "clang/AST/DeclObjC.h"
1819
#include "clang/AST/ExprCXX.h"
@@ -231,6 +232,37 @@ TEST_F(TestTypeSystemClang, TestBuiltinTypeForEncodingAndBitSize) {
231232
VerifyEncodingAndBitSize(*m_ast, eEncodingIEEE754, 64);
232233
}
233234

235+
TEST_F(TestTypeSystemClang, TestBuiltinTypeForEmptyTriple) {
236+
// Test that we can access type-info of builtin Clang AST
237+
// types without crashing even when the target triple is
238+
// empty.
239+
240+
TypeSystemClang ast("empty triple AST", llvm::Triple{});
241+
242+
// This test only makes sense if the builtin ASTContext types were
243+
// not initialized.
244+
ASSERT_TRUE(ast.getASTContext().VoidPtrTy.isNull());
245+
246+
EXPECT_FALSE(ast.GetBuiltinTypeByName(ConstString("int")).IsValid());
247+
EXPECT_FALSE(ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
248+
"char", dwarf::DW_ATE_signed_char, 8)
249+
.IsValid());
250+
EXPECT_FALSE(ast.GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingUint, 8)
251+
.IsValid());
252+
EXPECT_FALSE(ast.GetPointerSizedIntType(/*is_signed=*/false));
253+
EXPECT_FALSE(ast.GetIntTypeFromBitSize(8, /*is_signed=*/false));
254+
255+
CompilerType record_type = ast.CreateRecordType(
256+
nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "Record",
257+
llvm::to_underlying(clang::TagTypeKind::Struct),
258+
lldb::eLanguageTypeC_plus_plus, std::nullopt);
259+
TypeSystemClang::StartTagDeclarationDefinition(record_type);
260+
EXPECT_EQ(ast.AddFieldToRecordType(record_type, "field", record_type,
261+
eAccessPublic, /*bitfield_bit_size=*/8),
262+
nullptr);
263+
TypeSystemClang::CompleteTagDeclarationDefinition(record_type);
264+
}
265+
234266
TEST_F(TestTypeSystemClang, TestDisplayName) {
235267
TypeSystemClang ast("some name", llvm::Triple());
236268
EXPECT_EQ("some name", ast.getDisplayName());

0 commit comments

Comments
 (0)