From 49781d0584767e03e0ad47576e666225fa0cacb8 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sun, 2 Apr 2017 03:33:04 +0800 Subject: [PATCH] speed up fuzzer code generation - only output one top-level function or statement block - reduce `rng()` granularity from 2^32 to 65536 --- test/ufuzz.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/test/ufuzz.js b/test/ufuzz.js index bd110c3e..c5d5eea0 100644 --- a/test/ufuzz.js +++ b/test/ufuzz.js @@ -16,7 +16,7 @@ var UglifyJS = require(".."); var randomBytes = require("crypto").randomBytes; var sandbox = require("./sandbox"); -var MAX_GENERATED_TOPLEVELS_PER_RUN = 3; +var MAX_GENERATED_TOPLEVELS_PER_RUN = 1; var MAX_GENERATION_RECURSION_DEPTH = 12; var INTERVAL_COUNT = 100; @@ -288,22 +288,13 @@ var loops = 0; var funcs = 0; function rng(max) { - var r = parseInt(randomBytes(4).toString("hex"), 16) / 0xFFFFFFFF; + var r = randomBytes(2).readUInt16LE(0) / 0xFFFF; return Math.floor(max * r); } -function createTopLevelCodes(n) { - var s = ''; - while (n-- > 0) { - s += createTopLevelCode() + '\n\n//$$$$$$$$$$$$$$\n\n'; - } - return s; -} - function createTopLevelCode() { - var r = rng(3); - if (r > 0) return createFunctions(rng(MAX_GENERATED_TOPLEVELS_PER_RUN) + 1, MAX_GENERATION_RECURSION_DEPTH, IN_GLOBAL, ANY_TYPE, CANNOT_THROW, 0); - return createStatements(3, MAX_GENERATION_RECURSION_DEPTH, CANNOT_THROW, CANNOT_BREAK, CANNOT_CONTINUE, CANNOT_RETURN, 0); + if (rng(2) === 0) return createStatements(3, MAX_GENERATION_RECURSION_DEPTH, CANNOT_THROW, CANNOT_BREAK, CANNOT_CONTINUE, CANNOT_RETURN, 0); + return createFunctions(rng(MAX_GENERATED_TOPLEVELS_PER_RUN) + 1, MAX_GENERATION_RECURSION_DEPTH, IN_GLOBAL, ANY_TYPE, CANNOT_THROW, 0); } function createFunctions(n, recurmax, inGlobal, noDecl, canThrow, stmtDepth) { @@ -323,7 +314,7 @@ function createFunction(recurmax, inGlobal, noDecl, canThrow, stmtDepth) { var name = (inGlobal || rng(5) > 0) ? 'f' + func : createVarName(MANDATORY, noDecl); if (name === 'a' || name === 'b' || name === 'c') name = 'f' + func; // quick hack to prevent assignment to func names of being called var s = ''; - if (rng(5) === 1) { + if (rng(5) === 0) { // functions with functions. lower the recursion to prevent a mess. s = 'function ' + name + '(' + createVarName(MANDATORY) + '){' + createFunctions(rng(5) + 1, Math.ceil(recurmax * 0.7), NOT_GLOBAL, ANY_TYPE, canThrow, stmtDepth) + '}\n'; } else { @@ -748,7 +739,7 @@ for (var round = 1; round <= num_iterations; round++) { original_code = [ "var a = 100, b = 10, c = 0;", - createTopLevelCodes(rng(MAX_GENERATED_TOPLEVELS_PER_RUN) + 1), + createTopLevelCode(), "console.log(null, a, b, c);" // preceding `null` makes for a cleaner output (empty string still shows up etc) ].join("\n");