arena-b 1.0.0

Production-grade bump allocator with lock-free, slab, and virtual-memory tooling for parsers, game engines, and request-scoped services
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
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
//! Memory safety debugging with guards and use-after-rewind detection

extern crate alloc;

use alloc::alloc::{alloc, dealloc, Layout};
use alloc::string::String;
use alloc::vec::Vec;
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::collections::HashMap;
use std::sync::{LazyLock, RwLock};

pub const GUARD_MAGIC: u64 = 0xDEADBEEFCAFEBABE;
pub const FREED_MAGIC: u64 = 0xFEEDFACECAFEBABE;
const GUARD_SIZE: usize = 16;

// Debug guard values for corruption detection
#[repr(C)]
#[derive(Debug)]
struct DebugGuard {
    pre_guard: [u8; GUARD_SIZE],
    post_guard: [u8; GUARD_SIZE],
    magic: u64,
    size: usize,
    checkpoint_id: usize,
}

impl DebugGuard {
    fn new(size: usize, checkpoint_id: usize) -> Self {
        let mut guard = Self {
            pre_guard: [0xCC; GUARD_SIZE],
            post_guard: [0xCC; GUARD_SIZE],
            magic: GUARD_MAGIC,
            size,
            checkpoint_id,
        };

        // Write magic pattern to guards using byte pattern to avoid large shifts
        let bytes = GUARD_MAGIC.to_ne_bytes();
        for i in 0..GUARD_SIZE {
            guard.pre_guard[i] = bytes[i % bytes.len()];
            guard.post_guard[i] = bytes[i % bytes.len()];
        }

        guard
    }

    fn validate(&self) -> Result<(), &'static str> {
        if self.magic != GUARD_MAGIC {
            return Err("Magic number corrupted");
        }

        let bytes = GUARD_MAGIC.to_ne_bytes();
        for i in 0..GUARD_SIZE {
            let expected = bytes[i % bytes.len()];
            if self.pre_guard[i] != expected {
                return Err("Pre-guard corrupted");
            }
            if self.post_guard[i] != expected {
                return Err("Post-guard corrupted");
            }
        }

        Ok(())
    }
}

// Allocation tracking for use-after-rewind detection
#[derive(Debug, Clone)]
pub struct AllocationInfo {
    ptr: *mut u8,
    size: usize,
    checkpoint_id: usize,
    captured_backtrace: Option<String>,
}

unsafe impl Send for AllocationInfo {}
unsafe impl Sync for AllocationInfo {}

// Global debug state
pub static DEBUG_STATE: LazyLock<RwLock<DebugState>> =
    LazyLock::new(|| RwLock::new(DebugState::new()));
static VALIDATION_ENABLED: AtomicBool = AtomicBool::new(false);

pub struct DebugState {
    allocations: HashMap<usize, Vec<AllocationInfo>>,
    arena_checkpoints: HashMap<usize, usize>,
    next_arena_id: AtomicUsize,
    corrupted_allocations: usize,
    leak_reports: usize,
    backtraces_enabled: bool,
}

impl DebugState {
    fn new() -> Self {
        Self {
            allocations: HashMap::new(),
            arena_checkpoints: HashMap::new(),
            next_arena_id: AtomicUsize::new(1),
            corrupted_allocations: 0,
            leak_reports: 0,
            backtraces_enabled: cfg!(feature = "debug"),
        }
    }

    fn register_allocation(
        &mut self,
        arena_id: usize,
        ptr: *mut u8,
        size: usize,
        checkpoint_id: usize,
    ) {
        let info = AllocationInfo {
            ptr,
            size,
            checkpoint_id,
            captured_backtrace: self.capture_backtrace(),
        };

        self.allocations
            .entry(arena_id)
            .or_insert_with(Vec::new)
            .push(info);

        self.arena_checkpoints.insert(arena_id, checkpoint_id);
    }

    fn validate_allocation(&self, arena_id: usize, ptr: *mut u8) -> Result<(), &'static str> {
        if let Some(allocations) = self.allocations.get(&arena_id) {
            for info in allocations {
                if info.ptr == ptr {
                    // Check if allocation is from a valid checkpoint
                    if let Some(&current_checkpoint) = self.arena_checkpoints.get(&arena_id) {
                        if info.checkpoint_id > current_checkpoint {
                            return Err("Use-after-rewind detected");
                        }
                    }

                    // Validate guard bytes (use unaligned read to avoid UB)
                    let guard_ptr = unsafe { ptr.sub(GUARD_SIZE) };
                    let guard_val =
                        unsafe { core::ptr::read_unaligned(guard_ptr as *const DebugGuard) };

                    return guard_val.validate();
                }
            }
        }

        Err("Allocation not found")
    }

    fn validate_arena(&self, arena_id: usize) -> Result<(), String> {
        if let Some(allocations) = self.allocations.get(&arena_id) {
            let mut reports = Vec::new();
            for info in allocations {
                if let Err(err) = self.validate_allocation(arena_id, info.ptr) {
                    let trace = info
                        .captured_backtrace
                        .as_deref()
                        .unwrap_or("<backtrace unavailable>");
                    reports.push(format!("Pointer {:p}: {}\n{}", info.ptr, err, trace));
                }
            }

            if reports.is_empty() {
                Ok(())
            } else {
                Err(reports.join("\n"))
            }
        } else {
            Err("Arena not found".into())
        }
    }

    fn capture_backtrace(&self) -> Option<String> {
        if !self.backtraces_enabled {
            return None;
        }

        #[cfg(feature = "debug")]
        {
            Some(std::backtrace::Backtrace::capture().to_string())
        }

        #[cfg(not(feature = "debug"))]
        {
            None
        }
    }

    fn leak_report(&mut self, arena_id: usize) -> Vec<String> {
        let mut reports = Vec::new();
        if let Some(allocations) = self.allocations.get(&arena_id) {
            for info in allocations {
                let trace = info
                    .captured_backtrace
                    .as_deref()
                    .unwrap_or("<backtrace unavailable>");
                reports.push(format!(
                    "Leaked allocation: ptr={:p}, size={}, checkpoint={}\n{}",
                    info.ptr, info.size, info.checkpoint_id, trace
                ));
            }
        }
        self.leak_reports += reports.len();
        reports
    }

    fn rewind_to_checkpoint(&mut self, arena_id: usize, checkpoint_id: usize) {
        self.arena_checkpoints.insert(arena_id, checkpoint_id);

        // Mark allocations from future checkpoints as invalid
        if let Some(allocations) = self.allocations.get_mut(&arena_id) {
            for info in allocations.iter_mut() {
                if info.checkpoint_id > checkpoint_id {
                    // Corrupt the guard to detect use-after-rewind (write unaligned)
                    unsafe {
                        let guard_u8 = info.ptr.sub(GUARD_SIZE);
                        // Taint the first guard byte to mark corruption without causing aligned deref
                        core::ptr::write_unaligned(guard_u8, 0u8);
                    }
                }
            }
        }
    }

    pub fn get_stats(&self, arena_id: usize) -> (usize, usize) {
        let total = self
            .allocations
            .get(&arena_id)
            .map(|allocs| allocs.len())
            .unwrap_or(0);

        let corrupted = self.corrupted_allocations;

        (total, corrupted)
    }

    pub fn get_current_checkpoint_id(&self, arena_id: usize) -> usize {
        self.arena_checkpoints.get(&arena_id).copied().unwrap_or(0)
    }

    pub fn check_use_after_rewind(&self, arena_id: usize, ptr: *mut u8) -> Result<(), String> {
        self.validate_allocation(arena_id, ptr)
            .map_err(|e| e.to_string())
    }

    fn cleanup_arena(&mut self, arena_id: usize) {
        self.allocations.remove(&arena_id);
        self.arena_checkpoints.remove(&arena_id);
    }
}

// Public debug interface
pub fn register_allocation(arena_id: usize, ptr: *mut u8, size: usize, checkpoint_id: usize) {
    if let Ok(mut state) = DEBUG_STATE.write() {
        state.register_allocation(arena_id, ptr, size, checkpoint_id);
    }
}

pub fn validate_allocation(arena_id: usize, ptr: *mut u8) -> Result<(), &'static str> {
    if !VALIDATION_ENABLED.load(Ordering::Relaxed) {
        return Ok(());
    }

    if let Ok(state) = DEBUG_STATE.read() {
        state.validate_allocation(arena_id, ptr)
    } else {
        Err("Debug state locked")
    }
}

pub fn rewind_to_checkpoint(checkpoint_id: usize) {
    if let Ok(mut state) = DEBUG_STATE.write() {
        // Find all arenas and rewind them to the checkpoint
        let arena_ids: Vec<usize> = state.arena_checkpoints.keys().copied().collect();
        for arena_id in arena_ids {
            state.rewind_to_checkpoint(arena_id, checkpoint_id);
        }
    }
}

pub fn rewind_arena_to_checkpoint(arena_id: usize, checkpoint_id: usize) {
    if let Ok(mut state) = DEBUG_STATE.write() {
        state.rewind_to_checkpoint(arena_id, checkpoint_id);
    }
}

pub fn validate_arena(arena_id: usize) -> Result<(), String> {
    if !VALIDATION_ENABLED.load(Ordering::Relaxed) {
        return Ok(());
    }

    if let Ok(state) = DEBUG_STATE.read() {
        state.validate_arena(arena_id)
    } else {
        Err("Debug state locked".into())
    }
}

pub fn enable_validation(enable: bool) {
    VALIDATION_ENABLED.store(enable, Ordering::Relaxed);
}

pub fn leak_report(arena_id: usize) -> Vec<String> {
    if let Ok(mut state) = DEBUG_STATE.write() {
        state.leak_report(arena_id)
    } else {
        vec!["Debug state locked".into()]
    }
}

pub fn get_debug_stats() -> crate::core::DebugStats {
    if let Ok(state) = DEBUG_STATE.read() {
        crate::core::DebugStats {
            total_allocations: state.allocations.values().map(|v| v.len()).sum(),
            active_checkpoints: state.arena_checkpoints.len(),
            current_checkpoint_id: state.arena_checkpoints.values().max().copied().unwrap_or(0),
            corrupted_allocations: state.corrupted_allocations,
            leak_reports: state.leak_reports,
        }
    } else {
        crate::core::DebugStats::default()
    }
}

pub fn cleanup_arena(arena_id: usize) {
    if let Ok(mut state) = DEBUG_STATE.write() {
        state.cleanup_arena(arena_id);
    }
}

// Debug-enabled allocator wrapper
pub struct DebugAllocator {
    arena_id: usize,
    enabled: bool,
}

impl DebugAllocator {
    pub fn new() -> Self {
        let arena_id = if let Ok(state) = DEBUG_STATE.read() {
            state.next_arena_id.fetch_add(1, Ordering::Relaxed)
        } else {
            1
        };

        Self {
            arena_id,
            enabled: true,
        }
    }

    pub fn enable(&mut self) {
        self.enabled = true;
    }

    pub fn disable(&mut self) {
        self.enabled = false;
    }

    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    pub fn arena_id(&self) -> usize {
        self.arena_id
    }

    /// # Safety
    ///
    /// `ptr` must be a valid, non-null pointer to `size` bytes of initialized data
    /// allocated by the arena. The caller must ensure `size` is correct and that
    /// the memory is valid for reads and writes during guard wrapping.
    pub unsafe fn allocate_with_guard(
        &self,
        ptr: *mut u8,
        size: usize,
        checkpoint_id: usize,
    ) -> *mut u8 {
        if !self.enabled {
            return ptr;
        }

        let total_size = size + 2 * GUARD_SIZE + core::mem::size_of::<DebugGuard>();
        let layout = match Layout::from_size_align(total_size, 16) {
            Ok(l) => l,
            Err(_) => {
                eprintln!(
                    "DebugAllocator::allocate_with_guard: invalid layout total_size={}",
                    total_size
                );
                return ptr; // Fallback to original allocation when debug allocation can't be made
            }
        };

        let debug_ptr = unsafe { alloc(layout) };
        if debug_ptr.is_null() {
            return ptr; // Fallback to original allocation
        }

        // Create debug guard
        let guard = DebugGuard::new(size, checkpoint_id);

        unsafe {
            // Copy guard to the beginning
            core::ptr::copy_nonoverlapping(
                &guard as *const DebugGuard as *const u8,
                debug_ptr,
                core::mem::size_of::<DebugGuard>(),
            );

            // Copy pre-guard
            core::ptr::copy_nonoverlapping(
                guard.pre_guard.as_ptr(),
                debug_ptr.add(core::mem::size_of::<DebugGuard>()),
                GUARD_SIZE,
            );

            // Copy actual data
            core::ptr::copy_nonoverlapping(
                ptr,
                debug_ptr.add(core::mem::size_of::<DebugGuard>() + GUARD_SIZE),
                size,
            );

            // Copy post-guard
            core::ptr::copy_nonoverlapping(
                guard.post_guard.as_ptr(),
                debug_ptr.add(core::mem::size_of::<DebugGuard>() + GUARD_SIZE + size),
                GUARD_SIZE,
            );

            // Register allocation
            register_allocation(
                self.arena_id,
                debug_ptr.add(core::mem::size_of::<DebugGuard>() + GUARD_SIZE),
                size,
                checkpoint_id,
            );
        }

        // Do not deallocate the original pointer here — it may belong to arena chunks.

        unsafe { debug_ptr.add(core::mem::size_of::<DebugGuard>() + GUARD_SIZE) }
    }

    pub fn validate_pointer(&self, ptr: *mut u8) -> Result<(), &'static str> {
        if !self.enabled {
            return Ok(());
        }

        validate_allocation(self.arena_id, ptr)
    }

    pub fn validate_arena(&self) -> Result<(), String> {
        validate_arena(self.arena_id)
    }

    pub fn leak_report(&self) -> Vec<String> {
        leak_report(self.arena_id)
    }

    pub fn rewind(&self, checkpoint_id: usize) {
        if self.enabled {
            rewind_to_checkpoint(checkpoint_id);
        }
    }
}

impl Drop for DebugAllocator {
    fn drop(&mut self) {
        cleanup_arena(self.arena_id);
    }
}

impl Default for DebugAllocator {
    fn default() -> Self {
        Self::new()
    }
}

// Memory validation utilities
pub fn validate_all_allocations(arena_id: usize) -> Result<(), String> {
    if let Ok(state) = DEBUG_STATE.read() {
        if let Some(allocations) = state.allocations.get(&arena_id) {
            let mut errors = Vec::new();

            for info in allocations {
                if let Err(e) = state.validate_allocation(arena_id, info.ptr) {
                    errors.push(format!("Pointer {:p}: {}", info.ptr, e));
                }
            }

            if errors.is_empty() {
                Ok(())
            } else {
                Err(errors.join("; "))
            }
        } else {
            Ok(())
        }
    } else {
        Err("Debug state locked".to_string())
    }
}

// Check for memory corruption
pub fn check_memory_corruption(arena_id: usize) -> usize {
    if let Ok(state) = DEBUG_STATE.read() {
        if let Some(allocations) = state.allocations.get(&arena_id) {
            let mut corrupted = 0;

            for info in allocations {
                if state.validate_allocation(arena_id, info.ptr).is_err() {
                    corrupted += 1;
                }
            }

            corrupted
        } else {
            0
        }
    } else {
        0
    }
}