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.
This commit is contained in:
1 parent
522e3d2627
commit
1d09ff5d10
4 files changed
+114
-3
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<PathBuf>) {
|
||||
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<String> = (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")
|
||||
);
|
||||
}
|
||||
+2
-1
@@ -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=()
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user