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