Skip to main content

unityrs_testkit/
address_space.rs

1//! A cap on the process's address space: how a test makes the C allocator
2//! refuse.
3//!
4//! liblzma allocates its dictionary with C's `malloc`, which no Rust global
5//! allocator sees, so a [`crate::counted::Allocator`] cannot refuse it. A cap
6//! on the address space can (`RLIMIT_AS`): a mapping, or a `brk`, that would
7//! take the process past it fails, so `malloc` returns NULL and liblzma
8//! answers `LZMA_MEM_ERROR`. The cap is process-wide, so a test sets it only
9//! in a child process (this test binary again, running one ignored probe
10//! test, [`run`]), and there only around the call it is about:
11//! [`cap_above_current`] caps the soft limit a margin above what the process
12//! maps already, and the [`Capped`] it returns puts the soft limit back
13//! when it drops (a process may raise its soft limit up to the hard one).
14//!
15//! **One arena.** glibc gives a thread other than the main one an arena of
16//! its own: a 64 MiB reservation, already mapped (so already under any cap
17//! set later), that it grows into with `mprotect`. When the `mmap` of a
18//! large block fails, glibc grows that arena instead, so a block of up to
19//! 64 MiB passes the cap (libtest runs each test on a thread of its own).
20//! [`run`] starts the child with `MALLOC_ARENA_MAX=1`: every thread then
21//! takes the main arena, which grows with `brk`, which the cap refuses too.
22//! A probe also allocates and frees no large block before its cap: glibc
23//! keeps the memory of such a free (a freed `mmap`ped chunk raises its
24//! `mmap` threshold, and the heap then keeps what is freed below it) and
25//! serves a later block from it, which no cap sees.
26//!
27//! Linux only, on x86-64 and 64-bit Arm (whose `RLIMIT_AS` is 9 and `rlim_t`
28//! 64 bits, as declared here: the crate takes no `libc` dependency for two
29//! calls). Elsewhere [`SUPPORTED`] is false and [`cap_above_current`] is
30//! [`std::io::ErrorKind::Unsupported`]: a caller says it skipped.
31//!
32//! Owner: package C (its codec tests and the TPK's).
33
34use std::io;
35use std::process::{Command, Output};
36
37use crate::stack::{Probe, classify};
38
39/// Whether this target can cap its address space here.
40pub const SUPPORTED: bool = imp::SUPPORTED;
41
42/// The address space this process maps now, in bytes (`VmSize` in
43/// `/proc/self/status`).
44///
45/// # Errors
46/// When the target has no `/proc/self/status`, or it names no `VmSize`.
47pub fn mapped() -> io::Result<u64> {
48    let status = std::fs::read_to_string("/proc/self/status")?;
49    status
50        .lines()
51        .find_map(|l| l.strip_prefix("VmSize:"))
52        .and_then(|v| v.trim().strip_suffix("kB"))
53        .and_then(|kb| kb.trim().parse::<u64>().ok())
54        .map(|kb| kb * 1024)
55        .ok_or_else(|| io::Error::other("no VmSize in /proc/self/status"))
56}
57
58/// Runs this test binary's ignored test `probe` (its whole path, as
59/// `--exact` matches it) in a child process with `var` set to `value`, on
60/// one malloc arena (module docs). A child killed by `SIGKILL` from outside
61/// is run again, up to [`crate::child::KILLED_RUNS`] runs in all
62/// ([`crate::child::output`]; review round 8: one kill failed the test).
63///
64/// # Panics
65/// When the child can not start, comes to anything but a passed probe
66/// ([`classify`]), or is killed by `SIGKILL` on every run: the message says
67/// what, and ends with the child's last lines of output.
68#[track_caller]
69pub fn run(probe: &str, var: &str, value: &str) {
70    let exe = std::env::current_exe().expect("the test binary's path");
71    let Output {
72        status,
73        stdout,
74        stderr,
75    } = crate::child::output(
76        Command::new(exe)
77            .args(["--exact", probe, "--ignored", "--test-threads=1"])
78            .env(var, value)
79            .env("MALLOC_ARENA_MAX", "1"),
80    )
81    .unwrap_or_else(|e| panic!("cannot start probe {probe}: {e}"));
82    match classify(status, &stdout, &stderr) {
83        Ok(Probe::Fits) => {}
84        Ok(Probe::Overflows) => panic!("probe {probe} overflowed a stack"),
85        Err(e) => panic!("probe {probe}: {e}"),
86    }
87}
88
89/// The soft address-space limit this process had before a
90/// [`cap_above_current`], put back when it drops.
91#[derive(Debug)]
92#[must_use = "the cap lasts only while this lives"]
93pub struct Capped {
94    old: u64,
95    /// The soft limit set.
96    pub cap: u64,
97}
98
99impl Drop for Capped {
100    fn drop(&mut self) {
101        // Raising the soft limit back up to the hard one is always allowed.
102        let _ = imp::set_soft(self.old);
103    }
104}
105
106/// Caps this process's address space `margin` bytes above what it maps
107/// now ([`mapped`]): an allocation of more than `margin` bytes in one
108/// piece then fails, while smaller ones made before it fit.
109///
110/// # Errors
111/// When the target is not supported ([`SUPPORTED`]), or the limit cannot
112/// be read or set (a hard limit below the cap).
113pub fn cap_above_current(margin: u64) -> io::Result<Capped> {
114    let (old, hard) = imp::get()?;
115    let cap = mapped()?.saturating_add(margin);
116    if cap > hard {
117        return Err(io::Error::other(format!(
118            "the hard address-space limit, {hard} bytes, is below the cap, {cap}"
119        )));
120    }
121    imp::set_soft(cap)?;
122    Ok(Capped { old, cap })
123}
124
125#[cfg(all(
126    target_os = "linux",
127    any(target_arch = "x86_64", target_arch = "aarch64")
128))]
129mod imp {
130    use std::io;
131
132    pub const SUPPORTED: bool = true;
133
134    /// `RLIMIT_AS` on Linux x86-64 and 64-bit Arm.
135    const RLIMIT_AS: i32 = 9;
136
137    /// `struct rlimit`: `rlim_t` is `unsigned long`, 64 bits here.
138    #[repr(C)]
139    struct Rlimit {
140        cur: u64,
141        max: u64,
142    }
143
144    unsafe extern "C" {
145        fn getrlimit(resource: i32, rlim: *mut Rlimit) -> i32;
146        fn setrlimit(resource: i32, rlim: *const Rlimit) -> i32;
147    }
148
149    /// The soft and the hard limit.
150    pub fn get() -> io::Result<(u64, u64)> {
151        let mut r = Rlimit { cur: 0, max: 0 };
152        // SAFETY: `r` is a valid, writable `struct rlimit` for the call.
153        if unsafe { getrlimit(RLIMIT_AS, &raw mut r) } != 0 {
154            return Err(io::Error::last_os_error());
155        }
156        Ok((r.cur, r.max))
157    }
158
159    /// Sets the soft limit to `cur`, keeping the hard one.
160    pub fn set_soft(cur: u64) -> io::Result<()> {
161        let (_, max) = get()?;
162        let r = Rlimit { cur, max };
163        // SAFETY: `r` is a valid `struct rlimit`, only read by the call.
164        if unsafe { setrlimit(RLIMIT_AS, &raw const r) } != 0 {
165            return Err(io::Error::last_os_error());
166        }
167        Ok(())
168    }
169}
170
171#[cfg(not(all(
172    target_os = "linux",
173    any(target_arch = "x86_64", target_arch = "aarch64")
174)))]
175mod imp {
176    use std::io;
177
178    pub const SUPPORTED: bool = false;
179
180    fn unsupported() -> io::Error {
181        io::Error::new(
182            io::ErrorKind::Unsupported,
183            "an address-space cap is set on Linux x86-64 and 64-bit Arm only",
184        )
185    }
186
187    pub fn get() -> io::Result<(u64, u64)> {
188        Err(unsupported())
189    }
190
191    pub fn set_soft(_: u64) -> io::Result<()> {
192        Err(unsupported())
193    }
194}
195
196#[cfg(test)]
197mod tests {
198    use super::{SUPPORTED, cap_above_current, mapped, run};
199
200    const VAR: &str = "UNITYRS_TESTKIT_ADDRESS_SPACE_PROBE";
201
202    /// Run by the tests below, in a child: under a cap 4 MiB above what it
203    /// maps, an 8 MiB allocation fails (on this thread, libtest's own) and
204    /// a 64 KiB one does not; once the cap drops, the 8 MiB one succeeds.
205    /// `killed:N:DIR` is first killed by `SIGKILL` from outside on each of
206    /// its first `N` runs (`crate::child::kill_self_while`).
207    #[test]
208    #[ignore = "run by a_cap_refuses_what_passes_it_and_drops_with_its_guard"]
209    fn probe() {
210        let Ok(what) = std::env::var(VAR) else {
211            return;
212        };
213        if let Some(spec) = what.strip_prefix("killed:") {
214            crate::child::kill_self_while(spec);
215        }
216        let big = 8 << 20;
217        {
218            let capped = cap_above_current(4 << 20).unwrap();
219            assert!(capped.cap >= mapped().unwrap());
220            let mut v: Vec<u8> = Vec::new();
221            assert!(v.try_reserve_exact(big).is_err(), "8 MiB fit under the cap");
222            let mut w: Vec<u8> = Vec::new();
223            assert!(w.try_reserve_exact(64 << 10).is_ok());
224        }
225        let mut v: Vec<u8> = Vec::new();
226        assert!(
227            v.try_reserve_exact(big).is_ok(),
228            "the cap outlived its guard"
229        );
230    }
231
232    #[test]
233    fn a_cap_refuses_what_passes_it_and_drops_with_its_guard() {
234        if !SUPPORTED {
235            eprintln!("skipped: no address-space cap on this target");
236            return;
237        }
238        run("address_space::tests::probe", VAR, "1");
239    }
240
241    /// A probe the system kills from outside once is run again, and its
242    /// next run is the result (review round 8: the kill failed the test).
243    #[cfg(unix)]
244    #[test]
245    fn a_probe_killed_from_outside_once_is_run_again() {
246        if !SUPPORTED {
247            eprintln!("skipped: no address-space cap on this target");
248            return;
249        }
250        let marks = crate::child::Marks::new("address-space-once");
251        let spec = format!("killed:{}", marks.killed(1));
252        run("address_space::tests::probe", VAR, &spec);
253        assert_eq!(marks.runs(), 1, "the probe was not killed first");
254    }
255}