bpftool: Cast pointers for shadow types explicitly.
authorKui-Feng Lee <thinker.li@gmail.com>
Tue, 12 Mar 2024 01:37:26 +0000 (18:37 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 14 Mar 2024 20:34:21 +0000 (13:34 -0700)
According to a report, skeletons fail to assign shadow pointers when being
compiled with C++ programs. Unlike C doing implicit casting for void
pointers, C++ requires an explicit casting.

To support C++, we do explicit casting for each shadow pointer.

Also add struct_ops_module.skel.h to test_cpp to validate C++
compilation as part of BPF selftests.

Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20240312013726.1780720-1-thinker.li@gmail.com
tools/bpf/bpftool/gen.c
tools/testing/selftests/bpf/test_cpp.cpp

index 4fa4ade1ce7445eadac1cc92437218db97697e33..3ce277544c246d5615cb283a41089f3fbfe79e71 100644 (file)
@@ -1131,7 +1131,8 @@ static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
                        continue;
                codegen("\
                        \n\
-                               obj->struct_ops.%1$s = bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
+                               obj->struct_ops.%1$s = (typeof(obj->struct_ops.%1$s))\n\
+                                       bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
                        \n\
                        ", ident);
        }
index f4936834f76f4659db3b2af5adedb26cc6f84e34..dde0bb16e782e937fa19fa0556d8b9883a8de9f9 100644 (file)
@@ -7,6 +7,7 @@
 #include <bpf/bpf.h>
 #include <bpf/btf.h>
 #include "test_core_extern.skel.h"
+#include "struct_ops_module.skel.h"
 
 template <typename T>
 class Skeleton {
@@ -98,6 +99,7 @@ int main(int argc, char *argv[])
 {
        struct btf_dump_opts opts = { };
        struct test_core_extern *skel;
+       struct struct_ops_module *skel2;
        struct btf *btf;
        int fd;
 
@@ -118,6 +120,9 @@ int main(int argc, char *argv[])
        skel = test_core_extern__open_and_load();
        test_core_extern__destroy(skel);
 
+       skel2 = struct_ops_module__open_and_load();
+       struct_ops_module__destroy(skel2);
+
        fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
        if (fd < 0)
                std::cout << "FAILED to enable stats: " << fd << std::endl;