Skip to content

[Runtime] Fix leak of bridge objects in swift_generic_initWithTake #69977

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
Nov 27, 2023
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
2 changes: 1 addition & 1 deletion stdlib/public/runtime/BytecodeLayouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ constexpr InitFn initWithTakeTable[] = {
&copyingInitWithTake,
&copyingInitWithTake,
&unknownWeakInitWithTake,
&bridgeRetain,
&copyingInitWithTake,
&copyingInitWithTake,
&copyingInitWithTake,
nullptr, // Custom
Expand Down
15 changes: 15 additions & 0 deletions test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ public struct SinglePayloadEnumExtraTagBytesWrapper {
}
}

public struct NotBitwiseTakableBridge<T> {
let x: Int = 0
let y: [T]
weak var z: AnyObject? = nil

public init(_ y: [T]) {
self.y = y
}
}

public enum SinglePayloadEnumExtraTagBytes {
case empty0
case empty1
Expand Down Expand Up @@ -592,6 +602,11 @@ public func testInit<T>(_ ptr: UnsafeMutablePointer<T>, to x: T) {
ptr.initialize(to: x)
}

@inline(never)
public func testInitTake<T>(_ ptr: UnsafeMutablePointer<T>, to x: consuming T) {
ptr.initialize(to: consume x)
}

@inline(never)
public func testDestroy<T>(_ ptr: UnsafeMutablePointer<T>) {
ptr.deinitialize(count: 1)
Expand Down
44 changes: 44 additions & 0 deletions test/Interpreter/layout_string_witnesses_static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,50 @@ func testEnumWithExistential() {

testEnumWithExistential()

// Regression test for rdar://118606044
func testNotBitwiseTakableBridge() {
let ptr = UnsafeMutablePointer<NotBitwiseTakableBridge<SimpleClass>>.allocate(capacity: 1)

// initWithTake
do {
let x = NotBitwiseTakableBridge([SimpleClass(x: 23)])
testInitTake(ptr, to: consume x)
}

// assignWithTake
do {
let y = NotBitwiseTakableBridge([SimpleClass(x: 33)])

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
testAssign(ptr, from: y)
}

// assignWithCopy
do {
var z = NotBitwiseTakableBridge([SimpleClass(x: 43)])

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
testAssignCopy(ptr, from: &z)
}

// CHECK-NEXT: Before deinit
print("Before deinit")

// destroy
// CHECK-NEXT: SimpleClass deinitialized!
testDestroy(ptr)

ptr.deallocate()
}

testNotBitwiseTakableBridge()

#if os(macOS)
func testObjc() {
let ptr = UnsafeMutablePointer<ObjcWrapper>.allocate(capacity: 1)
Expand Down