libbpf: fix no-args func prototype BTF dumping syntax
Subsystems (4)
Andrii Nakryiko
maintainerPATCH 1/1 libbpf: fix no-args func prototype BTF dumping syntax
For all these years libbpf's BTF dumper has been emitting not strictly valid syntax for function prototypes that have no input arguments.
Instead of int (*blah)() we should emit int (*blah)(void).
This is not normally a problem, but it manifests when we get kfuncs in vmlinux.h that have no input arguments. Due to compiler internal specifics, we get no BTF information for such kfuncs, if they are not declared with proper (void).
The fix is trivial. We also need to adjust a few ancient tests that happily assumed () is correct.
--- tools/lib/bpf/btf_dump.c | 8 +++++--- .../selftests/bpf/progs/btf_dump_test_case_multidim.c | 4 ++-- .../selftests/bpf/progs/btf_dump_test_case_syntax.c | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-)
tools/lib/bpf/btf_dump.c +5 -3
@@ -1559,10 +1559,12 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,* Clang for BPF target generates func_proto with no * args as a func_proto with a single void arg (e.g., * `int (*f)(void)` vs just `int (*f)()`). We are -* going to pretend there are no args for such case. +* going to emit valid empty args (void) syntax for +* such case. Similarly and conveniently, valid +* no args case can be special-cased here as well. */ -if (vlen ==1 && p->type ==0 ) { -btf_dump_printf (d,")" ); +if (vlen ==0 || (vlen ==1 && p->type ==0 )) { +btf_dump_printf (d,"void)" );return ; }
tools/testing/selftests/bpf/progs/btf_dump_test_case_multidim.c +2 -2
@@ -14,9 +14,9 @@ typedef int *ptr_arr_t[6];typedef int *ptr_multiarr_t [7 ][8 ][9 ][10 ]; -typedef int * (*fn_ptr_arr_t [11 ])(); +typedef int * (*fn_ptr_arr_t [11 ])(void ); -typedef int * (*fn_ptr_multiarr_t [12 ][13 ])(); +typedef int * (*fn_ptr_multiarr_t [12 ][13 ])(void );struct root_struct {arr_t _1;
tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c +2 -2
@@ -100,7 +100,7 @@ typedef void (*printf_fn_t)(const char *, ...);* `int -> char *` function and returns pointer to a char. Equivalent: * typedef char * (*fn_input_t)(int); * typedef char * (*fn_output_outer_t)(fn_input_t); -* typedef const fn_output_outer_t (* fn_output_inner_t)(); +* typedef const fn_output_outer_t (* fn_output_inner_t)(void); * typedef const fn_output_inner_t fn_ptr_arr2_t[5]; */ /* ----- START-EXPECTED-OUTPUT ----- */
@@ -127,7 +127,7 @@ typedef void (* (*signal_t)(int, void (*)(int)))(int);typedef char * (*fn_ptr_arr1_t [10 ])(int **); -typedef char * (* (*const fn_ptr_arr2_t [5 ])())(char * (*)(int )); +typedef char * (* (*const fn_ptr_arr2_t [5 ])(void ))(char * (*)(int ));struct struct_w_typedefs {int_t a;
Signature · 1 line
2.43.0
Quoted 10 lines · Andrii Nakryiko
For all these years libbpf's BTF dumper has been emitting not strictly valid syntax for function prototypes that have no input arguments.
Instead of
int (*blah)()we should emitint (*blah)(void).This is not normally a problem, but it manifests when we get kfuncs in
vmlinux.hthat have no input arguments. Due to compiler internal specifics, we get no BTF information for such kfuncs, if they are not declared with proper(void).
Quoted 6 lines
The fix is trivial. We also need to adjust a few ancient tests that happily assumed
()is correct.Reported-by Tejun Heo <[email protected]>Fixes 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")Signed-off-by Andrii Nakryiko <[email protected]>
This patch was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:
Quoted 11 lines · On Fri, 12 Jul 2024 15:44:42 -0700 you wrote:
For all these years libbpf's BTF dumper has been emitting not strictly valid syntax for function prototypes that have no input arguments.
Instead of
int (*blah)()we should emitint (*blah)(void).This is not normally a problem, but it manifests when we get kfuncs in
vmlinux.hthat have no input arguments. Due to compiler internal specifics, we get no BTF information for such kfuncs, if they are not declared with proper(void).[...]
Here is the summary with links:
- [bpf-next] libbpf: fix no-args func prototype BTF dumping syntax
https://git.kernel.org/bpf/bpf/c/189f1a976e42
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html