From a7d954f1315544ae41988e4eb8a6552a04cc7d9f Mon Sep 17 00:00:00 2001 From: danghvu Date: Fri, 29 Nov 2013 10:57:45 -0600 Subject: [PATCH 1/3] Adjust binding tests to match api changes --- bindings/java/capstone/Arm.java | 13 +++--- bindings/java/capstone/Arm64.java | 5 ++- bindings/java/capstone/Capstone.java | 59 ++++++++++++++-------------- bindings/java/capstone/Mips.java | 3 +- bindings/java/capstone/X86.java | 2 +- bindings/python/test_arm.py | 11 +++++- 6 files changed, 52 insertions(+), 41 deletions(-) diff --git a/bindings/java/capstone/Arm.java b/bindings/java/capstone/Arm.java index 198a1f4a7e..a308a0b202 100644 --- a/bindings/java/capstone/Arm.java +++ b/bindings/java/capstone/Arm.java @@ -57,7 +57,7 @@ public static class MemType extends Structure { public int base; public int index; public int scale; - public long disp; + public int disp; @Override public List getFieldOrder() { @@ -67,7 +67,7 @@ public List getFieldOrder() { public static class OpValue extends Union { public int reg; - public long imm; + public int imm; public double fp; public MemType mem; @@ -99,7 +99,7 @@ public void read() { if (type == ARM_OP_FP) value.setType(Double.TYPE); if (type == ARM_OP_PIMM || type == ARM_OP_IMM || type == ARM_OP_CIMM) - value.setType(Long.TYPE); + value.setType(Integer.TYPE); if (type == ARM_OP_REG) value.setType(Integer.TYPE); if (type == ARM_OP_INVALID) @@ -122,12 +122,12 @@ public static class UnionOpInfo extends Capstone.UnionOpInfo { public Operand [] op; - public UnionOpInfo(){ - op = new Operand[32]; + public UnionOpInfo(){ + op = new Operand[20]; } public UnionOpInfo(Pointer p){ - op = new Operand[32]; + op = new Operand[20]; useMemory(p); read(); } @@ -142,6 +142,7 @@ public void read() { readField("_update_flags"); readField("_writeback"); readField("op_count"); + if (op_count == 0) return; op = new Operand[op_count]; readField("op"); } diff --git a/bindings/java/capstone/Arm64.java b/bindings/java/capstone/Arm64.java index 06cf9c9be3..05e86b1e4b 100644 --- a/bindings/java/capstone/Arm64.java +++ b/bindings/java/capstone/Arm64.java @@ -130,11 +130,11 @@ public static class UnionOpInfo extends Capstone.UnionOpInfo { public Operand [] op; public UnionOpInfo() { - op = new Operand[32]; + op = new Operand[8]; } public UnionOpInfo(Pointer p) { - op = new Operand[32]; + op = new Operand[8]; useMemory(p); read(); } @@ -148,6 +148,7 @@ public void read() { readField("_update_flags"); readField("_writeback"); readField("op_count"); + if (op_count == 0) return; op = new Operand[op_count]; readField("op"); } diff --git a/bindings/java/capstone/Capstone.java b/bindings/java/capstone/Capstone.java index 4e71e39f72..983694c99e 100644 --- a/bindings/java/capstone/Capstone.java +++ b/bindings/java/capstone/Capstone.java @@ -5,9 +5,10 @@ import com.sun.jna.Library; import com.sun.jna.Native; +import com.sun.jna.NativeLong; +import com.sun.jna.ptr.NativeLongByReference; import com.sun.jna.Structure; import com.sun.jna.Union; -import com.sun.jna.ptr.LongByReference; import com.sun.jna.Pointer; import com.sun.jna.ptr.PointerByReference; import com.sun.jna.ptr.IntByReference; @@ -31,7 +32,7 @@ protected static int max(int a, int b, int c, int d) { protected static class _cs_insn extends Structure { public int id; - public long address; + public NativeLong address; public short size; public byte[] mnemonic = new byte[32]; public byte[] operands = new byte[96]; @@ -40,13 +41,13 @@ protected static class _cs_insn extends Structure { public int[] groups = new int[8]; public _cs_insn(Pointer p) { - mnemonic = new byte[32]; - operands = new byte[96]; - regs_read = new int[32]; - regs_write = new int[32]; - groups = new int[8]; - useMemory(p); - read(); + mnemonic = new byte[32]; + operands = new byte[96]; + regs_read = new int[32]; + regs_write = new int[32]; + groups = new int[8]; + useMemory(p); + read(); } @Override @@ -58,7 +59,7 @@ public List getFieldOrder() { public static class cs_insn { public OpInfo op_info; public Pointer ptr_origin; - public long csh; + public NativeLong csh; public int id; public long address; @@ -72,9 +73,9 @@ public static class cs_insn { private CS cs; private int _size; - public cs_insn (_cs_insn struct, Pointer _ptr_origin, long _csh, CS _cs, OpInfo _op_info) { + public cs_insn (_cs_insn struct, Pointer _ptr_origin, NativeLong _csh, CS _cs, OpInfo _op_info) { id = struct.id; - address = struct.address; + address = struct.address.longValue(); size = struct.size; mnemonic = new String(struct.mnemonic).replace("\u0000",""); operands = new String(struct.operands).replace("\u0000",""); @@ -169,21 +170,21 @@ private cs_insn[] fromArrayPointer(Pointer pointer, int numberResults) } private interface CS extends Library { - public int cs_open(int arch, int mode, LongByReference handle); - public long cs_disasm_dyn(long handle, byte[] code, long code_len, - long addr, long count, PointerByReference insn); + public int cs_open(int arch, int mode, NativeLongByReference handle); + public NativeLong cs_disasm_dyn(NativeLong handle, byte[] code, NativeLong code_len, + NativeLong addr, NativeLong count, PointerByReference insn); public void cs_free(Pointer p); - public int cs_close(long handle); - public String cs_reg_name(long csh, int id); - public int cs_op_count(long csh, Pointer insn, int type); - public int cs_op_index(long csh, Pointer insn, int type, int index); - - public String cs_insn_name(long csh, int id); - public byte cs_insn_group(long csh, Pointer insn, int id); - public byte cs_reg_read(long csh, Pointer insn, int id); - public byte cs_reg_write(long csh, Pointer insn, int id); + public int cs_close(NativeLong handle); + public String cs_reg_name(NativeLong csh, int id); + public int cs_op_count(NativeLong csh, Pointer insn, int type); + public int cs_op_index(NativeLong csh, Pointer insn, int type, int index); + + public String cs_insn_name(NativeLong csh, int id); + public byte cs_insn_group(NativeLong csh, Pointer insn, int id); + public byte cs_reg_read(NativeLong csh, Pointer insn, int id); + public byte cs_reg_write(NativeLong csh, Pointer insn, int id); public void cs_version(IntByReference major, IntByReference minor); - public int cs_errno(long csh); + public int cs_errno(NativeLong csh); } public static final int CS_ARCH_ARM = 0; @@ -210,7 +211,7 @@ public long cs_disasm_dyn(long handle, byte[] code, long code_len, public static final int CS_ERR_MODE = 5; // Invalid/unsupported mode - private long csh; + private NativeLong csh; private PointerByReference insnRef; private CS cs; @@ -219,7 +220,7 @@ public Capstone(int arch, int mode) this.arch = arch; this.mode = mode; cs = (CS)Native.loadLibrary("capstone", CS.class); - LongByReference handleref = new LongByReference(); + NativeLongByReference handleref = new NativeLongByReference(); if (cs.cs_open(arch, mode, handleref) != CS_ERR_OK) { throw new RuntimeException("ERROR: Wrong arch or mode"); } @@ -241,10 +242,10 @@ public cs_insn[] disasm(byte[] code, long address) { public cs_insn[] disasm(byte[] code, long address, long count) { insnRef = new PointerByReference(); - long c = cs.cs_disasm_dyn(csh, code, code.length, address, count, insnRef); + NativeLong c = cs.cs_disasm_dyn(csh, code, new NativeLong(code.length), new NativeLong(address), new NativeLong(count), insnRef); Pointer p = insnRef.getValue(); - cs_insn[] all_insn = fromArrayPointer(p, (int)c); + cs_insn[] all_insn = fromArrayPointer(p, c.intValue()); return all_insn; } } diff --git a/bindings/java/capstone/Mips.java b/bindings/java/capstone/Mips.java index 37f6761e58..78b342d366 100644 --- a/bindings/java/capstone/Mips.java +++ b/bindings/java/capstone/Mips.java @@ -63,7 +63,7 @@ public List getFieldOrder() { } public static class UnionOpInfo extends Capstone.UnionOpInfo { - public short op_count; + public byte op_count; public Operand [] op; public UnionOpInfo() { @@ -82,6 +82,7 @@ public static int getSize() { public void read() { readField("op_count"); + if (op_count ==0) return; op = new Operand[op_count]; readField("op"); } diff --git a/bindings/java/capstone/X86.java b/bindings/java/capstone/X86.java index cf70f956e5..5682c3edef 100644 --- a/bindings/java/capstone/X86.java +++ b/bindings/java/capstone/X86.java @@ -84,7 +84,7 @@ public static class UnionOpInfo extends Capstone.UnionOpInfo { public byte sib_scale; public int sib_base; - public int op_count; + public char op_count; public Operand [] op; diff --git a/bindings/python/test_arm.py b/bindings/python/test_arm.py index bf3c4d9c66..ea7b635906 100755 --- a/bindings/python/test_arm.py +++ b/bindings/python/test_arm.py @@ -27,6 +27,13 @@ def to_x(s): 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(): def print_insn_detail(insn): @@ -40,7 +47,7 @@ def print_insn_detail(insn): 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 = 0x%s" %(c, to_x(i.value.imm))) + print("\t\toperands[%u].type: IMM = 0x%s" %(c, to_x_32(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,7 +67,7 @@ def print_insn_detail(insn): %(c, i.value.mem.scale)) if i.value.mem.disp != 0: print("\t\t\toperands[%u].mem.disp: 0x%s" \ - %(c, to_x(i.value.mem.disp))) + %(c, to_x_32(i.value.mem.disp))) if i.shift.type != ARM_SFT_INVALID and i.shift.value: print("\t\t\tShift: type = %u, value = %u\n" \ From bfd2de649313957e1f1cba751ec695b5134235df Mon Sep 17 00:00:00 2001 From: danghvu Date: Fri, 29 Nov 2013 18:27:28 +0100 Subject: [PATCH 2/3] Fix Arm64.java miss type --- bindings/java/capstone/Arm64.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bindings/java/capstone/Arm64.java b/bindings/java/capstone/Arm64.java index 05e86b1e4b..e7936e19c6 100644 --- a/bindings/java/capstone/Arm64.java +++ b/bindings/java/capstone/Arm64.java @@ -62,7 +62,7 @@ public class Arm64 { public static class MemType extends Structure { public int base; public int index; - public long disp; + public int disp; @Override public List getFieldOrder() { @@ -72,7 +72,7 @@ public List getFieldOrder() { public static class OpValue extends Union { public int reg; - public long imm; + public int imm; public double fp; public MemType mem; @@ -104,9 +104,7 @@ public void read() { value.setType(MemType.class); if (type == ARM64_OP_FP) value.setType(Double.TYPE); - if (type == ARM64_OP_IMM || type == ARM64_OP_CIMM) - value.setType(Long.TYPE); - if (type == ARM64_OP_REG) + if (type == ARM64_OP_IMM || type == ARM64_OP_CIMM || type == ARM64_OP_REG) value.setType(Integer.TYPE); if (type == ARM64_OP_INVALID) return; From 20b2d800a9057537ebd4b496331f6c2d72bca646 Mon Sep 17 00:00:00 2001 From: danghvu Date: Fri, 29 Nov 2013 15:23:37 -0600 Subject: [PATCH 3/3] Improved performance of java binding --- bindings/java/capstone/Capstone.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/bindings/java/capstone/Capstone.java b/bindings/java/capstone/Capstone.java index 983694c99e..38a7e97326 100644 --- a/bindings/java/capstone/Capstone.java +++ b/bindings/java/capstone/Capstone.java @@ -27,7 +27,10 @@ protected static abstract class OpInfo {} protected static abstract class UnionOpInfo extends Structure {} protected static int max(int a, int b, int c, int d) { - return Math.max(Math.max(Math.max(a,b),c),d); + if (a