bpf_jit_disasm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-only
  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>
  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. #include <limits.h>
  30. #include <tools/dis-asm-compat.h>
  31. #define CMD_ACTION_SIZE_BUFFER 10
  32. #define CMD_ACTION_READ_ALL 3
  33. static void get_exec_path(char *tpath, size_t size)
  34. {
  35. char *path;
  36. ssize_t len;
  37. snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
  38. tpath[size - 1] = 0;
  39. path = strdup(tpath);
  40. assert(path);
  41. len = readlink(path, tpath, size);
  42. if (len < 0)
  43. len = 0;
  44. tpath[len] = 0;
  45. free(path);
  46. }
  47. static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
  48. {
  49. int count, i, pc = 0;
  50. char tpath[PATH_MAX];
  51. struct disassemble_info info;
  52. disassembler_ftype disassemble;
  53. bfd *bfdf;
  54. memset(tpath, 0, sizeof(tpath));
  55. get_exec_path(tpath, sizeof(tpath));
  56. bfdf = bfd_openr(tpath, NULL);
  57. assert(bfdf);
  58. assert(bfd_check_format(bfdf, bfd_object));
  59. init_disassemble_info_compat(&info, stdout,
  60. (fprintf_ftype) fprintf,
  61. fprintf_styled);
  62. info.arch = bfd_get_arch(bfdf);
  63. info.mach = bfd_get_mach(bfdf);
  64. info.buffer = image;
  65. info.buffer_length = len;
  66. disassemble_init_for_target(&info);
  67. #ifdef DISASM_FOUR_ARGS_SIGNATURE
  68. disassemble = disassembler(info.arch,
  69. bfd_big_endian(bfdf),
  70. info.mach,
  71. bfdf);
  72. #else
  73. disassemble = disassembler(bfdf);
  74. #endif
  75. assert(disassemble);
  76. do {
  77. printf("%4x:\t", pc);
  78. count = disassemble(pc, &info);
  79. if (opcodes) {
  80. printf("\n\t");
  81. for (i = 0; i < count; ++i)
  82. printf("%02x ", (uint8_t) image[pc + i]);
  83. }
  84. printf("\n");
  85. pc += count;
  86. } while(count > 0 && pc < len);
  87. bfd_close(bfdf);
  88. }
  89. static char *get_klog_buff(unsigned int *klen)
  90. {
  91. int ret, len;
  92. char *buff;
  93. len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
  94. if (len < 0)
  95. return NULL;
  96. buff = malloc(len);
  97. if (!buff)
  98. return NULL;
  99. ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
  100. if (ret < 0) {
  101. free(buff);
  102. return NULL;
  103. }
  104. *klen = ret;
  105. return buff;
  106. }
  107. static char *get_flog_buff(const char *file, unsigned int *klen)
  108. {
  109. int fd, ret, len;
  110. struct stat fi;
  111. char *buff;
  112. fd = open(file, O_RDONLY);
  113. if (fd < 0)
  114. return NULL;
  115. ret = fstat(fd, &fi);
  116. if (ret < 0 || !S_ISREG(fi.st_mode))
  117. goto out;
  118. len = fi.st_size + 1;
  119. buff = malloc(len);
  120. if (!buff)
  121. goto out;
  122. memset(buff, 0, len);
  123. ret = read(fd, buff, len - 1);
  124. if (ret <= 0)
  125. goto out_free;
  126. close(fd);
  127. *klen = ret;
  128. return buff;
  129. out_free:
  130. free(buff);
  131. out:
  132. close(fd);
  133. return NULL;
  134. }
  135. static char *get_log_buff(const char *file, unsigned int *klen)
  136. {
  137. return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
  138. }
  139. static void put_log_buff(char *buff)
  140. {
  141. free(buff);
  142. }
  143. static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
  144. unsigned int *ilen)
  145. {
  146. char *ptr, *pptr, *tmp;
  147. off_t off = 0;
  148. unsigned int proglen;
  149. int ret, flen, pass, ulen = 0;
  150. regmatch_t pmatch[1];
  151. unsigned long base;
  152. regex_t regex;
  153. uint8_t *image;
  154. if (hlen == 0)
  155. return NULL;
  156. ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
  157. "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
  158. assert(ret == 0);
  159. ptr = haystack;
  160. memset(pmatch, 0, sizeof(pmatch));
  161. while (1) {
  162. ret = regexec(&regex, ptr, 1, pmatch, 0);
  163. if (ret == 0) {
  164. ptr += pmatch[0].rm_eo;
  165. off += pmatch[0].rm_eo;
  166. assert(off < hlen);
  167. } else
  168. break;
  169. }
  170. ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
  171. ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx",
  172. &flen, &proglen, &pass, &base);
  173. if (ret != 4) {
  174. regfree(&regex);
  175. return NULL;
  176. }
  177. if (proglen > 1000000) {
  178. printf("proglen of %u too big, stopping\n", proglen);
  179. return NULL;
  180. }
  181. image = malloc(proglen);
  182. if (!image) {
  183. printf("Out of memory\n");
  184. return NULL;
  185. }
  186. memset(image, 0, proglen);
  187. tmp = ptr = haystack + off;
  188. while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
  189. tmp = NULL;
  190. if (!strstr(ptr, "JIT code"))
  191. continue;
  192. pptr = ptr;
  193. while ((ptr = strstr(pptr, ":")))
  194. pptr = ptr + 1;
  195. ptr = pptr;
  196. do {
  197. image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
  198. if (ptr == pptr) {
  199. ulen--;
  200. break;
  201. }
  202. if (ulen >= proglen)
  203. break;
  204. ptr = pptr;
  205. } while (1);
  206. }
  207. assert(ulen == proglen);
  208. printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
  209. proglen, pass, flen);
  210. printf("%lx + <x>:\n", base);
  211. regfree(&regex);
  212. *ilen = ulen;
  213. return image;
  214. }
  215. static void usage(void)
  216. {
  217. printf("Usage: bpf_jit_disasm [...]\n");
  218. printf(" -o Also display related opcodes (default: off).\n");
  219. printf(" -O <file> Write binary image of code to file, don't disassemble to stdout.\n");
  220. printf(" -f <file> Read last image dump from file or stdin (default: klog).\n");
  221. printf(" -h Display this help.\n");
  222. }
  223. int main(int argc, char **argv)
  224. {
  225. unsigned int len, klen, opt, opcodes = 0;
  226. char *kbuff, *file = NULL;
  227. char *ofile = NULL;
  228. int ofd;
  229. ssize_t nr;
  230. uint8_t *pos;
  231. uint8_t *image = NULL;
  232. while ((opt = getopt(argc, argv, "of:O:")) != -1) {
  233. switch (opt) {
  234. case 'o':
  235. opcodes = 1;
  236. break;
  237. case 'O':
  238. ofile = optarg;
  239. break;
  240. case 'f':
  241. file = optarg;
  242. break;
  243. default:
  244. usage();
  245. return -1;
  246. }
  247. }
  248. bfd_init();
  249. kbuff = get_log_buff(file, &klen);
  250. if (!kbuff) {
  251. fprintf(stderr, "Could not retrieve log buffer!\n");
  252. return -1;
  253. }
  254. image = get_last_jit_image(kbuff, klen, &len);
  255. if (!image) {
  256. fprintf(stderr, "No JIT image found!\n");
  257. goto done;
  258. }
  259. if (!ofile) {
  260. get_asm_insns(image, len, opcodes);
  261. goto done;
  262. }
  263. ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
  264. if (ofd < 0) {
  265. fprintf(stderr, "Could not open file %s for writing: ", ofile);
  266. perror(NULL);
  267. goto done;
  268. }
  269. pos = image;
  270. do {
  271. nr = write(ofd, pos, len);
  272. if (nr < 0) {
  273. fprintf(stderr, "Could not write data to %s: ", ofile);
  274. perror(NULL);
  275. goto done;
  276. }
  277. len -= nr;
  278. pos += nr;
  279. } while (len);
  280. close(ofd);
  281. done:
  282. put_log_buff(kbuff);
  283. free(image);
  284. return 0;
  285. }