File: callback-pc.py

package info (click to toggle)
unicorn-engine 2.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,912 kB
  • sloc: ansic: 379,830; python: 9,213; sh: 9,011; java: 8,609; ruby: 4,241; pascal: 1,805; haskell: 1,379; xml: 490; cs: 424; makefile: 348; cpp: 298; asm: 64
file content (83 lines) | stat: -rwxr-xr-x 2,944 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# reg_write() can't modify PC from within trace callbacks
# issue #210

import regress
from unicorn import *
from unicorn.arm_const import *

BASE_ADDRESS = 0x10000000
THUMB_CODE = b"\x83\xb0" * 5  # sub sp, #0xc
TARGET_PC = 0xffffffff


class CallBackPCTest(regress.RegressTest):

    def test_instruction_trace(self):
        # initialize emulator in ARM's Thumb mode
        mu = Uc(UC_ARCH_ARM, UC_MODE_THUMB)

        # map some memory
        mu.mem_map(BASE_ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(BASE_ADDRESS, THUMB_CODE)

        # setup stack
        mu.reg_write(UC_ARM_REG_SP, BASE_ADDRESS + 2 * 1024 * 1024)

        def __hook_callback(uc, address, size, user_data):
            regress.logger.debug(">>> Tracing instruction at 0x%x, instruction size = %u", address, size)
            regress.logger.debug(">>> Setting PC to %#x", TARGET_PC)

            uc.reg_write(UC_ARM_REG_PC, TARGET_PC)

        # tracing all instructions with customized callback
        mu.hook_add(UC_HOOK_CODE, __hook_callback)

        # emulate one instruction. since pc was modified to point to an unmapped
        # area, this is expected to fail
        with self.assertRaises(UcError) as raisedEx:
            mu.emu_start(BASE_ADDRESS, BASE_ADDRESS + len(THUMB_CODE), count=1)

        self.assertEqual(UC_ERR_FETCH_UNMAPPED, raisedEx.exception.errno)

        # the block callback set PC to 0xffffffff, so at this point, the PC
        # value should be 0xffffffff.
        self.assertEqual(TARGET_PC, mu.reg_read(UC_ARM_REG_PC) | 0b1)

    def test_block_trace(self):
        # initialize emulator in ARM's Thumb mode
        mu = Uc(UC_ARCH_ARM, UC_MODE_THUMB)

        # map some memory
        mu.mem_map(BASE_ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(BASE_ADDRESS, THUMB_CODE)

        # setup stack
        mu.reg_write(UC_ARM_REG_SP, BASE_ADDRESS + 2 * 1024 * 1024)

        def __hook_callback(uc, address, size, user_data):
            regress.logger.debug(">>> Tracing basic block at 0x%x, block size = 0x%x", address, size)
            regress.logger.debug(">>> Setting PC to %#x", TARGET_PC)

            uc.reg_write(UC_ARM_REG_PC, TARGET_PC)

        # trace blocks with customized callback
        mu.hook_add(UC_HOOK_BLOCK, __hook_callback)

        # emulate one instruction. since pc was modified to point to an unmapped
        # area, this is expected to fail
        with self.assertRaises(UcError) as raisedEx:
            mu.emu_start(BASE_ADDRESS, BASE_ADDRESS + len(THUMB_CODE), count=1)

        self.assertEqual(UC_ERR_FETCH_UNMAPPED, raisedEx.exception.errno)

        # the block callback set PC to 0xffffffff, so at this point, the PC
        # value should be 0xffffffff.
        self.assertEqual(TARGET_PC, mu.reg_read(UC_ARM_REG_PC) | 0b1)


if __name__ == '__main__':
    regress.main()