use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::path::Path;
use std::ptr;
use std::sync::atomic::{AtomicU8, Ordering};
std::thread_local! {
static COUNTED: Cell<bool> = const { Cell::new(false) };
}
#[must_use]
#[inline]
pub fn counted() -> bool {
COUNTED.try_with(Cell::get).unwrap_or(false)
}
pub fn count_this_thread() {
COUNTED.with(|c| c.set(true));
}
pub fn stop_counting_this_thread() {
COUNTED.with(|c| c.set(false));
}
pub fn others_are_not_counted(live: impl Fn() -> usize) {
let step = AtomicU8::new(0);
std::thread::scope(|s| {
s.spawn(|| {
while step.load(Ordering::Acquire) != 1 {
std::thread::yield_now();
}
let block = std::hint::black_box(vec![1_u8; 1 << 16]);
step.store(2, Ordering::Release);
while step.load(Ordering::Acquire) != 3 {
std::thread::yield_now();
}
drop(block);
});
let before = live();
step.store(1, Ordering::Release);
while step.load(Ordering::Acquire) != 2 {
std::thread::yield_now();
}
let during = live();
step.store(3, Ordering::Release);
assert_eq!(
during, before,
"another thread's allocation moved the count"
);
});
}
pub trait Tally: Sync {
fn refuses(&self, size: usize) -> bool {
let _ = size;
false
}
fn refuses_realloc(&self, new_size: usize) -> bool {
self.refuses(new_size)
}
fn growing(&self, size: usize) {
let _ = size;
}
fn failed(&self, size: usize) {
let _ = size;
}
fn grew(&self, size: usize, zeroed: bool);
fn shrank(&self, size: usize);
fn shrunk(&self, old: usize, new: usize) {
self.shrank(old);
self.grew(new, false);
}
}
pub struct Allocator<T>(pub T);
const MIN_ALIGN: usize = if cfg!(target_pointer_width = "64") {
16
} else {
8
};
struct NoUnwind;
impl Drop for NoUnwind {
fn drop(&mut self) {
std::process::abort();
}
}
#[inline]
fn no_unwind<R>(f: impl FnOnce() -> R) -> R {
let armed = NoUnwind;
let r = f();
std::mem::forget(armed);
r
}
impl<T: Tally> Allocator<T> {
fn before(&self, size: usize) -> bool {
let tell = counted();
if tell {
no_unwind(|| self.0.growing(size));
}
tell
}
fn after(&self, tell: bool, p: *mut u8, size: usize, zeroed: bool) -> *mut u8 {
if tell {
if p.is_null() {
no_unwind(|| self.0.failed(size));
} else {
no_unwind(|| self.0.grew(size, zeroed));
}
}
p
}
}
unsafe impl<T: Tally> GlobalAlloc for Allocator<T> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if no_unwind(|| self.0.refuses(layout.size())) {
return ptr::null_mut();
}
let tell = self.before(layout.size());
let p = unsafe { System.alloc(layout) };
self.after(tell, p, layout.size(), false)
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
if no_unwind(|| self.0.refuses(layout.size())) {
return ptr::null_mut();
}
let tell = self.before(layout.size());
let p = unsafe { System.alloc_zeroed(layout) };
self.after(tell, p, layout.size(), true)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
if counted() {
no_unwind(|| self.0.shrank(layout.size()));
}
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if no_unwind(|| self.0.refuses_realloc(new_size)) {
return ptr::null_mut();
}
let in_place = layout.align() <= MIN_ALIGN && layout.align() <= new_size;
let shrinking = new_size <= layout.size() && in_place;
let tell = if shrinking {
counted()
} else {
self.before(new_size)
};
let p = unsafe { System.realloc(ptr, layout, new_size) };
if tell && p.is_null() && !shrinking {
no_unwind(|| self.0.failed(new_size));
}
if !p.is_null() && tell {
no_unwind(|| {
if shrinking {
self.0.shrunk(layout.size(), new_size);
} else {
self.0.grew(new_size, false);
self.0.shrank(layout.size());
}
});
}
p
}
}
#[must_use]
pub fn stray_allocators(root: &Path) -> Vec<String> {
let mut files = Vec::new();
crate::guard::rust_files(root, &mut files);
files.sort();
let mut found = Vec::new();
for path in files {
let text =
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
let hits: Vec<String> = text
.lines()
.enumerate()
.filter(|(_, line)| {
!line.trim_start().starts_with("//") && line.contains("GlobalAlloc for")
})
.map(|(i, _)| format!("{}:{}", path.display(), i + 1))
.collect();
if path.ends_with("unityrs-testkit/src/counted.rs") {
continue;
}
if path.ends_with("unityrs-batch/src/alloc.rs") {
if hits.len() != 1 {
found.push(crate::guard::exemption_broken(
&path,
hits.len(),
"implementations",
));
}
continue;
}
found.extend(hits);
}
found
}
#[cfg(test)]
mod tests {
use std::alloc::{GlobalAlloc, Layout, System};
use std::hint::black_box;
use std::sync::Mutex;
use super::{Allocator, Tally, count_this_thread, stray_allocators};
struct Log(Mutex<Vec<i64>>);
impl Tally for Log {
fn refuses(&self, size: usize) -> bool {
size == 12_345
}
fn grew(&self, size: usize, _zeroed: bool) {
self.0.lock().unwrap().push(i64::try_from(size).unwrap());
}
fn shrank(&self, size: usize) {
self.0.lock().unwrap().push(-i64::try_from(size).unwrap());
}
}
impl Log {
fn take(&self) -> Vec<i64> {
std::mem::take(&mut *self.0.lock().unwrap())
}
}
const HUGE: usize = usize::MAX / 4;
#[test]
fn a_shrink_below_alignment_counts_both_live_blocks() {
count_this_thread();
let a = Allocator(Log(Mutex::new(Vec::new())));
let layout = Layout::from_size_align(64, super::MIN_ALIGN).unwrap();
let small = Layout::from_size_align(1, layout.align()).unwrap();
unsafe {
let p = a.alloc(layout);
assert!(!p.is_null());
p.write(42);
let q = a.realloc(p, layout, small.size());
assert!(!q.is_null());
assert_eq!(q.read(), 42);
a.dealloc(q, small);
}
assert_eq!(a.0.take(), [64, 1, -64, -1]);
}
#[test]
fn an_exempt_allocator_with_two_implementations_is_reported() {
let dir = crate::scratch::Scratch::new("allocator-exemption");
let file = dir.join("unityrs-batch/src/alloc.rs");
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(
&file,
"impl GlobalAlloc for A {}\nimpl GlobalAlloc for B {}\n",
)
.unwrap();
assert_eq!(
stray_allocators(dir.path()),
[format!(
"{}: 2 implementations, the exemption is for one",
file.display()
)]
);
}
#[test]
fn a_failed_realloc_keeps_the_old_block_counted() {
count_this_thread();
let a = Allocator(Log(Mutex::new(Vec::new())));
let layout = Layout::from_size_align(64, 8).unwrap();
unsafe {
let p = a.alloc(layout);
assert!(!p.is_null());
assert_eq!(a.0.take(), [64]);
let q = black_box(a.realloc(p, layout, HUGE));
assert!(q.is_null(), "System met a request of {HUGE} bytes");
assert!(a.0.take().is_empty(), "a failed realloc moved the count");
let q = a.realloc(p, layout, 4096);
assert!(!q.is_null());
assert_eq!(
a.0.take(),
[4096, -64],
"the new block before the old is freed"
);
let big = Layout::from_size_align(4096, 8).unwrap();
assert!(a.realloc(q, big, 12_345).is_null(), "a refusal");
assert!(
a.alloc(Layout::from_size_align(12_345, 8).unwrap())
.is_null()
);
assert!(a.0.take().is_empty(), "a refusal counted");
a.dealloc(q, big);
assert_eq!(a.0.take(), [-4096]);
let z = a.alloc_zeroed(layout);
assert!(!z.is_null() && *z == 0);
a.dealloc(z, layout);
assert_eq!(a.0.take(), [64, -64]);
std::thread::scope(|s| {
s.spawn(|| {
let p = a.alloc(layout);
a.dealloc(p, layout);
});
});
assert!(a.0.take().is_empty(), "another thread counted");
assert!(black_box(System.alloc(Layout::from_size_align(HUGE, 8).unwrap())).is_null());
}
}
struct Panicking;
impl Tally for Panicking {
fn grew(&self, _: usize, _: bool) {
panic!("the tally's own panic");
}
fn shrank(&self, _: usize) {
panic!("the tally's own panic");
}
}
const UNWIND_VAR: &str = "UNITYRS_TESTKIT_TALLY_PANIC";
#[test]
#[ignore = "run by a_tally_that_panics_aborts_and_never_unwinds"]
fn tally_panic_probe() {
let Ok(what) = std::env::var(UNWIND_VAR) else {
return;
};
if let Some(spec) = what.strip_prefix("killed:") {
crate::child::kill_self_while(spec);
}
count_this_thread();
let a = Allocator(Panicking);
let layout = Layout::from_size_align(64, 8).unwrap();
let unwound = std::panic::catch_unwind(|| {
black_box(unsafe { a.alloc(layout) });
});
assert!(unwound.is_ok(), "the tally's panic unwound out of alloc");
}
#[cfg(unix)]
#[test]
fn a_tally_that_panics_aborts_and_never_unwinds() {
let marks = crate::child::Marks::new("tally-panic-once");
for (spec, killed) in [
("1".to_owned(), 0),
(format!("killed:{}", marks.killed(1)), 1),
] {
tally_panic_aborts(&spec);
assert_eq!(marks.runs(), killed, "{spec}");
}
}
#[cfg(unix)]
fn tally_panic_aborts(spec: &str) {
use std::os::unix::process::ExitStatusExt;
let exe = std::env::current_exe().unwrap();
let out = crate::child::output(
std::process::Command::new(exe)
.args([
"--exact",
"counted::tests::tally_panic_probe",
"--ignored",
"--test-threads=1",
"--nocapture",
])
.env(UNWIND_VAR, spec),
)
.unwrap();
let err = String::from_utf8_lossy(&out.stderr);
assert_eq!(
out.status.signal(),
Some(6),
"{spec}: the probe did not abort: {:?}\n{err}",
out.status
);
assert!(err.contains("the tally's own panic"), "{spec}: {err}");
assert!(
!err.contains("panic in a function that cannot unwind"),
"{spec}: std's nounwind abort reached instead of our guard: {err}"
);
}
struct FreshOnly(Log);
impl Tally for FreshOnly {
fn refuses(&self, size: usize) -> bool {
self.0.refuses(size)
}
fn refuses_realloc(&self, _new_size: usize) -> bool {
false
}
fn grew(&self, size: usize, zeroed: bool) {
self.0.grew(size, zeroed);
}
fn shrank(&self, size: usize) {
self.0.shrank(size);
}
}
#[test]
fn a_tally_may_refuse_fresh_allocations_and_no_realloc() {
count_this_thread();
let a = Allocator(FreshOnly(Log(Mutex::new(Vec::new()))));
let layout = Layout::from_size_align(64, 8).unwrap();
let refused = Layout::from_size_align(12_345, 8).unwrap();
unsafe {
assert!(a.alloc(refused).is_null());
assert!(a.alloc_zeroed(refused).is_null());
let p = a.alloc(layout);
assert!(!p.is_null());
let q = a.realloc(p, layout, 12_345);
assert!(!q.is_null(), "a realloc refused");
a.dealloc(q, refused);
assert_eq!(a.0.0.take(), [64, 12_345, -64, -12_345]);
}
}
#[test]
fn every_global_allocator_in_the_workspace_is_this_one() {
let crates = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
let found = stray_allocators(&crates);
assert!(
found.is_empty(),
"global allocators of their own, whose counts no test checks:\n{}",
found.join("\n")
);
}
}