diff --git a/bindings/Makefile b/bindings/Makefile new file mode 100644 index 0000000000..5794ac1afc --- /dev/null +++ b/bindings/Makefile @@ -0,0 +1,46 @@ +TMPDIR = /tmp/capstone_test + +DIFF = diff -u -w + +TEST = $(TMPDIR)/test +TEST_ARM = $(TMPDIR)/test_arm +TEST_ARM64 = $(TMPDIR)/test_arm64 +TEST_MIPS = $(TMPDIR)/test_mips +TEST_X86 = $(TMPDIR)/test_x86 + +all: expected python java #oclma ruby + +expected: + $(MAKE) -C ../tests + mkdir -p $(TMPDIR) + ../tests/test > $(TEST)_e + ../tests/test_arm > $(TEST_ARM)_e + ../tests/test_arm64 > $(TEST_ARM64)_e + ../tests/test_mips > $(TEST_MIPS)_e + ../tests/test_x86 > $(TEST_X86)_e + +python: FORCE + python python/test.py > $(TEST)_o + python python/test_arm.py > $(TEST_ARM)_o + python python/test_arm64.py > $(TEST_ARM64)_o + python python/test_mips.py > $(TEST_MIPS)_o + python python/test_x86.py > $(TEST_X86)_o + $(MAKE) test + +java: FORCE + $(MAKE) -C java + cd java; ./run.sh > $(TEST)_o + cd java; ./run.sh arm > $(TEST_ARM)_o + cd java; ./run.sh arm64 > $(TEST_ARM64)_o + cd java; ./run.sh mips > $(TEST_MIPS)_o + cd java; ./run.sh x86 > $(TEST_X86)_o + $(MAKE) test + +test: FORCE + $(DIFF) $(TEST)_e $(TEST)_o + $(DIFF) $(TEST_ARM)_e $(TEST_ARM)_o + $(DIFF) $(TEST_ARM64)_e $(TEST_ARM64)_o + $(DIFF) $(TEST_MIPS)_e $(TEST_MIPS)_o + $(DIFF) $(TEST_X86)_e $(TEST_X86)_o + +FORCE: diff --git a/bindings/java/Makefile b/bindings/java/Makefile index 6aad81662d..e87d356092 100644 --- a/bindings/java/Makefile +++ b/bindings/java/Makefile @@ -3,8 +3,28 @@ JNA = /usr/share/java/jna/jna.jar -all: - javac -classpath $(JNA) CS.java Arm.java Arm64.java Mips.java X86.java Test.java TestArm.java TestArm64.java TestMips.java TestX86.java +ifneq ($(wildcard $(JNA)),) +else + ifneq ($(wildcard /usr/share/java/jna.jar),) + JNA = /usr/share/java/jna.jar + else + JNA = + $(error Unable to find jna.jar) + endif +endif + +CAPSTONE_JAVA = Capstone.java Arm.java Arm64.java Mips.java X86.java + +all: capstone tests + +capstone: capstone_class + jar cf capstone.jar capstone/*.class + +capstone_class: + cd capstone; javac -classpath $(JNA) $(CAPSTONE_JAVA) + +tests: + javac -classpath "$(JNA):capstone.jar" Test.java TestArm.java TestArm64.java TestMips.java TestX86.java clean: - rm -rf *.class *.log + rm -rf *.class *.log *.jar diff --git a/bindings/java/Test.java b/bindings/java/Test.java index d485b29cbc..ec5a6f7202 100644 --- a/bindings/java/Test.java +++ b/bindings/java/Test.java @@ -5,6 +5,8 @@ import com.sun.jna.Memory; import com.sun.jna.Pointer; +import capstone.Capstone; + public class Test { public static class platform { public int arch; @@ -20,7 +22,7 @@ public platform(int a, int m, byte[] c, String s) { } }; - static String stringToHex(byte[] code) { + static public String stringToHex(byte[] code) { StringBuilder buf = new StringBuilder(200); for (byte ch: code) { if (buf.length() > 0) @@ -48,7 +50,7 @@ static public void main(String argv[]) { Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, new byte[] { (byte)0x8d, 0x4c, 0x32, 0x08, 0x01, (byte)0xd8, (byte)0x81, (byte)0xc6, 0x34, 0x12, 0x00, 0x00 }, - "X86 32bit (Intel syntax)" + "X86 32 (Intel syntax)" ), new platform( Capstone.CS_ARCH_X86, @@ -102,18 +104,20 @@ static public void main(String argv[]) { }; for (int j = 0; j < platforms.length; j++) { - System.out.println("************"); + System.out.println("****************"); System.out.println(String.format("Platform: %s", platforms[j].comment)); System.out.println(String.format("Code: %s", stringToHex(platforms[j].code))); + System.out.println("Disasm:"); Capstone cs = new Capstone(platforms[j].arch, platforms[j].mode); Capstone.cs_insn[] all_insn = cs.disasm(platforms[j].code, 0x1000); for (int i = 0; i < all_insn.length; i++) { - System.out.println(String.format("0x%x\t%s\t%s", all_insn[i].address, + System.out.println(String.format("0x%x: \t%s\t%s", all_insn[i].address, all_insn[i].mnemonic, all_insn[i].operands)); + /* if (all_insn[i].regs_read[0] != 0) { System.out.print("\tRegister read: "); for(int k = 0; k < all_insn[i].regs_read.length; k++) { @@ -142,8 +146,9 @@ static public void main(String argv[]) { System.out.print(String.format("%d ", all_insn[i].groups[k])); } System.out.println(); - } + }*/ } + System.out.printf("0x%x:\n\n", all_insn[all_insn.length-1].address + all_insn[all_insn.length-1].size); } } } diff --git a/bindings/java/TestArm.java b/bindings/java/TestArm.java index f095038851..2e7d20038e 100644 --- a/bindings/java/TestArm.java +++ b/bindings/java/TestArm.java @@ -5,6 +5,9 @@ import com.sun.jna.Memory; import com.sun.jna.Pointer; +import capstone.Capstone; +import capstone.Arm; + public class TestArm { static byte[] hexString2Byte(String s) { @@ -38,31 +41,19 @@ public static void print_ins_detail(Capstone.cs_insn ins) { Arm.OpInfo op_info = (Arm.OpInfo) ins.op_info; - if (op_info.cc != Arm.ARM_CC_AL && op_info.cc != Arm.ARM_CC_INVALID){ - System.out.printf("\tCode condition: %d\n", op_info.cc); - } - - if (op_info.update_flags) { - System.out.println("\tUpdate-flags: True"); - } - - if (op_info.writeback) { - System.out.println("\tWriteback: True"); - } - if (op_info.op != null) { System.out.printf("\top_count: %d\n", op_info.op.length); - for (int c=1; c 0) System.out.printf("\t\t\tShift: type = %d, value = %d\n", i.shift.type, i.shift.value); } + if (op_info.writeback) + System.out.println("\tWrite-back: True"); + + if (op_info.update_flags) + System.out.println("\tUpdate-flags: True"); + + if (op_info.cc != Arm.ARM_CC_AL && op_info.cc != Arm.ARM_CC_INVALID) + System.out.printf("\tCode condition: %d\n", op_info.cc); } } @@ -95,8 +94,9 @@ public static void main(String argv[]) { for (int i=0; i 0) System.out.printf("\t\t\tShift: type = %d, value = %d\n", i.shift.type, i.shift.value); @@ -77,6 +68,16 @@ public static void print_ins_detail(Capstone.cs_insn ins) { System.out.printf("\t\t\tExt: %d\n", i.ext); } } + + if (op_info.writeback) + System.out.println("\tWrite-back: True"); + + if (op_info.update_flags) + System.out.println("\tUpdate-flags: True"); + + if (op_info.cc != Arm64.ARM64_CC_AL && op_info.cc != Arm64.ARM64_CC_INVALID) + System.out.printf("\tCode condition: %d\n", op_info.cc); + } public static void main(String argv[]) { @@ -87,17 +88,20 @@ public static void main(String argv[]) { for (int i=0; i 0) { System.out.printf("\timm_count: %d\n", count); for (int i=0; i 0); writeback = (op_info._writeback > 0); if (op_info.op_count == 0) return; - op = new Operand[op_info.op_count]; - for (int i=0; iq", s).encode('hex') + while x[0] == '0': x = x[1:] + return x ### Test class cs def test_class(): @@ -24,24 +33,14 @@ def print_insn_detail(insn): # print address, mnemonic and operands print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str)) - if not insn.cc in [ARM_CC_AL, ARM_CC_INVALID]: - print("\tCode condition: %u" %insn.cc) - - if insn.update_flags: - print("\tUpdate-flags: True") - - if insn.writeback: - print("\tWriteback: True") - if len(insn.operands) > 0: print("\top_count: %u" %len(insn.operands)) c = 0 for i in insn.operands: - c += 1 if i.type == ARM_OP_REG: print("\t\toperands[%u].type: REG = %s" %(c, insn.reg_name(i.value.reg))) if i.type == ARM_OP_IMM: - print("\t\toperands[%u].type: IMM = %x" %(c, i.value.imm)) + print("\t\toperands[%u].type: IMM = 0x%s" %(c, to_x(i.value.imm))) if i.type == ARM_OP_PIMM: print("\t\toperands[%u].type: P-IMM = %u" %(c, i.value.imm)) if i.type == ARM_OP_CIMM: @@ -60,24 +59,35 @@ def print_insn_detail(insn): print("\t\t\toperands[%u].mem.scale: %u" \ %(c, i.value.mem.scale)) if i.value.mem.disp != 0: - print("\t\t\toperands[%u].mem.disp: %x" \ - %(c, i.value.mem.disp)) + print("\t\t\toperands[%u].mem.disp: 0x%s" \ + %(c, to_x(i.value.mem.disp))) if i.shift.type != ARM_SFT_INVALID and i.shift.value: print("\t\t\tShift: type = %u, value = %u\n" \ %(i.shift.type, i.shift.value)) + c+=1 + if insn.update_flags: + print("\tUpdate-flags: True") + if insn.writeback: + print("\tWrite-back: True") + if not insn.cc in [ARM_CC_AL, ARM_CC_INVALID]: + print("\tCode condition: %u" %insn.cc) for (arch, mode, code, comment) in all_tests: - print("*" * 30) + print("*" * 16) print("Platform: %s" %comment) + print("Code: %s" % to_hex(code)) print("Disasm:") - + try: md = cs(arch, mode) + last = None for insn in md.disasm(code, 0x1000): print_insn_detail(insn) + last = insn print + print "0x%x:\n" % (last.address + last.size) except: print("ERROR: Arch or mode unsupported!") diff --git a/bindings/python/test_arm64.py b/bindings/python/test_arm64.py index 4cb5f1e269..dada4d687f 100755 --- a/bindings/python/test_arm64.py +++ b/bindings/python/test_arm64.py @@ -11,6 +11,15 @@ (CS_ARCH_ARM64, CS_MODE_ARM, ARM64_CODE, "ARM-64"), ) +def to_hex(s): + return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK + +def to_x(s): + from struct import pack + if not s: return '0' + x = pack(">q", s).encode('hex') + while x[0] == '0': x = x[1:] + return x ### Test class cs def test_class(): @@ -18,24 +27,15 @@ def print_insn_detail(insn): # print address, mnemonic and operands print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str)) - if not insn.cc in [ARM64_CC_AL, ARM64_CC_INVALID]: - print("\tCode condition: %u" %insn.cc) - - if insn.update_flags: - print("\tUpdate-flags: True") - - if insn.writeback: - print("\tWrite-back: True") - if len(insn.operands) > 0: print("\top_count: %u" %len(insn.operands)) - c = 0 + c = -1 for i in insn.operands: c += 1 if i.type == ARM64_OP_REG: print("\t\toperands[%u].type: REG = %s" %(c, insn.reg_name(i.value.reg))) if i.type == ARM64_OP_IMM: - print("\t\toperands[%u].type: IMM = %x" %(c, i.value.imm)) + print("\t\toperands[%u].type: IMM = 0x%s" %(c, to_x(i.value.imm))) if i.type == ARM64_OP_CIMM: print("\t\toperands[%u].type: C-IMM = %u" %(c, i.value.imm)) if i.type == ARM64_OP_FP: @@ -49,8 +49,8 @@ def print_insn_detail(insn): print("\t\t\toperands[%u].mem.index: REG = %s" \ %(c, insn.reg_name(i.value.mem.index))) if i.value.mem.disp != 0: - print("\t\t\toperands[%u].mem.disp: %x" \ - %(c, i.value.mem.disp)) + print("\t\t\toperands[%u].mem.disp: 0x%s" \ + %(c, to_x(i.value.mem.disp))) if i.shift.type != ARM64_SFT_INVALID and i.shift.value: print("\t\t\tShift: type = %u, value = %u" \ @@ -59,17 +59,27 @@ def print_insn_detail(insn): if i.ext != ARM64_EXT_INVALID: print("\t\t\tExt: %u" %i.ext) + if insn.writeback: + print("\tWrite-back: True") + if not insn.cc in [ARM64_CC_AL, ARM64_CC_INVALID]: + print("\tCode condition: %u" %insn.cc) + if insn.update_flags: + print("\tUpdate-flags: True") for (arch, mode, code, comment) in all_tests: - print("*" * 30) + print("*" * 16) print("Platform: %s" %comment) + print("Code: %s" % to_hex(code)) print("Disasm:") - + try: md = cs(arch, mode) - for insn in md.disasm(code, 0x1000): + last = None + for insn in md.disasm(code, 0x2c): print_insn_detail(insn) + last = insn print + print "0x%x:\n" % (last.address + last.size) except: print("ERROR: Arch or mode unsupported!") diff --git a/bindings/python/test_mips.py b/bindings/python/test_mips.py index fe279cbd98..a22ce45cc4 100755 --- a/bindings/python/test_mips.py +++ b/bindings/python/test_mips.py @@ -13,6 +13,15 @@ (CS_ARCH_MIPS, CS_MODE_64 + CS_MODE_LITTLE_ENDIAN, MIPS_CODE2, "MIPS-64-EL (Little-endian)"), ) +def to_hex(s): + return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK + +def to_x(s): + from struct import pack + if not s: return '0' + x = pack(">q", s).encode('hex') + while x[0] == '0': x = x[1:] + return x ### Test class cs def test_class(): @@ -22,33 +31,38 @@ def print_insn_detail(insn): if len(insn.operands) > 0: print("\top_count: %u" %len(insn.operands)) - c = 0 + c = -1 for i in insn.operands: c += 1 if i.type == MIPS_OP_REG: print("\t\toperands[%u].type: REG = %s" %(c, insn.reg_name(i.value.reg))) if i.type == MIPS_OP_IMM: - print("\t\toperands[%u].type: IMM = %x" %(c, i.value.imm)) + print("\t\toperands[%u].type: IMM = 0x%s" %(c, to_x(i.value.imm))) if i.type == MIPS_OP_MEM: print("\t\toperands[%u].type: MEM" %c) if i.value.mem.base != 0: print("\t\t\toperands[%u].mem.base: REG = %s" \ %(c, insn.reg_name(i.value.mem.base))) if i.value.mem.disp != 0: - print("\t\t\toperands[%u].mem.disp: %x" \ - %(c, i.value.mem.disp)) + print("\t\t\toperands[%u].mem.disp: 0x%s" \ + %(c, to_x(i.value.mem.disp))) for (arch, mode, code, comment) in all_tests: - print("*" * 30) + print("*" * 16) print("Platform: %s" %comment) + print("Code: %s" % to_hex(code)) print("Disasm:") - + try: md = cs(arch, mode) + last = None for insn in md.disasm(code, 0x1000): print_insn_detail(insn) + last = insn print + + print "0x%x:\n" %(insn.address + insn.size) except: print("ERROR: Arch or mode unsupported!") diff --git a/bindings/python/test_x86.py b/bindings/python/test_x86.py index 2fe640bfcf..a3ec0b21d5 100755 --- a/bindings/python/test_x86.py +++ b/bindings/python/test_x86.py @@ -5,18 +5,33 @@ from capstone import * from capstone.x86 import * -X86_CODE16 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\xa1\x13\x48\x6d\x3a" -X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\xa1\x13\x48\x6d\x3a" -X86_CODE32 += "\x8d\x05\x34\x12\x00\x00" X86_CODE64 = "\x55\x48\x8b\x05\xb8\x13\x00\x00" +X86_CODE16 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6" +X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6" all_tests = ( (CS_ARCH_X86, CS_MODE_16, X86_CODE16, "X86 16bit (Intel syntax)"), - (CS_ARCH_X86, CS_MODE_32 + CS_MODE_SYNTAX_ATT, X86_CODE32, "X86 32bit (ATT syntax)"), + (CS_ARCH_X86, CS_MODE_32 + CS_MODE_SYNTAX_ATT, X86_CODE32, "X86 32 (AT&T syntax)"), (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)"), (CS_ARCH_X86, CS_MODE_64, X86_CODE64, "X86 64 (Intel syntax)"), ) +def to_hex(s): + return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK + +def to_x(s): + from struct import pack + if not s: return '0' + x = pack(">q", s).encode('hex') + while x[0] == '0': x = x[1:] + return x + +def to_x_32(s): + from struct import pack + if not s: return '0' + x = pack(">i", s).encode('hex') + while x[0] == '0': x = x[1:] + return x ### Test class cs def test_class(): @@ -48,29 +63,31 @@ def print_insn_detail(mode, insn): print("\tmodrm: 0x%x" %(insn.modrm)) # print displacement value - print("\tdisp: 0x%x" %(insn.disp)) + print("\tdisp: 0x%s" %to_x_32(insn.disp)) # SIB is not available in 16-bit mode if (mode & CS_MODE_16 == 0): # print SIB byte print("\tsib: 0x%x" %(insn.sib)) + if (insn.sib): + print("\tsib_index: %s, sib_scale: %d, sib_base: %s" % (insn.reg_name(insn.sib_index), insn.sib_scale, insn.reg_name(insn.sib_base))) count = insn.op_count(X86_OP_IMM) if count > 0: print("\timm_count: %u" %count) for i in xrange(count): index = insn.op_index(X86_OP_IMM, i + 1) - print("\t\timms[%u] = 0x%x" %(i+1, (insn.operands[index].value.imm))) + print("\t\timms[%u]: 0x%s" %(i+1, to_x(insn.operands[index].value.imm))) if len(insn.operands) > 0: print("\top_count: %u" %len(insn.operands)) - c = 0 + c = -1 for i in insn.operands: c += 1 if i.type == X86_OP_REG: print("\t\toperands[%u].type: REG = %s" %(c, insn.reg_name(i.value.reg))) if i.type == X86_OP_IMM: - print("\t\toperands[%u].type: IMM = 0x%x" %(c, i.value.imm)) + print("\t\toperands[%u].type: IMM = 0x%s" %(c, to_x(i.value.imm))) if i.type == X86_OP_FP: print("\t\toperands[%u].type: FP = %f" %(c, i.value.fp)) if i.type == X86_OP_MEM: @@ -82,21 +99,22 @@ def print_insn_detail(mode, insn): if i.value.mem.scale != 1: print("\t\t\toperands[%u].mem.scale: %u" %(c, i.value.mem.scale)) if i.value.mem.disp != 0: - print("\t\t\toperands[%u].mem.disp: 0x%x" %(c, i.value.mem.disp)) + print("\t\t\toperands[%u].mem.disp: 0x%s" %(c, to_x(i.value.mem.disp))) for (arch, mode, code, comment) in all_tests: - print("*" * 30) + print("*" * 16) print("Platform: %s" %comment) + print("Code: %s" % to_hex(code)) print("Disasm:") - - try: - md = cs(arch, mode) - for insn in md.disasm(code, 0x1000): - print_insn_detail(mode, insn) - print - except: - print("ERROR: Arch or mode unsupported!") + + md = cs(arch, mode) + last = None + for insn in md.disasm(code, 0x1000): + print_insn_detail(mode, insn) + last = insn + print + print ("0x%x:\n" % (last.address + last.size)) test_class()