insn_decoder_test.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * Copyright (C) IBM Corporation, 2009
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include <unistd.h>
  19. #include <stdarg.h>
  20. #define unlikely(cond) (cond)
  21. #include <asm/insn.h>
  22. #include <inat.c>
  23. #include <insn.c>
  24. /*
  25. * Test of instruction analysis in general and insn_get_length() in
  26. * particular. See if insn_get_length() and the disassembler agree
  27. * on the length of each instruction in an elf disassembly.
  28. *
  29. * Usage: objdump -d a.out | awk -f objdump_reformat.awk | ./insn_decoder_test
  30. */
  31. const char *prog;
  32. static int verbose;
  33. static int x86_64;
  34. static void usage(void)
  35. {
  36. fprintf(stderr, "Usage: objdump -d a.out | awk -f objdump_reformat.awk"
  37. " | %s [-y|-n] [-v]\n", prog);
  38. fprintf(stderr, "\t-y 64bit mode\n");
  39. fprintf(stderr, "\t-n 32bit mode\n");
  40. fprintf(stderr, "\t-v verbose mode\n");
  41. exit(1);
  42. }
  43. static void malformed_line(const char *line, int line_nr)
  44. {
  45. fprintf(stderr, "%s: error: malformed line %d:\n%s",
  46. prog, line_nr, line);
  47. exit(3);
  48. }
  49. static void pr_warn(const char *fmt, ...)
  50. {
  51. va_list ap;
  52. fprintf(stderr, "%s: warning: ", prog);
  53. va_start(ap, fmt);
  54. vfprintf(stderr, fmt, ap);
  55. va_end(ap);
  56. }
  57. static void dump_field(FILE *fp, const char *name, const char *indent,
  58. struct insn_field *field)
  59. {
  60. fprintf(fp, "%s.%s = {\n", indent, name);
  61. fprintf(fp, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
  62. indent, field->value, field->bytes[0], field->bytes[1],
  63. field->bytes[2], field->bytes[3]);
  64. fprintf(fp, "%s\t.got = %d, .nbytes = %d},\n", indent,
  65. field->got, field->nbytes);
  66. }
  67. static void dump_insn(FILE *fp, struct insn *insn)
  68. {
  69. fprintf(fp, "Instruction = {\n");
  70. dump_field(fp, "prefixes", "\t", &insn->prefixes);
  71. dump_field(fp, "rex_prefix", "\t", &insn->rex_prefix);
  72. dump_field(fp, "vex_prefix", "\t", &insn->vex_prefix);
  73. dump_field(fp, "opcode", "\t", &insn->opcode);
  74. dump_field(fp, "modrm", "\t", &insn->modrm);
  75. dump_field(fp, "sib", "\t", &insn->sib);
  76. dump_field(fp, "displacement", "\t", &insn->displacement);
  77. dump_field(fp, "immediate1", "\t", &insn->immediate1);
  78. dump_field(fp, "immediate2", "\t", &insn->immediate2);
  79. fprintf(fp, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
  80. insn->attr, insn->opnd_bytes, insn->addr_bytes);
  81. fprintf(fp, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
  82. insn->length, insn->x86_64, insn->kaddr);
  83. }
  84. static void parse_args(int argc, char **argv)
  85. {
  86. int c;
  87. prog = argv[0];
  88. while ((c = getopt(argc, argv, "ynv")) != -1) {
  89. switch (c) {
  90. case 'y':
  91. x86_64 = 1;
  92. break;
  93. case 'n':
  94. x86_64 = 0;
  95. break;
  96. case 'v':
  97. verbose = 1;
  98. break;
  99. default:
  100. usage();
  101. }
  102. }
  103. }
  104. #define BUFSIZE 256
  105. int main(int argc, char **argv)
  106. {
  107. char line[BUFSIZE], sym[BUFSIZE] = "<unknown>";
  108. unsigned char insn_buf[16];
  109. struct insn insn;
  110. int insns = 0;
  111. int warnings = 0;
  112. parse_args(argc, argv);
  113. while (fgets(line, BUFSIZE, stdin)) {
  114. char copy[BUFSIZE], *s, *tab1, *tab2;
  115. int nb = 0;
  116. unsigned int b;
  117. if (line[0] == '<') {
  118. /* Symbol line */
  119. strcpy(sym, line);
  120. continue;
  121. }
  122. insns++;
  123. memset(insn_buf, 0, 16);
  124. strcpy(copy, line);
  125. tab1 = strchr(copy, '\t');
  126. if (!tab1)
  127. malformed_line(line, insns);
  128. s = tab1 + 1;
  129. s += strspn(s, " ");
  130. tab2 = strchr(s, '\t');
  131. if (!tab2)
  132. malformed_line(line, insns);
  133. *tab2 = '\0'; /* Characters beyond tab2 aren't examined */
  134. while (s < tab2) {
  135. if (sscanf(s, "%x", &b) == 1) {
  136. insn_buf[nb++] = (unsigned char) b;
  137. s += 3;
  138. } else
  139. break;
  140. }
  141. /* Decode an instruction */
  142. insn_init(&insn, insn_buf, sizeof(insn_buf), x86_64);
  143. insn_get_length(&insn);
  144. if (insn.length != nb) {
  145. warnings++;
  146. pr_warn("Found an x86 instruction decoder bug, "
  147. "please report this.\n", sym);
  148. pr_warn("%s", line);
  149. pr_warn("objdump says %d bytes, but insn_get_length() "
  150. "says %d\n", nb, insn.length);
  151. if (verbose)
  152. dump_insn(stderr, &insn);
  153. }
  154. }
  155. if (warnings)
  156. pr_warn("Decoded and checked %d instructions with %d "
  157. "failures\n", insns, warnings);
  158. else
  159. fprintf(stdout, "%s: success: Decoded and checked %d"
  160. " instructions\n", prog, insns);
  161. return 0;
  162. }