-
Notifications
You must be signed in to change notification settings - Fork 30
/
Function.py
213 lines (155 loc) · 6.21 KB
/
Function.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
This file is part of SEA.
reserbot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
reserbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SEA. If not, see <http://www.gnu.org/licenses/>.
Copyright 2013 by neuromancer
"""
from Operand import *
class Function:
"""An abstract function class"""
parameter_typs = []
parameter_locs = []
parameter_vals = []
read_operands = []
write_operands = []
return_type = None
def __init__(self, pins = None, pbase = None):
self.parameter_typs = []
self.parameter_locs = []
self.parameter_vals = []
self.read_operands = []
self.write_operands = []
def getParameters(self):
return self.parameter_vals
def getParameterLocations(self):
return self.parameter_locs
def getReadVarOperands(self):
return filter(lambda o: o.isVar(), self.read_operands)
def getWriteVarOperands(self):
return filter(lambda o: o.isVar(), self.write_operands)
def getEq(self):
return []
def __loadParameters__(self, pars):
self.parameter_locs = []
self.parameter_vals = []
for (loc, val) in pars:
self.parameter_locs.append(loc)
self.parameter_vals.append(val)
def __locateParameter__(self, disp, size):
#assert(disp<=0)
mem_source = self.pbase.mem_source
mem_offset = self.pbase.mem_offset + disp
name = mem_source + "@" + str(mem_offset)
return Operand(name, size, mem_source, mem_offset)
class Skip_Func(Function):
pass
class Gets_Func(Function):
parameter_typs = [("char * str", "DWORD", 0, True)]
return_type = "void"
def __init__(self, pbase = None, pars = None):
self.internal_size = 84
if (type(pbase) <> type(None)):
self.pbase = pbase
for (ptype, size, disp, needed) in self.parameter_typs:
self.parameter_locs.append((ptype, self.__locateParameter__(disp, size), needed))
else:
self.__loadParameters__(pars)
# populate read operands
self.read_operands.append(self.parameter_locs[0])
# it is not necesary, since these locations won't be modified!
#for i in range(self.internal_size):
# self.read_operands.append(Operand("stdin:"+str(i), "BYTE"))
# populate write operands
for i in range(self.internal_size):
mem_source = self.parameter_vals[0].mem_source
mem_offset = self.parameter_vals[0].mem_offset + i
name = mem_source + "@" + str(mem_offset)
self.write_operands.append(Operand(name, "BYTE", mem_source, mem_offset))
#print len(self.write_operands)
class Strlen_Func(Function):
parameter_typs = [("char * str", "DWORD", 0, True)]
return_type = "int"
def __init__(self, pbase = None, pars = None):
self.internal_size = 10
if (type(pbase) <> type(None)):
self.pbase = pbase
for (ptype, size, disp, needed) in self.parameter_typs:
self.parameter_locs.append((ptype, self.__locateParameter__(disp, size), needed))
else:
self.__loadParameters__(pars)
# populate read operands
self.read_operands.append(self.parameter_locs[0])
# return value
self.write_operands.append(Operand("eax", "DWORD"))
class Strcpy_Func(Function):
parameter_typs = [("char * str", "DWORD", 0, True), ("char * str", "DWORD", 4, True)]
return_type = "void"
def __init__(self, pbase = None, pars = None):
self.internal_size = 256+4
if (type(pbase) <> type(None)):
self.pbase = pbase
for (ptype, size, disp, needed) in self.parameter_typs:
self.parameter_locs.append((ptype, self.__locateParameter__(disp, size), needed))
else:
self.__loadParameters__(pars)
# populate read operands
self.read_operands.append(self.parameter_locs[0])
self.read_operands.append(self.parameter_locs[1])
for i in range(self.internal_size):
msrc = self.parameter_vals[1].mem_source
if ("arg[" in msrc): #it's an argument!
self.read_operands.append(Operand(msrc+":"+str(i), "BYTE"))
else:
assert(0)
#self.read_operands.append(Operand("i:"+str(i), "BYTE"))
# populate write operands
for i in range(self.internal_size):
mem_source = self.parameter_vals[0].mem_source
mem_offset = self.parameter_vals[0].mem_offset + i
name = mem_source + "@" + str(mem_offset)
self.write_operands.append(Operand(name, "BYTE", mem_source, mem_offset))
class Alloc_Func(Function):
parameter_typs = [("int", "DWORD", 0, True)]
return_type = "void *"
def __init__(self, pbase = None, pars = None):
self.parameter_locs = []
self.parameter_vals = []
self.read_operands = []
self.write_operands = []
if (type(pbase) <> type(None)):
self.pbase = pbase
for (ptype, size, disp, needed) in self.parameter_typs:
self.parameter_locs.append((ptype, self.__locateParameter__(disp, size), needed))
else:
self.__loadParameters__(pars)
class Free_Func(Function):
parameter_typs = [("void *", "DWORD", 0, True)]
return_type = "void"
def __init__(self, pbase = None, pars = None):
self.parameter_locs = []
self.parameter_vals = []
self.read_operands = []
self.write_operands = []
if (type(pbase) <> type(None)):
self.pbase = pbase
for (ptype, size, disp, needed) in self.parameter_typs:
self.parameter_locs.append((ptype, self.__locateParameter__(disp, size), needed))
else:
self.__loadParameters__(pars)
funcs = {
"printf" : Skip_Func,
"puts" : Skip_Func,
"gets" : Gets_Func,
"malloc" : Alloc_Func,
"free" : Free_Func,
"strcpy" : Strcpy_Func,
"strlen" : Strlen_Func,
}