bpf_jit_disasm.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <assert.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <bfd.h>
  23. #include <dis-asm.h>
  24. #include <sys/klog.h>
  25. #include <sys/types.h>
  26. #include <regex.h>
  27. static void get_exec_path(char *tpath, size_t size)
  28. {
  29. char *path;
  30. ssize_t len;
  31. snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
  32. tpath[size - 1] = 0;
  33. path = strdup(tpath);
  34. assert(path);
  35. len = readlink(path, tpath, size);
  36. tpath[len] = 0;
  37. free(path);
  38. }
  39. static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
  40. {
  41. int count, i, pc = 0;
  42. char tpath[256];
  43. struct disassemble_info info;
  44. disassembler_ftype disassemble;
  45. bfd *bfdf;
  46. memset(tpath, 0, sizeof(tpath));
  47. get_exec_path(tpath, sizeof(tpath));
  48. bfdf = bfd_openr(tpath, NULL);
  49. assert(bfdf);
  50. assert(bfd_check_format(bfdf, bfd_object));
  51. init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf);
  52. info.arch = bfd_get_arch(bfdf);
  53. info.mach = bfd_get_mach(bfdf);
  54. info.buffer = image;
  55. info.buffer_length = len;
  56. disassemble_init_for_target(&info);
  57. disassemble = disassembler(bfdf);
  58. assert(disassemble);
  59. do {
  60. printf("%4x:\t", pc);
  61. count = disassemble(pc, &info);
  62. if (opcodes) {
  63. printf("\n\t");
  64. for (i = 0; i < count; ++i)
  65. printf("%02x ", (uint8_t) image[pc + i]);
  66. }
  67. printf("\n");
  68. pc += count;
  69. } while(count > 0 && pc < len);
  70. bfd_close(bfdf);
  71. }
  72. static char *get_klog_buff(int *klen)
  73. {
  74. int ret, len = klogctl(10, NULL, 0);
  75. char *buff = malloc(len);
  76. assert(buff && klen);
  77. ret = klogctl(3, buff, len);
  78. assert(ret >= 0);
  79. *klen = ret;
  80. return buff;
  81. }
  82. static void put_klog_buff(char *buff)
  83. {
  84. free(buff);
  85. }
  86. static int get_last_jit_image(char *haystack, size_t hlen,
  87. uint8_t *image, size_t ilen)
  88. {
  89. char *ptr, *pptr, *tmp;
  90. off_t off = 0;
  91. int ret, flen, proglen, pass, ulen = 0;
  92. regmatch_t pmatch[1];
  93. unsigned long base;
  94. regex_t regex;
  95. if (hlen == 0)
  96. return 0;
  97. ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
  98. "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
  99. assert(ret == 0);
  100. ptr = haystack;
  101. while (1) {
  102. ret = regexec(&regex, ptr, 1, pmatch, 0);
  103. if (ret == 0) {
  104. ptr += pmatch[0].rm_eo;
  105. off += pmatch[0].rm_eo;
  106. assert(off < hlen);
  107. } else
  108. break;
  109. }
  110. ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
  111. ret = sscanf(ptr, "flen=%d proglen=%d pass=%d image=%lx",
  112. &flen, &proglen, &pass, &base);
  113. if (ret != 4)
  114. return 0;
  115. tmp = ptr = haystack + off;
  116. while ((ptr = strtok(tmp, "\n")) != NULL && ulen < ilen) {
  117. tmp = NULL;
  118. if (!strstr(ptr, "JIT code"))
  119. continue;
  120. pptr = ptr;
  121. while ((ptr = strstr(pptr, ":")))
  122. pptr = ptr + 1;
  123. ptr = pptr;
  124. do {
  125. image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
  126. if (ptr == pptr || ulen >= ilen) {
  127. ulen--;
  128. break;
  129. }
  130. ptr = pptr;
  131. } while (1);
  132. }
  133. assert(ulen == proglen);
  134. printf("%d bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
  135. proglen, pass, flen);
  136. printf("%lx + <x>:\n", base);
  137. regfree(&regex);
  138. return ulen;
  139. }
  140. int main(int argc, char **argv)
  141. {
  142. int len, klen, opcodes = 0;
  143. char *kbuff;
  144. static uint8_t image[32768];
  145. if (argc > 1) {
  146. if (!strncmp("-o", argv[argc - 1], 2)) {
  147. opcodes = 1;
  148. } else {
  149. printf("usage: bpf_jit_disasm [-o: show opcodes]\n");
  150. exit(0);
  151. }
  152. }
  153. bfd_init();
  154. memset(image, 0, sizeof(image));
  155. kbuff = get_klog_buff(&klen);
  156. len = get_last_jit_image(kbuff, klen, image, sizeof(image));
  157. if (len > 0)
  158. get_asm_insns(image, len, opcodes);
  159. put_klog_buff(kbuff);
  160. return 0;
  161. }