llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

<details>
<summary>Changes</summary>

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() &amp;&amp; "Cannot retrieve a NULL type pointer"), 
function getCommonPtr, file Type.h, line 958.
Process 2680 stopped
* thread #<!-- -->15, name = '&lt;lldb.process.internal-state(pid=2712)&gt;', 
stop reason = hit program assert
    frame #<!-- -->4: 0x000000010cdf3cdc 
liblldb.20.0.0git.dylib`DWARFASTParserClang::ExtractIntFromFormValue(lldb_private::CompilerType
 const&amp;, lldb_private::plugin::dwarf::DWARFFormValue const&amp;) const 
(.cold.1) + 
liblldb.20.0.0git.dylib`DWARFASTParserClang::ParseObjCMethod(lldb_private::ObjCLanguage::MethodName
 const&amp;, lldb_private::plugin::dwarf::DWARFDIE const&amp;, 
lldb_private::CompilerType, ParsedDWARFTypeAttributes
, bool) (.cold.1):
-&gt;  0x10cdf3cdc &lt;+0&gt;:  stp    x29, x30, [sp, #-0x10]!
    0x10cdf3ce0 &lt;+4&gt;:  mov    x29, sp
    0x10cdf3ce4 &lt;+8&gt;:  adrp   x0, 545
    0x10cdf3ce8 &lt;+12&gt;: add    x0, x0, #<!-- -->0xa25 ; "ParseObjCMethod"
Target 0: (lldb) stopped.
(lldb) bt
* thread #<!-- -->15, name = '&lt;lldb.process.internal-state(pid=2712)&gt;', 
stop reason = hit program assert
    frame #<!-- -->0: 0x0000000180d08600 libsystem_kernel.dylib`__pthread_kill 
+ 8
    frame #<!-- -->1: 0x0000000180d40f50 libsystem_pthread.dylib`pthread_kill + 
288
    frame #<!-- -->2: 0x0000000180c4d908 libsystem_c.dylib`abort + 128
    frame #<!-- -->3: 0x0000000180c4cc1c libsystem_c.dylib`__assert_rtn + 284
  * frame #<!-- -->4: 0x000000010cdf3cdc 
liblldb.20.0.0git.dylib`DWARFASTParserClang::ExtractIntFromFormValue(lldb_private::CompilerType
 const&amp;, lldb_private::plugin::dwarf::DWARFFormValue const&amp;) const 
(.cold.1) + 
    frame #<!-- -->5: 0x0000000109d30acc 
liblldb.20.0.0git.dylib`lldb_private::TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding,
 unsigned long) + 1188
    frame #<!-- -->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

---
Full diff: https://github.com/llvm/llvm-project/pull/110481.diff


1 Files Affected:

- (modified) lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp (+24-4) 


``````````diff
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp 
b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index b0f49ebf2d2cbb..2f423269fecd2d 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -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,19 @@ 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 +759,11 @@ CompilerType
 TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
                                                      size_t bit_size) {
   ASTContext &ast = getASTContext();
+  if (!ast.VoidPtrTy) {
+    LLDB_LOG(GetLog(LLDBLog::Expressions), "{0} failed: builtin types on 
ASTContext were not initialized properly.", __func__);
+    return {};
+  }
+
   switch (encoding) {
   case eEncodingInvalid:
     if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
@@ -891,6 +906,11 @@ CompilerType 
TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize(
     llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size) {
   ASTContext &ast = getASTContext();
 
+  if (!ast.VoidPtrTy) {
+    LLDB_LOG(GetLog(LLDBLog::Expressions), "{0} failed: builtin types on 
ASTContext were not initialized properly.", __func__);
+    return {};
+  }
+
   switch (dw_ate) {
   default:
     break;

``````````

</details>


https://github.com/llvm/llvm-project/pull/110481
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to