1*94d3b452SApple OSS Distributions #include <darwintest.h>
2*94d3b452SApple OSS Distributions #include <darwintest_utils.h>
3*94d3b452SApple OSS Distributions #include <stdio.h>
4*94d3b452SApple OSS Distributions #include <assert.h>
5*94d3b452SApple OSS Distributions #include <setjmp.h>
6*94d3b452SApple OSS Distributions
7*94d3b452SApple OSS Distributions #define DEVELOPMENT 1
8*94d3b452SApple OSS Distributions #define DEBUG 0
9*94d3b452SApple OSS Distributions #define XNU_KERNEL_PRIVATE 1
10*94d3b452SApple OSS Distributions
11*94d3b452SApple OSS Distributions #define OS_REFCNT_DEBUG 1
12*94d3b452SApple OSS Distributions #define STRESS_TESTS 0
13*94d3b452SApple OSS Distributions
14*94d3b452SApple OSS Distributions #pragma clang diagnostic ignored "-Watomic-implicit-seq-cst"
15*94d3b452SApple OSS Distributions #pragma clang diagnostic ignored "-Wc++98-compat"
16*94d3b452SApple OSS Distributions
17*94d3b452SApple OSS Distributions void handle_panic(const char *func, char *str, ...);
18*94d3b452SApple OSS Distributions #define panic(...) handle_panic(__func__, __VA_ARGS__)
19*94d3b452SApple OSS Distributions
20*94d3b452SApple OSS Distributions #include "../libkern/os/refcnt.h"
21*94d3b452SApple OSS Distributions #include "../libkern/os/refcnt.c"
22*94d3b452SApple OSS Distributions
23*94d3b452SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
24*94d3b452SApple OSS Distributions
25*94d3b452SApple OSS Distributions /* import some of the refcnt internal state for testing */
26*94d3b452SApple OSS Distributions extern bool ref_debug_enable;
27*94d3b452SApple OSS Distributions os_refgrp_decl_extern(global_ref_group);
28*94d3b452SApple OSS Distributions
29*94d3b452SApple OSS Distributions T_GLOBAL_META(
30*94d3b452SApple OSS Distributions T_META_NAMESPACE("os_refcnt"),
31*94d3b452SApple OSS Distributions T_META_CHECK_LEAKS(false)
32*94d3b452SApple OSS Distributions );
33*94d3b452SApple OSS Distributions
34*94d3b452SApple OSS Distributions T_DECL(os_refcnt, "Basic atomic refcount")
35*94d3b452SApple OSS Distributions {
36*94d3b452SApple OSS Distributions struct os_refcnt rc;
37*94d3b452SApple OSS Distributions os_ref_init(&rc, NULL);
38*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "refcount correctly initialized");
39*94d3b452SApple OSS Distributions
40*94d3b452SApple OSS Distributions os_ref_retain(&rc);
41*94d3b452SApple OSS Distributions os_ref_retain(&rc);
42*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
43*94d3b452SApple OSS Distributions
44*94d3b452SApple OSS Distributions os_ref_count_t x = os_ref_release(&rc);
45*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
46*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
47*94d3b452SApple OSS Distributions
48*94d3b452SApple OSS Distributions os_ref_release_live(&rc);
49*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "release_live decreased count");
50*94d3b452SApple OSS Distributions
51*94d3b452SApple OSS Distributions x = os_ref_release(&rc);
52*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
53*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
54*94d3b452SApple OSS Distributions
55*94d3b452SApple OSS Distributions os_ref_init(&rc, NULL);
56*94d3b452SApple OSS Distributions T_ASSERT_TRUE(os_ref_retain_try(&rc), "try retained");
57*94d3b452SApple OSS Distributions
58*94d3b452SApple OSS Distributions (void)os_ref_release(&rc);
59*94d3b452SApple OSS Distributions (void)os_ref_release(&rc);
60*94d3b452SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "release");
61*94d3b452SApple OSS Distributions
62*94d3b452SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try(&rc), "try failed");
63*94d3b452SApple OSS Distributions }
64*94d3b452SApple OSS Distributions
65*94d3b452SApple OSS Distributions T_DECL(refcnt_raw, "Raw refcount")
66*94d3b452SApple OSS Distributions {
67*94d3b452SApple OSS Distributions os_ref_atomic_t rc;
68*94d3b452SApple OSS Distributions os_ref_init_raw(&rc, NULL);
69*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "refcount correctly initialized");
70*94d3b452SApple OSS Distributions
71*94d3b452SApple OSS Distributions os_ref_retain_raw(&rc, NULL);
72*94d3b452SApple OSS Distributions os_ref_retain_raw(&rc, NULL);
73*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
74*94d3b452SApple OSS Distributions
75*94d3b452SApple OSS Distributions os_ref_count_t x = os_ref_release_raw(&rc, NULL);
76*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
77*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
78*94d3b452SApple OSS Distributions
79*94d3b452SApple OSS Distributions os_ref_release_live_raw(&rc, NULL);
80*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "release_live decreased count");
81*94d3b452SApple OSS Distributions
82*94d3b452SApple OSS Distributions x = os_ref_release_raw(&rc, NULL);
83*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
84*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
85*94d3b452SApple OSS Distributions
86*94d3b452SApple OSS Distributions os_ref_init_raw(&rc, NULL);
87*94d3b452SApple OSS Distributions T_ASSERT_TRUE(os_ref_retain_try_raw(&rc, NULL), "try retained");
88*94d3b452SApple OSS Distributions
89*94d3b452SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
90*94d3b452SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
91*94d3b452SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "release");
92*94d3b452SApple OSS Distributions
93*94d3b452SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try_raw(&rc, NULL), "try failed");
94*94d3b452SApple OSS Distributions }
95*94d3b452SApple OSS Distributions
96*94d3b452SApple OSS Distributions T_DECL(refcnt_locked, "Locked refcount")
97*94d3b452SApple OSS Distributions {
98*94d3b452SApple OSS Distributions struct os_refcnt rc;
99*94d3b452SApple OSS Distributions os_ref_init(&rc, NULL);
100*94d3b452SApple OSS Distributions
101*94d3b452SApple OSS Distributions os_ref_retain_locked(&rc);
102*94d3b452SApple OSS Distributions os_ref_retain_locked(&rc);
103*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
104*94d3b452SApple OSS Distributions
105*94d3b452SApple OSS Distributions os_ref_count_t x = os_ref_release_locked(&rc);
106*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
107*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
108*94d3b452SApple OSS Distributions
109*94d3b452SApple OSS Distributions (void)os_ref_release_locked(&rc);
110*94d3b452SApple OSS Distributions x = os_ref_release_locked(&rc);
111*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
112*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
113*94d3b452SApple OSS Distributions }
114*94d3b452SApple OSS Distributions
115*94d3b452SApple OSS Distributions T_DECL(refcnt_raw_locked, "Locked raw refcount")
116*94d3b452SApple OSS Distributions {
117*94d3b452SApple OSS Distributions os_ref_atomic_t rc;
118*94d3b452SApple OSS Distributions os_ref_init_raw(&rc, NULL);
119*94d3b452SApple OSS Distributions
120*94d3b452SApple OSS Distributions os_ref_retain_locked_raw(&rc, NULL);
121*94d3b452SApple OSS Distributions os_ref_retain_locked_raw(&rc, NULL);
122*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
123*94d3b452SApple OSS Distributions
124*94d3b452SApple OSS Distributions os_ref_count_t x = os_ref_release_locked_raw(&rc, NULL);
125*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
126*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
127*94d3b452SApple OSS Distributions
128*94d3b452SApple OSS Distributions (void)os_ref_release_locked_raw(&rc, NULL);
129*94d3b452SApple OSS Distributions x = os_ref_release_locked_raw(&rc, NULL);
130*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
131*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
132*94d3b452SApple OSS Distributions }
133*94d3b452SApple OSS Distributions
134*94d3b452SApple OSS Distributions static void
do_bitwise_test(const os_ref_count_t bits)135*94d3b452SApple OSS Distributions do_bitwise_test(const os_ref_count_t bits)
136*94d3b452SApple OSS Distributions {
137*94d3b452SApple OSS Distributions os_ref_atomic_t rc;
138*94d3b452SApple OSS Distributions os_ref_count_t reserved = 0xaaaaaaaaU & ((1U << bits) - 1);
139*94d3b452SApple OSS Distributions
140*94d3b452SApple OSS Distributions T_LOG("do_bitwise_test(nbits:%d, reserved:%#x)", bits, reserved);
141*94d3b452SApple OSS Distributions
142*94d3b452SApple OSS Distributions os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
143*94d3b452SApple OSS Distributions
144*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "[%u bits] refcount initialized", bits);
145*94d3b452SApple OSS Distributions
146*94d3b452SApple OSS Distributions os_ref_retain_mask(&rc, bits, NULL);
147*94d3b452SApple OSS Distributions os_ref_retain_mask(&rc, bits, NULL);
148*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 3, "retain increased count");
149*94d3b452SApple OSS Distributions
150*94d3b452SApple OSS Distributions os_ref_count_t x = os_ref_release_mask(&rc, bits, NULL);
151*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
152*94d3b452SApple OSS Distributions
153*94d3b452SApple OSS Distributions os_ref_release_live_mask(&rc, bits, NULL);
154*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "release_live decreased count");
155*94d3b452SApple OSS Distributions
156*94d3b452SApple OSS Distributions x = os_ref_release_mask(&rc, bits, NULL);
157*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "released");
158*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
159*94d3b452SApple OSS Distributions
160*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(rc & ((1U << bits) - 1), reserved, "Reserved bits not modified");
161*94d3b452SApple OSS Distributions
162*94d3b452SApple OSS Distributions os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
163*94d3b452SApple OSS Distributions T_ASSERT_TRUE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try retained");
164*94d3b452SApple OSS Distributions if (reserved) {
165*94d3b452SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, reserved, NULL), "try reject");
166*94d3b452SApple OSS Distributions }
167*94d3b452SApple OSS Distributions
168*94d3b452SApple OSS Distributions (void)os_ref_release_mask(&rc, bits, NULL);
169*94d3b452SApple OSS Distributions (void)os_ref_release_mask(&rc, bits, NULL);
170*94d3b452SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "release");
171*94d3b452SApple OSS Distributions
172*94d3b452SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try fail");
173*94d3b452SApple OSS Distributions
174*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_bits_mask(&rc, bits), reserved, "Reserved bits not modified");
175*94d3b452SApple OSS Distributions }
176*94d3b452SApple OSS Distributions
177*94d3b452SApple OSS Distributions T_DECL(refcnt_bitwise, "Bitwise refcount")
178*94d3b452SApple OSS Distributions {
179*94d3b452SApple OSS Distributions do_bitwise_test(0);
180*94d3b452SApple OSS Distributions do_bitwise_test(1);
181*94d3b452SApple OSS Distributions do_bitwise_test(8);
182*94d3b452SApple OSS Distributions do_bitwise_test(26);
183*94d3b452SApple OSS Distributions
184*94d3b452SApple OSS Distributions os_ref_atomic_t rc = 0xaaaaaaaa;
185*94d3b452SApple OSS Distributions
186*94d3b452SApple OSS Distributions const os_ref_count_t nbits = 3;
187*94d3b452SApple OSS Distributions const os_ref_count_t count = 5;
188*94d3b452SApple OSS Distributions const os_ref_count_t bits = 7;
189*94d3b452SApple OSS Distributions os_ref_init_count_mask(&rc, nbits, NULL, count, bits);
190*94d3b452SApple OSS Distributions
191*94d3b452SApple OSS Distributions os_ref_count_t mask = (1U << nbits) - 1;
192*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(rc & mask, bits, "bits correctly initialized");
193*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(rc >> nbits, count, "count correctly initialized");
194*94d3b452SApple OSS Distributions }
195*94d3b452SApple OSS Distributions
196*94d3b452SApple OSS Distributions os_refgrp_decl(static, g1, "test group", NULL);
197*94d3b452SApple OSS Distributions os_refgrp_decl_extern(g1);
198*94d3b452SApple OSS Distributions
199*94d3b452SApple OSS Distributions T_DECL(refcnt_groups, "Group accounting")
200*94d3b452SApple OSS Distributions {
201*94d3b452SApple OSS Distributions #if OS_REFCNT_DEBUG
202*94d3b452SApple OSS Distributions ref_debug_enable = true;
203*94d3b452SApple OSS Distributions
204*94d3b452SApple OSS Distributions struct os_refcnt rc;
205*94d3b452SApple OSS Distributions os_ref_init(&rc, &g1);
206*94d3b452SApple OSS Distributions
207*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_children, 1, "group attached");
208*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(global_ref_group.grp_children, 1, "global group attached");
209*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_count, 1, "group count");
210*94d3b452SApple OSS Distributions T_ASSERT_EQ_ULLONG(g1.grp_retain_total, 1ULL, "group retains");
211*94d3b452SApple OSS Distributions T_ASSERT_EQ_ULLONG(g1.grp_release_total, 0ULL, "group releases");
212*94d3b452SApple OSS Distributions
213*94d3b452SApple OSS Distributions os_ref_retain(&rc);
214*94d3b452SApple OSS Distributions os_ref_retain(&rc);
215*94d3b452SApple OSS Distributions os_ref_release_live(&rc);
216*94d3b452SApple OSS Distributions os_ref_release_live(&rc);
217*94d3b452SApple OSS Distributions
218*94d3b452SApple OSS Distributions T_EXPECT_EQ_ULLONG(g1.grp_retain_total, 3ULL, "group retains");
219*94d3b452SApple OSS Distributions T_EXPECT_EQ_ULLONG(g1.grp_release_total, 2ULL, "group releases");
220*94d3b452SApple OSS Distributions
221*94d3b452SApple OSS Distributions os_ref_count_t x = os_ref_release(&rc);
222*94d3b452SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(x, 0, "released");
223*94d3b452SApple OSS Distributions
224*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_children, 0, "group detatched");
225*94d3b452SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_count, 0, "group count");
226*94d3b452SApple OSS Distributions #else
227*94d3b452SApple OSS Distributions T_SKIP("Refcount debugging disabled");
228*94d3b452SApple OSS Distributions #endif
229*94d3b452SApple OSS Distributions }
230*94d3b452SApple OSS Distributions
231*94d3b452SApple OSS Distributions enum {
232*94d3b452SApple OSS Distributions OSREF_UNDERFLOW = 1,
233*94d3b452SApple OSS Distributions OSREF_OVERFLOW = 2,
234*94d3b452SApple OSS Distributions OSREF_RETAIN = 3,
235*94d3b452SApple OSS Distributions OSREF_DEALLOC_LIVE = 4,
236*94d3b452SApple OSS Distributions };
237*94d3b452SApple OSS Distributions
238*94d3b452SApple OSS Distributions static jmp_buf jb;
239*94d3b452SApple OSS Distributions static bool expect_panic = false;
240*94d3b452SApple OSS Distributions
241*94d3b452SApple OSS Distributions void
handle_panic(const char * func,char * __unused str,...)242*94d3b452SApple OSS Distributions handle_panic(const char *func, char *__unused str, ...)
243*94d3b452SApple OSS Distributions {
244*94d3b452SApple OSS Distributions int ret = -1;
245*94d3b452SApple OSS Distributions if (!expect_panic) {
246*94d3b452SApple OSS Distributions T_FAIL("unexpected panic from %s", func);
247*94d3b452SApple OSS Distributions T_LOG("corrupt program state, aborting");
248*94d3b452SApple OSS Distributions abort();
249*94d3b452SApple OSS Distributions }
250*94d3b452SApple OSS Distributions expect_panic = false;
251*94d3b452SApple OSS Distributions
252*94d3b452SApple OSS Distributions if (strcmp(func, "os_ref_panic_underflow") == 0) {
253*94d3b452SApple OSS Distributions ret = OSREF_UNDERFLOW;
254*94d3b452SApple OSS Distributions } else if (strcmp(func, "os_ref_panic_overflow") == 0) {
255*94d3b452SApple OSS Distributions ret = OSREF_OVERFLOW;
256*94d3b452SApple OSS Distributions } else if (strcmp(func, "os_ref_panic_retain") == 0) {
257*94d3b452SApple OSS Distributions ret = OSREF_RETAIN;
258*94d3b452SApple OSS Distributions } else if (strcmp(func, "os_ref_panic_live") == 0) {
259*94d3b452SApple OSS Distributions ret = OSREF_DEALLOC_LIVE;
260*94d3b452SApple OSS Distributions } else {
261*94d3b452SApple OSS Distributions T_LOG("unexpected panic from %s", func);
262*94d3b452SApple OSS Distributions }
263*94d3b452SApple OSS Distributions
264*94d3b452SApple OSS Distributions longjmp(jb, ret);
265*94d3b452SApple OSS Distributions }
266*94d3b452SApple OSS Distributions
267*94d3b452SApple OSS Distributions T_DECL(refcnt_underflow, "Underflow")
268*94d3b452SApple OSS Distributions {
269*94d3b452SApple OSS Distributions os_ref_atomic_t rc;
270*94d3b452SApple OSS Distributions os_ref_init_raw(&rc, NULL);
271*94d3b452SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
272*94d3b452SApple OSS Distributions
273*94d3b452SApple OSS Distributions int x = setjmp(jb);
274*94d3b452SApple OSS Distributions if (x == 0) {
275*94d3b452SApple OSS Distributions expect_panic = true;
276*94d3b452SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
277*94d3b452SApple OSS Distributions T_FAIL("underflow not caught");
278*94d3b452SApple OSS Distributions } else {
279*94d3b452SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_UNDERFLOW, "underflow caught");
280*94d3b452SApple OSS Distributions }
281*94d3b452SApple OSS Distributions }
282*94d3b452SApple OSS Distributions
283*94d3b452SApple OSS Distributions T_DECL(refcnt_overflow, "Overflow")
284*94d3b452SApple OSS Distributions {
285*94d3b452SApple OSS Distributions os_ref_atomic_t rc;
286*94d3b452SApple OSS Distributions os_ref_init_count_raw(&rc, NULL, 0x0fffffffU);
287*94d3b452SApple OSS Distributions
288*94d3b452SApple OSS Distributions int x = setjmp(jb);
289*94d3b452SApple OSS Distributions if (x == 0) {
290*94d3b452SApple OSS Distributions expect_panic = true;
291*94d3b452SApple OSS Distributions (void)os_ref_retain_raw(&rc, NULL);
292*94d3b452SApple OSS Distributions T_FAIL("overflow not caught");
293*94d3b452SApple OSS Distributions } else {
294*94d3b452SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_RETAIN, "overflow caught");
295*94d3b452SApple OSS Distributions }
296*94d3b452SApple OSS Distributions }
297*94d3b452SApple OSS Distributions
298*94d3b452SApple OSS Distributions T_DECL(refcnt_resurrection, "Resurrection")
299*94d3b452SApple OSS Distributions {
300*94d3b452SApple OSS Distributions os_ref_atomic_t rc;
301*94d3b452SApple OSS Distributions os_ref_init_raw(&rc, NULL);
302*94d3b452SApple OSS Distributions os_ref_count_t n = os_ref_release_raw(&rc, NULL);
303*94d3b452SApple OSS Distributions
304*94d3b452SApple OSS Distributions T_QUIET; T_EXPECT_EQ_UINT(n, 0, "reference not released");
305*94d3b452SApple OSS Distributions
306*94d3b452SApple OSS Distributions int x = setjmp(jb);
307*94d3b452SApple OSS Distributions if (x == 0) {
308*94d3b452SApple OSS Distributions expect_panic = true;
309*94d3b452SApple OSS Distributions (void)os_ref_retain_raw(&rc, NULL);
310*94d3b452SApple OSS Distributions T_FAIL("resurrection not caught");
311*94d3b452SApple OSS Distributions } else {
312*94d3b452SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_RETAIN, "resurrection caught");
313*94d3b452SApple OSS Distributions }
314*94d3b452SApple OSS Distributions }
315*94d3b452SApple OSS Distributions
316*94d3b452SApple OSS Distributions T_DECL(refcnt_dealloc_live, "Dealloc expected live object")
317*94d3b452SApple OSS Distributions {
318*94d3b452SApple OSS Distributions os_ref_atomic_t rc;
319*94d3b452SApple OSS Distributions os_ref_init_raw(&rc, NULL);
320*94d3b452SApple OSS Distributions
321*94d3b452SApple OSS Distributions expect_panic = true;
322*94d3b452SApple OSS Distributions int x = setjmp(jb);
323*94d3b452SApple OSS Distributions if (x == 0) {
324*94d3b452SApple OSS Distributions expect_panic = true;
325*94d3b452SApple OSS Distributions os_ref_release_live_raw(&rc, NULL);
326*94d3b452SApple OSS Distributions T_FAIL("dealloc live not caught");
327*94d3b452SApple OSS Distributions } else {
328*94d3b452SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_DEALLOC_LIVE, "dealloc live caught");
329*94d3b452SApple OSS Distributions }
330*94d3b452SApple OSS Distributions }
331*94d3b452SApple OSS Distributions
332*94d3b452SApple OSS Distributions T_DECL(refcnt_initializer, "Static intializers")
333*94d3b452SApple OSS Distributions {
334*94d3b452SApple OSS Distributions struct os_refcnt rc = OS_REF_INITIALIZER;
335*94d3b452SApple OSS Distributions os_ref_atomic_t rca = OS_REF_ATOMIC_INITIALIZER;
336*94d3b452SApple OSS Distributions
337*94d3b452SApple OSS Distributions T_ASSERT_EQ_INT(0, os_ref_retain_try(&rc), NULL);
338*94d3b452SApple OSS Distributions T_ASSERT_EQ_INT(0, os_ref_get_count_raw(&rca), NULL);
339*94d3b452SApple OSS Distributions }
340*94d3b452SApple OSS Distributions
341*94d3b452SApple OSS Distributions #if STRESS_TESTS
342*94d3b452SApple OSS Distributions
343*94d3b452SApple OSS Distributions static const unsigned long iters = 1024 * 1024 * 32;
344*94d3b452SApple OSS Distributions
345*94d3b452SApple OSS Distributions static void *
func(void * _rc)346*94d3b452SApple OSS Distributions func(void *_rc)
347*94d3b452SApple OSS Distributions {
348*94d3b452SApple OSS Distributions struct os_refcnt *rc = _rc;
349*94d3b452SApple OSS Distributions for (unsigned long i = 0; i < iters; i++) {
350*94d3b452SApple OSS Distributions os_ref_retain(rc);
351*94d3b452SApple OSS Distributions os_ref_release_live(rc);
352*94d3b452SApple OSS Distributions }
353*94d3b452SApple OSS Distributions return NULL;
354*94d3b452SApple OSS Distributions }
355*94d3b452SApple OSS Distributions
356*94d3b452SApple OSS Distributions T_DECL(refcnt_stress, "Stress test")
357*94d3b452SApple OSS Distributions {
358*94d3b452SApple OSS Distributions pthread_t th1, th2;
359*94d3b452SApple OSS Distributions
360*94d3b452SApple OSS Distributions struct os_refcnt rc;
361*94d3b452SApple OSS Distributions os_ref_init(&rc, NULL);
362*94d3b452SApple OSS Distributions
363*94d3b452SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&th1, NULL, func, &rc), "pthread_create");
364*94d3b452SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&th2, NULL, func, &rc), "pthread_create");
365*94d3b452SApple OSS Distributions
366*94d3b452SApple OSS Distributions void *r1, *r2;
367*94d3b452SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(th1, &r1), "pthread_join");
368*94d3b452SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(th2, &r2), "pthread_join");
369*94d3b452SApple OSS Distributions
370*94d3b452SApple OSS Distributions os_ref_count_t x = os_ref_release(&rc);
371*94d3b452SApple OSS Distributions T_ASSERT_EQ_INT(x, 0, "Consistent refcount");
372*94d3b452SApple OSS Distributions }
373*94d3b452SApple OSS Distributions
374*94d3b452SApple OSS Distributions #endif
375