Debian Codegraph @

suite/regress/test_arm64_mov.py

raw symbols SCIP
1 import unittest
2 from capstone import *
3 from capstone.arm64 import *
4
5 # By Stevie Lavern <[email protected]>, 2023.
6 class ARM64MovRegAccessTest(unittest.TestCase):
7 # These instructions should all have their 1st operand register being WRITTEN and not READ.
8 PATTERNS_IMM = [
9 ("00 00 80 D2", "mov x0, #0"),
10 ("E2 66 82 52", "movz w2, #0x1337"),
11 ("A3 D5 9B 92", "movn x3, #0xdead"),
12 ("E4 DD 97 12", "movn w4, #0xbeef"),
13 ("03 40 A0 D2", "mov x3, #0x2000000") # aliased to MOVZXi.
14 ]
15
16 PATTERNS_REG = [
17 ("00 20 18 D5", "msr ttbr0_el1, x0"),
18 ("20 20 38 D5", "mrs x0, ttbr1_el1")
19 ]
20
21 def setUp(self):
22 self.insts = []
23 self.cs = Cs(CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN)
24 self.cs.detail = True
25
26 for pattern, asm in self.PATTERNS_IMM:
27 l = list(self.cs.disasm(bytes.fromhex(pattern), 0))
28 self.assertTrue(len(l) == 1)
29
30 _, expected_reg_written, _ = asm.split()
31 # strip comma and [].
32 expected_reg_written = [expected_reg_written[:-1]]
33 expected_reg_read = [] # nothing should be read.
34 expected_regs = [expected_reg_read, expected_reg_written]
35
36 self.insts.append((l[0], asm, expected_regs, False))
37
38 for pattern, asm in self.PATTERNS_REG:
39 l = list(self.cs.disasm(bytes.fromhex(pattern), 0))
40 self.assertTrue(len(l) == 1)
41
42 _, expected_reg_written, expected_reg_read = asm.split()
43 # strip comma and [], only keep general purpose registers.
44 expected_reg_written = expected_reg_written[:-1]
45 expected_reg_written = [expected_reg_written] if expected_reg_written[0].lower() == 'x' else []
46 expected_reg_read = [expected_reg_read] if expected_reg_read[0].lower() == 'x' else []
47 expected_regs = [expected_reg_read, expected_reg_written]
48
49 self.insts.append((l[0], asm, expected_regs, True))
50
51 def test_regs_access(self):
52 """Check that the `regs_access` API provides correct data"""
53
54 for inst, asm, expected_regs, pattern_reg in self.insts:
55 # Check that the instruction writes the first register operand and reads the second.
56 for i, decoded_regs in enumerate(map(lambda l: list(map(self.cs.reg_name, l)), inst.regs_access())):
57 self.assertEqual(decoded_regs, expected_regs[i], "%s has %r %s registers instead of %r" % (asm, decoded_regs, ["read", "written"][i], expected_regs[i]))
58
59 def test_operands(self):
60 """Check that the `operands` API provides correct data"""
61 for inst, asm, expected_regs, pattern_reg in self.insts:
62 ops = inst.operands
63 self.assertEqual(len(ops), 2)
64
65 reg_types = [CS_OP_REG, ARM64_OP_SYS] if pattern_reg else [CS_OP_REG]
66
67 self.assertIn(ops[0].type, reg_types, "%s has operand 0 with invalid type" % asm)
68 self.assertEqual(ops[0].access, CS_AC_WRITE, "%s has operand 0 with invalid access" % asm)
69 if pattern_reg:
70 self.assertIn(ops[1].type, reg_types, "%s has operand 0 with invalid type" % asm)
71 else:
72 self.assertEqual(ops[1].type, CS_OP_IMM, "%s has operand 0 with invalid type" % asm)
73
74 if __name__ == '__main__':
75 unittest.main()
76