Skip to content

Commit

Permalink
Merge pull request #6 from danghvu/master
Browse files Browse the repository at this point in the history
Consistency of test output for binding and core
  • Loading branch information
danghvu committed Nov 28, 2013
2 parents f10be9b + c4325f8 commit 7143f8a
Show file tree
Hide file tree
Showing 20 changed files with 469 additions and 198 deletions.
46 changes: 46 additions & 0 deletions bindings/Makefile
Original file line number Diff line number Diff line change
@@ -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:
26 changes: 23 additions & 3 deletions bindings/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions bindings/java/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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);
}
}
}
41 changes: 21 additions & 20 deletions bindings/java/TestArm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<op_info.op.length+1; c++) {
Arm.Operand i = (Arm.Operand) op_info.op[c-1];
for (int c=0; c<op_info.op.length; c++) {
Arm.Operand i = (Arm.Operand) op_info.op[c];
String imm = hex(i.value.imm);
if (i.type == Arm.ARM_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
if (i.type == Arm.ARM_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = %s\n", c, imm);
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == Arm.ARM_OP_PIMM)
System.out.printf("\t\toperands[%d].type: P-IMM = %s\n", c, imm);
System.out.printf("\t\toperands[%d].type: P-IMM = %d\n", c, i.value.imm);
if (i.type == Arm.ARM_OP_CIMM)
System.out.printf("\t\toperands[%d].type: C-IMM = %s\n", c, imm);
System.out.printf("\t\toperands[%d].type: C-IMM = %d\n", c, i.value.imm);
if (i.type == Arm.ARM_OP_FP)
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
if (i.type == Arm.ARM_OP_MEM) {
Expand All @@ -74,13 +65,21 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
if (index != null)
System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index);
if (i.value.mem.scale != 1)
System.out.printf("\t\t\toperands[%d].mem.scale: %s\n", c, hex(i.value.mem.scale));
System.out.printf("\t\t\toperands[%d].mem.scale: %d\n", c, (i.value.mem.scale));
if (i.value.mem.disp != 0)
System.out.printf("\t\t\toperands[%d].mem.disp: %s\n", c, hex(i.value.mem.disp));
System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, (i.value.mem.disp));
}
if (i.shift.type != Arm.ARM_SFT_INVALID && i.shift.value > 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);
}
}

Expand All @@ -95,8 +94,9 @@ public static void main(String argv[]) {

for (int i=0; i<all_tests.length; i++) {
Test.platform test = all_tests[i];
System.out.println(new String(new char[30]).replace("\0", "*"));
System.out.println(new String(new char[16]).replace("\0", "*"));
System.out.println("Platform: " + test.comment);
System.out.println("Code: " + Test.stringToHex(test.code));
System.out.println("Disasm:");

cs = new Capstone(test.arch, test.mode);
Expand All @@ -106,6 +106,7 @@ public static void main(String argv[]) {
print_ins_detail(all_ins[j]);
System.out.println();
}
System.out.printf("0x%x:\n\n", (all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size));
}
}

Expand Down
42 changes: 23 additions & 19 deletions bindings/java/TestArm64.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.sun.jna.Memory;
import com.sun.jna.Pointer;

import capstone.Capstone;
import capstone.Arm64;

public class TestArm64 {

static byte[] hexString2Byte(String s) {
Expand Down Expand Up @@ -35,29 +38,17 @@ public static void print_ins_detail(Capstone.cs_insn ins) {

Arm64.OpInfo op_info = (Arm64.OpInfo) ins.op_info;

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);
}

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<op_info.op.length+1; c++) {
Arm64.Operand i = (Arm64.Operand) op_info.op[c-1];
for (int c=0; c<op_info.op.length; c++) {
Arm64.Operand i = (Arm64.Operand) op_info.op[c];
String imm = hex(i.value.imm);
if (i.type == Arm64.ARM64_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
if (i.type == Arm64.ARM64_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = %s\n", c, imm);
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == Arm64.ARM64_OP_CIMM)
System.out.printf("\t\toperands[%d].type: C-IMM = %s\n", c, imm);
System.out.printf("\t\toperands[%d].type: C-IMM = %d\n", c, i.value.imm);
if (i.type == Arm64.ARM64_OP_FP)
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
if (i.type == Arm64.ARM64_OP_MEM) {
Expand All @@ -69,14 +60,24 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
if (index != null)
System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index);
if (i.value.mem.disp != 0)
System.out.printf("\t\t\toperands[%d].mem.disp: %s\n", c, hex(i.value.mem.disp));
System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, i.value.mem.disp);
}
if (i.shift.type != Arm64.ARM64_SFT_INVALID && i.shift.value > 0)
System.out.printf("\t\t\tShift: type = %d, value = %d\n", i.shift.type, i.shift.value);
if (i.ext != Arm64.ARM64_EXT_INVALID)
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[]) {
Expand All @@ -87,17 +88,20 @@ public static void main(String argv[]) {

for (int i=0; i<all_tests.length; i++) {
Test.platform test = all_tests[i];
System.out.println(new String(new char[30]).replace("\0", "*"));
System.out.println(new String(new char[16]).replace("\0", "*"));
System.out.println("Platform: " + test.comment);
System.out.println("Code: " + Test.stringToHex(test.code));
System.out.println("Disasm:");

cs = new Capstone(test.arch, test.mode);
Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x1000);
Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x2c);

for (int j = 0; j < all_ins.length; j++) {
print_ins_detail(all_ins[j]);
System.out.println();
}

System.out.printf("0x%x: \n\n", all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size);
}
}

Expand Down
14 changes: 10 additions & 4 deletions bindings/java/TestMips.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.sun.jna.Memory;
import com.sun.jna.Pointer;

import capstone.Capstone;
import capstone.Mips;

public class TestMips {

static byte[] hexString2Byte(String s) {
Expand Down Expand Up @@ -38,13 +41,13 @@ public static void print_ins_detail(Capstone.cs_insn ins) {

if (op_info.op != null) {
System.out.printf("\top_count: %d\n", op_info.op.length);
for (int c=1; c<op_info.op.length+1; c++) {
Mips.Operand i = (Mips.Operand) op_info.op[c-1];
for (int c=0; c<op_info.op.length; c++) {
Mips.Operand i = (Mips.Operand) op_info.op[c];
String imm = hex(i.value.imm);
if (i.type == Mips.MIPS_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
if (i.type == Mips.MIPS_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = %s\n", c, imm);
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == Mips.MIPS_OP_MEM) {
System.out.printf("\t\toperands[%d].type: MEM\n",c);
String base = cs.reg_name(i.value.mem.base);
Expand All @@ -66,8 +69,9 @@ public static void main(String argv[]) {

for (int i=0; i<all_tests.length; i++) {
Test.platform test = all_tests[i];
System.out.println(new String(new char[30]).replace("\0", "*"));
System.out.println(new String(new char[16]).replace("\0", "*"));
System.out.println("Platform: " + test.comment);
System.out.println("Code: " + Test.stringToHex(test.code));
System.out.println("Disasm:");

cs = new Capstone(test.arch, test.mode);
Expand All @@ -77,6 +81,8 @@ public static void main(String argv[]) {
print_ins_detail(all_ins[j]);
System.out.println();
}

System.out.printf("0x%x:\n\n", all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size);
}
}

Expand Down
Loading

0 comments on commit 7143f8a

Please sign in to comment.