Skip to content

[5.2] SILCombine: fix a miscompile in the alloc_stack optimization which causes a use-after-free. #29890

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
Feb 18, 2020

Conversation

eeckstein
Copy link
Contributor

This is a cherry-pick from #29882

Explanation: This fixes a miscompile in the SILCombine optimization. When trying to remove a "dead" alloc_stack, the optimization didn't take into account that release-instructions could be moved over copy-instructions. This makes the alloc_stack the only instance which keeps an object alive. Removing the alloc_stack resulted in a release of an object, followed by a retain: exactly in the wrong order. The compiled code crashed.
Scope: It's difficult to scope. The bug is in the compiler since a very long time. The fact that we didn't see it earlier indicates that this miscompile is unlikely to actually happen. On the other hand it can really be almost any kind of swift source code which can trigger this bug.
Risk: Low. It's basically an additional bail-out condition for the optimization.
Issue: rdar://problem/59496027
Testing: I added a regression test.
Reviewer: @aschwaighofer

@eeckstein eeckstein requested a review from a team as a code owner February 17, 2020 20:04
@eeckstein
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - fd0f3fced9d45c0ed2ab2594c17f3d3eac816c19

@eeckstein
Copy link
Contributor Author

Seems that I have to update a sil_combine test in the 5.2 branch

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - fd0f3fced9d45c0ed2ab2594c17f3d3eac816c19

…uses a use-after-free.

A "copy_addr [take] %src to [initialization] %alloc_stack" is replaced by a "destroy_addr %src" if the alloc_stack is otherwise dead.
This is okay as long as the "moved" object is kept alive otherwise.
This can break if a retain of %src is moved after the copy_addr. It cannot happen with OSSA.
So as soon as we have OSSA, we can remove the check again.

rdar://problem/59496027
@eeckstein
Copy link
Contributor Author

@swift-ci test

1 similar comment
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein merged commit d962b78 into swiftlang:swift-5.2-branch Feb 18, 2020
@eeckstein eeckstein deleted the fix-sil-combine-5.2 branch February 18, 2020 11:12
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