1# SPDX-License-Identifier: GPL-2.0 2# 3# Helpers for the objtool klp tests. A test builds a fixture twice, as the 4# original and (with -DPATCHED) the patched object, runs both through 5# "klp checksum" and diffs them, then asserts on the result. 6# 7# Assertions check properties rather than compare against recorded output: 8# codegen varies between compilers and golden files would report churn instead 9# of regressions. 10 11TESTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 12 13# Tests live in generic/ or in an architecture directory beside it, and each 14# carries its own fixtures. 15FIXTURES_DIR="$(cd "$(dirname "$0")/fixtures" 2>/dev/null && pwd)" 16 17# The kernel's convention: CROSS_COMPILE is the one knob, with per-tool 18# overrides for what it does not cover. objtool itself is always a host binary 19# -- it is built with HOSTCC and only reads ELF -- so an arm64 machine can run 20# the x86 tests against x86 objects given a compiler that emits them. 21# 22# readelf reads any target, so it rarely needs overriding, and either GNU 23# readelf or llvm-readelf will do: the assertions match on fields rather than 24# on columns, and where the two spell something differently -- "OS [0xff20]" 25# against "OS[0xff20]" for SHN_LIVEPATCH -- they accept both. BFD's objcopy is 26# usually built for the host's target alone, and llvm-objcopy is the 27# target-agnostic replacement. 28CROSS_COMPILE="${CROSS_COMPILE:-}" 29CC="${CC:-${CROSS_COMPILE}gcc}" 30LD="${LD:-${CROSS_COMPILE}ld}" 31READELF="${READELF:-${CROSS_COMPILE}readelf}" 32OBJCOPY="${OBJCOPY:-${CROSS_COMPILE}objcopy}" 33 34OBJTOOL="${OBJTOOL:-$TESTS_DIR/../objtool}" 35 36# klp_preflight 37# 38# Check the environment once, before any test runs, and report what was found. 39# 40klp_preflight() 41{ 42 local tmp tool cc_version arch host cc_arch 43 44 bail() { echo "Bail out! $*" >&2; exit 1; } 45 46 # A relative $OBJTOOL is relative to the objtool directory, not tests/. 47 [ -x "$OBJTOOL" ] || [ ! -x "$TESTS_DIR/../$OBJTOOL" ] || 48 OBJTOOL="$TESTS_DIR/../$OBJTOOL" 49 50 [ -x "$OBJTOOL" ] || 51 bail "objtool not found at '$OBJTOOL' -- build it first" 52 53 # run_diff() runs objtool from inside the test's working directory, so 54 # a relative path would resolve against that instead. 55 OBJTOOL="$(realpath "$OBJTOOL")" 56 57 "$OBJTOOL" klp 2>&1 | grep -q checksum || 58 bail "objtool was built without klp support; install libxxhash (>= 0.8) and rebuild" 59 60 command -v "${CC%% *}" >/dev/null || bail "compiler not found: $CC" 61 62 for tool in "$READELF" "$OBJCOPY" "$LD"; do 63 command -v "${tool%% *}" >/dev/null || bail "$tool not found" 64 done 65 66 tmp="$(mktemp -d)" || bail "mktemp failed" 67 echo 'int probe(void) { return 0; }' > "$tmp/probe.c" 68 $CC -c -o "$tmp/probe.o" "$tmp/probe.c" 2>/dev/null || 69 { rm -rf "$tmp"; bail "$CC cannot compile a trivial object"; } 70 71 # $CC, $ARCH and objtool have to agree about the target, and cross runs 72 # are where they stop agreeing: plain "CC=clang ARCH=x86_64" on an arm64 73 # box selects the x86 tests and then builds arm64 objects, because clang 74 # needs --target= to emit anything but the host's. 75 # 76 # Ask objtool rather than comparing machine names. It rejects an object 77 # it was not built for -- "unexpected ELF machine type" -- so one check 78 # covers every way the three can disagree, and says so once instead of 79 # failing every test for the same reason. 80 "$OBJTOOL" klp checksum "$tmp/probe.o" >/dev/null 2>&1 || 81 { rm -rf "$tmp" 82 bail "objtool rejects an object built by '$CC'; they target" \ 83 "different architectures (set CROSS_COMPILE, or" \ 84 "--target= for clang)"; } 85 86 # BFD objcopy is usually built for the host's target alone, and 87 # checksum_of() needs it to read the object under test. 88 $OBJCOPY -O binary --only-section=.text "$tmp/probe.o" "$tmp/probe.bin" 2>/dev/null || 89 { rm -rf "$tmp" 90 bail "$OBJCOPY cannot read objects built by '$CC'; install" \ 91 "binutils-multiarch or set OBJCOPY=llvm-objcopy"; } 92 # $ARCH only chooses which directory of tests runs, so it can disagree 93 # with what $CC builds without objtool noticing -- and the result is the 94 # wrong set of tests, quietly. 95 case "$($READELF -hW "$tmp/probe.o" | sed -n 's/.*Machine: *//p')" in 96 *X86-64*|*Intel*80386*) cc_arch=x86 ;; 97 *AArch64*) cc_arch=arm64 ;; 98 *) cc_arch= ;; 99 esac 100 rm -rf "$tmp" 101 102 # Normalize to the kernel's SRCARCH. 103 case "${ARCH:-$(uname -m)}" in 104 x86_64|i?86) arch=x86 ;; 105 aarch64*) arch=arm64 ;; 106 *) arch="${ARCH:-$(uname -m)}" ;; 107 esac 108 109 case "$(uname -m)" in 110 x86_64|i?86) host=x86 ;; 111 aarch64*) host=arm64 ;; 112 *) host="$(uname -m)" ;; 113 esac 114 115 [ -z "$cc_arch" ] || [ "$cc_arch" = "$arch" ] || 116 bail "ARCH says $arch but '$CC' builds $cc_arch objects;" \ 117 "the $arch tests would run against the wrong architecture" 118 119 KLP_TEST_ARCH="$arch" 120 KLP_TEST_PREFLIGHT=done 121 export OBJTOOL CC KLP_TEST_ARCH KLP_TEST_PREFLIGHT 122 123 cc_version="$($CC --version 2>/dev/null | head -1)" 124 cat <<EOF 125# preflight 126# objtool $OBJTOOL (klp: yes) 127# compiler $cc_version 128# arch $KLP_TEST_ARCH$([ "$arch" = "$host" ] || echo " (host $host, cross)") 129EOF 130} 131 132[ -n "${KLP_TEST_PREFLIGHT:-}" ] || klp_preflight 133 134# klp-build compiles the kernel this way; klp diff needs per-symbol sections to 135# extract individual functions. 136FIXTURE_CFLAGS="-c -O2 -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables" 137 138test_name="$(basename "$0" .sh)" 139workdir= 140 141pass() { echo "ok - $test_name${1:+: $1}"; exit 0; } 142fail() { echo "not ok - $test_name: $1"; exit 1; } 143 144# Two kinds of skip, and the runner tells them apart. 145# 146# declared_skip the test said in advance it does not apply here, e.g. 147# gcc_only on a clang run. Expected indefinitely. 148# probe_skip the construct did not turn up in the built object this 149# time. Weaker: it may appear on another compiler version, 150# and one which becomes permanent is a fixture that quietly 151# stopped testing anything. 152# 153# A bare skip() is neither, and the runner counts it as a failure: a test which 154# gives up for a reason it never declared is a hole, not an outcome. 155declared_skip() { echo "ok - $test_name # SKIP (declared) $*"; exit 0; } 156probe_skip() { echo "ok - $test_name # SKIP (probe) $*"; exit 0; } 157skip() { echo "ok - $test_name # SKIP $*"; exit 0; } 158 159# TAP directives. A test which is known to fail reports it rather than being 160# commented out and forgotten, and one which starts passing again says so 161# instead of quietly going green: the expectation has to be removed by hand, 162# which is the point. 163xfail() 164{ 165 echo "not ok - $test_name${1:+: $1} # TODO known failure" 166 exit 0 167} 168 169xpass() 170{ 171 echo "ok - $test_name${1:+: $1} # TODO expected failure, but passed" 172 exit 1 173} 174 175cleanup() { [ -n "$workdir" ] && rm -rf "$workdir"; } 176 177# setup [exported symbol...] 178setup() 179{ 180 # The environment was checked once when this file was sourced, so there 181 # is nothing to verify here: objtool exists at the resolved path, has 182 # klp support, and $CC works. 183 workdir="$(mktemp -d)" || fail "mktemp failed" 184 trap cleanup EXIT 185 186 export_syms "$@" 187} 188 189# export_syms [symbol...] 190# 191# Rewrite Module.symvers so exactly these symbols are exported by vmlinux. 192# Whether a symbol is listed decides between an ordinary relocation and a klp 193# relocation, so tests flip it to cover both. 194export_syms() 195{ 196 : > "$workdir/Module.symvers" 197 for sym in "$@"; do 198 printf '0x00000000\t%s\tvmlinux\tEXPORT_SYMBOL\t\n' \ 199 "$sym" >> "$workdir/Module.symvers" 200 done 201} 202 203# gcc_only / clang_only <reason> 204gcc_only() 205{ 206 case "$($CC --version 2>/dev/null | head -1)" in 207 *[Gg][Cc][Cc]*) return 0 ;; 208 esac 209 declared_skip "gcc only${1:+: $1}" 210} 211 212clang_only() 213{ 214 case "$($CC --version 2>/dev/null | head -1)" in 215 *clang*) return 0 ;; 216 esac 217 declared_skip "clang only${1:+: $1}" 218} 219 220# build_pair <fixture.c> [cflags...] 221build_pair() 222{ 223 local fixture="$FIXTURES_DIR/$1"; shift 224 225 [ -f "$fixture" ] || fail "missing fixture $fixture" 226 227 $CC $FIXTURE_CFLAGS "$@" -o "$workdir/orig.o" "$fixture" 2>"$workdir/cc.log" || 228 probe_skip "fixture does not build here: $(tail -1 "$workdir/cc.log")" 229 $CC $FIXTURE_CFLAGS "$@" -DPATCHED -o "$workdir/patched.o" "$fixture" 2>"$workdir/cc.log" || 230 probe_skip "fixture does not build here: $(tail -1 "$workdir/cc.log")" 231} 232 233# run_diff [expected exit status] 234run_diff() 235{ 236 local expect="${1:-0}" rc=0 237 238 # Checksums live in the objects, so only generate them once even when a 239 # test diffs the same pair again with a different Module.symvers. 240 if [ ! -e "$workdir/.checksummed" ]; then 241 "$OBJTOOL" klp checksum "$workdir/orig.o" || 242 fail "klp checksum orig.o failed" 243 "$OBJTOOL" klp checksum "$workdir/patched.o" || 244 fail "klp checksum patched.o failed" 245 touch "$workdir/.checksummed" 246 fi 247 248 # klp diff looks for Module.symvers relative to the working directory. 249 ( cd "$workdir" && "$OBJTOOL" klp diff orig.o patched.o out.o ) \ 250 > "$workdir/diff.log" 2>&1 || rc=$? 251 252 [ "$rc" = "$expect" ] || 253 fail "klp diff exited $rc, expected $expect: $(tail -2 "$workdir/diff.log")" 254} 255 256cc_supports() 257{ 258 echo 'int f(void) { return 0; }' > "$workdir/flagtest.c" 259 $CC $1 -c "$workdir/flagtest.c" -o "$workdir/flagtest.o" 2>/dev/null 260} 261 262# partial_link <output> <object...> 263# 264# "ld -r" through the compiler driver so the link targets the same 265# architecture as the objects. 266partial_link() 267{ 268 local out="$1"; shift 269 270 $CC -r -nostdlib -o "$out" "$@" 2>/dev/null || 271 $CC -r -nostdlib -fuse-ld=lld -o "$out" "$@" 2>/dev/null 272} 273 274# find_thinlto_toolchain 275# 276# Set $THIN_LD to an lld from the same LLVM release as $CC (or THIN_CC). A 277# mismatched pair fails with "Invalid summary version", which reads like a 278# broken test rather than a broken environment. 279# 280# ThinLTO is clang-only; callers must use clang_only before calling this. 281# Only $CC (or an explicit THIN_CC override) is consulted -- the harness does 282# not search for a second compiler beside a gcc $CC. 283find_thinlto_toolchain() 284{ 285 local cc ver ld 286 287 for cc in "${THIN_CC:-}" "$CC"; do 288 [ -n "$cc" ] || continue 289 command -v "${cc%% *}" >/dev/null 2>&1 || return 1 290 291 ver=$($cc -dumpversion 2>/dev/null | cut -d. -f1) 292 293 for ld in "${THIN_LD:-}" "ld.lld-$ver" ld.lld; do 294 [ -n "$ld" ] || continue 295 command -v "$ld" >/dev/null 2>&1 || continue 296 297 echo 'int probe(void) { return 0; }' > "$workdir/probe.c" 298 $cc -flto=thin -O2 -c "$workdir/probe.c" \ 299 -o "$workdir/probe.o" 2>/dev/null || continue 300 "$ld" -r "$workdir/probe.o" -o "$workdir/probe.elf" \ 301 2>/dev/null || continue 302 303 THIN_CC="$cc" 304 THIN_LD="$ld" 305 return 0 306 done 307 done 308 309 return 1 310} 311 312out_sections() { $READELF -S -W "$workdir/out.o" 2>/dev/null; } 313out_relocs() { $READELF -r -W "$workdir/out.o" 2>/dev/null; } 314out_symbols() { $READELF -s -W "$workdir/out.o" 2>/dev/null; } 315diff_log() { cat "$workdir/diff.log"; } 316 317assert_section() 318{ 319 out_sections | grep -q "[[:space:]]$1[[:space:]]" || 320 fail "expected section '$1' in output" 321} 322 323assert_patched() 324{ 325 assert_section ".text.$1" 326} 327 328assert_not_patched() 329{ 330 out_sections | grep -q "[[:space:]].text.$1[[:space:]]" && 331 fail "function '$1' should not have been cloned" 332 return 0 333} 334