From 1d09ff5d109a6d824f16eeabdc0dd75ad6d7ea64 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 19 Nov 2026 11:19:24 -0800 Subject: [PATCH] scripts: the gate scripts match their lists in the shell, not through a piped grep -q under pipefail scripts/linux-gate-policy's overlaps() piped printf into `grep -qxF` under `set -euo pipefail`. bash line-buffers its stdout, so grep can exit at its first match while printf is still writing; printf then ends with SIGPIPE, pipefail gives the pipeline its status, and a match reads false. The overlap went unseen and the policy accepted a scratch directory inside the checkout: linux_gate_policy::a_scratch_directory_may_not_overlap_a_protected_path failed so on a loaded msi (53490 at b14260ea79) and passed alone (53581). overlaps() now matches the lists with a `[[ ... == *...* ]]` test. scripts/linux-gate's bsdtar check had the same shape (`tar --version | grep -q bsdtar`) and is matched the same way. A new integration test, no_quiet_grep_under_pipefail, reads every script under scripts/ that sets pipefail and fails on a quiet grep after a single pipe, with a unit test of its detector. Verified: see the check job named in the landing. --- .../tests/integration/main.rs | 1 + .../no_quiet_grep_under_pipefail.rs | 107 ++++++++++++++++++ scripts/linux-gate | 3 +- scripts/linux-gate-policy | 6 +- 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 crates/metal2vulkan-validation/tests/integration/no_quiet_grep_under_pipefail.rs diff --git a/crates/metal2vulkan-validation/tests/integration/main.rs b/crates/metal2vulkan-validation/tests/integration/main.rs index 6ddf38d82..a737c34f4 100644 --- a/crates/metal2vulkan-validation/tests/integration/main.rs +++ b/crates/metal2vulkan-validation/tests/integration/main.rs @@ -35,6 +35,7 @@ mod half_division_cases; mod linux_gate_policy; mod metal_coverage; mod no_conflict_markers; +mod no_quiet_grep_under_pipefail; mod pipeline_selection; mod private_corpus_root; mod public_entry_rows; diff --git a/crates/metal2vulkan-validation/tests/integration/no_quiet_grep_under_pipefail.rs b/crates/metal2vulkan-validation/tests/integration/no_quiet_grep_under_pipefail.rs new file mode 100644 index 000000000..f5939d297 --- /dev/null +++ b/crates/metal2vulkan-validation/tests/integration/no_quiet_grep_under_pipefail.rs @@ -0,0 +1,107 @@ +//! No script that sets `pipefail` pipes into a quiet `grep`. `grep -q` exits at its first match, +//! which can end the command writing into the pipe with SIGPIPE before it has written everything; +//! under `pipefail` the pipeline's status is then the writer's, and it reads false on a match. +//! Whether that happens depends on scheduling, so a check written that way passes alone and fails +//! on a loaded host: `scripts/linux-gate-policy` once accepted a scratch directory inside the +//! checkout that way. Such a test is matched in the shell instead (`[[ ... == *...* ]]`). + +use std::fs; +use std::path::{Path, PathBuf}; + +/// Every file under `dir`, depth first, skipping build and cache directories. +fn walk(dir: &Path, found: &mut Vec) { + let Ok(entries) = fs::read_dir(dir) else { + return; + }; + for entry in entries.filter_map(Result::ok) { + let path = entry.path(); + if entry.file_type().is_ok_and(|kind| kind.is_dir()) { + if !matches!(entry.file_name().to_str(), Some("target" | "__pycache__")) { + walk(&path, found); + } + } else { + found.push(path); + } + } +} + +/// Whether the command `segment` (the text after a pipe) is a `grep` with a quiet option. +fn quiet_grep(segment: &str) -> bool { + let mut words = segment.split_whitespace(); + if !matches!(words.next(), Some("grep" | "egrep" | "fgrep")) { + return false; + } + words.take_while(|word| word.starts_with('-')).any(|word| { + matches!(word, "--quiet" | "--silent") + || (!word.starts_with("--") && word[1..].contains('q')) + }) +} + +/// Whether `line` pipes into a quiet `grep`: a single `|` (not `||`) followed by one. +fn pipes_into_quiet_grep(line: &str) -> bool { + if line.trim_start().starts_with('#') { + return false; + } + let bytes = line.as_bytes(); + (0..bytes.len()).any(|at| { + bytes[at] == b'|' + && (at == 0 || bytes[at - 1] != b'|') + && bytes.get(at + 1) != Some(&b'|') + && quiet_grep( + line[at + 1..] + .split([';', '&', ')', '|']) + .next() + .unwrap_or(""), + ) + }) +} + +#[test] +fn the_detector_finds_a_piped_quiet_grep_and_nothing_else() { + for hit in [ + "printf '%s\\n' \"$up\" | grep -qxF \"$id\"", + "if tar --version 2>/dev/null | grep -q bsdtar; then", + "x | grep -E -q y", + "x |grep --quiet y", + ] { + assert!(pipes_into_quiet_grep(hit), "{hit}"); + } + for miss in [ + "x || grep -q y", + "grep -q y file", + "x | grep -c y", + "# x | grep -q y", + "[[ $up == *\"$id\"* ]]", + ] { + assert!(!pipes_into_quiet_grep(miss), "{miss}"); + } +} + +#[test] +fn no_script_that_sets_pipefail_pipes_into_a_quiet_grep() { + let root = + fs::canonicalize(metal2vulkan_devtools::workspace_dir()).expect("the repository's root"); + let mut files = Vec::new(); + walk(&root.join("scripts"), &mut files); + let scripts: Vec<(PathBuf, String)> = (files.into_iter()) + .filter_map(|path| fs::read_to_string(&path).ok().map(|text| (path, text))) + .filter(|(_, text)| text.contains("pipefail")) + .collect(); + assert!( + scripts.len() >= 10, + "only {} scripts set pipefail, so the walk is looking in the wrong place", + scripts.len() + ); + let hits: Vec = (scripts.iter()) + .flat_map(|(path, text)| { + (text.lines().enumerate()) + .filter(|(_, line)| pipes_into_quiet_grep(line)) + .map(move |(at, line)| format!("{}:{}: {}", path.display(), at + 1, line.trim())) + }) + .collect(); + assert!( + hits.is_empty(), + "a quiet grep after a pipe under pipefail can read false on a match:\n{}", + hits.join("\n") + ); +} diff --git a/scripts/linux-gate b/scripts/linux-gate index e1fa70ef3..884b5638f 100755 --- a/scripts/linux-gate +++ b/scripts/linux-gate @@ -305,7 +305,8 @@ else fi # The listed files as a pax archive (symlinks as links, mtimes to the nanosecond, as `cp -a` kept # them; no macOS metadata). -if tar --version 2>/dev/null | grep -q bsdtar; then +# Matched in the shell: under pipefail, `| grep -q` can read false on a match (SIGPIPE). +if [[ $(tar --version 2>/dev/null) == *bsdtar* ]]; then tar_flags=(--no-mac-metadata --no-xattrs --no-acls --no-fflags) else tar_flags=() diff --git a/scripts/linux-gate-policy b/scripts/linux-gate-policy index 59fef7d20..63f11c6e4 100755 --- a/scripts/linux-gate-policy +++ b/scripts/linux-gate-policy @@ -89,14 +89,16 @@ ids_up() { } # True when the directories $1 and $2 are the same directory or one lies inside the other, by -# file identity in both directions; also true when either cannot be read. +# file identity in both directions; also true when either cannot be read. The lists are matched +# in the shell, not piped to `grep -q`: under pipefail, grep's exit at the first match can end +# the writer with SIGPIPE, and the pipeline then reads false on a match. overlaps() { local id_a id_b up_a up_b if ! id_a=$(file_id "$1") || ! id_b=$(file_id "$2") || ! up_a=$(ids_up "$1") || ! up_b=$(ids_up "$2"); then return 0 fi - printf '%s\n' "$up_a" | grep -qxF "$id_b" || printf '%s\n' "$up_b" | grep -qxF "$id_a" + [[ $'\n'"$up_a"$'\n' == *$'\n'"$id_b"$'\n'* || $'\n'"$up_b"$'\n' == *$'\n'"$id_a"$'\n'* ]] } # The protected path the directory $1 overlaps, if any.