xref: /linux/tools/testing/selftests/bpf/prog_tests/module_attach.c (revision 200323768787a0ee02e01c35c1aff13dc9d77dde)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 
4 #include <test_progs.h>
5 #include <stdbool.h>
6 #include "test_module_attach.skel.h"
7 
8 static int duration;
9 
10 static int trigger_module_test_writable(int *val)
11 {
12 	int fd, err;
13 	char buf[65];
14 	ssize_t rd;
15 
16 	fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
17 	err = -errno;
18 	if (!ASSERT_GE(fd, 0, "testmode_file_open"))
19 		return err;
20 
21 	rd = read(fd, buf, sizeof(buf) - 1);
22 	err = -errno;
23 	if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) {
24 		close(fd);
25 		return err;
26 	}
27 
28 	buf[rd] = '\0';
29 	*val = strtol(buf, NULL, 0);
30 	close(fd);
31 
32 	return 0;
33 }
34 
35 static int delete_module(const char *name, int flags)
36 {
37 	return syscall(__NR_delete_module, name, flags);
38 }
39 
40 void test_module_attach(void)
41 {
42 	const int READ_SZ = 456;
43 	const int WRITE_SZ = 457;
44 	struct test_module_attach* skel;
45 	struct test_module_attach__bss *bss;
46 	struct bpf_link *link;
47 	int err;
48 	int writable_val = 0;
49 
50 	skel = test_module_attach__open();
51 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
52 		return;
53 
54 	err = bpf_program__set_attach_target(skel->progs.handle_fentry_manual,
55 					     0, "bpf_testmod_test_read");
56 	ASSERT_OK(err, "set_attach_target");
57 
58 	err = test_module_attach__load(skel);
59 	if (CHECK(err, "skel_load", "failed to load skeleton\n"))
60 		return;
61 
62 	bss = skel->bss;
63 
64 	err = test_module_attach__attach(skel);
65 	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
66 		goto cleanup;
67 
68 	/* trigger tracepoint */
69 	ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read");
70 	ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write");
71 
72 	ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
73 	ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare");
74 	ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
75 	ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
76 	ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");
77 	ASSERT_EQ(bss->fexit_read_sz, READ_SZ, "fexit");
78 	ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet");
79 	ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret");
80 
81 	bss->raw_tp_writable_bare_early_ret = true;
82 	bss->raw_tp_writable_bare_out_val = 0xf1f2f3f4;
83 	ASSERT_OK(trigger_module_test_writable(&writable_val),
84 		  "trigger_writable");
85 	ASSERT_EQ(bss->raw_tp_writable_bare_in_val, 1024, "writable_test_in");
86 	ASSERT_EQ(bss->raw_tp_writable_bare_out_val, writable_val,
87 		  "writable_test_out");
88 
89 	test_module_attach__detach(skel);
90 
91 	/* attach fentry/fexit and make sure it get's module reference */
92 	link = bpf_program__attach(skel->progs.handle_fentry);
93 	if (!ASSERT_OK_PTR(link, "attach_fentry"))
94 		goto cleanup;
95 
96 	ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module");
97 	bpf_link__destroy(link);
98 
99 	link = bpf_program__attach(skel->progs.handle_fexit);
100 	if (!ASSERT_OK_PTR(link, "attach_fexit"))
101 		goto cleanup;
102 
103 	ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module");
104 	bpf_link__destroy(link);
105 
106 	link = bpf_program__attach(skel->progs.kprobe_multi);
107 	if (!ASSERT_OK_PTR(link, "attach_kprobe_multi"))
108 		goto cleanup;
109 
110 	ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module");
111 	bpf_link__destroy(link);
112 
113 cleanup:
114 	test_module_attach__destroy(skel);
115 }
116