blob: b4ce069d5a1418f28444eaf84805a89d4207f9fb [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 if (operands.segment != X86_REG_INVALID)
51 System.out.println("\tSegment override: " + ins.regName(operands.segment));
danghvu6a6947f2013-11-26 22:28:41 -060052
53
danghvu4ef20d52013-12-16 23:25:57 -060054 System.out.printf("\tOpcode: %s\n", array2hex(operands.opcode));
danghvu6a6947f2013-11-26 22:28:41 -060055
56 // print operand's size, address size, displacement size & immediate size
57 System.out.printf("\top_size: %d, addr_size: %d, disp_size: %d, imm_size: %d\n"
danghvu4ef20d52013-12-16 23:25:57 -060058 , operands.opSize, operands.addrSize, operands.dispSize, operands.immSize);
danghvu6a6947f2013-11-26 22:28:41 -060059
60 // print modRM byte
danghvu4ef20d52013-12-16 23:25:57 -060061 System.out.printf("\tmodrm: 0x%x\n", operands.modrm);
danghvu6a6947f2013-11-26 22:28:41 -060062
63 // print displacement value
danghvu4ef20d52013-12-16 23:25:57 -060064 System.out.printf("\tdisp: 0x%x\n", operands.disp);
danghvu6a6947f2013-11-26 22:28:41 -060065
66 // SIB is not available in 16-bit mode
danghvuf3ef6962013-11-27 21:41:17 -060067 if ( (cs.mode & Capstone.CS_MODE_16) == 0) {
danghvu35855b52013-11-26 22:42:30 -060068 // print SIB byte
danghvu4ef20d52013-12-16 23:25:57 -060069 System.out.printf("\tsib: 0x%x\n", operands.sib);
70 if (operands.sib != 0)
danghvuf3ef6962013-11-27 21:41:17 -060071 System.out.printf("\tsib_index: %s, sib_scale: %d, sib_base: %s\n",
danghvu4ef20d52013-12-16 23:25:57 -060072 ins.regName(operands.sibIndex), operands.sibScale, ins.regName(operands.sibBase));
danghvuf3ef6962013-11-27 21:41:17 -060073 }
danghvu6a6947f2013-11-26 22:28:41 -060074
danghvu4ef20d52013-12-16 23:25:57 -060075 int count = ins.opCount(X86_OP_IMM);
danghvu6a6947f2013-11-26 22:28:41 -060076 if (count > 0) {
77 System.out.printf("\timm_count: %d\n", count);
78 for (int i=0; i<count; i++) {
danghvu4ef20d52013-12-16 23:25:57 -060079 int index = ins.opIndex(X86_OP_IMM, i + 1);
80 System.out.printf("\t\timms[%d]: 0x%x\n", i+1, (operands.op[index].value.imm));
danghvu6a6947f2013-11-26 22:28:41 -060081 }
82 }
83
danghvu4ef20d52013-12-16 23:25:57 -060084 if (operands.op.length != 0) {
85 System.out.printf("\top_count: %d\n", operands.op.length);
86 for (int c=0; c<operands.op.length; c++) {
87 X86.Operand i = (X86.Operand) operands.op[c];
danghvu6a6947f2013-11-26 22:28:41 -060088 String imm = hex(i.value.imm);
danghvu2f666882013-12-01 13:32:16 -060089 if (i.type == X86_OP_REG)
danghvu4ef20d52013-12-16 23:25:57 -060090 System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
danghvu2f666882013-12-01 13:32:16 -060091 if (i.type == X86_OP_IMM)
danghvu7b088042013-11-27 10:58:31 -060092 System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
danghvu2f666882013-12-01 13:32:16 -060093 if (i.type == X86_OP_FP)
danghvu35855b52013-11-26 22:42:30 -060094 System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
danghvu2f666882013-12-01 13:32:16 -060095 if (i.type == X86_OP_MEM) {
danghvu6a6947f2013-11-26 22:28:41 -060096 System.out.printf("\t\toperands[%d].type: MEM\n",c);
danghvu4ef20d52013-12-16 23:25:57 -060097 String base = ins.regName(i.value.mem.base);
98 String index = ins.regName(i.value.mem.index);
danghvu6a6947f2013-11-26 22:28:41 -060099 if (base != null)
100 System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
101 if (index != null)
102 System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index);
103 if (i.value.mem.scale != 1)
danghvu7b088042013-11-27 10:58:31 -0600104 System.out.printf("\t\t\toperands[%d].mem.scale: %d\n", c, i.value.mem.scale);
danghvu6a6947f2013-11-26 22:28:41 -0600105 if (i.value.mem.disp != 0)
danghvu7b088042013-11-27 10:58:31 -0600106 System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, i.value.mem.disp);
danghvu6a6947f2013-11-26 22:28:41 -0600107 }
108 }
109 }
110 }
111
112 public static void main(String argv[]) {
113
114 final Test.platform[] all_tests = {
115 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_16, hexString2Byte(X86_CODE16), "X86 16bit (Intel syntax)"),
danghvu05006912013-12-05 19:33:38 -0600116 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 -0600117 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, hexString2Byte(X86_CODE32), "X86 32 (Intel syntax)"),
118 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_64, hexString2Byte(X86_CODE64), "X86 64 (Intel syntax)"),
119 };
120
121 for (int i=0; i<all_tests.length; i++) {
122 Test.platform test = all_tests[i];
danghvuf3ef6962013-11-27 21:41:17 -0600123 System.out.println(new String(new char[16]).replace("\0", "*"));
danghvu6a6947f2013-11-26 22:28:41 -0600124 System.out.println("Platform: " + test.comment);
danghvuf3ef6962013-11-27 21:41:17 -0600125 System.out.println("Code: " + Test.stringToHex(test.code));
danghvu6a6947f2013-11-26 22:28:41 -0600126 System.out.println("Disasm:");
127
128 cs = new Capstone(test.arch, test.mode);
Nguyen Anh Quynh6a1107c2014-01-07 23:47:18 +0800129 cs.setDetail(Capstone.CS_OPT_ON);
danghvu05006912013-12-05 19:33:38 -0600130 if (test.syntax != 0) {
131 cs.setSyntax(test.syntax);
132 }
danghvu4ef20d52013-12-16 23:25:57 -0600133 Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
danghvu6a6947f2013-11-26 22:28:41 -0600134
135 for (int j = 0; j < all_ins.length; j++) {
136 print_ins_detail(all_ins[j]);
137 System.out.println();
138 }
danghvuf3ef6962013-11-27 21:41:17 -0600139
140 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 -0600141 }
142 }
143
144}