Skip to content

Commit 189f1a9

Browse files
anakryikoborkmann
authored andcommitted
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. Fixes: 351131b ("libbpf: add btf_dump API for BTF-to-C conversion") Reported-by: Tejun Heo <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent c638b13 commit 189f1a9

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

tools/lib/bpf/btf_dump.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,10 +1559,12 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,
15591559
* Clang for BPF target generates func_proto with no
15601560
* args as a func_proto with a single void arg (e.g.,
15611561
* `int (*f)(void)` vs just `int (*f)()`). We are
1562-
* going to pretend there are no args for such case.
1562+
* going to emit valid empty args (void) syntax for
1563+
* such case. Similarly and conveniently, valid
1564+
* no args case can be special-cased here as well.
15631565
*/
1564-
if (vlen == 1 && p->type == 0) {
1565-
btf_dump_printf(d, ")");
1566+
if (vlen == 0 || (vlen == 1 && p->type == 0)) {
1567+
btf_dump_printf(d, "void)");
15661568
return;
15671569
}
15681570

tools/testing/selftests/bpf/progs/btf_dump_test_case_multidim.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ typedef int *ptr_arr_t[6];
1414

1515
typedef int *ptr_multiarr_t[7][8][9][10];
1616

17-
typedef int * (*fn_ptr_arr_t[11])();
17+
typedef int * (*fn_ptr_arr_t[11])(void);
1818

19-
typedef int * (*fn_ptr_multiarr_t[12][13])();
19+
typedef int * (*fn_ptr_multiarr_t[12][13])(void);
2020

2121
struct root_struct {
2222
arr_t _1;

tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ typedef void (*printf_fn_t)(const char *, ...);
100100
* `int -> char *` function and returns pointer to a char. Equivalent:
101101
* typedef char * (*fn_input_t)(int);
102102
* typedef char * (*fn_output_outer_t)(fn_input_t);
103-
* typedef const fn_output_outer_t (* fn_output_inner_t)();
103+
* typedef const fn_output_outer_t (* fn_output_inner_t)(void);
104104
* typedef const fn_output_inner_t fn_ptr_arr2_t[5];
105105
*/
106106
/* ----- START-EXPECTED-OUTPUT ----- */
@@ -127,7 +127,7 @@ typedef void (* (*signal_t)(int, void (*)(int)))(int);
127127

128128
typedef char * (*fn_ptr_arr1_t[10])(int **);
129129

130-
typedef char * (* (* const fn_ptr_arr2_t[5])())(char * (*)(int));
130+
typedef char * (* (* const fn_ptr_arr2_t[5])(void))(char * (*)(int));
131131

132132
struct struct_w_typedefs {
133133
int_t a;

0 commit comments

Comments
 (0)