* [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
2026-12-04 4:07 ` Alexei Starovoitov
0 siblings, 2 replies; 3+ 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] 3+ messages in thread* Re: [PATCH bpf] libbpf: Ignore integer type names in CO-RE type matching
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
2026-12-04 4:07 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 14:26 UTC (permalink / raw)
To: Haowen Deng, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] libbpf: non-standard multi-line comment style in
__bpf_core_types_match()
--
commit f67211ffe5606e27596cd47c946078ed431f5a75
Author: Haowen Deng <denghaowen59@gmail.com>
libbpf: Ignore integer type names in CO-RE type matching
This commit skips the BTF type name comparison for BTF_KIND_INT in
__bpf_core_types_match() so integer types match solely by size and
signedness across compilers, and adds a corresponding selftest.
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index 2672623a41989..9d81688bb4cbc 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.
> */
[Severity: Low]
This isn't a bug, but BPF multi-line comment style puts the opening /* on
its own line with the comment text starting on the next line.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202141620.366-1-denghaowen59@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH bpf] libbpf: Ignore integer type names in CO-RE type matching
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
@ 2026-12-04 4:07 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-12-04 4:07 UTC (permalink / raw)
To: Haowen Deng, bpf
Cc: linux-kselftest, 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
On Wed, Dec 02, 2026 at 06:16 AM Haowen Deng <denghaowen59@gmail.com> wrote:
> 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.
That's not enough for char.
The kernel uses -funsigned-char since
commit 3bc753c06dd0 ("kbuild: treat char as always unsigned").
char is signed in bpf progs.
So local_sgn is 1, targ_sgn is 0 and this line returns 0:
return local_t->size == targ_t->size && local_sgn == targ_sgn;
prog type vmlinux type today patch
-------------- ------------------ ----- -----
unsigned short short unsigned int 0 1
char char 0 0
bool char 0 1
'char comm[16]' and 'const char *' args still don't match.
Same for trace_event_raw_* from vmlinux.h. They end with 'char __data[]'.
Same name and size is the same C type. Lets ignore the sign in that case.
> +static void test_integer_type_names(void)
> +{
No need for 47 lines that call libbpf internals.
With the fix the existing cases check the size and the sign.
a_struct___diff_sz is int vs long. int_typedef___diff is int vs unsigned.
Two lines in the existing progs cover the rest.
With these two lines I expect this from reading the code:
subtest today patch patch + diff
----------------- ----- ----- ------------
type_based___diff FAIL OK OK
kernel FAIL FAIL OK
The diff is on top of your patch. relo_core.c and both progs compile.
diff --git a/Documentation/bpf/llvm_reloc.rst b/Documentation/bpf/llvm_reloc.rst
index 44188e2..c420980 100644
--- a/Documentation/bpf/llvm_reloc.rst
+++ b/Documentation/bpf/llvm_reloc.rst
@@ -323,7 +323,8 @@ Notes:
* The ``BPF_CORE_TYPE_MATCHES`` queries matching relation, defined as
follows:
- * for integers: types match if size and signedness match;
+ * for integers: types match if size and signedness match, ``char`` matches
+ ``char`` of any signedness;
* for arrays & pointers: target types are recursively matched;
* for structs & unions:
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 9d81688..9c287cd 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -1548,6 +1548,7 @@ static int bpf_core_composites_match(const struct btf *local_btf, const struct b
* Then, depending on type:
* - integers:
* - match if size and signedness match
+ * - char matches char of any signedness
* - arrays & pointers:
* - target types are recursively matched
* - structs & unions:
@@ -1654,7 +1655,13 @@ recur:
local_sgn = btf_int_encoding(local_t) & BTF_INT_SIGNED;
targ_sgn = btf_int_encoding(targ_t) & BTF_INT_SIGNED;
- return local_t->size == targ_t->size && local_sgn == targ_sgn;
+ if (local_t->size != targ_t->size)
+ return 0;
+
+ /* signedness of plain char depends on compiler flags */
+ return local_sgn == targ_sgn ||
+ bpf_core_names_match(local_btf, local_t->name_off,
+ targ_btf, targ_t->name_off);
}
case BTF_KIND_PTR:
if (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 a6690a3..80432c4 100644
--- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c
+++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
@@ -7,7 +7,6 @@
#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;
@@ -1148,54 +1147,8 @@ cleanup:
}
}
-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);
}
diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h
index 5760ae0..db768b9 100644
--- a/tools/testing/selftests/bpf/progs/core_reloc_types.h
+++ b/tools/testing/selftests/bpf/progs/core_reloc_types.h
@@ -990,7 +990,7 @@ struct a_complex_struct___diff {
struct a_struct___forward *a;
void *b;
} x;
- volatile long y;
+ volatile long long y;
};
union a_union___diff {
diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
index ee4a601..31e6f4b 100644
--- a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
+++ b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
@@ -37,6 +37,7 @@ struct mm_struct___wrong {
struct task_struct___local {
int pid;
+ char comm[16];
struct mm_struct___wrong *mm;
};
Also say in the commit log that bool now matches u8
and long matches long long.
Pls split libbpf and selftests changes and target bpf-next.
pw-bot: cr
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 4:07 UTC | newest]
Thread overview: 3+ 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
2026-12-04 4:07 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox