1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_attach_probe.skel.h"
4 
5 /* this is how USDT semaphore is actually defined, except volatile modifier */
6 volatile unsigned short uprobe_ref_ctr __attribute__((unused)) __attribute((section(".probes")));
7 
8 /* attach point */
9 static void method(void) {
10 	return ;
11 }
12 
13 void test_attach_probe(void)
14 {
15 	DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
16 	int duration = 0;
17 	struct bpf_link *kprobe_link, *kretprobe_link;
18 	struct bpf_link *uprobe_link, *uretprobe_link;
19 	struct test_attach_probe* skel;
20 	size_t uprobe_offset;
21 	ssize_t base_addr, ref_ctr_offset;
22 	bool legacy;
23 
24 	/* Check if new-style kprobe/uprobe API is supported.
25 	 * Kernels that support new FD-based kprobe and uprobe BPF attachment
26 	 * through perf_event_open() syscall expose
27 	 * /sys/bus/event_source/devices/kprobe/type and
28 	 * /sys/bus/event_source/devices/uprobe/type files, respectively. They
29 	 * contain magic numbers that are passed as "type" field of
30 	 * perf_event_attr. Lack of such file in the system indicates legacy
31 	 * kernel with old-style kprobe/uprobe attach interface through
32 	 * creating per-probe event through tracefs. For such cases
33 	 * ref_ctr_offset feature is not supported, so we don't test it.
34 	 */
35 	legacy = access("/sys/bus/event_source/devices/kprobe/type", F_OK) != 0;
36 
37 	base_addr = get_base_addr();
38 	if (CHECK(base_addr < 0, "get_base_addr",
39 		  "failed to find base addr: %zd", base_addr))
40 		return;
41 	uprobe_offset = get_uprobe_offset(&method, base_addr);
42 
43 	ref_ctr_offset = get_rel_offset((uintptr_t)&uprobe_ref_ctr);
44 	if (!ASSERT_GE(ref_ctr_offset, 0, "ref_ctr_offset"))
45 		return;
46 
47 	skel = test_attach_probe__open_and_load();
48 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
49 		return;
50 	if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n"))
51 		goto cleanup;
52 
53 	kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
54 						 false /* retprobe */,
55 						 SYS_NANOSLEEP_KPROBE_NAME);
56 	if (!ASSERT_OK_PTR(kprobe_link, "attach_kprobe"))
57 		goto cleanup;
58 	skel->links.handle_kprobe = kprobe_link;
59 
60 	kretprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kretprobe,
61 						    true /* retprobe */,
62 						    SYS_NANOSLEEP_KPROBE_NAME);
63 	if (!ASSERT_OK_PTR(kretprobe_link, "attach_kretprobe"))
64 		goto cleanup;
65 	skel->links.handle_kretprobe = kretprobe_link;
66 
67 	if (!legacy)
68 		ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_before");
69 
70 	uprobe_opts.retprobe = false;
71 	uprobe_opts.ref_ctr_offset = legacy ? 0 : ref_ctr_offset;
72 	uprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe,
73 						      0 /* self pid */,
74 						      "/proc/self/exe",
75 						      uprobe_offset,
76 						      &uprobe_opts);
77 	if (!ASSERT_OK_PTR(uprobe_link, "attach_uprobe"))
78 		goto cleanup;
79 	skel->links.handle_uprobe = uprobe_link;
80 
81 	if (!legacy)
82 		ASSERT_GT(uprobe_ref_ctr, 0, "uprobe_ref_ctr_after");
83 
84 	/* if uprobe uses ref_ctr, uretprobe has to use ref_ctr as well */
85 	uprobe_opts.retprobe = true;
86 	uprobe_opts.ref_ctr_offset = legacy ? 0 : ref_ctr_offset;
87 	uretprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe,
88 							 -1 /* any pid */,
89 							 "/proc/self/exe",
90 							 uprobe_offset, &uprobe_opts);
91 	if (!ASSERT_OK_PTR(uretprobe_link, "attach_uretprobe"))
92 		goto cleanup;
93 	skel->links.handle_uretprobe = uretprobe_link;
94 
95 	/* trigger & validate kprobe && kretprobe */
96 	usleep(1);
97 
98 	if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res",
99 		  "wrong kprobe res: %d\n", skel->bss->kprobe_res))
100 		goto cleanup;
101 	if (CHECK(skel->bss->kretprobe_res != 2, "check_kretprobe_res",
102 		  "wrong kretprobe res: %d\n", skel->bss->kretprobe_res))
103 		goto cleanup;
104 
105 	/* trigger & validate uprobe & uretprobe */
106 	method();
107 
108 	if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res",
109 		  "wrong uprobe res: %d\n", skel->bss->uprobe_res))
110 		goto cleanup;
111 	if (CHECK(skel->bss->uretprobe_res != 4, "check_uretprobe_res",
112 		  "wrong uretprobe res: %d\n", skel->bss->uretprobe_res))
113 		goto cleanup;
114 
115 cleanup:
116 	test_attach_probe__destroy(skel);
117 	ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_cleanup");
118 }
119