Skip to content

Conversation

Xazax-hun
Copy link
Contributor

We usually have unowned values when dealing with foreign types. Make sure the implicit value ctors will do a +1 to balance the releases. In a release build we had a use after free over-releasing the object. In assert builds we had an assertion failure:

Assertion failed: (value->getOwnershipKind() == OwnershipKind::Guaranteed), function forBorrowedObjectRValue, file ManagedValue.h, line 181.

rdar://160232360

@Xazax-hun Xazax-hun added the c++ interop Feature: Interoperability with C++ label Sep 10, 2025
Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

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

LGTM!

@Xazax-hun Xazax-hun force-pushed the handle-unowned-in-impicit-value-init branch 3 times, most recently from d79cf25 to b4fb025 Compare September 11, 2025 16:33
We usually have unowned values when dealing with foreign types. Make
sure the implicit value ctors will do a +1 to balance the releases. In a
release build we had a use after free over-releasing the object. In
assert builds we had an assertion failure:

Assertion failed: (value->getOwnershipKind() == OwnershipKind::Guaranteed),
function forBorrowedObjectRValue, file ManagedValue.h, line 181.

rdar://160232360
@Xazax-hun Xazax-hun force-pushed the handle-unowned-in-impicit-value-init branch from b4fb025 to fe3cc6d Compare September 12, 2025 11:02
@Xazax-hun
Copy link
Contributor Author

@swift-ci please test

@Xazax-hun Xazax-hun merged commit b5ef290 into swiftlang:main Sep 15, 2025
5 checks passed
@Xazax-hun Xazax-hun deleted the handle-unowned-in-impicit-value-init branch September 15, 2025 14:08
Xazax-hun added a commit to Xazax-hun/swift that referenced this pull request Sep 15, 2025
Explanation: Implicit value constructors did not account for unowned
ownership resulting in unbalanced refcounts and use after free errors.
This PR makes sure we +1 the unowned constructor arguments to balance
out the ref counts.
Issues: rdar://160232360
Original PRs: swiftlang#84203
Risk: Low, the fix is narrow to implicit value ctors taking foreign
reference types and this scenario was broken before the patch.
Testing: Added a compiler test.
Reviewers: @egorzhdan, @jckarter
Xazax-hun added a commit to Xazax-hun/swift that referenced this pull request Sep 25, 2025
…mpicit-value-init

[cxx-interop] Handle Unowned values in implicit value ctors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ interop Feature: Interoperability with C++

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants