Skip to content

Bug in SmallPtrSetImplBase::insert_imp_big #136698

Open
@headshog

Description

@headshog

Hi! I have found bug in llvm::SmallPtrSetImpl::insert. I have a function:

void removeParallelEdges(llvm::CallGraph &CG) {
  for (auto &&CGNode : *CG) {
      CallGraphNode *Node = CGNode.second.get();

      bool FoundParallelEdge = true;
      while (FoundParallelEdge) {
        SmallSet<Function *, 16> Visited;
        FoundParallelEdge = false;
        for (auto CI = Node->begin(), CE = Node->end(); CI != CE; CI++) {
          llvm::errs() << "HERE " << CI->second->getFunction() << " " << Visited.size() << "\n";
          if (!Visited.insert(CI->second->getFunction()).second) {
            FoundParallelEdge = true;
            Node->removeCallEdge(CI);
            break;
          }
        }
      }
  }
}

Here segmentation fault occurs with the following stacktrace:

#0  0x00007ffff05dd33a in llvm::SmallPtrSetImplBase::insert_imp_big(void const*) () from /lib/x86_64-linux-gnu/libLLVM.so.21.0
#1  0x000055555586baa1 in llvm::SmallPtrSetImplBase::insert_imp (this=0x7fffe6ff3408, Ptr=0x7fffa001d2a8) at /usr/lib/llvm-18/include/llvm/ADT/SmallPtrSet.h:155
#2  llvm::SmallPtrSetImpl<llvm::Function*>::insert (this=0x7fffe6ff3408, Ptr=0x7fffa001d2a8) at /usr/lib/llvm-18/include/llvm/ADT/SmallPtrSet.h:367
#3  DbgCallGraphDOTInfo::removeParallelEdges (this=this@entry=0x7fffe6ff37a8, ...)
...

And with output:

HERE 0x7f06780913a8 0
HERE 0x7f06780914e8 1
HERE 0x7f06781d6d38 2
HERE 0x7f06781d6ed8 3
HERE 0x7f06781d6fa8 4
HERE 0x7f06781d7118 5
HERE 0x7f06781d7258 6
HERE 0x7f06781d73d8 7
HERE 0x7f06781d7558 8
HERE 0x7f06781d7768 9
HERE 0x7f06781d78e8 10
HERE 0x7f06781d7a58 11
HERE 0x7f06781d7bc8 12
HERE 0x7f06781d7d58 13
HERE 0x7f06781d7ec8 14
HERE 0x7f06781d8038 15
HERE 0x7f06781d81a8 16
[1]    666112 segmentation fault (core dumped)

I've also tried to change LLVM version to 18 and didn't get this problem, so this bug appeared in newer version.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions