|
| 1 | +/* |
| 2 | + * Copyright 2026 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const { spawnSync } = require("child_process"); |
| 18 | +const fs = require("fs"); |
| 19 | +const path = require("path"); |
| 20 | + |
| 21 | +const benchmarkDirectory = __dirname; |
| 22 | +const workDirectory = path.join(benchmarkDirectory, "work", "anomalies"); |
| 23 | +const engineTarget = path.join( |
| 24 | + benchmarkDirectory, |
| 25 | + "..", |
| 26 | + "..", |
| 27 | + "tests", |
| 28 | + "engine", |
| 29 | + "fuzz.js", |
| 30 | +); |
| 31 | +const asyncTarget = path.join(benchmarkDirectory, "anomaly_fuzz.js"); |
| 32 | + |
| 33 | +function removeIfExists(targetPath) { |
| 34 | + fs.rmSync(targetPath, { force: true, recursive: true }); |
| 35 | +} |
| 36 | + |
| 37 | +function ensureDirectory(targetPath) { |
| 38 | + fs.mkdirSync(targetPath, { recursive: true }); |
| 39 | +} |
| 40 | + |
| 41 | +function runCommand(label, args, cwd, outputDirectory, expectedStatus = 0) { |
| 42 | + console.log(`\n[anomaly] ${label}`); |
| 43 | + console.log(`[anomaly] command: npx ${args.join(" ")}`); |
| 44 | + ensureDirectory(outputDirectory); |
| 45 | + const sanitizedLabel = label.replace(/[^a-z0-9]+/gi, "-").toLowerCase(); |
| 46 | + const stdoutPath = path.join(outputDirectory, `${sanitizedLabel}.stdout.log`); |
| 47 | + const stderrPath = path.join(outputDirectory, `${sanitizedLabel}.stderr.log`); |
| 48 | + const stdoutFd = fs.openSync(stdoutPath, "w"); |
| 49 | + const stderrFd = fs.openSync(stderrPath, "w"); |
| 50 | + const startedAt = Date.now(); |
| 51 | + const proc = spawnSync("npx", args, { |
| 52 | + cwd, |
| 53 | + env: { ...process.env }, |
| 54 | + shell: true, |
| 55 | + stdio: ["ignore", stdoutFd, stderrFd], |
| 56 | + windowsHide: true, |
| 57 | + }); |
| 58 | + const elapsedMs = Date.now() - startedAt; |
| 59 | + fs.closeSync(stdoutFd); |
| 60 | + fs.closeSync(stderrFd); |
| 61 | + |
| 62 | + if (proc.status !== expectedStatus) { |
| 63 | + throw new Error( |
| 64 | + `${label} failed with exit code ${proc.status}\nSTDOUT (${stdoutPath}):\n${fs.readFileSync(stdoutPath, "utf8")}\nSTDERR (${stderrPath}):\n${fs.readFileSync(stderrPath, "utf8")}`, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + elapsedMs, |
| 70 | + stderrPath, |
| 71 | + stdoutPath, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +function parseExecsPerSecond(stderrPath) { |
| 76 | + const stderr = fs.readFileSync(stderrPath, "utf8"); |
| 77 | + const match = stderr.match(/speed:\s+([0-9.]+) exec\/s/); |
| 78 | + if (!match) { |
| 79 | + throw new Error(`No LibAFL done line found in ${stderrPath}`); |
| 80 | + } |
| 81 | + return Number.parseFloat(match[1]); |
| 82 | +} |
| 83 | + |
| 84 | +function runGuidedNumericSmoke() { |
| 85 | + const outputDirectory = path.join(workDirectory, "guided-numeric"); |
| 86 | + const corpusDirectory = path.join(outputDirectory, "corpus"); |
| 87 | + removeIfExists(outputDirectory); |
| 88 | + ensureDirectory(corpusDirectory); |
| 89 | + fs.writeFileSync(path.join(corpusDirectory, "seed"), Buffer.alloc(4)); |
| 90 | + |
| 91 | + const result = runCommand( |
| 92 | + "guided numeric solve", |
| 93 | + [ |
| 94 | + "jazzer", |
| 95 | + engineTarget, |
| 96 | + "-f", |
| 97 | + "guided_numeric", |
| 98 | + "--engine=afl", |
| 99 | + "--sync", |
| 100 | + "--disable_bug_detectors=.*", |
| 101 | + "--runs=4000", |
| 102 | + "--seed=1337", |
| 103 | + "--maxLen=16", |
| 104 | + `--artifactPrefix=${outputDirectory}${path.sep}`, |
| 105 | + corpusDirectory, |
| 106 | + ], |
| 107 | + benchmarkDirectory, |
| 108 | + outputDirectory, |
| 109 | + 77, |
| 110 | + ); |
| 111 | + |
| 112 | + const output = |
| 113 | + fs.readFileSync(result.stdoutPath, "utf8") + |
| 114 | + fs.readFileSync(result.stderrPath, "utf8"); |
| 115 | + if (!output.includes("AFL numeric guidance finding")) { |
| 116 | + throw new Error("Guided numeric smoke did not report the expected finding"); |
| 117 | + } |
| 118 | + |
| 119 | + return { |
| 120 | + name: "guided-numeric", |
| 121 | + elapsedMs: result.elapsedMs, |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +function runAsyncSmoke() { |
| 126 | + const outputDirectory = path.join(workDirectory, "async-smoke"); |
| 127 | + const corpusDirectory = path.join(outputDirectory, "corpus"); |
| 128 | + removeIfExists(outputDirectory); |
| 129 | + ensureDirectory(corpusDirectory); |
| 130 | + fs.writeFileSync(path.join(corpusDirectory, "seed"), "async-seed"); |
| 131 | + |
| 132 | + const result = runCommand( |
| 133 | + "async throughput smoke", |
| 134 | + [ |
| 135 | + "jazzer", |
| 136 | + asyncTarget, |
| 137 | + "-f", |
| 138 | + "async_smoke", |
| 139 | + "--engine=afl", |
| 140 | + "--disable_bug_detectors=.*", |
| 141 | + "--runs=2000", |
| 142 | + "--seed=9001", |
| 143 | + "--maxLen=128", |
| 144 | + `--artifactPrefix=${outputDirectory}${path.sep}`, |
| 145 | + corpusDirectory, |
| 146 | + ], |
| 147 | + benchmarkDirectory, |
| 148 | + outputDirectory, |
| 149 | + ); |
| 150 | + |
| 151 | + const execsPerSecond = parseExecsPerSecond(result.stderrPath); |
| 152 | + if (execsPerSecond <= 0) { |
| 153 | + throw new Error("Async smoke reported a non-positive exec/sec rate"); |
| 154 | + } |
| 155 | + if (result.elapsedMs > 30000) { |
| 156 | + throw new Error( |
| 157 | + `Async smoke took unexpectedly long: ${result.elapsedMs} ms`, |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + return { |
| 162 | + name: "async-smoke", |
| 163 | + elapsedMs: result.elapsedMs, |
| 164 | + execsPerSecond, |
| 165 | + }; |
| 166 | +} |
| 167 | + |
| 168 | +function main() { |
| 169 | + ensureDirectory(workDirectory); |
| 170 | + const results = [runGuidedNumericSmoke(), runAsyncSmoke()]; |
| 171 | + for (const result of results) { |
| 172 | + const stats = [`elapsed_ms=${result.elapsedMs}`]; |
| 173 | + if (result.execsPerSecond !== undefined) { |
| 174 | + stats.push(`execs_per_second=${result.execsPerSecond}`); |
| 175 | + } |
| 176 | + console.log(`[anomaly] ${result.name}: ${stats.join(" ")}`); |
| 177 | + } |
| 178 | + fs.writeFileSync( |
| 179 | + path.join(workDirectory, "results.json"), |
| 180 | + JSON.stringify(results, null, 2), |
| 181 | + ); |
| 182 | + console.log( |
| 183 | + `\n[anomaly] wrote machine-readable results to ${path.join(workDirectory, "results.json")}`, |
| 184 | + ); |
| 185 | +} |
| 186 | + |
| 187 | +main(); |
0 commit comments