xref: /openbmc/linux/tools/lib/bpf/bpf_prog_linfo.c (revision dfd4f649)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
3 
4 #include <string.h>
5 #include <stdlib.h>
6 #include <linux/err.h>
7 #include <linux/bpf.h>
8 #include "libbpf.h"
9 
10 #ifndef min
11 #define min(x, y) ((x) < (y) ? (x) : (y))
12 #endif
13 
14 struct bpf_prog_linfo {
15 	void *raw_linfo;
16 	void *raw_jited_linfo;
17 	__u32 *nr_jited_linfo_per_func;
18 	__u32 *jited_linfo_func_idx;
19 	__u32 nr_linfo;
20 	__u32 nr_jited_func;
21 	__u32 rec_size;
22 	__u32 jited_rec_size;
23 };
24 
25 static int dissect_jited_func(struct bpf_prog_linfo *prog_linfo,
26 			      const __u64 *ksym_func, const __u32 *ksym_len)
27 {
28 	__u32 nr_jited_func, nr_linfo;
29 	const void *raw_jited_linfo;
30 	const __u64 *jited_linfo;
31 	__u64 last_jited_linfo;
32 	/*
33 	 * Index to raw_jited_linfo:
34 	 *      i: Index for searching the next ksym_func
35 	 * prev_i: Index to the last found ksym_func
36 	 */
37 	__u32 i, prev_i;
38 	__u32 f; /* Index to ksym_func */
39 
40 	raw_jited_linfo = prog_linfo->raw_jited_linfo;
41 	jited_linfo = raw_jited_linfo;
42 	if (ksym_func[0] != *jited_linfo)
43 		goto errout;
44 
45 	prog_linfo->jited_linfo_func_idx[0] = 0;
46 	nr_jited_func = prog_linfo->nr_jited_func;
47 	nr_linfo = prog_linfo->nr_linfo;
48 
49 	for (prev_i = 0, i = 1, f = 1;
50 	     i < nr_linfo && f < nr_jited_func;
51 	     i++) {
52 		raw_jited_linfo += prog_linfo->jited_rec_size;
53 		last_jited_linfo = *jited_linfo;
54 		jited_linfo = raw_jited_linfo;
55 
56 		if (ksym_func[f] == *jited_linfo) {
57 			prog_linfo->jited_linfo_func_idx[f] = i;
58 
59 			/* Sanity check */
60 			if (last_jited_linfo - ksym_func[f - 1] + 1 >
61 			    ksym_len[f - 1])
62 				goto errout;
63 
64 			prog_linfo->nr_jited_linfo_per_func[f - 1] =
65 				i - prev_i;
66 			prev_i = i;
67 
68 			/*
69 			 * The ksym_func[f] is found in jited_linfo.
70 			 * Look for the next one.
71 			 */
72 			f++;
73 		} else if (*jited_linfo <= last_jited_linfo) {
74 			/* Ensure the addr is increasing _within_ a func */
75 			goto errout;
76 		}
77 	}
78 
79 	if (f != nr_jited_func)
80 		goto errout;
81 
82 	prog_linfo->nr_jited_linfo_per_func[nr_jited_func - 1] =
83 		nr_linfo - prev_i;
84 
85 	return 0;
86 
87 errout:
88 	return -EINVAL;
89 }
90 
91 void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo)
92 {
93 	if (!prog_linfo)
94 		return;
95 
96 	free(prog_linfo->raw_linfo);
97 	free(prog_linfo->raw_jited_linfo);
98 	free(prog_linfo->nr_jited_linfo_per_func);
99 	free(prog_linfo->jited_linfo_func_idx);
100 	free(prog_linfo);
101 }
102 
103 struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
104 {
105 	struct bpf_prog_linfo *prog_linfo;
106 	__u32 nr_linfo, nr_jited_func;
107 
108 	nr_linfo = info->nr_line_info;
109 
110 	if (!nr_linfo)
111 		return NULL;
112 
113 	/*
114 	 * The min size that bpf_prog_linfo has to access for
115 	 * searching purpose.
116 	 */
117 	if (info->line_info_rec_size <
118 	    offsetof(struct bpf_line_info, file_name_off))
119 		return NULL;
120 
121 	prog_linfo = calloc(1, sizeof(*prog_linfo));
122 	if (!prog_linfo)
123 		return NULL;
124 
125 	/* Copy xlated line_info */
126 	prog_linfo->nr_linfo = nr_linfo;
127 	prog_linfo->rec_size = info->line_info_rec_size;
128 	prog_linfo->raw_linfo = malloc(nr_linfo * prog_linfo->rec_size);
129 	if (!prog_linfo->raw_linfo)
130 		goto err_free;
131 	memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info,
132 	       nr_linfo * prog_linfo->rec_size);
133 
134 	nr_jited_func = info->nr_jited_ksyms;
135 	if (!nr_jited_func ||
136 	    !info->jited_line_info ||
137 	    info->nr_jited_line_info != nr_linfo ||
138 	    info->jited_line_info_rec_size < sizeof(__u64) ||
139 	    info->nr_jited_func_lens != nr_jited_func ||
140 	    !info->jited_ksyms ||
141 	    !info->jited_func_lens)
142 		/* Not enough info to provide jited_line_info */
143 		return prog_linfo;
144 
145 	/* Copy jited_line_info */
146 	prog_linfo->nr_jited_func = nr_jited_func;
147 	prog_linfo->jited_rec_size = info->jited_line_info_rec_size;
148 	prog_linfo->raw_jited_linfo = malloc(nr_linfo *
149 					     prog_linfo->jited_rec_size);
150 	if (!prog_linfo->raw_jited_linfo)
151 		goto err_free;
152 	memcpy(prog_linfo->raw_jited_linfo,
153 	       (void *)(long)info->jited_line_info,
154 	       nr_linfo * prog_linfo->jited_rec_size);
155 
156 	/* Number of jited_line_info per jited func */
157 	prog_linfo->nr_jited_linfo_per_func = malloc(nr_jited_func *
158 						     sizeof(__u32));
159 	if (!prog_linfo->nr_jited_linfo_per_func)
160 		goto err_free;
161 
162 	/*
163 	 * For each jited func,
164 	 * the start idx to the "linfo" and "jited_linfo" array,
165 	 */
166 	prog_linfo->jited_linfo_func_idx = malloc(nr_jited_func *
167 						  sizeof(__u32));
168 	if (!prog_linfo->jited_linfo_func_idx)
169 		goto err_free;
170 
171 	if (dissect_jited_func(prog_linfo,
172 			       (__u64 *)(long)info->jited_ksyms,
173 			       (__u32 *)(long)info->jited_func_lens))
174 		goto err_free;
175 
176 	return prog_linfo;
177 
178 err_free:
179 	bpf_prog_linfo__free(prog_linfo);
180 	return NULL;
181 }
182 
183 const struct bpf_line_info *
184 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
185 				__u64 addr, __u32 func_idx, __u32 nr_skip)
186 {
187 	__u32 jited_rec_size, rec_size, nr_linfo, start, i;
188 	const void *raw_jited_linfo, *raw_linfo;
189 	const __u64 *jited_linfo;
190 
191 	if (func_idx >= prog_linfo->nr_jited_func)
192 		return NULL;
193 
194 	nr_linfo = prog_linfo->nr_jited_linfo_per_func[func_idx];
195 	if (nr_skip >= nr_linfo)
196 		return NULL;
197 
198 	start = prog_linfo->jited_linfo_func_idx[func_idx] + nr_skip;
199 	jited_rec_size = prog_linfo->jited_rec_size;
200 	raw_jited_linfo = prog_linfo->raw_jited_linfo +
201 		(start * jited_rec_size);
202 	jited_linfo = raw_jited_linfo;
203 	if (addr < *jited_linfo)
204 		return NULL;
205 
206 	nr_linfo -= nr_skip;
207 	rec_size = prog_linfo->rec_size;
208 	raw_linfo = prog_linfo->raw_linfo + (start * rec_size);
209 	for (i = 0; i < nr_linfo; i++) {
210 		if (addr < *jited_linfo)
211 			break;
212 
213 		raw_linfo += rec_size;
214 		raw_jited_linfo += jited_rec_size;
215 		jited_linfo = raw_jited_linfo;
216 	}
217 
218 	return raw_linfo - rec_size;
219 }
220 
221 const struct bpf_line_info *
222 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
223 		      __u32 insn_off, __u32 nr_skip)
224 {
225 	const struct bpf_line_info *linfo;
226 	__u32 rec_size, nr_linfo, i;
227 	const void *raw_linfo;
228 
229 	nr_linfo = prog_linfo->nr_linfo;
230 	if (nr_skip >= nr_linfo)
231 		return NULL;
232 
233 	rec_size = prog_linfo->rec_size;
234 	raw_linfo = prog_linfo->raw_linfo + (nr_skip * rec_size);
235 	linfo = raw_linfo;
236 	if (insn_off < linfo->insn_off)
237 		return NULL;
238 
239 	nr_linfo -= nr_skip;
240 	for (i = 0; i < nr_linfo; i++) {
241 		if (insn_off < linfo->insn_off)
242 			break;
243 
244 		raw_linfo += rec_size;
245 		linfo = raw_linfo;
246 	}
247 
248 	return raw_linfo - rec_size;
249 }
250