From 73951a11c64b4e748bbd1291d5021aef6aa400a5 Mon Sep 17 00:00:00 2001 From: Raphael Isemann Date: Mon, 23 Dec 2019 10:55:21 +0100 Subject: [PATCH] [lldb] Add sanity check to CreateDeclContext and fixed illformed CompilerContext in ClangExpressionDeclMap. 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 | 4 +++- lldb/source/Symbol/ClangASTContext.cpp | 2 ++ lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp index f8ec273..7a7d9b8 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp @@ -682,7 +682,9 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls( dyn_cast(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(context.m_decl_context)); + CompilerDeclContext compiler_decl_ctx = + m_clang_ast_context->CreateDeclContext( + const_cast(context.m_decl_context)); FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx, current_id); return; diff --git a/lldb/source/Symbol/ClangASTContext.cpp b/lldb/source/Symbol/ClangASTContext.cpp index f86e4bc..65d73d5 100644 --- a/lldb/source/Symbol/ClangASTContext.cpp +++ b/lldb/source/Symbol/ClangASTContext.cpp @@ -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); } diff --git a/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp b/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp index 1395b1f..bfc9f24 100644 --- a/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp +++ b/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp @@ -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])); -- 2.7.4