[lldb] Add sanity check to CreateDeclContext and fixed illformed CompilerContext...
authorRaphael Isemann <teemperor@gmail.com>
Mon, 23 Dec 2019 09:55:21 +0000 (10:55 +0100)
committerRaphael Isemann <teemperor@gmail.com>
Mon, 23 Dec 2019 10:48:02 +0000 (11:48 +0100)
This adds a check that the ClangASTContext actually fits to the
DeclContext that we want to create a CompilerDeclContext for. If
the ClangASTContext (and its associated ASTContext) does not fit
to the DeclContext (that is, the DeclContext wasn't created by the
ASTContext), all computations using this malformed CompilerDeclContext
will yield unpredictable results.

Also fixes the only place that actually hits this assert which is the
construction of a CompilerDeclContext in ClangExpressionDeclMap
where we pass an unrelated ASTContext instead of the ASTContext
of the current expression.

I had to revert my previous change to DWARFASTParserClangTests.cpp
back to using the unsafe direct construction of CompilerDeclContext
as this assert won't work if the DeclContext we pass isn't a valid
DeclContext in the first place.

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
lldb/source/Symbol/ClangASTContext.cpp
lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp

index f8ec273..7a7d9b8 100644 (file)
@@ -682,7 +682,9 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
           dyn_cast<NamespaceDecl>(context.m_decl_context)) {
     if (namespace_context->getName().str() ==
         std::string(g_lldb_local_vars_namespace_cstr)) {
-      CompilerDeclContext compiler_decl_ctx = GetClangASTContext()->CreateDeclContext(const_cast<clang::DeclContext*>(context.m_decl_context));
+      CompilerDeclContext compiler_decl_ctx =
+          m_clang_ast_context->CreateDeclContext(
+              const_cast<clang::DeclContext *>(context.m_decl_context));
       FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx,
                                current_id);
       return;
index f86e4bc..65d73d5 100644 (file)
@@ -1201,6 +1201,8 @@ CompilerType ClangASTContext::GetTypeForDecl(void *opaque_decl) {
 }
 
 CompilerDeclContext ClangASTContext::CreateDeclContext(DeclContext *ctx) {
+  // Check that the DeclContext actually belongs to this ASTContext.
+  assert(&ctx->getParentASTContext() == &getASTContext());
   return CompilerDeclContext(this, ctx);
 }
 
index 1395b1f..bfc9f24 100644 (file)
@@ -52,7 +52,7 @@ TEST_F(DWARFASTParserClangTests,
   for (int i = 0; i < 4; ++i)
     ast_parser.LinkDeclContextToDIE(decl_ctxs[i], dies[i]);
   ast_parser.EnsureAllDIEsInDeclContextHaveBeenParsed(
-      ast_ctx.CreateDeclContext(decl_ctxs[1]));
+      CompilerDeclContext(nullptr, decl_ctxs[1]));
 
   EXPECT_THAT(ast_parser.GetDeclContextToDIEMapKeys(),
               testing::UnorderedElementsAre(decl_ctxs[0], decl_ctxs[3]));