| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | set -u
|
| 12 |
|
| 13 | TESTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
| 14 |
|
| 15 | |
| 16 | |
| 17 | FIXTURES_DIR="$(cd "$(dirname "$0")/fixtures" 2>/dev/null && pwd)"
|
| 18 |
|
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | CROSS_COMPILE="${CROSS_COMPILE:-}"
|
| 31 | CC="${CC:-${CROSS_COMPILE}gcc}"
|
| 32 | LD="${LD:-${CROSS_COMPILE}ld}"
|
| 33 | READELF="${READELF:-${CROSS_COMPILE}readelf}"
|
| 34 | OBJCOPY="${OBJCOPY:-${CROSS_COMPILE}objcopy}"
|
| 35 |
|
| 36 | OBJTOOL="${OBJTOOL:-$TESTS_DIR/../objtool}"
|
| 37 |
|
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | klp_preflight()
|
| 43 | {
|
| 44 | local tmp tool cc_version arch host cc_arch
|
| 45 |
|
| 46 | bail() { echo "Bail out! $*" >&2; exit 1; }
|
| 47 |
|
| 48 | |
| 49 | [ -x "$OBJTOOL" ] || [ ! -x "$TESTS_DIR/../$OBJTOOL" ] ||
|
| 50 | OBJTOOL="$TESTS_DIR/../$OBJTOOL"
|
| 51 |
|
| 52 | [ -x "$OBJTOOL" ] ||
|
| 53 | bail "objtool not found at '$OBJTOOL' -- build it first"
|
| 54 |
|
| 55 | |
| 56 | |
| 57 | OBJTOOL="$(realpath "$OBJTOOL")"
|
| 58 |
|
| 59 | "$OBJTOOL" klp 2>&1 | grep -q checksum ||
|
| 60 | bail "objtool was built without klp support; install libxxhash (>= 0.8) and rebuild"
|
| 61 |
|
| 62 | command -v "${CC%% *}" >/dev/null || bail "compiler not found: $CC"
|
| 63 |
|
| 64 | for tool in "$READELF" "$OBJCOPY" "$LD"; do
|
| 65 | command -v "${tool%% *}" >/dev/null || bail "$tool not found"
|
| 66 | done
|
| 67 |
|
| 68 | tmp="$(mktemp -d)" || bail "mktemp failed"
|
| 69 | echo 'int probe(void) { return 0; }' > "$tmp/probe.c"
|
| 70 | $CC -c -o "$tmp/probe.o" "$tmp/probe.c" 2>/dev/null ||
|
| 71 | { rm -rf "$tmp"; bail "$CC cannot compile a trivial object"; }
|
| 72 |
|
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | "$OBJTOOL" klp checksum "$tmp/probe.o" >/dev/null 2>&1 ||
|
| 83 | { rm -rf "$tmp"
|
| 84 | bail "objtool rejects an object built by '$CC'; they target" \
|
| 85 | "different architectures (set CROSS_COMPILE, or" \
|
| 86 | "--target= for clang)"; }
|
| 87 |
|
| 88 | |
| 89 | |
| 90 | $OBJCOPY -O binary --only-section=.text "$tmp/probe.o" "$tmp/probe.bin" 2>/dev/null ||
|
| 91 | { rm -rf "$tmp"
|
| 92 | bail "$OBJCOPY cannot read objects built by '$CC'; install" \
|
| 93 | "binutils-multiarch or set OBJCOPY=llvm-objcopy"; }
|
| 94 | |
| 95 | |
| 96 | |
| 97 | case "$($READELF -hW "$tmp/probe.o" | sed -n 's/.*Machine: *//p')" in
|
| 98 | *X86-64*|*Intel*80386*) cc_arch=x86 ;;
|
| 99 | *AArch64*) cc_arch=arm64 ;;
|
| 100 | *) cc_arch= ;;
|
| 101 | esac
|
| 102 | rm -rf "$tmp"
|
| 103 |
|
| 104 | |
| 105 | case "${ARCH:-$(uname -m)}" in
|
| 106 | x86_64|i?86) arch=x86 ;;
|
| 107 | aarch64*) arch=arm64 ;;
|
| 108 | *) arch="${ARCH:-$(uname -m)}" ;;
|
| 109 | esac
|
| 110 |
|
| 111 | case "$(uname -m)" in
|
| 112 | x86_64|i?86) host=x86 ;;
|
| 113 | aarch64*) host=arm64 ;;
|
| 114 | *) host="$(uname -m)" ;;
|
| 115 | esac
|
| 116 |
|
| 117 | [ -z "$cc_arch" ] || [ "$cc_arch" = "$arch" ] ||
|
| 118 | bail "ARCH says $arch but '$CC' builds $cc_arch objects;" \
|
| 119 | "the $arch tests would run against the wrong architecture"
|
| 120 |
|
| 121 | KLP_TEST_ARCH="$arch"
|
| 122 | KLP_TEST_PREFLIGHT=done
|
| 123 | export OBJTOOL CC KLP_TEST_ARCH KLP_TEST_PREFLIGHT
|
| 124 |
|
| 125 | cc_version="$($CC --version 2>/dev/null | head -1)"
|
| 126 | cat <<EOF
|
| 127 | # preflight
|
| 128 | # objtool $OBJTOOL (klp: yes)
|
| 129 | # compiler $cc_version
|
| 130 | # arch $KLP_TEST_ARCH$([ "$arch" = "$host" ] || echo " (host $host, cross)")
|
| 131 | # tmpdir ${TMPDIR:-/tmp} (each test builds in a fresh directory here)
|
| 132 | EOF
|
| 133 | }
|
| 134 |
|
| 135 | [ -n "${KLP_TEST_PREFLIGHT:-}" ] || klp_preflight
|
| 136 |
|
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | FIXTURE_CFLAGS="-O2 -ffunction-sections -fdata-sections -fno-common \
|
| 154 | -fno-asynchronous-unwind-tables"
|
| 155 |
|
| 156 | test_name="$(basename "$0" .sh)"
|
| 157 | workdir=
|
| 158 |
|
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | orig_obj=orig.o
|
| 164 | patched_obj=patched.o
|
| 165 |
|
| 166 | pass() { KLP_TEST_REPORTED=1; echo "ok - $test_name${1:+: $1}"; exit 0; }
|
| 167 | fail() { KLP_TEST_FAILED=1; echo "not ok - $test_name: $*"; exit 1; }
|
| 168 |
|
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | declared_skip()
|
| 181 | {
|
| 182 | KLP_TEST_REPORTED=1
|
| 183 | echo "ok - $test_name # SKIP (declared) $*"
|
| 184 | exit 0
|
| 185 | }
|
| 186 |
|
| 187 | probe_skip()
|
| 188 | {
|
| 189 | KLP_TEST_REPORTED=1
|
| 190 | echo "ok - $test_name # SKIP (probe) $*"
|
| 191 | exit 0
|
| 192 | }
|
| 193 | skip() { echo "ok - $test_name # SKIP $*"; exit 0; }
|
| 194 |
|
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | xfail()
|
| 200 | {
|
| 201 | KLP_TEST_REPORTED=1
|
| 202 | echo "not ok - $test_name${1:+: $1} # TODO known failure"
|
| 203 | exit 0
|
| 204 | }
|
| 205 |
|
| 206 | xpass()
|
| 207 | {
|
| 208 | KLP_TEST_FAILED=1
|
| 209 | echo "ok - $test_name${1:+: $1} # TODO expected failure, but passed"
|
| 210 | exit 1
|
| 211 | }
|
| 212 |
|
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | cleanup()
|
| 220 | {
|
| 221 | [ -n "$workdir" ] || return 0
|
| 222 |
|
| 223 | |
| 224 | |
| 225 | |
| 226 | case "${KLP_TEST_KEEP:-${KEEP:-failed}}" in
|
| 227 | all) return 0 ;;
|
| 228 | none) rm -rf "$workdir" ;;
|
| 229 | failed|*)
|
| 230 | [ "${1:-}" = exit ] || {
|
| 231 | rm -rf "$workdir"
|
| 232 | return 0
|
| 233 | }
|
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | [ -n "${KLP_TEST_REPORTED:-}" ] && [ -z "${KLP_TEST_FAILED:-}" ] && {
|
| 242 | rm -rf "$workdir"
|
| 243 | return 0
|
| 244 | }
|
| 245 | |
| 246 | [ -n "${KLP_TEST_WORKDIR:-}" ] ||
|
| 247 | echo "# kept $workdir"
|
| 248 | ;;
|
| 249 | esac
|
| 250 | }
|
| 251 |
|
| 252 | |
| 253 | setup()
|
| 254 | {
|
| 255 | if [ -n "${KLP_TEST_WORKDIR:-}" ]; then
|
| 256 | workdir="$KLP_TEST_WORKDIR"
|
| 257 | mkdir -p "$workdir" || fail "cannot create $workdir"
|
| 258 | else
|
| 259 | workdir="$(mktemp -d)" || fail "mktemp failed"
|
| 260 | fi
|
| 261 | trap 'cleanup exit' EXIT
|
| 262 |
|
| 263 | export_syms "$@"
|
| 264 | }
|
| 265 |
|
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | export_syms()
|
| 272 | {
|
| 273 | : > "$workdir/Module.symvers"
|
| 274 | add_exports vmlinux "$@"
|
| 275 | }
|
| 276 |
|
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | add_exports()
|
| 286 | {
|
| 287 | local owner="$1"; shift
|
| 288 |
|
| 289 | add_exports_ns "$owner" "" "$@"
|
| 290 | }
|
| 291 |
|
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | add_exports_ns()
|
| 302 | {
|
| 303 | local owner="$1" ns="$2"; shift 2
|
| 304 |
|
| 305 | local sym
|
| 306 |
|
| 307 | for sym in "$@"; do
|
| 308 | printf '0x00000000\t%s\t%s\tEXPORT_SYMBOL\t%s\n' \
|
| 309 | "$sym" "$owner" "$ns" >> "$workdir/Module.symvers"
|
| 310 | done
|
| 311 | }
|
| 312 |
|
| 313 | |
| 314 | gcc_only()
|
| 315 | {
|
| 316 | case "$($CC --version 2>/dev/null | head -1)" in
|
| 317 | *[Gg][Cc][Cc]*) return 0 ;;
|
| 318 | esac
|
| 319 | declared_skip "gcc only${1:+: $1}"
|
| 320 | }
|
| 321 |
|
| 322 | clang_only()
|
| 323 | {
|
| 324 | case "$($CC --version 2>/dev/null | head -1)" in
|
| 325 | *clang*) return 0 ;;
|
| 326 | esac
|
| 327 | declared_skip "clang only${1:+: $1}"
|
| 328 | }
|
| 329 |
|
| 330 | |
| 331 | build_one()
|
| 332 | {
|
| 333 | local fixture out
|
| 334 | fixture="$FIXTURES_DIR/$1"
|
| 335 | out="$workdir/$2"
|
| 336 | shift 2
|
| 337 |
|
| 338 | [ -f "$fixture" ] || fail "missing fixture $fixture"
|
| 339 |
|
| 340 | |
| 341 | |
| 342 | rm -f "$workdir/.checksummed"
|
| 343 |
|
| 344 | $CC -c $FIXTURE_CFLAGS "$@" -o "$out" "$fixture" 2>"$workdir/cc.log" ||
|
| 345 | fail "$(basename "$fixture") does not build: $(tail -1 "$workdir/cc.log")"
|
| 346 | }
|
| 347 |
|
| 348 | |
| 349 | build_pair()
|
| 350 | {
|
| 351 | local fixture="$1"; shift
|
| 352 |
|
| 353 | |
| 354 | |
| 355 | |
| 356 | orig_obj=orig.o
|
| 357 | patched_obj=patched.o
|
| 358 |
|
| 359 | build_one "$fixture" orig.o "$@"
|
| 360 | build_one "$fixture" patched.o "$@" -DPATCHED
|
| 361 | }
|
| 362 |
|
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | run_objtool_check()
|
| 374 | {
|
| 375 | local obj
|
| 376 |
|
| 377 | |
| 378 | |
| 379 | |
| 380 | rm -f "$workdir/.checksummed"
|
| 381 |
|
| 382 | for obj in "$orig_obj" "$patched_obj"; do
|
| 383 | "$OBJTOOL" "$@" "$workdir/$obj" ||
|
| 384 | fail "objtool $* failed on $obj"
|
| 385 | done
|
| 386 | }
|
| 387 |
|
| 388 | run_checksum()
|
| 389 | {
|
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | [ -e "$workdir/.checksummed" ] && return 0
|
| 396 |
|
| 397 | "$OBJTOOL" klp checksum "$workdir/$orig_obj" ||
|
| 398 | fail "klp checksum $orig_obj failed"
|
| 399 | "$OBJTOOL" klp checksum "$workdir/$patched_obj" ||
|
| 400 | fail "klp checksum $patched_obj failed"
|
| 401 | touch "$workdir/.checksummed"
|
| 402 | }
|
| 403 |
|
| 404 | |
| 405 | run_diff()
|
| 406 | {
|
| 407 | local expect="${1:-0}" rc=0
|
| 408 |
|
| 409 | run_checksum
|
| 410 |
|
| 411 | |
| 412 | ( cd "$workdir" && "$OBJTOOL" klp diff "$orig_obj" "$patched_obj" out.o ) \
|
| 413 | > "$workdir/diff.log" 2>&1 || rc=$?
|
| 414 |
|
| 415 | [ "$rc" = "$expect" ] ||
|
| 416 | fail "klp diff exited $rc, expected $expect: $(tail -2 "$workdir/diff.log")"
|
| 417 | }
|
| 418 |
|
| 419 | cc_supports()
|
| 420 | {
|
| 421 | echo 'int f(void) { return 0; }' > "$workdir/flagtest.c"
|
| 422 | $CC $1 -c "$workdir/flagtest.c" -o "$workdir/flagtest.o" 2>/dev/null
|
| 423 | }
|
| 424 |
|
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | partial_link()
|
| 430 | {
|
| 431 | local out="$1"; shift
|
| 432 |
|
| 433 | rm -f "$workdir/.checksummed"
|
| 434 |
|
| 435 | $CC -r -nostdlib -o "$out" "$@" 2>/dev/null ||
|
| 436 | $CC -r -nostdlib -fuse-ld=lld -o "$out" "$@" 2>/dev/null
|
| 437 | }
|
| 438 |
|
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | link_vmlinux()
|
| 459 | {
|
| 460 | local out="$1" lds="$workdir/sort.lds"; shift
|
| 461 |
|
| 462 | echo 'SECTIONS { .data : { *(SORT_BY_NAME(.data.*)) } }' > "$lds"
|
| 463 |
|
| 464 | $LD -e 0 -T "$lds" -o "$out" "$@" 2>/dev/null ||
|
| 465 | $CC -nostdlib -Wl,-e,0 -Wl,-T,"$lds" \
|
| 466 | -o "$out" "$@" 2>/dev/null ||
|
| 467 | $CC -nostdlib -fuse-ld=lld -Wl,-e,0 -Wl,-T,"$lds" \
|
| 468 | -o "$out" "$@" 2>/dev/null
|
| 469 | }
|
| 470 |
|
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | make_vmlinux_pair()
|
| 483 | {
|
| 484 | local orig=() patched=() seen= arg
|
| 485 |
|
| 486 | for arg in "$@"; do
|
| 487 | if [ "$arg" = -- ]; then seen=y; continue; fi
|
| 488 | if [ -n "$seen" ]; then patched+=( "$arg" ); else orig+=( "$arg" ); fi
|
| 489 | done
|
| 490 |
|
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | [ "${#orig[@]}" -gt 0 ] && [ "${#patched[@]}" -gt 0 ] ||
|
| 496 | fail "make_vmlinux_pair needs objects either side of --"
|
| 497 |
|
| 498 | partial_link "$workdir/vmlinux.o" "${orig[@]}" ||
|
| 499 | probe_skip "partial link unavailable"
|
| 500 | partial_link "$workdir/patched.o" "${patched[@]}" ||
|
| 501 | probe_skip "partial link unavailable"
|
| 502 |
|
| 503 | "$OBJTOOL" --klp-symids --link "$workdir/vmlinux.o" ||
|
| 504 | fail "objtool --klp-symids failed"
|
| 505 |
|
| 506 | link_vmlinux "$workdir/vmlinux" "$workdir/vmlinux.o" ||
|
| 507 | probe_skip "cannot link a vmlinux here"
|
| 508 |
|
| 509 | orig_obj=vmlinux.o
|
| 510 | }
|
| 511 |
|
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | build_module_pair()
|
| 523 | {
|
| 524 | local fixture="$1" modname="$2"; shift 2
|
| 525 |
|
| 526 | build_pair "$fixture" -DMODNAME="\"$modname\"" "$@"
|
| 527 | }
|
| 528 |
|
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | find_thinlto_toolchain()
|
| 539 | {
|
| 540 | local cc ver ld
|
| 541 |
|
| 542 | for cc in "${THIN_CC:-}" "$CC"; do
|
| 543 | [ -n "$cc" ] || continue
|
| 544 | command -v "${cc%% *}" >/dev/null 2>&1 || return 1
|
| 545 |
|
| 546 | ver=$($cc -dumpversion 2>/dev/null | cut -d. -f1)
|
| 547 |
|
| 548 | for ld in "${THIN_LD:-}" "ld.lld-$ver" ld.lld; do
|
| 549 | [ -n "$ld" ] || continue
|
| 550 | command -v "$ld" >/dev/null 2>&1 || continue
|
| 551 |
|
| 552 | echo 'int probe(void) { return 0; }' > "$workdir/probe.c"
|
| 553 | $cc -flto=thin -O2 -c "$workdir/probe.c" \
|
| 554 | -o "$workdir/probe.o" 2>/dev/null || continue
|
| 555 | "$ld" -r "$workdir/probe.o" -o "$workdir/probe.elf" \
|
| 556 | 2>/dev/null || continue
|
| 557 |
|
| 558 | THIN_CC="$cc"
|
| 559 | THIN_LD="$ld"
|
| 560 | return 0
|
| 561 | done
|
| 562 | done
|
| 563 |
|
| 564 | return 1
|
| 565 | }
|
| 566 |
|
| 567 | out_sections() { $READELF -S -W "$workdir/out.o" 2>/dev/null; }
|
| 568 | out_relocs() { $READELF -r -W "$workdir/out.o" 2>/dev/null; }
|
| 569 | out_symbols() { $READELF -s -W "$workdir/out.o" 2>/dev/null; }
|
| 570 | diff_log() { cat "$workdir/diff.log"; }
|
| 571 |
|
| 572 | |
| 573 | |
| 574 | |
| 575 | out_strings() { $READELF -p "$1" "$workdir/out.o" 2>/dev/null; }
|
| 576 |
|
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 |
|
| 583 | in_sections() { $READELF -S -W "$workdir/$1" 2>/dev/null; }
|
| 584 | in_symbols() { $READELF -s -W "$workdir/$1" 2>/dev/null; }
|
| 585 | in_relocs() { $READELF -r -W "$workdir/$1" 2>/dev/null; }
|
| 586 |
|
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | count_input_symbols()
|
| 595 | {
|
| 596 | in_symbols "$1" | awk -v n="$2" '$4 == "OBJECT" && $8 == n' | wc -l
|
| 597 | }
|
| 598 |
|
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | re_quote() { printf '%s' "$1" | sed 's|[].[^$*\\/]|\\&|g'; }
|
| 606 |
|
| 607 | has_input_section() { in_sections "$1" | grep -q "[[:space:]]$(re_quote "$2")[[:space:]]"; }
|
| 608 | has_input_symbol() { in_symbols "$1" | awk -v n="$2" '$NF == n' | grep -q .; }
|
| 609 |
|
| 610 | assert_input_section()
|
| 611 | {
|
| 612 | local obj
|
| 613 |
|
| 614 | for obj in "$orig_obj" "$patched_obj"; do
|
| 615 | has_input_section "$obj" "$1" ||
|
| 616 | fail "fixture produced no section '$1' in $obj"
|
| 617 | done
|
| 618 | }
|
| 619 |
|
| 620 | assert_input_symbol()
|
| 621 | {
|
| 622 | local obj
|
| 623 |
|
| 624 | for obj in "$orig_obj" "$patched_obj"; do
|
| 625 | has_input_symbol "$obj" "$1" ||
|
| 626 | fail "fixture produced no symbol '$1' in $obj"
|
| 627 | done
|
| 628 | }
|
| 629 |
|
| 630 | require_input_section()
|
| 631 | {
|
| 632 | local obj
|
| 633 |
|
| 634 | for obj in "$orig_obj" "$patched_obj"; do
|
| 635 | has_input_section "$obj" "$1" ||
|
| 636 | probe_skip "compiler produced no section '$1' here"
|
| 637 | done
|
| 638 | }
|
| 639 |
|
| 640 | assert_section()
|
| 641 | {
|
| 642 | out_sections | grep -q "[[:space:]]$(re_quote "$1")[[:space:]]" ||
|
| 643 | fail "expected section '$1' in output"
|
| 644 | }
|
| 645 |
|
| 646 | assert_no_section()
|
| 647 | {
|
| 648 | out_sections | grep -q "[[:space:]]$(re_quote "$1")[[:space:]]" &&
|
| 649 | fail "unexpected section '$1' in output"
|
| 650 | return 0
|
| 651 | }
|
| 652 |
|
| 653 | assert_patched()
|
| 654 | {
|
| 655 | assert_section ".text.$1"
|
| 656 | }
|
| 657 |
|
| 658 | assert_not_patched()
|
| 659 | {
|
| 660 | out_sections | grep -q "[[:space:]]$(re_quote ".text.$1")[[:space:]]" &&
|
| 661 | fail "function '$1' should not have been cloned"
|
| 662 | return 0
|
| 663 | }
|
| 664 |
|
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | section_relocs()
|
| 671 | {
|
| 672 | local sec="${1//./\\.}"
|
| 673 |
|
| 674 | out_relocs | awk "/rela$sec'/,/^\$/"
|
| 675 | }
|
| 676 |
|
| 677 | assert_reloc_sym()
|
| 678 | {
|
| 679 | section_relocs "$1" | awk -v n="$2" '$5 == n' | grep -q . ||
|
| 680 | fail "expected a relocation to '$2' in '$1'"
|
| 681 | }
|
| 682 |
|
| 683 | assert_no_reloc_sym()
|
| 684 | {
|
| 685 | section_relocs "$1" | awk -v n="$2" '$5 == n' | grep -q . &&
|
| 686 | fail "unexpected relocation to '$2' in '$1'"
|
| 687 | return 0
|
| 688 | }
|
| 689 |
|
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | assert_reloc_count()
|
| 700 | {
|
| 701 | local n
|
| 702 |
|
| 703 | assert_section "$1"
|
| 704 |
|
| 705 | n="$(section_relocs "$1" | grep -cE '^[0-9a-f]{8,}')"
|
| 706 | [ "$n" = "$2" ] ||
|
| 707 | fail "expected $2 relocations in '$1', found $n"
|
| 708 | }
|
| 709 |
|
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | assert_klp_sym()
|
| 716 | {
|
| 717 | out_symbols | grep -q "\.klp\.sym\.${2:-[^.]*}\.$(re_quote "$1")," ||
|
| 718 | fail "expected klp symbol for '$1'"
|
| 719 | }
|
| 720 |
|
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | assert_klp_sympos()
|
| 729 | {
|
| 730 | out_symbols | grep -qE "\.klp\.sym\.[^.]+\.$(re_quote "$1"),$2([[:space:]]|\$)" ||
|
| 731 | fail "expected klp symbol for '$1' with sympos $2, found:$(
|
| 732 | out_symbols | grep -o "\.klp\.sym\.[^.]*\.$(re_quote "$1"),[0-9]*" |
|
| 733 | sort -u | tr '\n' ' ')"
|
| 734 | }
|
| 735 |
|
| 736 | assert_no_klp_sym()
|
| 737 | {
|
| 738 | out_symbols | grep -q "\.klp\.sym\.${2:-[^.]*}\.$(re_quote "$1")," &&
|
| 739 | fail "unexpected klp symbol for '$1'"
|
| 740 | return 0
|
| 741 | }
|
| 742 |
|
| 743 | assert_tombstone()
|
| 744 | {
|
| 745 | out_symbols | grep -qE "\.klp\.tombstone\.$(re_quote "$1")([[:space:]]|\$)" ||
|
| 746 | fail "expected a tombstone for '$1'"
|
| 747 | }
|
| 748 |
|
| 749 | assert_symbol()
|
| 750 | {
|
| 751 | out_symbols | awk -v n="$1" '$NF == n' | grep -q . ||
|
| 752 | fail "expected symbol '$1' in output"
|
| 753 | }
|
| 754 |
|
| 755 | assert_no_symbol()
|
| 756 | {
|
| 757 | out_symbols | awk -v n="$1" '$NF == n' | grep -q . &&
|
| 758 | fail "unexpected symbol '$1' in output"
|
| 759 | return 0
|
| 760 | }
|
| 761 |
|
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | assert_diff_log()
|
| 768 | {
|
| 769 | diff_log | grep -qE -- "$1" ||
|
| 770 | fail "expected '$1' in klp diff output: $(tail -2 "$workdir/diff.log")"
|
| 771 | }
|
| 772 |
|
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | checksum_of()
|
| 788 | {
|
| 789 | local obj="$workdir/$1" sym="$2" off
|
| 790 |
|
| 791 | off="$($READELF -rW "$obj" 2>/dev/null |
|
| 792 | awk -v s="$sym" '/rela\.discard\.sym_checksum/,/^$/ {
|
| 793 | if ($5 == s) { print $1; exit }
|
| 794 | }')"
|
| 795 |
|
| 796 | [ -n "$off" ] || return 1
|
| 797 |
|
| 798 | $OBJCOPY -O binary --only-section=.discard.sym_checksum \
|
| 799 | "$obj" "$workdir/checksums.bin" 2>/dev/null || return 1
|
| 800 |
|
| 801 | dd if="$workdir/checksums.bin" bs=1 skip=$((16#$off + 8)) count=8 \
|
| 802 | status=none | od -An -tx1 | tr -d ' \n'
|
| 803 | }
|
| 804 |
|
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | checksum_pair()
|
| 812 | {
|
| 813 | run_checksum
|
| 814 |
|
| 815 | orig_checksum="$(checksum_of "$orig_obj" "$1")"
|
| 816 | patched_checksum="$(checksum_of "$patched_obj" "$1")"
|
| 817 |
|
| 818 | [ -n "$orig_checksum" ] ||
|
| 819 | fail "no checksum recorded for '$1' in $orig_obj"
|
| 820 | [ -n "$patched_checksum" ] ||
|
| 821 | fail "no checksum recorded for '$1' in $patched_obj"
|
| 822 | }
|
| 823 |
|
| 824 | assert_checksum_differs()
|
| 825 | {
|
| 826 | checksum_pair "$1"
|
| 827 |
|
| 828 | [ "$orig_checksum" != "$patched_checksum" ] ||
|
| 829 | fail "checksum for '$1' unchanged at $orig_checksum, expected it to differ"
|
| 830 | }
|
| 831 |
|
| 832 | assert_checksum_matches()
|
| 833 | {
|
| 834 | checksum_pair "$1"
|
| 835 |
|
| 836 | [ "$orig_checksum" = "$patched_checksum" ] ||
|
| 837 | fail "checksum for '$1' changed from $orig_checksum to" \
|
| 838 | "$patched_checksum, expected no change"
|
| 839 | }
|
| 840 |
|
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | run_post_link()
|
| 849 | {
|
| 850 | local expect="${1:-0}" rc=0
|
| 851 |
|
| 852 | "$OBJTOOL" klp post-link "$workdir/out.o" \
|
| 853 | > "$workdir/post-link.log" 2>&1 || rc=$?
|
| 854 |
|
| 855 | [ "$rc" = "$expect" ] ||
|
| 856 | fail "klp post-link exited $rc, expected $expect:" \
|
| 857 | "$(tail -2 "$workdir/post-link.log")"
|
| 858 | }
|
| 859 |
|
| 860 | |
| 861 | |
| 862 | section_flags()
|
| 863 | {
|
| 864 | out_sections | sed 's/^ *\[[ 0-9]*\] *//' |
|
| 865 | awk -v s="$1" '$1 == s && $7 ~ /^[A-Za-z]+$/ { print $7 }'
|
| 866 | }
|
| 867 |
|
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | assert_section_flag()
|
| 874 | {
|
| 875 | local flags; flags="$(section_flags "$1")"
|
| 876 |
|
| 877 | [ -n "$flags" ] ||
|
| 878 | fail "section '$1' has no flags, expected '$2'"
|
| 879 | case "$flags" in
|
| 880 | *"$2"*) ;;
|
| 881 | *) fail "section '$1' has flags '$flags', expected '$2'" ;;
|
| 882 | esac
|
| 883 | }
|
| 884 |
|
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | assert_klp_rela()
|
| 891 | {
|
| 892 | local name=".klp.rela.$1.$2"
|
| 893 |
|
| 894 | out_sections | grep -q "[[:space:]]$(re_quote "$name")[[:space:]]" ||
|
| 895 | fail "expected section '$name' in output"
|
| 896 |
|
| 897 | assert_section_flag "$name" o
|
| 898 | }
|
| 899 |
|
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | assert_livepatch_sym()
|
| 907 | {
|
| 908 | out_symbols | grep -E 'OS ?\[0xff20\]' |
|
| 909 | grep -qE "\.klp\.sym\.[^.]+\.$(re_quote "$1")," ||
|
| 910 | fail "expected a klp symbol for '$1' in SHN_LIVEPATCH"
|
| 911 | }
|