Bug 444693

Summary: Crash on assert in ClangHelpers::buildDUChain when casting ParsingEnvironmentFile
Product: [Applications] kdevelop Reporter: Jonathan Verner <jonathan.verner>
Component: Language Support: CPP (Clang-based)Assignee: kdevelop-bugs-null
Status: RESOLVED WORKSFORME    
Severity: crash    
Priority: NOR    
Version First Reported In: git master   
Target Milestone: ---   
Platform: Neon   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Jonathan Verner 2021-10-31 00:43:24 UTC
I recently get quite consistent crashes (without clearing the cache) in `ClangHelpers::BuildDUChain`
on line 168 asserting on `envFile` with the following backtraces (removed the irrelevant parts):

#5  0x00007fff44c253f4 in ClangHelpers::buildDUChain(void*, QMultiHash<void*, Import> const&, ParseSession const&, QFlags<KDevelop::TopDUContext::Feature>, QHash<void*, KDevelop::ReferencedTopDUContext>&, QHash<KDevelop::IndexedString, KDevelop::ModificationRevision> const&, KDevelop::IndexedString const&, ClangIndex*, std::function<bool ()> const&)
    (file=0x7ffea000b4d0, imports=QMultiHash<void *, Import> (size = 0), session=..., features=..., includedFiles=QHash<void *, KDevelop::ReferencedTopDUContext> (size = 1) = {...}, unsavedRevisions=QHash<KDevelop::IndexedString, KDevelop::ModificationRevision> (size = 5) = {...}, parseDocument=..., index=0x55555a9504e0, abortFunction=...) at /home/jonathan/zdroj/clones/kdevelop/plugins/clang/duchain/clanghelpers.cpp:168
#6  0x00007fff44ce0557 in ClangParseJob::run(QSharedPointer<ThreadWeaver::JobInterface>, ThreadWeaver::Thread*) (this=0x555560618090)
    at /home/jonathan/zdroj/clones/kdevelop/plugins/clang/clangparsejob.cpp:341
#7  0x00007ffff24a5def in ThreadWeaver::IdDecorator::run(QSharedPointer<ThreadWeaver::JobInterface>, ThreadWeaver::Thread*)
    (this=<optimized out>, self=..., thread=0x55556083fdd0) at /usr/include/c++/9/bits/atomic_base.h:318


The `envFile` is the result of dynamic casting `context->parsingEnvironmentFile().data()` to `ClangParsingEnvironmentFile*`. This cast
does not go through, because the environment file is actually a `StandardParsingEnvironmentFile` (with language CMake).
This is not surprising, given that the first parameter to `buildDUChain` is a `CXFile` pointing to 

/home/jonathan/zdroj/clones/kdevelop/build/plugins/clangtidy/tests/CMakeFiles/test_clangtidyparser.dir/cmake_clean.cmake

i.e. a **cmake** file (!). The parseDocument parameter, on the other hand, points to

/home/jonathan/zdroj/clones/kdevelop/plugins/custom-buildsystem/kcm_custombuildsystem.h

the contents of session.environment are as follows:

(const ClangParsingEnvironment &) @0x7fff0fffe0b0: {
  <KDevelop::ParsingEnvironment> = {
    _vptr.ParsingEnvironment = 0x7fff44ccb840 <vtable for ClangParsingEnvironment+16>
  }, 
  members of ClangParsingEnvironment:
  m_projectPaths = QVector<KDevelop::Path> (size = 4) = {
    [0] = ("home", "jonathan", "zdroj", "clones", "kdev-python"),
    [1] = ("home", "jonathan", "zdroj", "clones", "kdev-python", "build"),
    [2] = ("home", "jonathan", "zdroj", "clones", "kdevelop"),
    [3] = ("home", "jonathan", "zdroj", "clones", "kdevelop", "build")
  },
  m_includes = QVector<KDevelop::Path> (size = 0),
  m_frameworkDirectories = QVector<KDevelop::Path> (size = 0),
  m_defines = QMap<QString, QString> (size = 0),
  m_pchInclude = ,
  m_workingDirectory = ,
  m_tuUrl = {
    m_index = 2414975
  },
  m_quality = ClangParsingEnvironment::Unknown,
  m_parserSettings = {
    parserOptions = "  "
  }
}

I am very confused. Why is the `ClangHelpers::buildDUChain` even run for a CMakeFile?? Why is the `parseDocument` a completely
unrelated header file???

(Note that ICore::self()->languageController()->languagesForUrl(path.toUrl()).first()->name() correctly returns "CMake" !!!)

Also note that, in `ClangParseJob::run`, `d_ptr->url.str() == "/home/jonathan/zdroj/clones/kdevelop/plugins/custom-buildsystem/kcm_custombuildsystem.h"`,
so the parse job was started for a header file. 

But the translation unit for this file (`clang()->index()->translationUnitForUrl(d_ptr->url).str()`) seems to be the completely unrelated 
cmake file 

/home/jonathan/zdroj/clones/kdevelop/build/plugins/clangtidy/tests/CMakeFiles/test_clangtidyparser.dir/cmake_clean.cmake

Why???

I have to go to sleep now, will try to investigate more later.
Comment 1 Jonathan Verner 2021-10-31 00:59:33 UTC
Hmm, so looking at the top context for 

/home/jonathan/zdroj/clones/kdevelop/plugins/custom-buildsystem/kcm_custombuildsystem.h

gotten via `DUChain::self()->chainForDocument(url)`, and calling
`indexedImporters` on it, the first importer context is actually
a `TopDUContext` whose url is the cmake file (!?!?):
Comment 2 Jonathan Verner 2021-10-31 17:08:08 UTC
I guess its just a cache corruption issue. Though I don't really know how this can happen or why it happens quite regularly :-(

Marking as needs info until I have more information.
Comment 3 Bug Janitor Service 2021-11-15 04:39:11 UTC
Dear Bug Submitter,

This bug has been in NEEDSINFO status with no change for at least
15 days. Please provide the requested information as soon as
possible and set the bug status as REPORTED. Due to regular bug
tracker maintenance, if the bug is still in NEEDSINFO status with
no change in 30 days the bug will be closed as RESOLVED > WORKSFORME
due to lack of needed information.

For more information about our bug triaging procedures please read the
wiki located here:
https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging

If you have already provided the requested information, please
mark the bug as REPORTED so that the KDE team knows that the bug is
ready to be confirmed.

Thank you for helping us make KDE software even better for everyone!
Comment 4 Bug Janitor Service 2021-11-30 04:38:40 UTC
This bug has been in NEEDSINFO status with no change for at least
30 days. The bug is now closed as RESOLVED > WORKSFORME
due to lack of needed information.

For more information about our bug triaging procedures please read the
wiki located here:
https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging

Thank you for helping us make KDE software even better for everyone!