From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796082658; b=qRVAhQU3sAYJVHrR6cYAslqQ4q9sFOHeQWs7zdSmdIabUxwIZTtgXNuHwH+InPfZDWz6 qOJK6c2wA81ba6dqdSZ90eai61GpVNi8D6wdPLnZwq5n/kPMrcuemOlxzXM6XIm18LFj4 SEe/ES5i3f7h40/+Lgh6vsZrPylGGzYeZOB56fq4PW4RlJx2twfSVcRWuaC6zhnIPgGGA Oak943xFCjV9x+09NnBquE7duTvokydK5+D9Yhe/Wv/2o16HwX9yN20Cj5p4iMxAs8HI/ gST37a74Y+ISAGn2rdJ7MbUL8fGJJZat/UFmDmi9jUvtIMFSAr8yQdzRh5+NBS3+rFw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796082658; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=GsvU6B6jl683Wv5/OoN/DKzjSCNQlzmPrwSMhHdu9f0=; b=z9d1GQiGJ74G74S5yaGqtfBiMx+KNXE6sRMygDyNqiZ8BZbzek3dlw4pQw+q/vObn0yM GUM+e+q+p4d8OriEEE3qLxxzdfWAPjVKp0KyGs3K5eW3dluBW8KbGs51ML+mJwyvPNsP1 cAQ4NvHu2AnfrhCdsLvlaQ3US2bITRUaNtmRR2C9HvceD7ueRYmufnvbffG7xE3Cgyb70 JrEgAxG41dmnye/Jb8j8yqoRzENlVqSVYHJpCDbjquChg7i9sTV0vC3RBQt0OG6DtbJDk W2I+QsOzghHuwwJI9P/CoUYs9ZtgOTZkewntkUyIHYFMJ3PfKSPK4qiHQnuWtkT5yDA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CCDwVdJb; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9CAF606A31A; Mon, 30 Nov 2026 23:50:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CCDwVdJb Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796082658; b=zs4mDZIXd6ZHngXhtAyCvKHsFf6Give9Tm2j5QRVmSZpQfhSKJeUG+DxOBzZGzSD8QUf 6k/e68Znq9lrWJMaX6UhQGGO5W5acFitUBvESggMOrLwKhQlGshBUuCmijxfNHUOJIvTe wBMZJnTgWS6CY33ZTDy1kanRh3B09vMVK+ADrc5dn3qXnCzh300gz7iwVGvxuLRoHIdXX TVYtxTKlwB3u0jAT7QH45E0HprEV9hmdaDNc379Porv/8aiPSupP40YIQDl3wiQvTd0lB g+3dVlXG/my8lOy09EUXkRVqEGsdp/ynsfGgy4J2+Rsu77ikkvwjrHBIPlfDLeCVf8w== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796082658; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=GsvU6B6jl683Wv5/OoN/DKzjSCNQlzmPrwSMhHdu9f0=; b=dGBF/WKXbCm+l+g4MYoRsD3BSPvUTb/2Njkev9nb7/SSxM/YMbuBONinWMICdTPVLkse 7SQ9n+eLnk97+ClQLsqAo5koEPf2Vlie5qjJHaGnOtoAYVGqoz1VBF1JDuA5UHkAke/HM jJOIf/JTcgAe5XriN9sTWBDcLSvsW+4nLFYr8R3hvvYjoNDiDZwk5JpBtZk/pE1gcJ0z+ 7arpvNd0yR6J1EQMQuqCAS51fEnYDrkBuLNp4Rm5NCx+8Uwo0EPuZ96gLrtJw4onEDDRc RQKoFm4VelAN4qvRKiA1NQgfv2y6vrpkTC5AsYx7JiNFCFEurVdOqJvFIOlJxkwvzYA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id CE6DF1F00898; Mon, 30 Nov 2026 23:50:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796082658; bh=GsvU6B6jl683Wv5/OoN/DKzjSCNQlzmPrwSMhHdu9f0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=CCDwVdJb4RLsZ23Tz1UqxXAqTHwY3c4BWIjBakxKOk2COcW1K9iqQNPdpt1TjL3+m gh1qcs7RQC+Uld1PD5qf0huvR38q8w+L8JGCDRdOrb1XGOpYtY6iRBOyo54Yw5tpD/ 7MDhRl3MoNBzX+qTLIIlhJ0Sm8jwhHwromk0dY/Mcl/lR+GlfJethRK9flSAgHxDgn GSCBLVO21k4g3FRlaZNt+/ce3ZFsd5nyMXfT3vnloWDCa8mnc0uV/z0USazyCL9Qu0 ULzzJmT9q1pjLISMhB07nXf9vwFibPgiz0oKhw6i36MFrrFPfUZSl/XqyhJGN970jJ +yDO2ttMncfQg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 5B8094EB0D; Mon, 30 Nov 2026 23:50:50 +0000 (UTC) From: Binoy Ravindran via B4 Relay Date: Mon, 30 Nov 2026 18:50:38 -0500 Subject: [PATCH v2 2/2] objtool/klp: Add test for constant pool labels X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261130-objtool-klp-cst-v2-2-2718c17fca37@vt.edu> References: <20261130-objtool-klp-cst-v2-0-2718c17fca37@vt.edu> In-Reply-To: <20261130-objtool-klp-cst-v2-0-2718c17fca37@vt.edu> To: Josh Poimboeuf , Peter Zijlstra , Song Liu , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt Cc: Joe Lawrence , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Binoy Ravindran X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796082649; l=6393; i=binoy@vt.edu; s=20261123; h=from:subject:message-id; bh=UdKfneuzXwMzyrodsLVJpReIS4K0+jtGACW1ElW6ROo=; b=RHyXKWsrdNR9EH4Eu8vHIySzTrhRmgJfjyT70kfYUrur+XWnNPD8lgFlNzKdSWBhkaKXz9NZY nPKSHJEgy98C1s/tPOinHTpoZF7RqbAYYDHe3TJqK5ja3h97gOXT4M/ X-Developer-Key: i=binoy@vt.edu; a=ed25519; pk=4cNdKPHP0InmhK/9bb7Tm2ZMUXUvLGfw1pmVt5b98fA= X-Endpoint-Received: by B4 Relay for binoy@vt.edu/20261123 with auth_id=1622 X-Original-From: Binoy Ravindran Reply-To: binoy@vt.edu Content-Transfer-Encoding: 7bit From: Binoy Ravindran 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 Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Binoy Ravindran --- .../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 +# +# 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 +# +# 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