use std::ffi::OsStr;
use std::os::fd::{AsFd as _, OwnedFd};
use rustix::io::Errno;
use super::{cgroup_id, find_descendant, open_cgroup, pieces, traverse, LeafDir, Tree, Visit, Walked, PATH_MAX_BYTES};
use crate::containment::cgroup::dir_walk_tests::{enter_private_mount_ns, open_path, DeepTree, Scratch};
use crate::test_groups::{cgroup, Group};
fn names_of(lens: &[usize]) -> Vec<String> {
lens.iter().map(|&n| "n".repeat(n)).collect()
}
fn runs(lens: &[usize]) -> Vec<std::ops::Range<usize>> {
let names = names_of(lens);
let names: Vec<&OsStr> = names.iter().map(OsStr::new).collect();
pieces(&names, PATH_MAX_BYTES)
}
#[skuld::test]
fn a_piece_of_exactly_path_max_minus_one_bytes_is_one_piece() {
assert_eq!(runs(&[255; 17]), [0..16, 16..17]);
assert_eq!(runs(&[255; 16]), vec![0..16]);
}
#[skuld::test]
fn a_piece_of_path_max_bytes_is_split() {
let mut lens = vec![255; 15];
lens.extend([256, 255]);
assert_eq!(runs(&lens), [0..15, 15..17]);
}
#[skuld::test]
fn a_name_that_nearly_fills_a_piece_stands_alone() {
assert_eq!(runs(&[300, 4000, 300]), [0..1, 1..2, 2..3]);
assert_eq!(runs(&[100, 4000, 100, 100]), [0..1, 1..2, 2..4]);
assert_eq!(runs(&[]), Vec::<std::ops::Range<usize>>::new());
}
#[skuld::test]
fn a_name_longer_than_a_piece_is_a_run_of_its_own() {
assert_eq!(runs(&[5000]), vec![0..1]);
assert_eq!(runs(&[100, 5000, 100]), [0..1, 1..2, 2..3]);
}
fn long_names(count: usize) -> Vec<String> {
names_of(&vec![200; count])
}
#[skuld::test]
fn a_symlink_at_the_split_of_a_long_path_is_refused() {
use rustix::fs::AtFlags;
let names = long_names(22);
for symlink_at in [19, 20] {
let tmp = tempfile::tempdir().expect("tempdir");
let root = open_path(tmp.path());
let real = tmp.path().join("real");
std::fs::create_dir_all(real.join(&names[0])).expect("make the symlink's target");
let chain = DeepTree::with_names(&root, names[..symlink_at].to_vec());
let parent = chain.bottom();
rustix::fs::symlinkat(&real, parent, names[symlink_at].as_str()).expect("make the symlink");
let opened = open_cgroup(root.as_fd(), &names.iter().map(OsStr::new).collect::<Vec<_>>());
rustix::fs::unlinkat(parent, names[symlink_at].as_str(), AtFlags::empty()).expect("remove the symlink");
assert!(
matches!(opened, Err(Errno::LOOP | Errno::NOTDIR)),
"a symlink as name {} was followed: {opened:?}",
symlink_at + 1
);
}
}
#[skuld::test]
fn cgroup_a_mount_at_the_split_of_a_long_path_is_not_crossed(#[fixture(cgroup)] _group: &Group) {
crate::log_capture::install();
for mounted in [20, 21] {
let scratch = Scratch::new(&format!("split-mount-{mounted}"));
let root = scratch.fd();
let chain = DeepTree::new(&root, &"n".repeat(200), 25);
let bottom = cgroup_id(chain.bottom().as_fd()).expect("the bottom's id");
let names = long_names(mounted);
let names: Vec<&OsStr> = names.iter().map(OsStr::new).collect();
let victim = tempfile::tempdir().expect("tempdir");
std::fs::create_dir_all(victim.path().join("keep/me")).expect("make the victim's empty dirs");
let (walked, swept, logs) = std::thread::scope(|scope| {
scope
.spawn(|| {
enter_private_mount_ns();
let root = scratch.fd();
let level = open_cgroup(root.as_fd(), &names[..mounted]).expect("open the level to mount over");
let _mounted = BindOver::new(victim.path(), &level);
let mark = crate::log_capture::mark();
let walked = find_descendant(root.as_fd(), bottom);
let swept = LeafDir::open_for_test(scratch.path()).remove_children();
let logs = crate::log_capture::records_since_on_current_thread(mark, "is behind a mount");
(walked, swept, logs)
})
.join()
.expect("the walking thread")
});
assert!(matches!(walked, Walked::Unknown(_)), "mount at {mounted}: {walked:?}");
assert_eq!(
logs.iter().filter(|(level, _)| *level == log::Level::Debug).count(),
2,
"mount at {mounted}: {logs:?}"
);
assert!(
swept.is_ok(),
"mount at {mounted}: the sweep failed: {}",
swept.as_ref().err().map_or(String::new(), ToString::to_string)
);
assert!(
victim.path().join("keep/me").is_dir(),
"mount at {mounted}: the sweep crossed the mount and deleted the victim's directories"
);
assert!(
matches!(find_descendant(root.as_fd(), bottom), Walked::Found),
"mount at {mounted}: the sweep removed what is beyond the mount"
);
}
}
struct BindOver(std::ffi::CString);
impl BindOver {
fn new(source: &std::path::Path, target: &OwnedFd) -> BindOver {
let source = std::ffi::CString::new(source.as_os_str().as_encoded_bytes()).expect("no NUL");
let target = std::ffi::CString::new(super::fd_path(target.as_fd())).expect("no NUL");
let rc = unsafe {
libc::mount(
source.as_ptr(),
target.as_ptr(),
std::ptr::null(),
libc::MS_BIND,
std::ptr::null(),
)
};
assert_eq!(rc, 0, "bind over the cgroup: {}", std::io::Error::last_os_error());
BindOver(target)
}
}
impl Drop for BindOver {
fn drop(&mut self) {
let rc = unsafe { libc::umount2(self.0.as_ptr(), 0) };
assert_eq!(rc, 0, "unmount the bind: {}", std::io::Error::last_os_error());
}
}
#[cfg(debug_assertions)]
#[skuld::test]
fn open_cgroup_refuses_a_name_that_is_not_one_component() {
let tmp = tempfile::tempdir().expect("tempdir");
let root = open_path(tmp.path());
for name in ["..", ".", "a/b", "/abs", ""] {
let refused = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
drop(open_cgroup(root.as_fd(), &[OsStr::new(name)]));
}));
let message = refused
.expect_err(&format!("{name:?} was taken"))
.downcast::<String>()
.map_or_else(|_| String::new(), |m| *m);
assert!(
message.contains("every name is one plain path component"),
"{name:?}: {message}"
);
}
}
fn sweep_and_walk(tag: &str, lens: &[usize]) {
let scratch = Scratch::new(tag);
let root = scratch.fd();
let chain = DeepTree::with_names(&root, names_of(lens));
let bottom = cgroup_id(chain.bottom().as_fd()).expect("the bottom's id");
let walked = find_descendant(root.as_fd(), bottom);
assert!(matches!(walked, Walked::Found), "{walked:?}");
let walked = find_descendant(root.as_fd(), 1);
assert!(matches!(walked, Walked::Absent), "{walked:?}");
let leaf = LeafDir::open_for_test(scratch.path());
let swept = leaf.remove_children();
assert_eq!(swept.expect("the sweep"), lens.len());
assert!(
std::fs::read_dir(scratch.path())
.expect("list the scratch cgroup")
.flatten()
.all(|e| !e.file_type().is_ok_and(|t| t.is_dir())),
"the first cgroup is removed"
);
assert!(leaf.swept().holds(bottom), "the bottom's id is recorded");
}
#[skuld::test]
fn cgroup_pieces_at_the_edge_of_path_max_are_opened(#[fixture(cgroup)] _group: &Group) {
sweep_and_walk("edge-4095", &[255; 17]);
let mut lens = vec![255; 15];
lens.extend([256, 255]);
sweep_and_walk("edge-4096", &lens);
}
#[skuld::test]
fn cgroup_names_over_name_max_are_swept_and_walked(#[fixture(cgroup)] _group: &Group) {
sweep_and_walk("names-300", &[300; 20]);
sweep_and_walk("names-whole-piece", &[4000, 4000, 300, 4000]);
sweep_and_walk("names-4095", &[100, 4095, 100]);
}
mod peak {
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
thread_local! {
static ARMED: Cell<bool> = const { Cell::new(false) };
static LIVE: Cell<isize> = const { Cell::new(0) };
static PEAK: Cell<isize> = const { Cell::new(0) };
}
struct Counting;
fn grow(bytes: usize) {
if ARMED.try_with(Cell::get) == Ok(true) {
let live = LIVE.with(Cell::get) + bytes as isize;
LIVE.with(|l| l.set(live));
PEAK.with(|p| p.set(p.get().max(live)));
}
}
fn shrink(bytes: usize) {
if ARMED.try_with(Cell::get) == Ok(true) {
LIVE.with(|l| l.set(l.get() - bytes as isize));
}
}
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let p = unsafe { System.alloc(layout) };
if !p.is_null() {
grow(layout.size());
}
p
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
shrink(layout.size());
unsafe { System.dealloc(ptr, layout) };
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let p = unsafe { System.realloc(ptr, layout, new_size) };
if !p.is_null() {
shrink(layout.size());
grow(new_size);
}
p
}
}
#[global_allocator]
static ALLOCATOR: Counting = Counting;
pub(super) fn of<T>(body: impl FnOnce() -> T) -> (T, usize) {
LIVE.with(|l| l.set(0));
PEAK.with(|p| p.set(0));
ARMED.with(|a| a.set(true));
let result = body();
ARMED.with(|a| a.set(false));
(result, PEAK.with(Cell::get).max(0) as usize)
}
}
#[skuld::test]
fn cgroup_the_traversal_holds_memory_linear_in_the_names_of_a_tree(#[fixture(cgroup)] _group: &Group) {
const DEPTH: usize = 100;
const NAME: usize = 200;
let scratch = Scratch::new("stored");
let root = scratch.fd();
let _chain = DeepTree::new(&root, &"n".repeat(NAME), DEPTH);
let mut tree = Tree::new();
let (traversed, peak) = peak::of(|| traverse(&mut tree, root.as_fd(), true, |_, _, _| Ok(Visit::Continue)));
assert!(traversed.is_ok(), "the traversal failed");
assert!(
peak < 10 * DEPTH * NAME,
"{peak} bytes held at once for {} bytes of names",
DEPTH * NAME
);
}