|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +//////////////////////////////////////////////////////////////////////////////// |
| 16 | + |
| 17 | +const { FuzzedDataProvider } = require("@jazzer.js/core"); |
| 18 | +const { rollup } = require("rollup"); |
| 19 | + |
| 20 | +const FORMATS = ["amd", "cjs", "es", "iife", "system", "umd"]; |
| 21 | +const GENERATED_CODE = ["es5", "es2015"]; |
| 22 | + |
| 23 | +function virtualPlugin(entryCode) { |
| 24 | + return { |
| 25 | + name: "virtual-entry", |
| 26 | + resolveId(source) { |
| 27 | + if (source === "entry.js") { |
| 28 | + return source; |
| 29 | + } |
| 30 | + // Mark every other import as external so we don't touch the real |
| 31 | + // filesystem while still exercising import-resolution code paths. |
| 32 | + return { id: source, external: true }; |
| 33 | + }, |
| 34 | + load(id) { |
| 35 | + if (id === "entry.js") { |
| 36 | + return entryCode; |
| 37 | + } |
| 38 | + return null; |
| 39 | + }, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +module.exports.fuzz = async function (data) { |
| 44 | + const provider = new FuzzedDataProvider(data); |
| 45 | + |
| 46 | + const inputOptions = { |
| 47 | + input: "entry.js", |
| 48 | + plugins: [virtualPlugin(provider.consumeString(provider.consumeIntegralInRange(0, 4096)))], |
| 49 | + onLog: () => {}, |
| 50 | + treeshake: provider.consumeBoolean(), |
| 51 | + }; |
| 52 | + |
| 53 | + const outputOptions = { |
| 54 | + format: FORMATS[provider.consumeIntegralInRange(0, FORMATS.length - 1)], |
| 55 | + name: provider.consumeString(provider.consumeIntegralInRange(0, 32)), |
| 56 | + generatedCode: |
| 57 | + GENERATED_CODE[provider.consumeIntegralInRange(0, GENERATED_CODE.length - 1)], |
| 58 | + compact: provider.consumeBoolean(), |
| 59 | + sourcemap: provider.consumeBoolean(), |
| 60 | + strict: provider.consumeBoolean(), |
| 61 | + esModule: provider.consumeBoolean(), |
| 62 | + }; |
| 63 | + |
| 64 | + let bundle; |
| 65 | + try { |
| 66 | + bundle = await rollup(inputOptions); |
| 67 | + await bundle.generate(outputOptions); |
| 68 | + } catch (error) { |
| 69 | + if (!isExpectedBundleError(error)) { |
| 70 | + throw error; |
| 71 | + } |
| 72 | + } finally { |
| 73 | + if (bundle) { |
| 74 | + try { |
| 75 | + await bundle.close(); |
| 76 | + } catch (_) { |
| 77 | + // ignore close errors |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +function isExpectedBundleError(error) { |
| 84 | + if (!error) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + if (typeof error.code === "string" && EXPECTED_CODES.has(error.code)) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + const message = |
| 91 | + typeof error.message === "string" ? error.message.toLowerCase() : ""; |
| 92 | + return EXPECTED_MESSAGES.some((m) => message.includes(m)); |
| 93 | +} |
| 94 | + |
| 95 | +// Rollup raises these structured error codes for malformed or unsupported |
| 96 | +// input; they are expected when fuzzing arbitrary source code and option |
| 97 | +// combinations. |
| 98 | +const EXPECTED_CODES = new Set([ |
| 99 | + "PARSE_ERROR", |
| 100 | + "UNRESOLVED_IMPORT", |
| 101 | + "MISSING_EXPORT", |
| 102 | + "INVALID_OPTION", |
| 103 | + "INVALID_EXPORT_OPTION", |
| 104 | + "VALIDATION_ERROR", |
| 105 | + "MISSING_NAME_OPTION_FOR_IIFE_EXPORT", |
| 106 | + "MISSING_NAME_OPTION_FOR_UMD_EXPORT", |
| 107 | + "MIXED_EXPORTS", |
| 108 | + "AMBIGUOUS_EXTERNAL_NAMESPACES", |
| 109 | + "INVALID_EXTERNAL_ID", |
| 110 | + "MODULE_LEVEL_DIRECTIVE", |
| 111 | + "INVALID_PLUGIN_HOOK", |
| 112 | + "ILLEGAL_REASSIGNMENT", |
| 113 | + "ILLEGAL_IDENTIFIER_AS_NAME", |
| 114 | + "BAD_LOADER", |
| 115 | + "ASSET_NOT_FINALISED", |
| 116 | + "CHUNK_NOT_GENERATED", |
| 117 | + "FILE_NAME_CONFLICT", |
| 118 | + "INVALID_CONFIG_MODULE_FORMAT", |
| 119 | + "INPUT_HOOK_IN_OUTPUT_PLUGIN", |
| 120 | + "PLUGIN_ERROR", |
| 121 | +]); |
| 122 | + |
| 123 | +const EXPECTED_MESSAGES = [ |
| 124 | + "parse error", |
| 125 | + "unexpected", |
| 126 | + "unterminated", |
| 127 | + "must be supplied", |
| 128 | + "invalid value", |
| 129 | + "is not exported", |
| 130 | + "could not resolve", |
| 131 | + "is not a valid", |
| 132 | + "exports is not specified", |
| 133 | + "you must supply", |
| 134 | +]; |
0 commit comments