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