diff --git a/CHANGELOG.md b/CHANGELOG.md index 761142744..505c1af30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog +- **Fixed** On Linux, cached tasks no longer fail to spawn child processes in restricted sandboxes, for example rootless bubblewrap that denies the `seccomp` syscall. When the `seccomp` filter for file-access tracking cannot be installed, the process runs untracked instead and the run is reported as not cached. The not-cached message now reads "automatic file tracking couldn't record everything this task did", which fits both this case and a full tracking channel ([#700](https://github.com/voidzero-dev/vite-task/issues/700), [#701](https://github.com/voidzero-dev/vite-task/pull/701)). - **Fixed** `vp run ` in a package that doesn't define the task now reports `Task "" not found` when another package's `vite.config.*` fails to load, instead of failing with that package's error. Showing the tasks to choose from loads every package's config, so the task selector or task list is skipped then ([#987](https://github.com/voidzero-dev/vite-task/pull/987)). - **Fixed** A task specifier like `vp run nope#build`, where no workspace package is named `nope`, now fails with `Package 'nope' not found in the workspace` and exits 1, including from a task's command, instead of running nothing and exiting 0. A short name that auto-completes to several scoped packages, like `app` for `@a/app` and `@b/app`, now fails as ambiguous. `--filter nope` still only warns and exits 0, unless `--fail-if-no-match` is set ([#984](https://github.com/voidzero-dev/vite-task/pull/984)). - **Added** `vp run -vv` also lists what each cached run depends on, changed or not, and the output files a hit restores: in the detailed summary, under `Unchanged:` and `Outputs:`, and with `--dry-run --json`, as each step's `baseline.fingerprint` and `baseline.outputs`. It shows, for an unexpected hit, whether an edited file is an input at all. `-v` is now a counted flag, so `-vv` is the same as `--verbose --verbose` ([#969](https://github.com/voidzero-dev/vite-task/pull/969)). diff --git a/crates/fspy/src/error.rs b/crates/fspy/src/error.rs index 6df8e4a5d..e47f84d42 100644 --- a/crates/fspy/src/error.rs +++ b/crates/fspy/src/error.rs @@ -35,10 +35,13 @@ pub enum SpawnError { /// so the accesses collected for the run are a subset of what it really /// touched. /// +/// The tracking channel can be full, or a process can continue untracked +/// because the seccomp filter could not be installed. +/// /// The run itself is unaffected: recording must never stop the program /// doing the work. What cannot be done is anything that needs every /// access, caching above all, which has to treat the run as untracked /// rather than as having touched only the paths that fit. #[derive(thiserror::Error, Clone, Copy, PartialEq, Eq, Debug)] -#[error("the file-access records did not fit in the tracking channel")] +#[error("automatic file tracking could not record every file access")] pub struct TrackingIncomplete; diff --git a/crates/fspy/tests/static_executable.rs b/crates/fspy/tests/static_executable.rs index 6e04158ea..9de887a03 100644 --- a/crates/fspy/tests/static_executable.rs +++ b/crates/fspy/tests/static_executable.rs @@ -3,42 +3,15 @@ //! the CI builds with `-crt-static` the binary becomes dynamically linked, //! defeating the purpose of these tests. #![cfg(all(target_os = "linux", not(target_env = "musl")))] -use std::{ - fs::{self, Permissions}, - os::unix::fs::PermissionsExt as _, - path::{Path, PathBuf}, - sync::LazyLock, -}; +use std::path::Path; use fspy::PathAccessIterable; -use fspy_shared_unix::is_dynamically_linked_to_libc; use test_log::test; -use crate::test_utils::assert_contains; +use crate::test_utils::{assert_contains, test_bin_path}; mod test_utils; -const TEST_BIN_CONTENT: &[u8] = include_bytes!(env!("CARGO_BIN_FILE_FSPY_TEST_BIN")); - -fn test_bin_path() -> &'static Path { - static TEST_BIN_PATH: LazyLock = LazyLock::new(|| { - assert_eq!( - is_dynamically_linked_to_libc(TEST_BIN_CONTENT), - Ok(false), - "Test binary is not a static executable" - ); - - let tmp_dir = env!("CARGO_TARGET_TMPDIR"); - let test_bin_path = PathBuf::from(tmp_dir).join("fspy-test-bin"); - fs::write(&test_bin_path, TEST_BIN_CONTENT).expect("failed to write test binary"); - fs::set_permissions(&test_bin_path, Permissions::from_mode(0o755)) - .expect("failed to set permissions on test binary"); - - test_bin_path - }); - TEST_BIN_PATH.as_path() -} - async fn track_test_bin(args: &[&str], cwd: Option<&str>) -> PathAccessIterable { let mut cmd = fspy::Command::new(test_bin_path()); if let Some(cwd) = cwd { diff --git a/crates/fspy/tests/test_utils/mod.rs b/crates/fspy/tests/test_utils/mod.rs index 485b269fa..e8ded0561 100644 --- a/crates/fspy/tests/test_utils/mod.rs +++ b/crates/fspy/tests/test_utils/mod.rs @@ -155,3 +155,46 @@ pub async fn spawn_command(cmd: vt_subprocess_test::Command) -> anyhow::Result

&'static Path { + use std::{ + fs::{self, Permissions}, + os::unix::fs::PermissionsExt as _, + path::PathBuf, + sync::LazyLock, + }; + + use fspy_shared_unix::is_dynamically_linked_to_libc; + + const TEST_BIN_CONTENT: &[u8] = include_bytes!(env!("CARGO_BIN_FILE_FSPY_TEST_BIN")); + + static TEST_BIN_PATH: LazyLock = LazyLock::new(|| { + assert_eq!( + is_dynamically_linked_to_libc(TEST_BIN_CONTENT), + Ok(false), + "Test binary is not a static executable" + ); + + let test_bin_path = PathBuf::from(env!("CARGO_TARGET_TMPDIR")) + .join(concat!("fspy-test-bin-", env!("CARGO_CRATE_NAME"))); + fs::write(&test_bin_path, TEST_BIN_CONTENT).expect("failed to write test binary"); + fs::set_permissions(&test_bin_path, Permissions::from_mode(0o755)) + .expect("failed to set permissions on test binary"); + + test_bin_path + }); + TEST_BIN_PATH.as_path() +} diff --git a/crates/fspy/tests/untracked_fallback.rs b/crates/fspy/tests/untracked_fallback.rs new file mode 100644 index 000000000..c3e95b94b --- /dev/null +++ b/crates/fspy/tests/untracked_fallback.rs @@ -0,0 +1,128 @@ +//! Tests for the untracked-exec fallback: when the preload cannot install +//! the seccomp filter, the exec must proceed untracked and the run must be +//! reported as incompletely tracked (so it is not cached), rather than +//! every spawn failing. Skipped on musl: no preload library exists there. +#![cfg(all(target_os = "linux", not(target_env = "musl")))] + +use std::{ + ffi::OsStr, + fs::{self, Permissions}, + os::unix::{ffi::OsStrExt as _, fs::PermissionsExt as _}, + path::{Path, PathBuf}, + process::Command, +}; + +use allocator_api2::alloc::Global; +use fspy_seccomp_unotify::payload::SeccompPayload; +use fspy_shared::ipc::{ + IpcStr, + channel::{Receiver, RecordsLost, channel}, +}; +use fspy_shared_unix::payload::{Payload, encode_payload}; + +use crate::test_utils::test_bin_path; + +mod test_utils; + +/// The preload cdylib, built as a dependency of this crate. +const PRELOAD_CDYLIB: &str = env!("CARGO_CDYLIB_FILE_FSPY_PRELOAD_UNIX"); + +/// Clears `command`'s environment and preloads fspy into it with a seccomp +/// payload whose install always fails. Returns the channel the preload +/// reports to. +fn preload_with_failing_seccomp(command: &mut Command) -> Receiver { + let receiver = channel(1 << 30, Global).unwrap(); + { + let preload_path: &IpcStr = Path::new(PRELOAD_CDYLIB).into(); + let payload = Payload { + ipc_channel_conf: receiver.conf(), + preload_path, + seccomp_payload: SeccompPayload::unreachable( + b"/nonexistent/fspy-unreachable-supervisor".to_vec(), + ), + }; + let bump = bumpalo::Bump::new(); + let encoded = encode_payload(payload, &bump); + command + .env_clear() + .env("LD_PRELOAD", PRELOAD_CDYLIB) + .env("FSPY_PAYLOAD", OsStr::from_bytes(encoded.encoded_string.as_ref())); + } + receiver +} + +/// A static binary exec'd from a traced process needs the preload's inline +/// seccomp install. When that install fails (here: the payload's empty +/// filter, which the kernel refuses), the binary must still run — untracked +/// — and the channel must report the loss. +#[test] +fn static_binary_runs_untracked_when_injection_fails() { + let mut command = Command::new("/bin/sh"); + let receiver = preload_with_failing_seccomp(&mut command); + let output = command + .arg("-c") + .arg(format!("exec {} stat /hello", test_bin_path().display())) + .output() + .expect("failed to spawn the shell"); + assert!( + output.status.success(), + "the static binary did not run: {}", + String::from_utf8_lossy(&output.stderr) + ); + + let Err(RecordsLost) = receiver.close() else { + panic!("the channel did not report the untracked exec"); + }; +} + +/// The untracked fallback must replay the interposed call as it was made, +/// not as `execve`. GNU `env` runs its command through `execvp`, so a bare +/// program name found only on `PATH` still runs when injection fails; +/// forwarding it to `execve` would fail with `ENOENT` instead. +#[test] +fn execvp_fallback_keeps_path_search() { + let test_bin = test_bin_path(); + let mut command = Command::new("/usr/bin/env"); + let receiver = preload_with_failing_seccomp(&mut command); + let output = command + .arg(test_bin.file_name().unwrap()) + .arg("stat") + .arg("/hello") + .env("PATH", test_bin.parent().unwrap()) + .current_dir("/") + .output() + .expect("failed to spawn env"); + assert!( + output.status.success(), + "the execvp fallback lost its PATH search: {}", + String::from_utf8_lossy(&output.stderr) + ); + + let Err(RecordsLost) = receiver.close() else { + panic!("the channel did not report the untracked exec"); + }; +} + +/// Only a failed seccomp install takes the untracked path. An exec that +/// fails the way the real exec would, here `ENOEXEC` for a script without a +/// `#!` line, keeps its errno and leaves the trace complete. `sh` then runs +/// the script itself, as it does without tracking. +#[test] +fn exec_errors_are_not_reported_as_lost() { + let script = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("fspy-script-without-shebang"); + fs::write(&script, "exit 0\n").expect("failed to write the script"); + fs::set_permissions(&script, Permissions::from_mode(0o755)) + .expect("failed to set permissions on the script"); + + let mut command = Command::new("/bin/sh"); + let receiver = preload_with_failing_seccomp(&mut command); + let output = + command.arg("-c").arg(script.as_os_str()).output().expect("failed to spawn the shell"); + assert!( + output.status.success(), + "the script without a shebang did not run: {}", + String::from_utf8_lossy(&output.stderr) + ); + + assert!(receiver.close().is_ok(), "an authentic exec error was reported as an untracked exec"); +} diff --git a/crates/fspy_client_unix/src/lib.rs b/crates/fspy_client_unix/src/lib.rs index 9ba769751..d51c90f10 100644 --- a/crates/fspy_client_unix/src/lib.rs +++ b/crates/fspy_client_unix/src/lib.rs @@ -87,6 +87,17 @@ impl<'a> Client<'a> { ipc_sender.send(&mode.with_path(path.into())); } + /// Marks the run's trace incomplete, so the receiver treats it as + /// untracked and the runner does not cache it. Call it before the + /// process does something that tracking cannot record, such as an exec + /// that continues untracked because [`PreExec::run`] failed. It does + /// nothing when this client has no channel sender. + pub fn report_loss(&self) { + if let Some(ipc_sender) = &self.ipc_sender { + ipc_sender.report_loss(); + } + } + /// Resolves and reports an exec, returning the transformed arguments to /// forward and what to run before forwarding them. /// diff --git a/crates/fspy_preload_unix/src/interceptions/spawn/exec/mod.rs b/crates/fspy_preload_unix/src/interceptions/spawn/exec/mod.rs index 08c8c20f7..745577c1c 100644 --- a/crates/fspy_preload_unix/src/interceptions/spawn/exec/mod.rs +++ b/crates/fspy_preload_unix/src/interceptions/spawn/exec/mod.rs @@ -50,12 +50,20 @@ unsafe fn collect_argv( argv } +/// Resolves, reports, and performs a tracked exec. +/// +/// `untracked` performs the interposed call as the caller made it, through +/// that function's own original: `execvp` keeps its `PATH` search, +/// `execveat` its `dirfd` and flags, `fexecve` its descriptor. It runs when +/// the seccomp filter cannot be installed, so the process still execs, +/// untracked. fn handle_exec( allocator: impl Allocator, config: ExecResolveConfig, prog: *const libc::c_char, argv: *const *const libc::c_char, envp: *const *const libc::c_char, + untracked: impl FnOnce() -> libc::c_int, ) -> libc::c_int { let client = global_client().expect("exec unexpectedly called before client initialized in ctor"); @@ -69,10 +77,15 @@ fn handle_exec( } }; if let Some(pre_exec) = pre_exec - && let Err(errno) = pre_exec.run() + && pre_exec.run().is_err() { - errno.set(); - return -1; + // The seccomp filter cannot be installed, for example in a sandbox + // that denies the `seccomp` syscall. Mark the run's trace incomplete + // so it is not cached, then run the interposed call untracked. Its + // envp still carries LD_PRELOAD/FSPY_PAYLOAD, so each generation + // tries tracking again on its own. + client.report_loss(); + return untracked(); } // SAFETY: raw_command's strings and arrays live in `allocator`, which outlives the call unsafe { execve::original()(raw_command.prog, raw_command.argv, raw_command.envp) } @@ -94,6 +107,8 @@ unsafe extern "C" fn execve( prog, argv, envp, + // SAFETY: the interposed execve's own arguments. + || unsafe { execve::original()(prog, argv, envp) }, ) } @@ -108,7 +123,17 @@ unsafe extern "C" fn execl(path: *const c_char, arg: *const c_char, mut valist: // SAFETY: valist holds execl's NULL-terminated argument list after arg, // and environ() returns the valid process environment pointer let (argv, envp) = unsafe { (collect_argv(&arena, arg, &mut valist), environ()) }; - handle_exec(&arena, ExecResolveConfig::search_path_disabled(), path, argv.as_ptr(), envp) + handle_exec( + &arena, + ExecResolveConfig::search_path_disabled(), + path, + argv.as_ptr(), + envp, + // A variadic original cannot be forwarded a collected argv; + // execl is execv over the same argv. + // SAFETY: argv is the NULL-terminated argument list execl was given. + || unsafe { execv::original()(path, argv.as_ptr()) }, + ) } intercept!(execlp(64): unsafe extern "C" fn(path: *const c_char, arg0: *const c_char, ...) -> c_int); @@ -122,7 +147,16 @@ unsafe extern "C" fn execlp(path: *const c_char, arg: *const c_char, mut valist: // SAFETY: valist holds execlp's NULL-terminated argument list after arg, // and environ() returns the valid process environment pointer let (argv, envp) = unsafe { (collect_argv(&arena, arg, &mut valist), environ()) }; - handle_exec(&arena, ExecResolveConfig::search_path_enabled(None), path, argv.as_ptr(), envp) + handle_exec( + &arena, + ExecResolveConfig::search_path_enabled(None), + path, + argv.as_ptr(), + envp, + // execlp is execvp over the same argv, PATH search included. + // SAFETY: argv is the NULL-terminated argument list execlp was given. + || unsafe { execvp::original()(path, argv.as_ptr()) }, + ) } intercept!(execle(64): unsafe extern "C" fn(path: *const c_char, arg0: *const c_char, ...) -> c_int); @@ -139,16 +173,21 @@ unsafe extern "C" fn execle(path: *const c_char, arg: *const c_char, mut valist: let argv = collect_argv(&arena, arg, &mut valist); (argv, valist.next_arg::<*const *const c_char>()) }; - handle_exec(&arena, ExecResolveConfig::search_path_disabled(), path, argv.as_ptr(), envp) + handle_exec( + &arena, + ExecResolveConfig::search_path_disabled(), + path, + argv.as_ptr(), + envp, + // execle is execve over the same argv and envp. + // SAFETY: argv is the NULL-terminated argument list execle was given, + // and envp is the environment it was given after the list. + || unsafe { execve::original()(path, argv.as_ptr(), envp) }, + ) } intercept!(execv(64): unsafe extern "C" fn(path: *const c_char, argv: *const *const c_char) -> c_int); unsafe extern "C" fn execv(path: *const c_char, argv: *const *const c_char) -> c_int { - #[expect( - clippy::no_effect_underscore_binding, - reason = "suppresses unused warning on *::original" - )] - let _unused = execv::original; // SAFETY: path, argv are valid pointers forwarded from the interposed function; environ() returns the process environment unsafe { handle_exec( @@ -157,6 +196,7 @@ unsafe extern "C" fn execv(path: *const c_char, argv: *const *const c_char) -> c path, argv, environ(), + || execv::original()(path, argv), ) } } @@ -166,18 +206,15 @@ intercept!(execvp(64): unsafe extern "C" fn( argv: *const *const libc::c_char, ) -> c_int); unsafe extern "C" fn execvp(prog: *const c_char, argv: *const *const c_char) -> c_int { - #[expect( - clippy::no_effect_underscore_binding, - reason = "suppresses unused warning on *::original" - )] - let _unused = execvp::original; - // SAFETY: environ() returns the valid process environment pointer handle_exec( fspy_nostd_alloc::pooled_bump(), ExecResolveConfig::search_path_enabled(None), prog, argv, + // SAFETY: environ() returns the valid process environment pointer unsafe { environ() }, + // SAFETY: the interposed execvp's own arguments. + || unsafe { execvp::original()(prog, argv) }, ) } @@ -208,17 +245,14 @@ mod linux_only { argv: *const *const libc::c_char, envp: *const *const libc::c_char, ) -> c_int { - #[expect( - clippy::no_effect_underscore_binding, - reason = "suppresses unused warning on *::original" - )] - let _unused = execvpe::original; handle_exec( fspy_nostd_alloc::pooled_bump(), ExecResolveConfig::search_path_enabled(None), file, argv, envp, + // SAFETY: the interposed execvpe's own arguments. + || unsafe { execvpe::original()(file, argv, envp) }, ) } intercept!(execveat(64): unsafe extern "C" fn( @@ -235,11 +269,6 @@ mod linux_only { envp: *const *mut libc::c_char, flags: c_int, // TODO: conform to semantics of flags ) -> libc::c_int { - #[expect( - clippy::no_effect_underscore_binding, - reason = "suppresses unused warning on *::original" - )] - let _unused = execveat::original; // Without `AT_EMPTY_PATH`, an empty pathname fails with `ENOENT` // before the kernel looks anything up, while resolving it against // `dirfd` would run whatever `dirfd` refers to. Let the real call @@ -273,6 +302,8 @@ mod linux_only { abs_path.as_ptr().cast(), argv.cast(), envp.cast(), + // SAFETY: the interposed execveat's own arguments. + || unsafe { execveat::original()(dirfd, pathname, argv, envp, flags) }, ) } @@ -286,11 +317,6 @@ mod linux_only { argv: *const *const libc::c_char, envp: *const *const libc::c_char, ) -> libc::c_int { - #[expect( - clippy::no_effect_underscore_binding, - reason = "suppresses unused warning on *::original" - )] - let _unused = fexecve::original; let mut prog = [0; PROC_FD_PATH_CAPACITY]; let prog = proc_fd_path(fd, &mut prog); handle_exec( @@ -299,6 +325,10 @@ mod linux_only { prog.as_ptr().cast(), argv, envp, + // The descriptor itself, not its /proc path: a sandbox without + // /proc can still fexecve. + // SAFETY: the interposed fexecve's own arguments. + || unsafe { fexecve::original()(fd, argv, envp) }, ) } } diff --git a/crates/fspy_preload_unix/src/interceptions/spawn/posix_spawn.rs b/crates/fspy_preload_unix/src/interceptions/spawn/posix_spawn.rs index 9e01cb1f3..b069df335 100644 --- a/crates/fspy_preload_unix/src/interceptions/spawn/posix_spawn.rs +++ b/crates/fspy_preload_unix/src/interceptions/spawn/posix_spawn.rs @@ -83,7 +83,15 @@ unsafe fn handle_posix_spawn( .join() .unwrap() }) - .unwrap_or_else(|errno| errno as _) + .unwrap_or_else(|_| { + // The seccomp filter cannot be installed, so the spawn never + // happened. Mark the run's trace incomplete so it is not cached, + // then spawn untracked with the caller's own arguments. + client.report_loss(); + // SAFETY: all arguments are the interposed posix_spawn(p) function's + // own valid arguments. + unsafe { original(pid, file, file_actions, attrp, argv, envp) } + }) } intercept!(posix_spawnp(64): PosixSpawnFn); diff --git a/crates/fspy_seccomp_unotify/src/payload/mod.rs b/crates/fspy_seccomp_unotify/src/payload/mod.rs index 6895bc55a..c4bd5342c 100644 --- a/crates/fspy_seccomp_unotify/src/payload/mod.rs +++ b/crates/fspy_seccomp_unotify/src/payload/mod.rs @@ -7,3 +7,17 @@ pub struct SeccompPayload { pub(crate) ipc_path: Vec, pub(crate) filter: Filter, } + +impl SeccompPayload { + /// Builds a payload whose installation is guaranteed to fail: the filter + /// is empty (the kernel refuses it) and nothing listens on `ipc_path`. + /// + /// Test support for the untracked-exec fallback: integration tests in + /// dependent crates use it to exercise a failing [`crate::target`] + /// install without a restricted sandbox. + #[doc(hidden)] + #[must_use] + pub const fn unreachable(ipc_path: Vec) -> Self { + Self { ipc_path, filter: Filter(Vec::new()) } + } +} diff --git a/crates/fspy_shared/src/ipc/channel/mod.rs b/crates/fspy_shared/src/ipc/channel/mod.rs index 0148450d2..af874cd6c 100644 --- a/crates/fspy_shared/src/ipc/channel/mod.rs +++ b/crates/fspy_shared/src/ipc/channel/mod.rs @@ -216,6 +216,18 @@ pub struct Sender { } impl Sender { + /// Reports that this process went on to perform an operation it could + /// not record, sealing the channel as incomplete. + /// + /// Used when a traced process escapes tracing — e.g. a preload that + /// could not install its injection machinery and forwards the operation + /// to the OS untracked. The receiver's close then reports + /// [`RecordsLost`], so the run is treated as untracked rather than + /// cached from a partial trace. + pub fn report_loss(&self) { + self.writer.report_loss(); + } + /// Serializes one record into a committed frame. /// /// A claim the channel refuses is skipped, because that is all a sender diff --git a/crates/fspy_shared/src/ipc/channel/shm_io/README.md b/crates/fspy_shared/src/ipc/channel/shm_io/README.md index 02c0193a0..17542c894 100644 --- a/crates/fspy_shared/src/ipc/channel/shm_io/README.md +++ b/crates/fspy_shared/src/ipc/channel/shm_io/README.md @@ -18,7 +18,7 @@ Any zero-initialized shared memory works. In practice it is a sparse file mapped Two `AtomicU64` counters sit at the front, followed by one 8-byte descriptor slot per frame. -- The **claim counter** counts frames ever claimed. Bit 63 is the CLOSED gate: the receiver sets it when it seals, and so does any writer whose claim failed, which is how the receiver hears about a lost record. +- The **claim counter** counts frames ever claimed. Bit 63 is the CLOSED gate: the receiver sets it when it seals, and so does any writer whose claim failed or that went on to do something it could not record, which is how the receiver hears about a lost record. - The **payload counter** counts payload bytes ever reserved. The mapping's size and the number of slots are the whole geometry, and every process attaching to a region passes the slot count it was created with. Payloads take what the table leaves. Attaching checks one bound: the counters and the table fit, and what remains is small enough for the descriptors' 32-bit offsets. Splitting 4 GiB at one slot per 64 bytes, say, gives ~67 million slots and ~3.5 GiB of payload room, which is tens of millions of records of a few hundred bytes; payload space runs out first. diff --git a/crates/fspy_shared/src/ipc/channel/shm_io/writer.rs b/crates/fspy_shared/src/ipc/channel/shm_io/writer.rs index 9cda0ab33..4b7eb1b9b 100644 --- a/crates/fspy_shared/src/ipc/channel/shm_io/writer.rs +++ b/crates/fspy_shared/src/ipc/channel/shm_io/writer.rs @@ -35,9 +35,10 @@ unsafe impl Sync for ShmWriter {} /// Why a frame could not be claimed. #[derive(thiserror::Error, Clone, Copy, PartialEq, Eq, Debug)] pub enum ClaimError { - /// The CLOSED gate is set, by a seal or by an earlier failed claim. - /// Dropping the record is right either way: the receiver had stopped - /// collecting, or that same bit already fails its seal. + /// The CLOSED gate is set, by a seal, by an earlier failed claim, or by + /// [`ShmWriter::report_loss`]. Dropping the record is right in every + /// case: the receiver had stopped collecting, or that same bit already + /// fails its seal. #[error("the channel has been closed")] Closed, /// No room left, or a frame longer than the `u32::MAX` a descriptor @@ -69,11 +70,28 @@ impl ShmWriter { Some(Self { mapped, _mem: mem }) } - /// Whether the CLOSED gate is set, by a seal or by a failed claim. + /// Whether the CLOSED gate is set, by a seal, by a failed claim, or by + /// [`ShmWriter::report_loss`]. pub fn is_closed(&self) -> bool { self.mapped.claims().load(Ordering::Relaxed) & CLOSED != 0 } + /// Sets the CLOSED gate to report that this writer went on to perform an + /// operation it could not record. + /// + /// The seal then fails and every later claim is refused, so the receiver + /// learns the frames are incomplete rather than mistaking what arrived + /// for the whole trace. + /// + /// Storing the gate rather than or-ing it in drops the claim count, + /// which nothing reads once the gate is set. The seal fails on the + /// bit before it looks at the count, and a claim that reads the + /// cleared count reads the gate along with it, so it gives up + /// before using a slot index. + pub fn report_loss(&self) { + self.mapped.claims().store(CLOSED, Ordering::Relaxed); + } + /// Claims a frame of exactly `frame_size` bytes. Wait-free: two /// `fetch_add`s, no retry loop (rule 1). /// @@ -87,14 +105,8 @@ impl ShmWriter { // The loss report (rule 1): the gate marks the frames incomplete // and shuts the channel down for later claims. - // - // Storing the gate rather than or-ing it in drops the claim count, - // which nothing reads once the gate is set. The seal fails on the - // bit before it looks at the count, and a claim that reads the - // cleared count reads the gate along with it, so it gives up - // before using a slot index. let report_loss = || { - mapped.claims().store(CLOSED, Ordering::Relaxed); + self.report_loss(); ClaimError::Capacity }; diff --git a/crates/vt/src/session/event.rs b/crates/vt/src/session/event.rs index 014209d47..ff1818e2a 100644 --- a/crates/vt/src/session/event.rs +++ b/crates/vt/src/session/event.rs @@ -114,11 +114,12 @@ pub enum CacheNotUpdatedReason { /// A runner-aware tool explicitly requested that this run not be cached /// (e.g. vite dev-server, a watch task). ToolRequested, - /// A tracked process could not record a file access it went on to - /// perform: the channel it reports them through had no room left, or - /// none for a record that long. The accesses that did arrive are a - /// subset of what the task touched, so caching from them would bake in - /// inputs and outputs that are not all of them. + /// A tracked process could not record everything it did. For example, + /// the channel it reports file accesses through had no room left, or + /// the seccomp filter could not be installed and an exec continued + /// untracked. The accesses that did arrive are a subset of what the + /// task touched, so caching from them would bake in inputs and outputs + /// that are not all of them. #[cfg_attr(not(fspy), expect(dead_code, reason = "only fspy tracking can be incomplete"))] TrackingIncomplete, } diff --git a/crates/vt/src/session/execute/cache_update.rs b/crates/vt/src/session/execute/cache_update.rs index aeb1ac3c3..c0a87c77a 100644 --- a/crates/vt/src/session/execute/cache_update.rs +++ b/crates/vt/src/session/execute/cache_update.rs @@ -104,8 +104,8 @@ pub(super) async fn update_cache( } // The accesses, or `None` when the task was not tracked at all. An - // `Err` means it made more file accesses than the tracking channel had - // room for, so what arrived is a subset of what it touched, and an entry + // `Err` means tracking could not record everything it did, so what + // arrived is a subset of what it touched, and an entry // built from a subset would replay with inputs and outputs missing. #[cfg(fspy)] let Ok(path_accesses) = outcome.path_accesses.as_ref().map(Result::as_ref).transpose() else { diff --git a/crates/vt/src/session/reporter/summary.rs b/crates/vt/src/session/reporter/summary.rs index fc1db4cc1..dac339713 100644 --- a/crates/vt/src/session/reporter/summary.rs +++ b/crates/vt/src/session/reporter/summary.rs @@ -121,8 +121,8 @@ pub enum SpawnOutcome { fspy_unsupported: bool, /// The IPC server error that caused the cache to be skipped, if any. ipc_server_error: Option, - /// `true` when the task made more file accesses than tracking had - /// room for, so the inferred inputs and outputs were a subset of + /// `true` when automatic file tracking could not record everything + /// the task did, so the inferred inputs and outputs were a subset of /// what it touched. Task ran successfully but cache was not /// updated. #[serde(default)] @@ -544,7 +544,7 @@ impl TaskResult { { return ( Str::from( - "→ Not cached: this task used more files than automatic tracking can record. Configure `input` and `output` manually to enable caching.", + "→ Not cached: automatic file tracking couldn't record everything this task did. Configure `input` and `output` manually to enable caching.", ), Vec::new(), &[], diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/fspy_shm_capacity/snapshots/shm_capacity_env_sizes_the_tracking_channel.md b/crates/vt_bin/tests/e2e_snapshots/fixtures/fspy_shm_capacity/snapshots/shm_capacity_env_sizes_the_tracking_channel.md index ada21e605..eeedd0a19 100644 --- a/crates/vt_bin/tests/e2e_snapshots/fixtures/fspy_shm_capacity/snapshots/shm_capacity_env_sizes_the_tracking_channel.md +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/fspy_shm_capacity/snapshots/shm_capacity_env_sizes_the_tracking_channel.md @@ -23,7 +23,7 @@ Performance: 0% cache hit rate Task Details: ──────────────────────────────────────────────── [1] fspy-shm-capacity#stat: $ vtt stat-many 20000 ✓ - → Not cached: this task used more files than automatic tracking can record. Configure `input` and `output` manually to enable caching. + → Not cached: automatic file tracking couldn't record everything this task did. Configure `input` and `output` manually to enable caching. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` @@ -46,6 +46,6 @@ Performance: 0% cache hit rate Task Details: ──────────────────────────────────────────────── [1] fspy-shm-capacity#stat: $ vtt stat-many 20000 ✓ - → Not cached: this task used more files than automatic tracking can record. Configure `input` and `output` manually to enable caching. + → Not cached: automatic file tracking couldn't record everything this task did. Configure `input` and `output` manually to enable caching. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ```