1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_attach_probe.skel.h"
4 
5 ssize_t get_base_addr() {
6 	size_t start, offset;
7 	char buf[256];
8 	FILE *f;
9 
10 	f = fopen("/proc/self/maps", "r");
11 	if (!f)
12 		return -errno;
13 
14 	while (fscanf(f, "%zx-%*x %s %zx %*[^\n]\n",
15 		      &start, buf, &offset) == 3) {
16 		if (strcmp(buf, "r-xp") == 0) {
17 			fclose(f);
18 			return start - offset;
19 		}
20 	}
21 
22 	fclose(f);
23 	return -EINVAL;
24 }
25 
26 void test_attach_probe(void)
27 {
28 	int duration = 0;
29 	struct bpf_link *kprobe_link, *kretprobe_link;
30 	struct bpf_link *uprobe_link, *uretprobe_link;
31 	struct test_attach_probe* skel;
32 	size_t uprobe_offset;
33 	ssize_t base_addr;
34 
35 	base_addr = get_base_addr();
36 	if (CHECK(base_addr < 0, "get_base_addr",
37 		  "failed to find base addr: %zd", base_addr))
38 		return;
39 	uprobe_offset = (size_t)&get_base_addr - base_addr;
40 
41 	skel = test_attach_probe__open_and_load();
42 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
43 		return;
44 	if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n"))
45 		goto cleanup;
46 
47 	kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
48 						 false /* retprobe */,
49 						 SYS_NANOSLEEP_KPROBE_NAME);
50 	if (CHECK(IS_ERR(kprobe_link), "attach_kprobe",
51 		  "err %ld\n", PTR_ERR(kprobe_link)))
52 		goto cleanup;
53 	skel->links.handle_kprobe = kprobe_link;
54 
55 	kretprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kretprobe,
56 						    true /* retprobe */,
57 						    SYS_NANOSLEEP_KPROBE_NAME);
58 	if (CHECK(IS_ERR(kretprobe_link), "attach_kretprobe",
59 		  "err %ld\n", PTR_ERR(kretprobe_link)))
60 		goto cleanup;
61 	skel->links.handle_kretprobe = kretprobe_link;
62 
63 	uprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uprobe,
64 						 false /* retprobe */,
65 						 0 /* self pid */,
66 						 "/proc/self/exe",
67 						 uprobe_offset);
68 	if (CHECK(IS_ERR(uprobe_link), "attach_uprobe",
69 		  "err %ld\n", PTR_ERR(uprobe_link)))
70 		goto cleanup;
71 	skel->links.handle_uprobe = uprobe_link;
72 
73 	uretprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uretprobe,
74 						    true /* retprobe */,
75 						    -1 /* any pid */,
76 						    "/proc/self/exe",
77 						    uprobe_offset);
78 	if (CHECK(IS_ERR(uretprobe_link), "attach_uretprobe",
79 		  "err %ld\n", PTR_ERR(uretprobe_link)))
80 		goto cleanup;
81 	skel->links.handle_uretprobe = uretprobe_link;
82 
83 	/* trigger & validate kprobe && kretprobe */
84 	usleep(1);
85 
86 	if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res",
87 		  "wrong kprobe res: %d\n", skel->bss->kprobe_res))
88 		goto cleanup;
89 	if (CHECK(skel->bss->kretprobe_res != 2, "check_kretprobe_res",
90 		  "wrong kretprobe res: %d\n", skel->bss->kretprobe_res))
91 		goto cleanup;
92 
93 	/* trigger & validate uprobe & uretprobe */
94 	get_base_addr();
95 
96 	if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res",
97 		  "wrong uprobe res: %d\n", skel->bss->uprobe_res))
98 		goto cleanup;
99 	if (CHECK(skel->bss->uretprobe_res != 4, "check_uretprobe_res",
100 		  "wrong uretprobe res: %d\n", skel->bss->uretprobe_res))
101 		goto cleanup;
102 
103 cleanup:
104 	test_attach_probe__destroy(skel);
105 }
106