|
| 1 | +// Copyright (C) 2026 Stacks Open Internet Foundation |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +use core::cell::Cell; |
| 16 | +use std::alloc::{GlobalAlloc, Layout}; |
| 17 | +use std::sync::OnceLock; |
| 18 | + |
| 19 | +thread_local! { |
| 20 | + static THREAD_ALLOCATIONS: Cell<AllocationCounter> = const { Cell::new(AllocationCounter::ZERO) }; |
| 21 | +} |
| 22 | + |
| 23 | +/// A static bool for checking if the tracking allocator is installed |
| 24 | +/// as the global allocator |
| 25 | +static TRACKING_ALLOCATOR_INSTALLED: OnceLock<bool> = OnceLock::new(); |
| 26 | + |
| 27 | +/// Counter for net allocated bytes |
| 28 | +#[derive(Clone, Copy)] |
| 29 | +pub struct AllocationCounter { |
| 30 | + net_allocated: u64, |
| 31 | +} |
| 32 | + |
| 33 | +impl AllocationCounter { |
| 34 | + pub const ZERO: Self = Self { net_allocated: 0 }; |
| 35 | + |
| 36 | + /// Net allocation (allocated - deallocated) over a `baseline` |
| 37 | + pub fn net_allocated(&self, baseline: &AllocationCounter) -> u64 { |
| 38 | + self.net_allocated.saturating_sub(baseline.net_allocated) |
| 39 | + } |
| 40 | + |
| 41 | + /// Return `self` with net allocated incremented by `increment` |
| 42 | + fn increment(mut self, increment: u64) -> Self { |
| 43 | + self.net_allocated = self.net_allocated.saturating_add(increment); |
| 44 | + self |
| 45 | + } |
| 46 | + |
| 47 | + /// Return `self` with net allocated decremented by `decrement` |
| 48 | + fn decrement(mut self, decrement: u64) -> Self { |
| 49 | + self.net_allocated = self.net_allocated.saturating_sub(decrement); |
| 50 | + self |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// Check if the tracking allocator is installed |
| 55 | +/// |
| 56 | +/// If the check has already been performed in this process, |
| 57 | +/// it returns the prior value. Otherwise, it forces an allocation |
| 58 | +/// and checks if the tracker picked it up. |
| 59 | +pub fn tracking_allocator_installed() -> bool { |
| 60 | + *TRACKING_ALLOCATOR_INSTALLED.get_or_init(|| { |
| 61 | + let before = thread_allocated(); |
| 62 | + let probe: Vec<u8> = Vec::with_capacity(1024); |
| 63 | + // Prevent the optimizer from eliding the allocation. |
| 64 | + std::hint::black_box(&probe); |
| 65 | + let installed = thread_allocated().net_allocated(&before) > 0; |
| 66 | + if !installed { |
| 67 | + error!( |
| 68 | + "TrackingAllocator is not installed as the global allocator; any configured memory limits will never trigger" |
| 69 | + ); |
| 70 | + } |
| 71 | + drop(probe); |
| 72 | + installed |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +/// Read the allocation counter for the current thread. |
| 77 | +/// |
| 78 | +/// Returns AllocationCounter::ZERO if the tracking allocator is not installed or if TLS is |
| 79 | +/// being torn down (thread shutdown). |
| 80 | +pub fn thread_allocated() -> AllocationCounter { |
| 81 | + THREAD_ALLOCATIONS |
| 82 | + .try_with(Cell::get) |
| 83 | + .unwrap_or(AllocationCounter::ZERO) |
| 84 | +} |
| 85 | + |
| 86 | +/// A `GlobalAlloc` wrapper that counts per-thread allocations and |
| 87 | +/// deallocations. Delegates all actual allocation work to the inner |
| 88 | +/// allocator `A`. |
| 89 | +pub struct TrackingAllocator<A: GlobalAlloc> { |
| 90 | + /// The underlying allocator that performs the real work. |
| 91 | + pub inner: A, |
| 92 | +} |
| 93 | + |
| 94 | +unsafe impl<A: GlobalAlloc> GlobalAlloc for TrackingAllocator<A> { |
| 95 | + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 96 | + let ptr = unsafe { self.inner.alloc(layout) }; |
| 97 | + if !ptr.is_null() { |
| 98 | + let _ = THREAD_ALLOCATIONS.try_with(|c| { |
| 99 | + let next = c.get().increment(layout.size() as u64); |
| 100 | + c.set(next); |
| 101 | + }); |
| 102 | + } |
| 103 | + ptr |
| 104 | + } |
| 105 | + |
| 106 | + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { |
| 107 | + unsafe { self.inner.dealloc(ptr, layout) }; |
| 108 | + let _ = THREAD_ALLOCATIONS.try_with(|c| { |
| 109 | + let next = c.get().decrement(layout.size() as u64); |
| 110 | + c.set(next); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { |
| 115 | + let ptr = unsafe { self.inner.alloc_zeroed(layout) }; |
| 116 | + if !ptr.is_null() { |
| 117 | + let _ = THREAD_ALLOCATIONS.try_with(|c| { |
| 118 | + let next = c.get().increment(layout.size() as u64); |
| 119 | + c.set(next); |
| 120 | + }); |
| 121 | + } |
| 122 | + ptr |
| 123 | + } |
| 124 | + |
| 125 | + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { |
| 126 | + let new_ptr = unsafe { self.inner.realloc(ptr, layout, new_size) }; |
| 127 | + // Note: if `new_ptr` is null, no deallocation or allocation |
| 128 | + // happened, `ptr` remains valid. |
| 129 | + if !new_ptr.is_null() { |
| 130 | + let _ = THREAD_ALLOCATIONS.try_with(|c| { |
| 131 | + let next = c |
| 132 | + .get() |
| 133 | + .decrement(layout.size() as u64) |
| 134 | + .increment(new_size as u64); |
| 135 | + c.set(next); |
| 136 | + }); |
| 137 | + } |
| 138 | + new_ptr |
| 139 | + } |
| 140 | +} |
0 commit comments