sandpit 0.5.3

A concurrent garbage collected arena
Documentation
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
use rand::prelude::*;
use sandpit::{Arena, Gc, GcOpt, Root, Trace};

#[derive(Debug, Clone, Copy)]
pub enum CollectionStrategy {
    Never,
    AfterEveryAllocation,
    VeryFrequent(usize),
    Infrequent(usize),
    Random(f64),
    Mixed { major_every: usize, minor_every: usize },
}

/// Configuration for the GC fuzzer
#[derive(Debug, Clone, Copy)]
pub struct FuzzConfig {
    /// Total number of allocation iterations to perform
    pub iterations: usize,

    /// Range for number of allocations per iteration (min, max)
    pub allocs_per_iteration: (usize, usize),

    /// Range for object sizes (min, max)
    pub object_size_range: (usize, usize),

    /// Range for object alignment (as power of 2: min, max)
    /// E.g., (0, 4) means alignments from 2^0=1 to 2^4=16
    pub alignment_range: (u32, u32),

    /// Range for slice lengths (min, max)
    pub slice_length_range: (usize, usize),

    /// Probability of allocating a slice vs a sized object (0.0 to 1.0)
    pub slice_probability: f64,

    /// Probability of allocating a string (0.0 to 1.0)
    pub string_probability: f64,

    /// Collection strategy to use
    pub collection_strategy: CollectionStrategy,

    /// Whether to verify allocations (slower but catches bugs)
    pub verify_allocations: bool,

    /// Random seed (None for random seed)
    pub seed: Option<u64>,
}

impl Default for FuzzConfig {
    fn default() -> Self {
        Self {
            iterations: 1000,
            allocs_per_iteration: (10, 100),
            object_size_range: (1, 1024),
            alignment_range: (0, 4),
            slice_length_range: (0, 1000),
            slice_probability: 0.3,
            string_probability: 0.2,
            collection_strategy: CollectionStrategy::VeryFrequent(10),
            verify_allocations: true,
            seed: None,
        }
    }
}

impl FuzzConfig {
    pub fn never_collect() -> Self {
        Self {
            collection_strategy: CollectionStrategy::Never,
            ..Default::default()
        }
    }

    pub fn collect_always() -> Self {
        Self {
            collection_strategy: CollectionStrategy::AfterEveryAllocation,
            iterations: 50,
            allocs_per_iteration: (5, 20),
            ..Default::default()
        }
    }

    pub fn collect_very_frequent() -> Self {
        Self {
            collection_strategy: CollectionStrategy::VeryFrequent(5),
            ..Default::default()
        }
    }

    pub fn collect_infrequent() -> Self {
        Self {
            collection_strategy: CollectionStrategy::Infrequent(100),
            iterations: 200,
            ..Default::default()
        }
    }

    pub fn collect_random(probability: f64) -> Self {
        Self {
            collection_strategy: CollectionStrategy::Random(probability),
            ..Default::default()
        }
    }

    pub fn collect_mixed() -> Self {
        Self {
            collection_strategy: CollectionStrategy::Mixed {
                major_every: 50,
                minor_every: 10,
            },
            ..Default::default()
        }
    }

    pub fn stress_test() -> Self {
        Self {
            iterations: 1000,
            allocs_per_iteration: (50, 200),
            object_size_range: (1, 4096),
            slice_length_range: (0, 10000),
            collection_strategy: CollectionStrategy::VeryFrequent(25),
            ..Default::default()
        }
    }
}

#[derive(Debug, Default)]
pub struct FuzzStats {
    pub total_allocations: usize,
    pub sized_allocations: usize,
    pub slice_allocations: usize,
    pub string_allocations: usize,
    pub collections_performed: usize,
    pub major_collections: usize,
    pub minor_collections: usize,
}

impl FuzzStats {
    pub fn print(&self) {
        println!("=== Fuzz Statistics ===");
        println!("Total allocations: {}", self.total_allocations);
        println!("  Sized: {}", self.sized_allocations);
        println!("  Slices: {}", self.slice_allocations);
        println!("  Strings: {}", self.string_allocations);
        println!("Collections performed: {}", self.collections_performed);
        println!("  Major: {}", self.major_collections);
        println!("  Minor: {}", self.minor_collections);
    }
}


fn should_collect(
    strategy: &CollectionStrategy,
    iteration: usize,
    alloc_count: usize,
    rng: &mut impl Rng,
) -> (bool, bool) {
    match strategy {
        CollectionStrategy::Never => (false, false),
        CollectionStrategy::AfterEveryAllocation => (true, iteration % 2 == 0),
        CollectionStrategy::VeryFrequent(n) => {
            if alloc_count % n == 0 {
                (true, alloc_count % (n * 2) == 0)
            } else {
                (false, false)
            }
        }
        CollectionStrategy::Infrequent(n) => {
            if alloc_count % n == 0 {
                (true, alloc_count % (n * 3) == 0)
            } else {
                (false, false)
            }
        }
        CollectionStrategy::Random(prob) => {
            if rng.gen::<f64>() < *prob {
                (true, rng.gen::<bool>())
            } else {
                (false, false)
            }
        }
        CollectionStrategy::Mixed { major_every, minor_every } => {
            if alloc_count % major_every == 0 {
                (true, true)
            } else if alloc_count % minor_every == 0 {
                (true, false)
            } else {
                (false, false)
            }
        }
    }
}

// Types for node verification fuzzing
use sandpit::GcVec;
use core::cell::Cell;

#[derive(Trace)]
enum NodeContent<'gc> {
    Sized(Gc<'gc, usize>),
    Slice(Gc<'gc, [usize]>),
    String(Gc<'gc, str>),
    VecValues(GcVec<'gc, Cell<usize>>),
    VecNodes(GcVec<'gc, Gc<'gc, VerifiedNode<'gc>>>),
}

#[derive(Trace)]
struct VerifiedNode<'gc> {
    id: Cell<usize>,
    content: NodeContent<'gc>,
    next: GcOpt<'gc, VerifiedNode<'gc>>,
}

// Helper: Create node content based on config probabilities
fn create_node_content<'gc, 'a>(
    mu: &'a sandpit::Mutator<'gc>,
    id: usize,
    config: &FuzzConfig,
    rng: &mut StdRng,
    stats: &mut FuzzStats,
) -> NodeContent<'gc>
where 'a: 'gc {
    let rand_val: f64 = rng.gen();

    if rand_val < config.string_probability {
        // String allocation
        let min_len = config.object_size_range.0;
        let max_len = config.object_size_range.1.min(1000).max(min_len);
        let string_len = if min_len == max_len {
            min_len
        } else {
            rng.gen_range(min_len..=max_len)
        };
        let string_content: String = (0..string_len)
            .map(|_| rng.sample(rand::distributions::Alphanumeric) as char)
            .collect();
        let gc_str = mu.alloc_str(&string_content);
        stats.string_allocations += 1;
        NodeContent::String(gc_str)
    } else if rand_val < config.string_probability + config.slice_probability {
        // Slice allocation
        let min_len = config.slice_length_range.0;
        let max_len = config.slice_length_range.1.min(50).max(min_len);
        let slice_len = if min_len == max_len {
            min_len
        } else {
            rng.gen_range(min_len..=max_len)
        };
        let slice = mu.alloc_array_from_fn(slice_len, |i| id * 1000 + i);
        stats.slice_allocations += 1;
        NodeContent::Slice(slice)
    } else {
        // Split remaining probability between sized, vec_values, vec_nodes
        let remaining_prob = 1.0 - config.string_probability - config.slice_probability;
        let adjusted_val = (rand_val - config.string_probability - config.slice_probability) / remaining_prob;

        if adjusted_val < 0.33 {
            // Sized allocation
            let data = Gc::new(mu, id * 777);
            stats.sized_allocations += 1;
            NodeContent::Sized(data)
        } else if adjusted_val < 0.66 {
            // GcVec of values
            let vec_len = rng.gen_range(0..=20);
            let vec = GcVec::new(mu);
            for i in 0..vec_len {
                vec.push(mu, Cell::new(id * 100 + i));
            }
            stats.sized_allocations += 1;
            NodeContent::VecValues(vec)
        } else {
            // GcVec of nodes (keep it small to avoid deep recursion)
            let vec_len = rng.gen_range(0..=100);
            let vec = GcVec::new(mu);
            // Create simple sized nodes to avoid infinite recursion
            for i in 0..vec_len {
                let inner_id = id + i;
                let simple_node = Gc::new(mu, VerifiedNode {
                    id: Cell::new(inner_id),
                    content: NodeContent::Sized(Gc::new(mu, inner_id * 777)),
                    next: GcOpt::new_none(),
                });
                vec.push(mu, simple_node);
            }
            stats.sized_allocations += 1;
            NodeContent::VecNodes(vec)
        }
    }
}

// Helper: Verify a single node's content
fn verify_node_content<'gc>(node: &VerifiedNode<'gc>, id: usize, prefix: &str) {
    match &node.content {
        NodeContent::Sized(data_gc) => {
            let data = data_gc.scoped_deref();
            assert_eq!(*data, id * 777,
                "{}Node data corruption! Expected {}, got {} for id {}",
                prefix, id * 777, *data, id);
        }
        NodeContent::Slice(slice_gc) => {
            let slice = slice_gc.scoped_deref();
            for (idx, &slice_val) in slice.iter().enumerate() {
                let expected = id * 1000 + idx;
                assert_eq!(slice_val, expected,
                    "{}Node slice corruption! Expected {}, got {} for id {} at index {}",
                    prefix, expected, slice_val, id, idx);
            }
        }
        NodeContent::String(string_gc) => {
            let _string = string_gc.scoped_deref();
            // String content is random, so we just verify it's still accessible
        }
        NodeContent::VecValues(vec) => {
            for i in 0..vec.len() {
                if let Some(cell) = vec.get_idx(i) {
                    let val = cell.get();
                    let expected = id * 100 + i;
                    assert_eq!(val, expected,
                        "{}Vec value corruption! Expected {}, got {} for id {} at index {}",
                        prefix, expected, val, id, i);
                }
            }
        }
        NodeContent::VecNodes(vec) => {
            for i in 0..vec.len() {
                if let Some(node_gc) = vec.get_idx(i) {
                    let inner_node = node_gc.scoped_deref();
                    let inner_id = inner_node.id.get();
                    verify_node_content(inner_node, inner_id, "???");
                    // Just verify we can access the node
                }
            }
        }
    }
}

// Helper: Verify all nodes in the list
fn verify_all_nodes<'gc>(
        head: GcOpt<'gc, VerifiedNode<'gc>>,
        expected_count: usize,
        prefix: &str,
    ) {
    let mut current = head;
    let mut verified_count = 0;

    while let Some(node_gc) = current.as_option() {
        let node = node_gc.scoped_deref();
        let id = node.id.get();
        verify_node_content(&node, id, prefix);
        verified_count += 1;
        current = node.next.clone();
    }

    assert_eq!(verified_count, expected_count,
        "{}Node count mismatch! Expected {}, found {} reachable nodes",
        prefix, expected_count, verified_count);
}

// Helper: Remove nodes from the front of the list
fn prune_nodes<'gc>(
        mu: &'gc sandpit::Mutator<'gc>,
        root: &'gc sandpit::InnerBarrier<GcOpt<'gc, VerifiedNode<'gc>>>,
        num_removes: usize,
        node_count: &mut usize,
    ) {
    for _ in 0..num_removes {
        if let Some(head_gc) = root.inner().as_option() {
            let head = head_gc.scoped_deref();
            let next = head.next.clone();
            root.write_barrier(mu, |barrier| {
                barrier.set(next);
            });
            *node_count -= 1;
        } else {
            break;
        }
    }
}

/// Fuzz test with nodes that verifies all allocated values remain valid
/// This tests graph structures (linked lists) with verification
pub fn fuzz_gc_with_node_verification(config: FuzzConfig) -> FuzzStats {
    use sandpit::InnerBarrier;

    let mut stats = FuzzStats::default();
    let mut rng: StdRng = if let Some(seed) = config.seed {
        StdRng::seed_from_u64(seed)
    } else {
        StdRng::from_entropy()
    };

    // Root points to the head of the linked list
    let arena: Arena<Root![InnerBarrier<GcOpt<'_, VerifiedNode<'_>>>]> = Arena::new(|mu| {
        InnerBarrier::new(mu, GcOpt::new_none())
    });

    let mut alloc_count = 0;
    let mut next_id = 0;
    let mut node_count = 0;

    for iteration in 0..config.iterations {
        arena.mutate(|mu, root| {
            let num_allocs = rng.gen_range(config.allocs_per_iteration.0..=config.allocs_per_iteration.1);

            // Add new nodes to the front of the list
            for _ in 0..num_allocs {
                let id = next_id;
                next_id += 1;

                let content = create_node_content(mu, id, &config, &mut rng, &mut stats);

                // Create new node pointing to current head
                let new_node = Gc::new(mu, VerifiedNode {
                    id: Cell::new(id),
                    content,
                    next: root.inner().clone(),
                });

                // Update head to point to new node
                root.write_barrier(mu, |barrier| {
                    barrier.set(GcOpt::from(new_node));
                });

                node_count += 1;
                stats.total_allocations += 1;
            }

            // Verify all nodes in the list
            if config.verify_allocations {
                verify_all_nodes(root.inner().clone(), node_count, "");
            }

            // Occasionally remove some nodes from the front to keep memory reasonable
            if node_count > 500 && rng.gen::<bool>() {
                let num_removes = rng.gen_range(1..=50);
                prune_nodes(mu, root, num_removes, &mut node_count);
            }

            alloc_count += 1;
        });

        let (should_collect, is_major) = should_collect(
            &config.collection_strategy,
            iteration,
            alloc_count,
            &mut rng,
        );

        if should_collect {
            if is_major {
                arena.major_collect();
                stats.major_collections += 1;
            } else {
                arena.minor_collect();
                stats.minor_collections += 1;
            }
            stats.collections_performed += 1;
        }
    }

    // Final verification pass - walk the entire list
    arena.mutate(|_mu, root| {
        verify_all_nodes(root.inner().clone(), node_count, "Final: ");
    });

    arena.major_collect();
    stats.collections_performed += 1;
    stats.major_collections += 1;

    stats
}