Skip to content

Commit 396ecdf

Browse files
authored
[Distributed] Destroy decoded parameters after executeDistributedTarget (#70806)
1 parent 353fb5d commit 396ecdf

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

lib/IRGen/GenDistributed.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ void DistributedAccessor::decodeArgument(unsigned argumentIdx,
486486
// The argument is +0, so we can use the address of the param in
487487
// the context directly.
488488
arguments.add(resultAddr);
489+
LoadedArguments.push_back(std::make_pair(resultValue.getAddress(), argumentType));
489490
break;
490491
}
491492

test/Distributed/Runtime/distributed_actor_encode_roundtrip.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeCodableForDistributedTests.swiftmodule -module-name FakeCodableForDistributedTests -disable-availability-checking %S/../Inputs/FakeCodableForDistributedTests.swift
33
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
4-
// XXX: %target-build-swift -emit-silgen -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeCodableForDistributedTests.swift %S/../Inputs/FakeDistributedActorSystems.swift
54
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeCodableForDistributedTests.swift %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
65
// RUN: %target-codesign %t/a.out
76
// RUN: %target-run %t/a.out | %FileCheck %s --color
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeCodableForDistributedTests.swiftmodule -module-name FakeCodableForDistributedTests -disable-availability-checking %S/../Inputs/FakeCodableForDistributedTests.swift
3+
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
4+
// XXX: %target-build-swift -emit-silgen -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeCodableForDistributedTests.swift %S/../Inputs/FakeDistributedActorSystems.swift
5+
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeCodableForDistributedTests.swift %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
6+
// RUN: %target-codesign %t/a.out
7+
// RUN: %target-run %t/a.out | %FileCheck %s
8+
9+
// REQUIRES: executable_test
10+
// REQUIRES: concurrency
11+
// REQUIRES: distributed
12+
13+
// rdar://76038845
14+
// UNSUPPORTED: use_os_stdlib
15+
// UNSUPPORTED: back_deployment_runtime
16+
17+
import Distributed
18+
import FakeDistributedActorSystems
19+
import FakeCodableForDistributedTests
20+
21+
typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem
22+
23+
class Sentinel {
24+
let str: String
25+
26+
init(_ str: String) {
27+
self.str = str
28+
print("\(str).init: \(Unmanaged.passUnretained(self).toOpaque())")
29+
}
30+
31+
deinit {
32+
print("\(str).deinit: \(Unmanaged.passUnretained(self).toOpaque())")
33+
}
34+
}
35+
36+
struct InnerStruct1 {
37+
let sentinel: Sentinel
38+
let innerStruct2: InnerStruct2
39+
40+
init() {
41+
self.sentinel = Sentinel("\(Self.self)")
42+
self.innerStruct2 = InnerStruct2()
43+
}
44+
}
45+
46+
struct InnerStruct2 {
47+
let sentinel: Sentinel
48+
49+
init() {
50+
self.sentinel = Sentinel("\(Self.self)")
51+
}
52+
}
53+
54+
enum InnerEnum {
55+
case v1(String)
56+
case v2(InnerStruct1)
57+
}
58+
59+
struct ArgumentType: Codable {
60+
let sentinel: Sentinel
61+
let value: Int
62+
let innerEnum: InnerEnum
63+
64+
init(_ value: Int) {
65+
self.sentinel = Sentinel("ArgumentType")
66+
self.value = value
67+
self.innerEnum = .v2(InnerStruct1())
68+
}
69+
70+
init(from decoder: Decoder) throws {
71+
self.sentinel = Sentinel("ArgumentType")
72+
self.value = 100
73+
self.innerEnum = .v2(InnerStruct1())
74+
}
75+
76+
func encode(to encoder: Encoder) throws {
77+
print("ArgumentType.encode")
78+
}
79+
}
80+
81+
distributed actor TestActor {
82+
public distributed func testFunc(arg: ArgumentType) {
83+
print("value=\(arg.value)")
84+
}
85+
}
86+
87+
@main
88+
struct Main {
89+
90+
static func main() async throws {
91+
let system = DefaultDistributedActorSystem()
92+
93+
let instance = TestActor(actorSystem: system)
94+
let resolved = try TestActor.resolve(id: instance.id, using: system)
95+
96+
// CHECK: ArgumentType.init: [[P1:0x[0-9]+]]
97+
// CHECK: InnerStruct1.init: [[P2:0x[0-9]+]]
98+
// CHECK: InnerStruct2.init: [[P3:0x[0-9]+]]
99+
100+
// CHECK: ArgumentType.deinit: [[P1]]
101+
// CHECK: InnerStruct1.deinit: [[P2]]
102+
// CHECK: InnerStruct2.deinit: [[P3]]
103+
104+
let arg = ArgumentType(100)
105+
try await resolved.testFunc(arg: arg)
106+
}
107+
}

test/Distributed/distributed_actor_thunk_doesnt_leak_class_arguments.swift renamed to test/Distributed/distributed_actor_thunk_doesnt_leak_arguments.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,52 @@ struct S<T> : Codable {
2424
var data: SomeClass<T>
2525
}
2626

27+
28+
class Sentinel {
29+
let str: String = ""
30+
}
31+
32+
struct InnerStruct1 {
33+
let sentinel: Sentinel
34+
let innerStruct2: InnerStruct2
35+
36+
init() {
37+
self.sentinel = Sentinel()
38+
self.innerStruct2 = InnerStruct2()
39+
}
40+
}
41+
42+
struct InnerStruct2 {
43+
let sentinel: Sentinel
44+
45+
init() {
46+
self.sentinel = Sentinel()
47+
}
48+
}
49+
50+
enum InnerEnum {
51+
case v1(String)
52+
case v2(InnerStruct1)
53+
}
54+
55+
struct ArgumentType: Codable {
56+
let sentinel: Sentinel
57+
let value: Int
58+
let innerEnum: InnerEnum
59+
60+
init() {
61+
self.sentinel = Sentinel()
62+
self.value = 100
63+
self.innerEnum = .v2(InnerStruct1())
64+
}
65+
66+
init(from decoder: Decoder) throws {
67+
fatalError("Not implemented")
68+
}
69+
70+
func encode(to encoder: Encoder) throws {}
71+
}
72+
2773
distributed actor Greeter {
2874
// CHECK-LABEL: define linkonce_odr hidden swifttailcc void @"$s15no_to_arg_leaks7GreeterC5test1yyAA9SomeClassCyxGYaKlFTETF"
2975
// CHECK: call void {{.*}}(ptr noalias [[PARAM:%.*]], ptr %arg_type)
@@ -35,6 +81,11 @@ distributed actor Greeter {
3581
// CHECK: call void {{.*}}(ptr noalias [[PARAM:%.*]], ptr %arg_type)
3682
// CHECK-NEXT: call swiftcc void @swift_task_dealloc(ptr [[PARAM]])
3783
distributed func test2<T>(_: S<T>) {}
84+
85+
// CHECK-LABEL: define linkonce_odr hidden swifttailcc void @"$s15no_to_arg_leaks7GreeterC5test3yyAA12ArgumentTypeVYaKFTETF"
86+
// CHECK: call void {{.*}}(ptr noalias [[PARAM:%.*]], ptr %arg_type)
87+
// CHECK-NEXT: call swiftcc void @swift_task_dealloc(ptr [[PARAM]])
88+
distributed func test3(_: ArgumentType) {}
3889
}
3990

4091
func test() async throws {
@@ -45,4 +96,5 @@ func test() async throws {
4596

4697
try await ref.test1(SomeClass<Int>())
4798
try await ref.test2(S(data: SomeClass<Int>()))
99+
try await ref.test3(.init())
48100
}

0 commit comments

Comments
 (0)