Debian Codegraph @

suite/regress/test_arm64_bra.py

raw symbols SCIP
1 import unittest
2 from capstone import *
3 from capstone.arm64 import *
4
5 class ARM64BRAARegAccessTest(unittest.TestCase):
6
7 # These instructions should all have all their register operands being READ.
8 # https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/BRAA--BRAAZ--BRAB--BRABZ--Branch-to-Register--with-pointer-authentication-
9 PATTERNS = [
10 ("5F 08 1F D6", "braaz x2"),
11 ("11 0A 1F D7", "braa x16, x17"),
12 ("1F 0C 1F D6", "brabz x0"),
13 ("11 0E 1F D7", "brab x16, x17"),
14 ]
15
16 def setUp(self):
17 self.insts = []
18 self.cs = Cs(CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN)
19 self.cs.detail = True
20
21 for pattern, asm in self.PATTERNS:
22 # Disassemble the instruction. Any error here means Capstone doesn't handle the instruction (maybe the wrong branch)
23 inst = next(self.cs.disasm(bytes.fromhex(pattern), 0))
24
25 expected_regs_read = list(map(lambda r: r.strip(', '), asm.split()[1:]))
26 expected_regs_written = [] # nothing written
27 expected_regs = [expected_regs_read, expected_regs_written]
28
29 self.insts.append((inst, asm, expected_regs))
30
31
32 def test_regs_access(self):
33 """Check that the `regs_access` API provides correct data"""
34
35 for inst, asm, expected_regs in self.insts:
36
37 # Check that the instruction writes the first register operand and reads the second
38 for i, decoded_regs in enumerate(map(lambda l: list(map(self.cs.reg_name, l)), inst.regs_access())):
39 self.assertEqual(decoded_regs, expected_regs[i], "%s has %r %s registers instead of %r" % (asm, decoded_regs, ["read", "written"][i], expected_regs[i]))
40
41
42 def test_operands(self):
43 """Check that the `operands` API provides correct data"""
44 for inst, asm, expected_regs in self.insts:
45 ops = inst.operands
46
47 expected_regs_read, expected_regs_written = expected_regs
48 self.assertEqual(len(expected_regs_written), 0)
49 #print("Ensuring %s has the following read registers: %r" % (asm, expected_regs_read))
50 self.assertEqual(len(ops), len(expected_regs_read))
51
52 for i, op in enumerate(ops):
53 self.assertEqual(op.type, CS_OP_REG, "%s has operand %d with invalid type" % (asm, i))
54 self.assertEqual(op.access, CS_AC_READ, "%s has operand %d with invalid access" % (asm, i))
55
56 if __name__ == '__main__':
57 unittest.main()