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
|
// ufuzz.js
// derived from https://github.com/qfox/uglyfuzzer by Peter van der Zee
"use strict";
// workaround for tty output truncation upon process.exit()
[process.stdout, process.stderr].forEach(function(stream){
if (stream._handle && stream._handle.setBlocking)
stream._handle.setBlocking(true);
});
var vm = require("vm");
var minify = require("..").minify;
function run_code(code) {
var stdout = "";
var original_write = process.stdout.write;
process.stdout.write = function(chunk) {
stdout += chunk;
};
try {
new vm.Script(code).runInNewContext({ console: console }, { timeout: 5000 });
return stdout;
} catch (ex) {
return ex;
} finally {
process.stdout.write = original_write;
}
}
function rng(max) {
return Math.floor(max * Math.random());
}
function createFunctionDecls(n, recurmax) {
if (--recurmax < 0) { return ';'; }
var s = '';
while (--n > 0) {
s += createFunctionDecl(recurmax) + '\n';
}
return s;
}
var funcs = 0;
function createFunctionDecl(recurmax) {
if (--recurmax < 0) { return ';'; }
var func = funcs++;
return 'function f' + func + '(){' + createStatements(3, recurmax) + '}\nf' + func + '();';
}
function createStatements(n, recurmax) {
if (--recurmax < 0) { return ';'; }
var s = '';
while (--n > 0) {
s += createStatement(recurmax);
}
return s;
}
var loops = 0;
function createStatement(recurmax) {
var loop = ++loops;
if (--recurmax < 0) { return ';'; }
switch (rng(7)) {
case 0:
return '{' + createStatement(recurmax) + '}';
case 1:
return 'if (' + createExpression(recurmax) + ')' + createStatement(recurmax);
case 2:
return '{var brake' + loop + ' = 5; do {' + createStatement(recurmax) + '} while ((' + createExpression(recurmax) + ') && --brake' + loop + ' > 0);}';
case 3:
return '{var brake' + loop + ' = 5; while ((' + createExpression(recurmax) + ') && --brake' + loop + ' > 0)' + createStatement(recurmax) + '}';
case 4:
return 'for (var brake' + loop + ' = 5; (' + createExpression(recurmax) + ') && brake' + loop + ' > 0; --brake' + loop + ')' + createStatement(recurmax);
case 5:
return ';';
case 6:
return createExpression() + ';';
}
}
function createExpression(recurmax) {
if (--recurmax < 0) { return '0'; }
switch (rng(8)) {
case 0:
return '(' + createUnaryOp() + 'a)';
case 1:
return '(a' + (Math.random() > 0.5 ? '++' : '--') + ')';
case 2:
return '(b ' + createAssignment() + ' a)';
case 3:
return '(' + Math.random() + ' > 0.5 ? a : b)';
case 4:
return createExpression(recurmax) + createBinaryOp() + createExpression(recurmax);
case 5:
return createValue();
case 6:
return '(' + createExpression(recurmax) + ')';
case 7:
return createExpression(recurmax) + '?(' + createExpression(recurmax) + '):(' + createExpression(recurmax) + ')';
}
}
function createValue() {
var values = [
'true',
'false',
'22',
'0',
'(-1)',
'NaN',
'undefined',
'null',
'"foo"',
'"bar"' ];
return values[rng(values.length)];
}
function createBinaryOp() {
switch (rng(6)) {
case 0:
return '+';
case 1:
return '-';
case 2:
return ',';
case 3:
return '&&';
case 4:
return '||';
case 5:
return '^';
}
}
function createAssignment() {
switch (rng(4)) {
case 0:
return '=';
case 1:
return '-=';
case 2:
return '^=';
case 3:
return '+=';
}
}
function createUnaryOp() {
switch (rng(4)) {
case 0:
return '--';
case 1:
return '++';
case 2:
return '~';
case 3:
return '!';
}
}
function log() {
console.log("//=============================================================");
console.log("// original code");
console.log("//");
console.log(original_code);
console.log();
console.log();
console.log("//-------------------------------------------------------------");
console.log("// original code (beautify'd)");
console.log("//");
console.log(beautify_code);
console.log();
console.log();
console.log("//-------------------------------------------------------------");
console.log("// uglified code");
console.log("//");
console.log(uglify_code);
console.log();
console.log();
console.log("original result:");
console.log(original_result);
console.log("beautified result:");
console.log(beautify_result);
console.log("uglified result:");
console.log(uglify_result);
}
var num_iterations = +process.argv[2] || 1/0;
var verbose = !!process.argv[3];
for (var round = 0; round < num_iterations; round++) {
process.stdout.write(round + " of " + num_iterations + "\r");
var original_code = [
"var a = 100, b = 10;",
createFunctionDecls(rng(3) + 1, 10),
"console.log(a, b);"
].join("\n");
var beautify_code = minify(original_code, {
fromString: true,
mangle: false,
compress: false,
output: {
beautify: true,
bracketize: true,
},
}).code;
var uglify_code = minify(beautify_code, {
fromString: true,
mangle: false,
compress: {
passes: 3,
},
output: {
beautify: true,
bracketize: true,
},
}).code;
var original_result = run_code(original_code);
var beautify_result = run_code(beautify_code);
var uglify_result = run_code(uglify_code);
var ok = original_result == beautify_result && original_result == uglify_result;
if (verbose || !ok) log();
if (!ok) process.exit(1);
}
|