-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPVF.py
2308 lines (2216 loc) · 100 KB
/
PVF.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import sys
import os
import random
import networkx as nx
import copy
from itertools import izip_longest
import setting as config
import InstructionAbstraction
from sets import Set
from multiprocessing import Manager, Process, current_process, Queue
import math
import time
import collections
from collections import OrderedDict
aceBits = 0
crashBits = 0
stack = []
rangeList = {}
gcount = 0
finalBits = []
loadstore = []
finalBits_control = []
control_start = 0
lower_bound = 0
f_level = 1
loadstore_bits = {}
pop = [1]
K = 0.3
ranking = {}
ranking_pvf = {}
def random_subset(iterator, K):
result = []
N = 0
for item in iterator:
N += 1
if len(result) == 0:
result.append(item)
if len(iterator)/len(result) > K:
result.append(item)
else:
s = int(random.random() * N)
if s < len(result):
result[s] = item
return result
def ordered_subset(iterator,K):
unordered = {}
noplus = []
count = 0
serialnumber = 0
for item in iterator:
serialnumber = count
if "constant" in item:
res = re.findall('constant\d+',item)
serialnumber = res[0].split("constant")[1]
unordered[serialnumber] = iterator.index(item)
else:
if "+" in item:
serialnumber = int(item.split("+")[1])
unordered[serialnumber] = iterator.index(item)
else:
noplus.append(item)
if len(unordered.keys()) == 1:
for item in iterator:
serialnumber = int(item)
unordered[serialnumber] = iterator.index(item)
ordered = collections.OrderedDict(sorted(unordered.items()))
klist = []
klist.extend(noplus)
print klist
for k,v in ordered.iteritems():
klist.append(iterator[v])
if len(klist) > len(iterator)*K:
break
print klist
return klist
def isfloat(x):
try:
a = float(x)
except ValueError:
return False
else:
return True
def isint(x):
try:
a = float(x)
b = int(a)
except ValueError:
return False
else:
return a == b
def iszero(x):
try:
if isfloat(x):
x = float(x)
if x == float(0):
return True
else:
return False
if isint(x):
x = int(x)
if x == 0:
return True
else:
return False
except ValueError:
return False
def removeDuCycle(ranking, ref):
for key, value in ranking.items():
cycles = ref[key]
for cycle in cycles:
min = sys.maxint
for item in value:
if int(cycle) == int(item[0]):
if int(item[1]) < min:
min = int(item[1])
ranking[key][:] = [tup for tup in ranking[key] if (int(tup[0]) == int(cycle) and int(tup[1]) == min) or int(tup[0]) != int(cycle)]
# if int(cycle) == int(item[0]) and int(item[1]) == min:
# pass
# if int(cycle) == int(item[0]) and int(item[1]) != min:
# ranking[key].remove(item)
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
class PVF:
def __init__(self, G, trace, indexMap,global_hash_cycle, cycle_index_lookup, index_cycle_lookup):
self.G = G
assert(len(trace) == 5)
self.trace = trace[0]
self.remap = trace[1]
self.memory = trace[2]
fm = InstructionAbstraction.FunctionMapping(config.IRpath)
structMap = fm.extractStruct()
self.structMap = structMap
self.indexMap = indexMap
self.global_hash_cycle = global_hash_cycle
self.cycle_index_lookup = cycle_index_lookup
self.index_cycle_lookup = index_cycle_lookup
def ordered_subset_bycycle(self, iterator,K):
unordered = {}
noplus = []
count = 0
serialnumber = 0
for item in iterator:
index = int(self.G.node[item]['cycle'])
if index not in unordered:
unordered[index] = iterator.index(item)
else:
index += random.random()
unordered[index] = iterator.index(item)
ordered = collections.OrderedDict(sorted(unordered.items()))
klist = []
for k,v in ordered.iteritems():
klist.append(iterator[v])
if len(klist) >= len(iterator)*K:
break
return klist
def bfs(self, G, source):
bfs_nodes = []
bfs_nodes.extend(source)
visited = Set()
gl_pre = []
i = 0
while i < len(bfs_nodes):
if "mem" not in G.node[bfs_nodes[i]]:
for edge in G.out_edges(bfs_nodes[i]):
if edge[1] not in visited:
bfs_nodes.append(edge[1])
if "pre" in G.node[bfs_nodes[i]]:
#gl_pre.append(G.node[bfs_nodes[i]]['pre'])
bfs_nodes.append(G.node[bfs_nodes[i]]['pre'])
visited.add(bfs_nodes[i])
else:
#for edge in G.out_edges(bfs_nodes[i]):
# if "store" in G.edge[edge[0]][edge[1]]['opcode']:
# if edge[1] not in visited:
# bfs_nodes.append(edge[1])
# if G.node[edge[1]]['post'] not in visited:
# bfs_nodes.append(G.node[edge[1]]['post'])
# elif edge[1] in gl_pre:
# bfs_nodes.append(edge[1])
# gl_pre.remove(edge[1])
if "store" in G.node[bfs_nodes[i]]:
op = G.node[bfs_nodes[i]]['store']
if op not in visited:
bfs_nodes.append(op)
if G.node[op]['post'] not in visited:
bfs_nodes.append(G.node[op]['post'])
#for node in gl_pre:
# if G.has_edge(node,bfs_nodes[i]):
# bfs_nodes.append(node)
# gl_pre.remove(node)
# break
i += 1
return bfs_nodes
def computeCrashRate(self):
for node in self.G.nodes_iter():
numOut = 0
for edge in self.G.out_edges(node):
if "virtual" not in self.G.edge[edge[0]][edge[1]]['opcode']:
numOut += 1
#if numOut == 0:
# print node
# for edge in uG.in_edges(node):
# if "alloca" == uG.edge[edge[0]][edge[1]]['opcode']:
# numOut += 1
self.G.node[node]['out_edge'] = numOut
self.simplePVF(self.G,self.G)
def computePVF(self, targetList):
#----------------
# Get the predecessors of the target node
#----------------
global aceBits
global crashBits
global lower_bound
global K
predecessors = []
predecessors_control = []
predecessors_memory = []
for node in self.G:
oprandlist = []
opcode = ""
for target in targetList:
if target in node:
#predecessors.append(node)
for edge in self.G.out_edges(node):
if "virtual" in self.G.edge[edge[0]][edge[1]]['opcode']:
predecessors_memory.append(edge[1])
for edge in self.G.in_edges(node):
if "virtual" not in self.G.edge[edge[0]][edge[1]]['opcode']:
oprandlist.append(edge[0])
opcode = self.G.edge[edge[0]][edge[1]]['opcode']
if opcode == "icmp" or opcode == "fcmp":
predecessors_control.extend(oprandlist)
predecessors_control.append(node)
#for node in predecessors[:]:
# for edge in self.G.out_edges(node):
# if self.G.edge[edge[0]][edge[1]]['opcode'] != 'virtual':
# predecessors.remove(node)
# i = 0
# while i < len(predecessors):
# flag = 0
# newlist = self.G.predecessors(predecessors[i])
# offsets = {}
# for newnode in newlist:
# if 'dest' in self.G.node[newnode]:
# if self.G.node[newnode]['dest'] in predecessors:
# flag = 1
# offset = predecessors.index(self.G.node[newnode]['dest'])
# if offset not in offsets:
# offsets[offset] = []:q
# offsets[offset].append(newnode)
# else:
# offsets[offset].append(newnode)
# else:
# if newnode not in predecessors:
# predecessors.append(newnode)
# if flag == 1:
# for node in offsets[sorted(offsets)[0]]:
# if node not in predecessors:
# predecessors.append(node)
# print "nodes in predecessors: "+str(len(predecessors))+" "+str(i)+" "+str(len(newlist))
# assert(len(predecessors) <= self.G.number_of_nodes())
# i += 1
#------------------------------------
# to test the scalability of the ePVF
#------------------------------------
print "TEST"
print len(predecessors_memory)
print len(predecessors_control)
#No need for selecting
#print predecessors_memory
predecessors_memory = self.ordered_subset_bycycle(predecessors_memory,K)
predecessors_control = self.ordered_subset_bycycle(predecessors_control,K)
#print predecessors_memory
#print "TEST"
#print len(predecessors_memory)
#print len(predecessors_control)
ReG = self.G.reverse()
print len(self.G.nodes())
p_set = set(predecessors_memory)
predecessors_memory = list(p_set)
print len(predecessors_memory)
print len(ReG.nodes())
sub_nodes = self.bfs(ReG,predecessors_memory)
subG = self.G.subgraph(sub_nodes)
print "Data flow graph"
print len(subG)
#processes = [Process(target=self.do_work, args=(ReG,target,sub_nodes)) for target in predecessors]
#for p in processes:
# p.start()
#for p in processes:
# p.join()
p_set = set(predecessors_control)
predecessors_control = list(p_set)
#for target in predecessors_control:
# if target in ReG:
# T = nx.bfs_tree(ReG, target)
# tnodes = T.nodes()
# control_nodes.extend(tnodes)
# ReG.remove_nodes_from(tnodes)
control_nodes = self.bfs(ReG,predecessors_control)
subControlG = self.G.subgraph(control_nodes)
print "Control flow graph"
print len(subControlG)
# source = []
# for node in subG:
# if subG.in_degree(node) == 0:
# source.append(node)
# if subG.in_degree(node) == 1:
# for edge in subG.in_edges(node):
# if subG.edge[edge[0]][edge[1]]['opcode'] == 'virtual':
# source.append(edge[1])
predecessors.extend(predecessors_control)
predecessors.extend(predecessors_memory)
p_set = set(predecessors)
predecessors = list(p_set)
#for target in predecessors:
# if target in ReG:
# T = nx.bfs_tree(ReG, target)
# tnodes = T.nodes()
# sub_nodes.extend(tnodes)
# ReG.remove_nodes_from(tnodes)
#sub_nodes.extend(T.nodes())
uG_nodes = self.bfs(ReG,predecessors)
nodes = set(uG_nodes)
uG_nodes = list(nodes)
uG = self.G.subgraph(uG_nodes)
print "Union Graph"
print len(uG)
#self.simplePVF(subG, subControlG)
#self.simplePVF(subControlG,subG)
for node in uG.nodes_iter():
numOut = 0
for edge in uG.out_edges(node):
if "virtual" not in uG.edge[edge[0]][edge[1]]['opcode']:
numOut += 1
#if numOut == 0:
# print node
# for edge in uG.in_edges(node):
# if "alloca" == uG.edge[edge[0]][edge[1]]['opcode']:
# numOut += 1
uG.node[node]['out_edge'] = numOut
print time.time()
print
#for node in subG.nodes_iter():
# numOut = 0
# for edge in subG.out_edges(node):
# if "virtual" not in subG.edge[edge[0]][edge[1]]['opcode']:
# numOut += 1
# subG.node[node]['out_edge'] = numOut
if lower_bound == 1:
self.simplePVF(subG,subControlG)
else:
self.simplePVF(uG,subG)
#visited = self.traverse4PVF(subG, "bo%2", targetList)
#for item in subG.nodes():
# if item not in visited:
# print "***"
# print item
return subG
def traverse4PVF(self, G, source, targetList):
global aceBits
visited = []
visited.append(source)
i = 0
while i < len(visited):
for edge in G.out_edges(visited[i]):
if edge[1] not in visited:
visited.append(edge[1])
self.getParent(G, edge[1], visited)
i += 1
for target in targetList:
if target in visited:
targetList.remove(target)
if len(targetList) == 0:
break
return visited
def getParent(self, G, node, visited):
global aceBits
opcode = ""
oprandlist = []
for edge in G.in_edges(node):
if "virtual" not in G.edge[edge[0]][edge[1]]['opcode']:
oprandlist.append(edge[0])
opcode = G.edge[edge[0]][edge[1]]['opcode']
#if edge[0] not in visited:
# if "virtual" not in G.edge[edge[0]][edge[1]]['opcode']:
# opcode = G.edge[edge[0]][edge[1]]['opcode']
if edge[0] not in visited:
visited.append(edge[0])
self.getParent(G, edge[0], visited)
aceBits += self.instructionPVF(G, opcode, oprandlist, node)
def getParent4CrashChain(self, G, node, max, min):
global crashBits
global stack
global rangeList
rangeList[node] = []
rangeList[node].append(max)
rangeList[node].append(min)
oplist = []
opcode = ""
stack4recursion = []
stack4recursion.append(node)
while len(stack4recursion) != 0:
node = stack4recursion.pop()
opcode = ""
for edge in G.in_edges(node):
if "virtual" not in G.edge[edge[0]][edge[1]]['opcode']:
oplist.append(edge[0])
opcode = G.edge[edge[0]][edge[1]]['opcode']
if opcode != "load" and opcode != "store":
dest = ""
#if len(oplist) == 1:
# dest = G.successors(oplist[0])[0]
#elif len(oplist) >= 2:
# snode = set(G.successors(oplist[0])).intersection(set(G.successors(oplist[1])))
# dest = snode.pop()
#else:
# pass
sorted_ops = sorted([i for i in oplist if node in self.indexMap[i]], key= lambda pos: self.indexMap[pos][node])
if opcode != "" and len(sorted_ops) != 0:
#stack.extend(sorted_ops)
#stack.append(opcode)
sorted_ops.append(opcode)
self.calculateCrashChainBackward(G,sorted_ops)
#print sorted_ops
#print "crash chain"
#if edge[0] not in visited:
# if "virtual" not in G.edge[edge[0]][edge[1]]['opcode']:
# opcode = G.edge[edge[0]][edge[1]]['opcode']
for op in sorted_ops:
if len(G.in_edges(op)) != 0:
#self.getParent4CrashChain(G, op, cbits)
stack4recursion.append(op)
else:
if opcode == "load":
for op in oplist:
for edge in G.in_edges(op):
opcode_new = G.edge[edge[0]][edge[1]]['opcode']
if opcode_new == "store":
if G.node[edge[0]]['out_edge'] > 0 or len(G.in_edges(G.node[edge[0]])) == 0:
if edge[0] not in rangeList:
rangeList[edge[0]] = []
rangeList[edge[0]].append(rangeList[node][0])
rangeList[edge[0]].append(rangeList[node][1])
finalBits.append(self.checkRange(G,edge[0],rangeList[node][0], rangeList[node][1], int(G.node[edge[0]]['len'])))
G.node[edge[0]]['out_edge'] = int(G.node[edge[0]]['out_edge']) -1
stack4recursion.append(edge[0])
oplist = []
def calculateCrashChain(self, G, opstack):
manager = Manager()
final = manager.list()
#print counter
if len(opstack) == 0:
return []
p = [ Process(target=self.worker, args=(G, opstack, final), name=i) for i in range(len(opstack))]
for each in p:
each.start()
for each in p:
each.join()
return final
def worker(self, G, opstack, final):
if InstructionAbstraction.isint(current_process().name) == True:
localop = opstack[int(current_process().name)]
if localop not in config.memoryInst and localop not in config.bitwiseInst and localop not in config.computationInst and localop not in config.castInst and localop not in config.pointerInst and localop not in config.otherInst:
original = int(G.node[localop]['value'])
size = G.node[localop]['len']
for i in range(int(size)):
mask = (1 << i)
new = original^mask
mapping = {}
oplist = []
opcode = ""
for e in reversed(opstack):
if e not in config.memoryInst and e not in config.bitwiseInst and e not in config.computationInst and e not in config.castInst and e not in config.pointerInst and e not in config.otherInst:
oplist.append(e)
else:
opcode = e
#if opcode == "getelementptr":
# print "hhhh"
v = self.brutalForce(G, oplist, opcode, mapping, localop, new)
node = ""
if len(oplist) == 1:
node = G.successors(oplist[0])[0]
elif len(oplist) >= 2:
snode = set(G.successors(oplist[0])).intersection(set(G.successors(oplist[1])))
node = snode.pop()
else:
pass
if localop == node:
mapping[node] = new
else:
mapping[node] = v
temp = mapping[node]
oplist = []
opcode = ""
final.append(temp)
def brutalForce(self, G, replay, opcode, mapping, localop, new):
values = []
for op in replay:
value = 0
if op in mapping:
value = mapping[op]
else:
value = int(G.node[op]['value'])
if op == localop:
value = new
values.append(value)
#for i in range(int(size)):
# mask = (1 << i)
# new_value = int(value)^mask
## sepcial case for ptr operations
if opcode == "getelementptr":
if len(replay) == 3:
size = G.node[replay[0]]['realTy']
values[1] = values[1]*int(size)
#size = G.node[replay[2]]['len']
if "structName" in G.node[replay[0]]:
structname = G.node[replay[0]]['structName']
sizelist = self.structMap["%"+structname]
t = 0
if values[2] >= len(sizelist):
for i in range(len(sizelist)):
t += sizelist[i]
t += (values[2]-len(sizelist) +1)*4
else:
for i in range(len(sizelist)):
t += sizelist[i]
values[2] = int(t/8)
if "elementTy" in G.node[replay[0]]:
element = G.node[replay[0]]['elementTy']
values[2] = values[2]*int(int(element)/8)
if len(replay) == 2:
if "realTy" in G.node[replay[0]]:
size = G.node[replay[0]]['realTy']
values[1] = values[1]*int(int(size)/8)
ret = self.calculateCrashInst(values, opcode)
return ret
def calculateCrashInst(self, values, opcode):
#print opcode
if opcode == "add" or opcode == "fadd":
return sum(values)
if opcode == "sub" or opcode == "fsub":
assert(len(values) == 2)
return values[0] - values[1]
if opcode == "fmul" or opcode == "mul":
assert(len(values) == 2)
return values[0]*values[1]
if opcode == "udiv" or opcode == "sdiv" or opcode == "fdiv":
assert(len(values) == 2)
assert(values[1] != 0)
return values[0]/values[1]
if opcode == "sext":
#bitstream = bin(values[0])
#sign = ""
#if bitstream.startswith("-"):
# bitstream = bitstream.lstrip("-")
# sign = "-"
#if bitstream.startswith("0b"):
# bitstream = bitstream.lstrip("0b")
#if len(bitstream) != config.OSbits:
# mis = 64 - len(bitstream)
# for i in range(mis):
# bitstream = '0'+bitstream
#return int(sign+"0b"+bitstream, 2)
return values[0]
if opcode == "phi":
return values[0]
if opcode == "srem":
assert(len(values) == 2)
if values[1] == 0:
return sys.maxint
else:
return values[0]%values[1]
if opcode == "getelementptr":
return sum(values)
if opcode == "bitcast":
#bitstream = bin(values[0])
#sign = ""
#if bitstream.startswith("-"):
# bitstream = bitstream.lstrip("-")
# sign = "-"
#if bitstream.startswith("0b"):
# bitstream = bitstream.lstrip("0b")
#if len(bitstream) != config.OSbits:
# mis = 64 - len(bitstream)
# for i in range(mis):
# bitstream = '0'+bitstream
#return int(sign+"0b"+bitstream, 2)
return values[0]
def calculateCrashChainBackward(self, G, opstack):
global rangeList
#rangeList[node] = []
#rangeList[node].append(max)
#rangeList[node].append(min)
oplist = []
global finalBits
for op in opstack:
if op not in config.memoryInst and op not in config.bitwiseInst and op not in config.computationInst and op not in config.castInst and op not in config.pointerInst and op not in config.otherInst:
oplist.append(op)
else:
opcode = op
self.getRange4OPs(G, oplist, opcode, rangeList, finalBits)
oplist = []
return finalBits
def getRange4OPs(self, G, oplist, opcode, rangeList, finalBits):
global ranking
global control_start
node = ""
if len(oplist) == 1:
nlist = G.successors(oplist[0])
for n in nlist:
node = n
if node in rangeList:
break
else:
snode = set(G.successors(oplist[0])).intersection(set(G.successors(oplist[1])))
while len(snode) != 0:
node = snode.pop()
if node in rangeList:
break
if node not in rangeList:
print "here"
for item in oplist:
if isfloat(G.node[item]['value']):
G.node[item]['value'] = int(float(G.node[item]['value']))
max_range = int(rangeList[node][0])
min_range = int(rangeList[node][1])
if opcode == "add" or opcode == "fadd":
assert(len(oplist) == 2)
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
for i in range(2):
max_op = max_range - int(G.node[oplist[1-i]]['value'])
min_op = min_range - int(G.node[oplist[1-i]]['value'])
if oplist[i] not in rangeList:
rangeList[oplist[i]] = []
rangeList[oplist[i]].append(max_op)
rangeList[oplist[i]].append(min_op)
type = G.node[oplist[i]]['len']
ace_inst += type
#if "constant" not in oplist[i]:
if int(G.node[oplist[i]]['out_edge']) > 0:
G.node[oplist[i]]['out_edge'] = int(G.node[oplist[i]]['out_edge']) -1
crash_tmp = self.checkRange(G,oplist[i],max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if opcode == "sub" or opcode == "fsub":
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
assert(len(oplist) == 2)
# the first operand
max_op = max_range + int(G.node[oplist[1]]['value'])
min_op = min_range + int(G.node[oplist[1]]['value'])
if oplist[0] not in rangeList:
rangeList[oplist[0]] = []
rangeList[oplist[0]].append(max_op)
rangeList[oplist[0]].append(min_op)
type = G.node[oplist[0]]['len']
ace_inst += type
#if "constant" not in oplist[0]:
if int(G.node[oplist[0]]['out_edge']) > 0:
G.node[oplist[0]]['out_edge'] = int(G.node[oplist[0]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[0],max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
max_op = int(G.node[oplist[0]]['value']) - min_range
min_op = int(G.node[oplist[0]]['value']) - max_range
if oplist[1] not in rangeList:
rangeList[oplist[1]] = []
rangeList[oplist[1]].append(max_op)
rangeList[oplist[1]].append(min_op)
type = G.node[oplist[1]]['len']
ace_inst += type
#if "constant" not in oplist[1]:
if int(G.node[oplist[1]]['out_edge']) > 0:
G.node[oplist[1]]['out_edge'] = int(G.node[oplist[1]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[1],max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if opcode == "fmul" or opcode == "mul":
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
assert(len(oplist) == 2)
for i in range(2):
if int(G.node[oplist[1-i]]['value']) == 0:
max_op = sys.maxint
min_op = -sys.maxint-1
else:
max_op = max_range/int(G.node[oplist[1-i]]['value'])
min_op = min_range/int(G.node[oplist[1-i]]['value'])
if oplist[i] not in rangeList:
rangeList[oplist[i]] = []
rangeList[oplist[i]].append(max_op)
rangeList[oplist[i]].append(min_op)
type = G.node[oplist[i]]['len']
ace_inst += type
#if "constant" not in oplist[i]:
if int(G.node[oplist[i]]['out_edge']) > 0:
G.node[oplist[i]]['out_edge'] = int(G.node[oplist[i]]['out_edge']) -1
crash_tmp = self.checkRange(G,oplist[i],max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if opcode == "udiv" or opcode == "sdiv" or opcode == "fdiv":
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
assert(len(oplist) == 2)
# the first operand
max_op = max_range*int(G.node[oplist[1]]['value'])
min_op = min_range*int(G.node[oplist[1]]['value'])
if oplist[0] not in rangeList:
rangeList[oplist[0]] = []
rangeList[oplist[0]].append(max_op)
rangeList[oplist[0]].append(min_op)
type = G.node[oplist[0]]['len']
ace_inst += type
#if "constant" not in oplist[0]:
if int(G.node[oplist[0]]['out_edge']) > 0:
G.node[oplist[0]]['out_edge'] = int(G.node[oplist[0]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[0] ,max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
max_op = int(G.node[oplist[0]]['value'])/min_range
min_op = int(G.node[oplist[0]]['value'])/max_range
if oplist[1] not in rangeList:
rangeList[oplist[1]] = []
rangeList[oplist[1]].append(max_op)
rangeList[oplist[1]].append(min_op)
type = G.node[oplist[1]]['len']
ace_inst += type
#if "constant" not in oplist[1]:
if int(G.node[oplist[1]]['out_edge']) > 0:
G.node[oplist[1]]['out_edge'] = int(G.node[oplist[1]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[1] ,max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if opcode == "sext":
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
max_op = max_range
min_op = min_range
if _index == 1013:
print "!!!"
if oplist[0] not in rangeList:
rangeList[oplist[0]] = []
rangeList[oplist[0]].append(max_op)
rangeList[oplist[0]].append(min_op)
type = G.node[oplist[0]]['len']
ace_inst += type
if int(G.node[oplist[0]]['out_edge']) > 0:
G.node[oplist[0]]['out_edge'] = int(G.node[oplist[0]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[0],max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if opcode == "phi":
max_op = max_range
min_op = min_range
if oplist[0] not in rangeList:
rangeList[oplist[0]] = []
rangeList[oplist[0]].append(max_op)
rangeList[oplist[0]].append(min_op)
type = G.node[oplist[0]]['len']
#finalBits.append(self.checkRange(G, oplist[0] ,max_op, min_op, type))
if opcode == "trunc":
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
max_op = max_range
min_op = min_range
if oplist[0] not in rangeList:
rangeList[oplist[0]] = []
rangeList[oplist[0]].append(max_op)
rangeList[oplist[0]].append(min_op)
type = G.node[oplist[0]]['len']
ace_inst += type
if int(G.node[oplist[0]]['out_edge']) > 0:
G.node[oplist[0]]['out_edge'] = int(G.node[oplist[0]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[0] ,max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if opcode == "srem":
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
assert(len(oplist) == 2)
type = int(G.node[oplist[0]]['len'])
value = int(G.node[oplist[0]]['value'])
tmp = []
for i in range(type):
mask = (1 << i)
value = value ^ mask
if value%int(G.node[oplist[1]]['value']) > max or value%int(G.node[oplist[1]]['value']) < min:
tmp.append(value)
rangeList[oplist[0]] = []
rangeList[oplist[0]].append(max(tmp))
rangeList[oplist[0]].append(min(tmp))
if int(G.node[oplist[0]]['out_edge']) > 0:
G.node[oplist[0]]['out_edge'] = int(G.node[oplist[0]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[0] ,max(tmp), min(tmp), type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
type = int(G.node[oplist[1]]['len'])
ace_inst += type
value = int(G.node[oplist[1]]['value'])
tmp = []
for i in range(type):
mask = (1 << i)
value = value ^ mask
if value == 0:
continue
if int(G.node[oplist[1]]['value'])%value > max or int(G.node[oplist[1]]['value'])%value < min:
tmp.append(value)
rangeList[oplist[1]] = []
rangeList[oplist[1]].append(max(tmp))
rangeList[oplist[1]].append(min(tmp))
if int(G.node[oplist[1]]['out_edge']) > 0:
G.node[oplist[1]]['out_edge'] = int(G.node[oplist[1]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[1] ,max(tmp), min(tmp), type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if opcode == "getelementptr":
cycle = int(G.node[node]['cycle'])
_index = self.cycle_index_lookup[cycle]
crash_inst = 0
ace_inst = 0
flag = 0
if len(oplist) == 2:
if "realTy" in G.node[oplist[0]]:
size = G.node[oplist[0]]['realTy']
#values[1] = values[1]*int(int(size)/8)
max_op = max_range - int(G.node[oplist[1]]['value'])*int(size)/8
min_op = min_range - int(G.node[oplist[1]]['value'])*int(size)/8
if oplist[0] not in rangeList:
rangeList[oplist[0]] = []
rangeList[oplist[0]].append(max_op)
rangeList[oplist[0]].append(min_op)
type = G.node[oplist[0]]['len']
ace_inst += type
#if "constant" not in oplist[0]:
if int(G.node[oplist[0]]['out_edge']) > 0:
G.node[oplist[0]]['out_edge'] = int(G.node[oplist[0]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[0],max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
max_op = (max_range - int(G.node[oplist[0]]['value']))*8/int(size)
min_op = (min_range - int(G.node[oplist[0]]['value']))*8/int(size)
if oplist[1] not in rangeList:
rangeList[oplist[1]] = []
rangeList[oplist[1]].append(max_op)
rangeList[oplist[1]].append(min_op)
type = G.node[oplist[1]]['len']
ace_inst += type
#if "constant" not in oplist[1]:
if int(G.node[oplist[1]]['out_edge']) > 0:
G.node[oplist[1]]['out_edge'] = int(G.node[oplist[1]]['out_edge']) -1
crash_tmp = self.checkRange(G, oplist[1] ,max_op, min_op, type)
finalBits.append(crash_tmp)
crash_inst += crash_tmp
flag += 1
if flag > 0:
if _index in ranking:
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
else:
ranking[_index] = []
ranking[_index].append([cycle,ace_inst-crash_inst,ace_inst])
if len(oplist) == 3:
if "realTy" in G.node[oplist[0]]:
size = G.node[oplist[0]]['realTy']
#size = G.node[replay[2]]['len']
if "structName" in G.node[oplist[0]]:
structname = G.node[oplist[0]]['structName']
sizelist = self.structMap["%"+structname]