This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Fix crashing race in ClangdServer shutdown with stdlib indexing
ClosedPublic

Authored by sammccall on Dec 21 2022, 10:16 AM.

Details

Summary

In principle it's OK for stdlib-indexing tasks to run after the TUScheduler is
destroyed, as mostly they just update the dynamic index. We do drain the
stdlib-indexing queue before destroying the index.

However the task captures references to the PreambleCallbacks object, which is
owned by the TUScheduler. Once this is destroyed (explicitly, early in
~ClangdServer) an outstanding stdlib-indexing task may use-after-free.

The fix here is to avoid capturing references to the PreambleCallbacks.
Alternatives would be to have TUScheduler (exclusively) not own its callbacks
so they could live longer, or explicitly stopping the TUScheduler instead of
early-destroying it. These both seem more invasive.

See https://reviews.llvm.org/D115232 for some more context.

Diff Detail

Event Timeline

sammccall created this revision.Dec 21 2022, 10:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 21 2022, 10:16 AM
Herald added a subscriber: arphaman. · View Herald Transcript
sammccall requested review of this revision.Dec 21 2022, 10:16 AM
kadircet accepted this revision.Dec 21 2022, 2:22 PM

thanks, lgtm!

clang-tools-extra/clangd/ClangdServer.cpp
86

formatting seems to be off here (looks like clang-format can't deal with this leading comment. do you mind dropping it? the comment above makes it clear that we shouldn't be keeping references already.

This revision is now accepted and ready to land.Dec 21 2022, 2:22 PM
This revision was landed with ongoing or failed builds.Dec 21 2022, 2:36 PM
This revision was automatically updated to reflect the committed changes.
sammccall marked an inline comment as done.