-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[ClangImporter] Avoid use-after-free of clang::DiagnosticOptions after rebranch #85445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ClangImporter] Avoid use-after-free of clang::DiagnosticOptions after rebranch #85445
Conversation
…r rebranch Upstream LLVM in llvm/llvm-project#139584 changed `DiagnosticOptions` from being a referenced counted object to just be a reference, not owned by the `clang::DiagnosticEngine`. In 0981b71 (part of swiftlang#82243), the usages of the Swift repository were adapted to the new memory model, but it introduced at least one use-after-free and a potential one around the usage of Clang in the Clang Importer. This commit tries to fix the use-after-free in both cases, by returning a `unique_ptr` to the `clang::DiagnosticOptions`, which makes the lifetime of the `DiagnosticOptions` match the lifetime of the variable that uses it (normally a `CompilerInvocation`). Other cases in 0981b71 should be safe because the lifetime of the `DiagnosticOptions` do not seem to propagate beyond the scope of the functions where they live (but I am not fully sure about the one in `IDETool/CompilerInvocation.cpp` completely). This was causing compiler crashes during the test `Interop/Cxx/stdlib/unsupported-stdlib.swift` which eventually uses `createClangDriver` and tries to emit a diagnostic, which in some cases was reading the memory from `DiagnosticOptions` when it was already out of scope.
|
@swift-ci please smoke test |
egorzhdan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for discovering and fixing this!
jansvoboda11
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this. Left a comment on ClangImporter::createClangInvocation(), but the rest looks good.
| std::unique_ptr<clang::CompilerInvocation> ClangImporter::createClangInvocation( | ||
| std::pair<std::unique_ptr<clang::CompilerInvocation>, | ||
| std::unique_ptr<clang::DiagnosticOptions>> | ||
| ClangImporter::createClangInvocation( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary. clangDiags is a local, so diagOpts being local should be fine. The returned CI doesn't hold on to either of these.
| /// Clang diagnostic options used to create the Clang invocation. | ||
| std::unique_ptr<clang::DiagnosticOptions> DiagnosticOptions; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Clang diagnostic options used to create the Clang invocation. | |
| std::unique_ptr<clang::DiagnosticOptions> DiagnosticOptions; | |
| /// Clang diagnostic options used to create the Clang driver. | |
| std::unique_ptr<clang::DiagnosticOptions> DiagnosticOptions; |
The invocation doesn't need external DiagnosticOptions.
|
I will remove the pieces that touched |
…swiftlang#85445) Feedback in swiftlang#85445 after it merged pointed out that the changes around `createClangInvocation` are not necessary because `CompilerInvocation` do not hold a reference to `clang::DiagnosticOptions`, only the `clang::driver::Driver` does. These changes undo the modifications done there and return the code to the previous state (but keeps the changes around `createClangDriver` which was causing the use-after-free).
That's right. |
…#85445) (#85457) Feedback in #85445 after it merged pointed out that the changes around `createClangInvocation` are not necessary because `CompilerInvocation` do not hold a reference to `clang::DiagnosticOptions`, only the `clang::driver::Driver` does. These changes undo the modifications done there and return the code to the previous state (but keeps the changes around `createClangDriver` which was causing the use-after-free).
Upstream LLVM in llvm/llvm-project#139584 changed
DiagnosticOptionsfrom being a referenced counted object to just be a reference, not owned by theclang::DiagnosticEngine.In 0981b71 (part of #82243), the usages of the Swift repository were adapted to the new memory model, but it introduced at least one use-after-free and a potential one around the usage of Clang in the Clang Importer.
This commit tries to fix the use-after-free in both cases, by returning a
unique_ptrto theclang::DiagnosticOptions, which makes the lifetime of theDiagnosticOptionsmatch the lifetime of the variable that uses it (normally aCompilerInvocation).Other cases in 0981b71 should be safe because the lifetime of the
DiagnosticOptionsdo not seem to propagate beyond the scope of the functions where they live (but I am not fully sure about the one inIDETool/CompilerInvocation.cppcompletely).This was causing compiler crashes during the test
Interop/Cxx/stdlib/unsupported-stdlib.swiftwhich eventually usescreateClangDriverand tries to emit a diagnostic, which in some cases was reading the memory fromDiagnosticOptionswhen it was already out of scope.