Skip to content

Commit 1419fc9

Browse files
authored
Merge pull request #3178 from CodaFi/leak-by-example
2 parents 517c0d5 + 56f495c commit 1419fc9

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

stdlib/private/StdlibUnittest/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ set(swift_stdlib_unittest_framework_depends)
55
set(swift_stdlib_unittest_compile_flags
66
"-Xfrontend" "-disable-objc-attr-requires-foundation-module")
77

8+
if (SWIFT_RUNTIME_ENABLE_LEAK_CHECKER)
9+
list(APPEND swift_stdlib_unittest_compile_flags "-DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER")
10+
endif()
11+
812
if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
913
list(APPEND swift_stdlib_unittest_platform_sources
1014
GetOSVersion.mm)

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,15 @@ public func runAllTests() {
11671167
}
11681168
}
11691169

1170+
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
1171+
1172+
@_silgen_name("swift_leaks_startTrackingObjects")
1173+
func startTrackingObjects(_: UnsafePointer<CChar>)
1174+
@_silgen_name("swift_leaks_stopTrackingObjects")
1175+
func stopTrackingObjects(_: UnsafePointer<CChar>) -> Int
1176+
1177+
#endif
1178+
11701179
public final class TestSuite {
11711180
public init(_ name: String) {
11721181
self.name = name
@@ -1225,6 +1234,11 @@ public final class TestSuite {
12251234
f()
12261235
}
12271236
let test = _testByName(testName)
1237+
1238+
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
1239+
startTrackingObjects(name)
1240+
#endif
1241+
12281242
switch test.code {
12291243
case .single(let code):
12301244
precondition(
@@ -1234,6 +1248,11 @@ public final class TestSuite {
12341248
case .parameterized(code: let code, _):
12351249
code(parameter!)
12361250
}
1251+
1252+
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
1253+
_ = stopTrackingObjects(name)
1254+
#endif
1255+
12371256
if let f = _testTearDownCode {
12381257
f()
12391258
}

test/1_stdlib/Leak.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-run-simple-swift | FileCheck %s
2+
// REQUIRES: leak-checker
3+
4+
import StdlibUnittest
5+
6+
class VictimObject {
7+
var field1, field2: String
8+
var block: () -> () = {}
9+
10+
init(field1: String, field2: String) {
11+
self.field1 = field1
12+
self.field2 = field2
13+
self.block = {
14+
self.field1 = self.field1 + self.field2
15+
self.field2 = self.field2 + self.field1
16+
}
17+
}
18+
}
19+
20+
let LeaksTests = TestSuite("Leaks")
21+
22+
// CHECK: [ RUN ] Leaks.Known leak
23+
// CHECK: {"name":"Leaks", "swift_count": 2, "objc_count": 0, "swift_objects": [{"type": "nominal", "name": "C4main12VictimObject", "kind": "Class"},{"type": "unknown", "kind": "HeapLocalVariable"}], "objc_objects": []}
24+
// CHECK: [ OK ] Leaks.Known leak
25+
LeaksTests.test("Known leak") {
26+
_ = VictimObject(field1: "Leak", field2: "Checker")
27+
}
28+
29+
runAllTests()
30+

test/lit.site.cfg.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ if "@SWIFT_ASAN_BUILD@" == "TRUE":
4040
else:
4141
config.available_features.add('no_asan')
4242

43+
if "@SWIFT_RUNTIME_ENABLE_LEAK_CHECKER@" == "TRUE":
44+
config.available_features.add('leak-checker')
45+
4346
if '@SWIFT_TOOLS_ENABLE_LTO@'.lower() in ['full', 'thin']:
4447
config.available_features.add('lto')
4548
else:

0 commit comments

Comments
 (0)