Skip to content

[5.9][Runtime] Immediate release and return when destroying partial instance of pure ObjC class. #65822

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

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented May 10, 2023

Cherry-pick of #65776 to release/5.9.

Make swift_deallocPartialClassInstance check if the object's class is a pure ObjC class, in which case there are no ivar destroyers and we can just return immediately.

It's possible for an allocWithZone: override to cause self to be a special object constructed in read-only memory. swift_deallocPartialClassInstance calls object_setClass to avoid running the dealloc method of any Swift subclasses, but this call crashes if self is read-only. It's unnecessary when the object's class is pure ObjC and therefore there are no Swift subclasses, so just skip it entirely.

rdar://107756747

…nstance of pure ObjC class.

Make swift_deallocPartialClassInstance check if the object's class is a pure ObjC class, in which case there are no ivar destroyers and we can just return immediately.

It's possible for an allocWithZone: override to cause self to be a special object constructed in read-only memory. swift_deallocPartialClassInstance calls object_setClass to avoid running the dealloc method of any Swift subclasses, but this call crashes if self is read-only. It's unnecessary when the object's class is pure ObjC and therefore there are no Swift subclasses, so just skip it entirely.

rdar://107756747
(cherry picked from commit 3a396af)
@mikeash mikeash requested a review from a team as a code owner May 10, 2023 15:06
@mikeash
Copy link
Contributor Author

mikeash commented May 10, 2023

• Description: A failable initializer in an extension on an ObjC class can attempt to mutate a read-only placeholder object when it fails.
• Risk: Low. If the new logic is wrong, it could cause other problems with failable initializers on ObjC classes. The new conditional executes the exact same code that would have run before, except for the absence of a object_setClass call which is a no-op when the new conditional is true.
• Original PR: #65776
• Reviewed By: @jckarter, @al45tair
• Testing: The Swift test suite covers this scenario, although the placeholder is in writable memory. I've manually verified that the placeholder is no longer mutated with this change.
• Resolves: rdar://107756747

@airspeedswift
Copy link
Member

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit 94e1379 into swiftlang:release/5.9 May 10, 2023
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