linux-imx/tools/testing/selftests/bpf/test_cpp.cpp
Tony Ambardar fb99b106ad selftests/bpf: Fix C++ compile error from missing _Bool type
[ Upstream commit aa95073fd2 ]

While building, bpftool makes a skeleton from test_core_extern.c, which
itself includes <stdbool.h> and uses the 'bool' type. However, the skeleton
test_core_extern.skel.h generated *does not* include <stdbool.h> or use the
'bool' type, instead using the C-only '_Bool' type. Compiling test_cpp.cpp
with g++ 12.3 for mips64el/musl-libc then fails with error:

  In file included from test_cpp.cpp:9:
  test_core_extern.skel.h:45:17: error: '_Bool' does not name a type
     45 |                 _Bool CONFIG_BOOL;
        |                 ^~~~~

This was likely missed previously because glibc uses a GNU extension for
<stdbool.h> with C++ (#define _Bool bool), not supported by musl libc.

Normally, a C fragment would include <stdbool.h> and use the 'bool' type,
and thus cleanly work after import by C++. The ideal fix would be for
'bpftool gen skeleton' to output the correct type/include supporting C++,
but in the meantime add a conditional define as above.

Fixes: 7c8dce4b16 ("bpftool: Make skeleton C code compilable with C++ compiler")
Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/6fc1dd28b8bda49e51e4f610bdc9d22f4455632d.1722244708.git.tony.ambardar@gmail.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-10-04 16:29:18 +02:00

135 lines
2.6 KiB
C++

/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#include <iostream>
#include <unistd.h>
#include <linux/bpf.h>
#include <linux/btf.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include <bpf/btf.h>
#ifndef _Bool
#define _Bool bool
#endif
#include "test_core_extern.skel.h"
template <typename T>
class Skeleton {
private:
T *skel;
public:
Skeleton(): skel(nullptr) { }
~Skeleton() { if (skel) T::destroy(skel); }
int open(const struct bpf_object_open_opts *opts = nullptr)
{
int err;
if (skel)
return -EBUSY;
skel = T::open(opts);
err = libbpf_get_error(skel);
if (err) {
skel = nullptr;
return err;
}
return 0;
}
int load() { return T::load(skel); }
int attach() { return T::attach(skel); }
void detach() { return T::detach(skel); }
const T* operator->() const { return skel; }
T* operator->() { return skel; }
const T *get() const { return skel; }
};
static void dump_printf(void *ctx, const char *fmt, va_list args)
{
}
static void try_skeleton_template()
{
Skeleton<test_core_extern> skel;
std::string prog_name;
int err;
LIBBPF_OPTS(bpf_object_open_opts, opts);
err = skel.open(&opts);
if (err) {
fprintf(stderr, "Skeleton open failed: %d\n", err);
return;
}
skel->data->kern_ver = 123;
skel->data->int_val = skel->data->ushort_val;
err = skel.load();
if (err) {
fprintf(stderr, "Skeleton load failed: %d\n", err);
return;
}
if (!skel->kconfig->CONFIG_BPF_SYSCALL)
fprintf(stderr, "Seems like CONFIG_BPF_SYSCALL isn't set?!\n");
err = skel.attach();
if (err) {
fprintf(stderr, "Skeleton attach failed: %d\n", err);
return;
}
prog_name = bpf_program__name(skel->progs.handle_sys_enter);
if (prog_name != "handle_sys_enter")
fprintf(stderr, "Unexpected program name: %s\n", prog_name.c_str());
bpf_link__destroy(skel->links.handle_sys_enter);
skel->links.handle_sys_enter = bpf_program__attach(skel->progs.handle_sys_enter);
skel.detach();
/* destructor will destroy underlying skeleton */
}
int main(int argc, char *argv[])
{
struct btf_dump_opts opts = { };
struct test_core_extern *skel;
struct btf *btf;
int fd;
try_skeleton_template();
/* libbpf.h */
libbpf_set_print(NULL);
/* bpf.h */
bpf_prog_get_fd_by_id(0);
/* btf.h */
btf = btf__new(NULL, 0);
if (!libbpf_get_error(btf))
btf_dump__new(btf, dump_printf, nullptr, &opts);
/* BPF skeleton */
skel = test_core_extern__open_and_load();
test_core_extern__destroy(skel);
fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
if (fd < 0)
std::cout << "FAILED to enable stats: " << fd << std::endl;
else
::close(fd);
std::cout << "DONE!" << std::endl;
return 0;
}