1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "get_func_ip_test.skel.h"
4 #include "get_func_ip_uprobe_test.skel.h"
5 
uprobe_trigger(void)6 static noinline void uprobe_trigger(void)
7 {
8 }
9 
test_function_entry(void)10 static void test_function_entry(void)
11 {
12 	struct get_func_ip_test *skel = NULL;
13 	int err, prog_fd;
14 	LIBBPF_OPTS(bpf_test_run_opts, topts);
15 
16 	skel = get_func_ip_test__open();
17 	if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open"))
18 		return;
19 
20 	err = get_func_ip_test__load(skel);
21 	if (!ASSERT_OK(err, "get_func_ip_test__load"))
22 		goto cleanup;
23 
24 	err = get_func_ip_test__attach(skel);
25 	if (!ASSERT_OK(err, "get_func_ip_test__attach"))
26 		goto cleanup;
27 
28 	skel->bss->uprobe_trigger = (unsigned long) uprobe_trigger;
29 
30 	prog_fd = bpf_program__fd(skel->progs.test1);
31 	err = bpf_prog_test_run_opts(prog_fd, &topts);
32 	ASSERT_OK(err, "test_run");
33 	ASSERT_EQ(topts.retval, 0, "test_run");
34 
35 	prog_fd = bpf_program__fd(skel->progs.test5);
36 	err = bpf_prog_test_run_opts(prog_fd, &topts);
37 
38 	ASSERT_OK(err, "test_run");
39 
40 	uprobe_trigger();
41 
42 	ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
43 	ASSERT_EQ(skel->bss->test2_result, 1, "test2_result");
44 	ASSERT_EQ(skel->bss->test3_result, 1, "test3_result");
45 	ASSERT_EQ(skel->bss->test4_result, 1, "test4_result");
46 	ASSERT_EQ(skel->bss->test5_result, 1, "test5_result");
47 	ASSERT_EQ(skel->bss->test7_result, 1, "test7_result");
48 	ASSERT_EQ(skel->bss->test8_result, 1, "test8_result");
49 
50 cleanup:
51 	get_func_ip_test__destroy(skel);
52 }
53 
54 #ifdef __x86_64__
55 extern void uprobe_trigger_body(void);
56 asm(
57 ".globl uprobe_trigger_body\n"
58 ".type uprobe_trigger_body, @function\n"
59 "uprobe_trigger_body:\n"
60 "	nop\n"
61 "	ret\n"
62 );
63 
test_function_body_kprobe(void)64 static void test_function_body_kprobe(void)
65 {
66 	struct get_func_ip_test *skel = NULL;
67 	LIBBPF_OPTS(bpf_test_run_opts, topts);
68 	LIBBPF_OPTS(bpf_kprobe_opts, kopts);
69 	struct bpf_link *link6 = NULL;
70 	int err, prog_fd;
71 
72 	skel = get_func_ip_test__open();
73 	if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open"))
74 		return;
75 
76 	/* test6 is x86_64 specific and is disabled by default,
77 	 * enable it for body test.
78 	 */
79 	bpf_program__set_autoload(skel->progs.test6, true);
80 
81 	err = get_func_ip_test__load(skel);
82 	if (!ASSERT_OK(err, "get_func_ip_test__load"))
83 		goto cleanup;
84 
85 	kopts.offset = skel->kconfig->CONFIG_X86_KERNEL_IBT ? 9 : 5;
86 
87 	link6 = bpf_program__attach_kprobe_opts(skel->progs.test6, "bpf_fentry_test6", &kopts);
88 	if (!ASSERT_OK_PTR(link6, "link6"))
89 		goto cleanup;
90 
91 	prog_fd = bpf_program__fd(skel->progs.test1);
92 	err = bpf_prog_test_run_opts(prog_fd, &topts);
93 	ASSERT_OK(err, "test_run");
94 	ASSERT_EQ(topts.retval, 0, "test_run");
95 
96 	ASSERT_EQ(skel->bss->test6_result, 1, "test6_result");
97 
98 cleanup:
99 	bpf_link__destroy(link6);
100 	get_func_ip_test__destroy(skel);
101 }
102 
test_function_body_uprobe(void)103 static void test_function_body_uprobe(void)
104 {
105 	struct get_func_ip_uprobe_test *skel = NULL;
106 	int err;
107 
108 	skel = get_func_ip_uprobe_test__open_and_load();
109 	if (!ASSERT_OK_PTR(skel, "get_func_ip_uprobe_test__open_and_load"))
110 		return;
111 
112 	err = get_func_ip_uprobe_test__attach(skel);
113 	if (!ASSERT_OK(err, "get_func_ip_test__attach"))
114 		goto cleanup;
115 
116 	skel->bss->uprobe_trigger_body = (unsigned long) uprobe_trigger_body;
117 
118 	uprobe_trigger_body();
119 
120 	ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
121 
122 cleanup:
123 	get_func_ip_uprobe_test__destroy(skel);
124 }
125 
test_function_body(void)126 static void test_function_body(void)
127 {
128 	test_function_body_kprobe();
129 	test_function_body_uprobe();
130 }
131 #else
132 #define test_function_body()
133 #endif
134 
test_get_func_ip_test(void)135 void test_get_func_ip_test(void)
136 {
137 	test_function_entry();
138 	test_function_body();
139 }
140