Repository navigation
Expand file tree
/
Copy pathcode_modification.py
More file actions
850 lines (793 loc) · 36.3 KB
/
Copy pathcode_modification.py
File metadata and controls
850 lines (793 loc) · 36.3 KB
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
'''
Security Patch Group: Patch Oversampling Task.
Developer: Shu Wang
Date: 2020-06-24
Version: S2020.06.24 (Version 5.0)
File Structure:
PatchClearance
|-- _old_versions # old versions for the programs.
|-- openssl # openssl data.
|-- file_jk # program files.
| -- after # 'after' version.
| -- before # 'before' version
|-- patch_jk # patch files.
|-- ast_jk # AST files.
| -- after # 'after' version.
| -- before # 'before' version
|-- out_jk # output program files.
| -- after # 'after' version.
| -- before # 'before' version
|-- outp_jk # output patch files.
|-- code_modification.py # main entrance.
|-- README.md # readme file.
Prerequirements:
LLVM 10.0.0 (Download Link: http://www.llvm.org/releases/download.html)
Usage:
python code_modification.py
'''
import os
import re
import random
import numpy as np
rootPath = './'
appPath = rootPath + '/openssl/'
datPath = appPath + '/file_jk/'
patPath = appPath + '/patch_jk/'
astPath = appPath + '/ast_jk/'
outPath = appPath + '/out_jk/'
optPath = appPath + '/outp_jk/'
_CHOICE_ = 8 # The choice number.
_DEBUG_ = 0 # 1: only use one sample, 0: use all samples.
_VERSION_ = 1 # 1: 'after', 0: 'before'.
version = 'after' if (_VERSION_) else 'before'
def main():
global _DEBUG_
# Generate AST files from program files.
#GenerateASTs(datPath, astPath)
# get patch lists from patch path.
patchList = ScanPatches(patPath, datPath)
# scan all the files.
for root, ds, fs in os.walk(os.path.join(datPath, version)):
for file in fs:
# if _DEBUG_ == 1, run single C/Cpp file; if _DEBUG_ == 0, run all files.
if _DEBUG_ <= 96: # can change to N.
extname = os.path.splitext(file)[1] # get the file extension.
if '.c' == extname or '.cpp' == extname: # if file extension is .c or .cpp.
if _DEBUG_ >= 1: _DEBUG_ += 1 # _DEBUG_ = 2
# ===================================================================
# get the corresponding program file name.
filename = os.path.join(root, file) # get the program file name.
filename = filename.replace('\\', '/')
print(filename)
# get the corresponding ast file name.
astname = filename.replace(datPath, astPath, 1) + '.txt'
if _DEBUG_: print('[DEBUG] ', astname)
# find all IF statements in the ast file.
ifstmts = FindIfStmts(astname)
for ifstmt in ifstmts:
# check if the IF statements changed in diff file.
patchname, linenums = CheckIfChanged(filename, ifstmt, patchList, version)
if (len(patchname)): # find the changed IF statements from diff file.
if _DEBUG_: print('[DEBUG] ', filename, ifstmt, patchname, linenums)
# ===========================================================
# get variants of the source code.
codeChanged, nChoice = CodeOversampling(filename, ifstmt, -1)
SaveToFile(codeChanged, root.replace(datPath, outPath, 1), file)
# get variants of the patch.
patchChanged, _, ok = PatchOversampling(patchname, version, filename, linenums, ifstmt, nChoice)
if (ok): SaveToFile(patchChanged, optPath, patchname.replace(patPath, ''))
return
def GenerateASTs(dataPath, ASTsPath):
'''
Generate AST files from dataPath to ASTsPath.
clang -fmodules -fsyntax-only -Xclang -ast-dump [dataPath]/**/**.c(pp) 1>[ASTsPath]/**/**.c(pp).txt 2>NUL
:param dataPath: data path.
:param ASTPath: ast path.
:return: 0 if success.
'''
# print the arguments.
print('Data Path: ' + dataPath)
print('ASTs Path: ' + ASTsPath)
# go through each file in data path.
for root, ds, fs in os.walk(dataPath):
for file in fs:
extname = os.path.splitext(file)[1] # get the file extension.
if '.c' == extname or '.cpp' == extname: # if file extension is .c or .cpp.
outRoot = root.replace(dataPath, ASTsPath, 1) # get the output path.
if not os.path.exists(outRoot): # if output path does not exist, build the directory.
os.makedirs(outRoot)
filename = os.path.join(root, file) # get the program file name.
astsname = os.path.join(outRoot, file + '.txt') # get the ast file name.
# set the shell command.
cmdstr = 'clang -fmodules -fsyntax-only -Xclang -ast-dump ' + filename + ' 1>' + astsname + ' 2>NUL'
print('[shell] >>> ' + cmdstr)
os.system(cmdstr) # run the shell command.
return 0
def ScanPatches(patchPath, dataPatch):
'''
Scan the patches and find all hunks that contains IF statements.
:param patchPath: patch file path.
:param dataPatch: program file path.
:return: all the hunk information that contains IF statements.
[[bfname, afname, bStart, bEnd, aStart, aEnd, filename], ...]
'''
def ReadPatch(filename, dataPatch):
'''
Scan the patches and find all hunks that contains IF statements.
:param filename: patch file name.
:param dataPatch: program file path.
:return: all the hunk information that contains IF statements.
[[bfname, afname, bStart, bEnd, aStart, aEnd, filename], ...]
'''
def FindCorrespondLine(atStat, lineNum):
'''
Find the line numbers of the hunk.
:param atStat: all the hunk information.
[[lineNum, bStart, bEnd, aStart, aEnd], ...]
:param lineNum: the total line number of the patch file.
:return: line number of hunk.
[bStart, bEnd, aStart, aEnd]
'''
for item in reversed(atStat):
if item[0] < lineNum:
break
return item[1:]
# read the patch file.
fp = open(filename, encoding='utf-8', errors='ignore') # get file point.
lines = fp.readlines() # read all lines.
numLines = len(lines) # get the line number.
# find all @@ -000,00 +000,00 @@
atStat = [] # [[i, 70, 71, 70, 73], ...]
for i in range(numLines):
# search from line[i]
if re.match(r'@@ -\d+,\d+ \+\d+,\d+ @@', lines[i]):
# get @ statement.
atstmt = re.findall(r'@@ -\d+,\d+ \+\d+,\d+ @@', lines[i])
atnum = re.findall(r'\d+', atstmt[0]) # get 4 numbers
bStart = int(atnum[0])
bEnd = bStart + max(1, int(atnum[1])) - 1
aStart = int(atnum[2])
aEnd = aStart + max(1, int(atnum[3])) - 1
if 0:
print(i, bStart, bEnd, aStart, aEnd)
atStat.append([i, bStart, bEnd, aStart, aEnd])
if 0:
print(atStat)
# read contents line by line.
outputs = []
bfname = ''
afname = ''
for i in range(numLines):
# line[i]
# find diff --git a/**** b/****
if re.match(r'diff --git a/(.*) b/(.*)', lines[i]):
segs = lines[i].split() # split the line.
aname = segs[2][2:] # get after name.
bname = segs[3][2:] # get before name.
if 0:
print(os.path.join(os.path.join(dataPatch, 'after'), aname), os.path.join(os.path.join(dataPatch, 'before'), bname))
# check if two files exist.
if (os.path.exists(os.path.join(os.path.join(dataPatch, 'after'), aname))
& os.path.exists(os.path.join(os.path.join(dataPatch, 'before'), bname))):
bfname = os.path.join(os.path.join(dataPatch, 'before'), bname).replace('\\', '/')
afname = os.path.join(os.path.join(dataPatch, 'after'), aname).replace('\\', '/')
else:
bfname = ''
afname = ''
# find IF statement.
ifstmts = re.findall(r'if\s*\(', lines[i])
if (len(ifstmts) & (bfname != '') & (afname != '')): # if lines[i] contains IF statements.
if 0:
print(i, lines[i], end='')
block = FindCorrespondLine(atStat, i) # get [70, 71, 70, 73]
if 0:
print(block)
outputs.append([bfname, afname] + block + [filename])
# delete replications.
finalOut = []
for item in outputs:
if not item in finalOut:
finalOut.append(item)
if 0:
print(finalOut)
return finalOut
# get all the changes that contains IF statements.
outputs = []
# scan all the patch files.
for root, ds, fs in os.walk(patchPath):
for file in fs:
filename = os.path.join(root, file) # get the patch filename.
filename = filename.replace('\\', '/')
# read the patch and get all patch changes that contains IF statements.
out = ReadPatch(filename, dataPatch)
outputs.extend(out)
# delete replications.
finalOut = []
for item in outputs:
if not item in finalOut:
finalOut.append(item)
if _DEBUG_:
print('[DEBUG] All the patch changes contains IF statements:\n[DEBUG] ', end='')
print(finalOut)
return finalOut
def FindIfStmts(fname):
'''
Find all the IF statements from the AST file.
:param fname: AST file name.
:return: All the IF block information.
[[ifStart, ifEnd], ...]
'''
# check if the ast file exists.
if not os.path.exists(fname):
print('[Warning] Cannot find the file %s' % (fname))
return []
# read lines of the ast file.
fp = open(fname, encoding='utf-8', errors='ignore') # get the ast file point.
lines = fp.readlines() # read all lines.
numLines = len(lines) # get the line number.
ifstmts = [] # [[74, 77], ...]
# search each line in the ast file.
for i in range(numLines): # lines[i].
# find ****IfStmt *** <line:000:00, line:000:00> ***
if re.match(r'(.*)IfStmt(.*)<line:\d+:\d+, line:\d+:\d+>(.*)', lines[i]):
if _DEBUG_ & 0:
print('[DEBUG] ', i, lines[i], end='')
# find <line:000:00, line:000:00>
linestmt = re.findall(r'<line:\d+:\d+, line:\d+:\d+>', lines[i])
if _DEBUG_ & 0:
print('[DEBUG] ', linestmt)
# find line numbers in <line:000:00, line:000:00>
numbers = re.findall(r'\d+', linestmt[0])
if _DEBUG_ & 0:
print('[DEBUG] ', [numbers[0], numbers[2]])
ifstmts.append([int(numbers[0]), int(numbers[2])])
# return line numbers of if statements.
if _DEBUG_:
print('[DEBUG] ', ifstmts)
return ifstmts
def CheckIfChanged(fname, ifstmt, patchList, mode='after'):
'''
Check if the IF statement is changed in the patches.
:param fname: program file.
:param ifstmt: IF statement.
[ifStart, ifEnd]
:param patchList: all the hunk information that contains IF statements.
[[bfname, afname, bStart, bEnd, aStart, aEnd, filename], ...]
:param mode: 'after' or 'before'
:return: YES: filename, [bStart, bEnd, aStart, aEnd]
NO: '', [0, 0, 0, 0]
'''
if mode == 'after':
for item in patchList:
if (fname == item[1]):
if (item[4]+3 <= ifstmt[0]) & (ifstmt[0] <= item[5]-3):
return item[6], item[2:6]
elif mode == 'before':
for item in patchList:
if (fname == item[0]):
if (item[2]+3 <= ifstmt[0]) & (ifstmt[0] <= item[3]-3):
return item[6], item[2:6]
return '', [0, 0, 0, 0]
def CodeOversampling(fname, ifstmt, nChoice=-1):
'''
Code Oversampling.
:param fname: the program file.
:param ifstmt: IF statement.
[ifStart, ifEnd]
:param nChoice: the choice of oversampling. -1: random, 0-N: specific.
:return: lines: the changed code.
nChoice: the final choice of oversampling.
'''
# read file from the filename.
fp = open(fname, encoding='utf-8', errors='ignore')
lines = fp.readlines()
numLines = len(lines)
# sparse the augments.
stmtStart = ifstmt[0] - 1
stmtEnd = ifstmt[1] - 1
if 0:
print(stmtStart, stmtEnd)
# print each line of the program file.
for i in range(stmtStart-3, stmtEnd+4):
if 0:
print(i, lines[i], end='')
# locate: get the IF block
ifBlock = ''
for i in range(stmtStart, stmtEnd+1):
ifBlock += lines[i]
if _DEBUG_: print(ifBlock)
# find the index of the first if__(
ifStart = re.findall(r'if\s*\(', ifBlock)
if 0:
print(ifStart)
indexIfStart = ifBlock.index(ifStart[0])
if 0:
print(indexIfStart, ifBlock[indexIfStart])
# find the corresponding (
indexIfLeft = indexIfStart + len(ifStart[0]) - 1
if 0:
print(indexIfLeft, ifBlock[indexIfLeft])
# find the corresponding )
mark = 1
indexIfRight = indexIfLeft
while(mark):
indexIfRight += 1
if ifBlock[indexIfRight] == '(':
mark += 1
elif ifBlock[indexIfRight] == ')':
mark -= 1
if 0:
print(indexIfRight, ifBlock[indexIfRight])
# modify: change the IF block.
if (nChoice not in range(_CHOICE_)): # if nChoice is not in our settings.
nChoice = random.randint(0, _CHOICE_-1) # randomly choose.
newBlock = ''
if (0 == nChoice): # add 1, modify 1
newBlock += ' ' * indexIfStart + 'const int _SYS_ZERO = 0; \n'
newBlock += ifBlock[:indexIfLeft+1] + '_SYS_ZERO || ' + ifBlock[indexIfLeft+1:]
elif (1 == nChoice): # add 1, modify 1
newBlock += ' ' * indexIfStart + 'const int _SYS_ONE = 1; \n'
newBlock += ifBlock[:indexIfLeft+1] + '_SYS_ONE && ' + ifBlock[indexIfLeft+1:]
elif (2 == nChoice):
newBlock += ' ' * indexIfStart + 'bool _SYS_STMT = ' + ifBlock[indexIfLeft+1:indexIfRight] + ';\n'
newBlock += ifBlock[:indexIfLeft+1] + 'True == _SYS_STMT' + ifBlock[indexIfRight:]
elif (3 == nChoice):
newBlock += ' ' * indexIfStart + 'bool _SYS_STMT = !(' + ifBlock[indexIfLeft + 1:indexIfRight] + ');\n'
newBlock += ifBlock[:indexIfLeft + 1] + '!_SYS_STMT' + ifBlock[indexIfRight:]
elif (4 == nChoice):
newBlock += ' ' * indexIfStart + 'int _SYS_VAL = 0;\n'
newBlock += ifBlock[:indexIfRight+1] + ' {\n'
newBlock += ' ' * (indexIfStart+4) + 'int _SYS_VAL = 1;\n'
newBlock += ' ' * indexIfStart +'}\n'
newBlock += ifBlock[:indexIfLeft+1] + '_SYS_VAL && ' + ifBlock[indexIfLeft+1:]
elif (5 == nChoice):
newBlock += ' ' * indexIfStart + 'int _SYS_VAL = 1;\n'
newBlock += ifBlock[:indexIfRight + 1] + ' {\n'
newBlock += ' ' * (indexIfStart + 4) + 'int _SYS_VAL = 0;\n'
newBlock += ' ' * indexIfStart + '}\n'
newBlock += ifBlock[:indexIfLeft+1] + '!_SYS_VAL || ' + ifBlock[indexIfLeft+1:]
elif (6 == nChoice):
newBlock += ' ' * indexIfStart + 'int _SYS_VAL = 0;\n'
newBlock += ifBlock[:indexIfRight + 1] + ' {\n'
newBlock += ' ' * (indexIfStart + 4) + 'int _SYS_VAL = 1;\n'
newBlock += ' ' * indexIfStart + '}\n'
newBlock += ifBlock[:indexIfLeft+1] + '_SYS_VAL' + ifBlock[indexIfRight:]
elif (7 == nChoice):
newBlock += ' ' * indexIfStart + 'int _SYS_VAL = 1;\n'
newBlock += ifBlock[:indexIfRight + 1] + ' {\n'
newBlock += ' ' * (indexIfStart + 4) + 'int _SYS_VAL = 0;\n'
newBlock += ' ' * indexIfStart + '}\n'
newBlock += ifBlock[:indexIfLeft + 1] + '!_SYS_VAL' + ifBlock[indexIfRight:]
if _DEBUG_: print(newBlock)
# change string to lists.
newBlockList = newBlock.split('\n')
ifBlockList = []
for stmt in newBlockList[:-1]:
ifBlockList.append(stmt + '\n')
if 0:
print(ifBlockList)
# delete original block.
del lines[stmtStart:stmtEnd+1]
# add modified block.
for i in range(len(ifBlockList)):
lines.insert(stmtStart+i, ifBlockList[i])
for i in range(stmtStart-3, stmtEnd+4):
if 0:
print(i, lines[i], end='')
return lines, nChoice
def SaveToFile(lines, path, file):
'''
Save the changed code or patch.
:param lines: the changed code or patch.
:param path: the save path.
:param file: the save filename without variant.
:return: 0 if success.
'''
# check the path.
if not os.path.exists(path):
os.makedirs(path)
# get all filenames in the folder.
fileList = os.listdir(path)
if 0:
print(fileList)
# get the filename and extension.
filename = os.path.splitext(file)
if 0:
print(filename[0], filename[1])
# find an available name.
fileIdx = 1
while(1):
fname = filename[0] + '_' + str(fileIdx).zfill(4) + filename[1]
if not os.path.exists(os.path.join(path, fname)):
break
else:
fileIdx += 1
# save file.
fpath = os.path.join(path, fname).replace('\\', '/')
fp = open(fpath, 'w')
for i in range(len(lines)):
fp.write(lines[i])
fp.close()
if _DEBUG_:
print('[DEBUG] Save code variant in ' + fpath)
return 0
def PatchOversampling(pname, version, fname, lnums, ifstmt, nChoice=-1):
'''
Patch Oversampling.
:param pname: patch filename.
:param version: 'after' or 'before'.
:param fname: program filename.
:param lnums: corresponding hunk.
[bStart, bEnd, aStart, aEnd]
:param ifstmt: IF statement.
[ifStart, ifEnd]
:param nChoice: the choice of oversampling. -1: random; 0-N: specific.
:return: lines: changed/unchanged patch.
nChoice: the final choice. -1 for failure; 0-N for success.
ok: 0 for failure; 1 for success.
'''
def AfterPatchProc(ifBlockSeg, ifPatchSeg):
marks = -1 * np.ones(len(ifBlockSeg) + 1, dtype=int)
for i in range(len(ifBlockSeg)):
for j in range(1 + np.max(marks), len(ifPatchSeg)):
if ifBlockSeg[i][1:] == ifPatchSeg[j][1:]:
marks[i] = j
break
#print(marks)
for i in reversed(range(len(marks) - 1)):
if (marks[i] >= 0):
#print(i, marks[i])
if (-1 == np.max(marks[i + 1:])):
ifBlockSeg = ifBlockSeg[:i + 1] + ifPatchSeg[marks[i] + 1:] + ifBlockSeg[i + 1:]
else:
indexNext = [item for item in marks[i + 1:] if (item >= 0)][0]
ifBlockSeg = ifBlockSeg[:i + 1] + ifPatchSeg[marks[i] + 1:indexNext] + ifBlockSeg[i + 1:]
if ('+' == ifPatchSeg[marks[i]][0]):
if ('-' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ''
elif ('+' == ifBlockSeg[i][0]):
ifBlockSeg.insert(i + 1, ifPatchSeg[marks[i]])
elif (' ' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ifPatchSeg[marks[i]]
elif ('-' == ifPatchSeg[marks[i]][0]):
if ('+' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ''
elif ('-' == ifBlockSeg[i][0]):
ifBlockSeg.insert(i + 1, ifPatchSeg[marks[i]])
elif (' ' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ifPatchSeg[marks[i]]
#print(ifBlockSeg)
newBlock = ''
#print(newBlock, end='')
for item in ifBlockSeg:
if '' != item:
newBlock += item + '\n'
return newBlock
def BeforePatchProc(ifBlockSeg, ifPatchSeg):
marks = -1 * np.ones(len(ifBlockSeg) + 1, dtype=int)
for i in reversed(range(len(ifBlockSeg))):
marks_temp = [item for item in marks if item >= 0]
rangeEnd = len(ifPatchSeg) if (np.max(marks) == -1) else (np.min(marks_temp))
for j in reversed(range(rangeEnd)):
if ifBlockSeg[i][1:] == ifPatchSeg[j][1:]:
marks[i] = j
break
#print(marks)
for i in reversed(range(len(marks) - 1)):
if (marks[i] >= 0):
#print(i, marks[i])
if (-1 == np.max(marks[i + 1:])):
ifBlockSeg = ifBlockSeg[:i + 1] + ifPatchSeg[marks[i] + 1:] + ifBlockSeg[i + 1:]
else:
indexNext = [item for item in marks[i + 1:] if (item >= 0)][0]
ifBlockSeg = ifBlockSeg[:i + 1] + ifPatchSeg[marks[i] + 1:indexNext] + ifBlockSeg[i + 1:]
if ('+' == ifPatchSeg[marks[i]][0]):
if ('-' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ''
elif ('+' == ifBlockSeg[i][0]):
ifBlockSeg.insert(i + 1, ifPatchSeg[marks[i]])
elif (' ' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ifPatchSeg[marks[i]]
elif ('-' == ifPatchSeg[marks[i]][0]):
if ('+' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ''
elif ('-' == ifBlockSeg[i][0]):
ifBlockSeg.insert(i + 1, ifPatchSeg[marks[i]])
elif (' ' == ifBlockSeg[i][0]):
ifBlockSeg[i] = ifPatchSeg[marks[i]]
#print(ifBlockSeg)
newBlock = ''
#print(newBlock, end='')
for item in ifBlockSeg:
if '' != item:
newBlock += item + '\n'
return newBlock
if _DEBUG_:
print(pname, version, fname, lnums, ifstmt, nChoice)
# get the matching file string.
filestr = fname.replace(datPath, '', 1)
filestr = filestr.replace(version, '', 1)
if 'after' == version:
filestr = 'a' + filestr
elif 'before' == version:
filestr = 'b' + filestr
# get the matching line string.
linestr = '@@ -'
linestr += str(lnums[0]) + ','
linestr += str(lnums[1]-lnums[0]+1) + ' +'
linestr += str(lnums[2]) + ','
linestr += str(lnums[3]-lnums[2]+1) + ' @@'
# get version string.
verstr = '+' if (version == 'after') else '-' if (version == 'before') else ' '
verlnum = lnums[2] if (version == 'after') else lnums[0] if (version == 'before') else 0
# read the patch file.
fp = open(pname, encoding='utf-8', errors='ignore')
lines = fp.readlines()
numLines = len(lines)
# get the line numbers of IF block in diff file.
markDiff = 0
markLine = 0
cnt = 0
ifloc = [0, 0]
# scan each line of the patch file.
for i in range(numLines): # lines[i].
# find diff --git a/**** b/****
if re.match(r'diff --git a/(.*) b/(.*)', lines[i]):
segs = lines[i].split() # split the line.
markDiff = 1 if (filestr in segs) else 0
# find @@ -000,00 +000,00 @@
if re.match(r'@@ -\d+,\d+ \+\d+,\d+ @@', lines[i]):
# get @ statement.
atstmt = re.findall(r'@@ -\d+,\d+ \+\d+,\d+ @@', lines[i])
markLine = 1 if (atstmt[0] == linestr) else 0
if ((markDiff & markLine) and (lines[i][0] in ['@', ' ', verstr])):
if _DEBUG_: print(i, lines[i], end='')
cnt += 1
if cnt == (ifstmt[0] - verlnum + 2):
ifloc[0] = i
ifloc[1] = min(ifloc[0] + ifstmt[1] - ifstmt[0], numLines - 1)
# locate: get the IF block
if _DEBUG_: print(ifloc)
ifBlock = ''
for i in range(ifloc[0], ifloc[1] + 1):
ifBlock += lines[i]
# find the index of the first if__(
ifStart = re.findall(r'if\s*\(', ifBlock)
if ([] == ifStart):
return lines, -1, 0
indexIfStart = ifBlock.index(ifStart[0])
# find the corresponding (
indexIfLeft = indexIfStart + len(ifStart[0]) - 1
# find the corresponding )
mark = 1
indexIfRight = indexIfLeft
while (mark):
indexIfRight += 1
if ifBlock[indexIfRight] == '(':
mark += 1
elif ifBlock[indexIfRight] == ')':
mark -= 1
# modify: change the IF block.
if (nChoice not in range(_CHOICE_)): # if nChoice is not in our settings.
nChoice = random.randint(0, _CHOICE_ - 1) # randomly choose.
newBlock = ''
if (0 == nChoice):
newBlock += verstr + ' ' * (indexIfStart-1) + 'const int _SYS_ZERO = 0; \n'
if ifBlock[0] == verstr: # start with '+' or '-'
newBlock += ifBlock[:indexIfLeft + 1] + '_SYS_ZERO || ' + ifBlock[indexIfLeft + 1:]
elif verstr == '+': # start with ' '
newBlock += '+' + ifBlock[1:indexIfLeft + 1] + '_SYS_ZERO || ' + ifBlock[indexIfLeft + 1:]
newBlock = '-' + ifBlock.split('\n')[0][1:] + '\n' + newBlock
elif verstr == '-': # start with ' ' and before.
indexEnter = ifBlock.find('\n', 1)
newBlock += '-' + ifBlock[1:indexIfLeft + 1] + '_SYS_ZERO || ' + ifBlock[indexIfLeft + 1:indexEnter+1]
newBlock += '+' + ifBlock.split('\n')[0][1:] + '\n'
newBlock += ifBlock[indexEnter + 1:]
elif (1 == nChoice):
newBlock += verstr + ' ' * (indexIfStart - 1) + 'const int _SYS_ONE = 1; \n'
if ifBlock[0] == verstr: # start with '+' or '-'
newBlock += ifBlock[:indexIfLeft + 1] + '_SYS_ONE && ' + ifBlock[indexIfLeft + 1:]
elif verstr == '+': # start with ' '
newBlock += '+' + ifBlock[1:indexIfLeft + 1] + '_SYS_ONE && ' + ifBlock[indexIfLeft + 1:]
newBlock = '-' + ifBlock.split('\n')[0][1:] + '\n' + newBlock
elif verstr == '-': # start with ' ' and before.
indexEnter = ifBlock.find('\n', 1)
newBlock += '-' + ifBlock[1:indexIfLeft + 1] + '_SYS_ONE && ' + ifBlock[indexIfLeft + 1:indexEnter + 1]
newBlock += '+' + ifBlock.split('\n')[0][1:] + '\n'
newBlock += ifBlock[indexEnter + 1:]
elif (2 == nChoice):
# get if block segments.
ifBlockSeg = ifBlock.split('\n')
del ifBlockSeg[-1]
# get the end of the line of the if judgement.
indexIfJudgeEnd = 0
if ('\n' != ifBlock[indexIfRight+1]):
indexIfJudgeEnd = ifBlock[indexIfRight+1:].find('\n', 1)
# get the deleted patch.
ifPatchDel = '-' + ifBlock[1:indexIfRight+1+indexIfJudgeEnd+1]
ifPatchDel = ifPatchDel.replace('\n ', '\n-')
ifPatchDel = ifPatchDel.replace('\n+', '\n-')
# get the added patch.
ifPatchAdd = '+' + ' ' * (indexIfStart-1) + 'bool _SYS_STMT = ' + ifBlock[indexIfLeft + 1:indexIfRight] + ';\n'
ifPatchAdd = ifPatchAdd.replace('\n ', '\n+')
ifPatchAdd = ifPatchAdd.replace('\n-', '\n+')
ifPatchAdd += '+' + ifBlock[1:indexIfLeft+1] + 'True == _SYS_STMT' + ifBlock[indexIfRight:indexIfRight+1+indexIfJudgeEnd+1]
# after or before.
if verstr == '+':
ifPatch = ifPatchDel + ifPatchAdd
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
newBlock = AfterPatchProc(ifBlockSeg, ifPatchSeg)
elif verstr == '-':
ifPatchDel = '+' + ifPatchDel[1:]
ifPatchDel = ifPatchDel.replace('\n-', '\n+')
ifPatchAdd = '-' + ifPatchAdd[1:]
ifPatchAdd = ifPatchAdd.replace('\n+', '\n-')
ifPatch = ifPatchAdd + ifPatchDel
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
#print(ifPatchSeg)
newBlock = BeforePatchProc(ifPatchSeg, ifBlockSeg)
# print
if _DEBUG_: print(ifBlock, end='')
if _DEBUG_: print('------------------------------')
if _DEBUG_: print(ifBlockSeg)
if _DEBUG_: print('------------------------------')
if _DEBUG_: print(ifPatchSeg)
if _DEBUG_: print('------------------------------')
elif (3 == nChoice):
# get if block segments.
ifBlockSeg = ifBlock.split('\n')
del ifBlockSeg[-1]
# get the end of the line of the if judgement.
indexIfJudgeEnd = 0
if ('\n' != ifBlock[indexIfRight + 1]):
indexIfJudgeEnd = ifBlock[indexIfRight + 1:].find('\n', 1)
# get the deleted patch.
ifPatchDel = '-' + ifBlock[1:indexIfRight + 1 + indexIfJudgeEnd + 1]
ifPatchDel = ifPatchDel.replace('\n ', '\n-')
ifPatchDel = ifPatchDel.replace('\n+', '\n-')
# get the added patch.
ifPatchAdd = '+' + ' ' * (indexIfStart - 1) + 'bool _SYS_STMT = !(' + ifBlock[indexIfLeft + 1:indexIfRight] + ');\n'
ifPatchAdd = ifPatchAdd.replace('\n ', '\n+')
ifPatchAdd = ifPatchAdd.replace('\n-', '\n+')
ifPatchAdd += '+' + ifBlock[1:indexIfLeft + 1] + '!_SYS_STMT' + ifBlock[indexIfRight:indexIfRight + 1 + indexIfJudgeEnd + 1]
# after or before.
if verstr == '+':
ifPatch = ifPatchDel + ifPatchAdd
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
newBlock = AfterPatchProc(ifBlockSeg, ifPatchSeg)
elif verstr == '-':
ifPatchDel = '+' + ifPatchDel[1:]
ifPatchDel = ifPatchDel.replace('\n-', '\n+')
ifPatchAdd = '-' + ifPatchAdd[1:]
ifPatchAdd = ifPatchAdd.replace('\n+', '\n-')
ifPatch = ifPatchAdd + ifPatchDel
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
#print(ifPatchSeg)
newBlock = BeforePatchProc(ifPatchSeg, ifBlockSeg)
elif (4 == nChoice):
newBlock += verstr + ' ' * (indexIfStart - 1) + 'int _SYS_VAL = 0;\n'
newBlock += ifBlock[:indexIfRight + 1] + ' {\n'
newBlock += verstr + ' ' * (indexIfStart + 3) + 'int _SYS_VAL = 1;\n'
newBlock += verstr + ' ' * (indexIfStart - 1) + '}\n'
newBlock = newBlock.replace('\n ', '\n' + verstr)
newBlock = newBlock.replace('\n-', '\n' + verstr)
newBlock = newBlock.replace('\n+', '\n' + verstr)
if ifBlock[0] == verstr: # start with '+' or '-'
newBlock += ifBlock[:indexIfLeft + 1] + '_SYS_VAL && ' + ifBlock[indexIfLeft + 1:]
elif verstr == '+': # start with ' '
newBlock += '+' + ifBlock[1:indexIfLeft + 1] + '_SYS_VAL && ' + ifBlock[indexIfLeft + 1:]
newBlock = '-' + ifBlock.split('\n')[0][1:] + '\n' + newBlock
elif verstr == '-': # start with ' ' and before.
indexEnter = ifBlock.find('\n', 1)
newBlock += '-' + ifBlock[1:indexIfLeft + 1] + '_SYS_VAL && ' + ifBlock[indexIfLeft + 1:indexEnter+1]
newBlock += '+' + ifBlock.split('\n')[0][1:] + '\n'
newBlock += ifBlock[indexEnter + 1:]
elif (5 == nChoice):
newBlock += verstr + ' ' * (indexIfStart - 1) + 'int _SYS_VAL = 1;\n'
newBlock += ifBlock[:indexIfRight + 1] + ' {\n'
newBlock += verstr + ' ' * (indexIfStart + 3) + 'int _SYS_VAL = 0;\n'
newBlock += verstr + ' ' * (indexIfStart - 1) + '}\n'
newBlock = newBlock.replace('\n ', '\n' + verstr)
newBlock = newBlock.replace('\n-', '\n' + verstr)
newBlock = newBlock.replace('\n+', '\n' + verstr)
if ifBlock[0] == verstr: # start with '+' or '-'
newBlock += ifBlock[:indexIfLeft + 1] + '!_SYS_VAL || ' + ifBlock[indexIfLeft + 1:]
elif verstr == '+': # start with ' '
newBlock += '+' + ifBlock[1:indexIfLeft + 1] + '!_SYS_VAL || ' + ifBlock[indexIfLeft + 1:]
newBlock = '-' + ifBlock.split('\n')[0][1:] + '\n' + newBlock
elif verstr == '-': # start with ' ' and before.
indexEnter = ifBlock.find('\n', 1)
newBlock += '-' + ifBlock[1:indexIfLeft + 1] + '!_SYS_VAL || ' + ifBlock[indexIfLeft + 1:indexEnter + 1]
newBlock += '+' + ifBlock.split('\n')[0][1:] + '\n'
newBlock += ifBlock[indexEnter + 1:]
elif (6 == nChoice):
# get if block segments.
ifBlockSeg = ifBlock.split('\n')
del ifBlockSeg[-1]
# get the end of the line of the if judgement.
indexIfJudgeEnd = 0
if ('\n' != ifBlock[indexIfRight + 1]):
indexIfJudgeEnd = ifBlock[indexIfRight + 1:].find('\n', 1)
# get the deleted patch.
ifPatchDel = '-' + ifBlock[1:indexIfRight + 1 + indexIfJudgeEnd + 1]
ifPatchDel = ifPatchDel.replace('\n ', '\n-')
ifPatchDel = ifPatchDel.replace('\n+', '\n-')
# get the added patch.
ifPatchAdd = verstr + ' ' * (indexIfStart - 1) + 'int _SYS_VAL = 0;\n'
ifPatchAdd += ifBlock[:indexIfRight + 1] + ' { \n'
ifPatchAdd += verstr + ' ' * (indexIfStart + 3) + 'int _SYS_VAL = 1;\n'
ifPatchAdd += verstr + ' ' * (indexIfStart - 1) + '} \n'
ifPatchAdd = ifPatchAdd.replace('\n ', '\n+')
ifPatchAdd = ifPatchAdd.replace('\n-', '\n+')
ifPatchAdd += '+' + ifBlock[1:indexIfLeft + 1] + '_SYS_VAL' + ifBlock[indexIfRight:indexIfRight + 1 + indexIfJudgeEnd + 1]
# after or before.
if verstr == '+':
ifPatch = ifPatchDel + ifPatchAdd
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
newBlock = AfterPatchProc(ifBlockSeg, ifPatchSeg)
elif verstr == '-':
ifPatchDel = '+' + ifPatchDel[1:]
ifPatchDel = ifPatchDel.replace('\n-', '\n+')
ifPatchAdd = '-' + ifPatchAdd[1:]
ifPatchAdd = ifPatchAdd.replace('\n+', '\n-')
ifPatch = ifPatchAdd + ifPatchDel
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
#print(ifPatchSeg)
newBlock = BeforePatchProc(ifPatchSeg, ifBlockSeg)
elif (7 == nChoice):
# get if block segments.
ifBlockSeg = ifBlock.split('\n')
del ifBlockSeg[-1]
# get the end of the line of the if judgement.
indexIfJudgeEnd = 0
if ('\n' != ifBlock[indexIfRight + 1]):
indexIfJudgeEnd = ifBlock[indexIfRight + 1:].find('\n', 1)
# get the deleted patch.
ifPatchDel = '-' + ifBlock[1:indexIfRight + 1 + indexIfJudgeEnd + 1]
ifPatchDel = ifPatchDel.replace('\n ', '\n-')
ifPatchDel = ifPatchDel.replace('\n+', '\n-')
# get the added patch.
ifPatchAdd = verstr + ' ' * (indexIfStart - 1) + 'int _SYS_VAL = 1;\n'
ifPatchAdd += ifBlock[:indexIfRight + 1] + ' { \n'
ifPatchAdd += verstr + ' ' * (indexIfStart + 3) + 'int _SYS_VAL = 0;\n'
ifPatchAdd += verstr + ' ' * (indexIfStart - 1) + '} \n'
ifPatchAdd = ifPatchAdd.replace('\n ', '\n+')
ifPatchAdd = ifPatchAdd.replace('\n-', '\n+')
ifPatchAdd += '+' + ifBlock[1:indexIfLeft + 1] + '!_SYS_VAL' + ifBlock[indexIfRight:indexIfRight + 1 + indexIfJudgeEnd + 1]
# after or before.
if verstr == '+':
ifPatch = ifPatchDel + ifPatchAdd
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
newBlock = AfterPatchProc(ifBlockSeg, ifPatchSeg)
elif verstr == '-':
ifPatchDel = '+' + ifPatchDel[1:]
ifPatchDel = ifPatchDel.replace('\n-', '\n+')
ifPatchAdd = '-' + ifPatchAdd[1:]
ifPatchAdd = ifPatchAdd.replace('\n+', '\n-')
ifPatch = ifPatchAdd + ifPatchDel
ifPatchSeg = ifPatch.split('\n')
del ifPatchSeg[-1]
#print(ifPatchSeg)
newBlock = BeforePatchProc(ifPatchSeg, ifBlockSeg)
# change string to lists.
newBlockList = newBlock.split('\n')
ifBlockList = []
for stmt in newBlockList[:-1]:
ifBlockList.append(stmt + '\n')
# delete original block.
del lines[ifloc[0]:ifloc[1] + 1]
# add modified block.
for i in range(len(ifBlockList)):
lines.insert(ifloc[0] + i, ifBlockList[i])
for i in range(ifloc[0]-3, min(ifloc[1]+10, len(lines))):
if _DEBUG_: print(i, lines[i], end='')
return lines, nChoice, 1
if __name__ == '__main__':
main()