Skip to content

[5.8][ClangImporter] Fix stack-use-after-scope in createClangInvocation #63882

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

Merged

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Feb 24, 2023

(Cherry-pick #63881 into release/5.8)

  • Explanation: There was a stack-use-after-scope in ClangImporter's createClangInvocation() function. When creating temporary diagnostic engine for creating invocation object, the consumer object is created inside if...else branches on the stack, but the pointer to it was used outside the scope. That can cause random unpredictable crashes, for example, if the clang compiler argument was malformed. This change resolves that by instantiating the diagnostic consumer on heap, then let the diagnostic engine take the ownership.
  • Scope: ClangImporter creation, while processing clang invocation arguments
  • Risk: Low
  • Issues: rdar://105801504
  • Testing: Passes current test suite. Testing with ASAN locally.
  • Reviewers: Artem Chikin (@artemcm)

ClangDiagnosticConsumer was initialized on the stack in a scope of 'if'
branch, and was passed to the DiagnosticEngine as a pointer. Then the
diagnostic engine was used outside the scope.

Instead, allocate the consumer in the heap, and pass to the engine with
`ShouldOwnClient` true. So the engine takes the ownership.

Speculative fix for rdar://105801504

(cherry picked from commit 1f3d671)
@rintaro rintaro marked this pull request as ready for review February 24, 2023 05:42
@rintaro rintaro requested a review from a team as a code owner February 24, 2023 05:42
@rintaro
Copy link
Member Author

rintaro commented Feb 24, 2023

@swift-ci Please test

@bnbarham bnbarham requested a review from nkcsgexi February 24, 2023 06:04
@rintaro
Copy link
Member Author

rintaro commented Feb 24, 2023

@rintaro
Copy link
Member Author

rintaro commented Feb 24, 2023

@swift-ci Please test Linux

Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me as a tentative fix.

@shahmishal shahmishal merged commit b2f7b0a into swiftlang:release/5.8 Feb 24, 2023
@AnthonyLatsis AnthonyLatsis added the 🍒 release cherry pick Flag: Release branch cherry picks label May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants