commit
401084bab3
265 files changed
+69224
No files matched your search
@@ -0,0 +1,596 @@
|
||||
#!/usr/bin/env node
|
||||
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
/**
|
||||
* Fuzz the pseudo-to-Mojo translator, the Mojo checker and the Mojo interpreter against the pinned compiler.
|
||||
*
|
||||
* The regression programs show what someone thought to write down. This writes programs nobody did: typed pseudo
|
||||
* expressions made at random from the notation's operators and built-ins over a handful of variables, one per
|
||||
* OUTPUT line. A line the pseudo itself stops on (a division by zero, a whole number past 2^53) or the checker
|
||||
* refuses is dropped, so every line left has an answer in the notation. The lines are translated to Mojo, built
|
||||
* and run by the local `mojo`, and each printed line is held to the pseudo's:
|
||||
*
|
||||
* rejected the compiler does not accept the Mojo the translator wrote for this line
|
||||
* stopped the built program stops at this line; the pseudo did not
|
||||
* differs the built program prints another value (more than ROUNDING apart: see scripts/mojo/local.mjs)
|
||||
* interpreter core's Mojo interpreter prints another line than the built program, or ends another way
|
||||
* checker core's Mojo checker reports an error on a program the compiler accepts
|
||||
*
|
||||
* Each finding is printed once per shape, with the smallest expression that shows it and how many others did.
|
||||
* A finding the translator cannot fix is a line it must refuse (`not translated`) or a note it must attach; add
|
||||
* the expression to library/pseudo/programs or tests/sync-mojo.test.mjs when you fix one.
|
||||
*
|
||||
* With --statements it writes whole programs instead: records, constants, routines with by-value and BYREF
|
||||
* parameters, loops of every kind, IF and CASE, the collection built-ins, copies changed after they are made, and a
|
||||
* tagged OUTPUT at many points. The pseudo's checker and a run are the filter: a line the checker refuses, a run stops
|
||||
* on, or the translator has no Mojo for is left out and the rest settled again; a program whose output depends on the
|
||||
* order SET members or map keys are visited is dropped. Each built program is held to the pseudo as a whole (the first
|
||||
* output line that differs, a rejection, a stop), and the interpreter and the checker to the built program. Each
|
||||
* shape is shown once, its smallest program shrunk by leaving out statements while the finding stays.
|
||||
*
|
||||
* node scripts/mojo/fuzz.mjs [--seed N] [--programs N] [--lines N] [--depth N] [--jobs N] [--keep DIR] [--json]
|
||||
* node scripts/mojo/fuzz.mjs --statements [--seed N] [--programs N] [--shrink BUILDS] [--shrink-seconds N] [--progress] [--dry] [--jobs N] [--keep DIR] [--json]
|
||||
*
|
||||
* The same seed gives the same programs. Exit status: 0 when nothing was found, 1 otherwise, 2 when the local
|
||||
* compiler is missing or is not the pinned version. Needs a build (dist/).
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { spawn, spawnSync } from 'node:child_process';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
const ROOT = fileURLToPath(new URL('../..', import.meta.url));
|
||||
const dist = (p) => pathToFileURL(path.join(ROOT, 'dist', p)).href;
|
||||
const argv = process.argv.slice(2);
|
||||
const opt = (name, dflt = null) => { const i = argv.indexOf(`--${name}`); return i >= 0 ? argv[i + 1] : dflt; };
|
||||
const flag = (name) => argv.includes(`--${name}`);
|
||||
if (!fs.existsSync(path.join(ROOT, 'dist', 'mojo', 'check.js'))) { console.error('mojo-fuzz: dist/ is missing; run `pixi run build` first'); process.exit(2); }
|
||||
|
||||
const { MOJO_VERSION } = await import(dist('mojo/generated/pin.js'));
|
||||
const { checkMojo } = await import(dist('mojo/check.js'));
|
||||
const { runMojo } = await import(dist('mojo/interp.js'));
|
||||
const { studyOf, runPseudo } = await import(dist('services/pseudo.js'));
|
||||
const { pseudoToMojo } = await import(dist('sync/pseudo-to-mojo.js'));
|
||||
const { outputDistance } = await import(dist('sync/compare.js'));
|
||||
// built as every program is: the pin's arguments (library/mojo/pin.json build.args), less an x86-64 CPU on another machine
|
||||
const { mojoBuildArgs } = await import(dist('services/godbolt.js'));
|
||||
const BUILD_ARGS = mojoBuildArgs('', { x86_64: process.arch === 'x64' }).split(' ').filter(Boolean);
|
||||
const { run: runProgram } = await import(dist('engines/pseudo/interp.js'));
|
||||
const { programGenerator, render, allNodes } = await import(pathToFileURL(path.join(ROOT, 'scripts', 'mojo', 'fuzz-programs.mjs')).href);
|
||||
|
||||
const ROUNDING = 1e-9;
|
||||
/**
|
||||
* Mojo 1.2.0 computes **, exp() and log2() on a Float64 to 9 to 13 significant digits, and a later step can make
|
||||
* that visible (a FLOOR next to a whole number, a SIN of a large angle, a difference of two near-equal values). A
|
||||
* line that uses one of them agrees when its numbers are within LOOSE of each other, or both within NEAR_ZERO of
|
||||
* nothing; it is counted apart. Everything else is held to ROUNDING.
|
||||
*/
|
||||
const INEXACT = /\^|POW\(|EXP\(|EXPM1\(|LOG2\(|SINH\(|COSH\(|TANH\(/;
|
||||
const LOOSE = 1e-5;
|
||||
const NEAR_ZERO = 1e-8;
|
||||
const nearZero = (a, b) => { const NUM = /-?\d+(?:\.\d+)?(?:e[+-]?\d+)?/gi; const xs = (a.match(NUM) ?? []).map(Number); const ys = (b.match(NUM) ?? []).map(Number); return xs.length === ys.length && xs.every((x, i) => x === ys[i] || (Math.abs(x) <= NEAR_ZERO && Math.abs(ys[i]) <= NEAR_ZERO)); };
|
||||
|
||||
/**
|
||||
* Findings that are the compiler's own, each with what was observed; they are printed apart and do not fail a
|
||||
* run. Take one off when a new pin fixes it (the fuzzer says when a listed one no longer occurs in a run that
|
||||
* could have shown it only by its absence, so read the list on every bump).
|
||||
*/
|
||||
const UPSTREAM = [
|
||||
{ kind: 'stopped', test: (f) => /SIGSEGV|SIGABRT/.test(f.detail) && /\^|POW\(/.test(f.expr),
|
||||
why: 'Mojo 1.2.0: a Float64 raised to a whole exponent of about 1e10 or more overflows the stack of the built program, which can print its earlier output again before it dies (library/mojo/probes/float-pow-huge)' },
|
||||
{ kind: 'stopped', test: (f) => /timeout/.test(f.detail) && /\^|POW\(/.test(f.expr) && /1e10|6\.02e23|4503599627370497/.test(f.expr),
|
||||
why: 'Mojo 1.2.0: a Float64 raised to a very large negative whole exponent does not come back in 30 seconds (library/mojo/probes/float-pow-huge)' },
|
||||
{ kind: 'stopped', test: (f) => /timeout/.test(f.detail) && /GCD\(|LCM\(/.test(f.expr) && /4503599627370497|1000000 \* 1000/.test(f.expr),
|
||||
why: 'Mojo 1.2.0: gcd() of an even number and an odd one subtracts instead of dividing (std/math/math.mojo shifts the first argument by the shared power of two only), so gcd(6, 4503599627370597) takes about 10^15 steps' },
|
||||
{ kind: 'differs', test: (f) => /\^|POW\(/.test(f.expr) && /1e10|6\.02e23|4503599627370497/.test(f.expr),
|
||||
why: 'Mojo 1.2.0: a Float64 raised to a very large exponent comes out wrong where it does not stop (0.5 ** 4503599627370497.0 prints 1.0, not 0.0), and a power of ten is not exact, which a FLOOR or a MOD after it shows (library/mojo/probes/float-pow-huge)' },
|
||||
{ kind: 'differs', test: (f) => /\b(SIN|COS|TAN)\(/.test(f.expr) && /COSH\(|EXP\(|\^|POW\(/.test(f.expr),
|
||||
why: 'Mojo 1.2.0: cosh() and exp() of a Float64 far from zero are good to about 10 digits (cosh(100.0) prints 1.3440585709627154e+43; the true value is 1.3440585709080678e+43; library/mojo/probes/float-cosh-accuracy), and the SIN, COS or TAN of so large a value depends on every one of its digits (Mojo\'s own sin, cos and tan of 1e22 are correct)' },
|
||||
{ kind: 'stopped', test: (f) => /SIGSEGV|SIGABRT/.test(f.detail) && /^\s*\w+(?:\[[^\]]*\])?\.\w+ <- /m.test(f.program ?? '') && /\b(FOR|WHILE|REPEAT)\b/.test(f.program ?? ''),
|
||||
why: 'Mojo 1.2.0: a struct whose List field is given a copy inside a loop, and which nothing reads after the loop, is freed twice: the built program crashes on the second or third pass (var r = Origin(List[Int]()); for e in range(3): var f = List[Int](); f.append(2); r.hash = f.copy()). Reading r after the loop, or moving f^ instead of copying, avoids it (library/mojo/probes/struct-field-loop-copy)' },
|
||||
{ kind: 'rejected', test: (f) => /did not finish/.test(f.detail),
|
||||
why: 'Mojo 1.2.0: `mojo build` does not finish on some small programs: a struct field set in a loop that holds an inner loop breaking on it, and set again after the loop (var p = Pair(True); for i in range(2): p.flag = True; for e in range(3): if p.flag: break; then p.flag = False; a minute and more, where a Bool variable in its place builds at once)' },
|
||||
];
|
||||
const SEED = Number(opt('seed', '1'));
|
||||
const PROGRAMS = Number(opt('programs', flag('statements') ? '100' : '20'));
|
||||
const LINES = Number(opt('lines', '40'));
|
||||
const DEPTH = Number(opt('depth', '3'));
|
||||
const JOBS = Math.max(1, Number(opt('jobs', '2')) || 2);
|
||||
const STATEMENTS = flag('statements');
|
||||
const SHRINK_BUILDS = Number(opt('shrink', '80'));
|
||||
// shrinking also runs the pseudo for every candidate, and a candidate that lost the line ending its loop runs to the
|
||||
// step limit: the builds were bounded and the time was not (seed 20261112 shrank one finding for over ten minutes)
|
||||
const SHRINK_SECONDS = Number(opt('shrink-seconds', '90'));
|
||||
const DRY = flag('dry');
|
||||
const BUILD_TIMEOUT = Number(opt('build-timeout', STATEMENTS ? '90' : '240')) * 1000;
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Random pseudo */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/** mulberry32: small, seeded, the same on every machine. */
|
||||
function rng(seed) {
|
||||
let a = seed >>> 0;
|
||||
return () => { a = (a + 0x6D2B79F5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
|
||||
}
|
||||
|
||||
const INTS = ['0', '1', '2', '3', '5', '7', '10', '12', '64', '100', '255', '1000', '65536', '1000000', '(-1)', '(-2)', '(-7)', '(-100)'];
|
||||
const SMALL = ['0', '1', '2', '3', '4', '5', '8'];
|
||||
const FLOATS = ['0.0', '1.0', '0.5', '1.5', '2.5', '3.5', '0.1', '0.25', '2.0', '3.25', '10.0', '100.0', '1e-3', '1e10', '6.02e23', '1e-10', '0.49999999999999994', '4503599627370497.0', '(-0.5)', '(-1.5)', '(-2.5)', '(-0.0)', '(-7.25)'];
|
||||
const STRINGS = ['"GATTACA"', '"tac"', '"A"', '""', '"hello, world"', '"a,b,,c"', '"Zebra"', '"T"'];
|
||||
|
||||
function generator(rand) {
|
||||
const pick = (xs) => xs[Math.floor(rand() * xs.length)];
|
||||
const some = (p) => rand() < p;
|
||||
const I = (d) => {
|
||||
if (d <= 0 || some(0.2)) return pick([...INTS, 'I', 'J', 'K', 'I', 'J']);
|
||||
const a = () => I(d - 1), f = () => F(d - 1);
|
||||
return pick([
|
||||
() => `${a()} + ${a()}`, () => `${a()} - ${a()}`, () => `${a()} * ${a()}`, () => `(${a()}) DIV (${a()})`, () => `(${a()}) MOD (${a()})`,
|
||||
() => `(${a()}) ^ ${pick(SMALL)}`, () => `ABS(${a()})`, () => `MIN(${a()}, ${a()})`, () => `MAX(${a()}, ${a()})`, () => `MIN(${a()}, ${a()}, ${a()})`,
|
||||
() => `MIN(${LI(d - 1)})`, () => `MAX(${LI(d - 1)})`, () => `LENGTH(${S(d - 1)})`, () => `LENGTH(${LI(d - 1)})`, () => `LENGTH(${LF(d - 1)})`,
|
||||
() => `ROUND(${f()})`, () => `FLOOR(${f()})`, () => `CEIL(${f()})`, () => `INT(${f()})`, () => `SIGN(${a()})`,
|
||||
() => `CLAMP(${a()}, ${a()}, ${a()})`, () => `GCD(${a()}, ${a()})`, () => `LCM(${a()}, ${a()})`,
|
||||
() => `INDEX_OF(${S(d - 1)}, ${S(0)})`, () => `INDEX_OF(${LI(d - 1)}, ${a()})`, () => `ORD(C)`, () => `SUM_OF(${LI(d - 1)})`,
|
||||
() => `LI[(${a()}) MOD LENGTH(LI)]`, () => `DOT(${LI(0)}, ${LI(0)})`,
|
||||
() => `BIT_AND(${a()}, ${a()})`, () => `BIT_OR(${a()}, ${a()})`, () => `BIT_XOR(${a()}, ${a()})`, () => `BIT_NOT(${a()})`,
|
||||
() => `SHIFT_LEFT(${a()}, ${pick(SMALL)})`, () => `SHIFT_RIGHT(${a()}, ${pick(SMALL)})`, () => `CHOOSE(${pick(SMALL)}, ${pick(SMALL)})`,
|
||||
() => `TO_INTEGER("${pick(['12', '0', '-7', '007'])}")`, () => `LENGTH(SPLIT(${S(d - 1)}, ${S(0)}))`,
|
||||
])();
|
||||
};
|
||||
const F = (d) => {
|
||||
if (d <= 0 || some(0.2)) return pick([...FLOATS, 'X', 'Y', 'Z', 'X', 'Y']);
|
||||
const a = () => F(d - 1), i = () => I(d - 1);
|
||||
return pick([
|
||||
() => `${a()} + ${a()}`, () => `${a()} - ${a()}`, () => `${a()} * ${a()}`, () => `(${a()}) / (${a()})`, () => `(${i()}) / (${i()})`,
|
||||
() => `${i()} * ${a()}`, () => `${a()} + ${i()}`, () => `${i()} - ${a()}`, () => `(${a()}) ^ ${pick(SMALL)}`, () => `(${a()}) ^ (${a()})`, () => `(${i()}) ^ (${a()})`,
|
||||
() => `ABS(${a()})`, () => `SQRT(${a()})`, () => `EXP(${a()})`, () => `LOG(${a()})`, () => `LOG2(${a()})`, () => `LOG10(${a()})`,
|
||||
() => `SIN(${a()})`, () => `COS(${a()})`, () => `TAN(${a()})`, () => `ATAN2(${a()}, ${a()})`, () => `HYPOT(${a()}, ${a()})`, () => `POW(${a()}, ${a()})`,
|
||||
() => `CBRT(${a()})`, () => `EXPM1(${a()})`, () => `LOG1P(${a()})`, () => `ASIN(${a()})`, () => `ACOS(${a()})`, () => `ATAN(${a()})`,
|
||||
() => `SINH(${a()})`, () => `COSH(${a()})`, () => `TANH(${a()})`, () => `TRUNC(${a()})`, () => `SIGN(${a()})`, () => `COPYSIGN(${a()}, ${a()})`,
|
||||
() => `CLAMP(${a()}, ${a()}, ${a()})`, () => `FMA(${a()}, ${a()}, ${a()})`, () => `MIN(${a()}, ${a()})`, () => `MAX(${a()}, ${i()})`, () => `MIN(${i()}, ${a()})`,
|
||||
() => `MEAN_OF(${LF(d - 1)})`, () => `MEAN_OF(${LI(d - 1)})`, () => `VARIANCE_OF(${LF(d - 1)})`, () => `STDEV_OF(${LI(d - 1)})`, () => `MEDIAN_OF(${LF(d - 1)})`, () => `MEDIAN_OF(${LI(d - 1)})`,
|
||||
() => `NORM_OF(${LF(d - 1)})`, () => `SUM_OF(${LF(d - 1)})`, () => `DOT(${LF(0)}, ${LF(0)})`, () => `LF[(${i()}) MOD LENGTH(LF)]`, () => `MIN(${LF(d - 1)})`, () => `MAX(${LF(d - 1)})`,
|
||||
() => `TO_FLOAT(${i()})`, () => 'PI()', () => 'E()', () => `ULP(${a()})`, () => `NEXT_AFTER(${a()}, ${a()})`, () => `TO_FLOAT("${pick(['2.5', '1e3', '-0.125', '10'])}")`,
|
||||
])();
|
||||
};
|
||||
const num = (d) => (some(0.5) ? I(d) : F(d));
|
||||
const B = (d) => {
|
||||
if (d <= 0 || some(0.15)) return pick(['TRUE', 'FALSE', 'P', 'Q']);
|
||||
const cmp = () => pick(['==', '!=', '<', '<=', '>', '>=']);
|
||||
return pick([
|
||||
() => `${num(d - 1)} ${cmp()} ${num(d - 1)}`, () => `${I(d - 1)} ${cmp()} ${I(d - 1)}`, () => `${F(d - 1)} ${cmp()} ${F(d - 1)}`,
|
||||
() => `${num(d - 1)} ${pick(['<', '<='])} ${num(d - 1)} ${pick(['<', '<='])} ${num(d - 1)}`,
|
||||
() => `(${B(d - 1)}) AND (${B(d - 1)})`, () => `(${B(d - 1)}) OR (${B(d - 1)})`, () => `NOT (${B(d - 1)})`,
|
||||
() => `${I(d - 1)} IN ${LI(d - 1)}`, () => `${num(d - 1)} IN [${I(0)}, ${I(0)})`, () => `C IN ${S(d - 1)}`,
|
||||
() => `IS_CLOSE(${F(d - 1)}, ${F(d - 1)}, 1e-9, 0.0)`, () => `${S(d - 1)} ${cmp()} ${S(d - 1)}`, () => `IS_FINITE(${F(d - 1)})`,
|
||||
])();
|
||||
};
|
||||
const S = (d) => {
|
||||
if (d <= 0 || some(0.25)) return pick([...STRINGS, 'S', 'T', 'S', 'T']);
|
||||
const a = () => S(d - 1);
|
||||
return pick([
|
||||
() => `${a()} & ${a()}`, () => `UPPER(${a()})`, () => `LOWER(${a()})`, () => `SLICE(S, ${pick(SMALL)}, ${pick(SMALL)})`, () => `REPLACE(${a()}, ${S(0)}, ${S(0)})`,
|
||||
() => `TO_STRING(${I(d - 1)})`, () => `TO_STRING(${F(d - 1)})`, () => `TO_STRING(${B(d - 1)})`, () => `JOIN(${LS(d - 1)}, ${S(0)})`,
|
||||
() => `${a()} & TO_STRING(${I(d - 1)})`, () => `"" & S[(${I(d - 1)}) MOD LENGTH(S)]`,
|
||||
])();
|
||||
};
|
||||
const LI = (d) => {
|
||||
if (d <= 0 || some(0.4)) return pick(['LI', 'LI', `[${I(0)}, ${I(0)}, ${I(0)}]`]);
|
||||
return pick([() => `SORTED(${LI(d - 1)})`, () => `SLICE(LI, ${pick(SMALL)}, ${pick(SMALL)})`, () => `FILL(${pick(SMALL)}, ${I(d - 1)})`, () => `[${I(d - 1)}, ${I(d - 1)}]`])();
|
||||
};
|
||||
const LF = (d) => {
|
||||
if (d <= 0 || some(0.4)) return pick(['LF', 'LF', `[${F(0)}, ${F(0)}, ${F(0)}]`]);
|
||||
return pick([() => `SORTED(${LF(d - 1)})`, () => `SLICE(LF, ${pick(SMALL)}, ${pick(SMALL)})`, () => `FILL(${pick(SMALL)}, ${F(d - 1)})`, () => `[${F(d - 1)}, ${I(d - 1)}]`])();
|
||||
};
|
||||
const LS = (d) => (d <= 0 || some(0.5) ? pick(['LS', `[${S(0)}, ${S(0)}]`]) : pick([() => `SPLIT(${S(d - 1)}, ${S(0)})`, () => `SORTED(${LS(d - 1)})`])());
|
||||
const KINDS = [[I, 4], [F, 5], [B, 3], [S, 2], [LI, 1], [LF, 1], [LS, 1]];
|
||||
const total = KINDS.reduce((n, [, w]) => n + w, 0);
|
||||
const expr = (d) => { let r = rand() * total; for (const [g, w] of KINDS) { r -= w; if (r < 0) return g(d); } return I(d); };
|
||||
const preamble = () => [
|
||||
`DECLARE I : INTEGER <- ${pick(['7', '1', '12', '100', '0'])}`, `DECLARE J : INTEGER <- ${pick(['-3', '-1', '5', '2', '-10'])}`, `DECLARE K : INTEGER <- ${pick(['0', '3', '1', '-2'])}`,
|
||||
`DECLARE X : FLOAT <- ${pick(['2.5', '0.5', '1.0', '3.75', '10.0'])}`, `DECLARE Y : FLOAT <- ${pick(['-0.75', '-2.5', '0.1', '1.5', '-1.0'])}`, `DECLARE Z : FLOAT <- ${pick(['1e-3', '0.0', '2.0', '1e6', '0.3'])}`,
|
||||
`DECLARE P : BOOL <- ${pick(['TRUE', 'FALSE'])}`, `DECLARE Q : BOOL <- ${pick(['TRUE', 'FALSE'])}`,
|
||||
`DECLARE S : STRING <- ${pick(['"GATTACA"', '"hello"', '"a,b,c"'])}`, `DECLARE T : STRING <- ${pick(['"tac"', '"T"', '"l"', '","'])}`, `DECLARE C : CHAR <- ${pick(["'A'", "'l'", "','"])}`,
|
||||
`DECLARE LI : LIST OF INTEGER <- ${pick(['[4, -1, 9, 0, 7]', '[3, 3, 1]', '[10, 20]'])}`, `DECLARE LF : LIST OF FLOAT <- ${pick(['[1.5, -2.25, 0.5, 3.0]', '[0.1, 0.2, 0.3]', '[2.0, 4.0]'])}`,
|
||||
`DECLARE LS : LIST OF STRING <- ${pick(['["pear", "apple", "fig"]', '["b", "a"]'])}`,
|
||||
];
|
||||
return { expr, preamble };
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* The local compiler */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
function compilerVersion() {
|
||||
const p = spawnSync('mojo', ['--version'], { encoding: 'utf8' });
|
||||
return p.status === 0 ? (/Mojo\s+(\S+)/.exec(p.stdout) || [])[1] ?? null : null;
|
||||
}
|
||||
const exec = (cmd, args, { cwd, timeout = 60000 } = {}) => new Promise((resolve) => {
|
||||
const c = spawn(cmd, args, { cwd, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
let stdout = ''; let stderr = ''; let timedOut = false; let capped = false;
|
||||
const t = setTimeout(() => { timedOut = true; c.kill('SIGKILL'); }, timeout);
|
||||
// at most 4 MiB of output is kept; `capped` says some was dropped, so nobody compares a line that was cut
|
||||
c.stdout.on('data', (d) => { if (stdout.length < 1 << 22) stdout += d; else capped = true; });
|
||||
c.stderr.on('data', (d) => { if (stderr.length < 1 << 22) stderr += d; });
|
||||
c.on('error', (e) => { clearTimeout(t); resolve({ status: null, signal: null, stdout, stderr: String(e), timedOut, capped }); });
|
||||
c.on('close', (status, signal) => { clearTimeout(t); resolve({ status, signal, stdout, stderr, timedOut, capped }); });
|
||||
});
|
||||
const HAVE_PRLIMIT = spawnSync('prlimit', ['--version'], { encoding: 'utf8' }).status === 0;
|
||||
|
||||
async function buildAndRun(source, dir, runTimeout = 30000) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, 'example.mojo'), source);
|
||||
fs.rmSync(path.join(dir, 'example'), { force: true });
|
||||
const b = await exec('mojo', ['build', ...BUILD_ARGS, '--disable-warnings', 'example.mojo', '-o', 'example'], { cwd: dir, timeout: BUILD_TIMEOUT });
|
||||
if (b.timedOut) return { compiled: false, stderr: `the compiler did not finish in ${BUILD_TIMEOUT / 1000} seconds` };
|
||||
if (b.status !== 0 || !fs.existsSync(path.join(dir, 'example'))) return { compiled: false, stderr: b.stderr || b.stdout };
|
||||
// a trap must not become a core dump on someone's desktop (see scripts/mojo/local.mjs)
|
||||
const r = HAVE_PRLIMIT ? await exec('prlimit', ['--core=1:1', './example'], { cwd: dir, timeout: runTimeout }) : await exec('./example', [], { cwd: dir, timeout: runTimeout });
|
||||
return { compiled: true, status: r.timedOut ? 'timeout' : r.status ?? `signal ${r.signal}`, stdout: r.stdout, stderr: r.stderr, capped: r.capped };
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* One program */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
const counts = { candidates: 0, refusedByChecker: 0, stoppedInPseudo: 0, notTranslated: 0, lines: 0, agree: 0, rounding: 0, inexact: 0, builds: 0 };
|
||||
const findings = [];
|
||||
const gaps = new Map();
|
||||
const outputOf = (pre, e) => {
|
||||
const st = studyOf(`${pre}\nOUTPUT ${e}\n`);
|
||||
if (!st.ok) return { refused: true };
|
||||
const tr = runPseudo(st, { maxSteps: 200000 });
|
||||
return tr.error ? { stops: tr.error.kind } : { line: tr.raw.output.join('\n') };
|
||||
};
|
||||
|
||||
async function oneProgram(n, workdir) {
|
||||
const rand = rng(SEED * 100003 + n);
|
||||
const g = generator(rand);
|
||||
const pre = g.preamble().join('\n');
|
||||
// lines that have an answer in the notation
|
||||
let lines = [];
|
||||
for (let tries = 0; lines.length < LINES && tries < LINES * 12; tries++) {
|
||||
const e = g.expr(1 + Math.floor(rand() * DEPTH));
|
||||
counts.candidates++;
|
||||
const o = outputOf(pre, e);
|
||||
if (o.refused) { counts.refusedByChecker++; continue; }
|
||||
if (o.stops) { counts.stoppedInPseudo++; continue; }
|
||||
if (o.line.includes('\n')) continue;
|
||||
lines.push({ expr: e, want: o.line });
|
||||
}
|
||||
const preLines = pre.split('\n').length;
|
||||
for (let round = 0; round < 12 && lines.length; round++) {
|
||||
const pseudo = `${pre}\n${lines.map((l) => `OUTPUT ${l.expr}`).join('\n')}\n`;
|
||||
const mj = pseudoToMojo(pseudo);
|
||||
if ('unsupported' in mj) { findings.push({ kind: 'rejected', expr: lines[0].expr, detail: `the translator refused a program the pseudo runs: ${mj.unsupported[0]?.message}` }); return; }
|
||||
const lineOf = (pseudoLine) => lines[pseudoLine - preLines - 1];
|
||||
// a line the translator says it has no Mojo for is not a finding: it said so
|
||||
const gapLines = new Set(mj.notes.filter((x) => x.id === 'translate-gap').map((x) => x.line));
|
||||
if (gapLines.size) {
|
||||
for (const x of mj.notes.filter((y) => y.id === 'translate-gap')) { const why = (/\((.*)\)\. The generated/.exec(x.message) || [])[1] ?? x.message; gaps.set(why, (gaps.get(why) || 0) + 1); }
|
||||
counts.notTranslated += gapLines.size;
|
||||
lines = lines.filter((_, i) => !gapLines.has(preLines + 1 + i));
|
||||
continue;
|
||||
}
|
||||
counts.builds++;
|
||||
const r = await buildAndRun(mj.source, path.join(workdir, `p${n}`));
|
||||
if (!r.compiled) {
|
||||
const bad = new Map();
|
||||
const errLines = r.stderr.split('\n');
|
||||
errLines.forEach((l, i) => {
|
||||
let m = /example\.mojo:(\d+):\d+: error: (.*)$/.exec(l);
|
||||
if (!m) return;
|
||||
if (/function instantiation failed/.test(m[2])) {
|
||||
// the error is reported at the def; the note that follows names the call, and the next one the reason
|
||||
const rest = errLines.slice(i + 1);
|
||||
const call = rest.map((x) => /example\.mojo:(\d+):\d+: note: call expansion failed/.exec(x)).find(Boolean);
|
||||
const why = rest.map((x) => /note: constraint failed: (.*)$/.exec(x)).find(Boolean);
|
||||
if (call) m = [m[0], call[1], `function instantiation failed${why ? `: ${why[1]}` : ''}`];
|
||||
}
|
||||
const from = mj.map[Number(m[1]) - 1];
|
||||
const hit = from ? lineOf(from) : null;
|
||||
if (hit && !bad.has(hit)) bad.set(hit, { message: m[2].slice(0, 160), mojo: (mj.source.split('\n')[Number(m[1]) - 1] || '').trim().slice(0, 200) });
|
||||
});
|
||||
if (!bad.size) { findings.push({ kind: 'rejected', expr: '(a whole program)', detail: `${(errLines.find((l) => /error:/.test(l)) || errLines[0] || '').slice(0, 200)}`, program: pseudo }); return; }
|
||||
for (const [l, why] of bad) findings.push({ kind: 'rejected', expr: l.expr, detail: why.message, mojo: why.mojo });
|
||||
lines = lines.filter((l) => !bad.has(l));
|
||||
continue;
|
||||
}
|
||||
const got = r.stdout.replace(/\n$/, '').split('\n');
|
||||
if (r.status !== 0) {
|
||||
// a trap says where it happened, on standard output, after everything printed before it; without that, the first
|
||||
// line that did not print what the pseudo gives is where the built program stopped. Counting the lines printed
|
||||
// is not enough: a program that overflows its stack can print what it had printed again, thousands of times,
|
||||
// before it dies (seed 201: a Float64 to a huge whole power, then 3,673 lines from 40 prints, exit 134).
|
||||
const said = /^At: .*example\.mojo:(\d+):\d+: (.*)$/.exec(got.at(-1) ?? '');
|
||||
const from = said ? mj.map[Number(said[1]) - 1] : null;
|
||||
const holds = (x, g) => { const far = outputDistance(x.want, g); return far <= ROUNDING || (INEXACT.test(x.expr) && Number.isFinite(far) && (far <= LOOSE || nearZero(x.want, g))); };
|
||||
const firstOff = r.stdout === '' ? 0 : lines.findIndex((x, i) => !holds(x, got[i] ?? ''));
|
||||
const l = from ? lineOf(from) : lines[firstOff < 0 ? Math.min(got.length, lines.length) : firstOff];
|
||||
if (said) r.stderr = `${said[2]}\n${r.stderr}`;
|
||||
if (l) { findings.push({ kind: 'stopped', expr: l.expr, detail: `the built program stopped (exit ${r.status}): ${(r.stderr.trim().split('\n').find((x) => /Error|error|At:/.test(x)) || '').slice(0, 160)}; the pseudo gives ${l.want}` }); lines = lines.filter((x) => x !== l); continue; }
|
||||
findings.push({ kind: 'stopped', expr: '(a whole program)', detail: `the built program stopped (exit ${r.status}) and nothing says where`, program: pseudo });
|
||||
return;
|
||||
}
|
||||
// the interpreter and the checker, against the same build
|
||||
const ck = checkMojo(mj.source);
|
||||
const errs = ck.diagnostics.filter((d) => d.severity === 'error');
|
||||
if (errs.length) for (const d of errs) { const hit = mj.map[d.line - 1] ? lineOf(mj.map[d.line - 1]) : null; findings.push({ kind: 'checker', expr: hit?.expr ?? '(the declarations)', detail: d.message.slice(0, 160) }); }
|
||||
else {
|
||||
const run = runMojo(ck.module, mj.source, { maxSteps: 400000, record: false });
|
||||
if (!run.refused && !run.limited) {
|
||||
const mine = run.stdout.replace(/\n$/, '').split('\n');
|
||||
for (let i = 0; i < lines.length; i++) if ((mine[i] ?? '') !== (got[i] ?? '')) { findings.push({ kind: 'interpreter', expr: lines[i].expr, detail: `the interpreter prints ${JSON.stringify((mine[i] ?? '').slice(0, 80))}; the built program prints ${JSON.stringify((got[i] ?? '').slice(0, 80))}` }); }
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
counts.lines++;
|
||||
const far = outputDistance(lines[i].want, got[i] ?? '');
|
||||
if (far === 0) counts.agree++;
|
||||
else if (far <= ROUNDING) counts.rounding++;
|
||||
else if (INEXACT.test(lines[i].expr) && Number.isFinite(far) && (far <= LOOSE || nearZero(lines[i].want, got[i] ?? ''))) counts.inexact++;
|
||||
else findings.push({ kind: 'differs', expr: lines[i].expr, detail: `the pseudo gives ${JSON.stringify(lines[i].want.slice(0, 80))}; the built program prints ${JSON.stringify((got[i] ?? '').slice(0, 80))}` });
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* One whole program (--statements) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
const STMT = { interpRan: 0, interpDeclined: 0, candidates: 0, firstTry: 0, survived: 0, dropped: 0, orderDependent: 0, linesGenerated: 0, linesKept: 0, programs: 0, outputs: 0, agree: 0, rounding: 0, builds: 0, shrinkBuilds: 0, noMain: 0, tooMuch: 0, pseudoFaults: [] };
|
||||
const coverage = new Map();
|
||||
const innermost = (owner, line) => (owner[line - 1] ?? []).at(-1) ?? null;
|
||||
const pseudoRun = (st, shuffle = false) => runProgram(st.program.program, { record: false, contracts: true, maxSteps: 200000, seed: shuffle ? 2 : 1, shuffle });
|
||||
|
||||
/**
|
||||
* A program the notation runs to its end, or why not: the checker's errors, a stop, a run that prints something
|
||||
* else when SET members and map keys are visited in another order, the lines the translator says it has no Mojo for.
|
||||
*/
|
||||
function settle(top, removed) {
|
||||
const r = render(top, removed);
|
||||
const st = studyOf(r.text);
|
||||
if (!st.ok) return { bad: 'checker', lines: st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.line), r };
|
||||
const tr = pseudoRun(st);
|
||||
if (tr.error) return { bad: tr.error.kind === 'internal' ? 'internal' : 'stops', lines: [tr.error.line].filter(Boolean), r, error: tr.error };
|
||||
const sh = pseudoRun(st, true);
|
||||
if (sh.error || sh.output.join('\n') !== tr.output.join('\n')) return { bad: 'order', lines: [], r };
|
||||
const mj = pseudoToMojo(r.text);
|
||||
if ('unsupported' in mj) return { bad: 'unsupported', lines: [], r };
|
||||
const gapNotes = mj.notes.filter((x) => x.id === 'translate-gap');
|
||||
if (gapNotes.length) return { bad: 'gap', lines: gapNotes.map((x) => x.line), notes: gapNotes, r };
|
||||
return { r, want: tr.output, mj };
|
||||
}
|
||||
|
||||
/** What the built program says about a settled program: the first finding, or null when everything agrees. */
|
||||
async function judgeBuilt(s, dir, top, removed) {
|
||||
const { r, want, mj } = s;
|
||||
const lineText = (l) => (r.text.split('\n')[l - 1] ?? '').trim();
|
||||
const nodeAt = (l) => innermost(r.owner, l);
|
||||
const tagNode = (outLine) => { const m = /^@(\d+) /.exec(outLine ?? ''); if (!m) return null; return allNodes(top).find((x) => x.tag === Number(m[1]) && !removed.has(x)) ?? null; };
|
||||
const b = await buildAndRun(mj.source, dir, 10000);
|
||||
// a program that prints more than the harness keeps (seed 20261112: 5.6 MB from a loop over a list it grows) was
|
||||
// compared up to a line cut in the middle and reported as different there. It is counted apart instead.
|
||||
if (b.compiled && b.capped) { STMT.tooMuch++; return null; }
|
||||
if (!b.compiled) {
|
||||
const errLines = b.stderr.split('\n');
|
||||
for (let i = 0; i < errLines.length; i++) {
|
||||
let m = /example\.mojo:(\d+):\d+: error: (.*)$/.exec(errLines[i]);
|
||||
if (!m) continue;
|
||||
if (/function instantiation failed/.test(m[2])) {
|
||||
const rest = errLines.slice(i + 1);
|
||||
const call = rest.map((x) => /example\.mojo:(\d+):\d+: note: call expansion failed/.exec(x)).find(Boolean);
|
||||
const why = rest.map((x) => /note: constraint failed: (.*)$/.exec(x)).find(Boolean);
|
||||
if (call) m = [m[0], call[1], `function instantiation failed${why ? `: ${why[1]}` : ''}`];
|
||||
}
|
||||
const from = mj.map[Number(m[1]) - 1];
|
||||
return { kind: 'rejected', shape: m[2].replace(/'[^']*'/g, "'…'").replace(/\d+/g, 'N').slice(0, 120), detail: m[2].slice(0, 200), mojo: (mj.source.split('\n')[Number(m[1]) - 1] || '').trim().slice(0, 200), expr: from ? lineText(from) : '(no pseudo line)', at: from ? nodeAt(from) : null };
|
||||
}
|
||||
return { kind: 'rejected', shape: /did not finish/.test(b.stderr) ? 'the compiler did not finish' : 'no error line', detail: (errLines.find((l) => /error:|did not finish/.test(l)) || errLines[0] || '').slice(0, 200), expr: '(a whole program)', at: null };
|
||||
}
|
||||
const got = b.stdout.replace(/\n$/, '').split('\n').filter((x, i, a) => !(x === '' && i === a.length - 1 && b.stdout === ''));
|
||||
const gotLines = b.stdout === '' ? [] : got;
|
||||
// the interpreter and the checker, against the same build
|
||||
const ck = checkMojo(mj.source);
|
||||
const errs = ck.diagnostics.filter((d) => d.severity === 'error');
|
||||
let side = null;
|
||||
if (errs.length) { const d = errs[0]; const from = mj.map[d.line - 1]; side = { kind: 'checker', shape: d.message.replace(/'[^']*'/g, "'…'").replace(/\d+/g, 'N').slice(0, 120), detail: d.message.slice(0, 200), mojo: (mj.source.split('\n')[d.line - 1] || '').trim(), expr: from ? lineText(from) : '(no pseudo line)', at: from ? nodeAt(from) : null }; }
|
||||
else {
|
||||
const run = runMojo(ck.module, mj.source, { maxSteps: 2000000, record: false });
|
||||
if (run.refused || run.limited) STMT.interpDeclined++; else STMT.interpRan++;
|
||||
if (!run.refused && !run.limited) {
|
||||
const mine = run.stdout === '' ? [] : run.stdout.replace(/\n$/, '').split('\n');
|
||||
const i = mine.findIndex((x, k) => x !== gotLines[k]);
|
||||
const at = i >= 0 ? i : mine.length !== gotLines.length ? Math.min(mine.length, gotLines.length) : -1;
|
||||
const endsOk = (b.status === 0) === !run.error;
|
||||
if (at >= 0 || !endsOk) side = { kind: 'interpreter', shape: tagNode(gotLines[at] ?? want[at])?.label ?? 'end', detail: at >= 0 ? `output line ${at + 1}: the interpreter prints ${JSON.stringify((mine[at] ?? '(nothing)').slice(0, 100))}; the built program prints ${JSON.stringify((gotLines[at] ?? '(nothing)').slice(0, 100))}` : `the interpreter ends ${run.error ? `with ${String(run.error.message ?? run.error).slice(0, 80)}` : 'normally'}; the built program exits ${b.status}`, expr: '(see the program)', at: null };
|
||||
}
|
||||
}
|
||||
if (b.status !== 0) {
|
||||
const said = /^At: .*example\.mojo:(\d+):\d+: (.*)$/m.exec(b.stdout);
|
||||
const from = said ? mj.map[Number(said[1]) - 1] : null;
|
||||
const why = b.status === 'timeout' ? 'it did not end within 10 seconds' : `exit ${b.status}: ${(b.stderr.trim().split('\n').find((x) => /Error|error|At:/.test(x)) || said?.[2] || '').slice(0, 160)}`;
|
||||
const nd = from ? nodeAt(from) : tagNode(want[gotLines.length]);
|
||||
return { kind: 'stopped', shape: `${nd?.label ?? 'somewhere'} ${b.status === 'timeout' ? 'timeout' : 'trap'}`, detail: `the built program stopped (${why}) after ${gotLines.length} of ${want.length} lines`, expr: from ? lineText(from) : want[gotLines.length] ?? '', at: nd };
|
||||
}
|
||||
for (let i = 0; i < Math.max(want.length, gotLines.length); i++) {
|
||||
const far = outputDistance(want[i] ?? '(nothing)', gotLines[i] ?? '(nothing)');
|
||||
STMT.outputs++;
|
||||
if (far === 0) { STMT.agree++; continue; }
|
||||
if (far <= ROUNDING) { STMT.rounding++; continue; }
|
||||
const nd = tagNode(want[i]) ?? tagNode(gotLines[i]);
|
||||
return { kind: 'differs', shape: nd?.label ?? 'untagged', detail: `output line ${i + 1}: the pseudo gives ${JSON.stringify((want[i] ?? '(nothing)').slice(0, 100))}; the built program prints ${JSON.stringify((gotLines[i] ?? '(nothing)').slice(0, 100))}`, expr: nd ? render([nd]).text.trim() : '(an untagged line)', at: nd };
|
||||
}
|
||||
return side;
|
||||
}
|
||||
|
||||
/**
|
||||
* Leave out nodes one at a time, keeping each removal after which the program still settles and the built program
|
||||
* still shows the same kind and shape of finding; stop when nothing more can go or the build budget is spent.
|
||||
*/
|
||||
async function shrink(f, workdir) {
|
||||
const removed = new Set(f.removed);
|
||||
let budget = SHRINK_BUILDS;
|
||||
const until = Date.now() + SHRINK_SECONDS * 1000;
|
||||
const holds = async (rem) => {
|
||||
const s = settle(f.top, rem);
|
||||
if (s.bad) return false;
|
||||
if (budget-- <= 0) return false;
|
||||
STMT.shrinkBuilds++;
|
||||
const g = await judgeBuilt(s, path.join(workdir, 'shrink'), f.top, rem);
|
||||
return !!g && g.kind === f.kind && g.shape === f.shape;
|
||||
};
|
||||
for (let changed = true; changed && budget > 0 && Date.now() < until;) {
|
||||
changed = false;
|
||||
for (const nd of allNodes(f.top)) {
|
||||
if (removed.has(nd) || budget <= 0 || Date.now() >= until) continue;
|
||||
const rem = new Set(removed); rem.add(nd);
|
||||
if (await holds(rem)) { removed.add(nd); changed = true; }
|
||||
}
|
||||
}
|
||||
return render(f.top, removed).text;
|
||||
}
|
||||
|
||||
const wholeFindings = [];
|
||||
async function oneWholeProgram(n, workdir) {
|
||||
const rand = rng(SEED * 100003 + n + 0x51a7e);
|
||||
const top = programGenerator(rand).program();
|
||||
const removed = new Set();
|
||||
STMT.candidates++;
|
||||
const generated = render(top).text.split('\n').length - 1;
|
||||
STMT.linesGenerated += generated;
|
||||
let clean = true; let builtOnce = false;
|
||||
for (let round = 0; round < 16; round++) {
|
||||
const s = settle(top, removed);
|
||||
if (s.bad) {
|
||||
clean = false;
|
||||
if (s.bad === 'order') { STMT.orderDependent++; return; }
|
||||
if (s.bad === 'internal') STMT.pseudoFaults.push({ message: s.error.message, line: s.error.line, program: s.r.text });
|
||||
if (s.bad === 'gap') for (const x of s.notes) { const why = (/\((.*)\)\. The generated/.exec(x.message) || [])[1] ?? x.message; gaps.set(why, (gaps.get(why) || 0) + 1); }
|
||||
const drop = s.lines.map((l) => innermost(s.r.owner, l)).filter(Boolean);
|
||||
if (!drop.length) { STMT.dropped++; return; }
|
||||
for (const d of drop) removed.add(d);
|
||||
continue;
|
||||
}
|
||||
if (!builtOnce) {
|
||||
builtOnce = true;
|
||||
STMT.programs++; if (clean) STMT.firstTry++; STMT.survived++;
|
||||
STMT.linesKept += s.r.text.split('\n').length - 1;
|
||||
for (const nd of allNodes(top)) if (!removed.has(nd)) coverage.set(nd.label, (coverage.get(nd.label) || 0) + 1);
|
||||
}
|
||||
if (DRY) { if (flag('show')) console.log(`--- program ${n}\n${s.r.text}`); return; }
|
||||
// when every line of the main part was left out, routines and declarations are all that remain: the translation
|
||||
// is a module with no main, which is right for it and which no compiler builds into a program. Nothing to compare.
|
||||
if (!/^def main\(/m.test(s.mj.source)) { STMT.noMain++; return; }
|
||||
STMT.builds++;
|
||||
const f = await judgeBuilt(s, path.join(workdir, `p${n}`), top, removed);
|
||||
if (!f) return;
|
||||
wholeFindings.push({ ...f, program: s.r.text, top, removed: new Set(removed), seedProgram: n });
|
||||
if (flag('progress')) process.stderr.write(`program ${n}: ${f.kind} ${f.shape}: ${f.detail.slice(0, 160)}\n`);
|
||||
// a line the compiler rejects is left out and the rest is built again, so one program can show several things
|
||||
if (f.kind === 'rejected' && f.at) { removed.add(f.at); continue; }
|
||||
return;
|
||||
}
|
||||
if (!builtOnce) STMT.dropped++;
|
||||
}
|
||||
|
||||
let wholeOurs = [];
|
||||
/** The statement mode's report: counts, coverage, each shape once with its smallest program shrunk, the compiler's own apart. */
|
||||
async function reportStatements(workdir) {
|
||||
const upstream = wholeFindings.filter((f) => UPSTREAM.some((u) => u.kind === f.kind && u.test(f)));
|
||||
wholeOurs = wholeFindings.filter((f) => !upstream.includes(f));
|
||||
const shapes = new Map();
|
||||
for (const f of wholeOurs) {
|
||||
const key = `${f.kind} ${f.shape}`;
|
||||
const s = shapes.get(key) ?? { kind: f.kind, shape: f.shape, count: 0, example: f };
|
||||
s.count++;
|
||||
if (f.program.length < s.example.program.length) s.example = f;
|
||||
shapes.set(key, s);
|
||||
}
|
||||
const sorted = [...shapes.values()].sort((a, b) => a.kind.localeCompare(b.kind) || b.count - a.count);
|
||||
for (const s of sorted) s.shrunk = SHRINK_BUILDS > 0 ? await shrink(s.example, workdir) : s.example.program;
|
||||
const pct = (a, b) => `${b ? Math.round((100 * a) / b) : 0}%`;
|
||||
if (flag('json')) {
|
||||
console.log(JSON.stringify({ compiler: `Mojo ${version}`, seed: SEED, counts: { ...STMT, pseudoFaults: STMT.pseudoFaults.length }, coverage: Object.fromEntries(coverage), shapes: sorted.map(({ example, ...x }) => ({ ...x, detail: example.detail, mojo: example.mojo, line: example.expr })), upstream: upstream.map(({ top, removed, at, ...x }) => x), gaps: Object.fromEntries(gaps), pseudoFaults: STMT.pseudoFaults }, null, 2));
|
||||
return;
|
||||
}
|
||||
console.log(`Mojo ${version}, seed ${SEED}, statements: ${STMT.candidates} programs generated; ${STMT.survived} (${pct(STMT.survived, STMT.candidates)}) settled into a program the notation runs to its end (${STMT.firstTry} as generated, the rest after leaving out the lines it refused or stopped on), keeping ${pct(STMT.linesKept, STMT.linesGenerated)} of their lines; ${STMT.orderDependent} depended on the order a SET or map is visited and ${STMT.dropped} could not be settled`);
|
||||
if (STMT.tooMuch) console.log(`${STMT.tooMuch} built program(s) printed more than 4 MiB, too much to compare here`);
|
||||
if (STMT.noMain) console.log(`${STMT.noMain} settled program(s) had no line left outside their routines, so there was no program to build`);
|
||||
console.log(`${STMT.builds} builds (${STMT.shrinkBuilds} more to shrink); ${STMT.outputs} output lines held to the pseudo, up to the first that differs (${STMT.agree} print the same, ${STMT.rounding} differ in their last digits only)`);
|
||||
console.log(`the interpreter ran ${STMT.interpRan} built programs to compare with the compiler and declined ${STMT.interpDeclined}`);
|
||||
console.log(`constructs in the programs built: ${[...coverage].sort((a, b) => b[1] - a[1]).map(([k, v]) => `${k} ${v}`).join(', ')}`);
|
||||
for (const kind of ['rejected', 'stopped', 'differs', 'interpreter', 'checker']) {
|
||||
const ss = sorted.filter((s) => s.kind === kind);
|
||||
if (!ss.length) continue;
|
||||
console.log(`\n${kind}: ${ss.reduce((n, s) => n + s.count, 0)} program(s), ${ss.length} shape(s)`);
|
||||
for (const s of ss) {
|
||||
console.log(` [${s.count}x] ${s.shape}: ${s.example.detail}`);
|
||||
if (s.example.expr) console.log(` at: ${s.example.expr.split('\n')[0]}`);
|
||||
if (s.example.mojo) console.log(` Mojo: ${s.example.mojo}`);
|
||||
console.log(` smallest program (program ${s.example.seedProgram}, shrunk):`);
|
||||
for (const l of s.shrunk.replace(/\n$/, '').split('\n')) console.log(` ${l}`);
|
||||
}
|
||||
}
|
||||
if (gaps.size) { console.log('\nnot translated (the translator says so; those lines were left out):'); for (const [why, k] of [...gaps].sort((a, b) => b[1] - a[1])) console.log(` [${k}x] ${why}`); }
|
||||
for (const u of UPSTREAM) {
|
||||
const fs_ = upstream.filter((f) => u.kind === f.kind && u.test(f));
|
||||
if (fs_.length) console.log(`\nthe compiler's own (${fs_.length} program(s), not counted): ${u.why}\n e.g. ${fs_[0].expr}`);
|
||||
}
|
||||
if (STMT.pseudoFaults.length) {
|
||||
console.log(`\nthe pseudo engine's own faults (${STMT.pseudoFaults.length}; reported, not counted): ${STMT.pseudoFaults[0].message} (line ${STMT.pseudoFaults[0].line})`);
|
||||
for (const l of STMT.pseudoFaults[0].program.replace(/\n$/, '').split('\n')) console.log(` ${l}`);
|
||||
}
|
||||
if (!wholeOurs.length) console.log('\nnothing found');
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Run */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
const version = compilerVersion();
|
||||
if (!version) { console.error('mojo-fuzz: no `mojo` on the PATH; run this as `pixi run mojo-fuzz` (pixi brings the pinned compiler)'); process.exit(2); }
|
||||
if (version !== MOJO_VERSION) { console.error(`mojo-fuzz: the local compiler is Mojo ${version}, and the pin is ${MOJO_VERSION}`); process.exit(2); }
|
||||
const keep = opt('keep');
|
||||
const workdir = keep ? (fs.mkdirSync(keep, { recursive: true }), path.resolve(keep)) : fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-mojo-fuzz-'));
|
||||
let next = 0;
|
||||
await Promise.all(Array.from({ length: JOBS }, async () => { while (next < PROGRAMS) { const n = next++; await (STATEMENTS ? oneWholeProgram : oneProgram)(n, workdir); if (!flag('json') && process.stderr.isTTY) process.stderr.write(`\r${next}/${PROGRAMS} `); } }));
|
||||
if (STATEMENTS) await reportStatements(workdir);
|
||||
if (!keep) fs.rmSync(workdir, { recursive: true, force: true });
|
||||
if (STATEMENTS) process.exit(wholeOurs.length ? 1 : 0);
|
||||
|
||||
/** The outermost operator or built-in of an expression: findings that share it and a message are one shape. */
|
||||
const head = (e) => (/^([A-Z_0-9]+)\(/.exec(e) || [])[1] ?? ((/\b(DIV|MOD|AND|OR|NOT|IN)\b|[&^*/+\-]|[<>=!]=?/.exec(e.replace(/\([^()]*\)/g, '_')) || [])[0] ?? 'value');
|
||||
const upstream = findings.filter((f) => UPSTREAM.some((u) => u.kind === f.kind && u.test(f)));
|
||||
const ours = findings.filter((f) => !upstream.includes(f));
|
||||
const shapes = new Map();
|
||||
for (const f of ours) {
|
||||
const what = f.kind === 'rejected' ? f.detail.replace(/'[^']*'/g, "'…'").replace(/\d+/g, 'N') : head(f.expr);
|
||||
const key = `${f.kind} ${what}`;
|
||||
const s = shapes.get(key) ?? { kind: f.kind, what, count: 0, example: f };
|
||||
s.count++;
|
||||
if (f.expr.length < s.example.expr.length) s.example = f;
|
||||
shapes.set(key, s);
|
||||
}
|
||||
const sorted = [...shapes.values()].sort((a, b) => a.kind.localeCompare(b.kind) || b.count - a.count);
|
||||
if (flag('json')) console.log(JSON.stringify({ compiler: `Mojo ${version}`, seed: SEED, counts, shapes: sorted, upstream, gaps: Object.fromEntries(gaps) }, null, 2));
|
||||
else {
|
||||
console.log(`Mojo ${version}, seed ${SEED}: ${PROGRAMS} programs, ${counts.lines} lines held to the pseudo (${counts.agree} print the same, ${counts.rounding} differ in their last digits only, ${counts.inexact} more by what Mojo's **, exp and log2 lose)`);
|
||||
console.log(`of ${counts.candidates} candidate lines the pseudo's checker refused ${counts.refusedByChecker}, a pseudo run stopped on ${counts.stoppedInPseudo}, and the translator said "not translated" for ${counts.notTranslated}`);
|
||||
for (const kind of ['rejected', 'stopped', 'differs', 'interpreter', 'checker']) {
|
||||
const ss = sorted.filter((s) => s.kind === kind);
|
||||
if (!ss.length) continue;
|
||||
console.log(`\n${kind}: ${ss.reduce((n, s) => n + s.count, 0)} line(s), ${ss.length} shape(s)`);
|
||||
for (const s of ss) { console.log(` [${s.count}x] OUTPUT ${s.example.expr}`); console.log(` ${s.example.detail}`); if (s.example.mojo) console.log(` ${s.example.mojo}`); }
|
||||
}
|
||||
if (gaps.size) { console.log('\nnot translated (the translator says so; not a finding):'); for (const [why, k] of [...gaps].sort((a, b) => b[1] - a[1])) console.log(` [${k}x] ${why}`); }
|
||||
for (const u of UPSTREAM) {
|
||||
const fs_ = upstream.filter((f) => u.kind === f.kind && u.test(f));
|
||||
if (fs_.length) console.log(`\nthe compiler's own (${fs_.length} line(s), not counted): ${u.why}\n e.g. OUTPUT ${fs_.reduce((a, b) => (b.expr.length < a.expr.length ? b : a)).expr}`);
|
||||
}
|
||||
if (!ours.length) console.log('\nnothing found');
|
||||
}
|
||||
process.exit(ours.length ? 1 : 0);
|
||||
Reference in new issue
Block a user