-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
91 lines (77 loc) · 2.27 KB
/
functions.py
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
84
85
86
87
88
89
90
91
class Mem(object):
def __init__(self, addr, size, isUsed):
self.addr = addr
self.size = size
self.isUsed = isUsed
class Function(object):
argc = 0
testcases = (
(None, None),
)
def __init__(self, emu, startEa, endEa):
self.emu = emu
self.startEa = startEa
self.endEa = endEa
self.ownMem = []
self.debug = False
self.debug_func = None
def dlog(self, s):
if self.debug == True:
if self.debug_func is None:
print(s)
else:
self.debug_func(s)
def __del__(self):
self.unmapAllMem()
def cleanup(self):
self.emu.restore()
self.emu.preStack()
def test(self):
for case in self.testcases:
self.cleanup()
if not self.checkOne(case):
return False
self.clearMem()
if not self.checkTwo():
return False
return True
def start(self):
try:
self.emu.startEmu(self.startEa, self.endEa)
except Exception as e:
raise e
def getMem(self, size):
if self.ownMem:
for mem in self.ownMem:
if mem.isUsed:
continue
elif mem.size <= size:
mem.isUsed = True
return mem.addr
addr = self.emu.allocMem(size)
self.ownMem.append(Mem(addr, self.emu.pageAlign(size), True))
return addr
def clearMem(self):
for mem in self.ownMem:
if mem.isUsed:
self.emu.writeMem(mem.addr, b'\x00'*mem.size)
mem.isUsed = False
def unmapAllMem(self):
while self.ownMem:
mem = self.ownMem.pop()
self.emu.target.mem_unmap(mem.addr, mem.size)
def setArgWithMem(self, i, size, data=None):
addr = self.getMem(size)
if data:
self.emu.writeMem(addr, data)
self.emu.setArgv(i, addr)
return addr
def setArgWithImm(self, i, value):
self.emu.setArgv(i, value)
def checkOne(self, case):
pass
def checkTwo(self):
'''
do more check when things get funny
'''
return True