support limited ufuzz testing for import (#4707)

This commit is contained in:
Alex Lam S.L
2021-02-28 18:27:41 +00:00
committed by GitHub
parent c549ee89b9
commit 81254f67e4
4 changed files with 45 additions and 11 deletions

View File

@@ -33,10 +33,11 @@ jobs:
- name: Install GNU Core Utilities
if: ${{ startsWith(matrix.os, 'macos') }}
env:
HOMEBREW_NO_BOTTLE_SOURCE_FALLBACK: 1
HOMEBREW_NO_INSTALL_CLEANUP: 1
shell: bash
run: |
brew install coreutils
while !(brew install coreutils); do echo "'brew install' failed - retrying..."; done
- name: Perform fuzzing
shell: bash
run: |

View File

@@ -732,7 +732,7 @@ function run_code(code, toplevel, result_cache, timeout) {
if (!value) {
var start = Date.now();
result_cache[key] = value = {
result: sandbox.run_code(sandbox.strip_exports(code), toplevel, timeout),
result: sandbox.run_code(sandbox.patch_module_statements(code), toplevel, timeout),
elapsed: Date.now() - start,
};
}

View File

@@ -49,13 +49,27 @@ exports.same_stdout = semver.satisfies(process.version, "0.12") ? function(expec
} : function(expected, actual) {
return typeof expected == typeof actual && strip_func_ids(expected) == strip_func_ids(actual);
};
exports.strip_exports = function(code) {
var count = 0;
return code.replace(/\bexport(?:\s*\{[^}]*}\s*?(?:$|\n|;)|\s+default\b(?:\s*(\(|\{|class\s*\{|class\s+(?=extends\b)|(?:async\s+)?function\s*(?:\*\s*)?\())?|\b)/g, function(match, header) {
exports.patch_module_statements = function(code) {
var count = 0, imports = [];
code = code.replace(/\bexport(?:\s*\{[^}]*}\s*?(?:$|\n|;)|\s+default\b(?:\s*(\(|\{|class\s*\{|class\s+(?=extends\b)|(?:async\s+)?function\s*(?:\*\s*)?\())?|\b)/g, function(match, header) {
if (!header) return "";
if (header.length == 1) return "0, " + header;
return header.slice(0, -1) + " _" + ++count + header.slice(-1);
}).replace(/\bimport\b(?:\s*([^('"]+)\bfrom\b)?\s*(['"]).*?\2(?:$|\n|;)/g, function(match, symbols) {
if (symbols) {
if (!/^[{*]/.test(symbols)) symbols = "default:" + symbols;
symbols = symbols.replace(/[{}]/g, "").trim().replace(/\s*,\s*/g, ",");
symbols = symbols.replace(/\*/, '"*"').replace(/\bas\s+(?!$|,|as\s)/g, ":");
imports.push([
"var {",
symbols,
"} = new Proxy(Object.create(null), { get(_, value) { return { value }; } });",
].join(""));
}
return "";
});
imports.push("");
return imports.join("\n") + code;
};
function is_error(result) {

View File

@@ -382,7 +382,7 @@ function strictMode() {
}
function appendExport(stmtDepth, allowDefault) {
if (stmtDepth == 1 && rng(20) == 0) {
if (SUPPORT.destructuring && stmtDepth == 1 && rng(20) == 0) {
if (allowDefault && !export_default && rng(5) == 0) {
export_default = true;
return "export default ";
@@ -1009,7 +1009,7 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
case STMT_SEMI:
return use_strict && rng(20) === 0 ? '"use strict";' : ";";
case STMT_EXPR:
if (stmtDepth == 1 && !export_default && rng(20) == 0) {
if (SUPPORT.destructuring && stmtDepth == 1 && !export_default && rng(20) == 0) {
export_default = true;
return "export default " + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ";";
}
@@ -1019,7 +1019,26 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
// note: default does not _need_ to be last
return "switch (" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") { " + createSwitchParts(recurmax, 4, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "}";
case STMT_VAR:
if (SUPPORT.destructuring && rng(20) == 0) {
if (SUPPORT.destructuring && stmtDepth == 1 && rng(5) == 0) {
unique_vars.push("c");
var s = rng(2) ? " " + createVarName(MANDATORY) : "";
if (rng(10)) {
if (s) s += ",";
if (rng(2)) {
s += " * as " + createVarName(MANDATORY);
} else {
var names = [];
for (var i = rng(4); --i >= 0;) {
var name = createVarName(MANDATORY);
names.push(rng(2) ? getDotKey() + " as " + name : name);
}
s += " { " + names.join(", ") + " }";
}
}
unique_vars.pop();
if (s) s += " from";
return "import" + s + ' "path/to/module.js";';
} else if (SUPPORT.destructuring && rng(20) == 0) {
var pairs = createAssignmentPairs(recurmax, stmtDepth, canThrow);
return appendExport(stmtDepth) + "var " + pairs.names.map(function(name, index) {
return index in pairs.values ? name + " = " + pairs.values[index] : name;
@@ -1966,7 +1985,7 @@ if (require.main !== module) {
}
function run_code(code, toplevel) {
return sandbox.run_code(sandbox.strip_exports(code), toplevel);
return sandbox.run_code(sandbox.patch_module_statements(code), toplevel);
}
function writeln(stream, msg) {
@@ -2384,8 +2403,8 @@ for (var round = 1; round <= num_iterations; round++) {
if (!ok && /\bclass\b/.test(original_code)) {
var original_strict = run_code('"use strict";' + original_code, toplevel);
var uglify_strict = run_code('"use strict";' + uglify_code, toplevel);
if (typeof original_strict != "string" && /strict/.test(original_strict.message)) {
ok = typeof uglify_strict != "string" && /strict/.test(uglify_strict.message);
if (typeof original_strict != "string") {
ok = typeof uglify_strict != "string";
} else {
ok = sandbox.same_stdout(original_strict, uglify_strict);
}