All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] libbpf: Ignore integer type names in CO-RE type matching
@ 2026-12-02 14:16 Haowen Deng
  2026-12-02 14:26 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Haowen Deng @ 2026-12-02 14:16 UTC (permalink / raw)
  To: bpf
  Cc: linux-kselftest, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Shuah Khan, Daniel Müller

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02 14:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 14:16 [PATCH bpf] libbpf: Ignore integer type names in CO-RE type matching Haowen Deng
2026-12-02 14:26 ` sashiko-bot

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.