Skip to content

Commit e2c766e

Browse files
committed
[Runtime] Fatal error if self escapes from deinit.
When deallocating a class instance, we check the retain count of the instance and error if it's greater than 1. Self is allowed to be temporarily passed to other code from deinit, but there's no way to extend the lifetime of the object. Retaining it no longer extensd the lifetime. If self escapes from deinit, the result is a dangling pointer and eventual crash. Instead of crashing randomly due to a dangling pointer, crash deliberately when destroying an object that has escaped. rdar://93848484
1 parent eafba9b commit e2c766e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

stdlib/public/runtime/HeapObject.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,10 @@ void swift::swift_rootObjCDealloc(HeapObject *self) {
705705
void swift::swift_deallocClassInstance(HeapObject *object,
706706
size_t allocatedSize,
707707
size_t allocatedAlignMask) {
708+
size_t retainCount = swift_retainCount(object);
709+
if (SWIFT_UNLIKELY(retainCount > 1))
710+
swift::fatalError(0, "Object %p deallocated with retain count %zd, reference may have escaped from deinit.", object, retainCount);
711+
708712
#if SWIFT_OBJC_INTEROP
709713
// We need to let the ObjC runtime clean up any associated objects or weak
710714
// references associated with this object.
@@ -713,6 +717,7 @@ void swift::swift_deallocClassInstance(HeapObject *object,
713717
#else
714718
const bool fastDeallocSupported = true;
715719
#endif
720+
716721
if (!fastDeallocSupported || !object->refCounts.getPureSwiftDeallocation()) {
717722
objc_destructInstance((id)object);
718723
}

test/Runtime/deinit_escape.swift

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %target-run-simple-swift
2+
3+
// REQUIRES: executable_test
4+
// UNSUPPORTED: use_os_stdlib
5+
// UNSUPPORTED: back_deployment_runtime
6+
7+
import StdlibUnittest
8+
9+
var DeinitEscapeTestSuite = TestSuite("DeinitEscape")
10+
11+
var globalObjects: [AnyObject] = []
12+
13+
DeinitEscapeTestSuite.test("deinit escapes self") {
14+
expectCrashLater()
15+
16+
class C {
17+
deinit {
18+
globalObjects.append(self)
19+
}
20+
}
21+
_ = C()
22+
23+
expectUnreachable()
24+
}
25+
26+
runAllTests()

0 commit comments

Comments
 (0)