Skip to content

Commit 7c8dce4

Browse files
anakryikoborkmann
authored andcommitted
bpftool: Make skeleton C code compilable with C++ compiler
When auto-generated BPF skeleton C code is included from C++ application, it triggers compilation error due to void * being implicitly casted to whatever target pointer type. This is supported by C, but not C++. To solve this problem, add explicit casts, where necessary. To ensure issues like this are captured going forward, add skeleton usage in test_cpp test. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 1162f84 commit 7c8dce4

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static int do_skeleton(int argc, char **argv)
397397
{ \n\
398398
struct %1$s *obj; \n\
399399
\n\
400-
obj = calloc(1, sizeof(*obj)); \n\
400+
obj = (typeof(obj))calloc(1, sizeof(*obj)); \n\
401401
if (!obj) \n\
402402
return NULL; \n\
403403
if (%1$s__create_skeleton(obj)) \n\
@@ -461,7 +461,7 @@ static int do_skeleton(int argc, char **argv)
461461
{ \n\
462462
struct bpf_object_skeleton *s; \n\
463463
\n\
464-
s = calloc(1, sizeof(*s)); \n\
464+
s = (typeof(s))calloc(1, sizeof(*s)); \n\
465465
if (!s) \n\
466466
return -1; \n\
467467
obj->skeleton = s; \n\
@@ -479,7 +479,7 @@ static int do_skeleton(int argc, char **argv)
479479
/* maps */ \n\
480480
s->map_cnt = %zu; \n\
481481
s->map_skel_sz = sizeof(*s->maps); \n\
482-
s->maps = calloc(s->map_cnt, s->map_skel_sz);\n\
482+
s->maps = (typeof(s->maps))calloc(s->map_cnt, s->map_skel_sz);\n\
483483
if (!s->maps) \n\
484484
goto err; \n\
485485
",
@@ -515,7 +515,7 @@ static int do_skeleton(int argc, char **argv)
515515
/* programs */ \n\
516516
s->prog_cnt = %zu; \n\
517517
s->prog_skel_sz = sizeof(*s->progs); \n\
518-
s->progs = calloc(s->prog_cnt, s->prog_skel_sz);\n\
518+
s->progs = (typeof(s->progs))calloc(s->prog_cnt, s->prog_skel_sz);\n\
519519
if (!s->progs) \n\
520520
goto err; \n\
521521
",
@@ -538,7 +538,7 @@ static int do_skeleton(int argc, char **argv)
538538
\n\
539539
\n\
540540
s->data_sz = %d; \n\
541-
s->data = \"\\ \n\
541+
s->data = (void *)\"\\ \n\
542542
",
543543
file_sz);
544544

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
371371
$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
372372

373373
# Make sure we are able to include and link libbpf against c++.
374-
$(OUTPUT)/test_cpp: test_cpp.cpp $(BPFOBJ)
374+
$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
375375
$(call msg, CXX,,$@)
376376
$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
377377

tools/testing/selftests/bpf/test_cpp.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2+
#include <iostream>
23
#include "libbpf.h"
34
#include "bpf.h"
45
#include "btf.h"
6+
#include "test_core_extern.skel.h"
57

68
/* do nothing, just make sure we can link successfully */
79

810
int main(int argc, char *argv[])
911
{
12+
struct test_core_extern *skel;
13+
1014
/* libbpf.h */
1115
libbpf_set_print(NULL);
1216

@@ -16,5 +20,11 @@ int main(int argc, char *argv[])
1620
/* btf.h */
1721
btf__new(NULL, 0);
1822

23+
/* BPF skeleton */
24+
skel = test_core_extern__open_and_load();
25+
test_core_extern__destroy(skel);
26+
27+
std::cout << "DONE!" << std::endl;
28+
1929
return 0;
2030
}

0 commit comments

Comments
 (0)