Skip to content

Fix unchecked_bitwise_cast to "escape" its operand's ownership #61393

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 3 commits into from
Oct 1, 2022

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Sep 30, 2022

Fix unchecked_bitwise_cast to "escape" its operand's ownership

This makes it impossible for OSSA utilities to reason about the
lifetime of the value being cast. This fixes bugs in which SILGen
generates bitwise casts without protecting that lifetime.

Also fix LexicalDestroyHoisting to bail out when ownership escapes via a
PointerEscape operand.

Fixes rdar://100527903 (Overrelease of optional closure when using shorthand syntax)

The previous OSSA strategy was to gradually eliminate all "pointer
escape" SIL patterns. That would define away a class of bugs in the
OSSA utilities themselves. This was not friendly to developers working
on SILGen, and we still don't have verification that can catch these
bugs.

The new strategy is to make all OSSA utilities aware of pointer
escapes. This acknowledges reality that SIL is imperfect. Instead,
optimizer support for transforming SIL always needs to recognize
potentially dangerous patterns.

The downside of this new strategy is that it hides performance
problems because SIL optimizations can give up whenever they happen to
see a "pointer escape". We need to keep working on this problem
without being motivated by miscompiling code.

This makes it impossible for OSSA utilities to reason about the
lifetime of the value being cast. This fixes bugs in which SILGen
generates bitwise casts without protecting that lifetime.

Fixes rdar://100527903 (Overrelease of optional closure when using
shorthand syntax)

The previous OSSA strategy was to gradually eliminate all "pointer
escape" SIL patterns. That would define away a class of bugs in the
OSSA utilities themselves. This was not friendly to developers working
on SILGen, and we still don't have verification that can catch these
bugs.

The new strategy is to make all OSSA utilities aware of pointer
escapes. This acknowledges reality that SIL is imperfect. Instead,
optimizer support for transforming SIL always needs to recognize
potentially dangerous patterns.

The downside of this new strategy is that it hides performance
problems because SIL optimizations can give up whenever they happen to
see a "pointer escape". We need to keep working on this problem
without being motivated by miscompiling code.
Fix the utilities used by LexicalDestroyHoisting that finds all uses
to report a "PointerEscape". We can't rely on lifetime analysis when
such a use is present.
Test that both canonicalization and lexical destroy hoisting recognize
an "bail out" in the presence of unchecked_bitwise_cast.
@atrick
Copy link
Contributor Author

atrick commented Sep 30, 2022

@swift-ci test

@atrick
Copy link
Contributor Author

atrick commented Sep 30, 2022

@swift-ci benchmark

@@ -238,9 +238,31 @@ OPERAND_OWNERSHIP(PointerEscape, ProjectExistentialBox)
OPERAND_OWNERSHIP(PointerEscape, UncheckedOwnershipConversion)
OPERAND_OWNERSHIP(PointerEscape, ConvertEscapeToNoEscape)

// UncheckedBitwiseCast ownership behaves like RefToUnowned. It produces an
// Unowned values from a non-trivial value, without consuming or borrowing the
Copy link
Contributor

Choose a reason for hiding this comment

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

"an Unowned value"

bool swift::findUsesOfSimpleValue(SILValue value,
SmallVectorImpl<Operand *> *usePoints) {
for (auto *use : value->getUses()) {
if (use->getOperandOwnership() == OperandOwnership::Borrow) {
switch (use->getOperandOwnership()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the same change in LexicalDestroyFolding's findBorroweeUsage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I'll need to figure out a test case for that one

@atrick atrick changed the title Fix closure release Fix unchecked_bitwise_cast to "escape" its operand's ownership Sep 30, 2022
@atrick
Copy link
Contributor Author

atrick commented Oct 1, 2022

@nate-chandler I'll merge this since CI is happy with it and the sooner it gets into builds the better. I'll immediately follow up with the destroy folding fix and comment grammar.

@atrick atrick merged commit 18143e2 into swiftlang:main Oct 1, 2022
@atrick atrick deleted the fix-closure-release branch October 1, 2022 02:38
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.

2 participants