-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
855 lines (763 loc) · 34.4 KB
/
simulation.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
import json
from operator import ne
from subprocess import call
from appscript import k
import js2py
import os, sys
from regex import F
from sqlalchemy import false
import divide_ob
import copy
import time
def writeNode(node):
tempFile = open('./temp.js.json', 'w')
tempFile.write(json.dumps(node))
tempFile.close()
def DFS_cnt(node, callee_name, cnts, constants):
if type(node) == dict:
token = node.get('type')
if token == 'CallExpression' and node.get('callee').get('type') == 'Identifier' and node.get('callee').get('name') in callee_name:
nodeName = node.get('callee').get('name')
cnts[nodeName] = cnts[nodeName] + 1
if 'arguments' in node.keys():
isConstant = True
for parameter in node.get('arguments'):
if parameter.get('type') != 'Literal':
isConstant = False
if isConstant:
constants[nodeName] = constants[nodeName] + 1
for key in node.keys():
DFS_cnt(node.get(key), callee_name, cnts, constants)
elif type(node) == list:
for i in range(len(node)):
DFS_cnt(node[i], callee_name, cnts, constants)
def DFS_replace(node, nodeName, jsFun):
if type(node) == dict:
for key in node.keys():
next = node.get(key)
if type(next) == dict:
token = next.get('type')
if token == 'CallExpression' and next.get('callee').get('type') == 'Identifier' and next.get('callee').get('name') == nodeName:
parameters = []
if 'arguments' in next.keys():
for argument in next.get('arguments'):
parameters.append(argument.get('value'))
# print("parameters", parameters, jsFun)
res = jsFun(parameters[0], parameters[1])
newNode = {'type' : 'Literal', 'value' : res, 'raw' : '\'' + res + '\''}
node[key] = newNode
continue
DFS_replace(next, nodeName, jsFun)
elif type(node) == list:
for i in range(len(node)):
next = node[i]
if type(next) == dict:
token = next.get('type')
if token == 'CallExpression' and next.get('callee').get('type') == 'Identifier' and next.get('callee').get('name') == nodeName:
parameters = []
if 'arguments' in next.keys():
for argument in next.get('arguments'):
parameters.append(argument.get('value'))
# print("parameters", parameters)
res = jsFun(parameters[0], parameters[1])
# print("res", res)
newNode = {'type' : 'Literal', 'value' : res, 'raw' : '\'' + res + '\''}
node[i] = newNode
continue
DFS_replace(node[i], nodeName, jsFun)
def call_graph(program):
callee = {}
callee_name = []
for index in program:
if index.get('type') != 'VariableDeclaration':
continue
declarations = index.get('declarations')
for declaration in declarations:
if 'init' not in declaration or declaration.get('init').get('type') != 'FunctionExpression':
continue
if 'id' not in declaration or declaration.get('id').get('type') != 'Identifier':
continue
callee_name.append(declaration.get('id').get('name'))
callee[declaration.get('id').get('name')] = declaration
cnts = {}
constants = {}
for name in callee_name:
cnts[name] = 0
constants[name] = 0
DFS_cnt(program, callee_name, cnts, constants)
# print(cnts)
# print(constants)
illegalFun = {}
maxNumber, maxName = 0, ''
for name in callee_name:
if cnts[name] == constants[name] and cnts[name] > 0:
# illegalFun[name] = callee[name]
if cnts[name] > maxNumber:
maxNumber = cnts[name]
maxName = name
illegalFun[maxName] = callee[maxName]
return illegalFun
# print(callee_name)
def isReturnOnly(node):
if node.get('type') != 'FunctionExpression':
return False
bodyNode = node.get('body')
if bodyNode.get('type') != 'BlockStatement' or len(bodyNode.get('body')) > 1:
return False
expNode = bodyNode.get('body')[0]
return expNode.get('type') == 'ReturnStatement'
def copyNode(src, dst):
if type(src) == dict:
for key in src.keys():
value = src.get(key)
if type(value) == dict:
dst[key] = {}
copyNode(src.get(key), dst.get(key))
elif type(dict) == list:
dst[key] = []
copyNode(src.get(key), dst.get(key))
else:
dst[key] = src.get(key)
elif type(src) == list:
for item in src:
if type(item) == dict:
newItem = {}
dst.append(newItem)
copyNode(item, newItem)
elif type(dict) == list:
newItem = []
dst.append(newItem)
copyNode(item, newItem)
else:
newItem = item
dst.append(newItem)
def repParameter(node, parameterMapping):
if type(node) == dict:
for key in node.keys():
if type(node.get(key)) == dict:
if node.get(key).get('type') == 'Identifier':
node[key] = parameterMapping[node.get(key).get('name')]
continue
repParameter(node.get(key), parameterMapping)
elif type(node) == list:
cnt = -1
for item in node:
cnt = cnt + 1
if type(item) == dict:
if item.get('type') == 'Identifier':
node[cnt] = parameterMapping[item.get('name')]
continue
repParameter(item, parameterMapping)
def unfoldReturn(node, function):
if type(node) == dict:
for key in node.keys():
next = node.get(key)
# print(next)
if type(next) == dict and next.get('type') == 'CallExpression' and type(next.get('callee')) == dict and next.get('callee').get('type') == 'MemberExpression' and next.get('callee').get('computed') == True:
callee = next.get('callee')
if callee.get('object').get('type') == 'Identifier' and callee.get('object').get('name') == function[0]:
if callee.get('property').get('type') == 'Literal' and callee.get('property').get('value') == function[1]:
newNode = {}
# print(function[2].get('body'))
retNode = (function[2].get('body').get('body')[0]).get('argument')
newNode = copy.deepcopy(retNode)
# copyNode(retNode, newNode)
mapping = {}
cnt = -1
for argus in function[2].get('params'):
cnt = cnt + 1
mapping[argus.get('name')] = next.get('arguments')[cnt]
# print(newNode)
# print('mapping\n', mapping, '\n')
# print('newNode\n', newNode)
repParameter(newNode, mapping)
# print(json.dumps(newNode, indent=4))
# print('newNode\n', newNode)
node[key] = newNode
continue
unfoldReturn(node.get(key), function)
elif type(node) == list:
cntNode = -1
for next in node:
cntNode = cntNode + 1
if type(next) == dict and next.get('type') == 'CallExpression' and type(next.get('callee')) == dict and next.get('callee').get('type') == 'MemberExpression' and next.get('callee').get('computed') == True:
callee = next.get('callee')
if callee.get('object').get('type') == 'Identifier' and callee.get('object').get('name') == function[0]:
if callee.get('property').get('type') == 'Literal' and callee.get('property').get('value') == function[1]:
newNode = {}
# print(function[2].get('body'))
retNode = (function[2].get('body').get('body')[0]).get('argument')
newNode = copy.deepcopy(retNode)
# copyNode(retNode, newNode)
mapping = {}
cnt = -1
for argus in function[2].get('params'):
cnt = cnt + 1
mapping[argus.get('name')] = next.get('arguments')[cnt]
repParameter(newNode, mapping)
# if cnt >= len(node):
# print(cnt, node)
# print(newNode)
# print(function)
node[cntNode] = newNode
continue
unfoldReturn(next, function)
def DFS_innermapping(node, res):
if type(node) == dict:
if node.get('type') == 'VariableDeclarator' and type(node.get('init')) == dict and node.get('init').get('type') == 'ObjectExpression':
toDel = []
for property in node.get('init').get('properties'):
if property.get('key').get('type') == 'Literal':
if isReturnOnly(property.get('value')):
res.append([node.get('id').get('name'), property.get('key').get('value'), property.get('value')])
toDel.append(property)
for item in toDel:
node.get('init').get('properties').remove(item)
for key in node.keys():
DFS_innermapping(node.get(key), res)
elif type(node) == list:
for item in node:
DFS_innermapping(item, res)
def DFS_innervalue(node, res):
if type(node) == dict:
if node.get('type') == 'VariableDeclarator' and type(node.get('init')) == dict and node.get('init').get('type') == 'ObjectExpression':
toDel = []
for property in node.get('init').get('properties'):
if property.get('key').get('type') == 'Literal':
if property.get('value').get('type') == 'Literal':
res.append([node.get('id').get('name'), property.get('key').get('value'), property.get('value')])
toDel.append(property)
for item in toDel:
node.get('init').get('properties').remove(item)
for key in node.keys():
DFS_innervalue(node.get(key), res)
elif type(node) == list:
for item in node:
DFS_innervalue(item, res)
def unfoldValue(node, function):
if type(node) == dict:
for key in node.keys():
next = node.get(key)
# print(next)
if type(next) == dict and next.get('type') == 'MemberExpression' and next.get('computed') == True:
if next.get('object').get('type') == 'Identifier' and next.get('object').get('name') == function[0]:
if next.get('property').get('type') == 'Literal' and next.get('property').get('value') == function[1]:
newNode = {}
# print('aaa')
newNode = copy.deepcopy(function[2])
node[key] = newNode
continue
unfoldValue(node.get(key), function)
elif type(node) == list:
cnt = -1
for next in node:
cnt = cnt + 1
if type(next) == dict and next.get('type') == 'MemberExpression' and next.get('computed') == True:
if next.get('object').get('type') == 'Identifier' and next.get('object').get('name') == function[0]:
if next.get('property').get('type') == 'Literal' and next.get('property').get('value') == function[1]:
# print('bbb')
newNode = {}
newNode = copy.deepcopy(function[2])
node[cnt] = newNode
continue
unfoldValue(next, function)
def unfoldValue_old(node, function):
if type(node) == dict:
for key in node.keys():
next = node.get(key)
# print(next)
if type(next) == dict and next.get('type') == 'CallExpression' and type(next.get('callee')) == dict and next.get('callee').get('type') == 'MemberExpression' and next.get('callee').get('computed') == True:
callee = next.get('callee')
if callee.get('object').get('type') == 'Identifier' and callee.get('object').get('name') == function[0]:
if callee.get('property').get('type') == 'Literal' and callee.get('property').get('value') == function[1]:
newNode = {}
# print('aaa')
# print(function[2].get('body'))
# retNode = (function[2].get('body').get('body')[0]).get('argument')
newNode = copy.deepcopy(function[2])
# copyNode(retNode, newNode)
# mapping = {}
# cnt = -1
# for argus in function[2].get('params'):
# cnt = cnt + 1
# mapping[argus.get('name')] = next.get('arguments')[cnt]
# print(newNode)
# print('mapping\n', mapping, '\n')
# print('newNode\n', newNode)
# repParameter(newNode, mapping)
# print(json.dumps(newNode, indent=4))
# print('newNode\n', newNode)
node[key] = newNode
continue
unfoldValue(node.get(key), function)
elif type(node) == list:
cnt = -1
for next in node:
cnt = cnt + 1
if type(next) == dict and next.get('type') == 'CallExpression' and type(next.get('callee')) == dict and next.get('callee').get('type') == 'MemberExpression' and next.get('callee').get('computed') == True:
callee = next.get('callee')
if callee.get('object').get('type') == 'Identifier' and callee.get('object').get('name') == function[0]:
if callee.get('property').get('type') == 'Literal' and callee.get('property').get('value') == function[1]:
# print('bbb')
newNode = {}
# print(function[2].get('body'))
# retNode = (function[2].get('body').get('body')[0]).get('argument')
newNode = copy.deepcopy(function[2])
# copyNode(retNode, newNode)
# mapping = {}
# cnt = -1
# for argus in function[2].get('params'):
# cnt = cnt + 1
# mapping[argus.get('name')] = next.get('arguments')[cnt]
# repParameter(newNode, mapping)
node[cnt] = newNode
continue
unfoldValue(next, function)
def empty_define(node):
if type(node) != dict or node.get('type') != 'VariableDeclaration':
return False
declarations = node.get('declarations')
for declaration in declarations:
# print('wow')
if declaration.get('type') != 'VariableDeclarator' or 'init' not in declaration.keys():
return False
if type(declaration.get('init')) != dict or declaration.get('init').get('type') != 'ObjectExpression' or len(declaration.get('init').get('properties')) > 0:
return False
return True
def DFS_empty(node):
if type(node) == dict:
for key in node.keys():
DFS_empty(node.get(key))
elif type(node) == list:
toDel = []
for next in node:
if type(next) == dict and next.get('type') == 'VariableDeclaration':
if empty_define(next):
toDel.append(next)
continue
DFS_empty(next)
for next in toDel:
node.remove(next)
def innerSimulation(syntax):
for funNode in syntax.get('body'):
res = []
DFS_innermapping(funNode, res)
for function in res:
# print('function', function)
unfoldReturn(funNode, function)
DFS_empty(funNode)
resValue = []
DFS_innervalue(funNode, resValue)
for function in resValue:
# print('function', function)
unfoldValue(funNode, function)
DFS_empty(funNode)
def simulate(inputFileName):
t1 = time.time()
inputFile = './jsdata/rand_jsjiami/' + inputFileName
# file = open(inputFile)
# if not os.path.exists(inputFile + '-fun'):
# os.mkdir(inputFile + '-fun')
srcFile = os.popen('node pparse.js ' + inputFile, 'r')# Not sure ...'r', 1)
syntax = json.loads(srcFile.read())
srcFile.close()
t2 = time.time()
# syntax = json.load(file)
illegalFun = call_graph(syntax.get('body'))
jsFuns = {}
partial = []
newProgram = {'type': 'Program', 'body': partial, 'sourceType': 'script'}
counter = 0
# divide_ob.moveDeclaration(syntax)
for declarations in syntax.get('body'):
counter = counter + 1
if counter > 3:
break
if declarations.get('type') != 'VariableDeclaration':
partial.append(declarations)
# print('wow', counter)
elif len(declarations) <= 0:
continue
elif declarations.get('declarations')[0].get('type') != 'VariableDeclarator' or 'init' not in declarations.get('declarations')[0].keys() or declarations.get('declarations')[0].get('init').get('type') != 'FunctionExpression':
partial.append(declarations)
# print('wow', counter)
toMove = []
for funName in illegalFun.keys():
illegalName = './jsdata/rand_temp/' + inputFileName + '.json'
print('illeagalName', illegalName)
illegalFile = open(illegalName, 'w')
wrapper = {}
for wrapperName in syntax.get('body'):
if wrapperName.get('type') == 'VariableDeclaration' and illegalFun.get(funName) in wrapperName.get('declarations'):
wrapper = wrapperName
# wrapper = {'type': 'VariableDeclaration', 'declarations':[illegalFun.get(funName)], 'kind': 'var'}
partial.append(wrapper)
illegalFile.write(json.dumps(newProgram))
partial.remove(wrapper)
illegalFile.close()
toMove.append(wrapper)
dstFile = os.popen('node ./pregenerate.js ' + illegalName, 'r')
dstRes = dstFile.read()
dstFile.close()
# illegalJS = open(illegalName + '-new.js', 'r+')
jsFuns[funName] = js2py.eval_js(dstRes)
# illegalJS.close()
t3 = time.time()
for funName in jsFuns.keys():
DFS_replace(syntax.get('body'), funName, jsFuns.get(funName))
for funNode in partial:
syntax.get('body').remove(funNode)
for funNode in toMove:
syntax.get('body').remove(funNode)
t4 = time.time()
innerSimulation(syntax)
outputFile = './jsdata/rand_jsjiami_recover/' + inputFileName
newFile = open(outputFile + '-fixed.json', 'w')
newFile.write(json.dumps(syntax, indent = 4))
newFile.close()
# print(syntax)
os.system('node ./regenerate.js ' + outputFile + '-fixed.json ' + outputFile)
# os.system('node ./parse.js ' + outputFile + '-fixed.json-new.js ' + outputFile + '-fixed.json')
# DFS(syntax, Isredunduncy, 0)
# writeNode(syntax.get('body')[0])
t5 = time.time()
return [t2 - t1, t3 - t2, t4 - t3, t5- t4]
def call_graph_detect(program):
callee = {}
callee_name = []
for index in program:
if index.get('type') != 'VariableDeclaration':
continue
declarations = index.get('declarations')
for declaration in declarations:
if 'init' not in declaration or declaration.get('init').get('type') != 'FunctionExpression':
continue
if 'id' not in declaration or declaration.get('id').get('type') != 'Identifier':
continue
callee_name.append(declaration.get('id').get('name'))
callee[declaration.get('id').get('name')] = declaration
cnts = {}
constants = {}
for name in callee_name:
cnts[name] = 0
constants[name] = 0
DFS_cnt(program, callee_name, cnts, constants)
# print(cnts)
# print(constants)
illegalFun = {}
maxNumber, maxName = 0, ''
for name in callee_name:
if cnts[name] == constants[name] and cnts[name] > 0:
# illegalFun[name] = callee[name]
if cnts[name] > maxNumber:
maxNumber = cnts[name]
maxName = name
illegalFun[maxName] = maxNumber
return illegalFun
# print(callee_name)
def detect_global(inputFile):
srcFile = os.popen('node pparse.js ' + inputFile, 'r')# Not sure ...'r', 1)
syntax = json.loads(srcFile.read())
srcFile.close()
# syntax = json.load(file)
illegalFun = call_graph_detect(syntax.get('body'))
for funName in illegalFun.keys():
if illegalFun[funName] > 1:
return True
return False
def DFS_controlFlow(node, flags):
if type(node) == dict:
if node.get('type') == 'WhileStatement':
test = node.get('test')
if test.get('type') == 'UnaryExpression' and test.get('operator') == '!':
argument = test.get('argument')
if argument.get('type') == 'UnaryExpression' and argument.get('operator') == '!':
if argument.get('argument').get('type') == 'ArrayExpression':
flags[0] = flags[0] + 1
body = node.get('body')
if body.get('type') == 'BlockStatement' and body.get('body')[0].get('type') == 'SwitchStatement':
flags[1] = flags[1] + 1
for key in node.keys():
DFS_controlFlow(node.get(key), flags)
elif type(node) == list:
for i in range(len(node)):
DFS_controlFlow(node[i], flags)
def detect_controlFlow(inputFile):
srcFile = os.popen('node pparse.js ' + inputFile, 'r')# Not sure ...'r', 1)
syntax = json.loads(srcFile.read())
srcFile.close()
# syntax = json.load(file)
flags = [0, 0]
DFS_controlFlow(syntax.get('body'), flags)
return flags[0] > 0 and flags[1] > 0
def DFS_number(node, flags):
if type(node) == dict:
if node.get('type') == 'BinaryExpression' and node.get('left').get('type') == 'Literal' and node.get('right').get('type'):
flags[0] = flags[0] + 1
for key in node.keys():
DFS_number(node.get(key), flags)
elif type(node) == list:
for i in range(len(node)):
DFS_number(node[i], flags)
def detect_number2Exp(inputFile):
srcFile = os.popen('node pparse.js ' + inputFile, 'r')# Not sure ...'r', 1)
syntax = json.loads(srcFile.read())
srcFile.close()
# syntax = json.load(file)
flags = [0]
DFS_number(syntax.get('body'), flags)
return flags[0]
def unfoldReturn2(node, function):
if type(node) == dict:
for key in node.keys():
next = node.get(key)
# print(next)
if type(next) == dict and next.get('type') == 'CallExpression' and type(next.get('callee')) == dict and next.get('callee').get('type') == 'MemberExpression' and next.get('callee').get('computed') == True:
callee = next.get('callee')
if callee.get('object').get('type') == 'Identifier' and callee.get('object').get('name') == function[0]:
if callee.get('property').get('type') == 'Literal' and callee.get('property').get('value') == function[1]:
newNode = {}
# print(function[2].get('body'))
retNode = (function[2].get('body').get('body')[0]).get('argument')
newNode = copy.deepcopy(retNode)
# copyNode(retNode, newNode)
mapping = {}
cnt = -1
for argus in function[2].get('params'):
cnt = cnt + 1
mapping[argus.get('name')] = next.get('arguments')[cnt]
# print(newNode)
# print('mapping\n', mapping, '\n')
# print('newNode\n', newNode)
repParameter(newNode, mapping)
# print(json.dumps(newNode, indent=4))
# print('newNode\n', newNode)
# node[key] = newNode
continue
unfoldReturn(node.get(key), function)
elif type(node) == list:
cnt = -1
for next in node:
cnt = cnt + 1
if type(next) == dict and next.get('type') == 'CallExpression' and type(next.get('callee')) == dict and next.get('callee').get('type') == 'MemberExpression' and next.get('callee').get('computed') == True:
callee = next.get('callee')
if callee.get('object').get('type') == 'Identifier' and callee.get('object').get('name') == function[0]:
if callee.get('property').get('type') == 'Literal' and callee.get('property').get('value') == function[1]:
newNode = {}
# print(function[2].get('body'))
retNode = (function[2].get('body').get('body')[0]).get('argument')
newNode = copy.deepcopy(retNode)
# copyNode(retNode, newNode)
mapping = {}
cnt = -1
for argus in function[2].get('params'):
cnt = cnt + 1
mapping[argus.get('name')] = next.get('arguments')[cnt]
repParameter(newNode, mapping)
# node[cnt] = newNode
continue
unfoldReturn(next, function)
def innerSimulation2(syntax):
for funNode in syntax.get('body'):
res = []
DFS_innermapping(funNode, res)
for function in res:
print('aaa')
# print('function', function)
unfoldReturn2(funNode, function)
DFS_empty(funNode)
resValue = []
# DFS_innervalue(funNode, resValue)
# for function in res:
# # print('function', function)
# unfoldReturn2(funNode, function)
# DFS_empty(funNode)
def simulate2(inputFileName):
t1 = time.time()
inputFile = './jsdata/rand_ob/' + inputFileName
# file = open(inputFile)
# if not os.path.exists(inputFile + '-fun'):
# os.mkdir(inputFile + '-fun')
srcFile = os.popen('node pparse.js ' + inputFile, 'r')# Not sure ...'r', 1)
syntax = json.loads(srcFile.read())
srcFile.close()
t2 = time.time()
print ('test1')
# syntax = json.load(file)
counter = 0
detect_global(inputFile)
# divide_ob.moveDeclaration(syntax)
if len(syntax.get('body')) >= 3:
toMove = []
for declarations in syntax.get('body'):
counter = counter + 1
if counter >= 3 and declarations.get('type') == 'ExpressionStatement':
if declarations.get('expression').get('type') == 'SequenceExpression':
temp = declarations.get('expression').get('expressions')[0]
declarations.get('expression').get('expressions').remove(temp)
break
elif declarations.get('expression').get('type') == 'CallExpression':
toMove.append(declarations)
break
else:
if counter == len(syntax.get('body')):
break
toMove.append(declarations)
# print('wow', counter)
for funNode in toMove:
syntax.get('body').remove(funNode)
# innerSimulation(syntax)
print('kkk')
outputFile = './jsdata/rand_ob_recover/' + inputFileName
newFile = open(outputFile + '-fixed.json', 'w')
newFile.write(json.dumps(syntax, indent = 4))
newFile.close()
t3 = time.time()
detect_global2(inputFile)
detect_global(inputFile)
try:
innerSimulation2(syntax.get('body'))
except:
print('bbb')
t4 = time.time()
detect_global2(inputFile)
t5 = time.time()
detect_number2Exp(inputFile)
t6 = time.time()
detect_controlFlow(inputFile)
# print(syntax)
os.system('node ./regenerate.js ' + outputFile + '-fixed.json ' + outputFile)
# os.system('node ./parse.js ' + outputFile + '-fixed.json-new.js ' + outputFile + '-fixed.json')
# DFS(syntax, Isredunduncy, 0)
# writeNode(syntax.get('body')[0])
t7 = time.time()
return [t2 - t1, t3 - t2, t4 - t3, t5 - t4, t6 - t5, t7 - t6]
def DFS_global2(node, flags):
if node.get('type') == 'Identifier' and '_0x' in node.get('name'):
flags[0] = flags[0] + 1
if type(node) == dict:
for key in node.keys():
DFS_number(node.get(key), flags)
elif type(node) == list:
for i in range(len(node)):
DFS_number(node[i], flags)
def detect_global2(inputFile):
# file = open(inputFile)
# if not os.path.exists(inputFile + '-fun'):
# os.mkdir(inputFile + '-fun')
srcFile = os.popen('node pparse.js ' + inputFile, 'r')# Not sure ...'r', 1)
syntax = json.loads(srcFile.read())
srcFile.close()
print ('test1')
# syntax = json.load(file)
counter = 0
flags = [0]
# divide_ob.moveDeclaration(syntax)
if len(syntax.get('body')) >= 3:
for declarations in syntax.get('body'):
counter = counter + 1
if counter <= 2:
DFS_global2(declarations, flags)
if flags[0] > 0:
return True
elif counter >= 3 and declarations.get('type') == 'ExpressionStatement':
if declarations.get('expression').get('type') == 'SequenceExpression':
return True
return False
def isControlFlowStart(node):
if type(node) != dict:
return False
isTrueWhile = False
if node.get('type') == 'WhileStatement':
test = node.get('test')
if test.get('type') == 'UnaryExpression' and test.get('operator') == '!':
argument = test.get('argument')
if argument.get('type') == 'UnaryExpression' and argument.get('operator') == '!':
if argument.get('argument').get('type') == 'ArrayExpression':
isTrueWhile = True
if not isTrueWhile:
return False
body = node.get('body')
if body.get('type') == 'BlockStatement':
# print(body['body'][0])
if body['body'][0].get('type') == 'SwitchStatement':
return isTrueWhile
def isSwitchOrder(node):
if type(node) != dict:
return False
if node.get('type') == 'VariableDeclarator' and 'init' in node.keys():
initNode = node.get('init')
if initNode.get('type') == 'CallExpression' and initNode.get('callee').get('type') == 'MemberExpression':
calleeNode = initNode.get('callee')
if 'computed' in calleeNode.keys() and calleeNode.get('computed') == True and 'object' in calleeNode.keys():
if calleeNode.get('property').get('type') == 'Literal' and calleeNode.get('property').get('value') == 'split':
if calleeNode.get('object').get('type') == 'Literal':
switchOrder = calleeNode.get('object').get('value')
if '|' in switchOrder:
return switchOrder
return ""
def repControlFlow(node, order):
orderList = order.split('|')
if len(orderList) < 2:
return node
newNode = {'type': 'BlockStatement', 'body': []}
discriminant = node['body']['body'][0]['discriminant']
switchVar = discriminant['object']['name']
caseList = node['body']['body'][0]['cases']
for order in orderList:
for case in caseList:
if case['test']['value'] != order:
continue
for i in range(len(case['consequent']) - 1):
newNode['body'].append(case['consequent'][i])
if case['consequent'][-1]['type'] != 'ContinueStatement':
newNode['body'].append(case['consequent'][-1])
# print(newNode)
return newNode
def DFS_controlFlow_rep(node, rep):
if type(node) == dict:
order = isSwitchOrder(node)
if len(order) > 2:
rep.append(order)
for key in node.keys():
if isControlFlowStart(node.get(key)):
newNode = repControlFlow(node.get(key), rep[0])
node[key] = newNode
continue
DFS_controlFlow_rep(node.get(key), rep)
elif type(node) == list:
for i in range(len(node)):
if isControlFlowStart(node[i]):
newNode = repControlFlow(node[i], rep[0])
node[i] = newNode
continue
DFS_controlFlow_rep(node[i], rep)
def simulate3(inputFileName):
t1 = time.time()
inputFile = './control/' + inputFileName
# file = open(inputFile)
# if not os.path.exists(inputFile + '-fun'):
# os.mkdir(inputFile + '-fun')
srcFile = os.popen('node pparse.js ' + inputFile, 'r')# Not sure ...'r', 1)
syntax = json.loads(srcFile.read())
srcFile.close()
t2 = time.time()
# syntax = json.load(file)
# illegalFun = call_graph(syntax.get('body'))
rep = []
# newProgram = {'type': 'Program', 'body': partial, 'sourceType': 'script'}
DFS_controlFlow_rep(syntax.get('body'), [])
counter = 0
outputFile = './control/' + inputFileName + '-con.js'
newFile = open(outputFile + '-fixed.json', 'w')
newFile.write(json.dumps(syntax, indent = 4))
newFile.close()
# print(syntax)
os.system('node ./regenerate.js ' + outputFile + '-fixed.json ' + outputFile)
# os.system('node ./parse.js ' + outputFile + '-fixed.json-new.js ' + outputFile + '-fixed.json')
# DFS(syntax, Isredunduncy, 0)
# writeNode(syntax.get('body')[0])
t5 = time.time()
return [t2 - t1, t5 - t2]
if __name__ == "__main__":
simulate3('44354.js-ob.js')