bpf_jit_disasm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <regex.h>
  25. #include <fcntl.h>
  26. #include <sys/klog.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #define CMD_ACTION_SIZE_BUFFER 10
  30. #define CMD_ACTION_READ_ALL 3
  31. static void get_exec_path(char *tpath, size_t size)
  32. {
  33. char *path;
  34. ssize_t len;
  35. snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
  36. tpath[size - 1] = 0;
  37. path = strdup(tpath);
  38. assert(path);
  39. len = readlink(path, tpath, size);
  40. tpath[len] = 0;
  41. free(path);
  42. }
  43. static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
  44. {
  45. int count, i, pc = 0;
  46. char tpath[256];
  47. struct disassemble_info info;
  48. disassembler_ftype disassemble;
  49. bfd *bfdf;
  50. memset(tpath, 0, sizeof(tpath));
  51. get_exec_path(tpath, sizeof(tpath));
  52. bfdf = bfd_openr(tpath, NULL);
  53. assert(bfdf);
  54. assert(bfd_check_format(bfdf, bfd_object));
  55. init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf);
  56. info.arch = bfd_get_arch(bfdf);
  57. info.mach = bfd_get_mach(bfdf);
  58. info.buffer = image;
  59. info.buffer_length = len;
  60. disassemble_init_for_target(&info);
  61. disassemble = disassembler(bfdf);
  62. assert(disassemble);
  63. do {
  64. printf("%4x:\t", pc);
  65. count = disassemble(pc, &info);
  66. if (opcodes) {
  67. printf("\n\t");
  68. for (i = 0; i < count; ++i)
  69. printf("%02x ", (uint8_t) image[pc + i]);
  70. }
  71. printf("\n");
  72. pc += count;
  73. } while(count > 0 && pc < len);
  74. bfd_close(bfdf);
  75. }
  76. static char *get_klog_buff(unsigned int *klen)
  77. {
  78. int ret, len;
  79. char *buff;
  80. len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
  81. buff = malloc(len);
  82. if (!buff)
  83. return NULL;
  84. ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
  85. if (ret < 0) {
  86. free(buff);
  87. return NULL;
  88. }
  89. *klen = ret;
  90. return buff;
  91. }
  92. static char *get_flog_buff(const char *file, unsigned int *klen)
  93. {
  94. int fd, ret, len;
  95. struct stat fi;
  96. char *buff;
  97. fd = open(file, O_RDONLY);
  98. if (fd < 0)
  99. return NULL;
  100. ret = fstat(fd, &fi);
  101. if (ret < 0 || !S_ISREG(fi.st_mode))
  102. goto out;
  103. len = fi.st_size + 1;
  104. buff = malloc(len);
  105. if (!buff)
  106. goto out;
  107. memset(buff, 0, len);
  108. ret = read(fd, buff, len - 1);
  109. if (ret <= 0)
  110. goto out_free;
  111. close(fd);
  112. *klen = ret;
  113. return buff;
  114. out_free:
  115. free(buff);
  116. out:
  117. close(fd);
  118. return NULL;
  119. }
  120. static char *get_log_buff(const char *file, unsigned int *klen)
  121. {
  122. return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
  123. }
  124. static void put_log_buff(char *buff)
  125. {
  126. free(buff);
  127. }
  128. static unsigned int get_last_jit_image(char *haystack, size_t hlen,
  129. uint8_t *image, size_t ilen)
  130. {
  131. char *ptr, *pptr, *tmp;
  132. off_t off = 0;
  133. int ret, flen, proglen, pass, ulen = 0;
  134. regmatch_t pmatch[1];
  135. unsigned long base;
  136. regex_t regex;
  137. if (hlen == 0)
  138. return 0;
  139. ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
  140. "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
  141. assert(ret == 0);
  142. ptr = haystack;
  143. memset(pmatch, 0, sizeof(pmatch));
  144. while (1) {
  145. ret = regexec(&regex, ptr, 1, pmatch, 0);
  146. if (ret == 0) {
  147. ptr += pmatch[0].rm_eo;
  148. off += pmatch[0].rm_eo;
  149. assert(off < hlen);
  150. } else
  151. break;
  152. }
  153. ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
  154. ret = sscanf(ptr, "flen=%d proglen=%d pass=%d image=%lx",
  155. &flen, &proglen, &pass, &base);
  156. if (ret != 4) {
  157. regfree(&regex);
  158. return 0;
  159. }
  160. tmp = ptr = haystack + off;
  161. while ((ptr = strtok(tmp, "\n")) != NULL && ulen < ilen) {
  162. tmp = NULL;
  163. if (!strstr(ptr, "JIT code"))
  164. continue;
  165. pptr = ptr;
  166. while ((ptr = strstr(pptr, ":")))
  167. pptr = ptr + 1;
  168. ptr = pptr;
  169. do {
  170. image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
  171. if (ptr == pptr || ulen >= ilen) {
  172. ulen--;
  173. break;
  174. }
  175. ptr = pptr;
  176. } while (1);
  177. }
  178. assert(ulen == proglen);
  179. printf("%d bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
  180. proglen, pass, flen);
  181. printf("%lx + <x>:\n", base);
  182. regfree(&regex);
  183. return ulen;
  184. }
  185. static void usage(void)
  186. {
  187. printf("Usage: bpf_jit_disasm [...]\n");
  188. printf(" -o Also display related opcodes (default: off).\n");
  189. printf(" -f <file> Read last image dump from file or stdin (default: klog).\n");
  190. printf(" -h Display this help.\n");
  191. }
  192. int main(int argc, char **argv)
  193. {
  194. unsigned int len, klen, opt, opcodes = 0;
  195. static uint8_t image[32768];
  196. char *kbuff, *file = NULL;
  197. while ((opt = getopt(argc, argv, "of:")) != -1) {
  198. switch (opt) {
  199. case 'o':
  200. opcodes = 1;
  201. break;
  202. case 'f':
  203. file = optarg;
  204. break;
  205. default:
  206. usage();
  207. return -1;
  208. }
  209. }
  210. bfd_init();
  211. memset(image, 0, sizeof(image));
  212. kbuff = get_log_buff(file, &klen);
  213. if (!kbuff) {
  214. fprintf(stderr, "Could not retrieve log buffer!\n");
  215. return -1;
  216. }
  217. len = get_last_jit_image(kbuff, klen, image, sizeof(image));
  218. if (len > 0)
  219. get_asm_insns(image, len, opcodes);
  220. else
  221. fprintf(stderr, "No JIT image found!\n");
  222. put_log_buff(kbuff);
  223. return 0;
  224. }