-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.1.js
211 lines (167 loc) · 5.14 KB
/
generator.1.js
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
var escodegen = require('escodegen');
var random = require('./random');
var estraverse = require('./estraverse/estraverse');
var SimpleMutator = require('./Mutate/SimpleMutate.js').simpleMutator;
var ExtendMutator = require('./Mutate/ExtendMutate.js').extendMutator;
var Traverse = require("./Traverse.js").traverse;
var Copy = require("./utils").copy;
var Dump = require("./utils").dump;
var InvalidMutate = require("./utils").invalidMutate;
var StatementMutate = require("./Mutate/StatementMutate.js")
var ExpressionMutate = require("./Mutate/ExpressionMutate.js")
var Program = require('./nodes/Program');
cache$ = require('./combinators');
oneOf = cache$.oneOf;
maybe = cache$.maybe;
let statements = [
//"BlockStatement", // block is not allowed be mutated, because in some cases block in an necessary statement
"BreakStatement",
"ContinueStatement",
"DebuggerStatement",
"DoWhileStatement",
"EmptyStatement",
"ExpressionStatement",
"ForInStatement",
"ForStatement",
//"FunctionDeclaration",
"IfStatement",
"LabeledStatement",
"ReturnStatement",
"SwitchStatement",
"ThrowStatement",
"TryStatement",
//"VariableDeclaration",
"WhileStatement",
"WithStatement",
"ClassDeclaration"
]
/*
Statement could mutate to another Statement
*/
function StatementNodeMutate(node, mutator, control_param) {
if (node == null)
return;
let isArray = Array.isArray(node);
let shadow = isArray ? [] : {};
if (control_param["mutateValue"] <= 0)
return Copy(node);
if (node["mutable"] == false)
return Copy(node);
for (var key in node) {
//console.log(key + ' ' + node[key]);
if (key == "loc" || key == "insert" || key == "mutable")
continue;
if ((typeof node[key] == "object")) {
let value = StatementNodeMutate(node[key], mutator, control_param);
if (value) shadow[key] = value;
} else {
//console.log(key + ' ' + node[key]);
shadow[key] = node[key];
}
}
if (["DoWhileStatement", "ForInStatement", "ForStatement", "IfStatement", "SwitchStatement", "WhileStatement", "WithStement", "ContinueStatement", "BreakStatement", "EmptyStatement", "ExpressionStatement", "FunctionDeclaration", "VariableDeclaration"].indexOf(node['type']) > -1) {
if (random.randomBool()) {
control_param["mutateValue"]--;
return mutator(node);
}
}
return shadow;
}
/*
Do the Simple mutate, only mutate value and operator in place
control_param used to control the mutate rant, the account of mutable value
In-place change
*/
function SimpleNodeMutate(node, control_param) {
if (node == null)
return;
var shadow = Copy(node);
//let isArray = Array.isArray(node);
//let shadow = isArray ? [] : {};
do{
if (node["mutable"] == false)
break;
if (control_param["mutateValue"] <= 0)
break;
// mutate the operator and value
if (["UpdateExpression", "LogicalExpression", "BinaryExpression", "UnaryExpression", "Literal", "AssignmentExpression"].indexOf(node['type']) > -1) {
if (random.randomBool(0.3)) {
control_param["mutateValue"]--;
shadow = SimpleMutator(node);
break;
}
}
// extend the simple statement
if (statements.indexOf(node['type']) > -1) {
if (random.randomBool(0.3)) {
control_param["mutateValue"]--;
shadow = ExtendMutator(node);
break;
}
}
for (var key in node) {
if (key == "loc" || key == "insert")
continue;
//console.log(key + ' ' + node[key]);
if ((typeof node[key] == "object")){
let value = SimpleNodeMutate(node[key], control_param);
if (value) shadow[key] = value;
}
}
}
while (false);
return shadow;
}
/*
CallExpression
MemberExpression
*/
function ExpressionNodeMutate(node, mutator, control_param) {
if (node == null)
return;
let isArray = Array.isArray(node);
let shadow = isArray ? [] : {};
if (control_param["mutateValue"] <= 0)
return;
if (["ArrayExpression", "CallExpression", "ConditionalExpression", "FunctionExpression", "Identifier", "LogicalExpression", "MemberExpression", "NewExpression", "ObjectExpression", "SequenceExpression", "ThisExpression", "UpdateExpression", "BinaryExpression", "UnaryExpression", "AssignmentExpression"].indexOf(node['type']) > -1) {
if (random.randomBool()) {
control_param["mutateValue"]--;
return mutator(node);
} else {
return Copy(node);
}
}
for (var key in node) {
if (key == "loc" || key == "insert")
continue;
//console.log(key + ' ' + node[key]);
if ((typeof node[key] == "object")) {
let value = ExpressionNodeMutate(node[key], mutator, control_param)
if (value) shadow[key] = value;
} else {
shadow[key] = node[key];
}
}
return shadow;
}
/*
Mutation fuzz
AST -> Shadow -> mutated AST -> js code
mutated AST
/ | \
1.statement mutate 2. expression mutate 3. value mutate
1. Traverse the AST to mark node that should be mutated, and record the object of each scope
the information stored in another tree named shadow
2. Traverse the Shadow ,mutate the marked node to
*/
/*
PathManager
*/
// read file from seed
var rf = require("fs");
var page_file = 'page.js';
var ast = Program(10,null);
var page = escodegen.generate(ast);
//console.log(page);
//}
rf.writeFileSync(page_file, page)