forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongRefcounting.cpp
1092 lines (856 loc) · 29.6 KB
/
LongRefcounting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- LongRefcounting.cpp - Slow reference-counting tests for Swift ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include <functional>
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Demangling/ManglingMacros.h"
#include "gtest/gtest.h"
#ifdef __APPLE__
// FIXME: is EXPECT_UNALLOCATED reliable enough for CI?
// EXPECT_ALLOCATED may fail falsely if the memory is re-allocated.
# include <malloc/malloc.h>
# define EXPECT_ALLOCATED(p) EXPECT_NE(0u, malloc_size(p))
# define EXPECT_UNALLOCATED(p) EXPECT_EQ(0u, malloc_size(p))
#else
// FIXME: heap assertion for other platforms?
# define EXPECT_ALLOCATED(p) (void)(p)
# define EXPECT_UNALLOCATED(p) (void)(p)
#endif
using namespace swift;
struct TestObject : HeapObject {
// *Addr = Value during deinit
size_t *Addr;
size_t Value;
// Check lifecycle state DEINITING during deinit
bool CheckLifecycle;
// Weak variable to check in CheckLifecycle. nullptr skips the check.
// On entry to deinit: must point to object
// On exit from deinit: is destroyed
WeakReference *WeakRef;
// Callback invoked during the object's deinit.
std::function<void()> DeinitCallback;
TestObject(size_t *addr, size_t value)
: Addr(addr), Value(value), CheckLifecycle(false), WeakRef(nullptr), DeinitCallback(nullptr)
{ }
};
static SWIFT_CC(swift) void deinitTestObject(SWIFT_CONTEXT HeapObject *_object) {
auto object = static_cast<TestObject*>(_object);
assert(object->Addr && "object already deallocated");
if (object->CheckLifecycle) {
// RC ok
swift_retain(object);
swift_retain(object);
swift_release(object);
swift_release(object);
// FIXME: RC underflow during deinit?
// URC load crashes
// URC increment OK
// URC decrement OK
ASSERT_DEATH(swift_unownedCheck(object),
"Attempted to read an unowned reference");
swift_unownedRetain(object);
swift_unownedRetain(object);
swift_unownedRelease(object);
swift_unownedRelease(object);
if (object->WeakRef) {
// WRC load is nil
// WRC increment is nil
// WRC decrement OK
// WRC -1
auto weak_value = swift_weakLoadStrong(object->WeakRef);
EXPECT_EQ(nullptr, weak_value);
swift_weakDestroy(object->WeakRef);
// WRC no change
swift_weakInit(object->WeakRef, object);
weak_value = swift_weakLoadStrong(object->WeakRef);
EXPECT_EQ(nullptr, weak_value);
// WRC no change
swift_weakInit(object->WeakRef, object);
weak_value = swift_weakLoadStrong(object->WeakRef);
EXPECT_EQ(nullptr, weak_value);
}
}
if (object->DeinitCallback != nullptr) {
object->DeinitCallback();
}
*object->Addr = object->Value;
object->Addr = nullptr;
object->~TestObject();
swift_deallocObject(object, sizeof(TestObject), alignof(TestObject) - 1);
}
static const FullMetadata<ClassMetadata> TestClassObjectMetadata = {
{ { &deinitTestObject }, { &VALUE_WITNESS_SYM(Bo) } },
{ { nullptr }, ClassFlags::UsesSwiftRefcounting, 0, 0, 0, 0, 0, 0 }
};
/// Create an object that, when deinited, stores the given value to
/// the given pointer.
static TestObject *allocTestObject(size_t *addr, size_t value) {
auto buf = swift_allocObject(&TestClassObjectMetadata,
sizeof(TestObject),
alignof(TestObject) - 1);
return new (buf) TestObject(addr, value);
}
///////////////////////////////////////////////////
// Max strong retain count and overflow checking //
///////////////////////////////////////////////////
template <bool atomic>
static void retainALot(TestObject *object, size_t &deinited,
uint64_t count) {
for (uint64_t i = 0; i < count; i++) {
if (atomic) swift_retain(object);
else swift_nonatomic_retain(object);
EXPECT_EQ(0u, deinited);
}
}
template <bool atomic>
static void releaseALot(TestObject *object, size_t &deinited,
uint64_t count) {
for (uint64_t i = 0; i < count; i++) {
if (atomic) swift_release(object);
else swift_nonatomic_release(object);
EXPECT_EQ(0u, deinited);
}
}
// Maximum legal retain count.
// 32-2 bits of extra retain count, plus 1 for the implicit retain.
const uint64_t maxRC = 1ULL << (32 - 2);
TEST(LongRefcountingTest, retain_max) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1.
// Retain to maxRC, release back to 1, then release and verify deallocation.
EXPECT_EQ(swift_retainCount(object), 1u);
retainALot<true>(object, deinited, maxRC - 1);
EXPECT_EQ(swift_retainCount(object), maxRC);
releaseALot<true>(object, deinited, maxRC - 1);
EXPECT_EQ(swift_retainCount(object), 1u);
EXPECT_EQ(0u, deinited);
swift_release(object);
EXPECT_EQ(1u, deinited);
}
TEST(LongRefcountingTest, retain_overflow_DeathTest) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1. Retain to maxRC, then retain again and verify overflow error.
retainALot<true>(object, deinited, maxRC - 1);
EXPECT_EQ(0u, deinited);
ASSERT_DEATH(swift_retain(object),
"Object was retained too many times");
}
TEST(LongRefcountingTest, nonatomic_retain_max) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1.
// Retain to maxRC, release back to 1, then release and verify deallocation.
EXPECT_EQ(swift_retainCount(object), 1u);
retainALot<false>(object, deinited, maxRC - 1);
EXPECT_EQ(swift_retainCount(object), maxRC);
releaseALot<false>(object, deinited, maxRC - 1);
EXPECT_EQ(swift_retainCount(object), 1u);
EXPECT_EQ(0u, deinited);
swift_nonatomic_release(object);
EXPECT_EQ(1u, deinited);
}
TEST(LongRefcountingTest, nonatomic_retain_overflow_DeathTest) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1. Retain to maxRC, then retain again and verify overflow error.
retainALot<false>(object, deinited, maxRC - 1);
EXPECT_EQ(0u, deinited);
ASSERT_DEATH(swift_nonatomic_retain(object),
"Object was retained too many times");
}
///////////////////////////////////////////////////
// Max unowned retain count and overflow checking //
///////////////////////////////////////////////////
template <bool atomic>
static void unownedRetainALot(TestObject *object, uint64_t count) {
for (uint64_t i = 0; i < count; i++) {
if (atomic) swift_unownedRetain(object);
else swift_nonatomic_unownedRetain(object);
EXPECT_ALLOCATED(object);
}
}
template <bool atomic>
static void unownedReleaseALot(TestObject *object, uint64_t count) {
for (uint64_t i = 0; i < count; i++) {
if (atomic) swift_unownedRelease(object);
else swift_nonatomic_unownedRelease(object);
EXPECT_ALLOCATED(object);
}
}
// Maximum legal unowned retain count. 31 bits minus one with no implicit +1.
const uint64_t maxURC = (1ULL << (32 - 1)) - 2;
TEST(LongRefcountingTest, unowned_retain_max) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1. URC is 1.
// Unowned-retain to maxURC.
// Release and verify deinited and not deallocated.
// Unowned-release back to 1, then unowned-release and verify deallocated.
EXPECT_EQ(swift_retainCount(object), 1u);
EXPECT_EQ(swift_unownedRetainCount(object), 1u);
unownedRetainALot<true>(object, maxURC - 1);
EXPECT_EQ(swift_unownedRetainCount(object), maxURC);
EXPECT_EQ(0u, deinited);
EXPECT_ALLOCATED(object);
swift_release(object);
EXPECT_EQ(1u, deinited);
EXPECT_ALLOCATED(object);
// Strong release decremented unowned count by 1.
EXPECT_EQ(swift_unownedRetainCount(object), maxURC - 1);
unownedReleaseALot<true>(object, maxURC - 2);
EXPECT_EQ(swift_unownedRetainCount(object), 1u);
EXPECT_ALLOCATED(object);
swift_unownedRelease(object);
EXPECT_UNALLOCATED(object);
}
TEST(LongRefcountingTest, unowned_retain_overflow_DeathTest) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// URC is 1. Retain to maxURC, then retain again and verify overflow error.
unownedRetainALot<true>(object, maxURC);
EXPECT_EQ(0u, deinited);
EXPECT_ALLOCATED(object);
ASSERT_DEATH(swift_unownedRetain(object),
"Object's unowned reference was retained too many times");
}
TEST(LongRefcountingTest, nonatomic_unowned_retain_max) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1. URC is 1.
// Unowned-retain to maxURC.
// Release and verify deinited and not deallocated.
// Unowned-release back to 1, then unowned-release and verify deallocated.
EXPECT_EQ(swift_retainCount(object), 1u);
EXPECT_EQ(swift_unownedRetainCount(object), 1u);
unownedRetainALot<false>(object, maxURC - 1);
EXPECT_EQ(swift_unownedRetainCount(object), maxURC);
EXPECT_EQ(0u, deinited);
EXPECT_ALLOCATED(object);
swift_release(object);
EXPECT_EQ(1u, deinited);
EXPECT_ALLOCATED(object);
// Strong release decremented unowned count by 1.
EXPECT_EQ(swift_unownedRetainCount(object), maxURC - 1);
unownedReleaseALot<false>(object, maxURC - 2);
EXPECT_EQ(swift_unownedRetainCount(object), 1u);
EXPECT_ALLOCATED(object);
swift_unownedRelease(object);
EXPECT_UNALLOCATED(object);
}
TEST(LongRefcountingTest, nonatomic_unowned_retain_overflow_DeathTest) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// URC is 1. Retain to maxURC, then retain again and verify overflow error.
unownedRetainALot<false>(object, maxURC);
EXPECT_EQ(0u, deinited);
EXPECT_ALLOCATED(object);
ASSERT_DEATH(swift_nonatomic_unownedRetain(object),
"Object's unowned reference was retained too many times");
}
/////////////////////////////////////////////////
// Max weak retain count and overflow checking //
/////////////////////////////////////////////////
static HeapObjectSideTableEntry *weakRetainALot(TestObject *object, uint64_t count) {
if (count == 0) return nullptr;
auto side = object->refCounts.formWeakReference();
for (uint64_t i = 1; i < count; i++) {
side = side->incrementWeak();
EXPECT_ALLOCATED(object);
}
return side;
}
template <bool atomic>
static void weakReleaseALot(HeapObjectSideTableEntry *side, uint64_t count) {
for (uint64_t i = 0; i < count; i++) {
if (atomic) side->decrementWeak();
else side->decrementWeakNonAtomic();
}
}
// Maximum legal weak retain count. 32 bits with no implicit +1.
const uint64_t maxWRC = (1ULL << 32) - 1;
TEST(LongRefcountingTest, weak_retain_max) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1. WRC is 1.
// Weak-retain to maxWRC.
// Release and verify deallocated object and live side table.
// Weak-release back to 1, then weak-release and verify deallocated.
EXPECT_EQ(swift_retainCount(object), 1u);
EXPECT_EQ(object->refCounts.getWeakCount(), 1u);
auto side = weakRetainALot(object, maxWRC - 1);
EXPECT_EQ(side->getWeakCount(), maxWRC);
EXPECT_EQ(0u, deinited);
EXPECT_ALLOCATED(object);
EXPECT_ALLOCATED(side);
swift_release(object);
EXPECT_EQ(1u, deinited);
EXPECT_UNALLOCATED(object);
EXPECT_ALLOCATED(side);
weakReleaseALot<true>(side, maxWRC - 2);
EXPECT_EQ(side->getWeakCount(), 1u);
EXPECT_ALLOCATED(side);
side->decrementWeak();
EXPECT_UNALLOCATED(side);
}
TEST(LongRefcountingTest, weak_retain_overflow_DeathTest) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// URC is 1. Retain to maxURC, then retain again and verify overflow error.
weakRetainALot(object, maxWRC - 1);
EXPECT_EQ(0u, deinited);
EXPECT_ALLOCATED(object);
ASSERT_DEATH(weakRetainALot(object, 1),
"Object's weak reference was retained too many times");
}
TEST(LongRefcountingTest, nonatomic_weak_retain_max) {
// Don't generate millions of failures if something goes wrong.
::testing::FLAGS_gtest_break_on_failure = true;
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
// RC is 1. WRC is 1.
// Weak-retain to maxWRC.
// Release and verify deallocated object and live side table.
// Weak-release back to 1, then weak-release and verify deallocated.
EXPECT_EQ(swift_retainCount(object), 1u);
EXPECT_EQ(object->refCounts.getWeakCount(), 1u);
auto side = weakRetainALot(object, maxWRC - 1);
EXPECT_EQ(side->getWeakCount(), maxWRC);
EXPECT_EQ(0u, deinited);
EXPECT_ALLOCATED(object);
EXPECT_ALLOCATED(side);
swift_release(object);
EXPECT_EQ(1u, deinited);
EXPECT_UNALLOCATED(object);
EXPECT_ALLOCATED(side);
weakReleaseALot<false>(side, maxWRC - 2);
EXPECT_EQ(side->getWeakCount(), 1u);
EXPECT_ALLOCATED(side);
side->decrementWeak();
EXPECT_UNALLOCATED(side);
}
//////////////////////
// Object lifecycle //
//////////////////////
// FIXME: use the real WeakReference definition
namespace swift {
class WeakReference {
uintptr_t value;
public:
void *getSideTable() {
return (void*)(value & ~3ULL);
}
};
} // namespace swift
// Lifecycle paths. One test each.
//
// LIVE -> DEINITING -> DEAD, no side table
// LIVE -> DEINITING -> DEINITED -> DEAD, no side table
//
// LIVE -> DEINITING -> DEAD, with side table
// LIVE -> DEINITING -> DEINITED -> DEAD, with side table
// LIVE -> DEINITING -> FREED -> DEAD, with side table
// LIVE -> DEINITING -> DEINITED -> FREED -> DEAD, with side table
// LIVE -> DEINITING -> DEAD, no side table
TEST(LongRefcountingTest, lifecycle_live_deiniting_no_side_DeathTest) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
object->CheckLifecycle = true;
// Object is LIVE
EXPECT_ALLOCATED(object);
// RC tested elsewhere
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// WRC load can't happen
// WRC increment adds side table which is tested elsewhere
// WRC decrement can't happen
// RC == 1
// URC == 1
// WRC == 1
swift_release(object); // DEINITING is in here
// Object is DEAD
// RC == 0
// URC == 0
// WRC == 0
EXPECT_UNALLOCATED(object);
}
// LIVE -> DEINITING -> DEINITED -> DEAD, no side table
TEST(LongRefcountingTest, lifecycle_live_deiniting_deinited_no_side_DeathTest) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
object->CheckLifecycle = true;
// Object is LIVE
EXPECT_ALLOCATED(object);
// RC tested elsewhere
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// WRC load can't happen
// WRC increment adds side table which is tested elsewhere
// WRC decrement can't happen
// RC == 1
// URC == 3
// WRC == 1
swift_release(object); // DEINITING is in here
// Object is DEINITED
// RC == 0
// URC == 2
// WRC == 1
EXPECT_EQ(1u, deinited);
EXPECT_ALLOCATED(object);
// RC can't happen
// WRC can't happen
// URC load crashes
// URC increment can't happen
// URC decrement OK
ASSERT_DEATH(swift_unownedCheck(object),
"Attempted to read an unowned reference");
swift_unownedRelease(object);
EXPECT_ALLOCATED(object);
// RC == 0
// URC == 1
// WRC == 1
swift_unownedRelease(object);
// Object is DEAD
// RC == 0
// URC == 0
// WRC == 0
EXPECT_UNALLOCATED(object);
}
// LIVE -> DEINITING -> DEAD, with side table
TEST(LongRefcountingTest, lifecycle_live_deiniting_with_side_DeathTest) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
object->CheckLifecycle = true;
// Object is LIVE
EXPECT_ALLOCATED(object);
// RC tested elsewhere
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// Remaining releases are performed after the side table is allocated.
// WRC load can't happen
// WRC increment adds side table
// WRC decrement can't happen
WeakReference w;
swift_weakInit(&w, object);
// Object is LIVE with side table
void *side = w.getSideTable();
EXPECT_ALLOCATED(side);
WeakReference w_deinit;
swift_weakInit(&w_deinit, object);
object->WeakRef = &w_deinit;
// destroyed during deinit
// RC increment ok
// RC decrement ok
swift_retain(object);
swift_retain(object);
swift_retain(object);
swift_release(object);
swift_release(object);
swift_release(object);
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// ...and balancing from previously...
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// WRC load OK
// WRC increment OK
// WRC decrement OK
WeakReference w2;
swift_weakInit(&w2, object);
HeapObject *weakValue = swift_weakTakeStrong(&w2);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
weakValue = swift_weakTakeStrong(&w);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
// RC == 1
// URC == 1
// WRC == 1
swift_release(object); // DEINITING is in here
// Object is DEAD
// RC == 0
// URC == 0
// WRC == 0
EXPECT_UNALLOCATED(side);
EXPECT_UNALLOCATED(object);
}
// LIVE -> DEINITING -> DEINITED -> DEAD, with side table
TEST(LongRefcountingTest, lifecycle_live_deiniting_deinited_with_side_DeathTest) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
object->CheckLifecycle = true;
// Object is LIVE
EXPECT_ALLOCATED(object);
// RC tested elsewhere
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// Remaining releases are performed during DEINITED.
// WRC load can't happen
// WRC increment adds side table
// WRC decrement can't happen
WeakReference w;
swift_weakInit(&w, object);
// Object is LIVE with side table
void *side = w.getSideTable();
EXPECT_ALLOCATED(side);
WeakReference w_deinit;
swift_weakInit(&w_deinit, object);
object->WeakRef = &w_deinit;
// destroyed during deinit
// RC increment ok
// RC decrement ok
swift_retain(object);
swift_retain(object);
swift_retain(object);
swift_release(object);
swift_release(object);
swift_release(object);
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// WRC load OK
// WRC increment OK
// WRC decrement OK
WeakReference w2;
swift_weakInit(&w2, object);
HeapObject *weakValue = swift_weakTakeStrong(&w2);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
weakValue = swift_weakTakeStrong(&w);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
// RC == 1
// URC == 3
// WRC == 1
swift_release(object); // DEINITING is in here
// Object is DEINITED
// RC == 0
// URC == 2
// WRC == 1
EXPECT_EQ(1u, deinited);
EXPECT_ALLOCATED(object);
EXPECT_ALLOCATED(side);
// RC can't happen
// WRC can't happen
// URC load crashes
// URC increment can't happen
// URC decrement OK
ASSERT_DEATH(swift_unownedCheck(object),
"Attempted to read an unowned reference");
swift_unownedRelease(object);
EXPECT_ALLOCATED(object);
EXPECT_ALLOCATED(side);
// RC == 0
// URC == 1
// WRC == 1
swift_unownedRelease(object);
// Object is DEAD
// RC == 0
// URC == 0
// WRC == 0
EXPECT_UNALLOCATED(object);
EXPECT_UNALLOCATED(side);
}
// LIVE -> DEINITING -> FREED -> DEAD, with side table
TEST(LongRefcountingTest, lifecycle_live_deiniting_freed_with_side_DeathTest) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
object->CheckLifecycle = true;
// Object is LIVE
EXPECT_ALLOCATED(object);
// RC tested elsewhere
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// WRC load can't happen
// WRC increment adds side table
// WRC decrement can't happen
WeakReference w;
swift_weakInit(&w, object);
// Object is LIVE with side table
void *side = w.getSideTable();
EXPECT_ALLOCATED(side);
WeakReference w_deinit;
swift_weakInit(&w_deinit, object);
object->WeakRef = &w_deinit;
// destroyed during deinit
// RC increment ok
// RC decrement ok
swift_retain(object);
swift_retain(object);
swift_retain(object);
swift_release(object);
swift_release(object);
swift_release(object);
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// WRC load OK
// WRC increment OK
// WRC decrement OK
WeakReference w2;
swift_weakInit(&w2, object);
HeapObject *weakValue = swift_weakLoadStrong(&w2);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
weakValue = swift_weakLoadStrong(&w);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
// RC == 1
// URC == 1
// WRC == 3
swift_release(object); // DEINITING is in here
// Object is FREED
// RC == 0
// URC == 0
// WRC == 2
EXPECT_EQ(1u, deinited);
EXPECT_UNALLOCATED(object);
EXPECT_ALLOCATED(side);
// RC can't happen
// URC can't happen
// WRC load is nil
// WRC increment can't happen
// WRC decrement OK
weakValue = swift_weakTakeStrong(&w2);
EXPECT_EQ(0, weakValue);
// RC == 0
// URC == 0
// WRC == 1
weakValue = swift_weakTakeStrong(&w);
// Object is DEAD
// RC == 0
// URC == 0
// WRC == 0
EXPECT_UNALLOCATED(side);
EXPECT_EQ(0, weakValue);
}
// LIVE -> DEINITING -> DEINITED -> FREED -> DEAD, with side table
TEST(LongRefcountingTest, lifecycle_live_deiniting_deinited_freed_with_side_DeathTest) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
size_t deinited = 0;
auto object = allocTestObject(&deinited, 1);
object->CheckLifecycle = true;
// Object is LIVE
EXPECT_ALLOCATED(object);
// RC tested elsewhere
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// Remaining releases are performed during DEINITED.
// WRC load can't happen
// WRC increment adds side table
// WRC decrement can't happen
WeakReference w;
swift_weakInit(&w, object);
// Object is LIVE with side table
void *side = w.getSideTable();
EXPECT_ALLOCATED(side);
WeakReference w_deinit;
swift_weakInit(&w_deinit, object);
object->WeakRef = &w_deinit;
// destroyed during deinit
// RC increment ok
// RC decrement ok
swift_retain(object);
swift_retain(object);
swift_retain(object);
swift_release(object);
swift_release(object);
swift_release(object);
// URC load OK
// URC increment OK
// URC decrement OK
swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRetain(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
swift_unownedRelease(object); swift_unownedCheck(object);
// WRC load OK
// WRC increment OK
// WRC decrement OK
WeakReference w2;
swift_weakInit(&w2, object);
HeapObject *weakValue = swift_weakLoadStrong(&w2);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
weakValue = swift_weakLoadStrong(&w);
EXPECT_EQ(weakValue, object);
swift_release(weakValue);
// RC == 1
// URC == 3
// WRC == 3
swift_release(object); // DEINITING is in here
// Object is DEINITED
// RC == 0
// URC == 2
// WRC == 3
EXPECT_EQ(1u, deinited);
EXPECT_ALLOCATED(object);
EXPECT_ALLOCATED(side);