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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Consolidated loom model-checked tests.

#![cfg(loom)]

mod common;

mod loom_arc {
    #![allow(clippy::std_instead_of_core, reason = "loom + std interop in tests")]
    #![allow(clippy::missing_panics_doc, reason = "test code")]
    #![allow(clippy::unwrap_used, reason = "test code")]
    use std::sync::atomic::{AtomicUsize as StdAtomicUsize, Ordering as StdOrdering};

    use loom::thread;
    use multitude::{Arc, Arena};

    #[expect(unused_imports, reason = "merged test module re-exports common helpers")]
    use crate::common;

    /// A payload type that increments a global counter on Drop.  We use a
    /// real `std::sync::atomic::AtomicUsize` here (NOT loom's), because
    /// the counter exists outside the model: it accumulates *across*
    /// permutations to verify each permutation drops exactly once.
    fn drop_counter() -> &'static StdAtomicUsize {
        static C: StdAtomicUsize = StdAtomicUsize::new(0);
        &C
    }

    struct DropCounted;

    impl Drop for DropCounted {
        fn drop(&mut self) {
            let _prev = drop_counter().fetch_add(1, StdOrdering::Relaxed);
        }
    }

    /// Build a fresh `Arena` with a tiny chunk size so chunk allocation
    /// happens deterministically per scenario.
    fn fresh_arena() -> Arena {
        Arena::builder().max_normal_alloc(4 * 1024).build()
    }

    #[test]
    fn arc_clone_drop_race() {
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let original: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let c1 = Arc::clone(&original);
            let c2 = Arc::clone(&original);

            let t1 = thread::spawn(move || {
                drop(c1);
            });
            let t2 = thread::spawn(move || {
                drop(c2);
            });

            t1.join().unwrap();
            t2.join().unwrap();

            // Owner-side drop of `original` and `arena`.
            drop(original);
            drop(arena);

            // Exactly one Drop must have run for the payload, regardless
            // of the interleaving Loom picked.
            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1, "DropCounted::drop must run exactly once");
        });
    }

    #[test]
    fn arc_drop_after_arena_drop() {
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let arc: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            // Owner drops the arena BEFORE the worker drops the Arc.
            // Worker's Arc::drop decs ref_count to 0 → runs teardown on
            // the worker (non-owner) thread → decs outstanding_chunks →
            // last reclaimer → free_storage(ArenaInner).
            let t = thread::spawn(move || {
                drop(arc);
            });

            drop(arena);
            t.join().unwrap();

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn two_arcs_two_threads() {
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let original: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let c1 = Arc::clone(&original);
            let c2 = Arc::clone(&original);
            // Owner releases its share first; only the worker-thread Arcs remain.
            drop(original);

            let t1 = thread::spawn(move || drop(c1));
            let t2 = thread::spawn(move || drop(c2));

            t1.join().unwrap();
            t2.join().unwrap();

            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn deferred_reconciliation_race() {
        // The owner allocates 2 Arcs on a Chunk (so the chunk's
        // arcs_issued is bumped twice non-atomically). The slot evicts
        // when we drop the arena, which does
        // `fetch_sub(LARGE - 2, Release)` on the chunk's atomic
        // `ref_count`. A worker thread is concurrently dropping one of
        // the Arcs (an atomic `fetch_sub(1, Release)`).
        //
        // After both have run and the owner-side `arc2.drop()` has also
        // run, the chunk's net refcount must be zero and teardown must
        // run exactly once.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let arc1: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let arc2: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            let t = thread::spawn(move || drop(arc1));

            // Drop `arc2` and the arena on the owner thread, racing the
            // worker's `arc1` drop.
            drop(arc2);
            drop(arena);

            t.join().unwrap();

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 2, "both DropCounted payloads must drop exactly once");
        });
    }

    #[test]
    fn arc_clone_then_send_then_drop() {
        // Owner clones an Arc and sends it to a worker. Worker drops its
        // clone. Owner drops the original and the arena. Verifies the
        // standard inc/dec ordering produces a correct final count even
        // when the inc and dec happen on different threads.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let original: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let cloned = Arc::clone(&original);

            let t = thread::spawn(move || drop(cloned));

            drop(original);
            t.join().unwrap();
            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn worker_clones_then_drops() {
        // Owner sends an Arc to a worker. The worker clones it on its
        // own thread, drops the clone, then drops the original. The
        // owner drops the arena. Verifies clone-on-non-owner-thread is
        // sound (the inc + later dec both happen on the worker, but
        // they're paired across the spawned thread boundary, exercising
        // Loom's HB tracking).
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let original: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            let t = thread::spawn(move || {
                let cloned = Arc::clone(&original);
                drop(cloned);
                drop(original);
            });

            // Owner waits for worker, then drops the arena.
            t.join().unwrap();
            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn arena_drop_concurrent_with_clone_and_drop() {
        // Three concurrent operations: owner drops the arena while two
        // workers are racing on Arc clone/drop. Stresses the
        // `arena_dropped` Acquire/Release pairing on cross-thread
        // teardown.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let original: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let c1 = Arc::clone(&original);

            let t1 = thread::spawn(move || drop(c1));
            // Owner drops original then arena while t1 races.
            drop(original);
            drop(arena);
            t1.join().unwrap();

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn two_workers_clone_and_drop_during_reset_and_arena_drop() {
        // Two workers drop their Arcs while the owner resets (a no-op on the
        // chunk) and then drops the arena. The chunk is torn
        // down when its last reference releases; reconcile-on-drop must
        // produce a refcount that reaches 0 exactly once.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let mut arena = fresh_arena();
            let arc1: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let arc2: Arc<DropCounted> = Arc::clone(&arc1);

            let t1 = thread::spawn(move || drop(arc1));
            let t2 = thread::spawn(move || drop(arc2));

            arena.reset();

            t1.join().unwrap();
            t2.join().unwrap();
            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1, "Drop must run exactly once");
        });
    }

    #[test]
    fn worker_drop_racing_reset_then_owner_drops_arena() {
        // Variant of `deferred_reconciliation_race`: the owner resets (a
        // no-op on the chunk) and then drops the arena, so the
        // worker's drop may be the last reference and hit the
        // `outstanding_chunks` last-reclaimer path on the now-detached chunk.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let mut arena = fresh_arena();
            let arc1: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            let t = thread::spawn(move || drop(arc1));

            arena.reset();
            drop(arena);

            t.join().unwrap();

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn second_alloc_after_reset_reuses_installed_chunk_with_active_worker() {
        // Owner allocates an Arc, resets (the chunk stays installed),
        // then allocates a second Arc on that same chunk, all while a worker
        // drops the first Arc. Stresses an allocation onto a live chunk
        // against an in-flight worker drop of an earlier handle.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let mut arena = fresh_arena();
            let arc1: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            let t = thread::spawn(move || drop(arc1));

            arena.reset();
            let arc2: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            drop(arc2);
            drop(arena);

            t.join().unwrap();

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 2, "both DropCounted payloads must drop exactly once");
        });
    }

    #[test]
    fn arc_clone_racing_drop_same_arc() {
        // Thread A clones an Arc while thread B drops a different clone
        // of the same chunk's Arc concurrently. The classic clone/drop
        // window: `fetch_add(Relaxed)` on one thread races with
        // `fetch_sub(Release) + fence(Acquire)` on the other.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let original: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let dropper_arc = Arc::clone(&original);
            let cloner_arc = Arc::clone(&original);
            drop(original);

            let t_a = thread::spawn(move || {
                let cloned = Arc::clone(&cloner_arc);
                drop(cloned);
                drop(cloner_arc);
            });
            let t_b = thread::spawn(move || drop(dropper_arc));

            t_a.join().unwrap();
            t_b.join().unwrap();
            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn deferred_reconciliation_two_workers_drop() {
        // Owner allocates 3 Arcs (so `arcs_issued = 3` non-atomically),
        // then evicts via arena drop (`fetch_sub(LARGE - 3, Release)`).
        // Two workers each drop one Arc concurrently with the eviction;
        // the third is dropped on the owner thread. Tests that the
        // reconciliation arithmetic stays correct with multiple workers
        // racing the eviction.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let arc1: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let arc2: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let arc3: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            let t1 = thread::spawn(move || drop(arc1));
            let t2 = thread::spawn(move || drop(arc2));

            drop(arc3);
            drop(arena);

            t1.join().unwrap();
            t2.join().unwrap();

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 3);
        });
    }

    #[test]
    fn arena_reset_concurrent_with_clone_and_drop() {
        // Owner calls `arena.reset()` (NOT drop) while a worker drops an Arc
        // clone. `reset` leaves the chunk untouched, so the chunk is
        // torn down later at arena drop; Drop must still run exactly once.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let mut arena = fresh_arena();
            let original: Arc<DropCounted> = arena.alloc_arc(DropCounted);
            let c1 = Arc::clone(&original);
            drop(original);

            let t = thread::spawn(move || drop(c1));

            arena.reset();
            t.join().unwrap();
            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 1);
        });
    }

    #[test]
    fn second_alloc_after_reset_shares_chunk_with_prior_generation_worker_drop() {
        // Owner allocates an Arc, resets (the chunk stays installed),
        // then allocates a second Arc on the same chunk. Concurrently, a
        // worker holding the first Arc drops it, hitting that chunk's
        // refcount. Both payloads must drop exactly once.
        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let mut arena = fresh_arena();
            let arc_gen1: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            let t = thread::spawn(move || drop(arc_gen1));

            arena.reset();
            let arc_gen2: Arc<DropCounted> = arena.alloc_arc(DropCounted);

            t.join().unwrap();
            drop(arc_gen2);
            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(after - baseline, 2);
        });
    }

    #[test]
    fn arc_str_clone_drop_race() {
        // Same pattern as `arc_clone_drop_race` but on the str path.
        // Arc<str> uses the same atomic refcount machinery as Arc<T>
        // (fetch_add/Relaxed + fetch_sub/Release + Acquire fence) but
        // through a different allocation/teardown shape (no DropEntry,
        // payload is non-Drop bytes). Verifies the str path's orderings.
        loom::model(|| {
            let arena = fresh_arena();
            let original = arena.alloc_str_arc("loom");
            let c1 = original.clone();
            let c2 = original.clone();

            let t1 = thread::spawn(move || drop(c1));
            let t2 = thread::spawn(move || drop(c2));

            t1.join().unwrap();
            t2.join().unwrap();

            drop(original);
            drop(arena);
        });
    }

    #[test]
    fn cache_push_push_race() {
        // Two workers concurrently drop the last `Arc` on two different
        // chunks. Both drops route through `release`, which
        // pushes the eligible chunk onto the provider's Treiber-stack
        // `cache` head via `compare_exchange_weak(AcqRel/Acquire)`.
        // Loom explores every interleaving of the two `push`
        // CAS retry loops, including the case where the first thread's
        // CAS observes `head` modified by the second's push and has to
        // re-store its `next` pointer.
        loom::model(|| {
            let arena = fresh_arena();
            // Each `Arc<[u32; 256]>` takes ~1 KiB + the per-`Arc` strong
            // prefix; with `max_normal_alloc = 4 KiB` chunks, two of these
            // allocate in separate chunks via refill, so dropping each on a
            // different worker forces two independent `push`
            // paths.
            let a: Arc<[u32; 256]> = arena.alloc_arc([0_u32; 256]);
            let b: Arc<[u32; 256]> = arena.alloc_arc([0_u32; 256]);

            let t1 = thread::spawn(move || drop(a));
            let t2 = thread::spawn(move || drop(b));

            t1.join().unwrap();
            t2.join().unwrap();

            // Owner allocates again — exercises `pop`
            // popping a chunk just pushed by the workers. Single-consumer
            // pop is the owner thread; loom orders the pop strictly after
            // the joins, but the pop's read of `head.next` must still see
            // the value the most recent pusher settled via its Release CAS.
            let c: Arc<[u32; 256]> = arena.alloc_arc([0_u32; 256]);
            drop(c);
            drop(arena);
        });
    }

    #[test]
    fn cache_push_pop_race() {
        // Models the F6 concern: verifies the popper's Relaxed read of
        // `head.next` is sound when a concurrent pusher installs a new node
        // above head. The implicit invariant is that no thread mutates an
        // installed node's `next` field after the push that installed it.
        loom::model(|| {
            let arena = fresh_arena();
            // Each `Arc<[u32; 256]>` takes ~1 KiB + the per-`Arc` strong
            // prefix; with `max_normal_alloc = 4 KiB` chunks, these
            // allocations refill into separate chunks so each drop/pop
            // exercises cache traffic.
            let cached: Arc<[u32; 256]> = arena.alloc_arc([0_u32; 256]);
            let racing: Arc<[u32; 256]> = arena.alloc_arc([0_u32; 256]);

            // Pre-populate the cache with one chunk.
            drop(cached);

            let t = thread::spawn(move || drop(racing));

            // Owner allocates while the worker may be pushing another chunk,
            // exercising `pop` against a concurrent push.
            let popped: Arc<[u32; 256]> = arena.alloc_arc([0_u32; 256]);

            t.join().unwrap();
            drop(popped);
            drop(arena);
        });
    }

    #[test]
    fn arc_assume_init_cross_thread_clones() {
        // Two workers each call `Arc::<MaybeUninit<T>>::assume_init` on
        // their own clone of the same allocation. Both `store_drop_fn`s
        // race on the chunk's `InnerDropEntry::drop_fn` atomic. The writes
        // are idempotent (same `drop_shim_one::<T>` pointer), but loom
        // verifies the Release/Acquire chain through `drop_count`
        // (publishing the entry slot from owner) and `refcount` (Acquire
        // fence on last decrement -> Relaxed load of `drop_fn` during
        // chunk-replay) holds under every reordering.
        //
        // `AssumeInitDropped` has all-zero-bits as a valid pattern (a
        // single `u64` field), so `alloc_zeroed_arc` produces a
        // legitimately-initialized value that `assume_init` can soundly
        // consume.
        struct AssumeInitDropped {
            _bytes: u64,
        }
        impl Drop for AssumeInitDropped {
            fn drop(&mut self) {
                drop_counter().fetch_add(1, StdOrdering::Relaxed);
            }
        }

        loom::model(|| {
            let baseline = drop_counter().load(StdOrdering::Relaxed);

            let arena = fresh_arena();
            let arc: Arc<core::mem::MaybeUninit<AssumeInitDropped>> = arena.alloc_zeroed_arc();
            let cloned = Arc::clone(&arc);

            let t1 = thread::spawn(move || {
                // SAFETY: all-zero bits is a valid `AssumeInitDropped`.
                let init: Arc<AssumeInitDropped> = unsafe { arc.assume_init() };
                drop(init);
            });
            let t2 = thread::spawn(move || {
                // SAFETY: all-zero bits is a valid `AssumeInitDropped`.
                let init: Arc<AssumeInitDropped> = unsafe { cloned.assume_init() };
                drop(init);
            });

            t1.join().unwrap();
            t2.join().unwrap();
            drop(arena);

            let after = drop_counter().load(StdOrdering::Relaxed);
            assert_eq!(
                after - baseline,
                1,
                "Drop runs exactly once even with two cross-thread assume_inits"
            );
        });
    }
}

mod loom_bytesbuf {
    #![cfg(all(loom, feature = "bytesbuf"))]
    #![allow(clippy::std_instead_of_core, reason = "loom + std interop in tests")]
    #![allow(clippy::missing_panics_doc, reason = "test code")]
    #![allow(clippy::unwrap_used, reason = "test code")]
    use bytesbuf::mem::Memory;
    use loom::thread;
    use multitude::Arena;

    #[expect(unused_imports, reason = "merged test module re-exports common helpers")]
    use crate::common;

    #[test]
    fn bytesbuf_view_clone_drop_race() {
        // Owner reserves an arena-backed `BytesBuf`, fills it, consumes it
        // into a view, clones the view, and sends the original + clone to
        // two workers that drop them concurrently. Each drop runs
        // `BlockRef::drop` -> `ArenaBlockState::ref_count.fetch_sub(1, Release)`;
        // the last decrement does `fence(Acquire)` and then frees the
        // backing chunk hold. Loom verifies the chain produces exactly one
        // chunk release regardless of which thread sees the last
        // decrement.
        loom::model(|| {
            let arena = Arena::new();
            let mut buf = arena.reserve(16);
            buf.put_slice(*b"hello");
            let view_a = buf.consume_all();
            let view_b = view_a.clone();

            let t1 = thread::spawn(move || drop(view_a));
            let t2 = thread::spawn(move || drop(view_b));

            t1.join().unwrap();
            t2.join().unwrap();
            drop(arena);
        });
    }

    #[test]
    fn bytesbuf_view_clone_in_worker_drop_split() {
        // Owner sends a view to worker A; worker A clones it on its own
        // thread and sends the clone to worker B. Then both workers drop
        // their views. Exercises clone-on-non-owner-thread (refcount
        // increment from one worker) paired with cross-thread drops.
        loom::model(|| {
            let arena = Arena::new();
            let mut buf = arena.reserve(8);
            buf.put_slice(*b"loom");
            let view = buf.consume_all();

            let t1 = thread::spawn(move || {
                let cloned = view.clone();
                cloned
            });
            let cloned = t1.join().unwrap();

            let t2 = thread::spawn(move || drop(cloned));
            t2.join().unwrap();

            drop(arena);
        });
    }
}