-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoremove.js
More file actions
210 lines (171 loc) · 6.43 KB
/
Copy pathautoremove.js
File metadata and controls
210 lines (171 loc) · 6.43 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
const md5 = require("md5");
const fs = require("fs");
const path = require("path");
const readlines = require("n-readlines");
const {ArgumentParser} = require('argparse');
const child_process = require('child_process');
const utils = require(path.join(process.env.MFUZZERLIB,"utils"));
const fixname = require(path.join(process.env.MFUZZERLIB,"fixname"));
class AutoRemove{
constructor(seedname, timeout, enginename, maxfixtry=50){
this.seedname = seedname;
if(enginename!="node" && !path.isAbsolute(enginename)){
enginename = path.resolve(enginename);
}
this.timeout = timeout;
this.enginename = enginename;
this.maxfixtry = maxfixtry;
this.fixed = false;
/*
console.log("================================================================================");
console.log("seedname:", this.seedname);
console.log("timeout:", this.timeout);
console.log("enginenamet:", this.enginename);
console.log("maxfixtry:", this.maxfixtry);
console.log("================================================================================");
*/
this.errorfilter = /[E|e][R|r][R|r][O|o][R|r]/
this.syntaxjsname = utils.abspath("jsc")
this.syntaxfixswitch = false
this.maxfixtry = maxfixtry
this.errormd5 = null
this.fix_many_times()
}
errorhash(text){
return md5(text);
}
fix_many_times(){
while(this.maxfixtry--){
var errorinfo = this.errorcheck();
if(errorinfo == null){
this.fixed = true;
break;
}
this.fix(errorinfo);
}
}
error_line_match(errorinfo){
var abs_seedname = path.resolve(this.seedname);
var patterns = [abs_seedname + ":(\\d+)",
this.seedname + ":(\\d+)",
"Error: Line (\\d+)",
"line: (\\d+)"
]
for(var index in patterns){
var pattern = new RegExp(patterns[index]);
var error_name = pattern.exec(errorinfo);
if(error_name!=null){
return error_name[1];
}
}
return null;
}
error_name_match(errorinfo){
var patterns = [
/SyntaxError: Illegal\s+(\w+)\-expression/,
/SyntaxError: Identifier\s+'(\w+)'\s+/,
/at\s+new\s+(\w+)\s+/,
/Error:\s+\"*\'*([\$|\w]+)\"*\'*\s+is not/,
/Error:\s+Function\s+\"*\'*(\w+)\"*\'*/,
/Error:\s+Cannot\s+read\s+property\s+\"*\'*(\w+)\"*\'*/,
/RangeError:\s+(\w+)\(\)\s+argument/,
/RangeError:\s+(\w+)\(\) radix argument must be between/,
/RangeError: Invalid string length\s+at\s+String\.(\w+)/,
/RangeError: Invalid array buffer length\s+at new\s+(\w+)\s+/,
/RangeError: Maximum call stack size exceeded\s+at\s+(\w+)\s+/,
/RangeError: Invalid array length\s+at\s+Array\.(\w+)/,
/TypeError: \w+\.(\w+) is not a/,
/TypeError: Cannot read property '(\w+)' of undefined/,
/TypeError: Cannot set property\s+(\w)\s+of/,
/TypeError: Cannot redefine property:\s+(\w+)/,
/TypeError:\s+(\w)\s+is not a function/,
/TypeError: Cannot set property '(\w+)' of undefined/,
/TypeError: Method Intl\.(\w+)/
]
for(var index in patterns){
var pattern = new RegExp(patterns[index]);
var error_name = pattern.exec(errorinfo);
if(error_name!=null){
return error_name[1];
}
}
return null;
}
swapengine(){
var tmp = this.enginename;
this.enginename = this.syntaxjsname;
this.syntaxjsname = tmp;
}
maybe_try_syntaxfix(erro_line){
this.swapengine()
this.error_line_fix((Number(erro_line)-1).toString())
this.swapengine()
}
error_line_fix(erro_line){
var edges = [];
var edges = [];
var next;
var count = 0;
var liner = new readlines(this.seedname);
while (next = liner.next()) {
if(count != erro_line-1){
edges.push(next.toString());
}
count = count + 1;
}
fs.writeFileSync(this.seedname, edges.join(""));
}
errorcheck(){
try{
var ret = child_process.spawnSync(this.enginename, [this.seedname], {encoding: "utf-8", timeout: this.timeout});
var output = ret.stdout + ret.stderr;
if(!this.errorfilter.test(output)){
return null;
}
var curerrmd5 = this.errorhash(output);
if(curerrmd5 == this.errormd5){
this.syntaxfixswitch = true;
}
this.errormd5 = curerrmd5;
var matchline = this.error_line_match(output);
if(matchline!=null){
return ["line", matchline];
}
var matchname = this.error_name_match(output);
if(matchname!=null){
return ["name", matchname];
}
return null;
}catch(err){
return err;
}
}
fix(errinfo){
if(errinfo[0] == "line"){
if(this.syntaxfixswitch){
this.maybe_try_syntaxfix(errinfo[1]);
this.syntaxfixswitch = false;
}else{
this.error_line_fix(errinfo[1]);
}
}else{
fixname.fixname(this.seedname, errinfo[1]);
}
}
}
/*
const parser = new ArgumentParser();
parser.add_argument("-s", "--seedname", { default: "test1.js" });
parser.add_argument("-j", "--enginename", { default: "node" });
parser.add_argument("-x", "--maxfixtry",{ default: "50" });
parser.add_argument("-t", "--timeout",{ default: "5000" });
args = parser.parse_args();
try{
new AutoRemove(seedname=args.seedname, enginename=args.enginename, maxfixtry=args.maxfixtry, timeout=args.timeout);
}catch(err){
console.log("@@@@@@@@@@@@@@@@@@@@ AutoRemove Error @@@@@@@@@@@@@@@@@@@@@@@");
console.log(err);
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}
*/
exports.AutoRemove = AutoRemove;