Debian Codegraph @

suite/regress/test_arm64_ldr_registers.py

raw symbols SCIP
1 import unittest
2 from capstone import *
3 from capstone.arm64 import *
4
5 _python3 = sys.version_info.major == 3
6
7
8 class SubRegTest(unittest.TestCase):
9
10 PATTERNS = [
11 ("41 00 40 F9", "ldr x1, [x2]"),
12 ("41 00 40 39", "ldrb w1, [x2]"),
13 ("41 00 C0 39", "ldrsb w1, [x2]"),
14 ("41 00 40 79", "ldrh w1, [x2]"),
15 ("88 c2 bf f8", "ldapr x8, [x20]"),
16 ]
17
18 def setUp(self):
19 self.insts = []
20 self.cs = Cs(CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN)
21 self.cs.detail = True
22
23 for pattern, asm in self.PATTERNS:
24 if _python3:
25 l = list(self.cs.disasm(bytes.fromhex(pattern), 0))
26 else:
27 l = list(self.cs.disasm(bytearray.fromhex(pattern), 0))
28 self.assertTrue(len(l) == 1)
29
30 _, expected_reg_written, expected_reg_read = asm.split()
31 # strip comma and []
32 expected_reg_written = expected_reg_written[:-1]
33 expected_reg_read = expected_reg_read[1:-1]
34 expected_regs = [expected_reg_read, expected_reg_written]
35
36 self.insts.append((l[0], asm, expected_regs))
37
38
39 def test_registers(self):
40 """Check that the `regs_access` API provides correct data"""
41
42 for inst, asm, expected_regs in self.insts:
43
44 # Check that the instruction writes the first register operand and reads the second
45 for i, decoded_regs in enumerate(map(lambda l: list(map(self.cs.reg_name, l)), inst.regs_access())):
46 self.assertEqual(len(decoded_regs), 1, "%s has %d %s registers instead of 1" % (asm, len(decoded_regs), ["read", "written"][i]))
47 decoded_reg = decoded_regs[0]
48 self.assertEqual(expected_regs[i], decoded_reg, "%s test"%i)
49
50 def test_operands(self):
51 """Check that the `operands` API provides correct data"""
52 for inst, asm, expected_regs in self.insts:
53 ops = inst.operands
54 self.assertEqual(len(ops), 2)
55
56 self.assertEqual(ops[0].type, CS_OP_REG, "%s has operand 0 with invalid type" % asm)
57 self.assertEqual(ops[0].access, CS_AC_WRITE, "%s has operand 0 with invalid access" % asm)
58 self.assertEqual(ops[1].type, CS_OP_MEM, "%s has operand 0 with invalid type" % asm)
59 self.assertEqual(self.cs.reg_name(ops[1].mem.base), expected_regs[0], "%s has operand 1 with invalid reg" % asm)
60 self.assertEqual(ops[1].access, CS_AC_READ, "%s has operand 1 with invalid access" % asm)
61
62 if __name__ == '__main__':
63 unittest.main()