Skip to content

Commit a5fb2e3

Browse files
committed
[lldb] Complete return types of CXXMethodDecls to prevent crashing due to covariant return types
Summary: Currently we crash in Clang's CodeGen when we call functions with covariant return types with this assert: ``` Assertion failed: (DD && "queried property of class with no definition"), function data, file clang/include/clang/AST/DeclCXX.h, line 433. ``` when calling `clang::CXXRecordDecl::isDerivedFrom` from the `ItaniumVTableBuilder`. Clang seems to assume that the underlying record decls of covariant return types are already completed. This is true during a normal Clang invocation as there the type checker will complete both decls when checking if the overloaded function is valid (i.e., the return types are covariant). When we minimally import our AST into the expression in LLDB we don't do this type checking (which would complete the record decls) and we end up trying to access the invalid record decls from CodeGen which makes us trigger the assert. This patch just completes the underlying types of ptr/ref return types of virtual function so that the underlying records are complete and we behave as Clang expects us to do. Fixes rdar://38048657 Reviewers: lhames, shafik Reviewed By: shafik Subscribers: abidh, JDevlieghere, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D73024
1 parent a497e1b commit a5fb2e3

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
import lldbsuite.test.lldbutil as lldbutil
4+
5+
class TestCase(TestBase):
6+
7+
mydir = TestBase.compute_mydir(__file__)
8+
NO_DEBUG_INFO_TESTCASE = True
9+
10+
def test(self):
11+
self.build()
12+
lldbutil.run_to_source_breakpoint(self,"// break here", lldb.SBFileSpec("main.cpp"))
13+
14+
# Test covariant return types for pointers to class that contains the called function.
15+
self.expect_expr("derived.getPtr()", result_type="Derived *")
16+
self.expect_expr("base_ptr_to_derived->getPtr()", result_type="Base *")
17+
self.expect_expr("base.getPtr()", result_type="Base *")
18+
# The same tests with reference types. LLDB drops the reference when it turns the
19+
# result into a SBValue so check for the the underlying type of the result.
20+
self.expect_expr("derived.getRef()", result_type="Derived")
21+
self.expect_expr("base_ptr_to_derived->getRef()", result_type="Base")
22+
self.expect_expr("base.getRef()", result_type="Base")
23+
24+
# Test covariant return types for pointers to class that does *not* contain the called function.
25+
self.expect_expr("derived.getOtherPtr()", result_type="OtherDerived *")
26+
self.expect_expr("base_ptr_to_derived->getOtherPtr()", result_type="OtherBase *")
27+
self.expect_expr("base.getOtherPtr()", result_type="OtherBase *")
28+
# The same tests with reference types. LLDB drops the reference when it turns the
29+
# result into a SBValue so check for the the underlying type of the result.
30+
self.expect_expr("derived.getOtherRef()", result_type="OtherDerived")
31+
self.expect_expr("base_ptr_to_derived->getOtherRef()", result_type="OtherBase")
32+
self.expect_expr("base.getOtherRef()", result_type="OtherBase")
33+
34+
# Test that we call the right function and get the right value back.
35+
self.expect_expr("derived.getOtherPtr()->value()", result_summary='"derived"')
36+
self.expect_expr("base_ptr_to_derived->getOtherPtr()->value()", result_summary='"derived"')
37+
self.expect_expr("base.getOtherPtr()->value()", result_summary='"base"')
38+
self.expect_expr("derived.getOtherRef().value()", result_summary='"derived"')
39+
self.expect_expr("base_ptr_to_derived->getOtherRef().value()", result_summary='"derived"')
40+
self.expect_expr("base.getOtherRef().value()", result_summary='"base"')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
struct OtherBase {
2+
// Allow checking actual type from the test by giving
3+
// this class and the subclass unique values here.
4+
virtual const char *value() { return "base"; }
5+
};
6+
struct OtherDerived : public OtherBase {
7+
const char *value() override { return "derived"; }
8+
};
9+
10+
// Those have to be globals as they would be completed if they
11+
// are members (which would make this test always pass).
12+
OtherBase other_base;
13+
OtherDerived other_derived;
14+
15+
struct Base {
16+
// Function with covariant return type that is same class.
17+
virtual Base* getPtr() { return this; }
18+
virtual Base& getRef() { return *this; }
19+
// Function with covariant return type that is a different class.
20+
virtual OtherBase* getOtherPtr() { return &other_base; }
21+
virtual OtherBase& getOtherRef() { return other_base; }
22+
};
23+
24+
struct Derived : public Base {
25+
Derived* getPtr() override { return this; }
26+
Derived& getRef() override { return *this; }
27+
OtherDerived* getOtherPtr() override { return &other_derived; }
28+
OtherDerived& getOtherRef() override { return other_derived; }
29+
};
30+
31+
int main() {
32+
Derived derived;
33+
Base base;
34+
Base *base_ptr_to_derived = &derived;
35+
(void)base_ptr_to_derived->getPtr();
36+
(void)base_ptr_to_derived->getRef();
37+
(void)base_ptr_to_derived->getOtherPtr();
38+
(void)base_ptr_to_derived->getOtherRef();
39+
return 0; // break here
40+
}

Diff for: lldb/source/Symbol/ClangASTImporter.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,25 @@ void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
976976
}
977977
}
978978

979+
/// Takes a CXXMethodDecl and completes the return type if necessary. This
980+
/// is currently only necessary for virtual functions with covariant return
981+
/// types where Clang's CodeGen expects that the underlying records are already
982+
/// completed.
983+
static void MaybeCompleteReturnType(ClangASTImporter &importer,
984+
CXXMethodDecl *to_method) {
985+
if (!to_method->isVirtual())
986+
return;
987+
QualType return_type = to_method->getReturnType();
988+
if (!return_type->isPointerType() && !return_type->isReferenceType())
989+
return;
990+
991+
clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl();
992+
if (!rd)
993+
return;
994+
995+
importer.CompleteTagDecl(rd);
996+
}
997+
979998
void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
980999
clang::Decl *to) {
9811000
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
@@ -1121,6 +1140,9 @@ void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
11211140
}
11221141
}
11231142
}
1143+
1144+
if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to))
1145+
MaybeCompleteReturnType(m_master, to_method);
11241146
}
11251147

11261148
clang::Decl *

0 commit comments

Comments
 (0)