BPF List
 help / color / mirror / Atom feed
From: Seke Kazuru <kazuruuni@gmail.com>
To: joe.lawrence@redhat.com
Cc: live-patching@vger.kernel.org, jpoimboe@kernel.org,
	mbenes@suse.cz, pmladek@suse.com, song@kernel.org,
	peterz@infradead.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] objtool/klp: Add test for computed-goto jump tables
Date: Tue,  1 Dec 2026 17:14:30 +0200	[thread overview]
Message-ID: <20261201151430.1712-1-kazuruuni@gmail.com> (raw)
In-Reply-To: <aw3hwnJKWXXF8KUE@redhat.com>

___bpf_prog_run() dispatches through a static local table of label
addresses.  Every entry points into the function itself, so the table
describes one build of the code rather than state shared with the
running kernel, but klp diff correlates it like any other static local.

That goes wrong in two ways.  When a patch leaves every label in place
the table is unchanged, the patched function is pointed at the running
kernel's copy, and every dispatch lands in the original handlers: the
module builds, loads and reports "patching complete", and the fix never
runs.  When a patch moves a label the correlated table has changed, and
klp diff rejects the patch with "changed data", so no fix to such a
function can be built.

The fixture is an interpreter in the shape of ___bpf_prog_run(), its
table filled with a default first as BPF_INSN_MAP() does, built with the
table in ordinary read-only data and in .data.rel.ro.c_jump_table, where
__annotate_jump_table puts it.  The test runs five segments, labels
moving and labels staying, the latter also in a module, and asserts the
premise of each before the result: the patched function carries its own
copy of the table, relocated into the new code; an ordinary static
local of the same function is still correlated; and the table of an
unchanged function is not pulled in.

The segments where no label moves come last, as only they depend on
the compiler's layout.  The change they make is in the last block: an
edit to an earlier handler that looked the same size still moved the
labels after it under Clang.

Verified with gcc 11 to 16 and Clang 15 to 19 and 21 to 23 on x86_64,
and with aarch64 gcc 13 and Clang 18 on Josh Poimboeuf's
klp-build-arm64-latest: the test fails on mainline; passes with
"jumptable" added to is_uncorrelated_static_local(), and with a rule
that leaves uncorrelated any static local whose relocations point
inside a function rather than at its start; fails at the
correlated-static check when no static local is correlated; and
reports a probe skip, never a pass, when the fixture's labels are
removed.  The same holds with the flags of -Os, IBT, retpoline, call
padding, kCFI and UBSAN bounds, except that Clang with retpolines
lowers the computed goto to a switch and keeps no code address in the
table, which the test reports as a probe skip.  On kernel/bpf/core.o
from 7.4-rc5 both forms show the same way: a change to the ALU_NEG
handler is rejected with "changed data", and a change to the text of
the pr_warn() in default_label builds with ___bpf_prog_run() pointing
at the running kernel's jumptable.

Link: https://lore.kernel.org/live-patching/CAJATrkz6CgsaAFjeGT_1TuUTxrpVYEo=9rGuobnhpU-LPxv_dA@mail.gmail.com/
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Seke Kazuru <kazuruuni@gmail.com>
---
 .../generic/fixtures/computed_goto_table.c    | 115 +++++++++++++++
 .../tests/generic/test-computed-goto-table.sh | 137 ++++++++++++++++++
 2 files changed, 252 insertions(+)
 create mode 100644 tools/objtool/tests/generic/fixtures/computed_goto_table.c
 create mode 100755 tools/objtool/tests/generic/test-computed-goto-table.sh

diff --git a/tools/objtool/tests/generic/fixtures/computed_goto_table.c b/tools/objtool/tests/generic/fixtures/computed_goto_table.c
new file mode 100644
index 0000000000..e64cb2ff4a
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/computed_goto_table.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A computed-goto interpreter in the shape of ___bpf_prog_run(): a static
+ * table of label addresses, filled with a default and then set per opcode,
+ * and dispatched by "goto *jumptable[op]".
+ *
+ * Every entry is the address of a label inside interp() itself, so the table
+ * describes this build of the function rather than holding state the running
+ * kernel has accumulated.
+ *
+ * The patch changes interp() in one of two ways:
+ *
+ *   - by default it changes the op_div handler, which moves the labels laid
+ *     out after it, so the table's contents change too;
+ *   - with SAME_SIZE it changes only the value default_label returns, in the
+ *     last block, so no label moves and the table is identical in both builds.
+ *
+ * ANNOTATE puts the tables where the kernel's __annotate_jump_table does with
+ * CONFIG_OBJTOOL.  Without it they land in ordinary read-only data, as they
+ * do on a kernel built without objtool.
+ *
+ * runs is an ordinary static local of the same function, which must go on
+ * being the running kernel's copy.  other() has a table of its own and the
+ * patch does not touch it.
+ */
+
+#ifndef MODNAME
+#define MODNAME "vmlinux"
+#endif
+
+/* klp diff takes the object's module name from .modinfo */
+static const char __modinfo[]
+	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=" MODNAME;
+
+#ifdef ANNOTATE
+#define __jump_table_sec __attribute__((section(".data.rel.ro.c_jump_table")))
+#else
+#define __jump_table_sec
+#endif
+
+struct insn {
+	unsigned char code;
+	int imm;
+};
+
+enum { OP_ADD = 1, OP_SUB, OP_DIV, OP_EXIT };
+
+long interp(const struct insn *insn, long acc)
+{
+	static unsigned long runs;
+	/*
+	 * Filled with a default first, as BPF_INSN_MAP() does, so that every
+	 * entry carries a relocation to a label of this function.
+	 */
+	static const void * const jumptable[256] __jump_table_sec = {
+		[0 ... 255] = &&default_label,
+		[OP_ADD]  = &&op_add,
+		[OP_SUB]  = &&op_sub,
+		[OP_DIV]  = &&op_div,
+		[OP_EXIT] = &&op_exit,
+	};
+
+	runs++;
+
+select_insn:
+	goto *jumptable[insn->code];
+
+op_add:
+	acc += insn->imm;
+	insn++;
+	goto select_insn;
+op_sub:
+	acc -= insn->imm;
+	insn++;
+	goto select_insn;
+op_div:
+#if defined(PATCHED) && !defined(SAME_SIZE)
+	acc = insn->imm ? acc / insn->imm : 0;
+#else
+	acc = acc / insn->imm;
+#endif
+	insn++;
+	goto select_insn;
+op_exit:
+	return acc + runs;
+default_label:
+	/*
+	 * The SAME_SIZE change has to be here, in the last block: an edit to
+	 * an earlier handler that looks the same size can still move the labels
+	 * after it (Clang laid out "+ 1" and "+ 2" in op_add differently).
+	 */
+#if defined(PATCHED) && defined(SAME_SIZE)
+	return -2;
+#else
+	return -1;
+#endif
+}
+
+long other(const unsigned char *op, long acc)
+{
+	static const void * const table[4] __jump_table_sec = {
+		&&o_end, &&o_inc, &&o_dec, &&o_end,
+	};
+
+next:
+	goto *table[*op++ & 3];
+o_inc:
+	acc++;
+	goto next;
+o_dec:
+	acc--;
+	goto next;
+o_end:
+	return acc;
+}
diff --git a/tools/objtool/tests/generic/test-computed-goto-table.sh b/tools/objtool/tests/generic/test-computed-goto-table.sh
new file mode 100755
index 0000000000..46789654c9
--- /dev/null
+++ b/tools/objtool/tests/generic/test-computed-goto-table.sh
@@ -0,0 +1,137 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# A computed-goto table travels with the function it belongs to.
+#
+# ___bpf_prog_run() dispatches through a static local table of label
+# addresses.  Those addresses point into the function itself, so the table
+# describes one build of the code; it is not state to share with the running
+# kernel.  klp diff correlates it like any other static local, and that goes
+# wrong in two ways:
+#
+#   - When the patch leaves every label where it was, the table's contents
+#     are unchanged, the patched function is pointed at the running kernel's
+#     copy, and every dispatch lands in the original handlers.  The module
+#     builds, loads and reports "patching complete", but the fix never runs.
+#   - When the patch moves a label, the correlated table has changed, and klp
+#     diff rejects the patch with "changed data".  No fix to such a function
+#     can be built.
+#
+# A correct patch carries the table with the function, its entries pointing
+# into the new code, in both cases.  Nothing fails at build or load time in the
+# first, so only a test notices.
+#
+# Report: https://lore.kernel.org/live-patching/CAJATrkz6CgsaAFjeGT_1TuUTxrpVYEo=9rGuobnhpU-LPxv_dA@mail.gmail.com/
+#
+# Load-bearing in the fixture: the table is filled with a default first so
+# that every entry has a relocation into interp(); the SAME_SIZE change is in
+# the last block, because an edit to an earlier handler moved the labels after
+# it under Clang although it looked the same size; and the table is named
+# jumptable, as in the kernel.  Both directions are checked: an ordinary
+# static local of the same function must still be correlated, and a table of
+# an unchanged function must not be pulled in.
+
+. "$(dirname "$0")/../lib.sh"
+
+# gcc names a static local <var>.<id>, Clang <func>.<var>.
+local_sym()
+{
+	in_symbols orig.o | awk -v v="$1" -v f="$2" '
+		$4 == "OBJECT" && ($NF ~ ("^" v "[.][0-9]+$") || $NF == f "." v) {
+			print $NF
+			exit
+		}'
+}
+
+# The section a symbol of out.o is defined in.
+out_sym_section()
+{
+	local ndx
+
+	ndx="$(out_symbols | awk -v n="$1" '$NF == n && $7 ~ /^[0-9]+$/ { print $7; exit }')"
+	[ -n "$ndx" ] || return 0
+	out_sections | sed 's/^ *\[ *//; s/\]//' | awk -v i="$ndx" '$1 == i { print $2; exit }'
+}
+
+# check [cflags...]: one build of the fixture, judged on both sides.
+check()
+{
+	local same_size= table runs other_table sec
+
+	case " $* " in *" -DSAME_SIZE "*) same_size=1 ;; esac
+
+	# -Wno-override-init stands in for kernel/bpf/Makefile, which builds
+	# core.o with it: the table is filled with a default and then
+	# overridden per opcode, which Clang warns about by default.
+	setup
+	if [ -n "${MODULE:-}" ]; then
+		build_module_pair computed_goto_table.c "$MODULE" -Wno-override-init "$@"
+	else
+		build_pair computed_goto_table.c -Wno-override-init "$@"
+	fi
+	run_checksum
+
+	table="$(local_sym jumptable interp)"
+	runs="$(local_sym runs interp)"
+	other_table="$(local_sym table other)"
+	[ -n "$table" ] && [ -n "$runs" ] && [ -n "$other_table" ] ||
+		fail "[$*] fixture produced no static local symbols" \
+		     "(jumptable '$table', runs '$runs', table '$other_table')"
+	assert_input_symbol "$table"
+
+	# The premise: the table's entries are relocations into interp's own
+	# text, and the build pair differs the way this segment needs.  Not
+	# every configuration keeps a table of code addresses: with retpolines
+	# Clang lowers the computed goto to a switch, and the table holds no
+	# address at all, so there is nothing for this test to check.
+	in_relocs orig.o |
+		awk '/^Relocation section/ { p = 0 } p; /^Relocation section/ && /data/ { p = 1 }' |
+		grep -q '[[:space:]]\.text\.interp[[:space:]]' ||
+		probe_skip "[$*] this compiler built the table without code addresses" \
+			   "(Clang does with retpolines)"
+	[ "$(checksum_of orig.o interp)" != "$(checksum_of patched.o interp)" ] ||
+		fail "[$*] the patch did not change interp"
+	if [ -n "$same_size" ]; then
+		[ "$(checksum_of orig.o "$table")" = "$(checksum_of patched.o "$table")" ] ||
+			probe_skip "[$*] the SAME_SIZE patch moved a label under this compiler"
+	else
+		[ "$(checksum_of orig.o "$table")" != "$(checksum_of patched.o "$table")" ] ||
+			fail "[$*] the patch moved no label, so the table did not change"
+	fi
+
+	run_diff
+
+	assert_patched interp
+	assert_not_patched other
+
+	# The table is the patch's own copy, pointing into the new interp
+	# (klp diff names it by the function symbol, interp + <offset>).
+	assert_no_klp_sym "$table"
+	assert_symbol "$table"
+	sec="$(out_sym_section "$table")"
+	[ -n "$sec" ] || fail "[$*] '$table' is not defined in the output"
+	section_relocs "$sec" | awk '$5 == "interp" || $5 == ".text.interp"' | grep -q . ||
+		fail "[$*] '$table' in the output does not point into the patched interp"
+
+	# The other direction: state is still shared, and the table of a
+	# function the patch does not change stays out.
+	assert_klp_sym "$runs" "${MODULE:-vmlinux}"
+	assert_no_symbol "$other_table"
+
+	cleanup
+}
+
+# Labels move: rejected as "changed data" today.  Unconditional, so the test
+# can never skip as a whole.
+check
+check -DANNOTATE
+
+# Labels stay: the silent case.  Built last, since a compiler which moves a
+# label anyway can only skip it.
+check -DSAME_SIZE
+check -DANNOTATE -DSAME_SIZE
+
+# The same in a module: the state it shares is the module's.
+MODULE=klp_test_mod check -DANNOTATE -DSAME_SIZE
+
+pass "computed-goto table carried with its function, whether or not its labels move"
-- 
2.43.0


  parent reply	other threads:[~2026-12-01 15:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28  6:51 objtool/klp: ___bpf_prog_run jumptable is correlated to the original, fix never runs Seke Kazuru
2026-11-30 20:51 ` Joe Lawrence
2026-12-01 15:11   ` Seke Kazuru
2026-12-01 15:14   ` Seke Kazuru [this message]
2026-12-01 15:22     ` [PATCH] objtool/klp: Add test for computed-goto jump tables sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201151430.1712-1-kazuruuni@gmail.com \
    --to=kazuruuni@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=song@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox