bpftool, selftests: Add tests for C dump and fix a dropped type
Subsystems (4)
Ihor Solodrai
reviewercover bpftool, selftests: Add tests for C dump and fix a dropped type
"bpftool btf dump format c" generates the vmlinux.h that BPF programs are built against, and it has no direct test coverage. Building against the header is not a substitute for testing it, nothing a compiler accepts would break the build.
Patch #3 is an example of a bug that might have been caught by a dedicated "format c" test.
---
Ihor Solodrai (4):
selftests/bpf: NUL-terminate bpftool command output selftests/bpf: Add tests for bpftool btf dump format c
bpftool: Don't drop a type in the sorted C dump selftests/bpf: Check that sorting preserves types in bpftool dump
tools/bpf/bpftool/btf.c | 6 +- tools/testing/selftests/bpf/bpftool_helpers.c | 7 +- .../bpf/prog_tests/bpftool_btf_dump.c | 223 ++++++++++++++++++ 3 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c
Signature · 1 line
2.55.0
Ihor Solodrai
reviewerPATCH 1/4 selftests/bpf: NUL-terminate bpftool command output
run_command() writes a bpftool command's output into a caller-supplied buffer, and every caller treats that buffer as a C string.
However fread() reports a byte count and doesn't terminate the string. The helper does not terminate either, so callers have to zero the buffer first. prog_tests/bpftool_metadata.c does not, for example.
Read one byte less and terminate in the helper.
--- tools/testing/selftests/bpf/bpftool_helpers.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/bpftool_helpers.c +5 -2
@@ -53,6 +53,7 @@ static int run_command(char *args, char *output_buf, size_t output_max_len)static char bpftool_path[PATH_MAX ] = {};bool suppress_output = !(output_buf && output_max_len);char command[BPFTOOL_FULL_CMD_MAX_LEN ]; +size_t n;FILE *f;int ret;
@@ -68,8 +69,10 @@ static int run_command(char *args, char *output_buf, size_t output_max_len)if (!f)return 1 ; -if (!suppress_output) -fread (output_buf,1 , output_max_len, f); +if (!suppress_output) { + n =fread (output_buf,1 , output_max_len -1 , f); + output_buf[n] ='\0' ; + } ret =pclose (f);return ret;
Signature · 1 line
2.55.0
Re: [PATCH bpf-next v1 1/4] selftests/bpf: NUL-terminate bpftool command output
Quoted 7 lines · Ihor Solodrai
run_command()writes a bpftool command's output into a caller-supplied buffer, and every caller treats that buffer as a C string.However
fread()reports a byte count and doesn't terminate the string. The helper does not terminate either, so callers have to zero the buffer first.prog_tests/bpftool_metadata.cdoes not, for example.
Quoted 5 lines
Read one byte less and terminate in the helper.
Fixes f21fae577446 ("selftests/bpf: Add a few helpers for bpftool testing")Signed-off-by Ihor Solodrai <[email protected]>---
...
Ihor Solodrai
reviewerPATCH 2/4 selftests/bpf: Add tests for bpftool btf dump format c
In reply to Ihor Solodrai"bpftool btf dump format c" generates the vmlinux.h that BPF programs are built against, and it has no test coverage at all. The only in-tree consumers are build systems, and none of them diff the result.
Building against the header only catches what a compiler rejects. Missing macros, a missing preserve_access_index pragma and the wrong padding width all compile.
Add a test for those three, over both sort orderings.
--- .../bpf/prog_tests/bpftool_btf_dump.c | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +151 -0
@@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <bpftool_helpers.h> +#include <bpf/btf.h> +#include <unistd.h> + +#define DUMP_BUF_SZ (16 * 1024) + +static int btf_to_tmpfile (struct btf *btf,char *path,size_t path_sz) +{ +const void *raw; +ssize_t written; +__u32 sz; +int fd; + + raw =btf__raw_data (btf, &sz); +if (!ASSERT_OK_PTR (raw,"raw_data" )) +return -1 ; + +snprintf (path, path_sz,"/tmp/bpftool_btf_dump.XXXXXX" ); + fd =mkstemp (path); +if (!ASSERT_OK_FD (fd,"mkstemp" )) +return -1 ; + + written =write (fd, raw, sz); +close (fd); +if (!ASSERT_EQ (written, sz,"write_btf" )) { +unlink (path); +return -1 ; + } +return 0 ; +} + +static char *dump_c (const char *path,bool sorted) +{ +char args[MAX_BPFTOOL_CMD_LEN ]; +char *buf; +int err; + + buf =malloc (DUMP_BUF_SZ ); +if (!ASSERT_OK_PTR (buf,"alloc" )) +return NULL ; + +snprintf (args,sizeof (args),"btf dump file %s format c%s" , + path, sorted ?"" :" unsorted" ); + err =get_bpftool_command_output (args, buf,DUMP_BUF_SZ ); +if (!ASSERT_OK (err,"dump" )) { +free (buf); +return NULL ; + } +return buf; +} + +/* +* struct holey { int c; <32 bit hole> int tail; }; +* +* One record with a hole, and a 4-byte long to pad it with. How records +* themselves are rendered is already covered by the build, so the fixture does +* not need to be more elaborate than that. +*/ +static struct btf *mk_btf (void ) +{ +struct btf *btf; +int id, err; + + btf =btf__new_empty (); +if (!ASSERT_OK_PTR (btf,"new_empty" )) +return NULL ; + + id =btf__add_int (btf,"int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_EQ (id,1 ,"int" )) + goto err_out; + + id =btf__add_int (btf,"long int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_GT (id,0 ,"long" )) + goto err_out; + + id =btf__add_struct (btf,"holey" ,16 ); +if (!ASSERT_GT (id,0 ,"struct_holey" )) + goto err_out; + + err =btf__add_field (btf,"c" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"holey_c" )) + goto err_out; + + err =btf__add_field (btf,"tail" ,1 ,96 ,0 ); +if (!ASSERT_OK (err,"holey_tail" )) + goto err_out; + +btf__set_pointer_size (btf,4 ); + +return btf; +err_out: +btf__free (btf); +return NULL ; +} + +/* +* Check only what the selftests build cannot: +* - bpf_helpers.h defines __ksym and __weak as well, and no program uses +* __bpf_fastcall, so losing the macro block changes nothing; +* - building without the preserve_access_index pragma is a supported mode +* (BPF_NO_PRESERVE_ACCESS_INDEX), so losing it only costs CO-RE; +* - the padding width comes from the BTF's pointer size, and a native build +* never runs the host bpftool over a differently sized target's BTF. +*/ +static void test_dump (const char *path,bool sorted) +{ +char *buf; + + buf =dump_c (path, sorted); +if (!buf) +return ; + +ASSERT_HAS_SUBSTR (buf,"#define __ksym __attribute__((section(\".ksyms\")))" , +"ksym" ); +ASSERT_HAS_SUBSTR (buf,"#define __weak __attribute__((weak))" ,"weak" ); +ASSERT_HAS_SUBSTR (buf,"#define __bpf_fastcall __attribute__((bpf_fastcall))" , +"bpf_fastcall" ); + +ASSERT_HAS_SUBSTR (buf,"#ifndef BPF_NO_PRESERVE_ACCESS_INDEX" ,"pai_guard" ); +ASSERT_HAS_SUBSTR (buf, +"#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)" , +"pai_push" ); +ASSERT_HAS_SUBSTR (buf,"#pragma clang attribute pop" ,"pai_pop" ); + +ASSERT_HAS_SUBSTR (buf,"long: 32;" ,"target_ptr_size" ); + +free (buf); +} + +void test_bpftool_btf_dump (void ) +{ +char path[PATH_MAX ]; +struct btf *btf; + + btf =mk_btf (); +if (!btf) +return ; +if (btf_to_tmpfile (btf, path,sizeof (path))) + goto out; + +if (test__start_subtest ("c_sorted" )) +test_dump (path, true); +if (test__start_subtest ("c_unsorted" )) +test_dump (path, false); + +unlink (path); +out: +btf__free (btf); +}
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v1 2/4] selftests/bpf: Add tests for bpftool btf dump format c
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The comment describing the `holey` struct layout contradicts the actual code, creating a 64-bit…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The comment describing the holey struct layout contradicts the actual code, creating a 64-bit hole instead of the documented 32-bit hole. --
--- Patch [2]: [PATCH bpf-next v1 2/4] selftests/bpf: Add tests for bpftool btf dump format c --- commit 6c049c9e731fda95ed237645c2367be42ed67768
Author: Ihor Solodrai <[email protected]>
selftests/bpf: Add tests for bpftool btf dump format c
This commit adds a selftest for "bpftool btf dump format c" to ensure that the generated vmlinux.h header contains the correct macros, pragmas, and padding widths. It verifies these features across both sorted and unsorted BTF output formats.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +0
-0
[ ... ]
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +9
-0+/* +* struct holey { int c; <32 bit hole> int tail; }; +* +* One record with a hole, and a 4-byte long to pad it with. How records +* themselves are rendered is already covered by the build, so the fixture does +* not need to be more elaborate than that. +*/ +static struct btf *mk_btf (void ) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 86
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +9
-0+ id =btf__add_struct (btf,"holey" ,16 ); +if (!ASSERT_GT (id,0 ,"struct_holey" )) + goto err_out; + + err =btf__add_field (btf,"c" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"holey_c" )) + goto err_out; + + err =btf__add_field (btf,"tail" ,1 ,96 ,0 );
[Severity: Low]
This isn't a bug, but does this create a 64-bit hole instead of the 32-bit hole mentioned in the comment above?
Since "c" is a 32-bit integer ending at bit 32 and "tail" starts at bit offset 96, the gap between them appears to be 64 bits.
Could the comment be updated to accurately reflect the actual struct layout generated here?
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +2
-0+if (!ASSERT_OK (err,"holey_tail" )) + goto err_out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH bpf-next v1 2/4] selftests/bpf: Add tests for bpftool btf dump format c
In reply to Ihor SolodraiThis isn't a bug, but since the pointer size isn't part of the serialized BTF and bpftool re-derives it from the 4-byte "long int" type, does the btf__set_pointer_size() call still do anything here…
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +0
-0@@ -0,0 +1,151 @@
[ ... ]
Quoted 20 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +20
-0+static struct btf *mk_btf (void ) +{ +struct btf *btf; +int id, err; + + btf =btf__new_empty (); +if (!ASSERT_OK_PTR (btf,"new_empty" )) +return NULL ; + + id =btf__add_int (btf,"int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_EQ (id,1 ,"int" )) + goto err_out; + + id =btf__add_int (btf,"long int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_GT (id,0 ,"long" )) + goto err_out; + + id =btf__add_struct (btf,"holey" ,16 ); +if (!ASSERT_GT (id,0 ,"struct_holey" )) + goto err_out;
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 90
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +10
-0+ + err =btf__add_field (btf,"c" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"holey_c" )) + goto err_out; + + err =btf__add_field (btf,"tail" ,1 ,96 ,0 ); +if (!ASSERT_OK (err,"holey_tail" )) + goto err_out; + +btf__set_pointer_size (btf,4 );
This isn't a bug, but since the pointer size isn't part of the serialized BTF and bpftool re-derives it from the 4-byte "long int" type, does the btf__set_pointer_size() call still do anything here, or could it go?
The BTF wire format has no pointer-size field, so btf__raw_data() doesn't carry it into the temp file. When bpftool re-parses the file it recomputes the value with determine_ptr_size().
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 96
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +6
-0+ +return btf; +err_out: +btf__free (btf); +return NULL ; +}
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/32317197542
Eduard Zingerman
maintainerRe: [PATCH bpf-next v1 2/4] selftests/bpf: Add tests for bpftool btf dump format c
In reply to Ihor SolodraiQuoted 22 lines · Ihor Solodrai
"bpftool btf dump format c" generates the
vmlinux.hthat BPF programs are built against, and it has no test coverage at all. The only in-tree consumers are build systems, and none of them diff the result.Building against the header only catches what a compiler rejects. Missing macros, a missing
preserve_access_indexpragma and the wrong padding width all compile.Add a test for those three, over both sort orderings.
Signed-off-by Ihor Solodrai <[email protected]>--- .../bpf/prog_tests/bpftool_btf_dump.c | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.ctools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +0
-0
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +9
-0@@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <bpftool_helpers.h> +#include <bpf/btf.h> +#include <unistd.h> + +#define DUMP_BUF_SZ (16 * 1024) + +static int btf_to_tmpfile (struct btf *btf,char *path,size_t path_sz)
Nit: let's drop path_sz and assume PATH_MAX.
Quoted 71 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +71
-0+{ +const void *raw; +ssize_t written; +__u32 sz; +int fd; + + raw =btf__raw_data (btf, &sz); +if (!ASSERT_OK_PTR (raw,"raw_data" )) +return -1 ; + +snprintf (path, path_sz,"/tmp/bpftool_btf_dump.XXXXXX" ); + fd =mkstemp (path); +if (!ASSERT_OK_FD (fd,"mkstemp" )) +return -1 ; + + written =write (fd, raw, sz); +close (fd); +if (!ASSERT_EQ (written, sz,"write_btf" )) { +unlink (path); +return -1 ; + } +return 0 ; +} + +static char *dump_c (const char *path,bool sorted) +{ +char args[MAX_BPFTOOL_CMD_LEN ]; +char *buf; +int err; + + buf =malloc (DUMP_BUF_SZ ); +if (!ASSERT_OK_PTR (buf,"alloc" )) +return NULL ; + +snprintf (args,sizeof (args),"btf dump file %s format c%s" , + path, sorted ?"" :" unsorted" ); + err =get_bpftool_command_output (args, buf,DUMP_BUF_SZ ); +if (!ASSERT_OK (err,"dump" )) { +free (buf); +return NULL ; + } +return buf; +} + +/* +* struct holey { int c; <32 bit hole> int tail; }; +* +* One record with a hole, and a 4-byte long to pad it with. How records +* themselves are rendered is already covered by the build, so the fixture does +* not need to be more elaborate than that. +*/ +static struct btf *mk_btf (void ) +{ +struct btf *btf; +int id, err; + + btf =btf__new_empty (); +if (!ASSERT_OK_PTR (btf,"new_empty" )) +return NULL ; + + id =btf__add_int (btf,"int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_EQ (id,1 ,"int" )) + goto err_out; + + id =btf__add_int (btf,"long int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_GT (id,0 ,"long" )) + goto err_out; + + id =btf__add_struct (btf,"holey" ,16 ); +if (!ASSERT_GT (id,0 ,"struct_holey" )) + goto err_out;
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 90
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +10
-0+ + err =btf__add_field (btf,"c" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"holey_c" )) + goto err_out; + + err =btf__add_field (btf,"tail" ,1 ,96 ,0 ); +if (!ASSERT_OK (err,"holey_tail" )) + goto err_out; + +btf__set_pointer_size (btf,4 );
Nit: I think the approach taken by btf_distill.c:test_distilled_base()
is a bit better, it drops verbose error checks after each btf__* constructor call and calls VALIDATE_RAW_BTF() instead. This is both concise and self-documenting.
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 101
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +11
-0+ +return btf; +err_out: +btf__free (btf); +return NULL ; +} + +/* +* Check only what the selftests build cannot: +* - bpf_helpers.h defines __ksym and __weak as well, and no program uses +* __bpf_fastcall, so losing the macro block changes nothing;
The above bullet point does not make sense.
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 105
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +4
-0+* - building without the preserve_access_index pragma is a supported mode +* (BPF_NO_PRESERVE_ACCESS_INDEX), so losing it only costs CO-RE; +* - the padding width comes from the BTF's pointer size, and a native build +* never runs the host bpftool over a differently sized target's BTF.
Please make these two inline with actual asserts,
also "... so losing it only costs CO-RE" is completely out of context here.
Quoted 46 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +46
-0+*/ +static void test_dump (const char *path,bool sorted) +{ +char *buf; + + buf =dump_c (path, sorted); +if (!buf) +return ; + +ASSERT_HAS_SUBSTR (buf,"#define __ksym __attribute__((section(\".ksyms\")))" , +"ksym" ); +ASSERT_HAS_SUBSTR (buf,"#define __weak __attribute__((weak))" ,"weak" ); +ASSERT_HAS_SUBSTR (buf,"#define __bpf_fastcall __attribute__((bpf_fastcall))" , +"bpf_fastcall" ); + +ASSERT_HAS_SUBSTR (buf,"#ifndef BPF_NO_PRESERVE_ACCESS_INDEX" ,"pai_guard" ); +ASSERT_HAS_SUBSTR (buf, +"#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)" , +"pai_push" ); +ASSERT_HAS_SUBSTR (buf,"#pragma clang attribute pop" ,"pai_pop" ); + +ASSERT_HAS_SUBSTR (buf,"long: 32;" ,"target_ptr_size" ); + +free (buf); +} + +void test_bpftool_btf_dump (void ) +{ +char path[PATH_MAX ]; +struct btf *btf; + + btf =mk_btf (); +if (!btf) +return ; +if (btf_to_tmpfile (btf, path,sizeof (path))) + goto out; + +if (test__start_subtest ("c_sorted" )) +test_dump (path, true); +if (test__start_subtest ("c_unsorted" )) +test_dump (path, false); + +unlink (path); +out: +btf__free (btf); +}
Ihor Solodrai
reviewerRe: [PATCH bpf-next v1 2/4] selftests/bpf: Add tests for bpftool btf dump format c
Quoted 63 lines · Eduard Zingerman
Quoted 22 lines · Ihor Solodrai
"bpftool btf dump format c" generates the
vmlinux.hthat BPF programs are built against, and it has no test coverage at all. The only in-tree consumers are build systems, and none of them diff the result.Building against the header only catches what a compiler rejects. Missing macros, a missing
preserve_access_indexpragma and the wrong padding width all compile.Add a test for those three, over both sort orderings.
Signed-off-by Ihor Solodrai <[email protected]>--- .../bpf/prog_tests/bpftool_btf_dump.c | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+)create mode 100644
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.ctools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +0
-0tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +9
-0@@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <bpftool_helpers.h> +#include <bpf/btf.h> +#include <unistd.h> + +#define DUMP_BUF_SZ (16 * 1024) + +static int btf_to_tmpfile (struct btf *btf,char *path,size_t path_sz)Nit: let's drop
path_szand assumePATH_MAX.Quoted 28 lines
[...]
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +27
-0+static struct btf *mk_btf (void ) +{ +struct btf *btf; +int id, err; + + btf =btf__new_empty (); +if (!ASSERT_OK_PTR (btf,"new_empty" )) +return NULL ; + + id =btf__add_int (btf,"int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_EQ (id,1 ,"int" )) + goto err_out; + + id =btf__add_int (btf,"long int" ,4 ,BTF_INT_SIGNED ); +if (!ASSERT_GT (id,0 ,"long" )) + goto err_out; + + id =btf__add_struct (btf,"holey" ,16 ); +if (!ASSERT_GT (id,0 ,"struct_holey" )) + goto err_out; + + err =btf__add_field (btf,"c" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"holey_c" )) + goto err_out; + + err =btf__add_field (btf,"tail" ,1 ,96 ,0 ); +if (!ASSERT_OK (err,"holey_tail" ))
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 90
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +3
-0+ goto err_out; + +btf__set_pointer_size (btf,4 );Nit: I think the approach taken by btf_distill.c:test_distilled_base()
is a bit better, it drops verbose error checks after each btf__* constructor call and callsVALIDATE_RAW_BTF()instead. This is both concise and self-documenting.
Wasn't aware of that one. Will try it, thanks.
Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c line 101
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c new file +11
-0+ +return btf; +err_out: +btf__free (btf); +return NULL ; +} + +/* +* Check only what the selftests build cannot: +* - bpf_helpers.h defines __ksym and __weak as well, and no program uses +* __bpf_fastcall, so losing the macro block changes nothing; The above bullet point does not make sense.
Quoted 7 lines
Quoted 4 lines
+ * - building without the
preserve_access_indexpragma is a supported mode
+ * (BPF_NO_PRESERVE_ACCESS_INDEX), so losing it only costs CO-RE;
+ * - the padding width comes from the BTF's pointer size, and a native build
+ * never runs the host bpftool over a differently sized target's BTF.Please make these two inline with actual asserts,
also "... so losing it only costs CO-RE" is completely out of context here.
This block made sense to me yesterday.
I was either too "in context" or too tired. Will fix.
Quoted 2 lines
Quoted 2 lines
[...]
Ihor Solodrai
reviewerPATCH 3/4 bpftool: Don't drop a type in the sorted C dump
In reply to Ihor SolodraiThe C dump sorts types by default, so that generated headers are diffable. The sorted dump emits one type fewer than the unsorted dump of the same BTF.
dump_btf_c() starts its loop at index 1 to skip the void type at BTF type ID 0. That holds for the unsorted dump, where the array index is the type ID, but not after qsort(): position 0 is then the lowest ranked type, and btf_type_rank() ranks an anonymous enum 0 while void takes the default rank of 10. So the enum is skipped, and void is emitted instead as a no-op.
Skip by type ID rather than by position.
--- tools/bpf/bpftool/btf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/bpf/bpftool/btf.c +5 -1
@@ -805,9 +805,13 @@ static int dump_btf_c(const struct btf *btf,if (sort_dump) datums =sort_btf_c (btf); -for (i =1 ; i < cnt; i++) { +for (i =0 ; i < cnt; i++) {int idx = datums ? datums[i].index : i; +/* type ID 0 is void, skip it */ +if (!idx) +continue ; + err =btf_dump__dump_type (d, idx);if (err) goto done;
Signature · 1 line
2.55.0
Re: [PATCH bpf-next v1 3/4] bpftool: Don't drop a type in the sorted C dump
Quoted 11 lines · Ihor Solodrai
The C dump sorts types by default, so that generated headers are diffable. The sorted dump emits one type fewer than the unsorted dump of the same BTF.
dump_btf_c()starts its loop at index 1 to skip the void type at BTF type ID 0. That holds for the unsorted dump, where the array index is the type ID, but not afterqsort(): position 0 is then the lowest ranked type, andbtf_type_rank()ranks an anonymous enum 0 while void takes the default rank of 10. So the enum is skipped, and void is emitted instead as a no-op.
Quoted 5 lines
Skip by type ID rather than by position.
Fixes 94133cf24bb3 ("bpftool: Introduce btf c dump sorting")Signed-off-by Ihor Solodrai <[email protected]>---
...
Re: [PATCH bpf-next v1 3/4] bpftool: Don't drop a type in the sorted C dump
In reply to Ihor SolodraiQuoted 25 lines · Ihor Solodrai
The C dump sorts types by default, so that generated headers are diffable. The sorted dump emits one type fewer than the unsorted dump of the same BTF.
dump_btf_c()starts its loop at index 1 to skip the void type at BTF type ID 0. That holds for the unsorted dump, where the array index is the type ID, but not afterqsort(): position 0 is then the lowest ranked type, andbtf_type_rank()ranks an anonymous enum 0 while void takes the default rank of 10. So the enum is skipped, and void is emitted instead as a no-op.Skip by type ID rather than by position.
Fixes 94133cf24bb3 ("bpftool: Introduce btf c dump sorting")Signed-off-by Ihor Solodrai <[email protected]>--- tools/bpf/bpftool/btf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)tools/bpf/bpftool/btf.c +0
-0
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 812
Quoted 9 lines
tools/bpf/bpftool/btf.c +3
-1@@ -805,9 +805,13 @@ static int dump_btf_c(const struct btf *btf,if (sort_dump) datums =sort_btf_c (btf); -for (i =1 ; i < cnt; i++) { +for (i =0 ; i < cnt; i++) {int idx = datums ? datums[i].index : i; +/* type ID 0 is void, skip it */ +if (!idx)
nit: my personal preference, but when dealing with integers that do not represent "error/success" (zero being a success), let's not use boolean operations. this should be obvious idx == 0, IMO
pw-bot: cr
(but nice find!)
Quoted 8 lines
tools/bpf/bpftool/btf.c +2
-1+continue ; + err =btf_dump__dump_type (d, idx);if (err) goto done; --2.55.0
Ihor Solodrai
reviewerPATCH 4/4 selftests/bpf: Check that sorting preserves types in bpftool dump
In reply to Ihor SolodraiSorting the C dump is meant to make generated headers diffable. The sorted and unsorted dumps of the same BTF have to emit the same types.
Extend the fixture with a few more top-level definitions, including anonymous enums, and add a subtest comparing how many types are emitted with or without sort.
--- .../bpf/prog_tests/bpftool_btf_dump.c | 78 ++++++++++++++++++- 1 file changed, 75 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +75 -3
@@ -53,15 +53,18 @@ static char *dump_c(const char *path, bool sorted)/* * struct holey { int c; <32 bit hole> int tail; }; +* enum { E0 = 1 }; enum { E1 = 2 }; enum { E2 = 3 }; +* struct s { int f; }; +* union u { int f; }; * * One record with a hole, and a 4-byte long to pad it with. How records -* themselves are rendered is already covered by the build, so the fixture does -* not need to be more elaborate than that. +* themselves are rendered is already covered by the build. +* The anonymous enums matter to the sorting subtest. */ static struct btf *mk_btf (void ) {struct btf *btf; -int id, err; +int id, err, i; btf =btf__new_empty ();if (!ASSERT_OK_PTR (btf,"new_empty" ))
@@ -87,6 +90,36 @@ static struct btf *mk_btf(void)if (!ASSERT_OK (err,"holey_tail" )) goto err_out; +for (i =0 ; i <3 ; i++) { +char name[16 ]; + +snprintf (name,sizeof (name),"E%d" , i); + + id =btf__add_enum (btf,NULL ,4 ); +if (!ASSERT_GT (id,0 ,"anon_enum" )) + goto err_out; + + err =btf__add_enum_value (btf, name, i +1 ); +if (!ASSERT_OK (err,"enum_val" )) + goto err_out; + } + + id =btf__add_struct (btf,"s" ,4 ); +if (!ASSERT_GT (id,0 ,"struct_s" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"s_field" )) + goto err_out; + + id =btf__add_union (btf,"u" ,4 ); +if (!ASSERT_GT (id,0 ,"union_u" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"u_field" )) + goto err_out; +btf__set_pointer_size (btf,4 );return btf;
@@ -95,6 +128,42 @@ static struct btf *mk_btf(void)return NULL ; } +static int count_substr (const char *haystack,const char *needle) +{ +const char *p = haystack; +int n =0 ; + +for (; (p =strstr (p, needle)); p++) + n++; + +return n; +} + +static void test_sort_preserves_types (const char *path) +{ +char *sorted =NULL , *unsorted =NULL ; +int n_sorted, n_unsorted; + + sorted =dump_c (path, true); + unsorted =dump_c (path, false); +if (!sorted || !unsorted) + goto out; + +/* +* Count definitions by the closing brace in their first column. Counting +* "struct"/"union"/"enum" openers instead would also count forward +* declarations, and which types need one depends on emission order. +*/ + n_unsorted =count_substr (unsorted,"\n}" ); + n_sorted =count_substr (sorted,"\n}" ); +ASSERT_GT (n_unsorted,0 ,"types_emitted" ); +ASSERT_EQ (n_sorted, n_unsorted,"same_type_count" ); + +out: +free (sorted); +free (unsorted); +} +/* * Check only what the selftests build cannot: * - bpf_helpers.h defines __ksym and __weak as well, and no program uses
@@ -145,6 +214,9 @@ void test_bpftool_btf_dump(void)if (test__start_subtest ("c_unsorted" ))test_dump (path, false); +if (test__start_subtest ("c_sort_preserves_types" )) +test_sort_preserves_types (path); +unlink (path); out:btf__free (btf);
Signature · 1 line
2.55.0
Eduard Zingerman
maintainerRe: [PATCH bpf-next v1 4/4] selftests/bpf: Check that sorting preserves types in bpftool dump
On Wed, 2026-08-19 at 17:06 -0700, Ihor Solodrai wrote:
...
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c static struct btf *mk_btf(void) · line 93
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +1
-0@@ -87,6 +90,36 @@ static struct btf *mk_btf(void)if (!ASSERT_OK (err,"holey_tail" )) goto err_out; +for (i =0 ; i <3 ; i++) {
Testing this locally, a single enum suffices to showcase the bug, why the complication?
Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +29
-0+char name[16 ]; + +snprintf (name,sizeof (name),"E%d" , i); + + id =btf__add_enum (btf,NULL ,4 ); +if (!ASSERT_GT (id,0 ,"anon_enum" )) + goto err_out; + + err =btf__add_enum_value (btf, name, i +1 ); +if (!ASSERT_OK (err,"enum_val" )) + goto err_out; + } + + id =btf__add_struct (btf,"s" ,4 ); +if (!ASSERT_GT (id,0 ,"struct_s" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"s_field" )) + goto err_out; + + id =btf__add_union (btf,"u" ,4 ); +if (!ASSERT_GT (id,0 ,"union_u" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"u_field" )) + goto err_out; +btf__set_pointer_size (btf,4 );return btf;@@ -95,6 +128,42 @@ static struct btf *mk_btf(void)
...
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +7
-0+static void test_sort_preserves_types (const char *path) +{ +char *sorted =NULL , *unsorted =NULL ; +int n_sorted, n_unsorted; + + sorted =dump_c (path, true); + unsorted =dump_c (path, false);
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c static struct btf *mk_btf(void) · line 158
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +10
-0+if (!sorted || !unsorted) + goto out; + +/* +* Count definitions by the closing brace in their first column. Counting +* "struct"/"union"/"enum" openers instead would also count forward +* declarations, and which types need one depends on emission order. +*/ + n_unsorted =count_substr (unsorted,"\n}" ); + n_sorted =count_substr (sorted,"\n}" );
This does not include e.g. typedef. I'd just match for presence of enum. Or better yet, compare full expected output with actual one and tailor several BTF definitions, each for a specific sub-test.
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +8
-0+ASSERT_GT (n_unsorted,0 ,"types_emitted" ); +ASSERT_EQ (n_sorted, n_unsorted,"same_type_count" ); + +out: +free (sorted); +free (unsorted); +} +/* * Check only what the selftests build cannot: * - bpf_helpers.h defines __ksym and __weak as well, and no program uses
...
Ihor Solodrai
reviewerRe: [PATCH bpf-next v1 4/4] selftests/bpf: Check that sorting preserves types in bpftool dump
Quoted 4 lines · Eduard Zingerman
On Wed, 2026-08-19 at 17:06 -0700, Ihor Solodrai wrote:
...
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c static struct btf *mk_btf(void) · line 93
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +1
-0@@ -87,6 +90,36 @@ static struct btf *mk_btf(void)if (!ASSERT_OK (err,"holey_tail" )) goto err_out; +for (i =0 ; i <3 ; i++) {Testing this locally, a single enum suffices to showcase the bug, why the complication?
Hi Eduard, thank you for reviewing so quickly.
For the bug that was fixed, yes. But in general, I thought it's good to have a couple of different things in the test sample.
Quoted 50 lines
Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +29
-0+char name[16 ]; + +snprintf (name,sizeof (name),"E%d" , i); + + id =btf__add_enum (btf,NULL ,4 ); +if (!ASSERT_GT (id,0 ,"anon_enum" )) + goto err_out; + + err =btf__add_enum_value (btf, name, i +1 ); +if (!ASSERT_OK (err,"enum_val" )) + goto err_out; + } + + id =btf__add_struct (btf,"s" ,4 ); +if (!ASSERT_GT (id,0 ,"struct_s" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"s_field" )) + goto err_out; + + id =btf__add_union (btf,"u" ,4 ); +if (!ASSERT_GT (id,0 ,"union_u" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"u_field" )) + goto err_out; +btf__set_pointer_size (btf,4 );return btf;@@ -95,6 +128,42 @@ static struct btf *mk_btf(void)...
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +13
-0+static void test_sort_preserves_types (const char *path) +{ +char *sorted =NULL , *unsorted =NULL ; +int n_sorted, n_unsorted; + + sorted =dump_c (path, true); + unsorted =dump_c (path, false); +if (!sorted || !unsorted) + goto out; + +/* +* Count definitions by the closing brace in their first column. Counting +* "struct"/"union"/"enum" openers instead would also count forward
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c static struct btf *mk_btf(void) · line 158
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +4
-0+* declarations, and which types need one depends on emission order. +*/ + n_unsorted =count_substr (unsorted,"\n}" ); + n_sorted =count_substr (sorted,"\n}" );This does not include e.g. typedef. I'd just match for presence of enum. Or better yet, compare full expected output with actual one and tailor several BTF definitions, each for a specific sub-test.
I wanted to avoid committing a sample generated header. I think the exact string check will also be too sensitive to the changes in libbpf dumper or bpftool.
Presence of enum only tests the concrete bug, and I wanted some simple proxy to a more generic sorted vs unsorted output check. That is, "did we drop anything while sorting?" check.
Ideally we'd need to parse the headers and compare the full set of emitted types.. An overkill here, I think.
We can just add counting typedefs as well.
That's an easy improvement. wdyt?
Quoted 14 lines
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +8
-0+ASSERT_GT (n_unsorted,0 ,"types_emitted" ); +ASSERT_EQ (n_sorted, n_unsorted,"same_type_count" ); + +out: +free (sorted); +free (unsorted); +} +/* * Check only what the selftests build cannot: * - bpf_helpers.h defines __ksym and __weak as well, and no program uses ...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v1 4/4] selftests/bpf: Check that sorting preserves types in bpftool dump
Quoted 14 lines · Ihor Solodrai
Quoted 4 lines · Eduard Zingerman
On Wed, 2026-08-19 at 17:06 -0700, Ihor Solodrai wrote:
...
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c static struct btf *mk_btf(void) · line 93
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +1
-0@@ -87,6 +90,36 @@ static struct btf *mk_btf(void)if (!ASSERT_OK (err,"holey_tail" )) goto err_out; +for (i =0 ; i <3 ; i++) {Testing this locally, a single enum suffices to showcase the bug, why the complication?
Quoted 4 lines
Hi Eduard, thank you for reviewing so quickly.
For the bug that was fixed, yes. But in general, I thought it's good to have a couple of different things in the test sample.
We have a good set of tests for libbpf's BTF dump logic (not bpftool). Given that, I think it is better to keep tests focused.
Quoted 68 lines
Quoted 51 lines
Quoted 35 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +29
-0+char name[16 ]; + +snprintf (name,sizeof (name),"E%d" , i); + + id =btf__add_enum (btf,NULL ,4 ); +if (!ASSERT_GT (id,0 ,"anon_enum" )) + goto err_out; + + err =btf__add_enum_value (btf, name, i +1 ); +if (!ASSERT_OK (err,"enum_val" )) + goto err_out; + } + + id =btf__add_struct (btf,"s" ,4 ); +if (!ASSERT_GT (id,0 ,"struct_s" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"s_field" )) + goto err_out; + + id =btf__add_union (btf,"u" ,4 ); +if (!ASSERT_GT (id,0 ,"union_u" )) + goto err_out; + + err =btf__add_field (btf,"f" ,1 ,0 ,0 ); +if (!ASSERT_OK (err,"u_field" )) + goto err_out; +btf__set_pointer_size (btf,4 );return btf;@@ -95,6 +128,42 @@ static struct btf *mk_btf(void)...
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +13
-0+static void test_sort_preserves_types (const char *path) +{ +char *sorted =NULL , *unsorted =NULL ; +int n_sorted, n_unsorted; + + sorted =dump_c (path, true); + unsorted =dump_c (path, false); +if (!sorted || !unsorted) + goto out; + +/* +* Count definitions by the closing brace in their first column. Counting +* "struct"/"union"/"enum" openers instead would also count forward Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c static struct btf *mk_btf(void) · line 158
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +4
-0+* declarations, and which types need one depends on emission order. +*/ + n_unsorted =count_substr (unsorted,"\n}" ); + n_sorted =count_substr (sorted,"\n}" );This does not include e.g. typedef. I'd just match for presence of enum. Or better yet, compare full expected output with actual one and tailor several BTF definitions, each for a specific sub-test.
I wanted to avoid committing a sample generated header. I think the exact string check will also be too sensitive to the changes in libbpf dumper or bpftool.
Presence of enum only tests the concrete bug, and I wanted some simple proxy to a more generic sorted vs unsorted output check. That is, "did we drop anything while sorting?" check.
Quoted 5 lines
Ideally we'd need to parse the headers and compare the full set of emitted types.. An overkill here, I think.
We can just add counting typedefs as well.
That's an easy improvement. wdyt?
I don't think bpftool output changes all that match.
I won't block on this, but working with text is simpler in case if the test fails. One can get the 'diff' output directly pointing at what went wrong (and it would be nice to have a utility that shows the diff in case of mismatch, btf dump tests do that, but it is not abstracted).
Quoted 15 lines
Quoted 15 lines
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpftool_btf_dump.c +8
-0+ASSERT_GT (n_unsorted,0 ,"types_emitted" ); +ASSERT_EQ (n_sorted, n_unsorted,"same_type_count" ); + +out: +free (sorted); +free (unsorted); +} +/* * Check only what the selftests build cannot: * - bpf_helpers.h defines __ksym and __weak as well, and no program uses ...