mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Binoy Ravindran via B4 Relay <devnull+binoy.vt.edu@kernel.org>
To: Josh Poimboeuf <jpoimboe@kernel.org>,
	 Peter Zijlstra <peterz@infradead.org>,
	Song Liu <song@kernel.org>,
	 Nathan Chancellor <nathan@kernel.org>,
	 Nick Desaulniers <ndesaulniers@google.com>,
	 Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>
Cc: Joe Lawrence <joe.lawrence@redhat.com>,
	linux-kernel@vger.kernel.org,  llvm@lists.linux.dev,
	Binoy Ravindran <binoy@vt.edu>
Subject: [PATCH v2 2/2] objtool/klp: Add test for constant pool labels
Date: Mon, 30 Nov 2026 18:50:38 -0500	[thread overview]
Message-ID: <20261130-objtool-klp-cst-v2-2-2718c17fca37@vt.edu> (raw)
In-Reply-To: <20261130-objtool-klp-cst-v2-0-2718c17fca37@vt.edu>

From: Binoy Ravindran <binoy@vt.edu>

A function that loads a constant from a .rodata.cst* pool reaches it
through a local label with no size: .LC0 with gcc, .LCPI0_0 with clang.
klp diff clones that label with its data. Before the previous patch,
__clone_symbol() copied no data for such a label and placed it at the
end of the output section, so the patched function loaded whatever was
linked there. Nothing fails at build or load time.

The fixture loads a double, which neither x86 nor arm64 can encode as an
immediate, so both compilers put it in .rodata.cst8. The test finds the
label by section, type and size rather than by name. It checks that the
output pool holds the eight bytes that the label names in the patched
object, and that every label in the output points at them.

Verified on x86-64 with gcc 12.2.0 and clang 14.0.6. With the previous
patch's change to klp-diff.c reverted, the test fails because the
constant is not copied. With it restored, the test passes. The whole
suite passes under both compilers. gcc was run as
"gcc -fno-PIE -fcf-protection=none": the distribution's gcc builds PIE
by default and then rejects -mnop-mcount, which makes every gcc test
that needs an ftrace call site fail.

Suggested-by: Joe Lawrence <joe.lawrence@redhat.com>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
 .../objtool/tests/generic/fixtures/constant_pool.c | 31 ++++++++
 tools/objtool/tests/generic/test-constant-pool.sh  | 91 ++++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/tools/objtool/tests/generic/fixtures/constant_pool.c b/tools/objtool/tests/generic/fixtures/constant_pool.c
new file mode 100644
index 000000000000..73ff434a2420
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/constant_pool.c
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A changed function that loads a constant from a .rodata.cst* pool.
+ *
+ * The compiler reaches a pool entry through a local label with no size: .LC0
+ * for gcc, .LCPI0_0 for clang.  The assembler keeps that label in the symbol
+ * table, rather than relocating against the section, because the section is
+ * SHF_MERGE and the linker may move its entries.
+ *
+ * A floating-point constant is used because it is the one way freestanding C
+ * gets a constant pool from both compilers on both architectures: neither x86
+ * nor arm64 can encode an arbitrary double as an immediate.  1234.5678 is
+ * chosen so that no other bytes in the object look like it.
+ */
+
+static const char __modinfo[]
+	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
+
+int untouched(int x)
+{
+	return x * 3;
+}
+
+double changed(double x)
+{
+#ifdef PATCHED
+	return x * 1234.5678;
+#else
+	return x * 2.5;
+#endif
+}
diff --git a/tools/objtool/tests/generic/test-constant-pool.sh b/tools/objtool/tests/generic/test-constant-pool.sh
new file mode 100755
index 000000000000..19e9468e0c08
--- /dev/null
+++ b/tools/objtool/tests/generic/test-constant-pool.sh
@@ -0,0 +1,91 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# A constant in a .rodata.cst* pool travels with the function that loads it.
+#
+# The compiler reaches the constant through a local label that has no size.
+# klp diff clones that label with its data, but __clone_symbol() used to copy
+# data only for a symbol with a size, so for such a label it copied nothing
+# and placed the label at the current end of the output section.  The patched
+# function then loads whatever lands there when the module is linked: the
+# module builds, loads and patches without a warning, and computes with the
+# wrong constant.
+#
+# The label's name depends on the compiler (.LC0 for gcc, .LCPI0_0 for
+# clang), so the test finds it by section, type and size, and checks the bytes
+# rather than the name.  The section is .rodata.cst8 under both compilers.
+
+. "$(dirname "$0")/../lib.sh"
+
+POOL=.rodata.cst8
+
+# pool_labels <object>
+#
+# The values of the sizeless, untyped symbols defined in $POOL: the labels the
+# compiler uses for pool entries.  Section symbols are type SECTION, so they
+# are not included.
+pool_labels()
+{
+	local idx
+
+	idx="$($READELF -S -W "$1" 2>/dev/null |
+	       sed -n "s/^ *\[ *\([0-9]*\)\] $(re_quote "$POOL") .*/\1/p" | head -1)"
+	[ -n "$idx" ] || return 0
+
+	$READELF -s -W "$1" 2>/dev/null |
+		awk -v i="$idx" '$4 == "NOTYPE" && $3 == "0" && $7 == i { print $2 }'
+}
+
+# pool_bytes <object>
+#
+# The contents of $POOL as one hex string, two digits per byte.
+pool_bytes()
+{
+	$OBJCOPY -O binary --only-section="$POOL" "$1" "$workdir/pool.bin" 2>/dev/null &&
+		od -An -v -tx1 "$workdir/pool.bin" | tr -d ' \n'
+}
+
+setup
+build_pair constant_pool.c
+
+# The premise: the patched function loads a constant from the pool through a
+# sizeless label.  Without it the rest passes whether or not the bug is there.
+assert_input_section "$POOL"
+labels="$(pool_labels "$workdir/$patched_obj")"
+[ -n "$labels" ] || fail "no sizeless label in $POOL of $patched_obj"
+
+# The eight bytes the patched function expects: the entry at its label.
+label="$(echo "$labels" | head -1)"
+in_pool="$(pool_bytes "$workdir/$patched_obj")" || fail "cannot read $POOL of $patched_obj"
+off=$((16#$label))
+want="${in_pool:$((off * 2)):16}"
+[ "${#want}" = 16 ] || fail "label at $label lies outside $POOL of $patched_obj"
+
+run_diff
+
+assert_patched     changed
+assert_not_patched untouched
+
+# The result: the output pool holds that entry, and every label in it points
+# at an entry inside the section rather than at its end.
+assert_section "$POOL"
+out_pool="$(pool_bytes "$workdir/out.o")" || fail "cannot read $POOL of out.o"
+
+case "$out_pool" in
+*"$want"*) ;;
+*)	fail "the constant ($want) was not copied into $POOL of out.o" \
+	     "(contents: ${out_pool:-empty})" ;;
+esac
+
+out_labels="$(pool_labels "$workdir/out.o")"
+for l in $out_labels; do
+	o=$((16#$l))
+	got="${out_pool:$((o * 2)):16}"
+	[ "${#got}" = 16 ] ||
+		fail "label at $l points past the end of $POOL in out.o" \
+		     "($(( ${#out_pool} / 2 )) bytes)"
+	[ "$got" = "$want" ] ||
+		fail "label at $l in out.o names $got, expected $want"
+done
+
+pass "constant pool entry copied with its sizeless label"

-- 
2.39.5



      parent reply	other threads:[~2026-11-30 23:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 23:50 [PATCH v2 0/2] objtool/klp: Copy constant pool entries for local labels Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` [PATCH v2 1/2] " Binoy Ravindran via B4 Relay
2026-11-30 23:50 ` Binoy Ravindran via B4 Relay [this message]

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=20261130-objtool-klp-cst-v2-2-2718c17fca37@vt.edu \
    --to=devnull+binoy.vt.edu@kernel.org \
    --cc=binoy@vt.edu \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --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

all inboxes | Powered by JetHome®