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