blob: 75bf526a01686990f4204ad69e50811fbc7d40be [file] [log] [blame]
Daniel Borkmanne306e2c2013-03-20 12:11:47 +00001/*
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>
Daniel Borkmanna6ed3832015-07-30 16:04:22 +020025#include <regex.h>
26#include <fcntl.h>
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000027#include <sys/klog.h>
28#include <sys/types.h>
Daniel Borkmanna6ed3832015-07-30 16:04:22 +020029#include <sys/stat.h>
Prashant Bholecdc89c92017-11-02 17:09:45 +090030#include <limits.h>
Daniel Borkmanna6ed3832015-07-30 16:04:22 +020031
32#define CMD_ACTION_SIZE_BUFFER 10
33#define CMD_ACTION_READ_ALL 3
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000034
35static 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
Alexei Starovoitoved4afd42014-05-15 15:56:38 -070052static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000053{
54 int count, i, pc = 0;
Prashant Bholecdc89c92017-11-02 17:09:45 +090055 char tpath[PATH_MAX];
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000056 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 disassemble = disassembler(bfdf);
76 assert(disassemble);
77
78 do {
79 printf("%4x:\t", pc);
80
81 count = disassemble(pc, &info);
82
83 if (opcodes) {
84 printf("\n\t");
85 for (i = 0; i < count; ++i)
86 printf("%02x ", (uint8_t) image[pc + i]);
87 }
88 printf("\n");
89
90 pc += count;
91 } while(count > 0 && pc < len);
92
93 bfd_close(bfdf);
94}
95
Daniel Borkmanna6ed3832015-07-30 16:04:22 +020096static char *get_klog_buff(unsigned int *klen)
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000097{
Daniel Borkmanna6ed3832015-07-30 16:04:22 +020098 int ret, len;
99 char *buff;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000100
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200101 len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
Colin Ian King25a54342016-05-05 23:39:33 +0100102 if (len < 0)
103 return NULL;
104
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200105 buff = malloc(len);
106 if (!buff)
107 return NULL;
108
109 ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
110 if (ret < 0) {
111 free(buff);
112 return NULL;
113 }
114
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000115 *klen = ret;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000116 return buff;
117}
118
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200119static char *get_flog_buff(const char *file, unsigned int *klen)
120{
121 int fd, ret, len;
122 struct stat fi;
123 char *buff;
124
125 fd = open(file, O_RDONLY);
126 if (fd < 0)
127 return NULL;
128
129 ret = fstat(fd, &fi);
130 if (ret < 0 || !S_ISREG(fi.st_mode))
131 goto out;
132
133 len = fi.st_size + 1;
134 buff = malloc(len);
135 if (!buff)
136 goto out;
137
138 memset(buff, 0, len);
139 ret = read(fd, buff, len - 1);
140 if (ret <= 0)
141 goto out_free;
142
143 close(fd);
144 *klen = ret;
145 return buff;
146out_free:
147 free(buff);
148out:
149 close(fd);
150 return NULL;
151}
152
153static char *get_log_buff(const char *file, unsigned int *klen)
154{
155 return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
156}
157
158static void put_log_buff(char *buff)
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000159{
160 free(buff);
161}
162
David Daneye274da12017-06-13 16:49:35 -0700163static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
164 unsigned int *ilen)
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000165{
166 char *ptr, *pptr, *tmp;
167 off_t off = 0;
168 int ret, flen, proglen, pass, ulen = 0;
169 regmatch_t pmatch[1];
Alexei Starovoitoved4afd42014-05-15 15:56:38 -0700170 unsigned long base;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000171 regex_t regex;
David Daneye274da12017-06-13 16:49:35 -0700172 uint8_t *image;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000173
174 if (hlen == 0)
David Daneye274da12017-06-13 16:49:35 -0700175 return NULL;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000176
177 ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
178 "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
179 assert(ret == 0);
180
181 ptr = haystack;
Daniel Borkmann082739a2015-05-25 14:08:03 +0200182 memset(pmatch, 0, sizeof(pmatch));
183
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000184 while (1) {
185 ret = regexec(&regex, ptr, 1, pmatch, 0);
186 if (ret == 0) {
187 ptr += pmatch[0].rm_eo;
188 off += pmatch[0].rm_eo;
189 assert(off < hlen);
190 } else
191 break;
192 }
193
194 ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
195 ret = sscanf(ptr, "flen=%d proglen=%d pass=%d image=%lx",
Alexei Starovoitoved4afd42014-05-15 15:56:38 -0700196 &flen, &proglen, &pass, &base);
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200197 if (ret != 4) {
198 regfree(&regex);
David Daneye274da12017-06-13 16:49:35 -0700199 return NULL;
200 }
201 if (proglen > 1000000) {
202 printf("proglen of %d too big, stopping\n", proglen);
203 return NULL;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200204 }
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000205
David Daneye274da12017-06-13 16:49:35 -0700206 image = malloc(proglen);
207 if (!image) {
208 printf("Out of memory\n");
209 return NULL;
210 }
211 memset(image, 0, proglen);
212
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000213 tmp = ptr = haystack + off;
David Daneye274da12017-06-13 16:49:35 -0700214 while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000215 tmp = NULL;
216 if (!strstr(ptr, "JIT code"))
217 continue;
218 pptr = ptr;
219 while ((ptr = strstr(pptr, ":")))
220 pptr = ptr + 1;
221 ptr = pptr;
222 do {
223 image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
David Daneye274da12017-06-13 16:49:35 -0700224 if (ptr == pptr) {
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000225 ulen--;
226 break;
227 }
David Daneye274da12017-06-13 16:49:35 -0700228 if (ulen >= proglen)
229 break;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000230 ptr = pptr;
231 } while (1);
232 }
233
234 assert(ulen == proglen);
235 printf("%d bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
236 proglen, pass, flen);
Alexei Starovoitoved4afd42014-05-15 15:56:38 -0700237 printf("%lx + <x>:\n", base);
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000238
239 regfree(&regex);
David Daneye274da12017-06-13 16:49:35 -0700240 *ilen = ulen;
241 return image;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000242}
243
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200244static void usage(void)
245{
246 printf("Usage: bpf_jit_disasm [...]\n");
247 printf(" -o Also display related opcodes (default: off).\n");
David Daneyb6518e62017-04-11 14:30:52 -0700248 printf(" -O <file> Write binary image of code to file, don't disassemble to stdout.\n");
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200249 printf(" -f <file> Read last image dump from file or stdin (default: klog).\n");
250 printf(" -h Display this help.\n");
251}
252
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000253int main(int argc, char **argv)
254{
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200255 unsigned int len, klen, opt, opcodes = 0;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200256 char *kbuff, *file = NULL;
David Daneyb6518e62017-04-11 14:30:52 -0700257 char *ofile = NULL;
258 int ofd;
259 ssize_t nr;
260 uint8_t *pos;
David Daneye274da12017-06-13 16:49:35 -0700261 uint8_t *image = NULL;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000262
David Daneyb6518e62017-04-11 14:30:52 -0700263 while ((opt = getopt(argc, argv, "of:O:")) != -1) {
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200264 switch (opt) {
265 case 'o':
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000266 opcodes = 1;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200267 break;
David Daneyb6518e62017-04-11 14:30:52 -0700268 case 'O':
269 ofile = optarg;
270 break;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200271 case 'f':
272 file = optarg;
273 break;
274 default:
275 usage();
276 return -1;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000277 }
278 }
279
280 bfd_init();
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000281
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200282 kbuff = get_log_buff(file, &klen);
283 if (!kbuff) {
284 fprintf(stderr, "Could not retrieve log buffer!\n");
285 return -1;
286 }
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000287
David Daneye274da12017-06-13 16:49:35 -0700288 image = get_last_jit_image(kbuff, klen, &len);
289 if (!image) {
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200290 fprintf(stderr, "No JIT image found!\n");
David Daneyb6518e62017-04-11 14:30:52 -0700291 goto done;
292 }
293 if (!ofile) {
294 get_asm_insns(image, len, opcodes);
295 goto done;
296 }
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000297
David Daneyb6518e62017-04-11 14:30:52 -0700298 ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
299 if (ofd < 0) {
300 fprintf(stderr, "Could not open file %s for writing: ", ofile);
301 perror(NULL);
302 goto done;
303 }
304 pos = image;
305 do {
306 nr = write(ofd, pos, len);
307 if (nr < 0) {
308 fprintf(stderr, "Could not write data to %s: ", ofile);
309 perror(NULL);
310 goto done;
311 }
312 len -= nr;
313 pos += nr;
314 } while (len);
315 close(ofd);
316
317done:
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200318 put_log_buff(kbuff);
David Daneye274da12017-06-13 16:49:35 -0700319 free(image);
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000320 return 0;
321}