Skip to content

Commit 28d1490

Browse files
authored
[lldb][SymbolFileDWARF] Share GetDIEToType between SymbolFiles of a SymbolFileDWARFDebugMap (#120569)
The problem here manifests as follows: 1. We are stopped in main.o, so the first `ParseTypeFromDWARF` on `FooImpl<char>` gets called on `main.o`'s SymbolFile. This adds a mapping from *declaration die* -> `TypeSP` into `main.o`'s `GetDIEToType` map. 2. We then `CompleteType(FooImpl<char>)`. Depending on the order of entries in the debug-map, this might call `CompleteType` on `lib.o`'s SymbolFile. In which case, `GetDIEToType().lookup(decl_die)` will return a `nullptr`. This is already a bit iffy because some of the surrounding code assumes we don't call `CompleteTypeFromDWARF` with a `nullptr` `Type*`. E.g., `CompleteEnumType` blindly dereferences it (though enums will never encounter this because their definition is fetched in ParseEnum, unlike for structures). 3. While in `CompleteTypeFromDWARF`, we call `ParseTypeFromDWARF` again. This will parse the member function `FooImpl::Create` and its return type which is a typedef to `FooImpl*`. But now we're inside `lib.o`'s SymbolFile, so we call it on the definition DIE. In step (2) we just inserted a `nullptr` into `GetDIEToType` for the definition DIE, so we trivially return a `nullptr` from `ParseTypeFromDWARF`. Instead of reporting back this parse failure to the user LLDB trucks on and marks `FooImpl::Ref` to be `void*`. This test-case will trigger an assert in `TypeSystemClang::VerifyDecl` even if we just `frame var` (but only in debug-builds). In release builds where this function is a no-op, we'll create an incorrect Clang AST node for the `Ref` typedef. The proposed fix here is to share the `GetDIEToType` map between SymbolFiles if a debug-map exists. **Alternatives considered** * Check the `GetDIEToType` map of the `SymbolFile` that the declaration DIE belongs to. The assumption here being that if we called `ParseTypeFromDWARF` on a declaration, the `GetDIEToType` map that the result was inserted into was the one on that DIE's SymbolFile. This was the first version of this patch, but that felt like a weaker version sharing the map. It complicates the code in `CompleteType` and is less consistent with the other bookkeeping structures we already share between SymbolFiles * Return from `SymbolFileDWARF::CompleteType` if there is no type in the current `GetDIEToType`. Then `SymbolFileDWARFDebugMap::CompleteType` could continue to the next `SymbolFile` which does own the type. But that didn't quite work because we remove the `GetForwardCompilerTypeToDie` entry in `SymbolFile::CompleteType`, which `SymbolFileDWARFDebugMap::CompleteType` relies upon for iterating
1 parent 944b6f8 commit 28d1490

File tree

11 files changed

+91
-8
lines changed

11 files changed

+91
-8
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,8 +3633,7 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
36333633
static_cast<DWARFASTParserClang *>(
36343634
SymbolFileDWARF::GetDWARFParser(*dst_class_die.GetCU()));
36353635
auto link = [&](DWARFDIE src, DWARFDIE dst) {
3636-
SymbolFileDWARF::DIEToTypePtr &die_to_type =
3637-
dst_class_die.GetDWARF()->GetDIEToType();
3636+
auto &die_to_type = dst_class_die.GetDWARF()->GetDIEToType();
36383637
clang::DeclContext *dst_decl_ctx =
36393638
dst_dwarf_ast_parser->m_die_to_decl_ctx[dst.GetDIE()];
36403639
if (dst_decl_ctx)

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,13 @@ static ConstString GetDWARFMachOSegmentName() {
482482
return g_dwarf_section_name;
483483
}
484484

485+
llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &
486+
SymbolFileDWARF::GetDIEToType() {
487+
if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
488+
return debug_map_symfile->GetDIEToType();
489+
return m_die_to_type;
490+
}
491+
485492
llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> &
486493
SymbolFileDWARF::GetForwardDeclCompilerTypeToDIE() {
487494
if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
@@ -1594,6 +1601,8 @@ bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
15941601
if (!dwarf_ast)
15951602
return false;
15961603
Type *type = GetDIEToType().lookup(decl_die.GetDIE());
1604+
assert(type);
1605+
15971606
if (decl_die != def_die) {
15981607
GetDIEToType()[def_die.GetDIE()] = type;
15991608
DWARFASTParserClang *ast_parser =

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,7 @@ class SymbolFileDWARF : public SymbolFileCommon {
335335
m_file_index = file_index;
336336
}
337337

338-
typedef llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> DIEToTypePtr;
339-
340-
virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; }
338+
virtual llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &GetDIEToType();
341339

342340
virtual llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> &
343341
GetForwardDeclCompilerTypeToDIE();
@@ -529,7 +527,7 @@ class SymbolFileDWARF : public SymbolFileCommon {
529527
UniqueDWARFASTTypeMap m_unique_ast_type_map;
530528
// A map from DIE to lldb_private::Type. For record type, the key might be
531529
// either declaration DIE or definition DIE.
532-
DIEToTypePtr m_die_to_type;
530+
llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> m_die_to_type;
533531
DIEToVariableSP m_die_to_variable_sp;
534532
// A map from CompilerType to the struct/class/union/enum DIE (might be a
535533
// declaration or a definition) that is used to construct it.

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon {
291291
return m_unique_ast_type_map;
292292
}
293293

294+
llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &GetDIEToType() {
295+
return m_die_to_type;
296+
}
297+
294298
// OSOEntry
295299
class OSOEntry {
296300
public:
@@ -329,6 +333,8 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon {
329333
llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef>
330334
m_forward_decl_compiler_type_to_die;
331335
UniqueDWARFASTTypeMap m_unique_ast_type_map;
336+
llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> m_die_to_type;
337+
332338
DebugMap m_debug_map;
333339

334340
// When an object file from the debug map gets parsed in

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ bool SymbolFileDWARFDwo::ParseVendorDWARFOpcode(
102102
return GetBaseSymbolFile().ParseVendorDWARFOpcode(op, opcodes, offset, stack);
103103
}
104104

105-
SymbolFileDWARF::DIEToTypePtr &SymbolFileDWARFDwo::GetDIEToType() {
105+
llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &
106+
SymbolFileDWARFDwo::GetDIEToType() {
106107
return GetBaseSymbolFile().GetDIEToType();
107108
}
108109

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SymbolFileDWARFDwo : public SymbolFileDWARF {
7070
SymbolFileDWARF *GetDIERefSymbolFile(const DIERef &die_ref) override;
7171

7272
protected:
73-
DIEToTypePtr &GetDIEToType() override;
73+
llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &GetDIEToType() override;
7474

7575
DIEToVariableSP &GetDIEToVariable() override;
7676

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := lib.cpp main.cpp
2+
3+
include Makefile.rules
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class TestCaseTypedefToOuterFwd(TestBase):
8+
"""
9+
We find a global variable whose type is forward declared
10+
(whose definition is in either main.o or lib.o). We then
11+
try to get the 'Ref' typedef nested within that forward
12+
declared type. This test makes sure we correctly resolve
13+
this typedef.
14+
15+
We test this for two cases, where the definition lives
16+
in main.o or lib.o.
17+
"""
18+
19+
def check_global_var(self, target, name: str):
20+
var = target.FindFirstGlobalVariable(name)
21+
self.assertSuccess(var.GetError(), f"Found {name}")
22+
23+
var_type = var.GetType()
24+
self.assertTrue(var_type)
25+
26+
impl = var_type.GetPointeeType()
27+
self.assertTrue(impl)
28+
29+
ref = impl.FindDirectNestedType("Ref")
30+
self.assertTrue(ref)
31+
32+
self.assertEqual(ref.GetCanonicalType(), var_type)
33+
34+
def test(self):
35+
self.build()
36+
target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
37+
self.check_global_var(target, "gLibExternalDef")
38+
self.check_global_var(target, "gMainExternalDef")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "lib.h"
2+
3+
template <typename T> struct FooImpl {
4+
using Ref = FooImpl<T> *;
5+
6+
Ref Create() { return new FooImpl<T>(); }
7+
};
8+
9+
FooImpl<char> gLibLocalDef;
10+
BarImpl<char> *gLibExternalDef = nullptr;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef LIB_H_IN
2+
#define LIB_H_IN
3+
4+
template <typename T> struct FooImpl;
5+
template <typename T> struct BarImpl;
6+
7+
#endif // LIB_H_IN
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "lib.h"
2+
3+
template <typename T> struct BarImpl {
4+
using Ref = BarImpl<T> *;
5+
6+
Ref Create() { return new BarImpl<T>(); }
7+
};
8+
9+
BarImpl<char> gMainLocalDef;
10+
FooImpl<char> *gMainExternalDef = nullptr;
11+
12+
int main() { return 0; }

0 commit comments

Comments
 (0)