1#!/usr/bin/python
2
3from __future__ import print_function
4
5import time
6
7from unicorn import *
8from unicorn.x86_const import *
9
10import objgraph
11
12import regress
13
14ADDRESS = 0x8048000
15STACK_ADDRESS = 0xffff000
16STACK_SIZE = 4096
17'''
1831 DB           xor ebx, ebx
1953              push ebx
2043              inc ebx
2153              push ebx
226A 02           push 2
236A 66           push 66h
2458              pop eax
2589 E1           mov ecx, esp
26CD 80           int 80h
27'''
28CODE = "\x31\xDB\x53\x43\x53\x6A\x02\x6A\x66\x58\x89\xE1\xCD\x80"
29EP = ADDRESS + 0x54
30
31def hook_code(mu, address, size, user_data):
32        print(">>> Tracing instruction at 0x%x, instruction size = %u" %(address, size))
33
34def emu_loop():
35    emu = Uc(UC_ARCH_X86, UC_MODE_32)
36    emu.mem_map(ADDRESS, 0x1000)
37    emu.mem_write(EP, CODE)
38
39    emu.mem_map(STACK_ADDRESS, STACK_SIZE)
40    emu.reg_write(UC_X86_REG_ESP, STACK_ADDRESS + STACK_SIZE)
41
42    i = emu.hook_add(UC_HOOK_CODE, hook_code, None)
43    emu.hook_del(i)
44
45    emu.emu_start(EP, EP + len(CODE), count = 3)
46    print("EIP: 0x%x" % emu.reg_read(UC_X86_REG_EIP))
47
48def debugMem():
49    import gc
50    gc.collect()  # don't care about stuff that would be garbage collected properly
51    #print("Orphaned objects in gc.garbage:", gc.garbage)
52    assert(len(objgraph.by_type("Uc")) == 0)
53    #assert(len(objgraph.get_leaking_objects()) == 0)
54
55class EmuLoopReferenceTest(regress.RegressTest):
56    def runTest(self):
57        for i in range(5):
58            emu_loop()
59        debugMem()
60
61
62if __name__ == '__main__':
63    regress.main()