Skip to content

ClangImporter: Take ownership of clang::TargetOptions used for code generation #82912

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
merged 1 commit into from
Jul 9, 2025

Conversation

AnthonyLatsis
Copy link
Collaborator

Since llvm/llvm-project#106271, clang::TargetInfo does not co-own target options, so there is no longer anything to keep them alive after we discard the throwaway Clang invocation they originate from. Make the importer take ownership of a copy of code generation target options to avoid a use after free.

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

@AnthonyLatsis
Copy link
Collaborator Author

preset=buildbot_incremental,tools=RA,stdlib=RA

@swift-ci please test with preset macOS

clang::MacroBuilder builder(predefines);
targetInfo->getTargetDefines(Instance->getLangOpts(), builder);
} else {
targetInfo =
Copy link
Contributor

Choose a reason for hiding this comment

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

Is my understanding right that TargetOpts remains nullptr when we use the existing invocation? I wonder if this can get a bit confusing that we don't have one unique way to refer to TargetOpts regardless of which path we took.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, correct. That’s pretty much why I didn’t add a getter for TargetOpts. It’s just there to keep the options alive for as long as CodeGenTargetInfo when they differ from the importer invocation’s. You’re still supposed to access them through the invocation or getCodeGenTargetInfo, depending on which you want.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a doc comment to TargetOpts.

generation

Since llvm/llvm-project#106271,
`clang::TargetInfo` does not co-own target options, so there is no
longer anything to keep them alive after we discard the throwaway Clang
invocation they originate from. Make the importer take ownership of a
copy of code generation target options to avoid a use after free.
@AnthonyLatsis
Copy link
Collaborator Author

Copy link
Contributor

@artemcm artemcm left a comment

Choose a reason for hiding this comment

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

Thank you.

@AnthonyLatsis
Copy link
Collaborator Author

Targeted test failures gone; merging.

@AnthonyLatsis AnthonyLatsis merged commit 7c77838 into swiftlang:rebranch Jul 9, 2025
@AnthonyLatsis AnthonyLatsis deleted the rebranch branch July 9, 2025 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants