strawberry/scripts/fold_changed.sh
trvon 6628ea9aea
Harden against crashes and I/O faults
Fix for the 2026-10-25 host freeze (an uncaught iostream exception on a
/proc read aborted the daemon; its core dump deadlocked with the exec
guard): non-throwing /proc reads, crash handlers that release fanotify
permission fds, guarded thread entries, from_chars parsing.

Adds the io fault-injection seam with property tests, source invariant
checks (R1-R6) with self-test fixtures, libFuzzer targets and seeds
(including I/O-fault fuzzing), the fold changed-files scan scripts and
the agent workflow docs (CLAUDE.md, prompts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:17 -06:00

56 lines
2.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# Run fold (bounded symbolic SAST, ../fold) on the C++ translation units changed
# relative to a git base (default: HEAD, i.e. uncommitted changes; pass a ref,
# e.g. `origin/main`, or `--all` for the whole tree).
#
# Fold needs LLVM 18-19, so it runs in fold's validation image, capped so it can
# never take the workstation down (--cpus=2 --memory=6g, nice 19, 2 processes).
# The fold build and conan cache persist in build/audit/cache across runs.
#
# scripts/fold_changed.sh # uncommitted changes
# scripts/fold_changed.sh main # changes since main
# scripts/fold_changed.sh --all # every TU (slow)
# FOLD_DETECTORS=null-deref scripts/fold_changed.sh
# FOLD_FILES="src/store/codec.cpp" scripts/fold_changed.sh
# Results: build/audit/out/fold.json (triage per docs/prompts/PROMPT-strawberry-audit.md).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
FOLD_REPO="${FOLD_REPO:-$ROOT/../fold}"
A="$ROOT/build/audit"
mkdir -p "$A/cache" "$A/out"
if [ -n "${FOLD_FILES:-}" ]; then
FILES="$FOLD_FILES" # explicit list, e.g. FOLD_FILES="src/store/codec.cpp"
elif [ "${1:-}" = "--all" ]; then
FILES=""
else
BASE="${1:-HEAD}"
FILES=$(cd "$ROOT" && { git diff --name-only "$BASE" -- 'src/*.cpp' 'mcp/*.cpp' 'app/*.cpp'; \
git ls-files --others --exclude-standard -- 'src/*.cpp' 'mcp/*.cpp'; } | sort -u | tr '\n' ' ')
if [ -z "$FILES" ]; then
echo "fold: no changed C++ translation units vs $BASE"
exit 0
fi
fi
[ -d "$FOLD_REPO" ] || { echo "fold repo not found at $FOLD_REPO (set FOLD_REPO)"; exit 2; }
docker image inspect fold-linux-validate >/dev/null 2>&1 || \
nice -n 19 docker build --cpu-quota=200000 --cpu-period=100000 --memory=4g \
-t fold-linux-validate -f "$FOLD_REPO/docker/Dockerfile.linux-validate" "$FOLD_REPO"
echo "fold: scanning ${FILES:-all translation units}"
nice -n 19 docker run --rm -u 0 --cpus=2 --memory=6g --memory-swap=6g --cpu-shares=256 --entrypoint bash \
-e FOLD_FILES="$FILES" -e FOLD_DETECTORS="${FOLD_DETECTORS:-}" \
-v "$FOLD_REPO":/fold:ro -v "$ROOT":/strawberry:ro -v "$A/cache":/cache -v "$A/out":/out \
-v "$ROOT/scripts/fold_scan_in_docker.sh":/run.sh:ro fold-linux-validate /run.sh
python3 -I - "$A/out/fold.json" <<'PY'
import json, sys, collections
try:
results = json.load(open(sys.argv[1]))["runs"][0]["results"]
except Exception as e:
print("fold: no results:", e); sys.exit(1)
c = collections.Counter((r["properties"]["detector"], r["properties"]["confidence"]) for r in results)
print(f"fold: {len(results)} finding(s)")
for (det, conf), n in c.most_common():
print(f" {n:4} {det} ({conf})")
print("verify each in source before fixing or reporting; store triage in YAMS (source=evidence)")
PY