Debian Codegraph @

bindings/python/tests/test_arm64.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Capstone Python bindings, by Nguyen Anh Quynnh <[email protected]>
4
5 from __future__ import print_function
6 from capstone import *
7 from capstone.arm64 import *
8
9
10 ARM64_CODE = b"\x09\x00\x38\xd5\xbf\x40\x00\xd5\x0c\x05\x13\xd5\x20\x50\x02\x0e\x20\xe4\x3d\x0f\x00\x18\xa0\x5f\xa2\x00\xae\x9e\x9f\x37\x03\xd5\xbf\x33\x03\xd5\xdf\x3f\x03\xd5\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x20\x04\x81\xda\x20\x08\x02\x8b\x10\x5b\xe8\x3c"
11 mask64 = lambda v: v & 0xFFFFFFFFFFFFFFFF
12 all_tests = (
13 (CS_ARCH_ARM64, CS_MODE_ARM, ARM64_CODE, "ARM-64"),
14 )
15
16
17 def print_insn_detail(insn):
18 # print address, mnemonic and operands
19 print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
20
21 # "data" instruction generated by SKIPDATA option has no detail
22 if insn.id == 0:
23 return
24
25 if len(insn.operands) > 0:
26 print("\top_count: %u" % len(insn.operands))
27 c = -1
28 for i in insn.operands:
29 c += 1
30 if i.type == ARM64_OP_REG:
31 print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
32 if i.type == ARM64_OP_IMM:
33 print("\t\toperands[%u].type: IMM = 0x%x" % (c, mask64(i.imm)))
34 if i.type == ARM64_OP_CIMM:
35 print("\t\toperands[%u].type: C-IMM = %u" % (c, i.imm))
36 if i.type == ARM64_OP_FP:
37 print("\t\toperands[%u].type: FP = %f" % (c, i.fp))
38 if i.type == ARM64_OP_MEM:
39 print("\t\toperands[%u].type: MEM" % c)
40 if i.mem.base != 0:
41 print("\t\t\toperands[%u].mem.base: REG = %s" \
42 % (c, insn.reg_name(i.mem.base)))
43 if i.mem.index != 0:
44 print("\t\t\toperands[%u].mem.index: REG = %s" \
45 % (c, insn.reg_name(i.mem.index)))
46 if i.mem.disp != 0:
47 print("\t\t\toperands[%u].mem.disp: 0x%x" \
48 % (c, mask64(i.mem.disp)))
49 if i.type == ARM64_OP_REG_MRS:
50 print("\t\toperands[%u].type: REG_MRS = 0x%x" % (c, i.reg))
51 if i.type == ARM64_OP_REG_MSR:
52 print("\t\toperands[%u].type: REG_MSR = 0x%x" % (c, i.reg))
53 if i.type == ARM64_OP_PSTATE:
54 print("\t\toperands[%u].type: PSTATE = 0x%x" % (c, i.pstate))
55 if i.type == ARM64_OP_SYS:
56 print("\t\toperands[%u].type: SYS = 0x%x" % (c, i.sys))
57 if i.type == ARM64_OP_PREFETCH:
58 print("\t\toperands[%u].type: PREFETCH = 0x%x" % (c, i.prefetch))
59 if i.type == ARM64_OP_BARRIER:
60 print("\t\toperands[%u].type: BARRIER = 0x%x" % (c, i.barrier))
61 if i.type == ARM64_OP_SVCR:
62 print("\t\toperands[%u].type: SYS = 0x%x" % (c, i.sys))
63 if i.svcr == ARM64_SVCR_SVCRSM:
64 print("\t\t\toperands[%u].svcr: BIT = SM" % (c))
65 if i.svcr == ARM64_SVCR_SVCRZA:
66 print("\t\t\toperands[%u].svcr: BIT = ZA" % (c))
67 if i.svcr == ARM64_SVCR_SVCRSMZA:
68 print("\t\t\toperands[%u].svcr: BIT = SM & ZA" % (c))
69 if i.type == ARM64_OP_SME_INDEX:
70 print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
71 if i.sme_index.base != ARM64_REG_INVALID:
72 print("\t\t\toperands[%u].index.base: REG = %s" % (c, insn.reg_name(i.reg)))
73 if i.sme_index.disp != 0 :
74 print("\t\t\toperands[%u].index.disp: 0x%x" %(c, i.sme_index.disp))
75
76 if i.shift.type != ARM64_SFT_INVALID and i.shift.value:
77 print("\t\t\tShift: type = %u, value = %u" % (i.shift.type, i.shift.value))
78
79 if i.ext != ARM64_EXT_INVALID:
80 print("\t\t\tExt: %u" % i.ext)
81
82 if i.vas != ARM64_VAS_INVALID:
83 print("\t\t\tVector Arrangement Specifier: 0x%x" % i.vas)
84
85 if i.vector_index != -1:
86 print("\t\t\tVector Index: %u" % i.vector_index)
87
88 if i.access == CS_AC_READ:
89 print("\t\toperands[%u].access: READ\n" % (c))
90 elif i.access == CS_AC_WRITE:
91 print("\t\toperands[%u].access: WRITE\n" % (c))
92 elif i.access == CS_AC_READ | CS_AC_WRITE:
93 print("\t\toperands[%u].access: READ | WRITE\n" % (c))
94
95
96 if insn.writeback:
97 if insn.post_index:
98 print("\tWrite-back: Post")
99 else:
100 print("\tWrite-back: Pre")
101
102 if not insn.cc in [ARM64_CC_AL, ARM64_CC_INVALID]:
103 print("\tCode-condition: %u" % insn.cc)
104 if insn.update_flags:
105 print("\tUpdate-flags: True")
106
107 (regs_read, regs_write) = insn.regs_access()
108
109 if len(regs_read) > 0:
110 print("\tRegisters read:", end="")
111 for r in regs_read:
112 print(" %s" %(insn.reg_name(r)), end="")
113 print("")
114
115 if len(regs_write) > 0:
116 print("\tRegisters modified:", end="")
117 for r in regs_write:
118 print(" %s" %(insn.reg_name(r)), end="")
119 print("")
120
121
122 # ## Test class Cs
123 def test_class():
124
125 for (arch, mode, code, comment) in all_tests:
126 print("*" * 16)
127 print("Platform: %s" % comment)
128 print("Code: %s" % code.hex(' '))
129 print("Disasm:")
130
131 try:
132 md = Cs(arch, mode)
133 md.detail = True
134 for insn in md.disasm(code, 0x2c):
135 print_insn_detail(insn)
136 print ()
137 print("0x%x:\n" % (insn.address + insn.size))
138 except CsError as e:
139 print("ERROR: %s" % e)
140
141
142 if __name__ == '__main__':
143 test_class()