llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.8k stars 11.45k forks source link

clang-repl: SIGSEGV in libclang #72980

Open p4vook opened 9 months ago

p4vook commented 9 months ago

Minimal example

echo "{ int x; a::b(x); }" | clang-repl

Expected result

Error is shown, but the evaluation continues

Actual result

Error is shown, clang-repl gets terminated by SIGSEGV.

Stack trace

LLVM 17.0.4

  * frame #0: 0x00007ffff5188a90 libclang-cpp.so.17`clang::Decl::getTranslationUnitDecl()
    frame #1: 0x00007ffff7b72f60 libclang-cpp.so.17`clang::IncrementalParser::CleanUpPTU(clang::PartialTranslationUnit&) + 192
    frame #2: 0x00007ffff7b74308 libclang-cpp.so.17`clang::IncrementalParser::ParseOrWrapTopLevelDecl() + 2264
    frame #3: 0x00007ffff7b74f4d libclang-cpp.so.17`clang::IncrementalParser::Parse(llvm::StringRef) + 1165
    frame #4: 0x00007ffff7b782b8 libclang-cpp.so.17`clang::Interpreter::ParseAndExecute(llvm::StringRef, clang::Value*) + 184
    frame #5: 0x00005555555614be clang-repl`main + 3534
    frame #6: 0x00007fffeaa5078a libc.so.6`___lldb_unnamed_symbol3154 + 122
    frame #7: 0x00007fffeaa50845 libc.so.6`__libc_start_main + 133
    frame #8: 0x000055555555c3b1 clang-repl`_start + 33

Also tested with current master (commit 0d77978e), same result.

UPD: worth mentioning that clang++ itself processes this file correctly, without segfaults.

p4vook commented 9 months ago

I was able to get debug info: clang-repl: /var/tmp/portage/sys-devel/clang-18.0.0_pre20231119/work/clang/include/clang/AST/DeclBase.h:1336: clang::NamedDecl* clang::DeclListNode::iterator::operator*() const: Assertion Ptr && "dereferencing end() iterator"' failed.

p4vook commented 9 months ago

@EugeneZelenko are you sure clang-repl is clang-tools-extra? The repository layout suggests it's just a part of clang itself. Sorry for tag.

EugeneZelenko commented 9 months ago

@p4vook: Is it separate binary? If so, it's not exactly clang. If there are many issues related to it, it'll make sense to create dedicated label.

vgvassilev commented 8 months ago

@p4vook, can you move the fix of this issue out of the bigger pull request and add a regression test for it?

p4vook commented 8 months ago

Done

@p4vook, can you move the fix of this issue out of the bigger pull request and add a regression test for it?

p4vook commented 7 months ago

I believe this is a deeper issue, it shouldn't be fixed up just by a simple check for nullptr, this seems to be an issue of inserting statements into wrong nodes of syntax tree if namespaces are present. I'll convert the PR to draft.