#[must_use]
pub struct Scope {
#[cfg(feature = "realtime-audit")]
_scope: enabled::Scope,
}
#[inline]
pub fn processing() -> Scope {
Scope {
#[cfg(feature = "realtime-audit")]
_scope: enabled::Scope::enter(enabled::Context::Host),
}
}
#[inline]
pub fn plugin() -> Scope {
Scope {
#[cfg(feature = "realtime-audit")]
_scope: enabled::Scope::enter(enabled::Context::Plugin),
}
}
#[inline]
pub fn query() -> Scope {
#[cfg(feature = "realtime-audit")]
enabled::count_query();
plugin()
}
#[inline]
pub fn callback() -> Scope {
Scope {
#[cfg(feature = "realtime-audit")]
_scope: enabled::Scope::enter(enabled::Context::Callback),
}
}
#[cfg(feature = "realtime-audit")]
pub use enabled::{Allocator, processing_allocations, report};
#[cfg(feature = "realtime-audit")]
mod enabled {
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Context {
Outside,
Host,
Plugin,
Callback,
}
thread_local! {
static CONTEXT: Cell<Context> = const { Cell::new(Context::Outside) };
}
static COUNTS: [[AtomicU64; 2]; 2] = [
[AtomicU64::new(0), AtomicU64::new(0)],
[AtomicU64::new(0), AtomicU64::new(0)],
];
static QUERIES: AtomicU64 = AtomicU64::new(0);
pub struct Scope {
enclosing: Context,
entered: Context,
}
impl Scope {
pub fn enter(context: Context) -> Scope {
let enclosing = CONTEXT.get();
let entered = match (enclosing, context) {
(Context::Outside, Context::Host) => {
rtsan_standalone::realtime_enter();
Context::Host
}
(Context::Host | Context::Callback, Context::Plugin) => {
rtsan_standalone::disable();
Context::Plugin
}
(Context::Plugin, Context::Callback) => {
rtsan_standalone::enable();
Context::Callback
}
_ => enclosing,
};
CONTEXT.set(entered);
Scope { enclosing, entered }
}
}
impl Drop for Scope {
fn drop(&mut self) {
match (self.enclosing, self.entered) {
(Context::Outside, Context::Host) => rtsan_standalone::realtime_exit(),
(Context::Host | Context::Callback, Context::Plugin) => rtsan_standalone::enable(),
(Context::Plugin, Context::Callback) => rtsan_standalone::disable(),
_ => {}
}
CONTEXT.set(self.enclosing);
}
}
pub fn count_query() {
if matches!(CONTEXT.get(), Context::Host | Context::Callback) {
QUERIES.fetch_add(1, Ordering::Relaxed);
}
}
fn count(free: bool) {
let row = match CONTEXT.get() {
Context::Host => 0,
Context::Callback => 1,
Context::Outside | Context::Plugin => return,
};
COUNTS[row][usize::from(free)].fetch_add(1, Ordering::Relaxed);
}
pub struct Allocator;
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
count(false);
unsafe { System.alloc(layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
count(false);
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
count(true);
unsafe { System.dealloc(pointer, layout) }
}
unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, size: usize) -> *mut u8 {
count(false);
unsafe { System.realloc(pointer, layout, size) }
}
}
pub fn processing_allocations() -> (u64, u64) {
let [allocations, frees] = &COUNTS[0];
(
allocations.load(Ordering::Relaxed),
frees.load(Ordering::Relaxed),
)
}
pub fn report() -> String {
let [[host, host_freed], [callback, callback_freed]] = COUNTS
.each_ref()
.map(|row| row.each_ref().map(|count| count.load(Ordering::Relaxed)));
format!(
"{} {host} allocations and {host_freed} frees in processing, {callback} allocations \
and {callback_freed} frees in host callbacks, {} plugin queries",
crate::messages::AUDIT,
QUERIES.load(Ordering::Relaxed)
)
}
}