Merge pull request #184 from danghvu/v3

Fix binding tests and update Java binding to v3
test2
Nguyen Anh Quynh 10 years ago
commit b69cb94bcd
  1. 10
      bindings/java/Makefile
  2. 2
      bindings/java/Test.java
  3. 43
      bindings/java/TestArm.java
  4. 20
      bindings/java/TestArm64.java
  5. 2
      bindings/java/TestPpc.java
  6. 4
      bindings/java/TestX86.java
  7. 2
      bindings/java/TestXcore.java
  8. 42
      bindings/java/capstone/Arm.java
  9. 19
      bindings/java/capstone/Capstone.java
  10. 11
      bindings/java/capstone/X86.java
  11. 1
      bindings/java/run.sh
  12. 6
      bindings/python/test_arm.py
  13. 2
      bindings/python/test_arm64.py
  14. 2
      bindings/python/test_ppc.py
  15. 5
      bindings/python/test_sparc.py
  16. 2
      bindings/python/test_xcore.py
  17. 18
      bindings/python/xprint.py

@ -21,8 +21,10 @@ else
endif
CAPSTONE_JAVA = Capstone.java Arm_const.java Arm64_const.java Mips_const.java \
X86_const.java Ppc_const.java Sparc_const.java Sysz_const.java \
Arm.java Arm64.java Mips.java X86.java Ppc.java Sparc.java Systemz.java
X86_const.java Xcore_const.java Ppc_const.java Sparc_const.java\
Sysz_const.java \
Arm.java Arm64.java Mips.java X86.java Xcore.java Ppc.java\
Sparc.java Systemz.java
all: gen_const capstone tests
@ -40,7 +42,9 @@ endif
tests: jna
@mkdir -p $(OBJDIR)
javac -d $(OBJDIR) -classpath "$(JNA):$(BLDIR)/capstone.jar" Test.java TestArm.java TestArm64.java TestMips.java TestX86.java TestPpc.java TestSparc.java TestSystemz.java
javac -d $(OBJDIR) -classpath "$(JNA):$(BLDIR)/capstone.jar" Test.java\
TestArm.java TestArm64.java TestMips.java TestX86.java TestXcore.java\
TestPpc.java TestSparc.java TestSystemz.java
gen_const:
cd ../ && python const_generator.py java

@ -42,7 +42,7 @@ public class Test {
public static final byte[] SPARC_CODE = new byte[] { (byte)0x80, (byte)0xa0, (byte)0x40, (byte)0x02, (byte)0x85, (byte)0xc2, (byte)0x60, (byte)0x08, (byte)0x85, (byte)0xe8, (byte)0x20, (byte)0x01, (byte)0x81, (byte)0xe8, (byte)0x00, (byte)0x00, (byte)0x90, (byte)0x10, (byte)0x20, (byte)0x01, (byte)0xd5, (byte)0xf6, (byte)0x10, (byte)0x16, (byte)0x21, (byte)0x00, (byte)0x00, (byte)0x0a, (byte)0x86, (byte)0x00, (byte)0x40, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x12, (byte)0xbf, (byte)0xff, (byte)0xff, (byte)0x10, (byte)0xbf, (byte)0xff, (byte)0xff, (byte)0xa0, (byte)0x02, (byte)0x00, (byte)0x09, (byte)0x0d, (byte)0xbf, (byte)0xff, (byte)0xff, (byte)0xd4, (byte)0x20, (byte)0x60, (byte)0x00, (byte)0xd4, (byte)0x4e, (byte)0x00, (byte)0x16, (byte)0x2a, (byte)0xc2, (byte)0x80, (byte)0x03 };
public static final byte[] SYSZ_CODE = new byte[] { (byte)0xed, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x1a, (byte)0x5a, (byte)0x0f, (byte)0x1f, (byte)0xff, (byte)0xc2, (byte)0x09, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x07, (byte)0xf7, (byte)0xeb, (byte)0x2a, (byte)0xff, (byte)0xff, (byte)0x7f, (byte)0x57, (byte)0xe3, (byte)0x01, (byte)0xff, (byte)0xff, (byte)0x7f, (byte)0x57, (byte)0xeb, (byte)0x00, (byte)0xf0, (byte)0x00, (byte)0x00, (byte)0x24, (byte)0xb2, (byte)0x4f, (byte)0x00, (byte)0x78 };
public static final byte[] SPARCV9_CODE = new byte[] { (byte)0x81, (byte)0xa8, (byte)0x0a, (byte)0x24, (byte)0x89, (byte)0xa0, (byte)0x10, (byte)0x20, (byte)0x89, (byte)0xa0, (byte)0x1a, (byte)0x60, (byte)0x89, (byte)0xa0, (byte)0x00, (byte)0xe0 };
public static final byte[] XCORE_CODE = new byte[] { (byte)0xfe, (byte)0x0f, (byte)0xfe, (byte)0x17, (byte)0x13, (byte)0x17, (byte)0xc6, (byte)0xfe, (byte)0xec, (byte)0x17, (byte)0x97, (byte)0xf8, (byte)0xec, (byte)0x4f, (byte)0x1f, (byte)0xfd, (byte)0xec, (byte)0x37, (byte)0x0f, (byte)0xf2, (byte)0x45, (byte)0x5b, (byte)0xf9, (byte)0xfa, (byte)0x02, (byte)0x06, (byte)0x1b, (byte)0x10 };
public static final byte[] XCORE_CODE = new byte[] { (byte)0xfe, (byte)0x0f, (byte)0xfe, (byte)0x17, (byte)0x13, (byte)0x17, (byte)0xc6, (byte)0xfe, (byte)0xec, (byte)0x17, (byte)0x97, (byte)0xf8, (byte)0xec, (byte)0x4f, (byte)0x1f, (byte)0xfd, (byte)0xec, (byte)0x37, (byte)0x07, (byte)0xf2, (byte)0x45, (byte)0x5b, (byte)0xf9, (byte)0xfa, (byte)0x02, (byte)0x06, (byte)0x1b, (byte)0x10 };
static public void main(String argv[]) {
platform[] platforms = {

@ -19,10 +19,10 @@ public class TestArm {
return data;
}
static final String ARM_CODE = "EDFFFFEB04e02de500000000e08322e5f102030e0000a0e30230c1e7000053e3";
static final String ARM_CODE = "EDFFFFEB04e02de500000000e08322e5f102030e0000a0e30230c1e7000053e3000201f10540d0e8";
static final String ARM_CODE2 = "d1e800f0f02404071f3cf2c000004ff00001466c";
static final String THUMB_CODE2 = "4ff00001bde80088d1e800f0";
static final String THUMB_CODE = "7047eb4683b0c9681fb1";
static final String THUMB_CODE2 = "4ff00001bde80088d1e800f018bfadbff3ff0b0c86f3008980f3008c4ffa99f6d0ffa201";
static final String THUMB_CODE = "7047eb4683b0c9681fb130bfaff32084";
public static Capstone cs;
@ -44,6 +44,8 @@ public class TestArm {
for (int c=0; c<operands.op.length; c++) {
Arm.Operand i = (Arm.Operand) operands.op[c];
String imm = hex(i.value.imm);
if (i.type == ARM_OP_SYSREG)
System.out.printf("\t\toperands[%d].type: SYSREG = %d\n", c, i.value.reg);
if (i.type == ARM_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
if (i.type == ARM_OP_IMM)
@ -52,6 +54,8 @@ public class TestArm {
System.out.printf("\t\toperands[%d].type: P-IMM = %d\n", c, i.value.imm);
if (i.type == ARM_OP_CIMM)
System.out.printf("\t\toperands[%d].type: C-IMM = %d\n", c, i.value.imm);
if (i.type == ARM_OP_SETEND)
System.out.printf("\t\toperands[%d].type: SETEND = %s\n", c, i.value.setend == ARM_SETEND_BE? "be" : "le");
if (i.type == ARM_OP_FP)
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
if (i.type == ARM_OP_MEM) {
@ -67,18 +71,35 @@ public class TestArm {
if (i.value.mem.disp != 0)
System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, (i.value.mem.disp));
}
if (i.vector_index > 0)
System.out.printf("\t\t\toperands[%d].vector_index = %d\n", c, (i.vector_index));
if (i.shift.type != ARM_SFT_INVALID && i.shift.value > 0)
System.out.printf("\t\t\tShift: type = %d, value = %d\n", i.shift.type, i.shift.value);
System.out.printf("\t\t\tShift: %d = %d\n", i.shift.type, i.shift.value);
}
if (operands.writeback)
System.out.println("\tWrite-back: True");
}
if (operands.writeback)
System.out.println("\tWrite-back: True");
if (operands.updateFlags)
System.out.println("\tUpdate-flags: True");
if (operands.updateFlags)
System.out.println("\tUpdate-flags: True");
if (operands.cc != ARM_CC_AL && operands.cc != ARM_CC_INVALID)
System.out.printf("\tCode condition: %d\n", operands.cc);
}
if (operands.cc != ARM_CC_AL && operands.cc != ARM_CC_INVALID)
System.out.printf("\tCode condition: %d\n", operands.cc);
if (operands.cpsMode > 0)
System.out.printf("\tCPSI-mode: %d\n", operands.cpsMode);
if (operands.cpsFlag > 0)
System.out.printf("\tCPSI-flag: %d\n", operands.cpsFlag);
if (operands.vectorData > 0)
System.out.printf("\tVector-data: %d\n", operands.vectorData);
if (operands.vectorSize > 0)
System.out.printf("\tVector-size: %d\n", operands.vectorSize);
if (operands.usermode)
System.out.printf("\tUser-mode: True\n");
}
public static void main(String argv[]) {

@ -19,7 +19,7 @@ public class TestArm64 {
return data;
}
static final String ARM64_CODE = "217c029b217c00530040214be10b40b9200481da2008028b";
static final String ARM64_CODE = "090038d5bf4000d50c0513d52050020e20e43d0f0018a05fa200ae9e9f3703d5bf3303d5df3f03d5217c029b217c00530040214be10b40b9200481da2008028b105be83c";
public static Capstone cs;
@ -43,6 +43,15 @@ public class TestArm64 {
String imm = hex(i.value.imm);
if (i.type == ARM64_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
if (i.type == ARM64_OP_REG_MRS)
System.out.printf("\t\toperands[%d].type: REG_MRS = 0x%x\n", c, i.value.reg);
if (i.type == ARM64_OP_REG_MSR)
System.out.printf("\t\toperands[%d].type: REG_MSR = 0x%x\n", c, i.value.reg);
if (i.type == ARM64_OP_PSTATE)
System.out.printf("\t\toperands[%d].type: PSTATE = 0x%x\n", c, i.value.imm);
if (i.type == ARM64_OP_BARRIER)
System.out.printf("\t\toperands[%d].type: BARRIER = 0x%x\n", c, i.value.imm);
if (i.type == ARM64_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == ARM64_OP_CIMM)
@ -64,6 +73,13 @@ public class TestArm64 {
System.out.printf("\t\t\tShift: type = %d, value = %d\n", i.shift.type, i.shift.value);
if (i.ext != ARM64_EXT_INVALID)
System.out.printf("\t\t\tExt: %d\n", i.ext);
if (i.vas != ARM64_VAS_INVALID)
System.out.printf("\t\t\tVector Arrangement Specifier: 0x%x\n", i.vas);
if (i.vess != ARM64_VESS_INVALID)
System.out.printf("\t\t\tVector Element Size Specifier: %d\n", i.vess);
if (i.vector_index != -1)
System.out.printf("\t\t\tVector Index: %d\n", i.vector_index);
}
}
@ -74,7 +90,7 @@ public class TestArm64 {
System.out.println("\tUpdate-flags: True");
if (operands.cc != ARM64_CC_AL && operands.cc != ARM64_CC_INVALID)
System.out.printf("\tCode condition: %d\n", operands.cc);
System.out.printf("\tCode-condition: %d\n", operands.cc);
}

@ -19,7 +19,7 @@ public class TestPpc {
return data;
}
static final String PPC_CODE = "80200000803f00001043230ed04400804c4322022d0300807c4320147c4320934f2000214cc80021";
static final String PPC_CODE = "80200000803f00001043230ed04400804c4322022d0300807c4320147c4320934f2000214cc8002140820014";
public static Capstone cs;

@ -66,8 +66,8 @@ public class TestX86 {
// print SIB byte
System.out.printf("\tsib: 0x%x\n", operands.sib);
if (operands.sib != 0)
System.out.printf("\tsib_index: %s, sib_scale: %d, sib_base: %s\n",
ins.regName(operands.sibIndex), operands.sibScale, ins.regName(operands.sibBase));
System.out.printf("\t\tsib_base: %s\n\t\tsib_index: %s\n\t\tsib_scale: %d\n",
ins.regName(operands.sibBase), ins.regName(operands.sibIndex), operands.sibScale);
}
if (operands.sseCC != 0)

@ -61,7 +61,7 @@ public class TestXcore {
public static void main(String argv[]) {
final Test.platform[] all_tests = {
new Test.platform(Capstone.CS_ARCH_XCORE, Capstone.CS_MODE_BIG_ENDIAN, hexString2Byte(XCORE_CODE), "Xcore"),
new Test.platform(Capstone.CS_ARCH_XCORE, Capstone.CS_MODE_BIG_ENDIAN, hexString2Byte(XCORE_CODE), "XCore"),
};
for (int i=0; i<all_tests.length; i++) {

@ -30,10 +30,11 @@ public class Arm {
public int imm;
public double fp;
public MemType mem;
public int setend;
@Override
public List getFieldOrder() {
return Arrays.asList("reg", "imm", "fp", "mem");
return Arrays.asList("reg", "imm", "fp", "mem", "setend");
}
}
@ -48,11 +49,13 @@ public class Arm {
}
public static class Operand extends Structure {
public int vector_index;
public OpShift shift;
public int type;
public OpValue value;
public void read() {
readField("vector_index");
readField("type");
if (type == ARM_OP_MEM)
value.setType(MemType.class);
@ -70,14 +73,19 @@ public class Arm {
@Override
public List getFieldOrder() {
return Arrays.asList("shift", "type", "value");
return Arrays.asList("vector_index", "shift", "type", "value");
}
}
public static class UnionOpInfo extends Capstone.UnionOpInfo {
public boolean usermode;
public int vector_size;
public int vector_data;
public int cps_mode;
public int cps_flag;
public int cc;
public byte _update_flags;
public byte _writeback;
public byte update_flags;
public byte writeback;
public byte op_count;
public Operand [] op;
@ -87,9 +95,14 @@ public class Arm {
}
public void read() {
readField("usermode");
readField("vector_size");
readField("vector_data");
readField("cps_mode");
readField("cps_flag");
readField("cc");
readField("_update_flags");
readField("_writeback");
readField("update_flags");
readField("writeback");
readField("op_count");
op = new Operand[op_count];
if (op_count != 0)
@ -98,20 +111,31 @@ public class Arm {
@Override
public List getFieldOrder() {
return Arrays.asList("cc", "_update_flags", "_writeback", "op_count", "op");
return Arrays.asList("usermode", "vector_size", "vector_data",
"cps_mode", "cps_flag", "cc", "update_flags", "writeback", "op_count", "op");
}
}
public static class OpInfo extends Capstone.OpInfo {
public boolean usermode;
public int vectorSize;
public int vectorData;
public int cpsMode;
public int cpsFlag;
public int cc;
public boolean updateFlags;
public boolean writeback;
public Operand [] op = null;
public OpInfo(UnionOpInfo op_info) {
usermode = op_info.usermode;
vectorSize = op_info.vector_size;
vectorData = op_info.vector_data;
cpsMode = op_info.cps_mode;
cpsFlag = op_info.cps_flag;
cc = op_info.cc;
updateFlags = (op_info._update_flags > 0);
writeback = (op_info._writeback > 0);
updateFlags = (op_info.update_flags > 0);
writeback = (op_info.writeback > 0);
op = op_info.op;
}
}

@ -47,14 +47,16 @@ public class Capstone {
// instruction mnemonic. NOTE: irrelevant for diet engine.
public byte[] mnemonic;
// instruction operands. NOTE: irrelevant for diet engine.
public byte[] operands;
public byte[] op_str;
// detail information of instruction.
public _cs_detail.ByReference cs_detail;
public _cs_insn() {
bytes = new byte[16];
mnemonic = new byte[32];
operands = new byte[160];
op_str = new byte[160];
java.util.Arrays.fill(mnemonic, (byte) 0);
java.util.Arrays.fill(op_str, (byte) 0);
}
public _cs_insn(Pointer p) {
@ -65,7 +67,7 @@ public class Capstone {
@Override
public List getFieldOrder() {
return Arrays.asList("id", "address", "size", "bytes", "mnemonic", "operands", "cs_detail");
return Arrays.asList("id", "address", "size", "bytes", "mnemonic", "op_str", "cs_detail");
}
}
@ -120,8 +122,12 @@ public class Capstone {
size = insn.size;
if (!diet) {
mnemonic = new String(insn.mnemonic).replace("\u0000","");
opStr = new String(insn.operands).replace("\u0000","");
int lm = 0;
while (insn.mnemonic[lm++] != 0);
int lo = 0;
while (insn.op_str[lo++] != 0);
mnemonic = new String(insn.mnemonic, 0, lm-1);
opStr = new String(insn.op_str, 0, lo-1);
}
cs = _cs;
@ -415,7 +421,8 @@ public class Capstone {
CsInsn[] allInsn = fromArrayRaw((_cs_insn[]) byref.toArray(c.intValue()));
// free allocated memory
cs.cs_free(p, c);
// cs.cs_free(p, c);
// FIXME(danghvu): Can't free because memory is still inside CsInsn
return allInsn;
}

@ -41,7 +41,7 @@ public class X86 {
public static class Operand extends Structure {
public int type;
public OpValue value;
public int size;
public byte size;
public int avx_bcast;
public boolean avx_zero_opmask;
@ -79,10 +79,10 @@ public class X86 {
public int sib_base;
public int sse_cc;
public int avx_cc;
public boolean avx_sae;
public byte avx_sae;
public int avx_rm;
public char op_count;
public byte op_count;
public Operand [] op;
@ -103,6 +103,7 @@ public class X86 {
public byte [] prefix;
public byte [] opcode;
public byte opSize;
public byte rex;
public byte addrSize;
public byte dispSize;
public byte immSize;
@ -114,6 +115,8 @@ public class X86 {
public int sibBase;
public int sseCC;
public int avxCC;
public boolean avxSae;
public int avxRm;
public Operand[] op;
@ -130,7 +133,7 @@ public class X86 {
sibBase = e.sib_base;
sseCC = e.sse_cc;
avxCC = e.avx_cc;
avxSae = e.avx_sae;
avxSae = e.avx_sae > 0;
avxRm = e.avx_rm;
op = new Operand[e.op_count];
for (int i=0; i<e.op_count; i++)

@ -16,6 +16,7 @@ case "$1" in
"arm64") java -classpath ${JNA}:. TestArm64 ;;
"mips") java -classpath ${JNA}:. TestMips ;;
"x86") java -classpath ${JNA}:. TestX86 ;;
"xcore") java -classpath ${JNA}:. TestXcore; ;;
"ppc") java -classpath ${JNA}:. TestPpc ;;
"sparc") java -classpath ${JNA}:. TestSparc ;;
"systemz") java -classpath ${JNA}:. TestSystemz ;;

@ -47,9 +47,9 @@ def print_insn_detail(insn):
print("\t\toperands[%u].type: SYSREG = %u" % (c, i.reg))
if i.type == ARM_OP_SETEND:
if i.setend == ARM_SETEND_BE:
print("\t\toperands[%u].type: SETEND = be")
print("\t\toperands[%u].type: SETEND = be" % c)
else:
print("\t\toperands[%u].type: SETEND = le")
print("\t\toperands[%u].type: SETEND = le" % c)
if i.type == ARM_OP_MEM:
print("\t\toperands[%u].type: MEM" % c)
if i.mem.base != 0:
@ -66,7 +66,7 @@ def print_insn_detail(insn):
% (c, to_x_32(i.mem.disp)))
if i.shift.type != ARM_SFT_INVALID and i.shift.value:
print("\t\t\tShift: type = %u, value = %u\n" \
print("\t\t\tShift: %u = %u" \
% (i.shift.type, i.shift.value))
if i.vector_index != -1:
print("\t\t\toperands[%u].vector_index = %u" %(c, i.vector_index))

@ -78,7 +78,7 @@ def print_insn_detail(insn):
if insn.writeback:
print("\tWrite-back: True")
if not insn.cc in [ARM64_CC_AL, ARM64_CC_INVALID]:
print("\tCode condition: %u" % insn.cc)
print("\tCode-condition: %u" % insn.cc)
if insn.update_flags:
print("\tUpdate-flags: True")

@ -5,7 +5,7 @@ from __future__ import print_function
from capstone import *
from capstone.ppc import *
from xprint import to_x, to_hex, to_x_32
PPC_CODE = b"\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21"
PPC_CODE = b"\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14"
all_tests = (
(CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, PPC_CODE, "PPC-64"),

@ -38,6 +38,9 @@ def print_insn_detail(insn):
if i.mem.base != 0:
print("\t\t\toperands[%u].mem.base: REG = %s" \
% (c, insn.reg_name(i.mem.base)))
if i.mem.index != 0:
print("\t\t\toperands[%u].mem.index: REG = %s" \
% (c, insn.reg_name(i.mem.index)))
if i.mem.disp != 0:
print("\t\t\toperands[%u].mem.disp: 0x%s" \
% (c, to_x_32(i.mem.disp)))
@ -46,7 +49,7 @@ def print_insn_detail(insn):
if insn.cc:
print("\tCode condition: %u" % insn.cc)
if insn.hint:
print("\tBranch hint: %u" % insn.hint)
print("\tHint code: %u" % insn.hint)
# ## Test class Cs

@ -43,7 +43,7 @@ def print_insn_detail(insn):
print("\t\t\toperands[%u].mem.disp: 0x%s" \
% (c, to_x(i.mem.disp)))
if i.mem.direct != 1:
print("\t\t\toperands[%u].mem.direct: -1")
print("\t\t\toperands[%u].mem.direct: -1" % c)
c += 1

@ -12,24 +12,24 @@ def to_hex(s):
else:
return " ".join("0x{0:02x}".format(ord(c)) for c in s)
def to_hex2(s):
if _python3:
r = "".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK
else:
r = "".join("{0:02x}".format(ord(c)) for c in s)
while r[0] == '0': r = r[1:]
return r
def to_x(s):
from struct import pack
if not s: return '0'
x = pack(">q", s)
while x[0] in ('\0', 0): x = x[1:]
if _python3:
return "".join("{0:02x}".format(c) for c in x) # <-- Python 3 is OK
else:
return "".join("{0:02x}".format(ord(c)) for c in x)
return to_hex2(x)
def to_x_32(s):
from struct import pack
if not s: return '0'
x = pack(">i", s)
while x[0] in ('\0', 0): x = x[1:]
if _python3:
return "".join("{0:02x}".format(c) for c in x) # <-- Python 3 is OK
else:
return "".join("{0:02x}".format(ord(c)) for c in x)
return to_hex2(x)

Loading…
Cancel
Save