insn_decoder_test.c 4.1 KB

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