-
Notifications
You must be signed in to change notification settings - Fork 14
/
GraphletsConstraints.py
344 lines (252 loc) · 10.2 KB
/
GraphletsConstraints.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import itertools
from constraint import Problem,MinConflictsSolver
from x86Analyzer import X86AnalyzerBase
traceHack = False
class GraphletsConstraints(X86AnalyzerBase):
#__registers32Bit = ["eax","ebx","ecx","edx","esi","edi","ebp","esp"]
__dictNames = ['ref','tar']
__printConds = False
def __init__(self,nodeGradesInfos=[]):
X86AnalyzerBase.__init__(self,nodeGradesInfos)
self.problem = Problem(MinConflictsSolver())
# this is to make it human readable ..
# we will generator the solution only when we need it and then cache it
# TODO make __
self.sol = None
for key in self.rewriteDict.keys():
self.rewriteDict[key]['symbolCache'] = {}
self.rewriteDict[key]['curLine'] = 1
self.rewriteDict[key]['curPos'] = 1
self.createRewrite()
def getEmptyDict(self):
d = X86AnalyzerBase.getEmptyDict(self)
for key in d.keys():
d[key]['transitions'] = []
d[key]['valuesTrackDict'] = {}
#if key != self.REGISTER:
d[key]['domain'] = set()
#self.rewriteDict[key]['inCmdCounter'] = 1
return d
# this will add recorded value to dict, even if there is a conflict it will be recorded...
#
def insertToDictWithType(self,tarCmdNum,fromStr,refCmdNum,toStr,typeStr,dict2insert=None):
assert(dict2insert != None)
dict2insert[typeStr]['transitions'].append((tarCmdNum,fromStr,toStr))
#if typeStr != self.REGISTER:
dict2insert[typeStr]['domain'].add(toStr)
def commitChanges(self,tmpDict):
for key in self.rewriteDict.keys():
self.rewriteDict[key]['transitions'].extend(tmpDict[key]['transitions'])
#if key != self.REGISTER:
self.rewriteDict[key]['domain'].update(tmpDict[key]['domain'])
# black list has no generation:) , we can use the rwdict type as they are the same..
def getRewriteWithType(self,tarCmdNum,fromStr,typeStr,FoundBlacklistElement):
if self.sol == None:
self.callSol()
if self.rewriteDict[typeStr]['curLine'] < tarCmdNum :
self.rewriteDict[typeStr]['curLine'] = tarCmdNum
self.rewriteDict[typeStr]['curPos'] = 1
varName = self.getVarName(self.getShort(typeStr), tarCmdNum, self.rewriteDict[typeStr]['curPos'])
self.rewriteDict[typeStr]['curPos'] += 1
if self.sol != None and varName in self.sol:
# we have a value! update cache and return it
newVal = self.sol[varName]
self.rewriteDict[typeStr]['symbolCache'][fromStr] = newVal
return newVal
elif fromStr in self.rewriteDict[typeStr]['symbolCache']:
return self.rewriteDict[typeStr]['symbolCache'][fromStr]
else:
#not found in this type's map in this generation, return original
return fromStr
def getShort(self,name):
if name == self.FUNCNAME:
return "f"
elif name == self.VAR:
return "m"
elif name == self.REGISTER:
return "r"
else :
raise hell
def getVarName(self,preFix,curLine,curPos):
return preFix + str(curLine) + "-" + str(curPos) + "_TAR"
# TODO - make this __
def callSol(self):
# we need to go over each dict that is useable, and feed vars and constraints
for typeDict in [x for x in self.rewriteDict.keys() if self.rewriteDict[x]['useAble']==True ]:
curLine = 1
curPos = 1
preFix = self.getShort(typeDict)
#if typeDict != self.REGISTER:
domain = list(self.rewriteDict[typeDict]['domain'])
#else:
# domain =self.__registers32Bit
for (line,tarStr,refStr) in self.rewriteDict[typeDict]['transitions']:
if curLine < line:
curPos = 1
curLine = line
tarName = self.getVarName(preFix, curLine, curPos)
self.problem.addVariables([tarName],domain)
if (self.__printConds):
print "CONS(text) -> " + tarName + " == " + refStr
self.problem.addConstraint(self.checkInputVsTarget(refStr),[tarName])
if tarStr in self.rewriteDict[typeDict]['valuesTrackDict'] != None:
if (self.__printConds):
print "CONS(bag) -> " + self.rewriteDict[typeDict]['valuesTrackDict'][tarStr] + " == " + tarName
self.problem.addConstraint(self.varsEqual,[tarName,self.rewriteDict[typeDict]['valuesTrackDict'][tarStr]])
self.rewriteDict[typeDict]['valuesTrackDict'][tarStr] = tarName
curPos+=1
self.sol = self.problem.getSolution()
if traceHack:
print "(Number of broken - " + str(self.problem.getSolver().getHack()) + ")",
def varsEqual(self,v1, v2):
return v1==v2
def checkInputVsTarget(self,target):
def retFunc(inputVal):
return inputVal == target
return retFunc
def getSolution(self):
if self.sol == None:
self.callSol()
return self.sol
def printSol(self,sol):
#better call sol!
if sol == None:
print "NO SOL!"
return
last = 1
for key in sorted(sol.iterkeys()):
if int(key[1:2]) != last:
last = int(key[1:2])
print ""
print key + ": " + sol[key] + " ",
def getBrokenNumber(self):
return self.problem.getSolver().getHack()
def NodeInfoFromCode(refCmds,tarCmds):
matched = map(lambda (ref,tar): {'ref':ref,'tar':tar},zip(refCmds,tarCmds))
for index,record in enumerate(matched):
record['tarCmdNum'] = index+1
record['refCmdNum'] = index+1
return {'matchedCmds':matched ,'deletedCmds':[],'insertedCmds':[],'gradesDict':{'ratio':0,'contain':0},'refCode':";".join(refCmds)+";",'tarCode':";".join(tarCmds)+";"}
def selfTest():
refCmds1 = ["MOV eax,ebx" ]
tarCmds1 = ["MOV ecx,edx" ]
refCmds2 = ["MOV esi,eax" ]
tarCmds2 = ["MOV edi,ecx" ]
gc = GraphletsConstraints([NodeInfoFromCode(refCmds1,tarCmds1),NodeInfoFromCode(refCmds2,tarCmds2)])
#gc.callSol()
#gc.printSol(gc.getSolution())
for cmds in gc.getRW():
pass
#print cmds
"""
(1)Add ecx,ebx
(+)Xor ebx,ebx
(2)Mov [IO],ecx
(3)Mov ecx,[IO+4]
(4)Add eax,ecx
(1) Add ebx,esi
(2) Mov [ME],ebx
(X) Lea ebx, [ME8]
(3) Mov esi,[ME+4]
(4) Add eax,esi
"""
refCmds1 = ["Add ecx,ebx" ]
refCmds2 = ["Mov [var_IO],ecx" ]
refCmds3 = ["Mov ecx,[var_IO+4] " ]
refCmds4 = ["Add eax,ecx" ]
tarCmds1 = ["Add ebx,esi" ]
tarCmds2 = ["Mov [var_ME],ebx" ]
tarCmds3 = ["Mov esi,[var_ME+4]" ]
tarCmds4 = ["Add eax,esi" ]
gc = GraphletsConstraints([NodeInfoFromCode(refCmds1,tarCmds1),NodeInfoFromCode(refCmds2,tarCmds2),NodeInfoFromCode(refCmds3,tarCmds3),NodeInfoFromCode(refCmds4,tarCmds4)])
#gc.callSol()
#gc.printSol(gc.getSolution())
for cmds in gc.getRW():
print cmds
""" REF 1 2 TAR 1 2
============================
1: MOV eax,ebx; MOV ecx,edx
2: MOV esi,eax; MOV edi,ecx
"""
"""
gc = GraphletsConstraints()
gc.addNewReg(1, 1, "eax", "ecx")
gc.addNewReg(1, 2, "ebx", "edx")
gc.addNewReg(2, 1, "esi", "edi")
gc.addNewReg(2, 2, "eax", "ecx")
sol = gc.getSolution()
#gc.printSol(sol)
"""
""" REF 1 2 TAR 1 2
============================
1: MOV eax,ebx; MOV ecx,edx
2: MOV ecx,eax; MOV edi,ecx
3: MOV edx,eax; MOV eax,ecx
4: MOV esi,edx; MOV edi,ecx
ecx -> eax
edx -> ebx
edi -> ecx
eax -> edx
edi -> esi
ecx -> edx
"""
"""
gc = GraphletsConstraints()
gc.addNewReg(1, 1, "eax", "ecx")
gc.addNewReg(1, 2, "ebx", "edx")
gc.addNewReg(2, 1, "ecx", "edi")
gc.addNewReg(2, 2, "eax", "ecx")
gc.addNewReg(3, 1, "edx", "eax")
gc.addNewReg(3, 2, "eax", "ecx")
gc.addNewReg(4, 1, "esi", "edi")
gc.addNewReg(4, 2, "edx", "ecx")
#gc.addNewReg(5, 1, "edi", "esi")
#gc.addNewReg(5, 2, "eax", "ecx")
sol = gc.getSolution()
#gc.printSol(sol)
"""
""" REF 1 2 TAR 1 2
============================
1: MOV eax,ebx; MOV ecx,edx
2: MOV ecx,eax; MOV edi,ecx
3: MOV edx,eax; MOV eax,ecx
4: MOV esi,edx; MOV edi,ecx
5: MOV edi,eax MOV esi,ecx
ecx -> eax (4) - 1
edx -> ebx
edi -> ecx (2)
eax -> edx
esi -> edi
"""
"""
gc = GraphletsConstraints()
gc.addNewReg(1, 1, "eax", "ecx")
gc.addNewReg(1, 2, "ebx", "edx")
gc.addNewReg(2, 1, "ecx", "edi")
gc.addNewReg(2, 2, "eax", "ecx")
gc.addNewReg(3, 1, "edx", "eax")
gc.addNewReg(3, 2, "eax", "ecx")
gc.addNewReg(4, 1, "esi", "edi")
gc.addNewReg(4, 2, "edx", "ecx")
gc.addNewReg(5, 1, "edi", "esi")
gc.addNewReg(5, 2, "eax", "ecx")
sol = gc.getSolution()
gc.printSol(sol)
"""
if __name__ == '__main__':
selfTest()
#
"""
problem = Problem(MinConflictsSolver())
vars = ["a", "b","c"]
problem.addVariables(vars, [1, 2, 3])
problem.addConstraint(lambda x,y: y> x, ["a","b"])
problem.addConstraint(lambda x,y: y> x, ["a","c"])
problem.addConstraint(lambda x,y: y> x, ["b","c"])
problem.addConstraint(lambda a: a> 1, ["a"])
problem.addConstraint(check, ["c"])
problem.addConstraint(lambda b: b <3, ["b"])
problem.addConstraint(lambda a,b: a != b, ["a","b"])
problem.addConstraint(lambda a,c: a != c, ["a","c"])
print problem.getSolution()
"""