aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/test_cpp.cpp
blob: 0bd9990e83faf3edd5b977ea4f1ef315234407a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* 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>
#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 destory 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;
}
>/+2 2024-04-19Git 2.40.2v2.40.2Johannes Schindelin3-2/+9 2024-04-19Git 2.39.4v2.39.4Johannes Schindelin3-2/+81 2024-04-19fsck: warn about symlink pointing inside a gitdirJohannes Schindelin4-0/+117 2024-04-19core.hooksPath: add some protection while cloningJohannes Schindelin2-1/+27 2024-04-19init.templateDir: consider this config setting protectedJohannes Schindelin2-7/+61 2024-04-19clone: prevent hooks from running during a cloneJohannes Schindelin3-1/+94 2024-04-19Add a helper function to compare file contentsJohannes Schindelin4-0/+123 2024-04-17init: refactor the template directory discovery into its own functionJohannes Schindelin3-18/+37 2024-04-17find_hook(): refactor the `STRIP_EXTENSION` logicJohannes Schindelin1-7/+11 2024-04-17clone: when symbolic links collide with directories, keep the latterJohannes Schindelin3-2/+31 2024-04-17entry: report more colliding pathsJohannes Schindelin3-1/+20 2024-04-17t5510: verify that D/F confusion cannot lead to an RCEJohannes Schindelin1-0/+24 2024-04-17submodule: require the submodule path to contain directories onlyJohannes Schindelin4-5/+113 2024-04-17clone_submodule: avoid using `access()` on directoriesJohannes Schindelin1-1/+1 2024-04-17submodules: submodule paths must not contain symlinksJohannes Schindelin2-0/+83 2024-04-17clone: prevent clashing git dirs when cloning submodule in parallelFilip Hejsek3-2/+66