From 8455c9aba3c924b5b1d9cceb5b06b5047ffb15c7 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sun, 1 Nov 2026 22:14:14 +0000 Subject: [PATCH] tools/kernel-pr-check: refuse a --build that holds the tree where it deletes The builds delete and rewrite OUT/src, OUT/src-base, OUT/build and OUT/build-base, but --build was only refused inside the kernel tree. An OUT one level above a tree named src (-C /x/src --build /x) passed, and the builds' rm -rf of OUT/src deleted the checkout the tool promises never to touch. An OUT/src that holds a worktree's git directory went the same way, and a symlink at OUT/build into the tree had make build in the tree and the device-tree step delete its arch/arm64/boot/dts. Resolve every directory the builds delete or rewrite, symlinks included, and refuse an OUT where one of them is the tree or its git directory, holds it or lies inside it. The tree's own path is now resolved physically too, so a symlinked -C is compared by what it points at. An OUT beside the tree, or holding a tree under another name, still builds. The guard and the deletes share one list of what is deleted, so a new delete can't slip past it. The header says where the builds go, and --help now prints all of it (it stopped a line short before). tests/kernel-pr-check covers these layouts, and a symlink deeper in OUT/build, with an rm that records what it is asked to delete under the fixtures and deletes none of it. Co-Authored-By: Claude Opus 5.5 --- tests/kernel-pr-check | 33 +++++++++++++++++++++++++++++++++ tools/kernel-pr-check | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/tests/kernel-pr-check b/tests/kernel-pr-check index 4b7497dd7..659276d2d 100755 --- a/tests/kernel-pr-check +++ b/tests/kernel-pr-check @@ -93,6 +93,39 @@ rel_build_out=$(cd "$k" && "$here/tools/kernel-pr-check" -C . --build out omamob [[ $rel_build_status == 2 ]] || { echo "$rel_build_out"; fail "a relative --build inside the tree: exit $rel_build_status, expected 2"; } echo "ok - --build is refused inside the kernel tree, relative or not" +# Nor where the builds would delete the tree or its git directory. clang and +# ld.lld stubs let the tool reach the builds, and its rm records what under +# $c (resolved, as the rm compares resolved paths) it is asked to delete and +# deletes none of it: a regression reports the deletion instead of making it. +c=$(cd -- "$t" && pwd -P)/collide real_rm=$(command -v rm) +mkdir -p "$c/bin" "$c/link-out" "$c/deep-out/build" +printf '#!/bin/sh\nexit 0\n' > "$c/bin/clang" +cp "$c/bin/clang" "$c/bin/ld.lld" +cat > "$c/bin/rm" <<'EOF' +#!/bin/bash +for a; do [[ $a == -* ]] || [[ $(realpath -m -- "$a")/ != "$COLLIDE"/* ]] || { echo "$a" >> "$COLLIDE/rm.log"; exit 1; }; done +exec "$REAL_RM" "$@" +EOF +chmod +x "$c/bin/clang" "$c/bin/ld.lld" "$c/bin/rm" +git clone -q "$k" "$c/src" +git clone -q "$k" "$c/main/src" +git -C "$c/main/src" worktree add -q "$c/main/wt" HEAD +ln -s "$c/src" "$c/link-out/build" +ln -s "$c/src/arch" "$c/deep-out/build/arch" +refused() { # name tree out + local out status=0 + out=$(PATH=$c/bin:$PATH COLLIDE=$c REAL_RM=$real_rm "$here/tools/kernel-pr-check" -C "$2" --build "$3" origin/omamobile HEAD 2>&1) || status=$? + [[ ! -e $c/rm.log ]] || { echo "$out"; fail "$1: deleted $(paste -sd' ' "$c/rm.log")"; } + [[ $status == 2 ]] || { echo "$out"; fail "$1: exit $status, expected 2"; } + grep -c 'overlaps the kernel tree' <<<"$out" >/dev/null || { echo "$out"; fail "$1: silent"; } +} +refused "OUT holds a tree named src" "$c/src" "$c" +refused "OUT/build links to the tree" "$c/src" "$c/link-out" +refused "OUT/build/arch links into the tree" "$c/src" "$c/deep-out" +refused "OUT/src holds a worktree's git directory" "$c/main/wt" "$c/main" +[[ -x $c/src/scripts/checkpatch.pl && -d $c/main/src/.git ]] || fail "a collision fixture lost its tree" +echo "ok - --build is refused where its src, src-base, build or build-base would delete the tree or its git directory" + # --merge: the base moves on after the branch is cut. A clean merge passes # and says so; a conflict fails as "rebase needed" while the commit checks # still run. diff --git a/tools/kernel-pr-check b/tools/kernel-pr-check index efca37ea9..720b4103a 100755 --- a/tools/kernel-pr-check +++ b/tools/kernel-pr-check @@ -25,9 +25,10 @@ # tools/kernel-pr-check [-C KERNEL] [--build OUT] [--merge] BASE [HEAD] # # KERNEL defaults to $KERNEL_BASE/linux (~/omamobile-kernel/linux, or -# ~/android/mainline/linux on older setups), HEAD to HEAD. It reads git and -# builds in OUT (and OUT-base, for the base's device trees); it never -# touches the tree's checkout or anything else. +# ~/android/mainline/linux on older setups), HEAD to HEAD. It reads git, +# exports what it builds to OUT/src and OUT/src-base and builds in OUT/build +# and OUT/build-base; it never touches the tree's checkout or anything else, +# and refuses an OUT that would. # # --merge builds what would land: HEAD merged onto BASE (a test merge, # written as git objects only), compared with BASE itself. The commit checks @@ -53,7 +54,7 @@ while (($#)); do -C) kernel=${2:?-C needs a kernel tree}; shift 2 ;; --build) out=${2:?--build needs an output directory}; shift 2 ;; --merge) merge=1; shift ;; - -h|--help) sed -n '3,35p' "$self"; exit 0 ;; + -h|--help) sed -n '3,37p' "$self"; exit 0 ;; -*) echo "kernel-pr-check: unknown option $1" >&2; exit 2 ;; *) break ;; esac @@ -67,14 +68,30 @@ if [[ ! -d $kernel ]]; then echo "kernel-pr-check: no such tree $kernel" >&2 exit 2 fi -kernel=$(CDPATH='' cd -- "$kernel" && pwd) -# --build too: the builds run after the cd, and they delete and re-extract -# directories under OUT, so OUT must never resolve inside the kernel tree. +# Symlinks resolved too, so the --build check below sees the real tree. +kernel=$(CDPATH='' cd -- "$kernel" && pwd -P) +# --build too: the builds run after the cd. They replace OUT/src and +# OUT/src-base, build in OUT/build and OUT/build-base and delete $stale there. +# None of these, symlinks resolved, may be the tree or its git directory (a +# worktree's lies outside it), hold it or lie inside it: an OUT that was the +# parent of a tree named src had the tree deleted. if [[ -n $out ]]; then out=$(realpath -m -- "$out") - case $out/ in - "$kernel"/*) echo "kernel-pr-check: --build $out is inside the kernel tree; use a directory outside it" >&2; exit 2 ;; - esac + src=$out/src srcb=$out/src-base + stale=("$out"/build{,-base}/{arch/arm64/boot/dts,Documentation/devicetree/bindings/processed-schema.json}) + inside() { [[ ${1%/}/ == "${2%/}"/* ]]; } # $1 is $2 or lies inside it + inside "$out" "$kernel" && { echo "kernel-pr-check: --build $out is inside the kernel tree; use a directory outside it" >&2; exit 2; } + gitdir=$(git -C "$kernel" rev-parse --path-format=absolute --git-common-dir 2>/dev/null) && gitdir=$(realpath -m -- "$gitdir") + for d in "$src" "$srcb" "$out"/build{,-base} "${stale[@]}"; do + r=$(realpath -m -- "$d") || { echo "kernel-pr-check: can't resolve $d" >&2; exit 2; } + for p in "$kernel" ${gitdir:+"$gitdir"}; do + inside "$r" "$p" || inside "$p" "$r" || continue + [[ $p == "$kernel" ]] && where="the kernel tree $p" || where="the kernel tree's git directory $p" + [[ $r == "$d" ]] || d+=" (that is, $r)" + echo "kernel-pr-check: --build $out overlaps $where: the builds replace $d; use another directory" >&2 + exit 2 + done + done mkdir -p -- "$out" || { echo "kernel-pr-check: can't create $out" >&2; exit 2; } fi cd -- "$kernel" || exit 2 @@ -201,7 +218,6 @@ if [[ -n $out ]]; then skip "builds: no clang or ld.lld" else # A clean export of what's built and its base, so the tree's own checkout is never touched. - src=$out/src srcb=$out/src-base rm -rf "$src" "$srcb"; mkdir -p "$src" "$srcb" git archive "$build_head" | tar -x -C "$src" k=(make -s -C "$src" O="$out/build" ARCH=arm64 LLVM=1 -j"$(nproc)") @@ -230,8 +246,7 @@ ${bad%$'\n'}"; fi # outputs so make rebuilds, and prints, every warning. The processed # schema goes too, or a reused OUT validates against an earlier # tree's bindings (kernel #32's own binding went unseen that way). - rm -rf "$out/build/arch/arm64/boot/dts" "$out/build-base/arch/arm64/boot/dts" \ - "$out"/build{,-base}/Documentation/devicetree/bindings/processed-schema.json + rm -rf "${stale[@]}" log=$("${k[@]}" W=1 $check $targets 2>&1); st=$? if ((st)); then fail "device trees don't build: