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