summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/module_attach.c
blob: 92c336333fcb182ea6e41c8375171b6a7bac4b05 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */

#include <test_progs.h>
#include <stdbool.h>
#include "test_module_attach.skel.h"
#include "testing_helpers.h"

static const char * const read_tests[] = {
	"handle_raw_tp",
	"handle_tp_btf",
	"handle_fentry",
	"handle_fentry_explicit",
	"handle_fmod_ret",
};

static const char * const detach_tests[] = {
	"handle_fentry",
	"handle_fexit",
	"kprobe_multi",
};

static const int READ_SZ = 456;
static const int WRITE_SZ = 457;

static int trigger_module_test_writable(int *val)
{
	int fd, err;
	char buf[65];
	ssize_t rd;

	fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
	err = -errno;
	if (!ASSERT_GE(fd, 0, "testmode_file_open"))
		return err;

	rd = read(fd, buf, sizeof(buf) - 1);
	err = -errno;
	if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) {
		close(fd);
		return err;
	}

	buf[rd] = '\0';
	*val = strtol(buf, NULL, 0);
	close(fd);

	return 0;
}

static void test_module_attach_prog(const char *prog_name, int sz,
				    const char *attach_target, int ret)
{
	struct test_module_attach *skel;
	struct bpf_program *prog;
	int err;

	skel = test_module_attach__open();
	if (!ASSERT_OK_PTR(skel, "module_attach open"))
		return;

	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
	if (!ASSERT_OK_PTR(prog, "module_attach find_program"))
		goto cleanup;
	bpf_program__set_autoload(prog, true);

	if (attach_target) {
		err = bpf_program__set_attach_target(prog, 0, attach_target);
		if (!ASSERT_OK(err, attach_target))
			goto cleanup;
	}

	err = test_module_attach__load(skel);
	if (!ASSERT_OK(err, "module_attach load"))
		goto cleanup;

	err = test_module_attach__attach(skel);
	if (!ASSERT_OK(err, "module_attach attach"))
		goto cleanup;

	if (sz) {
		/* trigger both read and write though each test uses only one */
		ASSERT_OK(trigger_module_test_read(sz), "trigger_read");
		ASSERT_OK(trigger_module_test_write(sz), "trigger_write");

		ASSERT_EQ(skel->bss->sz, sz, prog_name);
	}

	if (ret)
		ASSERT_EQ(skel->bss->retval, ret, "ret");
cleanup:
	test_module_attach__destroy(skel);
}

static void test_module_attach_writable(void)
{
	struct test_module_attach__bss *bss;
	struct test_module_attach *skel;
	int writable_val = 0;
	int err;

	skel = test_module_attach__open();
	if (!ASSERT_OK_PTR(skel, "module_attach open"))
		return;

	bpf_program__set_autoload(skel->progs.handle_raw_tp_writable_bare, true);

	err = test_module_attach__load(skel);
	if (!ASSERT_OK(err, "module_attach load"))
		goto cleanup;

	bss = skel->bss;

	err = test_module_attach__attach(skel);
	if (!ASSERT_OK(err, "module_attach attach"))
		goto cleanup;

	bss->raw_tp_writable_bare_early_ret = true;
	bss->raw_tp_writable_bare_out_val = 0xf1f2f3f4;
	ASSERT_OK(trigger_module_test_writable(&writable_val),
		  "trigger_writable");
	ASSERT_EQ(bss->raw_tp_writable_bare_in_val, 1024, "writable_test_in");
	ASSERT_EQ(bss->raw_tp_writable_bare_out_val, writable_val,
		  "writable_test_out");
cleanup:
	test_module_attach__destroy(skel);
}

static void test_module_attach_detach(const char *prog_name)
{
	struct test_module_attach *skel;
	struct bpf_program *prog;
	struct bpf_link *link;
	int err;

	skel = test_module_attach__open();
	if (!ASSERT_OK_PTR(skel, "module_attach open"))
		return;

	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
	if (!ASSERT_OK_PTR(prog, "module_attach find_program"))
		goto cleanup;
	bpf_program__set_autoload(prog, true);

	err = test_module_attach__load(skel);
	if (!ASSERT_OK(err, "module_attach load"))
		goto cleanup;

	/* attach and make sure it gets module reference */
	link = bpf_program__attach(prog);
	if (!ASSERT_OK_PTR(link, "module_attach attach"))
		goto cleanup;

	ASSERT_ERR(try_unload_module("bpf_testmod", 1, false), "try_unload_module");
	bpf_link__destroy(link);
cleanup:
	test_module_attach__destroy(skel);
}

void test_module_attach(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(read_tests); i++) {
		if (!test__start_subtest(read_tests[i]))
			continue;
		test_module_attach_prog(read_tests[i], READ_SZ, NULL, 0);
	}
	if (test__start_subtest("handle_raw_tp_bare"))
		test_module_attach_prog("handle_raw_tp_bare", WRITE_SZ, NULL, 0);
	if (test__start_subtest("handle_raw_tp_writable_bare"))
		test_module_attach_writable();
	if (test__start_subtest("handle_fentry_manual")) {
		test_module_attach_prog("handle_fentry_manual", READ_SZ,
					"bpf_testmod_test_read", 0);
	}
	if (test__start_subtest("handle_fentry_explicit_manual")) {
		test_module_attach_prog("handle_fentry_explicit_manual",
					READ_SZ,
					"bpf_testmod:bpf_testmod_test_read", 0);
	}
	if (test__start_subtest("handle_fexit"))
		test_module_attach_prog("handle_fexit", READ_SZ, NULL, -EIO);
	if (test__start_subtest("handle_fexit_ret"))
		test_module_attach_prog("handle_fexit_ret", 0, NULL, 0);
	for (i = 0; i < ARRAY_SIZE(detach_tests); i++) {
		char test_name[50];

		snprintf(test_name, sizeof(test_name), "%s_detach", detach_tests[i]);
		if (!test__start_subtest(test_name))
			continue;
		test_module_attach_detach(detach_tests[i]);
	}
}