[PATCH v2 36/58] objtool/klp: Add test for position-independent checksums
From: Song Liu <song@kernel.org>
Date: 2026-09-14 06:28:40
Subsystem:
objtool, the rest · Maintainers:
Josh Poimboeuf, Peter Zijlstra, Linus Torvalds
A function that only moves has not changed, and its checksum must not move
with it. Otherwise every patch reports as changed everything that shifted
because something ahead of it grew.
The fixture is built with -fno-function-sections, overriding the harness
default: with per-function sections every function sits at offset 0 of its
own section and nothing ever moves, so the test would prove nothing. It
also calls across to another function rather than looping within itself --
a loop branch keeps the same displacement wherever the function goes, so it
is not position-dependent to begin with.
This tests the behavior of commit cca84cb12908 ("objtool/klp: Fix
position-dependent checksums for non-relocated jumps/calls").
Assisted-by: Claude:claude-opus-4
Based-on-test-by: Joe Lawrence [off-list ref]
Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@kernel.org>
---
.../generic/fixtures/checksum_position.c | 45 +++++++++++++++++++
.../tests/generic/test-checksum-position.sh | 35 +++++++++++++++
2 files changed, 80 insertions(+)
create mode 100644 tools/objtool/tests/generic/fixtures/checksum_position.c
create mode 100755 tools/objtool/tests/generic/test-checksum-position.sh
diff --git a/tools/objtool/tests/generic/fixtures/checksum_position.c b/tools/objtool/tests/generic/fixtures/checksum_position.c
new file mode 100644
index 000000000000..dab6f847a92d
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/checksum_position.c@@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A function whose position in the section changes between the two builds, + * without the function itself changing. + * + * PATCHED adds a function ahead of it, so target() moves. It must be built + * without -ffunction-sections, or every function sits at offset 0 of its own + * section and nothing ever moves -- which is why the test passes + * -fno-function-sections. + * + * target() calls callee() twice, and a call within the same section needs no + * relocation: the displacement is in the instruction. It is that displacement + * which moves, and hashing those bytes makes the checksum move with it. + */ + +static const char __modinfo[] + __attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux"; + +__attribute__((noinline)) static int callee(int x) +{ + return x * 5 + 1; +} + +/* + * Inserted between callee() and target(), so the distance target's call has to + * encode changes. A jump or call within the same section needs no relocation: + * the displacement is in the instruction, and it is that displacement which + * moves. + */ +#ifdef PATCHED +__attribute__((noinline)) int padding(int x) +{ + int i, s = 0; + + for (i = 0; i < x; i++) + s += i * 3; + + return s; +} +#endif + +__attribute__((noinline)) int target(int x) +{ + return callee(x) + callee(x + 1); +}
diff --git a/tools/objtool/tests/generic/test-checksum-position.sh b/tools/objtool/tests/generic/test-checksum-position.sh
new file mode 100755
index 000000000000..459a718d75e7
--- /dev/null
+++ b/tools/objtool/tests/generic/test-checksum-position.sh@@ -0,0 +1,35 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# A function's checksum must not depend on where the function sits. +# +# A jump or call without a relocation encodes its target as an offset from the +# instruction. Hashing those bytes makes the checksum change whenever anything +# ahead of the function changes size -- so an unrelated edit elsewhere in the +# file reports this function as changed too, and the patch grows to include it +# and everything it references. Nothing fails; the livepatch is just larger and +# riskier than the patch it came from. +# +# Here the "patch" adds a function ahead of target() and changes nothing else. + +. "$(dirname "$0")/../lib.sh" + +setup + +# -fno-function-sections, or each function is at offset 0 of its own section +# and target() never moves. +build_pair checksum_position.c -fno-function-sections + +assert_input_symbol target + +# The fixture is only meaningful if target() actually moved. +orig_off="$(in_symbols orig.o | awk '$8 == "target" { print $2 }')" +new_off="$( in_symbols patched.o | awk '$8 == "target" { print $2 }')" +[ -n "$orig_off" ] && [ -n "$new_off" ] || + fail "target symbol not found in both objects" +[ "$orig_off" != "$new_off" ] || + probe_skip "compiler did not move target() between builds" + +assert_checksum_matches target + +pass "checksum unchanged when the function only moves"
--
2.53.0-Meta