diff options
author | Ivan Murashko <ivan.murashko@gmail.com> | 2023-09-04 18:52:01 +0100 |
---|---|---|
committer | Ivan Murashko <ivan.murashko@gmail.com> | 2023-09-04 18:53:49 +0100 |
commit | d03a7f15f019beb1896872ba9321cfed5f16a05f (patch) | |
tree | fa6602d26261523826da71b644d2a66a2fb3320a /clang/lib/Frontend/CompilerInvocation.cpp | |
parent | 6ca4fe64f167a4c7b0fa281ad2b384f7fb7f2cce (diff) | |
download | llvm-d03a7f15f019beb1896872ba9321cfed5f16a05f.zip llvm-d03a7f15f019beb1896872ba9321cfed5f16a05f.tar.gz llvm-d03a7f15f019beb1896872ba9321cfed5f16a05f.tar.bz2 |
[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
Diffstat (limited to 'clang/lib/Frontend/CompilerInvocation.cpp')
0 files changed, 0 insertions, 0 deletions