1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "kprobe_multi.skel.h"
4 #include "trace_helpers.h"
5 #include "bpf/libbpf_internal.h"
6 
7 static struct ksyms *ksyms;
8 
kprobe_multi_testmod_check(struct kprobe_multi * skel)9 static void kprobe_multi_testmod_check(struct kprobe_multi *skel)
10 {
11 	ASSERT_EQ(skel->bss->kprobe_testmod_test1_result, 1, "kprobe_test1_result");
12 	ASSERT_EQ(skel->bss->kprobe_testmod_test2_result, 1, "kprobe_test2_result");
13 	ASSERT_EQ(skel->bss->kprobe_testmod_test3_result, 1, "kprobe_test3_result");
14 
15 	ASSERT_EQ(skel->bss->kretprobe_testmod_test1_result, 1, "kretprobe_test1_result");
16 	ASSERT_EQ(skel->bss->kretprobe_testmod_test2_result, 1, "kretprobe_test2_result");
17 	ASSERT_EQ(skel->bss->kretprobe_testmod_test3_result, 1, "kretprobe_test3_result");
18 }
19 
test_testmod_attach_api(struct bpf_kprobe_multi_opts * opts)20 static void test_testmod_attach_api(struct bpf_kprobe_multi_opts *opts)
21 {
22 	struct kprobe_multi *skel = NULL;
23 
24 	skel = kprobe_multi__open_and_load();
25 	if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
26 		return;
27 
28 	skel->bss->pid = getpid();
29 
30 	skel->links.test_kprobe_testmod = bpf_program__attach_kprobe_multi_opts(
31 						skel->progs.test_kprobe_testmod,
32 						NULL, opts);
33 	if (!skel->links.test_kprobe_testmod)
34 		goto cleanup;
35 
36 	opts->retprobe = true;
37 	skel->links.test_kretprobe_testmod = bpf_program__attach_kprobe_multi_opts(
38 						skel->progs.test_kretprobe_testmod,
39 						NULL, opts);
40 	if (!skel->links.test_kretprobe_testmod)
41 		goto cleanup;
42 
43 	ASSERT_OK(trigger_module_test_read(1), "trigger_read");
44 	kprobe_multi_testmod_check(skel);
45 
46 cleanup:
47 	kprobe_multi__destroy(skel);
48 }
49 
test_testmod_attach_api_addrs(void)50 static void test_testmod_attach_api_addrs(void)
51 {
52 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
53 	unsigned long long addrs[3];
54 
55 	addrs[0] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test1");
56 	ASSERT_NEQ(addrs[0], 0, "ksym_get_addr_local");
57 	addrs[1] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test2");
58 	ASSERT_NEQ(addrs[1], 0, "ksym_get_addr_local");
59 	addrs[2] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test3");
60 	ASSERT_NEQ(addrs[2], 0, "ksym_get_addr_local");
61 
62 	opts.addrs = (const unsigned long *) addrs;
63 	opts.cnt = ARRAY_SIZE(addrs);
64 
65 	test_testmod_attach_api(&opts);
66 }
67 
test_testmod_attach_api_syms(void)68 static void test_testmod_attach_api_syms(void)
69 {
70 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
71 	const char *syms[3] = {
72 		"bpf_testmod_fentry_test1",
73 		"bpf_testmod_fentry_test2",
74 		"bpf_testmod_fentry_test3",
75 	};
76 
77 	opts.syms = syms;
78 	opts.cnt = ARRAY_SIZE(syms);
79 	test_testmod_attach_api(&opts);
80 }
81 
serial_test_kprobe_multi_testmod_test(void)82 void serial_test_kprobe_multi_testmod_test(void)
83 {
84 	ksyms = load_kallsyms_local();
85 	if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_local"))
86 		return;
87 
88 	if (test__start_subtest("testmod_attach_api_syms"))
89 		test_testmod_attach_api_syms();
90 
91 	if (test__start_subtest("testmod_attach_api_addrs"))
92 		test_testmod_attach_api_addrs();
93 
94 	free_kallsyms_local(ksyms);
95 }
96