Thomas Gleixner | a7d7a39 | 2019-06-03 07:44:48 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 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> |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 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 Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 25 | #include <regex.h> |
| 26 | #include <fcntl.h> |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 27 | #include <sys/klog.h> |
| 28 | #include <sys/types.h> |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 29 | #include <sys/stat.h> |
Prashant Bhole | cdc89c9 | 2017-11-02 17:09:45 +0900 | [diff] [blame] | 30 | #include <limits.h> |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 31 | |
| 32 | #define CMD_ACTION_SIZE_BUFFER 10 |
| 33 | #define CMD_ACTION_READ_ALL 3 |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 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 | |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 52 | static void get_asm_insns(uint8_t *image, size_t len, int opcodes) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 53 | { |
| 54 | int count, i, pc = 0; |
Prashant Bhole | cdc89c9 | 2017-11-02 17:09:45 +0900 | [diff] [blame] | 55 | char tpath[PATH_MAX]; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 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 | |
Roman Gushchin | fb98266 | 2017-12-27 19:16:29 +0000 | [diff] [blame] | 75 | #ifdef DISASM_FOUR_ARGS_SIGNATURE |
| 76 | disassemble = disassembler(info.arch, |
| 77 | bfd_big_endian(bfdf), |
| 78 | info.mach, |
| 79 | bfdf); |
| 80 | #else |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 81 | disassemble = disassembler(bfdf); |
Roman Gushchin | fb98266 | 2017-12-27 19:16:29 +0000 | [diff] [blame] | 82 | #endif |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 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 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 103 | static char *get_klog_buff(unsigned int *klen) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 104 | { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 105 | int ret, len; |
| 106 | char *buff; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 107 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 108 | len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0); |
Colin Ian King | 25a5434 | 2016-05-05 23:39:33 +0100 | [diff] [blame] | 109 | if (len < 0) |
| 110 | return NULL; |
| 111 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 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 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 122 | *klen = ret; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 123 | return buff; |
| 124 | } |
| 125 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 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) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 166 | { |
| 167 | free(buff); |
| 168 | } |
| 169 | |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 170 | static uint8_t *get_last_jit_image(char *haystack, size_t hlen, |
| 171 | unsigned int *ilen) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 172 | { |
| 173 | char *ptr, *pptr, *tmp; |
| 174 | off_t off = 0; |
Dan Carpenter | b223e3b | 2018-01-18 12:35:21 +0300 | [diff] [blame] | 175 | unsigned int proglen; |
| 176 | int ret, flen, pass, ulen = 0; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 177 | regmatch_t pmatch[1]; |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 178 | unsigned long base; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 179 | regex_t regex; |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 180 | uint8_t *image; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 181 | |
| 182 | if (hlen == 0) |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 183 | return NULL; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 184 | |
| 185 | ret = regcomp(®ex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ " |
| 186 | "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED); |
| 187 | assert(ret == 0); |
| 188 | |
| 189 | ptr = haystack; |
Daniel Borkmann | 082739a | 2015-05-25 14:08:03 +0200 | [diff] [blame] | 190 | memset(pmatch, 0, sizeof(pmatch)); |
| 191 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 192 | while (1) { |
| 193 | ret = regexec(®ex, 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 Carpenter | b223e3b | 2018-01-18 12:35:21 +0300 | [diff] [blame] | 203 | ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx", |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 204 | &flen, &proglen, &pass, &base); |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 205 | if (ret != 4) { |
| 206 | regfree(®ex); |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 207 | return NULL; |
| 208 | } |
| 209 | if (proglen > 1000000) { |
| 210 | printf("proglen of %d too big, stopping\n", proglen); |
| 211 | return NULL; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 212 | } |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 213 | |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 214 | image = malloc(proglen); |
| 215 | if (!image) { |
| 216 | printf("Out of memory\n"); |
| 217 | return NULL; |
| 218 | } |
| 219 | memset(image, 0, proglen); |
| 220 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 221 | tmp = ptr = haystack + off; |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 222 | while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) { |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 223 | 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 Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 232 | if (ptr == pptr) { |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 233 | ulen--; |
| 234 | break; |
| 235 | } |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 236 | if (ulen >= proglen) |
| 237 | break; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 238 | ptr = pptr; |
| 239 | } while (1); |
| 240 | } |
| 241 | |
| 242 | assert(ulen == proglen); |
Dan Carpenter | b223e3b | 2018-01-18 12:35:21 +0300 | [diff] [blame] | 243 | printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n", |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 244 | proglen, pass, flen); |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 245 | printf("%lx + <x>:\n", base); |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 246 | |
| 247 | regfree(®ex); |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 248 | *ilen = ulen; |
| 249 | return image; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 252 | static void usage(void) |
| 253 | { |
| 254 | printf("Usage: bpf_jit_disasm [...]\n"); |
| 255 | printf(" -o Also display related opcodes (default: off).\n"); |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 256 | printf(" -O <file> Write binary image of code to file, don't disassemble to stdout.\n"); |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 257 | printf(" -f <file> Read last image dump from file or stdin (default: klog).\n"); |
| 258 | printf(" -h Display this help.\n"); |
| 259 | } |
| 260 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 261 | int main(int argc, char **argv) |
| 262 | { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 263 | unsigned int len, klen, opt, opcodes = 0; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 264 | char *kbuff, *file = NULL; |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 265 | char *ofile = NULL; |
| 266 | int ofd; |
| 267 | ssize_t nr; |
| 268 | uint8_t *pos; |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 269 | uint8_t *image = NULL; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 270 | |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 271 | while ((opt = getopt(argc, argv, "of:O:")) != -1) { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 272 | switch (opt) { |
| 273 | case 'o': |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 274 | opcodes = 1; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 275 | break; |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 276 | case 'O': |
| 277 | ofile = optarg; |
| 278 | break; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 279 | case 'f': |
| 280 | file = optarg; |
| 281 | break; |
| 282 | default: |
| 283 | usage(); |
| 284 | return -1; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | bfd_init(); |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 289 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 290 | kbuff = get_log_buff(file, &klen); |
| 291 | if (!kbuff) { |
| 292 | fprintf(stderr, "Could not retrieve log buffer!\n"); |
| 293 | return -1; |
| 294 | } |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 295 | |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 296 | image = get_last_jit_image(kbuff, klen, &len); |
| 297 | if (!image) { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 298 | fprintf(stderr, "No JIT image found!\n"); |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 299 | goto done; |
| 300 | } |
| 301 | if (!ofile) { |
| 302 | get_asm_insns(image, len, opcodes); |
| 303 | goto done; |
| 304 | } |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 305 | |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 306 | 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 | |
| 325 | done: |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 326 | put_log_buff(kbuff); |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 327 | free(image); |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 328 | return 0; |
| 329 | } |