Commit 7c8dce4b authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann

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: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20191226210253.3132060-1-andriin@fb.com
parent 1162f844
...@@ -397,7 +397,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -397,7 +397,7 @@ static int do_skeleton(int argc, char **argv)
{ \n\ { \n\
struct %1$s *obj; \n\ struct %1$s *obj; \n\
\n\ \n\
obj = calloc(1, sizeof(*obj)); \n\ obj = (typeof(obj))calloc(1, sizeof(*obj)); \n\
if (!obj) \n\ if (!obj) \n\
return NULL; \n\ return NULL; \n\
if (%1$s__create_skeleton(obj)) \n\ if (%1$s__create_skeleton(obj)) \n\
...@@ -461,7 +461,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -461,7 +461,7 @@ static int do_skeleton(int argc, char **argv)
{ \n\ { \n\
struct bpf_object_skeleton *s; \n\ struct bpf_object_skeleton *s; \n\
\n\ \n\
s = calloc(1, sizeof(*s)); \n\ s = (typeof(s))calloc(1, sizeof(*s)); \n\
if (!s) \n\ if (!s) \n\
return -1; \n\ return -1; \n\
obj->skeleton = s; \n\ obj->skeleton = s; \n\
...@@ -479,7 +479,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -479,7 +479,7 @@ static int do_skeleton(int argc, char **argv)
/* maps */ \n\ /* maps */ \n\
s->map_cnt = %zu; \n\ s->map_cnt = %zu; \n\
s->map_skel_sz = sizeof(*s->maps); \n\ s->map_skel_sz = sizeof(*s->maps); \n\
s->maps = calloc(s->map_cnt, s->map_skel_sz);\n\ s->maps = (typeof(s->maps))calloc(s->map_cnt, s->map_skel_sz);\n\
if (!s->maps) \n\ if (!s->maps) \n\
goto err; \n\ goto err; \n\
", ",
...@@ -515,7 +515,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -515,7 +515,7 @@ static int do_skeleton(int argc, char **argv)
/* programs */ \n\ /* programs */ \n\
s->prog_cnt = %zu; \n\ s->prog_cnt = %zu; \n\
s->prog_skel_sz = sizeof(*s->progs); \n\ s->prog_skel_sz = sizeof(*s->progs); \n\
s->progs = calloc(s->prog_cnt, s->prog_skel_sz);\n\ s->progs = (typeof(s->progs))calloc(s->prog_cnt, s->prog_skel_sz);\n\
if (!s->progs) \n\ if (!s->progs) \n\
goto err; \n\ goto err; \n\
", ",
...@@ -538,7 +538,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -538,7 +538,7 @@ static int do_skeleton(int argc, char **argv)
\n\ \n\
\n\ \n\
s->data_sz = %d; \n\ s->data_sz = %d; \n\
s->data = \"\\ \n\ s->data = (void *)\"\\ \n\
", ",
file_sz); file_sz);
......
...@@ -371,7 +371,7 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) ...@@ -371,7 +371,7 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ $(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
# Make sure we are able to include and link libbpf against c++. # Make sure we are able to include and link libbpf against c++.
$(OUTPUT)/test_cpp: test_cpp.cpp $(BPFOBJ) $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
$(call msg, CXX,,$@) $(call msg, CXX,,$@)
$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@ $(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
......
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#include <iostream>
#include "libbpf.h" #include "libbpf.h"
#include "bpf.h" #include "bpf.h"
#include "btf.h" #include "btf.h"
#include "test_core_extern.skel.h"
/* do nothing, just make sure we can link successfully */ /* do nothing, just make sure we can link successfully */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct test_core_extern *skel;
/* libbpf.h */ /* libbpf.h */
libbpf_set_print(NULL); libbpf_set_print(NULL);
...@@ -16,5 +20,11 @@ int main(int argc, char *argv[]) ...@@ -16,5 +20,11 @@ int main(int argc, char *argv[])
/* btf.h */ /* btf.h */
btf__new(NULL, 0); btf__new(NULL, 0);
/* BPF skeleton */
skel = test_core_extern__open_and_load();
test_core_extern__destroy(skel);
std::cout << "DONE!" << std::endl;
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment