llvm / llvm-project

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

Segmentation Fault when extensions in "ImplementationFileExtensions" or "HeaderFileExtensions" settings have leading dots #96806

Open phantom-jacob opened 3 weeks ago

phantom-jacob commented 3 weeks ago

Ubuntu 22.04, g++-10, LLVM 17:

/usr/bin/clang-tidy-17 --fix --extra-arg-before=--driver-mode=g++ /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp -- /usr/bin/g++ -DDEV -DPHANTOM_CLOUD -I/home/jacobd/git/phantom/alchemy -I/home/jacobd/git/phantom/build/alchemy -I/home/jacobd/git/phantom/build/schema -isystem /home/jacobd/.conan/data/boost/1.83.0/_/_/package/1714959b53650488b37c41cd3a9ade3fb312129e/include -isystem /home/jacobd/.conan/data/protobuf/3.15.8/_/_/package/2174169f0a98f7948e48a1ac4492bd66e02fcec8/include -isystem /home/jacobd/.conan/data/zlib/1.3.1/_/_/package/af11149481c69e4ca43a19f2690848b74023e63e/include -Wno-unused-command-line-argument -O2 -g -DNDEBUG -fPIC -pipe -Wall -Werror -Wdelete-non-virtual-dtor -ggdb -DSHOULD_LOG_IPC -fcoroutines -std=gnu++2a -MD -MT alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -MF alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o.d -o alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -c /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.  Program arguments: /usr/bin/clang-tidy-17 --fix --extra-arg-before=--driver-mode=g++ /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp -- /usr/bin/g++ -DDEV -DPHANTOM_CLOUD -I/home/jacobd/git/phantom/alchemy -I/home/jacobd/git/phantom/build/alchemy -I/home/jacobd/git/phantom/build/schema -isystem /home/jacobd/.conan/data/boost/1.83.0/_/_/package/1714959b53650488b37c41cd3a9ade3fb312129e/include -isystem /home/jacobd/.conan/data/protobuf/3.15.8/_/_/package/2174169f0a98f7948e48a1ac4492bd66e02fcec8/include -isystem /home/jacobd/.conan/data/zlib/1.3.1/_/_/package/af11149481c69e4ca43a19f2690848b74023e63e/include -Wno-unused-command-line-argument -O2 -g -DNDEBUG -fPIC -pipe -Wall -Werror -Wdelete-non-virtual-dtor -ggdb -DSHOULD_LOG_IPC -fcoroutines -std=gnu++2a -MD -MT alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -MF alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o.d -o alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -c /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp
 #0 0x0000776be2ccc406 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-17/bin/../lib/libLLVM-17.so.1+0xccc406)
 #1 0x0000776be2cca5b0 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-17/bin/../lib/libLLVM-17.so.1+0xcca5b0)
 #2 0x0000776be2ccca9b (/usr/lib/llvm-17/bin/../lib/libLLVM-17.so.1+0xccca9b)
 #3 0x0000776be1842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000606037df1cec clang::tidy::ClangTidyContext::diag(llvm::StringRef, llvm::StringRef, clang::DiagnosticIDs::Level) (/usr/bin/clang-tidy-17+0x137bcec)
 #5 0x0000606037df177a clang::tidy::ClangTidyContext::setCurrentFile(llvm::StringRef) (/usr/bin/clang-tidy-17+0x137b77a)
 #6 0x0000606037de4e2f clang::tidy::getCheckNames[abi:cxx11](clang::tidy::ClangTidyOptions const&, bool) (/usr/bin/clang-tidy-17+0x136ee2f)
 #7 0x00006060370e4113 clang::tidy::clangTidyMain(int, char const**) (/usr/bin/clang-tidy-17+0x66e113)
 #8 0x0000776be1829d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
 #9 0x0000776be1829e40 call_init ./csu/../csu/libc-start.c:128:20
#10 0x0000776be1829e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#11 0x00006060370e0e65 _start (/usr/bin/clang-tidy-17+0x66ae65)
[1]    581833 segmentation fault (core dumped)  /usr/bin/clang-tidy-17 --fix --extra-arg-before=--driver-mode=g++  --  -DDEV

Using this clang-tidy config:

---
Checks:
  - "-*"
  - bugprone-use-after-move
  - clang-analyzer-*
  - cppcoreguidelines-avoid-capturing-lambda-coroutines
  - cppcoreguidelines-avoid-const-or-ref-data-members
  - cppcoreguidelines-avoid-goto
  # it's pretty time consuming to fix/triage all of these, but
  # it is a high value lint, so we should try to enable it
  - "-cppcoreguidelines-avoid-non-const-global-variables"
  - cppcoreguidelines-avoid-reference-coroutine-parameters
  - cppcoreguidelines-interfaces-global-init
  - cppcoreguidelines-misleading-capture-default-by-value
  - cppcoreguidelines-missing-std-forward
  - cppcoreguidelines-prefer-member-initializer
  - cppcoreguidelines-pro-type-const-cast
  - cppcoreguidelines-pro-type-member-init
  - cppcoreguidelines-rvalue-reference-param-not-moved
  - cppcoreguidelines-slicing
  - cppcoreguidelines-virtual-class-destructor
  - modernize-concat-nested-namespaces
  - modernize-loop-convert
  - modernize-make-shared
  - modernize-make-unique
  - modernize-raw-string-literal
  - modernize-use-default-member-init
  - modernize-use-equals-default
  - modernize-use-equals-delete
  - modernize-use-nullptr
  - modernize-use-override
  - modernize-use-using
  - performance-for-range-copy
  - performance-inefficient-algorithm
  - performance-move-const-arg
  - readability-braces-around-statements
  - readability-duplicate-include
  - readability-else-after-return
  - readability-redundant-access-specifiers
  - readability-redundant-casting
  - readability-redundant-member-init
  - readability-simplify-boolean-expr
HeaderFilterRegex: ".*alchemy.*"
ImplementationFileExtensions: [".cpp"]
HeaderFileExtensions: [".h"]
FormatStyle: file
CheckOptions:
  - key: google-readability-braces-around-statements.ShortStatementLines
    value: "1"
  - key: google-readability-function-size.StatementThreshold
    value: "800"
  - key: google-readability-namespace-comments.ShortNamespaceLines
    value: "10"
  - key: google-readability-namespace-comments.SpacesBeforeComments
    value: "2"
  - key: modernize-loop-convert.MaxCopySize
    value: "16"
  - key: modernize-loop-convert.MinConfidence
    value: reasonable
  - key: modernize-loop-convert.NamingStyle
    value: CamelCase
  - key: modernize-pass-by-value.IncludeStyle
    value: llvm
  - key: modernize-replace-auto-ptr.IncludeStyle
    value: llvm
  - key: cppcoreguidelines-avoid-do-while.IgnoreMacros
    value: true
...
EugeneZelenko commented 3 weeks ago

Could you please try 18 or main branch? https://godbolt.org should be helpful.

phantom-jacob commented 3 weeks ago

It looks like the seg fault is resolved if I comment out these two options in my .clang-tidy file:

ImplementationFileExtensions: [".cpp"]
HeaderFileExtensions: [".h"]
phantom-jacob commented 3 weeks ago

Confirmed seg fault persists with clang-tidy-18 when using either one of these settings:

ImplementationFileExtensions: [".cpp"]
HeaderFileExtensions: [".h"]

Output:

/usr/bin/clang-tidy-18 --extra-arg-before=--driver-mode=g++ /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp -- /usr/bin/g++ -DDEV -DPHANTOM_CLOUD -I/home/jacobd/git/phantom/alchemy -I/home/jacobd/git/phantom/build/alchemy -I/home/jacobd/git/phantom/build/schema -isystem /home/jacobd/.conan/data/boost/1.83.0/_/_/package/1714959b53650488b37c41cd3a9ade3fb312129e/include -isystem /home/jacobd/.conan/data/protobuf/3.15.8/_/_/package/2174169f0a98f7948e48a1ac4492bd66e02fcec8/include -isystem /home/jacobd/.conan/data/zlib/1.3.1/_/_/package/af11149481c69e4ca43a19f2690848b74023e63e/include -Wno-unused-command-line-argument -O2 -g -DNDEBUG -fPIC -pipe -Wall -Werror -Wdelete-non-virtual-dtor -ggdb -DSHOULD_LOG_IPC -fcoroutines -std=gnu++2a -MD -MT alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -MF alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o.d -o alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -c /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.  Program arguments: /usr/bin/clang-tidy-18 --extra-arg-before=--driver-mode=g++ /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp -- /usr/bin/g++ -DDEV -DPHANTOM_CLOUD -I/home/jacobd/git/phantom/alchemy -I/home/jacobd/git/phantom/build/alchemy -I/home/jacobd/git/phantom/build/schema -isystem /home/jacobd/.conan/data/boost/1.83.0/_/_/package/1714959b53650488b37c41cd3a9ade3fb312129e/include -isystem /home/jacobd/.conan/data/protobuf/3.15.8/_/_/package/2174169f0a98f7948e48a1ac4492bd66e02fcec8/include -isystem /home/jacobd/.conan/data/zlib/1.3.1/_/_/package/af11149481c69e4ca43a19f2690848b74023e63e/include -Wno-unused-command-line-argument -O2 -g -DNDEBUG -fPIC -pipe -Wall -Werror -Wdelete-non-virtual-dtor -ggdb -DSHOULD_LOG_IPC -fcoroutines -std=gnu++2a -MD -MT alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -MF alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o.d -o alchemy/libutil_v2/CMakeFiles/util_v2.dir/pretty_print.cpp.o -c /home/jacobd/git/phantom/alchemy/libutil_v2/pretty_print.cpp
 #0 0x00007dc41ed94716 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xd94716)
 #1 0x00007dc41ed926d0 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xd926d0)
 #2 0x00007dc41ed94ddb (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xd94ddb)
 #3 0x00007dc41d842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00006316ff30b73c clang::tidy::ClangTidyContext::diag(llvm::StringRef, llvm::StringRef, clang::DiagnosticIDs::Level) (/usr/bin/clang-tidy-18+0x136a73c)
 #5 0x00006316ff30b1ca clang::tidy::ClangTidyContext::setCurrentFile(llvm::StringRef) (/usr/bin/clang-tidy-18+0x136a1ca)
 #6 0x00006316ff2fe801 clang::tidy::getCheckNames[abi:cxx11](clang::tidy::ClangTidyOptions const&, bool) (/usr/bin/clang-tidy-18+0x135d801)
 #7 0x00006316fe69b46e clang::tidy::clangTidyMain(int, char const**) (/usr/bin/clang-tidy-18+0x6fa46e)
 #8 0x00007dc41d829d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
 #9 0x00007dc41d829e40 call_init ./csu/../csu/libc-start.c:128:20
#10 0x00007dc41d829e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#11 0x00006316fe6980e5 _start (/usr/bin/clang-tidy-18+0x6f70e5)
[1]    600220 segmentation fault (core dumped)  /usr/bin/clang-tidy-18 --extra-arg-before=--driver-mode=g++  -- /usr/bin/g++
EugeneZelenko commented 3 weeks ago

@phantom-jacob: Could you please provide reproducer, preferably on https://godbolt.org?

phantom-jacob commented 3 weeks ago

Confirmed reproducible with an empty file. Seems entirely related to the config:

$ touch empty.cpp
$ clang-tidy-18 empty.cpp
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.  Program arguments: clang-tidy-18 empty.cpp
 #0 0x00007734f7594716 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xd94716)
 #1 0x00007734f75926d0 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xd926d0)
 #2 0x00007734f7594ddb (/usr/lib/llvm-18/bin/../lib/libLLVM.so.18.1+0xd94ddb)
 #3 0x00007734f6042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00005a053e54e73c clang::tidy::ClangTidyContext::diag(llvm::StringRef, llvm::StringRef, clang::DiagnosticIDs::Level) (/usr/lib/llvm-18/bin/clang-tidy+0x136a73c)
 #5 0x00005a053e54e1ca clang::tidy::ClangTidyContext::setCurrentFile(llvm::StringRef) (/usr/lib/llvm-18/bin/clang-tidy+0x136a1ca)
 #6 0x00005a053e541801 clang::tidy::getCheckNames[abi:cxx11](clang::tidy::ClangTidyOptions const&, bool) (/usr/lib/llvm-18/bin/clang-tidy+0x135d801)
 #7 0x00005a053d8de46e clang::tidy::clangTidyMain(int, char const**) (/usr/lib/llvm-18/bin/clang-tidy+0x6fa46e)
 #8 0x00007734f6029d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
 #9 0x00007734f6029e40 call_init ./csu/../csu/libc-start.c:128:20
#10 0x00007734f6029e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#11 0x00005a053d8db0e5 _start (/usr/lib/llvm-18/bin/clang-tidy+0x6f70e5)
[1]    604354 segmentation fault (core dumped)  clang-tidy-18 empty.cpp
phantom-jacob commented 3 weeks ago

Okay, narrowed it down even further. It appears to work if I remove the . characters from the extension lists. Either of these settings will cause clang-tidy to crash:

ImplementationFileExtensions: [".cpp"]
HeaderFileExtensions: [".h"]

But with a small edit, clang-tidy accepts the config and no longer crashes:

ImplementationFileExtensions: ["cpp"]
HeaderFileExtensions: ["h"]

So, in all it's probably a fairly minor issue, but it could handle the invalid config more gracefully

PiotrZSL commented 2 weeks ago

Crash is because when emitting warning "Invalid header file extensions" diagnostic engine is not set, and because of that we got nullptr dereference. This isn't easy to fix.