Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

LookupVisibleHelper::lookupInDeclContext crashes when lookups Map got modified #50319

Open Quuxplusone opened 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR51352
Status NEW
Importance P enhancement
Reported by manjian (manjian2006@gmail.com)
Reported on 2021-08-04 23:40:37 -0700
Last modified on 2021-08-04 23:52:03 -0700
Version 12.0
Hardware PC Linux
CC blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
tag: llvmorg-12.0.1

map modified bt:
Thread 1 "lldb" hit Hardware watchpoint 6: *$18

Old value = 330
New value = 332
llvm::SmallDenseMap<clang::DeclarationName, clang::StoredDeclsList, 4u,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::setNumEntries (this=0x2893480, Num=166) at /home/linzj/ssdsrc/llvm-
project2/llvm/include/llvm/ADT/DenseMap.h:1121
1121      }
(gdb) bt
#0  llvm::SmallDenseMap<clang::DeclarationName, clang::StoredDeclsList, 4u,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::setNumEntries (this=0x2893480, Num=166) at /home/linzj/ssdsrc/llvm-
project2/llvm/include/llvm/ADT/DenseMap.h:1121
#1  0x00007f28300b2a3e in
llvm::DenseMapBase<llvm::SmallDenseMap<clang::DeclarationName,
clang::StoredDeclsList, 4u, llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList> >,
clang::DeclarationName, clang::StoredDeclsList,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::setNumEntries (this=0x2893480, Num=166) at /home/linzj/ssdsrc/llvm-
project2/llvm/include/llvm/ADT/DenseMap.h:489
#2  0x00007f28300b4b4a in
llvm::DenseMapBase<llvm::SmallDenseMap<clang::DeclarationName,
clang::StoredDeclsList, 4u, llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList> >,
clang::DeclarationName, clang::StoredDeclsList,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::incrementNumEntries (this=0x2893480) at /home/linzj/ssdsrc/llvm-
project2/llvm/include/llvm/ADT/DenseMap.h:493
#3  0x00007f28300b4a77 in
llvm::DenseMapBase<llvm::SmallDenseMap<clang::DeclarationName,
clang::StoredDeclsList, 4u, llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList> >,
clang::DeclarationName, clang::StoredDeclsList,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::InsertIntoBucketImpl<clang::DeclarationName> (this=0x2893480, Key=...,
Lookup=..., TheBucket=0xde0a0e0)
    at /home/linzj/ssdsrc/llvm-project2/llvm/include/llvm/ADT/DenseMap.h:593
#4  0x00007f28300b54c9 in
llvm::DenseMapBase<llvm::SmallDenseMap<clang::DeclarationName,
clang::StoredDeclsList, 4u, llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList> >,
clang::DeclarationName, clang::StoredDeclsList,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::InsertIntoBucket<clang::DeclarationName> (this=0x2893480,
TheBucket=0xde0a0e0, Key=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/include/llvm/ADT/DenseMap.h:547
#5  0x00007f28300b5483 in
llvm::DenseMapBase<llvm::SmallDenseMap<clang::DeclarationName,
clang::StoredDeclsList, 4u, llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList> >,
clang::DeclarationName, clang::StoredDeclsList,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::FindAndConstruct (this=0x2893480, Key=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/include/llvm/ADT/DenseMap.h:338
#6  0x00007f28300b1480 in
llvm::DenseMapBase<llvm::SmallDenseMap<clang::DeclarationName,
clang::StoredDeclsList, 4u, llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList> >,
clang::DeclarationName, clang::StoredDeclsList,
llvm::DenseMapInfo<clang::DeclarationName>,
llvm::detail::DenseMapPair<clang::DeclarationName, clang::StoredDeclsList>
>::operator[] (this=0x2893480, Key=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/include/llvm/ADT/DenseMap.h:342
#7  0x00007f28300aea45 in clang::DeclContext::makeDeclVisibleInContextImpl
(this=0x9657d30, D=0xf91eec8, Internal=true)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/AST/DeclBase.cpp:1930
#8  0x00007f28300ae346 in clang::DeclContext::makeDeclVisibleInContextWithFlags
(this=0x9657d30, D=0xf91eec8, Internal=true, Recoverable=true)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/AST/DeclBase.cpp:1893
#9  0x00007f28300ae49c in clang::DeclContext::addDeclInternal (this=0x9657d30,
D=0xf91eec8) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/DeclBase.cpp:1589
#10 0x00007f282fef9fba in clang::ASTNodeImporter::addDeclToContexts
(this=0x7ffe093a4250, FromD=0x8987400, ToD=0xf91eec8)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:290
#11 0x00007f282fec33b9 in clang::ASTNodeImporter::VisitRecordDecl
(this=0x7ffe093a4250, D=0x8987400) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:2875
#12 0x00007f282ff3b8ce in clang::declvisitor::Base<std::add_pointer,
clang::ASTNodeImporter, llvm::Expected<clang::Decl*> >::VisitCXXRecordDecl
(this=0x7ffe093a4250, D=0x8987400)
    at tools/clang/include/clang/AST/DeclNodes.inc:263
#13 0x00007f282ff09e7d in clang::declvisitor::Base<std::add_pointer,
clang::ASTNodeImporter, llvm::Expected<clang::Decl*> >::Visit
(this=0x7ffe093a4250, D=0x8987400)
    at tools/clang/include/clang/AST/DeclNodes.inc:263
#14 0x00007f282feeb386 in clang::ASTImporter::ImportImpl (this=0xcdd71c0,
FromD=0x8987400) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:8131
#15 0x00007f282c4e2e07 in
lldb_private::ClangASTImporter::ASTImporterDelegate::ImportImpl
(this=0xcdd71c0, From=0x8987400)
    at /home/linzj/ssdsrc/llvm-project2/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp:892
#16 0x00007f282feec492 in clang::ASTImporter::Import (this=0xcdd71c0,
FromD=0x8987400) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:8293
#17 0x00007f282fef650c in clang::ASTNodeImporter::import<clang::RecordDecl>
(this=0x7ffe093a4b00, From=0x8987400) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:164
#18 0x00007f282feb87b6 in clang::ASTNodeImporter::VisitRecordType
(this=0x7ffe093a4b00, T=0x89874b0) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:1397
#19 0x00007f282ff0ac59 in clang::TypeVisitor<clang::ASTNodeImporter,
llvm::Expected<clang::QualType> >::Visit (this=0x7ffe093a4b00, T=0x89874b0) at
tools/clang/include/clang/AST/TypeNodes.inc:69
#20 0x00007f282feeb71e in clang::ASTImporter::Import (this=0xcdd71c0,
FromT=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:8164
#21 0x00007f282fef4209 in clang::ASTNodeImporter::import<clang::QualType>
(this=0x7ffe093a4d20, From=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:178
#22 0x00007f282feb662b in clang::ASTNodeImporter::VisitPointerType
(this=0x7ffe093a4d20, T=0x8987540) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:1096
#23 0x00007f282ff0abb1 in clang::TypeVisitor<clang::ASTNodeImporter,
llvm::Expected<clang::QualType> >::Visit (this=0x7ffe093a4d20, T=0x8987540) at
tools/clang/include/clang/AST/TypeNodes.inc:61
#24 0x00007f282feeb71e in clang::ASTImporter::Import (this=0xcdd71c0,
FromT=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:8164
#25 0x00007f282fef4209 in clang::ASTNodeImporter::import<clang::QualType>
(this=0x7ffe093a5130, From=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:178
--Type <RET> for more, q to quit, c to continue without paging--
#26 0x00007f282feb74fa in clang::ASTNodeImporter::VisitFunctionProtoType
(this=0x7ffe093a5130, T=0x8987570) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:1233
#27 0x00007f282ff0aa45 in clang::TypeVisitor<clang::ASTNodeImporter,
llvm::Expected<clang::QualType> >::Visit (this=0x7ffe093a5130, T=0x8987570) at
tools/clang/include/clang/AST/TypeNodes.inc:47
#28 0x00007f282feeb71e in clang::ASTImporter::Import (this=0xcdd71c0,
FromT=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:8164
#29 0x00007f282fef4209 in clang::ASTNodeImporter::import<clang::QualType>
(this=0x7ffe093a5c20, From=...) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:178
#30 0x00007f282fef584b in
clang::ASTNodeImporter::importChecked<clang::QualType> (this=0x7ffe093a5c20,
Err=..., From=...)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:197
#31 0x00007f282fec624f in clang::ASTNodeImporter::VisitFunctionDecl
(this=0x7ffe093a5c20, D=0x89875a0) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:3353
#32 0x00007f282fec7e0e in clang::ASTNodeImporter::VisitCXXMethodDecl
(this=0x7ffe093a5c20, D=0x89875a0) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:3546
#33 0x00007f282ff0a091 in clang::declvisitor::Base<std::add_pointer,
clang::ASTNodeImporter, llvm::Expected<clang::Decl*> >::Visit
(this=0x7ffe093a5c20, D=0x89875a0)
    at tools/clang/include/clang/AST/DeclNodes.inc:411
#34 0x00007f282feeb386 in clang::ASTImporter::ImportImpl (this=0xcdd71c0,
FromD=0x89875a0) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:8131
#35 0x00007f282c4e2e07 in
lldb_private::ClangASTImporter::ASTImporterDelegate::ImportImpl
(this=0xcdd71c0, From=0x89875a0)
    at /home/linzj/ssdsrc/llvm-project2/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp:892
#36 0x00007f282feec492 in clang::ASTImporter::Import (this=0xcdd71c0,
FromD=0x89875a0) at /home/linzj/ssdsrc/llvm-
project2/llvm/tools/clang/lib/AST/ASTImporter.cpp:8293
#37 0x00007f282c4df556 in lldb_private::ClangASTImporter::CopyDecl
(this=0x567d980, dst_ast=0x2214970, decl=0x89875a0)
    at /home/linzj/ssdsrc/llvm-project2/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp:78
#38 0x00007f282c50477e in lldb_private::ClangASTSource::CopyDecl
(this=0x9dce630, src_decl=0x89875a0)
    at /home/linzj/ssdsrc/llvm-project2/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp:1729
#39 0x00007f282c504659 in
lldb_private::ClangASTSource::FindExternalLexicalDecls(clang::DeclContext
const*, llvm::function_ref<bool (clang::Decl::Kind)>,
llvm::SmallVectorImpl<clang::Decl*>&) (
    this=0x9dce630, decl_context=0xae21e60, predicate=..., decls=...) at /home/linzj/ssdsrc/llvm-project2/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp:482
#40 0x00007f282a020c55 in
lldb_private::ClangASTSource::ClangASTSourceProxy::FindExternalLexicalDecls(clang::DeclContext
const*, llvm::function_ref<bool (clang::Decl::Kind)>,
llvm::SmallVectorImpl<clang::Decl*>&) (this=0x1f3a430, DC=0xae21e60,
IsKindWeWant=..., Decls=...) at /home/linzj/ssdsrc/llvm-
project2/lldb/source/./Plugins/ExpressionParser/Clang/ClangASTSource.h:223
#41 0x00007f28300b0471 in clang::ExternalASTSource::FindExternalLexicalDecls
(this=0x1f3a430, DC=0xae21e60, Result=...)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/include/clang/AST/ExternalASTSource.h:187
#42 0x00007f28300ad157 in
clang::DeclContext::LoadLexicalDeclsFromExternalStorage (this=0xae21e60) at
/home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/AST/DeclBase.cpp:1347
#43 0x00007f28300ae5f4 in clang::DeclContext::buildLookup (this=0xae21e60) at
/home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/AST/DeclBase.cpp:1613
#44 0x00007f28300aef03 in clang::DeclContext::lookup (this=0xae21e60, Name=...)
at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/AST/DeclBase.cpp:1716
#45 0x00007f282ec57fd1 in getConstructors (Context=..., Record=0xae21e20) at
/home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/Sema/SemaCodeComplete.cpp:1018
#46 0x00007f282ec5720c in (anonymous
namespace)::ResultBuilder::MaybeAddConstructorResults (this=0x7ffe093a8260,
R=...)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/Sema/SemaCodeComplete.cpp:1043
#47 0x00007f282ec38c9c in (anonymous namespace)::ResultBuilder::AddResult
(this=0x7ffe093a8260, R=..., CurContext=0xde9b270, Hiding=0x0,
InBaseClass=false)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/Sema/SemaCodeComplete.cpp:1350
#48 0x00007f282ec52322 in (anonymous
namespace)::CodeCompletionDeclConsumer::FoundDecl (this=0x7ffe093a8110,
ND=0xae21e20, Hiding=0x0, Ctx=0x9657d30, InBaseClass=false)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/Sema/SemaCodeComplete.cpp:1618
#49 0x00007f282f44e387 in (anonymous
namespace)::LookupVisibleHelper::lookupInDeclContext (this=0x7ffe093a7ed8,
Ctx=0x9657d30, Result=..., QualifiedNameLookup=false, InBaseClass=false)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/Sema/SemaLookup.cpp:3805
#50 0x00007f282f44d7a9 in (anonymous
namespace)::LookupVisibleHelper::lookupInScope (this=0x7ffe093a7ed8,
S=0x28bb2c0, Result=..., UDirs=...)
    at /home/linzj/ssdsrc/llvm-project2/llvm/tools/clang/lib/Sema/SemaLookup.cpp:3987

After the map got modified, the iterator of this map will not by sync.
Quuxplusone commented 3 years ago
The following patch fixes this.

diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 29038ab9fe1c..786380bed046 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -3797,9 +3797,10 @@ private:
     bool Load = LoadExternal ||
                 !(isa<TranslationUnitDecl>(Ctx) || isa<NamespaceDecl>(Ctx));
     // Enumerate all of the results in this context.
-    for (DeclContextLookupResult R :
-         Load ? Ctx->lookups()
-              : Ctx->noload_lookups(/*PreserveInternalState=*/false)) {
+    SmallVector<DeclContextLookupResult, 8> Lookups(
+        Load ? Ctx->lookups()
+             : Ctx->noload_lookups(/*PreserveInternalState=*/false));
+    for (DeclContextLookupResult &R : Lookups) {
       for (auto *D : R) {
         if (auto *ND = Result.getAcceptableDecl(D)) {
           Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass);