1
0

testdisa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. testdisa.c
  5. Abstract:
  6. This program tests the disassembler by feeding it instructions as input.
  7. Author:
  8. Evan Green 21-Jun-2012
  9. Environment:
  10. Development
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/lib/types.h>
  16. #include <minoca/lib/status.h>
  17. #include <minoca/lib/im.h>
  18. #include "../disasm.h"
  19. #include <assert.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. #define MALLOC malloc
  28. #define FREE free
  29. //
  30. // ----------------------------------------------- Internal Function Prototypes
  31. //
  32. ULONG
  33. DbgpPrintAddress (
  34. PDISASSEMBLED_INSTRUCTION Instruction,
  35. BOOL Print
  36. );
  37. LONG
  38. DbgpGetFileSize (
  39. FILE *File
  40. );
  41. //
  42. // -------------------------------------------------------------------- Globals
  43. //
  44. //
  45. // ------------------------------------------------------ Data Type Definitions
  46. //
  47. //
  48. // ------------------------------------------------------------------ Functions
  49. //
  50. INT
  51. main (
  52. INT ArgumentCount,
  53. CHAR **Arguments
  54. )
  55. /*++
  56. Routine Description:
  57. This routine is the main entry point for the program. It collects the
  58. options passed to it, and invokes the disassembler.
  59. Arguments:
  60. ArgumentCount - Supplies the number of command line arguments the program
  61. was invoked with.
  62. Arguments - Supplies a tokenized array of command line arguments.
  63. Return Value:
  64. Returns an integer exit code. 0 for success, nonzero otherwise.
  65. --*/
  66. {
  67. ULONG ArmInstruction;
  68. ULONG BytesDisassembled;
  69. ULONG BytesRead;
  70. PUCHAR CurrentInstruction;
  71. DISASSEMBLED_INSTRUCTION Disassembly;
  72. CHAR DisassemblyBuffer[1024];
  73. ULONG Failures;
  74. FILE *File;
  75. PVOID FileBuffer;
  76. PSTR Filename;
  77. LONG FileSize;
  78. BOOL ForceThumb;
  79. IMAGE_BUFFER ImageBuffer;
  80. IMAGE_INFORMATION ImageInformation;
  81. PUCHAR InstructionStream;
  82. MACHINE_LANGUAGE Language;
  83. PSTR LanguageString;
  84. BOOL PrintDisassembly;
  85. BOOL Result;
  86. KSTATUS Status;
  87. ULONG TextSize;
  88. Failures = 0;
  89. FileBuffer = NULL;
  90. ForceThumb = FALSE;
  91. memset(&ImageBuffer, 0, sizeof(IMAGE_BUFFER));
  92. InstructionStream = NULL;
  93. PrintDisassembly = TRUE;
  94. if (ArgumentCount < 2) {
  95. printf("Usage: testdisa [-q] [-t] <file>\n"
  96. "Options:\n"
  97. " -q Quiet. Don't print disassembly, only errors.\n"
  98. " -t Force thumb mode. Only applies to ARM images.\n");
  99. return 1;
  100. }
  101. while (TRUE) {
  102. if (strcasecmp(Arguments[1], "-q") == 0) {
  103. PrintDisassembly = FALSE;
  104. Arguments += 1;
  105. } else if (strcasecmp(Arguments[1], "-t") == 0) {
  106. ForceThumb = TRUE;
  107. Arguments += 1;
  108. } else {
  109. break;
  110. }
  111. }
  112. //
  113. // Determine the file size and load the file into memory.
  114. //
  115. Filename = Arguments[1];
  116. File = fopen(Filename, "rb");
  117. if (File == NULL) {
  118. Result = FALSE;
  119. Failures += 1;
  120. goto MainEnd;
  121. }
  122. FileSize = DbgpGetFileSize(File);
  123. if (FileSize <= 0) {
  124. Result = FALSE;
  125. Failures += 1;
  126. goto MainEnd;
  127. }
  128. FileBuffer = MALLOC(FileSize);
  129. if (FileBuffer == NULL) {
  130. Result = FALSE;
  131. Failures += 1;
  132. goto MainEnd;
  133. }
  134. BytesRead = fread(FileBuffer, 1, FileSize, File);
  135. if (BytesRead != FileSize) {
  136. Result = FALSE;
  137. Failures += 1;
  138. goto MainEnd;
  139. }
  140. ImageBuffer.Data = FileBuffer;
  141. ImageBuffer.Size = FileSize;
  142. Status = ImGetImageInformation(&ImageBuffer, &ImageInformation);
  143. if (!KSUCCESS(Status)) {
  144. Result = FALSE;
  145. Failures += 1;
  146. goto MainEnd;
  147. }
  148. //
  149. // Get the text section.
  150. //
  151. Result = ImGetImageSection(&ImageBuffer,
  152. ".text",
  153. (PVOID *)&InstructionStream,
  154. NULL,
  155. &TextSize,
  156. NULL);
  157. if (Result == FALSE) {
  158. printf("Error: Could not load text section for file %s.\n", Filename);
  159. Failures += 1;
  160. goto MainEnd;
  161. }
  162. //
  163. // Determine the machine language.
  164. //
  165. Language = MachineLanguageInvalid;
  166. LanguageString = "Unknown";
  167. switch (ImageInformation.Machine) {
  168. case ImageMachineTypeX86:
  169. Language = MachineLanguageX86;
  170. LanguageString = "x86";
  171. break;
  172. case ImageMachineTypeArm32:
  173. Language = MachineLanguageArm;
  174. LanguageString = "ARM";
  175. if (((ImageInformation.EntryPoint & 0x1) != 0) ||
  176. (ForceThumb != FALSE)) {
  177. Language = MachineLanguageThumb2;
  178. LanguageString = "Thumb2";
  179. }
  180. break;
  181. default:
  182. printf("Unknown machine type %d!\n", ImageInformation.Machine);
  183. Failures += 1;
  184. goto MainEnd;
  185. }
  186. if (PrintDisassembly != FALSE) {
  187. printf("Disassembling %s (%s), %d bytes.\n",
  188. Filename,
  189. LanguageString,
  190. TextSize);
  191. }
  192. //
  193. // Disassemble the file contents.
  194. //
  195. BytesDisassembled = 0;
  196. CurrentInstruction = InstructionStream;
  197. while (BytesDisassembled < TextSize) {
  198. //
  199. // Print the offset from the start of disassembly and disassemble the
  200. // instruction.
  201. //
  202. if (PrintDisassembly != FALSE) {
  203. printf("\n%04x: ", BytesDisassembled);
  204. }
  205. Result = DbgDisassemble((UINTN)CurrentInstruction,
  206. CurrentInstruction,
  207. DisassemblyBuffer,
  208. sizeof(DisassemblyBuffer),
  209. &Disassembly,
  210. Language);
  211. if (Result == FALSE) {
  212. Failures += 1;
  213. printf("ERROR decoding instruction, partial string: ");
  214. DisassemblyBuffer[99] = '\0';
  215. printf(DisassemblyBuffer);
  216. goto MainEnd;
  217. }
  218. //
  219. // For ARM, print the binary code first, since it's always a pretty
  220. // consistent size.
  221. //
  222. if (Language == MachineLanguageArm) {
  223. if (Disassembly.BinaryLength != 4) {
  224. printf("Error: got %d byte ARM disassembly.\n",
  225. Disassembly.BinaryLength);
  226. Failures += 1;
  227. }
  228. ArmInstruction = *((PULONG)CurrentInstruction);
  229. CurrentInstruction += Disassembly.BinaryLength;
  230. BytesDisassembled += Disassembly.BinaryLength;
  231. if (PrintDisassembly != FALSE) {
  232. printf("%08x ", ArmInstruction);
  233. }
  234. } else if (Language == MachineLanguageThumb2) {
  235. ArmInstruction = *((PUSHORT)CurrentInstruction);
  236. if (PrintDisassembly != FALSE) {
  237. printf(" %04x", ArmInstruction);
  238. }
  239. if (Disassembly.BinaryLength == 4) {
  240. ArmInstruction = *(((PUSHORT)CurrentInstruction) + 1);
  241. if (PrintDisassembly != FALSE) {
  242. printf("%04x ", ArmInstruction);
  243. }
  244. } else if (Disassembly.BinaryLength == 2) {
  245. if (PrintDisassembly != FALSE) {
  246. printf(" ");
  247. }
  248. } else if (Disassembly.BinaryLength != 2) {
  249. printf("Error: Got %d byte Thumb-2 disassembly.\n",
  250. Disassembly.BinaryLength);
  251. Failures += 1;
  252. ArmInstruction = *((PULONG)CurrentInstruction);
  253. }
  254. CurrentInstruction += Disassembly.BinaryLength;
  255. BytesDisassembled += Disassembly.BinaryLength;
  256. }
  257. //
  258. // Print the mnemonic, which should exist in any case.
  259. //
  260. if (Disassembly.Mnemonic == NULL) {
  261. printf("Error: NULL opcode mnemonic.\n");
  262. Failures += 1;
  263. }
  264. if (PrintDisassembly != FALSE) {
  265. printf("%s\t", Disassembly.Mnemonic);
  266. }
  267. //
  268. // Attempt to print the first (destination) operand. If the operand
  269. // is an address, print that as well.
  270. //
  271. if (Disassembly.DestinationOperand != NULL) {
  272. if (strcasecmp(Disassembly.DestinationOperand, "err") == 0) {
  273. printf("Error: got ERR destination operand!\n");
  274. Failures += 1;
  275. }
  276. if (PrintDisassembly != FALSE) {
  277. printf("%s", Disassembly.DestinationOperand);
  278. }
  279. if (Disassembly.AddressIsDestination != FALSE) {
  280. if (DbgpPrintAddress(&Disassembly, PrintDisassembly) != 0) {
  281. printf("Error: Invalid operand address.\n");
  282. Failures += 1;
  283. }
  284. }
  285. //
  286. // Attempt to print the second (source) operand. If the operand is
  287. // an address, print that as well.
  288. //
  289. if (Disassembly.SourceOperand != NULL) {
  290. if (strcasecmp(Disassembly.DestinationOperand, "err") == 0) {
  291. printf("Error: got ERR source operand!\n");
  292. Failures += 1;
  293. }
  294. if (PrintDisassembly != FALSE) {
  295. printf(", %s", Disassembly.SourceOperand);
  296. }
  297. if (Disassembly.AddressIsDestination == FALSE) {
  298. if (DbgpPrintAddress(&Disassembly, PrintDisassembly) != 0) {
  299. printf("Error: Invalid operand address.\n");
  300. Failures += 1;
  301. }
  302. }
  303. //
  304. // Attempt to print the third operand. This operand only exists
  305. // in rare circumstances on x86, and can never be an address.
  306. // On ARM, third and fourth operands are the norm.
  307. //
  308. if (Disassembly.ThirdOperand != NULL) {
  309. if (strcasecmp(Disassembly.ThirdOperand, "err") == 0) {
  310. printf("Error: got ERR source operand!\n");
  311. Failures += 1;
  312. }
  313. if (PrintDisassembly != FALSE) {
  314. printf(", %s", Disassembly.ThirdOperand);
  315. }
  316. //
  317. // Print the fourth operand, which will only ever be set
  318. // on ARM.
  319. //
  320. if ((Disassembly.FourthOperand != NULL) &&
  321. (PrintDisassembly != FALSE)) {
  322. printf(", %s", Disassembly.FourthOperand);
  323. }
  324. //
  325. // If the third operand wasn't present, a fourth better not be
  326. // either.
  327. //
  328. } else if (Disassembly.FourthOperand != NULL) {
  329. printf("Error: Got fourth operand but no third!\n");
  330. Failures += 1;
  331. }
  332. } else {
  333. //
  334. // If there was no second operand, there should definitely be
  335. // no third or fourth operand.
  336. //
  337. if ((Disassembly.ThirdOperand != NULL) ||
  338. (Disassembly.FourthOperand != NULL)) {
  339. printf("Error: Got third/fourth operands but no second "
  340. "operand!\n");
  341. Failures += 1;
  342. }
  343. }
  344. } else {
  345. //
  346. // If there was no first operand, there should definitely be no
  347. // second, third, or fourth operand.
  348. //
  349. if ((Disassembly.SourceOperand != NULL) ||
  350. (Disassembly.ThirdOperand != NULL) ||
  351. (Disassembly.FourthOperand != NULL)) {
  352. printf("Error: Got second/third/fourth operand, but no "
  353. "first!\n");
  354. Failures += 1;
  355. }
  356. }
  357. //
  358. // Print the binary contents for x86 disassembly.
  359. //
  360. if (Language == MachineLanguageX86) {
  361. if (Disassembly.BinaryLength == 0) {
  362. printf("Error: got a zero length instruction\n");
  363. Failures += 1;
  364. goto MainEnd;
  365. }
  366. if (PrintDisassembly != FALSE) {
  367. printf(" \t; ");
  368. }
  369. while (Disassembly.BinaryLength != 0) {
  370. if (PrintDisassembly != FALSE) {
  371. printf("%02x", *CurrentInstruction);
  372. }
  373. CurrentInstruction += 1;
  374. BytesDisassembled += 1;
  375. Disassembly.BinaryLength -= 1;
  376. }
  377. }
  378. }
  379. if (PrintDisassembly != FALSE) {
  380. printf("\n");
  381. }
  382. MainEnd:
  383. if (FileBuffer != NULL) {
  384. FREE(FileBuffer);
  385. }
  386. if (Failures != 0) {
  387. printf("\n*** %d Failures in disassembly test for file %s! ***\n",
  388. Failures,
  389. Filename);
  390. return 1;
  391. } else {
  392. printf("All disassembler tests passed for file %s.\n", Filename);
  393. }
  394. return 0;
  395. }
  396. //
  397. // --------------------------------------------------------- Internal Functions
  398. //
  399. ULONG
  400. DbgpPrintAddress (
  401. PDISASSEMBLED_INSTRUCTION Instruction,
  402. BOOL Print
  403. )
  404. /*++
  405. Routine Description:
  406. This routine prints an address encoded in a disassembled instruction.
  407. Arguments:
  408. Instruction - Supplies a pointer to the instruction containing the address
  409. to decode.
  410. Print - Supplies a boolean indicating if the value should
  411. actually be printed.
  412. Return Value:
  413. Returns 0 on success, or 1 on failure.
  414. --*/
  415. {
  416. if (Instruction->AddressIsValid == FALSE) {
  417. return 0;
  418. }
  419. if (Print != FALSE) {
  420. printf(" (0x%08I64x)", Instruction->OperandAddress);
  421. }
  422. return 0;
  423. }
  424. LONG
  425. DbgpGetFileSize (
  426. FILE *File
  427. )
  428. /*++
  429. Routine Description:
  430. This routine determines the size of an opened file.
  431. Arguments:
  432. File - Supplies the file handle.
  433. Return Value:
  434. Returns the file length.
  435. --*/
  436. {
  437. INT CurrentPosition;
  438. LONG FileSize;
  439. CurrentPosition = ftell(File);
  440. fseek(File, 0, SEEK_END);
  441. FileSize = ftell(File);
  442. fseek(File, CurrentPosition, SEEK_SET);
  443. return FileSize;
  444. }