All of lore.kernel.org
 help / color / mirror / Atom feed
From: Haowen Deng <denghaowen59@gmail.com>
To: bpf@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Shuah Khan" <shuah@kernel.org>,
	"Daniel Müller" <deso@posteo.net>
Subject: [PATCH bpf] libbpf: Ignore integer type names in CO-RE type matching
Date: Wed,  2 Dec 2026 22:16:08 +0800	[thread overview]
Message-ID: <20261202141620.366-1-denghaowen59@gmail.com> (raw)

GCC and Clang can encode the same integer type under different BTF names.
For example, a function parameter declared as unsigned short can appear
as "short unsigned int" in GCC BTF and "unsigned short" in Clang BTF.
The name comparison in __bpf_core_types_match() rejects these parameters
before reaching the integer comparison.

Skip the integer name comparison and use the existing size and
signedness checks. Add a BPF selftest that checks differently named
integer parameters and verifies that a change in size or signedness
still prevents a match.

The new test failed with the original libbpf and passed with the fix
when linked to each version in a standalone runner. On an x86-64 QEMU
guest running the patched bpf tree, test_progs -t
core_reloc,core_reloc_btfgen reported 3 tests and 159 subtests passed,
16 skipped, and no failures.

Codex assisted with investigating the reported mismatch, implementing
the fix, and writing and running the regression test.

Fixes: ec6209c8d42f ("bpf, libbpf: Add type match support")
Closes: https://github.com/libbpf/libbpf/issues/995
Signed-off-by: Haowen Deng <denghaowen59@gmail.com>
---
 tools/lib/bpf/relo_core.c                     | 14 ++++--
 .../selftests/bpf/prog_tests/core_reloc.c     | 47 +++++++++++++++++++
 2 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 2672623a4..9d81688bb 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -1582,16 +1582,20 @@ int __bpf_core_types_match(const struct btf *local_btf, __u32 local_id, const st
 	if (!local_t || !targ_t)
 		return -EINVAL;
 
-	/* While the name check happens after typedefs are skipped, root-level
+	local_k = btf_kind(local_t);
+	targ_k = btf_kind(targ_t);
+
+	/* Integer type names can differ by compiler. Match them by size and
+	 * signedness below.
+	 *
+	 * While the name check happens after typedefs are skipped, root-level
 	 * typedefs would still be name-matched as that's the contract with
 	 * callers.
 	 */
-	if (!bpf_core_names_match(local_btf, local_t->name_off, targ_btf, targ_t->name_off))
+	if (local_k != BTF_KIND_INT &&
+	    !bpf_core_names_match(local_btf, local_t->name_off, targ_btf, targ_t->name_off))
 		return 0;
 
-	local_k = btf_kind(local_t);
-	targ_k = btf_kind(targ_t);
-
 	switch (local_k) {
 	case BTF_KIND_UNKN:
 		return local_k == targ_k;
diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
index 80432c465..a6690a359 100644
--- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c
+++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
@@ -7,6 +7,7 @@
 #include <sys/mman.h>
 #include <sys/syscall.h>
 #include <bpf/btf.h>
+#include "bpf/relo_core.h"
 #include "bpftool_helpers.h"
 
 static int duration = 0;
@@ -1147,8 +1148,54 @@ static void run_core_reloc_tests(bool use_btfgen)
 	}
 }
 
+static void test_integer_type_names(void)
+{
+	static const struct {
+		const char *label;
+		int size;
+		int encoding;
+		int expected;
+	} cases[] = {
+		{ "same_size_and_signedness", 2, 0, 1 },
+		{ "different_size", 4, 0, 0 },
+		{ "different_signedness", 2, BTF_INT_SIGNED, 0 },
+	};
+	int i, local_int, local_proto, targ_int, targ_proto;
+	struct btf *btf;
+
+	btf = btf__new_empty();
+	if (!ASSERT_OK_PTR(btf, "btf_new"))
+		return;
+
+	local_int = btf__add_int(btf, "unsigned short", 2, 0);
+	if (!ASSERT_GT(local_int, 0, "local_int"))
+		goto out;
+	local_proto = btf__add_func_proto(btf, 0);
+	if (!ASSERT_GT(local_proto, 0, "local_proto") ||
+	    !ASSERT_OK(btf__add_func_param(btf, "mode", local_int), "local_param"))
+		goto out;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++) {
+		targ_int = btf__add_int(btf, "short unsigned int", cases[i].size,
+					cases[i].encoding);
+		if (!ASSERT_GT(targ_int, 0, "targ_int"))
+			goto out;
+		targ_proto = btf__add_func_proto(btf, 0);
+		if (!ASSERT_GT(targ_proto, 0, "targ_proto") ||
+		    !ASSERT_OK(btf__add_func_param(btf, "mode", targ_int), "targ_param"))
+			goto out;
+
+		ASSERT_EQ(bpf_core_types_match(btf, local_proto, btf, targ_proto),
+			  cases[i].expected, cases[i].label);
+	}
+out:
+	btf__free(btf);
+}
+
 void test_core_reloc(void)
 {
+	if (test__start_subtest("integer_type_names"))
+		test_integer_type_names();
 	run_core_reloc_tests(false);
 }
 

base-commit: 12e84ae1e0bd1f221eb804ecfa2a1e2105270015
-- 
2.43.0


             reply	other threads:[~2026-12-02 14:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 14:16 Haowen Deng [this message]
2026-12-02 14:26 ` [PATCH bpf] libbpf: Ignore integer type names in CO-RE type matching 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=20261202141620.366-1-denghaowen59@gmail.com \
    --to=denghaowen59@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=deso@posteo.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.