-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
707 lines (615 loc) · 29.7 KB
/
utils.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
import torch
import torch.nn as nn
import copy
import random
import sys
from tqdm import tqdm
from torch.utils.data.dataset import Dataset
import os
import numpy as np
import csv
from python_parser.run_parser import get_example, get_example_batch
python_keywords = ['import', '', '[', ']', ':', ',', '.', '(', ')', '{', '}', 'not', 'is', '=', "+=", '-=', "<", ">",
'+', '-', '*', '/', 'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global',
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try',
'while', 'with', 'yield']
java_keywords = ["abstract", "assert", "boolean", "break", "byte", "case", "catch", "do", "double", "else", "enum",
"extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
"int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return",
"short", "static", "strictfp", "super", "switch", "throws", "transient", "try", "void", "volatile",
"while"]
java_special_ids = ["main", "args", "Math", "System", "Random", "Byte", "Short", "Integer", "Long", "Float", "Double", "Character",
"Boolean", "Data", "ParseException", "SimpleDateFormat", "Calendar", "Object", "String", "StringBuffer",
"StringBuilder", "DateFormat", "Collection", "List", "Map", "Set", "Queue", "ArrayList", "HashSet", "HashMap"]
c_keywords = ["auto", "break", "case", "char", "const", "continue",
"default", "do", "double", "else", "enum", "extern",
"float", "for", "goto", "if", "inline", "int", "long",
"register", "restrict", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef",
"union", "unsigned", "void", "volatile", "while",
"_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex",
"_Generic", "_Imaginary", "_Noreturn", "_Static_assert",
"_Thread_local", "__func__"]
c_macros = ["NULL", "_IOFBF", "_IOLBF", "BUFSIZ", "EOF", "FOPEN_MAX", "TMP_MAX", # <stdio.h> macro
"FILENAME_MAX", "L_tmpnam", "SEEK_CUR", "SEEK_END", "SEEK_SET",
"NULL", "EXIT_FAILURE", "EXIT_SUCCESS", "RAND_MAX", "MB_CUR_MAX"] # <stdlib.h> macro
c_special_ids = ["main", # main function
"stdio", "cstdio", "stdio.h", # <stdio.h> & <cstdio>
"size_t", "FILE", "fpos_t", "stdin", "stdout", "stderr", # <stdio.h> types & streams
"remove", "rename", "tmpfile", "tmpnam", "fclose", "fflush", # <stdio.h> functions
"fopen", "freopen", "setbuf", "setvbuf", "fprintf", "fscanf",
"printf", "scanf", "snprintf", "sprintf", "sscanf", "vprintf",
"vscanf", "vsnprintf", "vsprintf", "vsscanf", "fgetc", "fgets",
"fputc", "getc", "getchar", "putc", "putchar", "puts", "ungetc",
"fread", "fwrite", "fgetpos", "fseek", "fsetpos", "ftell",
"rewind", "clearerr", "feof", "ferror", "perror", "getline"
"stdlib", "cstdlib", "stdlib.h", # <stdlib.h> & <cstdlib>
"size_t", "div_t", "ldiv_t", "lldiv_t", # <stdlib.h> types
"atof", "atoi", "atol", "atoll", "strtod", "strtof", "strtold", # <stdlib.h> functions
"strtol", "strtoll", "strtoul", "strtoull", "rand", "srand",
"aligned_alloc", "calloc", "malloc", "realloc", "free", "abort",
"atexit", "exit", "at_quick_exit", "_Exit", "getenv",
"quick_exit", "system", "bsearch", "qsort", "abs", "labs",
"llabs", "div", "ldiv", "lldiv", "mblen", "mbtowc", "wctomb",
"mbstowcs", "wcstombs",
"string", "cstring", "string.h", # <string.h> & <cstring>
"memcpy", "memmove", "memchr", "memcmp", "memset", "strcat", # <string.h> functions
"strncat", "strchr", "strrchr", "strcmp", "strncmp", "strcoll",
"strcpy", "strncpy", "strerror", "strlen", "strspn", "strcspn",
"strpbrk" ,"strstr", "strtok", "strxfrm",
"memccpy", "mempcpy", "strcat_s", "strcpy_s", "strdup", # <string.h> extension functions
"strerror_r", "strlcat", "strlcpy", "strsignal", "strtok_r",
"iostream", "istream", "ostream", "fstream", "sstream", # <iostream> family
"iomanip", "iosfwd",
"ios", "wios", "streamoff", "streampos", "wstreampos", # <iostream> types
"streamsize", "cout", "cerr", "clog", "cin",
"boolalpha", "noboolalpha", "skipws", "noskipws", "showbase", # <iostream> manipulators
"noshowbase", "showpoint", "noshowpoint", "showpos",
"noshowpos", "unitbuf", "nounitbuf", "uppercase", "nouppercase",
"left", "right", "internal", "dec", "oct", "hex", "fixed",
"scientific", "hexfloat", "defaultfloat", "width", "fill",
"precision", "endl", "ends", "flush", "ws", "showpoint",
"sin", "cos", "tan", "asin", "acos", "atan", "atan2", "sinh", # <math.h> functions
"cosh", "tanh", "exp", "sqrt", "log", "log10", "pow", "powf",
"ceil", "floor", "abs", "fabs", "cabs", "frexp", "ldexp",
"modf", "fmod", "hypot", "ldexp", "poly", "matherr"]
special_char = ['[', ']', ':', ',', '.', '(', ')', '{', '}', 'not', 'is', '=', "+=", '-=', "<", ">", '+', '-', '*', '/',
'|']
def select_parents(population):
length = range(len(population))
index_1 = random.choice(length)
index_2 = random.choice(length)
chromesome_1 = population[index_1]
chromesome_2 = population[index_2]
return chromesome_1, index_1, chromesome_2, index_2
def mutate(chromesome, variable_substitue_dict):
tgt_index = random.choice(range(len(chromesome)))
tgt_word = list(chromesome.keys())[tgt_index]
chromesome[tgt_word] = random.choice(variable_substitue_dict[tgt_word])
return chromesome
def crossover(csome_1, csome_2, r=None):
if r is None:
r = random.choice(range(len(csome_1))) # 随机选择一个位置.
# 但是不能选到0
child_1 = {}
child_2 = {}
for index, variable_name in enumerate(csome_1.keys()):
if index < r: #前半段
child_2[variable_name] = csome_1[variable_name]
child_1[variable_name] = csome_2[variable_name]
else:
child_1[variable_name] = csome_1[variable_name]
child_2[variable_name] = csome_2[variable_name]
return child_1, child_2
def map_chromesome(chromesome: dict, code: str, lang: str) -> str:
temp_replace = get_example_batch(code, chromesome, lang)
return temp_replace
input = ["0ab", "\ndsd", "说啊", "'z'", "for"]
from keyword import iskeyword
def is_valid_variable_python(name: str) -> bool:
return name.isidentifier() and not iskeyword(name)
def is_valid_variable_java(name: str) -> bool:
if not name.isidentifier():
return False
elif name in java_keywords:
return False
elif name in java_special_ids:
return False
return True
def is_valid_variable_c(name: str) -> bool:
if not name.isidentifier():
return False
elif name in c_keywords:
return False
elif name in c_macros:
return False
elif name in c_special_ids:
return False
return True
def is_valid_variable_name(name: str, lang: str) -> bool:
# check if matches language keywords
if lang == 'python':
return is_valid_variable_python(name)
elif lang == 'c':
return is_valid_variable_c(name)
elif lang == 'java':
return is_valid_variable_java(name)
else:
return False
def is_valid_substitue(substitute: str, tgt_word: str, lang: str) -> bool:
'''
判断生成的substitues是否valid,如是否满足命名规范
'''
is_valid = True
if not is_valid_variable_name(substitute, lang):
is_valid = False
return is_valid
def _tokenize(seq, tokenizer):
seq = seq.replace('\n', '')
words = seq.split(' ')
sub_words = []
keys = []
index = 0
for word in words:
# 并非直接tokenize这句话,而是tokenize了每个splited words.
sub = tokenizer.tokenize(word)
sub_words += sub
keys.append([index, index + len(sub)])
# 将subwords对齐
index += len(sub)
return words, sub_words, keys
def get_identifier_posistions_from_code(words_list: list, variable_names: list) -> dict:
'''
给定一串代码,以及variable的变量名,如: a
返回这串代码中这些变量名对应的位置.
'''
positions = {}
for name in variable_names:
for index, token in enumerate(words_list):
if name == token:
try:
positions[name].append(index)
except:
positions[name] = [index]
return positions
def get_bpe_substitues(substitutes, tokenizer, mlm_model):
'''
得到substitues
'''
# substitutes L, k
substitutes = substitutes[0:12, 0:4] # maximum BPE candidates
# find all possible candidates
all_substitutes = []
for i in range(substitutes.size(0)):
if len(all_substitutes) == 0:
lev_i = substitutes[i]
all_substitutes = [[int(c)] for c in lev_i]
else:
lev_i = []
for all_sub in all_substitutes[:24]: # 去掉不用的计算.
for j in substitutes[i]:
lev_i.append(all_sub + [int(j)])
all_substitutes = lev_i
# all substitutes list of list of token-id (all candidates)
c_loss = nn.CrossEntropyLoss(reduction='none')
word_list = []
# all_substitutes = all_substitutes[:24]
all_substitutes = torch.tensor(all_substitutes) # [ N, L ]
all_substitutes = all_substitutes[:24].to('cuda')
# 不是,这个总共不会超过24... 那之前生成那么多也没用....
N, L = all_substitutes.size()
word_predictions = mlm_model(all_substitutes)[0] # N L vocab-size
ppl = c_loss(word_predictions.view(N * L, -1), all_substitutes.view(-1)) # [ N*L ]
ppl = torch.exp(torch.mean(ppl.view(N, L), dim=-1)) # N
_, word_list = torch.sort(ppl)
word_list = [all_substitutes[i] for i in word_list]
final_words = []
for word in word_list:
tokens = [tokenizer._convert_id_to_token(int(i)) for i in word]
text = tokenizer.convert_tokens_to_string(tokens)
final_words.append(text)
return final_words
def get_substitues(substitutes, tokenizer, mlm_model, use_bpe, substitutes_score=None, threshold=3.0):
'''
将生成的substitued subwords转化为words
'''
# substitues L,k
# from this matrix to recover a word
words = []
sub_len, k = substitutes.size() # sub-len, k
if sub_len == 0:
# 比如空格对应的subwords就是[a,a],长度为0
return words
elif sub_len == 1:
# subwords就是本身
for (i, j) in zip(substitutes[0], substitutes_score[0]):
if threshold != 0 and j < threshold:
break
words.append(tokenizer._decode([int(i)]))
# 将id转为token.
else:
# word被分解成了多个subwords
if use_bpe == 1:
words = get_bpe_substitues(substitutes, tokenizer, mlm_model)
else:
return words
return words
def get_masked_code_by_position(tokens: list, positions: dict):
'''
给定一段文本,以及需要被mask的位置,返回一组masked后的text
Example:
tokens: [a,b,c]
positions: [0,2]
Return:
[<mask>, b, c]
[a, b, <mask>]
'''
masked_token_list = []
replace_token_positions = []
for variable_name in positions.keys():
for pos in positions[variable_name]:
masked_token_list.append(tokens[0:pos] + ['<unk>'] + tokens[pos + 1:])
replace_token_positions.append(pos)
return masked_token_list, replace_token_positions
def build_vocab(codes, limit=5000):
vocab_cnt = {"<str>": 0, "<char>": 0, "<int>": 0, "<fp>": 0}
for c in tqdm(codes):
for t in c:
if len(t)>0:
if t[0] == '"' and t[-1] == '"':
vocab_cnt["<str>"] += 1
elif t[0] == "'" and t[-1] == "'":
vocab_cnt["<char>"] += 1
elif t[0] in "0123456789.":
if 'e' in t.lower():
vocab_cnt["<fp>"] += 1
elif '.' in t:
if t == '.':
if t not in vocab_cnt.keys():
vocab_cnt[t] = 0
vocab_cnt[t] += 1
else:
vocab_cnt["<fp>"] += 1
else:
vocab_cnt["<int>"] += 1
elif t in vocab_cnt.keys():
vocab_cnt[t] += 1
else:
vocab_cnt[t] = 1
vocab_cnt = sorted(vocab_cnt.items(), key=lambda x:x[1], reverse=True)
idx2txt = ["<unk>"] + ["<pad>"] + [it[0] for index, it in enumerate(vocab_cnt) if index < limit-1]
txt2idx = {}
for idx in range(len(idx2txt)):
txt2idx[idx2txt[idx]] = idx
return idx2txt, txt2idx
# From MHM codebases
# import pycparser
# import torch
def getTensor(batch, batchfirst=False):
inputs, labels = batch['x'], batch['y']
inputs, labels = torch.tensor(inputs, dtype=torch.long).cuda(), \
torch.tensor(labels, dtype=torch.long).cuda()
if batchfirst:
# inputs_pos = [[pos_i + 1 if w_i != 0 else 0 for pos_i, w_i in enumerate(inst)] for inst in inputs]
# inputs_pos = torch.tensor(inputs_pos, dtype=torch.long).cuda()
return inputs, labels
inputs = inputs.permute([1, 0])
return inputs, labels
__key_words__ = ["auto", "break", "case", "char", "const", "continue",
"default", "do", "double", "else", "enum", "extern",
"float", "for", "goto", "if", "inline", "int", "long",
"register", "restrict", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef",
"union", "unsigned", "void", "volatile", "while",
"_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex",
"_Generic", "_Imaginary", "_Noreturn", "_Static_assert",
"_Thread_local", "__func__"]
__ops__ = ["...", ">>=", "<<=", "+=", "-=", "*=", "/=", "%=", "&=", "^=", "|=",
">>", "<<", "++", "--", "->", "&&", "||", "<=", ">=", "==", "!=", ";",
"{", "<%", "}", "%>", ",", ":", "=", "(", ")", "[", "<:", "]", ":>",
".", "&", "!", "~", "-", "+", "*", "/", "%", "<", ">", "^", "|", "?"]
__macros__ = ["NULL", "_IOFBF", "_IOLBF", "BUFSIZ", "EOF", "FOPEN_MAX", "TMP_MAX", # <stdio.h> macro
"FILENAME_MAX", "L_tmpnam", "SEEK_CUR", "SEEK_END", "SEEK_SET",
"NULL", "EXIT_FAILURE", "EXIT_SUCCESS", "RAND_MAX", "MB_CUR_MAX"] # <stdlib.h> macro
__special_ids__ = ["main", # main function
"stdio", "cstdio", "stdio.h", # <stdio.h> & <cstdio>
"size_t", "FILE", "fpos_t", "stdin", "stdout", "stderr", # <stdio.h> types & streams
"remove", "rename", "tmpfile", "tmpnam", "fclose", "fflush", # <stdio.h> functions
"fopen", "freopen", "setbuf", "setvbuf", "fprintf", "fscanf",
"printf", "scanf", "snprintf", "sprintf", "sscanf", "vprintf",
"vscanf", "vsnprintf", "vsprintf", "vsscanf", "fgetc", "fgets",
"fputc", "getc", "getchar", "putc", "putchar", "puts", "ungetc",
"fread", "fwrite", "fgetpos", "fseek", "fsetpos", "ftell",
"rewind", "clearerr", "feof", "ferror", "perror", "getline"
"stdlib", "cstdlib", "stdlib.h", # <stdlib.h> & <cstdlib>
"size_t", "div_t", "ldiv_t", "lldiv_t", # <stdlib.h> types
"atof", "atoi", "atol", "atoll", "strtod", "strtof", "strtold", # <stdlib.h> functions
"strtol", "strtoll", "strtoul", "strtoull", "rand", "srand",
"aligned_alloc", "calloc", "malloc", "realloc", "free", "abort",
"atexit", "exit", "at_quick_exit", "_Exit", "getenv",
"quick_exit", "system", "bsearch", "qsort", "abs", "labs",
"llabs", "div", "ldiv", "lldiv", "mblen", "mbtowc", "wctomb",
"mbstowcs", "wcstombs",
"string", "cstring", "string.h", # <string.h> & <cstring>
"memcpy", "memmove", "memchr", "memcmp", "memset", "strcat", # <string.h> functions
"strncat", "strchr", "strrchr", "strcmp", "strncmp", "strcoll",
"strcpy", "strncpy", "strerror", "strlen", "strspn", "strcspn",
"strpbrk" ,"strstr", "strtok", "strxfrm",
"memccpy", "mempcpy", "strcat_s", "strcpy_s", "strdup", # <string.h> extension functions
"strerror_r", "strlcat", "strlcpy", "strsignal", "strtok_r",
"iostream", "istream", "ostream", "fstream", "sstream", # <iostream> family
"iomanip", "iosfwd",
"ios", "wios", "streamoff", "streampos", "wstreampos", # <iostream> types
"streamsize", "cout", "cerr", "clog", "cin",
"boolalpha", "noboolalpha", "skipws", "noskipws", "showbase", # <iostream> manipulators
"noshowbase", "showpoint", "noshowpoint", "showpos",
"noshowpos", "unitbuf", "nounitbuf", "uppercase", "nouppercase",
"left", "right", "internal", "dec", "oct", "hex", "fixed",
"scientific", "hexfloat", "defaultfloat", "width", "fill",
"precision", "endl", "ends", "flush", "ws", "showpoint",
"sin", "cos", "tan", "asin", "acos", "atan", "atan2", "sinh", # <math.h> functions
"cosh", "tanh", "exp", "sqrt", "log", "log10", "pow", "powf",
"ceil", "floor", "abs", "fabs", "cabs", "frexp", "ldexp",
"modf", "fmod", "hypot", "ldexp", "poly", "matherr"]
__parser__ = None
def tokens2seq(_tokens):
'''
Return the source code, given the token sequence.
'''
seq = ""
for t in _tokens:
if t == "<INT>":
seq += "0 "
elif t == "<FP>":
seq += "0. "
elif t == "<STR>":
seq += "\"\" "
elif t == "<CHAR>":
seq += "'\0' "
else:
while "<__SPACE__>" in t:
t.replace("<__SPACE__>", " ")
while "<__BSLASH_N__>" in t:
t.replace("<__BSLASH_N__>", "\n")
while "<__BSLASH_R__>" in t:
t.replace("<__BSLASH_R__>", "\r")
seq += t + " "
return seq
def getAST(_seq=""):
'''
Return the AST of a c/c++ file.
'''
global __parser__
if __parser__ is None:
__parser__ = pycparser.CParser()
_ast = __parser__.parse(_seq)
return _ast
def getDecl(_seq="", _syms={}):
'''
Return all declaration names in an AST.
'''
_node = getAST(_seq)
if isinstance(_node, pycparser.c_ast.Decl):
if isinstance(_node.children()[0][1], pycparser.c_ast.TypeDecl):
_syms.add(_node.name)
elif isinstance(_node.children()[0][1], pycparser.c_ast.PtrDecl):
_syms.add(_node.name)
elif isinstance(_node.children()[0][1], pycparser.c_ast.ArrayDecl):
_syms.add(_node.name)
elif isinstance(_node.children()[0][1], pycparser.c_ast.FuncDecl):
_syms.add(_node.name)
elif isinstance(_node.children()[0][1], pycparser.c_ast.Struct):
_syms.add(_node.children()[0][1].name)
if not _node.name is None:
_syms.add(_node.name)
elif isinstance(_node.children()[0][1], pycparser.c_ast.Union):
_syms.add(_node.children()[0][1].name)
if not _node.name is None:
_syms.add(_node.name)
try:
for _child in _node.children():
_syms = getDecl(_child[1], _syms)
except:
_node.show()
return _syms
def isUID(_text=""):
'''
Return if a token is a UID.
'''
_text = _text.strip()
if _text == '':
return False
if " " in _text or "\n" in _text or "\r" in _text:
return False
elif _text in __key_words__:
return False
elif _text in __ops__:
return False
elif _text in __macros__:
return False
elif _text in __special_ids__:
return False
elif _text[0].lower() in "0123456789":
return False
elif "'" in _text or '"' in _text:
return False
elif _text[0].lower() in "abcdefghijklmnopqrstuvwxyz_":
for _c in _text[1:-1]:
if _c.lower() not in "0123456789abcdefghijklmnopqrstuvwxyz_":
return False
else:
return False
return True
def getUID(_tokens=[], uids=[]):
'''
Return all UIDs and their indeces, given a token sequence.
'''
ids = {}
for i, t in enumerate(_tokens):
if isUID(t) and t in uids[0].keys():
if t in ids.keys():
ids[t].append(i)
else:
ids[t] = [i]
return ids
class CodeDataset(Dataset):
def __init__(self, examples):
self.examples = examples
def __len__(self):
return len(self.examples)
def __getitem__(self, i):
return torch.tensor(self.examples[i].input_ids),torch.tensor(self.examples[i].label)
class GraphCodeDataset(Dataset):
def __init__(self, examples, args):
self.examples = examples
self.args=args
def __len__(self):
return len(self.examples)
def __getitem__(self, item):
#calculate graph-guided masked function
attn_mask=np.zeros((self.args.code_length+self.args.data_flow_length,
self.args.code_length+self.args.data_flow_length),dtype=np.bool)
#calculate begin index of node and max length of input
node_index=sum([i>1 for i in self.examples[item].position_idx])
max_length=sum([i!=1 for i in self.examples[item].position_idx])
#sequence can attend to sequence
attn_mask[:node_index,:node_index]=True
#special tokens attend to all tokens
for idx,i in enumerate(self.examples[item].input_ids):
if i in [0,2]:
attn_mask[idx,:max_length]=True
#nodes attend to code tokens that are identified from
for idx,(a,b) in enumerate(self.examples[item].dfg_to_code):
if a<node_index and b<node_index:
attn_mask[idx+node_index,a:b]=True
attn_mask[a:b,idx+node_index]=True
#nodes attend to adjacent nodes
for idx,nodes in enumerate(self.examples[item].dfg_to_dfg):
for a in nodes:
if a+node_index<len(self.examples[item].position_idx):
attn_mask[idx+node_index,a+node_index]=True
return (torch.tensor(self.examples[item].input_ids),
torch.tensor(attn_mask),
torch.tensor(self.examples[item].position_idx),
torch.tensor(self.examples[item].label))
class CodePairDataset(Dataset):
def __init__(self, examples, args):
self.examples = examples
self.args=args
def __len__(self):
return len(self.examples)
def __getitem__(self, item):
#calculate graph-guided masked function
attn_mask_1= np.zeros((self.args.code_length+self.args.data_flow_length,
self.args.code_length+self.args.data_flow_length),dtype=np.bool)
#calculate begin index of node and max length of input
node_index=sum([i>1 for i in self.examples[item].position_idx_1])
max_length=sum([i!=1 for i in self.examples[item].position_idx_1])
#sequence can attend to sequence
attn_mask_1[:node_index,:node_index]=True
#special tokens attend to all tokens
for idx,i in enumerate(self.examples[item].input_ids_1):
if i in [0,2]:
attn_mask_1[idx,:max_length]=True
#nodes attend to code tokens that are identified from
for idx,(a,b) in enumerate(self.examples[item].dfg_to_code_1):
if a<node_index and b<node_index:
attn_mask_1[idx+node_index,a:b]=True
attn_mask_1[a:b,idx+node_index]=True
#nodes attend to adjacent nodes
for idx,nodes in enumerate(self.examples[item].dfg_to_dfg_1):
for a in nodes:
if a+node_index<len(self.examples[item].position_idx_1):
attn_mask_1[idx+node_index,a+node_index]=True
#calculate graph-guided masked function
attn_mask_2= np.zeros((self.args.code_length+self.args.data_flow_length,
self.args.code_length+self.args.data_flow_length),dtype=np.bool)
#calculate begin index of node and max length of input
node_index=sum([i>1 for i in self.examples[item].position_idx_2])
max_length=sum([i!=1 for i in self.examples[item].position_idx_2])
#sequence can attend to sequence
attn_mask_2[:node_index,:node_index]=True
#special tokens attend to all tokens
for idx,i in enumerate(self.examples[item].input_ids_2):
if i in [0,2]:
attn_mask_2[idx,:max_length]=True
#nodes attend to code tokens that are identified from
for idx,(a,b) in enumerate(self.examples[item].dfg_to_code_2):
if a<node_index and b<node_index:
attn_mask_2[idx+node_index,a:b]=True
attn_mask_2[a:b,idx+node_index]=True
#nodes attend to adjacent nodes
for idx,nodes in enumerate(self.examples[item].dfg_to_dfg_2):
for a in nodes:
if a+node_index<len(self.examples[item].position_idx_2):
attn_mask_2[idx+node_index,a+node_index]=True
return (torch.tensor(self.examples[item].input_ids_1),
torch.tensor(self.examples[item].position_idx_1),
torch.tensor(attn_mask_1),
torch.tensor(self.examples[item].input_ids_2),
torch.tensor(self.examples[item].position_idx_2),
torch.tensor(attn_mask_2),
torch.tensor(self.examples[item].label))
def set_seed(seed=42):
random.seed(seed)
os.environ['PYHTONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
class Recorder():
def __init__(self, file_path: str) -> None:
self.file_path = file_path
self.f = open(file_path, 'w')
self.writer = csv.writer(self.f)
self.writer.writerow(["Index",
"Original Code",
"Program Length",
"Adversarial Code",
"True Label",
"Original Prediction",
"Adv Prediction",
"Is Success",
"Extracted Names",
"Importance Score",
"No. Changed Names",
"No. Changed Tokens",
"Replaced Names",
"Attack Type",
"Query Times",
"Time Cost"])
def write(self, index, code, prog_length, adv_code, true_label, orig_label, temp_label, is_success, variable_names, score_info, nb_changed_var, nb_changed_pos, replace_info, attack_type, query_times, time_cost):
self.writer.writerow([index,
code,
prog_length,
adv_code,
true_label,
orig_label,
temp_label,
is_success,
",".join(variable_names),
score_info,
nb_changed_var,
nb_changed_pos,
replace_info,
attack_type,
query_times,
time_cost])
def writemhm(self, index, code, prog_length, adv_code, true_label, orig_label, temp_label, is_success, variable_names, score_info, nb_changed_var, nb_changed_pos, replace_info, attack_type, query_times, time_cost):
self.writer.writerow([index,
code,
prog_length,
adv_code,
true_label,
orig_label,
temp_label,
is_success,
variable_names,
score_info,
nb_changed_var,
nb_changed_pos,
replace_info,
attack_type,
query_times,
time_cost])