Fix BTF-to-C converter's padding generation
Subsystems (6)
cover Fix BTF-to-C converter's padding generation
Fix BTF-to-C logic of handling padding at the end of a struct. Fix existing test that should have captured this. Also move test_btf_dump into a test_progs test to leverage common infrastructure.
Andrii Nakryiko (3):
libbpf: fix struct end padding in btf_dump selftests/bpf: convert test_btf_dump into test_progs test selftests/bpf: fix btf_dump padding test case
tools/lib/bpf/btf_dump.c | 8 +-
tools/testing/selftests/bpf/Makefile | 2 +-
.../btf_dump.c} | 88 +++++++------------
.../bpf/progs/btf_dump_test_case_padding.c | 5 +-
4 files changed, 46 insertions(+), 57 deletions(-)
rename tools/testing/selftests/bpf/{test_btf_dump.c => prog_tests/btf_dump.c} (51%)Signature · 1 line
2.17.1
PATCH 1/3 libbpf: fix struct end padding in btf_dump
Fix a case where explicit padding at the end of a struct is necessary due to non-standart alignment requirements of fields (which BTF doesn't capture explicitly).
--- tools/lib/bpf/btf_dump.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/btf_dump.c +7 -1
@@ -876,7 +876,6 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,__u16 vlen =btf_vlen (t); packed = is_struct ?btf_is_struct_packed (d->btf, id, t) :0 ; - align = packed ?1 :btf_align_of (d->btf, id); btf_dump_printf(d, "%s%s%s {", is_struct ? "struct" : "union",
@@ -906,6 +905,13 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,btf_dump_printf (d,";" ); } +/* pad at the end, if necessary */ +if (is_struct) { + align = packed ?1 :btf_align_of (d->btf, id); +btf_dump_emit_bit_padding (d, off, t->size *8 ,0 , align, + lvl +1 ); + } +if (vlen)btf_dump_printf (d,"\n" );btf_dump_printf (d,"%s}" ,pfx (lvl));
Signature · 1 line
2.17.1
RE: [PATCH bpf-next 1/3] libbpf: fix struct end padding in btf_dump
Quoted 8 lines · Andrii Nakryiko
Fix a case where explicit padding at the end of a struct is necessary due to non-standart alignment requirements of fields (which BTF doesn't capture explicitly).
Fixes 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")Reported-by John Fastabend <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>---
Tested a few different kernels and seems to have resolved the issue I was seeing.
PATCH 2/3 selftests/bpf: convert test_btf_dump into test_progs test
In reply to Andrii NakryikoConvert test_btf_dump into a part of test_progs, instead of a stand-alone test binary.
---
tools/testing/selftests/bpf/Makefile | 2 +-
.../btf_dump.c} | 88 +++++++------------
2 files changed, 35 insertions(+), 55 deletions(-)
rename tools/testing/selftests/bpf/{test_btf_dump.c => prog_tests/btf_dump.c} (51%)tools/testing/selftests/bpf/Makefile +1 -1
@@ -29,7 +29,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage test_select_reuseport test_section_names \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ - test_btf_dump test_cgroup_attach xdping + test_cgroup_attach xdping BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c))) TEST_GEN_FILES = $(BPF_OBJ_FILES)
tools/testing/selftests/bpf/prog_tests/btf_dump.c renamed from tools/testing/selftests/bpf/test_btf_dump.c +34 -54
158 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 3/3 selftests/bpf: fix btf_dump padding test case
In reply to Andrii NakryikoExisting padding test case for btf_dump has a good test that was supposed to test padding generation at the end of a struct, but its expected output was specified incorrectly. Fix this.
--- .../testing/selftests/bpf/progs/btf_dump_test_case_padding.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/btf_dump_test_case_padding.c +4 -1
@@ -62,6 +62,10 @@ struct padded_a_lot {* long: 64; * long: 64; * int b; +* long: 32; +* long: 64; +* long: 64; +* long: 64; *}; * */
@@ -95,7 +99,6 @@ struct zone_padding {struct zone {int a;short b; - short: 16;struct zone_padding __pad__; };
Signature · 1 line
2.17.1
Quoted 4 lines · Andrii Nakryiko
Fix BTF-to-C logic of handling padding at the end of a struct. Fix existing test that should have captured this. Also move
test_btf_dumpinto atest_progstest to leverage common infrastructure.
Applied.
But I see some build weirdness.
Probably due to some previous patches.
Parallel build in selftests/bpf/ is not always succeeding.
For this particular set. New progs/*.c files failed to build the first time.
Quoted 7 lines · Alexei Starovoitov
Quoted 4 lines · Andrii Nakryiko
Fix BTF-to-C logic of handling padding at the end of a struct. Fix existing test that should have captured this. Also move
test_btf_dumpinto atest_progstest to leverage common infrastructure.
Quoted 5 lines
Applied.
But I see some build weirdness.
Probably due to some previous patches.
Parallel build in selftests/bpf/ is not always succeeding.
For this particular set. New progs/*.c files failed to build the first time.
I think it's another problem with test_attach_probe.c - it depends on libbpf's auto-generated bpf_helper_defs.h, which is not enforced to happen before progs/*.c are built. I think it's time to do some selftests/bpf/Makefile overhaul, honestly. But I'll try to come up with a quick fix for this particular issue as well.