Skip to content

[stdlib] Avoid materializing strong references in potential dangling unmanaged opaque functions #70945

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 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions stdlib/public/core/Unmanaged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ public struct Unmanaged<Instance: AnyObject> {
public static func fromOpaque(
@_nonEphemeral _ value: UnsafeRawPointer
) -> Unmanaged {
return Unmanaged(_private: unsafeBitCast(value, to: Instance.self))
// NOTE: `value` is allowed to represent a dangling reference, so
// this function must not ever try to dereference it. For
// example, this function must NOT use the init(_private:) initializer
// because doing so requires materializing a strong reference to 'Instance'.
// This materialization would be enough to convince the compiler to add
// retain/releases which must be avoided for the opaque pointer functions.
// 'Unmanaged<Instance>' is layout compatible with 'UnsafeRawPointer' and
// casting to that will not attempt to retain the reference held at 'value'.
unsafeBitCast(value, to: Unmanaged<Instance>.self)
}

/// Unsafely converts an unmanaged class reference to a pointer.
Expand All @@ -48,7 +56,13 @@ public struct Unmanaged<Instance: AnyObject> {
/// - Returns: An opaque pointer to the value of this unmanaged reference.
@_transparent
public func toOpaque() -> UnsafeMutableRawPointer {
return unsafeBitCast(_value, to: UnsafeMutableRawPointer.self)
// NOTE: `self` is allowed to be a dangling reference.
// Therefore, this function must not unsafeBitCast '_value' because
// that will get a strong reference temporary value that the compiler will
// try to retain/release. Use 'self' to avoid this. 'Unmanaged<Instance>' is
// layout compatible with 'UnsafeRawPointer' and casting from that will not
// attempt to retain the reference held at '_value'.
unsafeBitCast(self, to: UnsafeMutableRawPointer.self)
}

/// Creates an unmanaged reference with an unbalanced retain.
Expand Down
22 changes: 22 additions & 0 deletions test/stdlib/Unmanaged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,26 @@ UnmanagedTests.test("Opaque") {
_fixLifetime(ref)
}

UnmanagedTests.test("Opaque avoid retain/release") {
// The purpose of this test is to ensure that the fromOpaque/toOpaque calls do
// NOT attempt to retain/release the class instance at the passed pointer.
// Here we're simulating a dangling pointer referencing a class who has
// already been released (the allocated pointer points at nothing) and
// attempting to create an Unmanaged instance from it and get back the
// pointer. This test's expectEqual is kind of bogus, we're just checking that
// it doesn't crash.

// Create a dangling pointer, usually to unmapped memory.
Copy link
Contributor

Choose a reason for hiding this comment

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

usually 😂

let ref = UnsafeRawPointer(bitPattern: 1)!
// Turn it into a dangling unmanaged reference.
// We expect this not to crash, as this operation isn't
// supposed to dereference the pointer in any way.
let unmanaged = Unmanaged<Foobar>.fromOpaque(ref)
// Similarly, converting the unmanaged reference back to a
// pointer should not ever try to dereference it either.
let ref2 = unmanaged.toOpaque()
// ...and we must get back the same pointer.
expectEqual(ref, ref2)
}

runAllTests()