[all-commits] [llvm/llvm-project] d03a7f: [clangd] SIGSEGV at clangd: DiagnosticConsumer Is ...

Ivan Murashko via All-commits all-commits at lists.llvm.org
Mon Sep 4 10:54:16 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: d03a7f15f019beb1896872ba9321cfed5f16a05f
      https://github.com/llvm/llvm-project/commit/d03a7f15f019beb1896872ba9321cfed5f16a05f
  Author: Ivan Murashko <ivan.murashko at gmail.com>
  Date:   2023-09-04 (Mon, 04 Sep 2023)

  Changed paths:
    M clang-tools-extra/clangd/Preamble.cpp

  Log Message:
  -----------
  [clangd] SIGSEGV at clangd: DiagnosticConsumer Is Used After Free

This is a follow-up patch for D148088. The dynamic symbol index (`FileIndex::updatePreamble`) may run in a separate thread, and the `DiagnosticConsumer` that is set up in `buildPreamble` might go out of scope before it is used. This could result in a SIGSEGV when attempting to call any method of the `DiagnosticConsumer` class.

The function `buildPreamble` sets up the `DiagnosticConsumer` as follows:
```
... buildPreamble(...) {
...
  StoreDiags PreambleDiagnostics;
  ...
  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
    CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(),
                                        &PreambleDiagnostics,
                                        /*ShouldOwnClient=*/false);
  ...
  // The call might use the diagnostic consumer in a separate thread
  PreambleCallback(...)
  ...
}
```
`PreambleDiagnostics` might be out of scope for `buildPreamble` function when we call it inside `PreambleCallback` in a separate thread.

The Fix
The fix involves replacing the client (DiagnosticConsumer) with an `IgnoringDiagConsumer` instance, which will print messages to the clangd log.

Alternatively, we can replace `PreambleDiagnostics` with an object that is owned by `DiagnosticsEngine`.

Note
There is no corresponding LIT/GTest for this issue, since there is a specific race condition that is difficult to reproduce within a test framework.

Test Plan:
```
ninja check-clangd
```

Reviewed By: kadircet, sammccall

Differential Revision: https://reviews.llvm.org/D159363




More information about the All-commits mailing list