1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 void test_stacktrace_build_id(void)
5 {
6 	int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
7 	const char *prog_name = "tracepoint/random/urandom_read";
8 	const char *file = "./test_stacktrace_build_id.o";
9 	int err, prog_fd, stack_trace_len;
10 	__u32 key, previous_key, val, duration = 0;
11 	struct bpf_program *prog;
12 	struct bpf_object *obj;
13 	struct bpf_link *link = NULL;
14 	char buf[256];
15 	int i, j;
16 	struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH];
17 	int build_id_matches = 0;
18 	int retry = 1;
19 
20 retry:
21 	err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
22 	if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
23 		return;
24 
25 	prog = bpf_object__find_program_by_title(obj, prog_name);
26 	if (CHECK(!prog, "find_prog", "prog '%s' not found\n", prog_name))
27 		goto close_prog;
28 
29 	link = bpf_program__attach_tracepoint(prog, "random", "urandom_read");
30 	if (CHECK(IS_ERR(link), "attach_tp", "err %ld\n", PTR_ERR(link)))
31 		goto close_prog;
32 
33 	/* find map fds */
34 	control_map_fd = bpf_find_map(__func__, obj, "control_map");
35 	if (CHECK(control_map_fd < 0, "bpf_find_map control_map",
36 		  "err %d errno %d\n", err, errno))
37 		goto disable_pmu;
38 
39 	stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap");
40 	if (CHECK(stackid_hmap_fd < 0, "bpf_find_map stackid_hmap",
41 		  "err %d errno %d\n", err, errno))
42 		goto disable_pmu;
43 
44 	stackmap_fd = bpf_find_map(__func__, obj, "stackmap");
45 	if (CHECK(stackmap_fd < 0, "bpf_find_map stackmap", "err %d errno %d\n",
46 		  err, errno))
47 		goto disable_pmu;
48 
49 	stack_amap_fd = bpf_find_map(__func__, obj, "stack_amap");
50 	if (CHECK(stack_amap_fd < 0, "bpf_find_map stack_amap",
51 		  "err %d errno %d\n", err, errno))
52 		goto disable_pmu;
53 
54 	assert(system("dd if=/dev/urandom of=/dev/zero count=4 2> /dev/null")
55 	       == 0);
56 	assert(system("./urandom_read") == 0);
57 	/* disable stack trace collection */
58 	key = 0;
59 	val = 1;
60 	bpf_map_update_elem(control_map_fd, &key, &val, 0);
61 
62 	/* for every element in stackid_hmap, we can find a corresponding one
63 	 * in stackmap, and vise versa.
64 	 */
65 	err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
66 	if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
67 		  "err %d errno %d\n", err, errno))
68 		goto disable_pmu;
69 
70 	err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
71 	if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
72 		  "err %d errno %d\n", err, errno))
73 		goto disable_pmu;
74 
75 	err = extract_build_id(buf, 256);
76 
77 	if (CHECK(err, "get build_id with readelf",
78 		  "err %d errno %d\n", err, errno))
79 		goto disable_pmu;
80 
81 	err = bpf_map_get_next_key(stackmap_fd, NULL, &key);
82 	if (CHECK(err, "get_next_key from stackmap",
83 		  "err %d, errno %d\n", err, errno))
84 		goto disable_pmu;
85 
86 	do {
87 		char build_id[64];
88 
89 		err = bpf_map_lookup_elem(stackmap_fd, &key, id_offs);
90 		if (CHECK(err, "lookup_elem from stackmap",
91 			  "err %d, errno %d\n", err, errno))
92 			goto disable_pmu;
93 		for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i)
94 			if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID &&
95 			    id_offs[i].offset != 0) {
96 				for (j = 0; j < 20; ++j)
97 					sprintf(build_id + 2 * j, "%02x",
98 						id_offs[i].build_id[j] & 0xff);
99 				if (strstr(buf, build_id) != NULL)
100 					build_id_matches = 1;
101 			}
102 		previous_key = key;
103 	} while (bpf_map_get_next_key(stackmap_fd, &previous_key, &key) == 0);
104 
105 	/* stack_map_get_build_id_offset() is racy and sometimes can return
106 	 * BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID;
107 	 * try it one more time.
108 	 */
109 	if (build_id_matches < 1 && retry--) {
110 		bpf_link__destroy(link);
111 		bpf_object__close(obj);
112 		printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
113 		       __func__);
114 		goto retry;
115 	}
116 
117 	if (CHECK(build_id_matches < 1, "build id match",
118 		  "Didn't find expected build ID from the map\n"))
119 		goto disable_pmu;
120 
121 	stack_trace_len = PERF_MAX_STACK_DEPTH
122 		* sizeof(struct bpf_stack_build_id);
123 	err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
124 	CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
125 	      "err %d errno %d\n", err, errno);
126 
127 disable_pmu:
128 	bpf_link__destroy(link);
129 
130 close_prog:
131 	bpf_object__close(obj);
132 }
133