1 #include <darwintest.h>
2 #include <darwintest_utils.h>
9 #define XNU_KERNEL_PRIVATE 1
11 #define OS_REFCNT_DEBUG 1
12 #define STRESS_TESTS 0
14 void handle_panic(const char *func
, char *str
, ...);
15 #define panic(...) handle_panic(__func__, __VA_ARGS__)
17 #include "../libkern/os/refcnt.h"
18 #include "../libkern/os/refcnt.c"
20 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
22 /* import some of the refcnt internal state for testing */
23 extern bool ref_debug_enable
;
24 os_refgrp_decl_extern(global_ref_group
);
27 T_META_NAMESPACE("os_refcnt"),
28 T_META_CHECK_LEAKS(false)
31 T_DECL(os_refcnt
, "Basic atomic refcount")
34 os_ref_init(&rc
, NULL
);
35 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 1, "refcount correctly initialized");
39 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 3, "retain increased count");
41 os_ref_count_t x
= os_ref_release(&rc
);
42 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 2, "release decreased count");
43 T_ASSERT_EQ_UINT(x
, 2, "release returned correct count");
45 os_ref_release_live(&rc
);
46 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 1, "release_live decreased count");
48 x
= os_ref_release(&rc
);
49 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 0, "released");
50 T_ASSERT_EQ_UINT(x
, 0, "returned released");
52 os_ref_init(&rc
, NULL
);
53 x
= os_ref_retain_try(&rc
);
54 T_ASSERT_GT_INT(x
, 0, "try retained");
56 (void)os_ref_release(&rc
);
57 (void)os_ref_release(&rc
);
58 T_QUIET
; T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 0, "release");
60 x
= os_ref_retain_try(&rc
);
61 T_ASSERT_EQ_INT(x
, 0, "try failed");
64 T_DECL(refcnt_raw
, "Raw refcount")
67 os_ref_init_raw(&rc
, NULL
);
68 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 1, "refcount correctly initialized");
70 os_ref_retain_raw(&rc
, NULL
);
71 os_ref_retain_raw(&rc
, NULL
);
72 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 3, "retain increased count");
74 os_ref_count_t x
= os_ref_release_raw(&rc
, NULL
);
75 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 2, "release decreased count");
76 T_ASSERT_EQ_UINT(x
, 2, "release returned correct count");
78 os_ref_release_live_raw(&rc
, NULL
);
79 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 1, "release_live decreased count");
81 x
= os_ref_release_raw(&rc
, NULL
);
82 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 0, "released");
83 T_ASSERT_EQ_UINT(x
, 0, "returned released");
85 os_ref_init_raw(&rc
, NULL
);
86 x
= os_ref_retain_try_raw(&rc
, NULL
);
87 T_ASSERT_GT_INT(x
, 0, "try retained");
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");
93 x
= os_ref_retain_try_raw(&rc
, NULL
);
94 T_ASSERT_EQ_INT(x
, 0, "try failed");
97 T_DECL(refcnt_locked
, "Locked refcount")
100 os_ref_init(&rc
, NULL
);
102 os_ref_retain_locked(&rc
);
103 os_ref_retain_locked(&rc
);
104 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 3, "retain increased count");
106 os_ref_count_t x
= os_ref_release_locked(&rc
);
107 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 2, "release decreased count");
108 T_ASSERT_EQ_UINT(x
, 2, "release returned correct count");
110 (void)os_ref_release_locked(&rc
);
111 x
= os_ref_release_locked(&rc
);
112 T_ASSERT_EQ_UINT(os_ref_get_count(&rc
), 0, "released");
113 T_ASSERT_EQ_UINT(x
, 0, "returned released");
116 T_DECL(refcnt_raw_locked
, "Locked raw refcount")
119 os_ref_init_raw(&rc
, NULL
);
121 os_ref_retain_locked_raw(&rc
, NULL
);
122 os_ref_retain_locked_raw(&rc
, NULL
);
123 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 3, "retain increased count");
125 os_ref_count_t x
= os_ref_release_locked_raw(&rc
, NULL
);
126 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 2, "release decreased count");
127 T_ASSERT_EQ_UINT(x
, 2, "release returned correct count");
129 (void)os_ref_release_locked_raw(&rc
, NULL
);
130 x
= os_ref_release_locked_raw(&rc
, NULL
);
131 T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc
), 0, "released");
132 T_ASSERT_EQ_UINT(x
, 0, "returned released");
135 T_DECL(refcnt_mask_locked
, "Locked bitwise refcount")
137 const os_ref_count_t b
= 12;
139 os_ref_count_t reserved
= 0xaaa;
140 os_ref_init_count_mask(&rc
, NULL
, 1, reserved
, b
);
142 os_ref_retain_locked_mask(&rc
, NULL
, b
);
143 os_ref_retain_locked_mask(&rc
, NULL
, b
);
144 T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, b
), 3, "retain increased count");
146 os_ref_count_t x
= os_ref_release_locked_mask(&rc
, NULL
, b
);
147 T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, b
), 2, "release decreased count");
148 T_ASSERT_EQ_UINT(x
, 2, "release returned correct count");
149 T_ASSERT_EQ_UINT(rc
& ((1U << b
) - 1), reserved
, "Reserved bits not modified");
151 (void)os_ref_release_locked_mask(&rc
, NULL
, b
);
152 x
= os_ref_release_locked_mask(&rc
, NULL
, b
);
153 T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, b
), 0, "released");
154 T_ASSERT_EQ_UINT(x
, 0, "returned released");
155 T_ASSERT_EQ_UINT(rc
& ((1U << b
) - 1), reserved
, "Reserved bits not modified");
159 do_bitwise_test(const os_ref_count_t bits
)
162 os_ref_count_t reserved
= 0xaaaaaaaaU
& ((1U << bits
) - 1);
163 os_ref_init_count_mask(&rc
, NULL
, 1, reserved
, bits
);
165 T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, bits
), 1, "[%u bits] refcount initialized", bits
);
167 os_ref_retain_mask(&rc
, NULL
, bits
);
168 os_ref_retain_mask(&rc
, NULL
, bits
);
169 T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, bits
), 3, "retain increased count");
171 os_ref_count_t x
= os_ref_release_mask(&rc
, NULL
, bits
);
172 T_ASSERT_EQ_UINT(x
, 2, "release returned correct count");
174 os_ref_release_live_mask(&rc
, NULL
, bits
);
175 T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, bits
), 1, "release_live decreased count");
177 x
= os_ref_release_mask(&rc
, NULL
, bits
);
178 T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, bits
), 0, "released");
179 T_ASSERT_EQ_UINT(x
, 0, "returned released");
181 T_ASSERT_EQ_UINT(rc
& ((1U << bits
) - 1), reserved
, "Reserved bits not modified");
183 os_ref_init_count_mask(&rc
, NULL
, 1, reserved
, bits
);
184 x
= os_ref_retain_try_mask(&rc
, NULL
, bits
);
185 T_ASSERT_GT_INT(x
, 0, "try retained");
187 (void)os_ref_release_mask(&rc
, NULL
, bits
);
188 (void)os_ref_release_mask(&rc
, NULL
, bits
);
189 T_QUIET
; T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc
, bits
), 0, "release");
191 x
= os_ref_retain_try_mask(&rc
, NULL
, bits
);
192 T_ASSERT_EQ_INT(x
, 0, "try failed");
194 T_ASSERT_EQ_UINT(rc
& ((1U << bits
) - 1), reserved
, "Reserved bits not modified");
197 T_DECL(refcnt_bitwise
, "Bitwise refcount")
204 os_ref_atomic_t rc
= 0xaaaaaaaa;
206 const os_ref_count_t nbits
= 3;
207 const os_ref_count_t count
= 5;
208 const os_ref_count_t bits
= 7;
209 os_ref_init_count_mask(&rc
, NULL
, count
, bits
, nbits
);
211 os_ref_count_t mask
= (1U << nbits
) - 1;
212 T_ASSERT_EQ_UINT(rc
& mask
, bits
, "bits correctly initialized");
213 T_ASSERT_EQ_UINT(rc
>> nbits
, count
, "count correctly initialized");
216 os_refgrp_decl(static, g1
, "test group", NULL
);
217 os_refgrp_decl_extern(g1
);
219 T_DECL(refcnt_groups
, "Group accounting")
222 ref_debug_enable
= true;
225 os_ref_init(&rc
, &g1
);
227 T_ASSERT_EQ_UINT(g1
.grp_children
, 1, "group attached");
228 T_ASSERT_EQ_UINT(global_ref_group
.grp_children
, 1, "global group attached");
229 T_ASSERT_EQ_UINT(g1
.grp_count
, 1, "group count");
230 T_ASSERT_EQ_ULLONG(g1
.grp_retain_total
, 1ULL, "group retains");
231 T_ASSERT_EQ_ULLONG(g1
.grp_release_total
, 0ULL, "group releases");
235 os_ref_release_live(&rc
);
236 os_ref_release_live(&rc
);
238 T_EXPECT_EQ_ULLONG(g1
.grp_retain_total
, 3ULL, "group retains");
239 T_EXPECT_EQ_ULLONG(g1
.grp_release_total
, 2ULL, "group releases");
241 os_ref_count_t x
= os_ref_release(&rc
);
242 T_QUIET
; T_ASSERT_EQ_UINT(x
, 0, "released");
244 T_ASSERT_EQ_UINT(g1
.grp_children
, 0, "group detatched");
245 T_ASSERT_EQ_UINT(g1
.grp_count
, 0, "group count");
247 T_SKIP("Refcount debugging disabled");
254 OSREF_RESURRECTION
= 3,
255 OSREF_DEALLOC_LIVE
= 4,
259 static bool expect_panic
= false;
262 handle_panic(const char *func
, char *__unused str
, ...)
266 T_FAIL("unexpected panic from %s", func
);
267 T_LOG("corrupt program state, aborting");
270 expect_panic
= false;
272 if (strcmp(func
, "os_ref_panic_underflow") == 0) {
273 ret
= OSREF_UNDERFLOW
;
274 } else if (strcmp(func
, "os_ref_panic_overflow") == 0) {
275 ret
= OSREF_OVERFLOW
;
276 } else if (strcmp(func
, "os_ref_panic_resurrection") == 0) {
277 ret
= OSREF_RESURRECTION
;
278 } else if (strcmp(func
, "os_ref_panic_live") == 0) {
279 ret
= OSREF_DEALLOC_LIVE
;
281 T_LOG("unexpected panic from %s", func
);
287 T_DECL(refcnt_underflow
, "Underflow")
290 os_ref_init_raw(&rc
, NULL
);
291 (void)os_ref_release_raw(&rc
, NULL
);
296 (void)os_ref_release_raw(&rc
, NULL
);
297 T_FAIL("underflow not caught");
299 T_ASSERT_EQ_INT(x
, OSREF_UNDERFLOW
, "underflow caught");
303 T_DECL(refcnt_overflow
, "Overflow")
306 os_ref_init_count_raw(&rc
, NULL
, 0x0fffffffU
);
311 (void)os_ref_retain_raw(&rc
, NULL
);
312 T_FAIL("overflow not caught");
314 T_ASSERT_EQ_INT(x
, OSREF_OVERFLOW
, "overflow caught");
318 T_DECL(refcnt_resurrection
, "Resurrection")
321 os_ref_init_raw(&rc
, NULL
);
322 os_ref_count_t n
= os_ref_release_raw(&rc
, NULL
);
324 T_QUIET
; T_EXPECT_EQ_UINT(n
, 0, "reference not released");
329 (void)os_ref_retain_raw(&rc
, NULL
);
330 T_FAIL("resurrection not caught");
332 T_ASSERT_EQ_INT(x
, OSREF_RESURRECTION
, "resurrection caught");
336 T_DECL(refcnt_dealloc_live
, "Dealloc expected live object")
339 os_ref_init_raw(&rc
, NULL
);
345 os_ref_release_live_raw(&rc
, NULL
);
346 T_FAIL("dealloc live not caught");
348 T_ASSERT_EQ_INT(x
, OSREF_DEALLOC_LIVE
, "dealloc live caught");
352 T_DECL(refcnt_initializer
, "Static intializers")
354 struct os_refcnt rc
= OS_REF_INITIALIZER
;
355 os_ref_atomic_t rca
= OS_REF_ATOMIC_INITIALIZER
;
357 T_ASSERT_EQ_INT(0, os_ref_retain_try(&rc
), NULL
);
358 T_ASSERT_EQ_INT(0, os_ref_get_count_raw(&rca
), NULL
);
363 static const unsigned long iters
= 1024 * 1024 * 32;
368 struct os_refcnt
*rc
= _rc
;
369 for (unsigned long i
= 0; i
< iters
; i
++) {
371 os_ref_release_live(rc
);
376 T_DECL(refcnt_stress
, "Stress test")
381 os_ref_init(&rc
, NULL
);
383 T_ASSERT_POSIX_ZERO(pthread_create(&th1
, NULL
, func
, &rc
), "pthread_create");
384 T_ASSERT_POSIX_ZERO(pthread_create(&th2
, NULL
, func
, &rc
), "pthread_create");
387 T_ASSERT_POSIX_ZERO(pthread_join(th1
, &r1
), "pthread_join");
388 T_ASSERT_POSIX_ZERO(pthread_join(th2
, &r2
), "pthread_join");
390 os_ref_count_t x
= os_ref_release(&rc
);
391 T_ASSERT_EQ_INT(x
, 0, "Consistent refcount");