xref: /linux/tools/bpf/bpf_jit_disasm.c (revision d9104cec3e8fe4b458b74709853231385779001f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Minimal BPF JIT image disassembler
4  *
5  * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
6  * debugging or verification purposes.
7  *
8  * To get the disassembly of the JIT code, do the following:
9  *
10  *  1) `echo 2 > /proc/sys/net/core/bpf_jit_enable`
11  *  2) Load a BPF filter (e.g. `tcpdump -p -n -s 0 -i eth1 host 192.168.20.0/24`)
12  *  3) Run e.g. `bpf_jit_disasm -o` to read out the last JIT code
13  *
14  * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
15  */
16 
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <assert.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <bfd.h>
24 #include <dis-asm.h>
25 #include <regex.h>
26 #include <fcntl.h>
27 #include <sys/klog.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <limits.h>
31 #include <tools/dis-asm-compat.h>
32 
33 #define CMD_ACTION_SIZE_BUFFER		10
34 #define CMD_ACTION_READ_ALL		3
35 
get_exec_path(char * tpath,size_t size)36 static void get_exec_path(char *tpath, size_t size)
37 {
38 	char *path;
39 	ssize_t len;
40 
41 	snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
42 	tpath[size - 1] = 0;
43 
44 	path = strdup(tpath);
45 	assert(path);
46 
47 	len = readlink(path, tpath, size);
48 	if (len < 0)
49 		len = 0;
50 	tpath[len] = 0;
51 
52 	free(path);
53 }
54 
get_asm_insns(uint8_t * image,size_t len,int opcodes)55 static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
56 {
57 	int count, i, pc = 0;
58 	char tpath[PATH_MAX];
59 	struct disassemble_info info;
60 	disassembler_ftype disassemble;
61 	bfd *bfdf;
62 
63 	memset(tpath, 0, sizeof(tpath));
64 	get_exec_path(tpath, sizeof(tpath));
65 
66 	bfdf = bfd_openr(tpath, NULL);
67 	assert(bfdf);
68 	assert(bfd_check_format(bfdf, bfd_object));
69 
70 	init_disassemble_info_compat(&info, stdout,
71 				     (fprintf_ftype) fprintf,
72 				     fprintf_styled);
73 	info.arch = bfd_get_arch(bfdf);
74 	info.mach = bfd_get_mach(bfdf);
75 	info.buffer = image;
76 	info.buffer_length = len;
77 
78 	disassemble_init_for_target(&info);
79 
80 #ifdef DISASM_FOUR_ARGS_SIGNATURE
81 	disassemble = disassembler(info.arch,
82 				   bfd_big_endian(bfdf),
83 				   info.mach,
84 				   bfdf);
85 #else
86 	disassemble = disassembler(bfdf);
87 #endif
88 	assert(disassemble);
89 
90 	do {
91 		printf("%4x:\t", pc);
92 
93 		count = disassemble(pc, &info);
94 
95 		if (opcodes) {
96 			printf("\n\t");
97 			for (i = 0; i < count; ++i)
98 				printf("%02x ", (uint8_t) image[pc + i]);
99 		}
100 		printf("\n");
101 
102 		pc += count;
103 	} while(count > 0 && pc < len);
104 
105 	bfd_close(bfdf);
106 }
107 
get_klog_buff(unsigned int * klen)108 static char *get_klog_buff(unsigned int *klen)
109 {
110 	int ret, len;
111 	char *buff;
112 
113 	len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
114 	if (len < 0)
115 		return NULL;
116 
117 	buff = malloc(len);
118 	if (!buff)
119 		return NULL;
120 
121 	ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
122 	if (ret < 0) {
123 		free(buff);
124 		return NULL;
125 	}
126 
127 	*klen = ret;
128 	return buff;
129 }
130 
get_flog_buff(const char * file,unsigned int * klen)131 static char *get_flog_buff(const char *file, unsigned int *klen)
132 {
133 	int fd, ret, len;
134 	struct stat fi;
135 	char *buff;
136 
137 	fd = open(file, O_RDONLY);
138 	if (fd < 0)
139 		return NULL;
140 
141 	ret = fstat(fd, &fi);
142 	if (ret < 0 || !S_ISREG(fi.st_mode))
143 		goto out;
144 
145 	len = fi.st_size + 1;
146 	buff = malloc(len);
147 	if (!buff)
148 		goto out;
149 
150 	memset(buff, 0, len);
151 	ret = read(fd, buff, len - 1);
152 	if (ret <= 0)
153 		goto out_free;
154 
155 	close(fd);
156 	*klen = ret;
157 	return buff;
158 out_free:
159 	free(buff);
160 out:
161 	close(fd);
162 	return NULL;
163 }
164 
get_log_buff(const char * file,unsigned int * klen)165 static char *get_log_buff(const char *file, unsigned int *klen)
166 {
167 	return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
168 }
169 
put_log_buff(char * buff)170 static void put_log_buff(char *buff)
171 {
172 	free(buff);
173 }
174 
get_last_jit_image(char * haystack,size_t hlen,unsigned int * ilen)175 static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
176 				   unsigned int *ilen)
177 {
178 	char *ptr, *pptr, *tmp;
179 	off_t off = 0;
180 	unsigned int proglen;
181 	int ret, flen, pass, ulen = 0;
182 	regmatch_t pmatch[1];
183 	unsigned long base;
184 	regex_t regex;
185 	uint8_t *image;
186 
187 	if (hlen == 0)
188 		return NULL;
189 
190 	ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
191 		      "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
192 	assert(ret == 0);
193 
194 	ptr = haystack;
195 	memset(pmatch, 0, sizeof(pmatch));
196 
197 	while (1) {
198 		ret = regexec(&regex, ptr, 1, pmatch, 0);
199 		if (ret == 0) {
200 			ptr += pmatch[0].rm_eo;
201 			off += pmatch[0].rm_eo;
202 			assert(off < hlen);
203 		} else
204 			break;
205 	}
206 
207 	ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
208 	ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx",
209 		     &flen, &proglen, &pass, &base);
210 	if (ret != 4) {
211 		regfree(&regex);
212 		return NULL;
213 	}
214 	if (proglen > 1000000) {
215 		printf("proglen of %u too big, stopping\n", proglen);
216 		return NULL;
217 	}
218 
219 	image = malloc(proglen);
220 	if (!image) {
221 		printf("Out of memory\n");
222 		return NULL;
223 	}
224 	memset(image, 0, proglen);
225 
226 	tmp = ptr = haystack + off;
227 	while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
228 		tmp = NULL;
229 		if (!strstr(ptr, "JIT code"))
230 			continue;
231 		pptr = ptr;
232 		while ((ptr = strstr(pptr, ":")))
233 			pptr = ptr + 1;
234 		ptr = pptr;
235 		do {
236 			image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
237 			if (ptr == pptr) {
238 				ulen--;
239 				break;
240 			}
241 			if (ulen >= proglen)
242 				break;
243 			ptr = pptr;
244 		} while (1);
245 	}
246 
247 	assert(ulen == proglen);
248 	printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
249 	       proglen, pass, flen);
250 	printf("%lx + <x>:\n", base);
251 
252 	regfree(&regex);
253 	*ilen = ulen;
254 	return image;
255 }
256 
usage(void)257 static void usage(void)
258 {
259 	printf("Usage: bpf_jit_disasm [...]\n");
260 	printf("       -o          Also display related opcodes (default: off).\n");
261 	printf("       -O <file>   Write binary image of code to file, don't disassemble to stdout.\n");
262 	printf("       -f <file>   Read last image dump from file or stdin (default: klog).\n");
263 	printf("       -h          Display this help.\n");
264 }
265 
main(int argc,char ** argv)266 int main(int argc, char **argv)
267 {
268 	unsigned int len, klen, opt, opcodes = 0;
269 	char *kbuff, *file = NULL;
270 	char *ofile = NULL;
271 	int ofd;
272 	ssize_t nr;
273 	uint8_t *pos;
274 	uint8_t *image = NULL;
275 
276 	while ((opt = getopt(argc, argv, "of:O:")) != -1) {
277 		switch (opt) {
278 		case 'o':
279 			opcodes = 1;
280 			break;
281 		case 'O':
282 			ofile = optarg;
283 			break;
284 		case 'f':
285 			file = optarg;
286 			break;
287 		default:
288 			usage();
289 			return -1;
290 		}
291 	}
292 
293 	bfd_init();
294 
295 	kbuff = get_log_buff(file, &klen);
296 	if (!kbuff) {
297 		fprintf(stderr, "Could not retrieve log buffer!\n");
298 		return -1;
299 	}
300 
301 	image = get_last_jit_image(kbuff, klen, &len);
302 	if (!image) {
303 		fprintf(stderr, "No JIT image found!\n");
304 		goto done;
305 	}
306 	if (!ofile) {
307 		get_asm_insns(image, len, opcodes);
308 		goto done;
309 	}
310 
311 	ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
312 	if (ofd < 0) {
313 		fprintf(stderr, "Could not open file %s for writing: ", ofile);
314 		perror(NULL);
315 		goto done;
316 	}
317 	pos = image;
318 	do {
319 		nr = write(ofd, pos, len);
320 		if (nr < 0) {
321 			fprintf(stderr, "Could not write data to %s: ", ofile);
322 			perror(NULL);
323 			goto done;
324 		}
325 		len -= nr;
326 		pos += nr;
327 	} while (len);
328 	close(ofd);
329 
330 done:
331 	put_log_buff(kbuff);
332 	free(image);
333 	return 0;
334 }
335