aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/bpf/prog_tests/btf_tag.c
blob: 071430cd54debce4641913a0a6439c76be05ca07 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include <test_progs.h>
#include <bpf/btf.h>
#include "test_btf_decl_tag.skel.h"

/* struct btf_type_tag_test is referenced in btf_type_tag.skel.h */
struct btf_type_tag_test {
        int **p;
};
#include "btf_type_tag.skel.h"
#include "btf_type_tag_user.skel.h"
#include "btf_type_tag_percpu.skel.h"

static void test_btf_decl_tag(void)
{
	struct test_btf_decl_tag *skel;

	skel = test_btf_decl_tag__open_and_load();
	if (!ASSERT_OK_PTR(skel, "btf_decl_tag"))
		return;

	if (skel->rodata->skip_tests) {
		printf("%s:SKIP: btf_decl_tag attribute not supported", __func__);
		test__skip();
	}

	test_btf_decl_tag__destroy(skel);
}

static void test_btf_type_tag(void)
{
	struct btf_type_tag *skel;

	skel = btf_type_tag__open_and_load();
	if (!ASSERT_OK_PTR(skel, "btf_type_tag"))
		return;

	if (skel->rodata->skip_tests) {
		printf("%s:SKIP: btf_type_tag attribute not supported", __func__);
		test__skip();
	}

	btf_type_tag__destroy(skel);
}

/* loads vmlinux_btf as well as module_btf. If the caller passes NULL as
 * module_btf, it will not load module btf.
 *
 * Returns 0 on success.
 * Return -1 On error. In case of error, the loaded btf will be freed and the
 * input parameters will be set to pointing to NULL.
 */
static int load_btfs(struct btf **vmlinux_btf, struct btf **module_btf,
		     bool needs_vmlinux_tag)
{
	const char *module_name = "bpf_testmod";
	__s32 type_id;

	if (!env.has_testmod) {
		test__skip();
		return -1;
	}

	*vmlinux_btf = btf__load_vmlinux_btf();
	if (!ASSERT_OK_PTR(*vmlinux_btf, "could not load vmlinux BTF"))
		return -1;

	if (!needs_vmlinux_tag)
		goto load_module_btf;

	/* skip the test if the vmlinux does not have __user tags */
	type_id = btf__find_by_name_kind(*vmlinux_btf, "user", BTF_KIND_TYPE_TAG);
	if (type_id <= 0) {
		printf("%s:SKIP: btf_type_tag attribute not in vmlinux btf", __func__);
		test__skip();
		goto free_vmlinux_btf;
	}

load_module_btf:
	/* skip loading module_btf, if not requested by caller */
	if (!module_btf)
		return 0;

	*module_btf = btf__load_module_btf(module_name, *vmlinux_btf);
	if (!ASSERT_OK_PTR(*module_btf, "could not load module BTF"))
		goto free_vmlinux_btf;

	/* skip the test if the module does not have __user tags */
	type_id = btf__find_by_name_kind(*module_btf, "user", BTF_KIND_TYPE_TAG);
	if (type_id <= 0) {
		printf("%s:SKIP: btf_type_tag attribute not in %s", __func__, module_name);
		test__skip();
		goto free_module_btf;
	}

	return 0;

free_module_btf:
	btf__free(*module_btf);
free_vmlinux_btf:
	btf__free(*vmlinux_btf);

	*vmlinux_btf = NULL;
	if (module_btf)
		*module_btf = NULL;
	return -1;
}

static void test_btf_type_tag_mod_user(bool load_test_user1)
{
	struct btf *vmlinux_btf = NULL, *module_btf = NULL;
	struct btf_type_tag_user *skel;
	int err;

	if (load_btfs(&vmlinux_btf, &module_btf, /*needs_vmlinux_tag=*/false))
		return;

	skel = btf_type_tag_user__open();
	if (!ASSERT_OK_PTR(skel, "btf_type_tag_user"))
		goto cleanup;

	bpf_program__set_autoload(skel->progs.test_sys_getsockname, false);
	if (load_test_user1)
		bpf_program__set_autoload(skel->progs.test_user2, false);
	else
		bpf_program__set_autoload(skel->progs.test_user1, false);

	err = btf_type_tag_user__load(skel);
	ASSERT_ERR(err, "btf_type_tag_user");

	btf_type_tag_user__destroy(skel);

cleanup:
	btf__free(module_btf);
	btf__free(vmlinux_btf);
}

static void test_btf_type_tag_vmlinux_user(void)
{
	struct btf_type_tag_user *skel;
	struct btf *vmlinux_btf = NULL;
	int err;

	if (load_btfs(&vmlinux_btf, NULL, /*needs_vmlinux_tag=*/true))
		return;

	skel = btf_type_tag_user__open();
	if (!ASSERT_OK_PTR(skel, "btf_type_tag_user"))
		goto cleanup;

	bpf_program__set_autoload(skel->progs.test_user2, false);
	bpf_program__set_autoload(skel->progs.test_user1, false);

	err = btf_type_tag_user__load(skel);
	ASSERT_ERR(err, "btf_type_tag_user");

	btf_type_tag_user__destroy(skel);

cleanup:
	btf__free(vmlinux_btf);
}

static void test_btf_type_tag_mod_percpu(bool load_test_percpu1)
{
	struct btf *vmlinux_btf, *module_btf;
	struct btf_type_tag_percpu *skel;
	int err;

	if (load_btfs(&vmlinux_btf, &module_btf, /*needs_vmlinux_tag=*/false))
		return;

	skel = btf_type_tag_percpu__open();
	if (!ASSERT_OK_PTR(skel, "btf_type_tag_percpu"))
		goto cleanup;

	bpf_program__set_autoload(skel->progs.test_percpu_load, false);
	bpf_program__set_autoload(skel->progs.test_percpu_helper, false);
	if (load_test_percpu1)
		bpf_program__set_autoload(skel->progs.test_percpu2, false);
	else
		bpf_program__set_autoload(skel->progs.test_percpu1, false);

	err = btf_type_tag_percpu__load(skel);
	ASSERT_ERR(err, "btf_type_tag_percpu");

	btf_type_tag_percpu__destroy(skel);

cleanup:
	btf__free(module_btf);
	btf__free(vmlinux_btf);
}

static void test_btf_type_tag_vmlinux_percpu(bool load_test)
{
	struct btf_type_tag_percpu *skel;
	struct btf *vmlinux_btf = NULL;
	int err;

	if (load_btfs(&vmlinux_btf, NULL, /*needs_vmlinux_tag=*/true))
		return;

	skel = btf_type_tag_percpu__open();
	if (!ASSERT_OK_PTR(skel, "btf_type_tag_percpu"))
		goto cleanup;

	bpf_program__set_autoload(skel->progs.test_percpu2, false);
	bpf_program__set_autoload(skel->progs.test_percpu1, false);
	if (load_test) {
		bpf_program__set_autoload(skel->progs.test_percpu_helper, false);

		err = btf_type_tag_percpu__load(skel);
		ASSERT_ERR(err, "btf_type_tag_percpu_load");
	} else {
		bpf_program__set_autoload(skel->progs.test_percpu_load, false);

		err = btf_type_tag_percpu__load(skel);
		ASSERT_OK(err, "btf_type_tag_percpu_helper");
	}

	btf_type_tag_percpu__destroy(skel);

cleanup:
	btf__free(vmlinux_btf);
}

void test_btf_tag(void)
{
	if (test__start_subtest("btf_decl_tag"))
		test_btf_decl_tag();
	if (test__start_subtest("btf_type_tag"))
		test_btf_type_tag();

	if (test__start_subtest("btf_type_tag_user_mod1"))
		test_btf_type_tag_mod_user(true);
	if (test__start_subtest("btf_type_tag_user_mod2"))
		test_btf_type_tag_mod_user(false);
	if (test__start_subtest("btf_type_tag_sys_user_vmlinux"))
		test_btf_type_tag_vmlinux_user();

	if (test__start_subtest("btf_type_tag_percpu_mod1"))
		test_btf_type_tag_mod_percpu(true);
	if (test__start_subtest("btf_type_tag_percpu_mod2"))
		test_btf_type_tag_mod_percpu(false);
	if (test__start_subtest("btf_type_tag_percpu_vmlinux_load"))
		test_btf_type_tag_vmlinux_percpu(true);
	if (test__start_subtest("btf_type_tag_percpu_vmlinux_helper"))
		test_btf_type_tag_vmlinux_percpu(false);
}