Skip to content

[Frontend] Clean Up Usage of UnifiedStatsReporter #30109

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 5 commits into from
Feb 28, 2020

Conversation

CodaFi
Copy link
Contributor

@CodaFi CodaFi commented Feb 28, 2020

This patch does a bunch of NFC things, and one particularly not NFC thing to fix a gigantic use-after-free in frontend jobs that pass -dump-api-path.

NFC Stuff:

  • Centralize ownership of UnifiedStatsReporter in the CompilerInstance and automatically set it up along with the rest of its state.
  • Delete a bunch of useless UnifiedStatsReporter parameters
  • Reduce reliance on the ASTContext as a source of truth for how to recover the UnifiedStatsReporter - it will be deallocated if we reach LLVM codegen.

Not-so-NFC Stuff:

  • Fix a use-after-free involving -dump-api-path and codegen with single primaries.

The routine that frees up some memory by releasing the ASTContext and SILModules conditionally could potentially be run which would destroy the resources needed by that frontend action. See the last commit.

@CodaFi CodaFi requested a review from davidungar February 28, 2020 00:11
@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

@swift-ci please test

@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

And because Windows is pretty good at surfacing use-after-moves:

@swift-ci please test Windows platform

Copy link
Contributor

@davidungar davidungar left a comment

Choose a reason for hiding this comment

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

Overall, very nice! The change I would really like is the use of a NullablePtr instead of a plain pointer to a stats structure that may or may not be null.

The lifetime of the UnifiedStatsReporter was not entirely clear from context. Stick it in the CompilerInstance instead so it can live as long as the compile job.

It is critical that its lifetime be extended beyond that of the ASTContext, as the context may be torn down by the time code generation happens, but additional statistics are recorded during LLVM codegen.
The only non-trivial bit is making the DiagnosticEngine parameter to swift::performLLVM required. No callers were taking advantage of this parameter allowing NULL.
Now that the CompilerInstance owns the stats reporter, use that instead.
Centralize part of the routine that selects which resources to free. Then, add an additional condition for -dump-api-path.

Before, if this option were specified along with -emit-llvm or -c, the compiler would try to rebuild the torn-down ModuleDecl and crash trying to access the torn-down ASTContext.
@CodaFi CodaFi force-pushed the lies-more-lies-and-statistics branch from 05fb938 to a6651a9 Compare February 28, 2020 01:13
@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

@swift-ci smoke test

@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

@swift-ci test Windows platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 05fb9382d92ffdc5e8dd4dfb968b2d2451883abb

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 05fb9382d92ffdc5e8dd4dfb968b2d2451883abb

@davidungar
Copy link
Contributor

Glad my thoughts made sense. Thanks for using them.

@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

@swift-ci please test Windows platform

@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

@swift-ci smoke test Linux platform

1 similar comment
@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

@swift-ci smoke test Linux platform

@CodaFi
Copy link
Contributor Author

CodaFi commented Feb 28, 2020

Windows seems out for maintenance.

⛵️

@CodaFi CodaFi merged commit d494cc8 into swiftlang:master Feb 28, 2020
@CodaFi CodaFi deleted the lies-more-lies-and-statistics branch February 28, 2020 18:24
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