blob: be77a6369c908f5b2a0c9deb26dc6ac57c98a983 [file] [log] [blame]
danghvu6a6947f2013-11-26 22:28:41 -06001// Capstone Java binding
2// By Nguyen Anh Quynh & Dang Hoang Vu, 2013
3
danghvuf86a7d52013-11-27 15:09:07 -06004import capstone.Capstone;
5import capstone.X86;
6
danghvu2f666882013-12-01 13:32:16 -06007import static capstone.X86_const.*;
8
danghvu6a6947f2013-11-26 22:28:41 -06009public class TestX86 {
10
11 static byte[] hexString2Byte(String s) {
12 // from http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
13 int len = s.length();
14 byte[] data = new byte[len / 2];
15 for (int i = 0; i < len; i += 2) {
16 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
17 + Character.digit(s.charAt(i+1), 16));
18 }
19 return data;
20 }
21
danghvu6a6947f2013-11-26 22:28:41 -060022 static final String X86_CODE64 = "55488b05b8130000";
danghvu7b088042013-11-27 10:58:31 -060023 static final String X86_CODE16 = "8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
24 static final String X86_CODE32 = "8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
danghvu6a6947f2013-11-26 22:28:41 -060025
26 public static Capstone cs;
27
28 private static String hex(int i) {
29 return Integer.toString(i, 16);
30 }
31
32 private static String hex(long i) {
33 return Long.toString(i, 16);
34 }
35
36 private static String array2hex(byte[] arr) {
37 String ret = "";
38 for (int i=0 ;i<arr.length; i++)
39 ret += String.format("0x%02x ", arr[i]);
40 return ret;
41 }
42
danghvu4ef20d52013-12-16 23:25:57 -060043 public static void print_ins_detail(Capstone.CsInsn ins) {
44 System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
danghvu6a6947f2013-11-26 22:28:41 -060045
danghvu4ef20d52013-12-16 23:25:57 -060046 X86.OpInfo operands = (X86.OpInfo) ins.operands;
danghvu6a6947f2013-11-26 22:28:41 -060047
danghvu4ef20d52013-12-16 23:25:57 -060048 System.out.printf("\tPrefix: %s\n", array2hex(operands.prefix));
danghvu6a6947f2013-11-26 22:28:41 -060049
danghvu4ef20d52013-12-16 23:25:57 -060050 System.out.printf("\tOpcode: %s\n", array2hex(operands.opcode));
danghvu6a6947f2013-11-26 22:28:41 -060051
Nguyen Anh Quynhe68ce0e2014-06-18 12:33:39 +080052 // print address size
53 System.out.printf("\taddr_size: %d\n", operands.addrSize);
danghvu6a6947f2013-11-26 22:28:41 -060054
55 // print modRM byte
danghvu4ef20d52013-12-16 23:25:57 -060056 System.out.printf("\tmodrm: 0x%x\n", operands.modrm);
danghvu6a6947f2013-11-26 22:28:41 -060057
58 // print displacement value
danghvu4ef20d52013-12-16 23:25:57 -060059 System.out.printf("\tdisp: 0x%x\n", operands.disp);
danghvu6a6947f2013-11-26 22:28:41 -060060
61 // SIB is not available in 16-bit mode
danghvuf3ef6962013-11-27 21:41:17 -060062 if ( (cs.mode & Capstone.CS_MODE_16) == 0) {
danghvu35855b52013-11-26 22:42:30 -060063 // print SIB byte
danghvu4ef20d52013-12-16 23:25:57 -060064 System.out.printf("\tsib: 0x%x\n", operands.sib);
65 if (operands.sib != 0)
danghvuf3ef6962013-11-27 21:41:17 -060066 System.out.printf("\tsib_index: %s, sib_scale: %d, sib_base: %s\n",
danghvu4ef20d52013-12-16 23:25:57 -060067 ins.regName(operands.sibIndex), operands.sibScale, ins.regName(operands.sibBase));
danghvuf3ef6962013-11-27 21:41:17 -060068 }
danghvu6a6947f2013-11-26 22:28:41 -060069
danghvu4ef20d52013-12-16 23:25:57 -060070 int count = ins.opCount(X86_OP_IMM);
danghvu6a6947f2013-11-26 22:28:41 -060071 if (count > 0) {
72 System.out.printf("\timm_count: %d\n", count);
73 for (int i=0; i<count; i++) {
danghvu4ef20d52013-12-16 23:25:57 -060074 int index = ins.opIndex(X86_OP_IMM, i + 1);
75 System.out.printf("\t\timms[%d]: 0x%x\n", i+1, (operands.op[index].value.imm));
danghvu6a6947f2013-11-26 22:28:41 -060076 }
77 }
78
danghvu4ef20d52013-12-16 23:25:57 -060079 if (operands.op.length != 0) {
80 System.out.printf("\top_count: %d\n", operands.op.length);
81 for (int c=0; c<operands.op.length; c++) {
82 X86.Operand i = (X86.Operand) operands.op[c];
danghvu6a6947f2013-11-26 22:28:41 -060083 String imm = hex(i.value.imm);
danghvu2f666882013-12-01 13:32:16 -060084 if (i.type == X86_OP_REG)
danghvu4ef20d52013-12-16 23:25:57 -060085 System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
danghvu2f666882013-12-01 13:32:16 -060086 if (i.type == X86_OP_IMM)
danghvu7b088042013-11-27 10:58:31 -060087 System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
danghvu2f666882013-12-01 13:32:16 -060088 if (i.type == X86_OP_FP)
danghvu35855b52013-11-26 22:42:30 -060089 System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
danghvu2f666882013-12-01 13:32:16 -060090 if (i.type == X86_OP_MEM) {
danghvu6a6947f2013-11-26 22:28:41 -060091 System.out.printf("\t\toperands[%d].type: MEM\n",c);
Nguyen Anh Quynh04678422014-06-24 14:35:47 +080092 String segment = ins.regName(i.value.mem.segment);
danghvu4ef20d52013-12-16 23:25:57 -060093 String base = ins.regName(i.value.mem.base);
94 String index = ins.regName(i.value.mem.index);
Nguyen Anh Quynh04678422014-06-24 14:35:47 +080095 if (segment != null)
96 System.out.printf("\t\t\toperands[%d].mem.segment: REG = %s\n", c, segment);
danghvu6a6947f2013-11-26 22:28:41 -060097 if (base != null)
98 System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
99 if (index != null)
100 System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index);
101 if (i.value.mem.scale != 1)
danghvu7b088042013-11-27 10:58:31 -0600102 System.out.printf("\t\t\toperands[%d].mem.scale: %d\n", c, i.value.mem.scale);
danghvu6a6947f2013-11-26 22:28:41 -0600103 if (i.value.mem.disp != 0)
danghvu7b088042013-11-27 10:58:31 -0600104 System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, i.value.mem.disp);
danghvu6a6947f2013-11-26 22:28:41 -0600105 }
Nguyen Anh Quynh83800cd2014-06-18 14:21:36 +0800106
107 // Operand size is irrlevant for X86_OP_IMM operand
Nguyen Anh Quynhe68ce0e2014-06-18 12:33:39 +0800108 if (i.type != X86_OP_IMM) {
109 System.out.printf("\t\toperands[%d].size: %d\n",c, i.size);
110 }
danghvu6a6947f2013-11-26 22:28:41 -0600111 }
112 }
113 }
114
115 public static void main(String argv[]) {
116
117 final Test.platform[] all_tests = {
118 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_16, hexString2Byte(X86_CODE16), "X86 16bit (Intel syntax)"),
danghvu05006912013-12-05 19:33:38 -0600119 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, Capstone.CS_OPT_SYNTAX_ATT, hexString2Byte(X86_CODE32), "X86 32 (AT&T syntax)"),
danghvu6a6947f2013-11-26 22:28:41 -0600120 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, hexString2Byte(X86_CODE32), "X86 32 (Intel syntax)"),
121 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_64, hexString2Byte(X86_CODE64), "X86 64 (Intel syntax)"),
122 };
123
124 for (int i=0; i<all_tests.length; i++) {
125 Test.platform test = all_tests[i];
danghvuf3ef6962013-11-27 21:41:17 -0600126 System.out.println(new String(new char[16]).replace("\0", "*"));
danghvu6a6947f2013-11-26 22:28:41 -0600127 System.out.println("Platform: " + test.comment);
danghvuf3ef6962013-11-27 21:41:17 -0600128 System.out.println("Code: " + Test.stringToHex(test.code));
danghvu6a6947f2013-11-26 22:28:41 -0600129 System.out.println("Disasm:");
130
131 cs = new Capstone(test.arch, test.mode);
Nguyen Anh Quynh6a1107c2014-01-07 23:47:18 +0800132 cs.setDetail(Capstone.CS_OPT_ON);
danghvu05006912013-12-05 19:33:38 -0600133 if (test.syntax != 0) {
134 cs.setSyntax(test.syntax);
135 }
danghvu4ef20d52013-12-16 23:25:57 -0600136 Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
danghvu6a6947f2013-11-26 22:28:41 -0600137
138 for (int j = 0; j < all_ins.length; j++) {
139 print_ins_detail(all_ins[j]);
140 System.out.println();
141 }
danghvuf3ef6962013-11-27 21:41:17 -0600142
143 System.out.printf("0x%x:\n\n", all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size);
danghvu6a6947f2013-11-26 22:28:41 -0600144 }
145 }
146
147}