Skip to content

Fix use-after-free in SILCombine #34145

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
Oct 2, 2020
Merged

Conversation

meg-gupta
Copy link
Contributor

SILCombine maintains a worklist of instructions and deleting of instructions is valid only via callbacks that remove them from the worklist as well. It calls swift::tryDeleteDeadClosure which in turn calls SILBuilder apis like emitStrongRelease/emitReleaseValue/emitDestroyValue which can delete instructions via SILInstruction::eraseFromParent leaving behind a stale entry in SILCombine's worklist causing a crash.

This PR adds swift::emitDestroyOperation which correctly calls the appropriate InstModCallbacks on added/removed instructions. This comes from swift::releasePartialApplyCapturedArg which was handling creation of destroys with callbacks correctly.

@meg-gupta meg-gupta requested a review from eeckstein October 1, 2020 08:53
@meg-gupta
Copy link
Contributor Author

I tried to change the SILBuilder apis emitStrongRelease/emitReleaseValue/emitDestroyValue such that they don't do any "folding". Even though there are no changes in benchmarks, several tests need to be updated. Because there are no optimizations in GuaranteedARCOpts at -Onone that can optimize the same way as the "folding" in these apis. I'll do this cleanup at a later time. cc @gottesmm

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test OS X Platform
Git Sha - d7086c513021003c81e3154195231999c4b2aa23

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - d7086c513021003c81e3154195231999c4b2aa23

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

LGTM!
(I just have a suggestion for improvement - see my comment).

// issues must have been dealt with by our caller.
if (operand->getType().isAddress()) {
// Then emit the destroy_addr for this operand
SILInstruction *newInst = builder.emitDestroyAddrAndFold(loc, operand);
Copy link
Contributor

Choose a reason for hiding this comment

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

emitDetroyAddrAndFold can return a nullptr. Shouldn't createNewInst only be called if it's not a nullptr?
This is not new with this PR, so obviously it can deal with nullptrs. But still, I think, just to be on the save side createNewInst should not be called with a nullptr.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Fixed it.

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - f05acca

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test OS X platform

@meg-gupta
Copy link
Contributor Author

@swift-ci test Windows platform

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - f05acca

@swift-ci
Copy link
Contributor

swift-ci commented Oct 1, 2020

Build failed
Swift Test OS X Platform
Git Sha - f05acca

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta meg-gupta merged commit e2a9bf2 into swiftlang:main Oct 2, 2020
ainu-bot added a commit to google/swift that referenced this pull request Oct 2, 2020
* 'master' of github.com:apple/swift:
  Fix use-after-free in SILCombine (swiftlang#34145)
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