# shellcheck shell=bash # Gate for the kernel modules a build collects (WARDEN_MODULES_COLLECT in # build-kernel.sh). Sourced, not run: reading it changes nothing. # # Every collected module must carry this kernel's vermagic, so a module built # against another tree, or a hand-copied .ko, cannot reach an image. A # directory that needs a test-only Kconfig symbol must find it =m after # olddefconfig. tests/test-hang/run-module-gate-tests.sh runs these functions # against fixtures, including the refusal paths. # The test-only Kconfig symbol a collected directory needs (empty for the rest). # Release and debug builds list no test directory, so they get no symbol. test_symbol_of() { case "$1" in drivers/warden-test) echo WARDEN_TEST_HANG ;; *) : ;; esac } # module_vermagic_check : 0 when the module's # vermagic is " " (the release, then a space), else 1 with a FATAL line. module_vermagic_check() { local krel=$1 ko=$2 vm if [ -z "$krel" ]; then echo "FATAL: kernel release is empty" >&2 return 1 fi # vermagic is a NUL-separated .modinfo string; one printable run per line. vm="$(tr -c '[:print:]' '\n' < "$ko" | grep -m1 '^vermagic=' || true)" case "$vm" in "vermagic=$krel "*) return 0 ;; '') echo "FATAL: $(basename "$ko") has no vermagic" >&2 ;; *) echo "FATAL: $(basename "$ko") vermagic '${vm#vermagic=}' is not this kernel's '$krel'" >&2 ;; esac return 1 } # collect_modules : copies every # .ko under / into , each only after it passed # module_vermagic_check. Sets COLLECTED to the count. Returns 1 at the first # module that fails, when the directory is missing, or when it yields none. collect_modules() { local src=$1 d=$2 krel=$3 out=$4 ko COLLECTED=0 if [ ! -d "$src/$d" ]; then echo "FATAL: WARDEN_MODULES_COLLECT dir '$d' not in tree" >&2 return 1 fi while IFS= read -r ko; do module_vermagic_check "$krel" "$ko" || return 1 cp "$ko" "$out/" || return 1 COLLECTED=$((COLLECTED + 1)) done < <(find "$src/$d" -name '*.ko') if [ "$COLLECTED" -eq 0 ]; then echo "FATAL: no .ko produced under '$d'" >&2 return 1 fi } # require_test_config : a directory with a test symbol # must find it =m in the configuration after olddefconfig (an unmet dependency # drops the symbol without a word). Returns 1 with a FATAL line otherwise. require_test_config() { local cfg=$1 d=$2 sym sym="$(test_symbol_of "$d")" [ -n "$sym" ] || return 0 if ! grep -qx "CONFIG_${sym}=m" "$cfg" 2>/dev/null; then echo "FATAL: $d needs CONFIG_${sym}=m, which did not survive olddefconfig" >&2 return 1 fi }