blob: c8ae95804728141654cd4e50b2a612d5f28ed151 [file] [log] [blame]
Thomas Gleixnera7d7a392019-06-03 07:44:48 +02001// SPDX-License-Identifier: GPL-2.0-only
Daniel Borkmanne306e2c2013-03-20 12:11:47 +00002/*
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>
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000015 */
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
Roman Gushchinfb982662017-12-27 19:16:29 +000075#ifdef DISASM_FOUR_ARGS_SIGNATURE
76 disassemble = disassembler(info.arch,
77 bfd_big_endian(bfdf),
78 info.mach,
79 bfdf);
80#else
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000081 disassemble = disassembler(bfdf);
Roman Gushchinfb982662017-12-27 19:16:29 +000082#endif
Daniel Borkmanne306e2c2013-03-20 12:11:47 +000083 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
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200103static char *get_klog_buff(unsigned int *klen)
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000104{
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200105 int ret, len;
106 char *buff;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000107
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200108 len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
Colin Ian King25a54342016-05-05 23:39:33 +0100109 if (len < 0)
110 return NULL;
111
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200112 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
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000122 *klen = ret;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000123 return buff;
124}
125
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200126static 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;
153out_free:
154 free(buff);
155out:
156 close(fd);
157 return NULL;
158}
159
160static 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
165static void put_log_buff(char *buff)
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000166{
167 free(buff);
168}
169
David Daneye274da12017-06-13 16:49:35 -0700170static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
171 unsigned int *ilen)
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000172{
173 char *ptr, *pptr, *tmp;
174 off_t off = 0;
Dan Carpenterb223e3b2018-01-18 12:35:21 +0300175 unsigned int proglen;
176 int ret, flen, pass, ulen = 0;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000177 regmatch_t pmatch[1];
Alexei Starovoitoved4afd42014-05-15 15:56:38 -0700178 unsigned long base;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000179 regex_t regex;
David Daneye274da12017-06-13 16:49:35 -0700180 uint8_t *image;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000181
182 if (hlen == 0)
David Daneye274da12017-06-13 16:49:35 -0700183 return NULL;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000184
185 ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
186 "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
187 assert(ret == 0);
188
189 ptr = haystack;
Daniel Borkmann082739a2015-05-25 14:08:03 +0200190 memset(pmatch, 0, sizeof(pmatch));
191
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000192 while (1) {
193 ret = regexec(&regex, ptr, 1, pmatch, 0);
194 if (ret == 0) {
195 ptr += pmatch[0].rm_eo;
196 off += pmatch[0].rm_eo;
197 assert(off < hlen);
198 } else
199 break;
200 }
201
202 ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
Dan Carpenterb223e3b2018-01-18 12:35:21 +0300203 ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx",
Alexei Starovoitoved4afd42014-05-15 15:56:38 -0700204 &flen, &proglen, &pass, &base);
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200205 if (ret != 4) {
206 regfree(&regex);
David Daneye274da12017-06-13 16:49:35 -0700207 return NULL;
208 }
209 if (proglen > 1000000) {
210 printf("proglen of %d too big, stopping\n", proglen);
211 return NULL;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200212 }
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000213
David Daneye274da12017-06-13 16:49:35 -0700214 image = malloc(proglen);
215 if (!image) {
216 printf("Out of memory\n");
217 return NULL;
218 }
219 memset(image, 0, proglen);
220
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000221 tmp = ptr = haystack + off;
David Daneye274da12017-06-13 16:49:35 -0700222 while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000223 tmp = NULL;
224 if (!strstr(ptr, "JIT code"))
225 continue;
226 pptr = ptr;
227 while ((ptr = strstr(pptr, ":")))
228 pptr = ptr + 1;
229 ptr = pptr;
230 do {
231 image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
David Daneye274da12017-06-13 16:49:35 -0700232 if (ptr == pptr) {
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000233 ulen--;
234 break;
235 }
David Daneye274da12017-06-13 16:49:35 -0700236 if (ulen >= proglen)
237 break;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000238 ptr = pptr;
239 } while (1);
240 }
241
242 assert(ulen == proglen);
Dan Carpenterb223e3b2018-01-18 12:35:21 +0300243 printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000244 proglen, pass, flen);
Alexei Starovoitoved4afd42014-05-15 15:56:38 -0700245 printf("%lx + <x>:\n", base);
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000246
247 regfree(&regex);
David Daneye274da12017-06-13 16:49:35 -0700248 *ilen = ulen;
249 return image;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000250}
251
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200252static void usage(void)
253{
254 printf("Usage: bpf_jit_disasm [...]\n");
255 printf(" -o Also display related opcodes (default: off).\n");
David Daneyb6518e62017-04-11 14:30:52 -0700256 printf(" -O <file> Write binary image of code to file, don't disassemble to stdout.\n");
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200257 printf(" -f <file> Read last image dump from file or stdin (default: klog).\n");
258 printf(" -h Display this help.\n");
259}
260
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000261int main(int argc, char **argv)
262{
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200263 unsigned int len, klen, opt, opcodes = 0;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200264 char *kbuff, *file = NULL;
David Daneyb6518e62017-04-11 14:30:52 -0700265 char *ofile = NULL;
266 int ofd;
267 ssize_t nr;
268 uint8_t *pos;
David Daneye274da12017-06-13 16:49:35 -0700269 uint8_t *image = NULL;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000270
David Daneyb6518e62017-04-11 14:30:52 -0700271 while ((opt = getopt(argc, argv, "of:O:")) != -1) {
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200272 switch (opt) {
273 case 'o':
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000274 opcodes = 1;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200275 break;
David Daneyb6518e62017-04-11 14:30:52 -0700276 case 'O':
277 ofile = optarg;
278 break;
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200279 case 'f':
280 file = optarg;
281 break;
282 default:
283 usage();
284 return -1;
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000285 }
286 }
287
288 bfd_init();
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000289
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200290 kbuff = get_log_buff(file, &klen);
291 if (!kbuff) {
292 fprintf(stderr, "Could not retrieve log buffer!\n");
293 return -1;
294 }
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000295
David Daneye274da12017-06-13 16:49:35 -0700296 image = get_last_jit_image(kbuff, klen, &len);
297 if (!image) {
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200298 fprintf(stderr, "No JIT image found!\n");
David Daneyb6518e62017-04-11 14:30:52 -0700299 goto done;
300 }
301 if (!ofile) {
302 get_asm_insns(image, len, opcodes);
303 goto done;
304 }
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000305
David Daneyb6518e62017-04-11 14:30:52 -0700306 ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
307 if (ofd < 0) {
308 fprintf(stderr, "Could not open file %s for writing: ", ofile);
309 perror(NULL);
310 goto done;
311 }
312 pos = image;
313 do {
314 nr = write(ofd, pos, len);
315 if (nr < 0) {
316 fprintf(stderr, "Could not write data to %s: ", ofile);
317 perror(NULL);
318 goto done;
319 }
320 len -= nr;
321 pos += nr;
322 } while (len);
323 close(ofd);
324
325done:
Daniel Borkmanna6ed3832015-07-30 16:04:22 +0200326 put_log_buff(kbuff);
David Daneye274da12017-06-13 16:49:35 -0700327 free(image);
Daniel Borkmanne306e2c2013-03-20 12:11:47 +0000328 return 0;
329}