gdp-wired 0.43.0 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -3
- package/dist/core/c/reader.js +22 -7
- package/dist/core/check.js +3 -2
- package/dist/core/cli.d.ts +7 -0
- package/dist/core/cli.js +184 -12
- package/dist/core/counts.d.ts +1 -1
- package/dist/core/counts.js +30 -12
- package/dist/core/data.d.ts +6 -1
- package/dist/core/data.js +894 -152
- package/dist/core/dsl.d.ts +2 -0
- package/dist/core/dsl.js +88 -10
- package/dist/core/effect-prims.d.ts +60 -0
- package/dist/core/effect-prims.js +625 -0
- package/dist/core/effect.d.ts +17 -1
- package/dist/core/effect.js +108 -4
- package/dist/core/ex/harness.exs +2 -0
- package/dist/core/ex/reader.exs +17 -1
- package/dist/core/exists.d.ts +15 -1
- package/dist/core/exists.js +263 -41
- package/dist/core/extract-c.d.ts +2 -0
- package/dist/core/extract-c.js +22 -13
- package/dist/core/extract-check.d.ts +7 -0
- package/dist/core/extract-check.js +57 -4
- package/dist/core/extract-ex.js +5 -5
- package/dist/core/extract-go-check.js +22 -17
- package/dist/core/extract-go.d.ts +3 -0
- package/dist/core/extract-go.js +24 -9
- package/dist/core/extract-jvm.js +68 -16
- package/dist/core/extract-print.js +3 -0
- package/dist/core/extract-py.js +5 -5
- package/dist/core/extract-rb.js +5 -5
- package/dist/core/extract-read.d.ts +8 -0
- package/dist/core/extract-read.js +55 -0
- package/dist/core/extract-rs.js +28 -25
- package/dist/core/extract-shapes.js +20 -0
- package/dist/core/extract-sol.js +23 -12
- package/dist/core/extract-step.d.ts +59 -3
- package/dist/core/extract-step.js +334 -64
- package/dist/core/extract-swift.js +22 -15
- package/dist/core/extract.d.ts +1 -0
- package/dist/core/extract.js +27 -2
- package/dist/core/fresh.d.ts +2 -2
- package/dist/core/fresh.js +7 -3
- package/dist/core/go/reader.go +7 -1
- package/dist/core/infer.js +113 -40
- package/dist/core/jvm/Harness.java +8 -1
- package/dist/core/jvm/JavaReader.java +10 -2
- package/dist/core/jvm/Jvm.java +18 -2
- package/dist/core/jvm/KtReader.java +60 -5
- package/dist/core/machine.d.ts +2 -0
- package/dist/core/machine.js +32 -6
- package/dist/core/py/harness.py +128 -24
- package/dist/core/py/reader.py +50 -5
- package/dist/core/rb/reader.rb +52 -5
- package/dist/core/rs/src/main.rs +10 -0
- package/dist/core/sol/Replay.sol +52 -0
- package/dist/core/sol/harness.py +511 -0
- package/dist/core/solver.js +5 -4
- package/dist/core/system.d.ts +62 -1
- package/dist/core/system.js +274 -6
- package/dist/core/transfer.js +17 -0
- package/dist/core/watch.js +1 -1
- package/dist/interchange/check.d.ts +18 -1
- package/dist/interchange/check.js +19 -12
- package/dist/interchange/cli.d.ts +2 -0
- package/dist/interchange/cli.js +45 -0
- package/dist/interchange/format.d.ts +5 -1
- package/dist/interchange/format.js +25 -9
- package/dist/kernel/cdcl.js +65 -14
- package/dist/kernel/epr.js +35 -5
- package/dist/kernel/kernel.d.ts +2 -0
- package/dist/kernel/kernel.js +67 -40
- package/dist/kernel/prove.d.ts +22 -0
- package/dist/kernel/prove.js +156 -7
- package/dist/kernel/reader.js +3 -2
- package/dist/lean-kernel/emit.d.ts +9 -3
- package/dist/lean-kernel/emit.js +170 -29
- package/dist/lean-kernel/library.js +1 -1
- package/package.json +12 -3
package/dist/core/exists.js
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// value before, shifts of it, the request's values and the state before; each is chosen to satisfy P at every
|
|
15
15
|
// counterexample so far, per case of the step (the conditions its updates split on: the request's clause, say).
|
|
16
16
|
// A law under a `forall` key takes witness terms only (a ghost per key would be a map).
|
|
17
|
-
import { carriedView, evalC, evalE, interpret, n as k, walk, x as v } from "./machine.js";
|
|
17
|
+
import { carriedView, evalC, evalE, fastStep, interpret, n as k, walk, x as v } from "./machine.js";
|
|
18
18
|
import { clock, proveLaw, proveLaws, withDeadline, withinDeadline } from "../kernel/prove.js";
|
|
19
19
|
import { env } from "./solver.js";
|
|
20
20
|
import { ghostedMachine, ghostedOk, substC } from "../kernel/kernel.js";
|
|
@@ -162,7 +162,12 @@ const atomsOf2 = (c) => (c.op === 'and' || c.op === 'or' ? c.xs.flatMap(atomsOf2
|
|
|
162
162
|
// taken as often as any other. Each state's step from the state it was first reached from is kept: `pathTo`.)
|
|
163
163
|
const pathTo = new WeakMap();
|
|
164
164
|
export function small(m, p, b, cap = 3000, work = 300_000, walks = 0) {
|
|
165
|
+
// (past a thousand steps, through the generated JavaScript, fastStep: the interpreter's states, as compile.test.ts holds
|
|
166
|
+
// them; a small machine's few steps cost less than its compiling)
|
|
165
167
|
const run = interpret(m), start = run.init(p);
|
|
168
|
+
let steps = 0, fast = run.step;
|
|
169
|
+
const step = (p2, st, r) => { if (++steps === 1000)
|
|
170
|
+
fast = fastStep(m) ?? run.step; return fast(p2, st, r); };
|
|
166
171
|
const subsets = Array.from({ length: 1 << b }, (_, s) => new Map(Array.from({ length: b }, (_, i) => i).filter(i => (s >> i) & 1).map(i => [i, 1])));
|
|
167
172
|
// (an input the assumption keeps below a few values, an action's, over all of them; the rest below b)
|
|
168
173
|
const below = (i) => Math.max(b, ...(m.assume ? atomsOf2(m.assume) : []).flatMap(c => (c.op === '<' && c.a.op === 'var' && c.a.name === i && c.b.op === 'const' && c.b.value <= 8 ? [c.b.value] : [])));
|
|
@@ -182,7 +187,7 @@ export function small(m, p, b, cap = 3000, work = 300_000, walks = 0) {
|
|
|
182
187
|
break;
|
|
183
188
|
let next;
|
|
184
189
|
try {
|
|
185
|
-
next =
|
|
190
|
+
next = step(p, st, r);
|
|
186
191
|
}
|
|
187
192
|
catch {
|
|
188
193
|
continue;
|
|
@@ -207,7 +212,7 @@ export function small(m, p, b, cap = 3000, work = 300_000, walks = 0) {
|
|
|
207
212
|
const r = reqs[rnd(reqs.length)];
|
|
208
213
|
let next;
|
|
209
214
|
try {
|
|
210
|
-
next =
|
|
215
|
+
next = step(p, st, r);
|
|
211
216
|
}
|
|
212
217
|
catch {
|
|
213
218
|
continue;
|
|
@@ -314,7 +319,9 @@ const shapeOf = (body, vars) => {
|
|
|
314
319
|
byVars.set(id, out);
|
|
315
320
|
return out;
|
|
316
321
|
};
|
|
317
|
-
|
|
322
|
+
// (`quick`: a run's watch, which reads only whether it holds: first 0, then the points without the cells, a witness there
|
|
323
|
+
// one; the verdict and its cap as without)
|
|
324
|
+
export function someHolds(body, vars, env, st, m, quick = false) {
|
|
318
325
|
const { groups, atoms, linearOnly, reads } = shapeOf(body, vars);
|
|
319
326
|
const value = (e) => { try {
|
|
320
327
|
return evalE(e, env, st, m);
|
|
@@ -323,12 +330,7 @@ export function someHolds(body, vars, env, st, m) {
|
|
|
323
330
|
return undefined;
|
|
324
331
|
} };
|
|
325
332
|
// A bound natural read as a map's key: the cells the maps hold, and one past them, are every case a read gives.
|
|
326
|
-
const
|
|
327
|
-
if (reads)
|
|
328
|
-
for (const c of Object.values(st.maps))
|
|
329
|
-
for (const key of c.keys())
|
|
330
|
-
cells.add(key);
|
|
331
|
-
const points = (g) => {
|
|
333
|
+
const points = (g, cells) => {
|
|
332
334
|
const ps = new Set([0, 1, ...cells, ...[...cells].map(c => c + 1)]);
|
|
333
335
|
for (const a of atoms) {
|
|
334
336
|
const s = a.k[g[0]] ?? 0, s2 = g.length === 2 ? (a.k[g[1]] ?? 0) : 0;
|
|
@@ -352,8 +354,7 @@ export function someHolds(body, vars, env, st, m) {
|
|
|
352
354
|
catch {
|
|
353
355
|
return undefined;
|
|
354
356
|
} };
|
|
355
|
-
|
|
356
|
-
let tried = 0, unknown = false;
|
|
357
|
+
let tried = 0, unknown = false, lists = [];
|
|
357
358
|
const go = (i, w) => {
|
|
358
359
|
if (i === groups.length) {
|
|
359
360
|
tried++;
|
|
@@ -371,6 +372,26 @@ export function someHolds(body, vars, env, st, m) {
|
|
|
371
372
|
}
|
|
372
373
|
return undefined;
|
|
373
374
|
};
|
|
375
|
+
// (first the points without the cells: P's guard false at a tuple, as at most of a law's, holds at 0 at once; a witness
|
|
376
|
+
// there is one; then every point, as before, its verdict and cap the same)
|
|
377
|
+
const none = new Set();
|
|
378
|
+
if (reads && quick) {
|
|
379
|
+
const w0 = Object.assign({}, ...groups.map(g => at(g, 0)));
|
|
380
|
+
if (test(w0))
|
|
381
|
+
return { holds: true, witness: w0 };
|
|
382
|
+
lists = groups.map(g => points(g, none));
|
|
383
|
+
const near = go(0, {});
|
|
384
|
+
if (near)
|
|
385
|
+
return { holds: true, witness: near };
|
|
386
|
+
}
|
|
387
|
+
const cells = new Set();
|
|
388
|
+
if (reads)
|
|
389
|
+
for (const c of Object.values(st.maps))
|
|
390
|
+
for (const key of c.keys())
|
|
391
|
+
cells.add(key);
|
|
392
|
+
lists = groups.map(g => points(g, cells));
|
|
393
|
+
tried = 0;
|
|
394
|
+
unknown = false;
|
|
374
395
|
const hit = go(0, {});
|
|
375
396
|
if (hit)
|
|
376
397
|
return { holds: true, witness: hit };
|
|
@@ -505,6 +526,144 @@ function planOf(m, law) {
|
|
|
505
526
|
groups.forEach((g, i) => { starts[i] = [g.map(n => w.ghost[n]?.start ?? k(0))]; steps[i] = [g.map(n => w.ghost[n]?.step ?? v(n))]; });
|
|
506
527
|
return { law, groups, terms, starts, steps, maps: [], pick: w?.ghost ? { mode: 'ghost', start: groups.map(() => 0), cases: new Map() } : { mode: 'term', at: groups.map(() => 0) }, seen: { term: [], start: [], step: new Map() }, given };
|
|
507
528
|
}
|
|
529
|
+
// A ghost map (lean-kernel/GhostMap.lean; kernel.ts's Ghosted): for a law about every key with one value ∃x. P, a map W
|
|
530
|
+
// the step keeps, written where a write of the rule sets a cell (its value with the cell's own read at 0 is 1), at that
|
|
531
|
+
// write's key, to a request's value outside the key; the law proven is P with x at W[k] for the law's keys k in the write
|
|
532
|
+
// key's places, beside the ghost's own law: where the written map's cell is set (and the law's guards hold), P at W[k].
|
|
533
|
+
// Every such place and value is a candidate; those false on the small instances' states are never tried
|
|
534
|
+
// (core/counts.ts's), the rest in turn by the kernel's goals.
|
|
535
|
+
// A write's provenance: where map M's cell is set by a write at a key of inputs, a ghost map G at that key holding another
|
|
536
|
+
// input of the request, val, read at that key (G[i·N + p] := r where DbResponseSent[i·N + p] is set); G's own law, where
|
|
537
|
+
// M's cell is set, what the write's guard says of val there, and the cells the same guard sets at keys val is in (T[i·N +
|
|
538
|
+
// n] beside DbRequestSent[i·N + r]), at G's cell. None where the guard says nothing of val.
|
|
539
|
+
const keyedOf = (m) => {
|
|
540
|
+
const keyed = new Set();
|
|
541
|
+
walk([m.step, m.assume ?? null], o => { if (o.op === 'get')
|
|
542
|
+
for (const n of namesOf(o.key))
|
|
543
|
+
keyed.add(n); });
|
|
544
|
+
for (const w2 of Object.values(m.step.maps ?? {}))
|
|
545
|
+
if (!('each' in w2))
|
|
546
|
+
for (const n of namesOf(w2.key))
|
|
547
|
+
keyed.add(n);
|
|
548
|
+
return keyed;
|
|
549
|
+
};
|
|
550
|
+
function provenanceOf(m, mp2, val2, taken, keyed = keyedOf(m)) {
|
|
551
|
+
const w2 = m.step.maps?.[mp2];
|
|
552
|
+
if (!w2 || 'each' in w2)
|
|
553
|
+
return undefined;
|
|
554
|
+
const kv2 = namesOf(w2.key), flags = Object.fromEntries(Object.keys(m.step.flags ?? {}).map(f => [f, { op: 'flag', name: f }]));
|
|
555
|
+
if (!kv2.length || !kv2.every(n => m.inputs.includes(n) && keyed.has(n)) || kv2.includes(val2) || !keyed.has(val2))
|
|
556
|
+
return undefined;
|
|
557
|
+
const setOf = (mp, w) => substC({ op: '<=', a: k(1), b: w.value }, { vars: {}, maps: { [mp]: () => k(0) }, flags });
|
|
558
|
+
const set2 = setOf(mp2, w2);
|
|
559
|
+
const conj = (c, depth = 0) => (c.op === 'and' ? c.xs.flatMap(y => conj(y, depth)) : c.op === 'flag' && depth < 3 && m.step.flags?.[c.name] && m.step.flags[c.name].op !== 'all' ? conj(m.step.flags[c.name], depth + 1) : [c]);
|
|
560
|
+
const lits = [...new Set([...JSON.stringify(w2.value).matchAll(/"op":"flag","name":"([^"]+)"/g)].map(x => x[1]))].flatMap(f => conj({ op: 'flag', name: f }));
|
|
561
|
+
let gname = `${mp2}$g`;
|
|
562
|
+
for (let j = 2; taken.has(gname); j++)
|
|
563
|
+
gname = `${mp2}$g${j}`;
|
|
564
|
+
const ks = kv2.map((_, j) => `g${j}$key`), G = (t) => ({ op: 'get', map: gname, key: t }), K2 = substVarsE(w2.key, Object.fromEntries(kv2.map((n, j) => [n, v(ks[j])])));
|
|
565
|
+
const sub = { [val2]: G(K2), ...Object.fromEntries(kv2.map((n, j) => [n, v(ks[j])])) }, mine = (ns) => ns.includes(val2) && ns.every(n => n === val2 || kv2.includes(n));
|
|
566
|
+
const says = lits.filter(c => c.op !== 'flag' && mine(namesOf(c))).map(c => substVars(c, sub));
|
|
567
|
+
// (the cells the same guard sets beside it)
|
|
568
|
+
for (const [mp3, w3] of Object.entries(m.step.maps ?? {}))
|
|
569
|
+
if (mp3 !== mp2 && !('each' in w3) && mine(namesOf(w3.key)) && key(setOf(mp3, w3)) === key(set2))
|
|
570
|
+
says.push({ op: '<=', a: k(1), b: { op: 'get', map: mp3, key: substVarsE(w3.key, sub) } });
|
|
571
|
+
if (!says.length)
|
|
572
|
+
return undefined;
|
|
573
|
+
const inv = { name: `${gname} where ${mp2} is set`, forall: ks[0], ...(ks.length > 1 ? { keys: ks.slice(1) } : {}), holds: { op: 'or', xs: [{ op: 'not', x: { op: '<=', a: k(1), b: { op: 'get', map: mp2, key: K2 } } }, says.length === 1 ? says[0] : { op: 'and', xs: says }] } };
|
|
574
|
+
return { name: gname, write: { key: w2.key, value: { op: 'if', c: set2, then: v(val2), else: G(w2.key) } }, inv };
|
|
575
|
+
}
|
|
576
|
+
// The writes that set a map's cell from 0: the cell as a term of the request alone and the condition it is set under, a
|
|
577
|
+
// write at one key (its key), or at every key where its value reads `each = τ`, τ over the request alone.
|
|
578
|
+
function settersOf(m, mp) {
|
|
579
|
+
const w = m.step.maps?.[mp];
|
|
580
|
+
if (!w)
|
|
581
|
+
return [];
|
|
582
|
+
const flags = Object.fromEntries(Object.keys(m.step.flags ?? {}).map(f => [f, { op: 'flag', name: f }]));
|
|
583
|
+
const setAt = (val) => substC({ op: '<=', a: k(1), b: val }, { vars: {}, maps: { [mp]: () => k(0) }, flags });
|
|
584
|
+
const reqOnly = (t) => !readsMaps(t) && namesOf(t).length > 0 && namesOf(t).every(n => m.inputs.includes(n));
|
|
585
|
+
if (!('each' in w))
|
|
586
|
+
return reqOnly(w.key) ? [{ at: w.key, set: setAt(w.value) }] : [];
|
|
587
|
+
const ats = new Map();
|
|
588
|
+
walk(w.value, o => { if (o.op === '=')
|
|
589
|
+
for (const [a, b] of [[o.a, o.b], [o.b, o.a]])
|
|
590
|
+
if (a.op === 'var' && a.name === w.each && reqOnly(b))
|
|
591
|
+
ats.set(key(b), b); });
|
|
592
|
+
return [...ats.values()].map(at => ({ at, set: setAt(substVarsE(w.value, { [w.each]: at })) }));
|
|
593
|
+
}
|
|
594
|
+
// The supports a law made plain by binderGhostsOf may need, those the small instances keep: each ghost below the bound the
|
|
595
|
+
// assumption puts on the input it holds (G[k] := n, n < 64: G[k] < 64); each disjunct with its names at their terms,
|
|
596
|
+
// where one or two control facts hold (a scalar at a value, a cell at the law's key of a map written at one input's
|
|
597
|
+
// key): what keeps a witness before the law reads it (no_lost_keys: Owner at Owner$n[k] where Owned[k] before start);
|
|
598
|
+
// and each map empty at every key while a scalar holds a value (no transfer before start).
|
|
599
|
+
function ghostSupports(m, law, c, gm) {
|
|
600
|
+
const L = law.forall, out = [], cmp = (op, a, b) => ({ op, a, b }), lim = (m.assume ? atomsOf2(m.assume) : []);
|
|
601
|
+
for (const [name, w] of Object.entries(c.maps)) {
|
|
602
|
+
const val = w.write.value, i = val.op === 'if' && val.then.op === 'var' ? val.then.name : undefined;
|
|
603
|
+
const bound = lim.find(a => a.op === '<' && a.a.op === 'var' && a.a.name === i && a.b.op === 'const');
|
|
604
|
+
if (bound)
|
|
605
|
+
out.push({ name: `${name} below ${showE(bound.b)}`, forall: L, holds: cmp('<', { op: 'get', map: name, key: v(L) }, bound.b) });
|
|
606
|
+
}
|
|
607
|
+
const sts = smallOf(m).flatMap(q => q.sts), seen = (n) => [...new Set(sts.map(st => st.scalars[n] ?? 0))].slice(0, 3);
|
|
608
|
+
const facts = [...Object.keys(m.state).flatMap(n => seen(n).map(x => cmp('=', v(n), k(x)))),
|
|
609
|
+
...Object.entries(m.step.maps ?? {}).flatMap(([mp, w]) => ('each' in w || w.key.op !== 'var' ? [] : [0, 1].map(x => cmp('=', { op: 'get', map: mp, key: v(L) }, k(x)))))];
|
|
610
|
+
const ds = (law.holds.op === 'or' ? law.holds.xs : [law.holds]).flatMap(d => (d.op === 'or' ? d.xs : [d])).filter(d => law.some.some(x => namesOf(d).includes(x)));
|
|
611
|
+
const holds = (g, d) => ({ op: 'or', xs: [{ op: 'not', x: g.length === 1 ? g[0] : { op: 'and', xs: g } }, d] });
|
|
612
|
+
for (const d of ds)
|
|
613
|
+
for (const [j, f] of facts.entries())
|
|
614
|
+
for (const g of [[f], ...facts.slice(j + 1).filter(f2 => key(f2.a) !== key(f.a)).map(f2 => [f, f2])])
|
|
615
|
+
out.push({ name: `${showC(holds(g, d)).slice(0, 80)} (${out.length})`, forall: L, holds: holds(g, substC(d, { vars: c.sub, maps: {}, flags: {} })) });
|
|
616
|
+
for (const f of facts.filter(f2 => !readsMaps(f2)))
|
|
617
|
+
for (const mp of Object.keys(m.maps ?? {}))
|
|
618
|
+
out.push({ name: `${mp} empty where ${showC(f)} (${out.length})`, forall: L, holds: holds([f], cmp('=', { op: 'get', map: mp, key: v(L) }, k(0))) });
|
|
619
|
+
return out.filter(l2 => { try {
|
|
620
|
+
return trueSmall(m, [l2], gm);
|
|
621
|
+
}
|
|
622
|
+
catch {
|
|
623
|
+
return false;
|
|
624
|
+
} }).slice(0, 24);
|
|
625
|
+
}
|
|
626
|
+
// Ghost maps for a law about one key with several bound names (a disjunction of ∃s, each name in its own disjuncts): for
|
|
627
|
+
// each name, a write that sets a cell the law reads at a key holding the name: the name the request's value the write's
|
|
628
|
+
// key holds there (Owner[N·k + x] set at N·k + n: x is n), or the cell itself (TransferMsg[z] set at τ: z is τ, the
|
|
629
|
+
// law's key one input of τ and each other a ghost map at it: G[k] := n where the cell is set). Each name a term over the
|
|
630
|
+
// ghosts (kernel.ts's someOfGhostsOk reads them so); every choice of one a name, at most 64.
|
|
631
|
+
function binderGhostsOf(m, law, taken) {
|
|
632
|
+
const L = law.forall, xs = law.some;
|
|
633
|
+
if (law.keys?.length || xs.some(x => x.endsWith('$neg')))
|
|
634
|
+
return [];
|
|
635
|
+
const options = xs.map((x, jx) => {
|
|
636
|
+
const out = [], reads = [];
|
|
637
|
+
walk(law.holds, o => { if (o.op === 'get' && namesOf(o.key).includes(x))
|
|
638
|
+
reads.push({ mp: o.map, at: o.key }); });
|
|
639
|
+
for (const { mp, at: kappa } of reads)
|
|
640
|
+
for (const { at: tau, set } of settersOf(m, mp))
|
|
641
|
+
for (const ki of namesOf(tau)) {
|
|
642
|
+
const ghostOf = (i) => {
|
|
643
|
+
let name = `${mp}$${i}${jx || ''}`;
|
|
644
|
+
for (let j = 2; taken.has(name); j++)
|
|
645
|
+
name = `${mp}$${i}${jx || ''}~${j}`;
|
|
646
|
+
return [name, { start: 0, write: { key: v(ki), value: { op: 'if', c: set, then: v(i), else: { op: 'get', map: name, key: v(ki) } } } }];
|
|
647
|
+
};
|
|
648
|
+
const others = namesOf(tau).filter(i => i !== ki);
|
|
649
|
+
if (kappa.op === 'var' && kappa.name === x) {
|
|
650
|
+
const gs = others.map(i => [i, ghostOf(i)]);
|
|
651
|
+
out.push({ maps: Object.fromEntries(gs.map(([, q]) => q)), term: substVarsE(tau, { [ki]: v(L), ...Object.fromEntries(gs.map(([i, q]) => [i, { op: 'get', map: q[0], key: v(L) }])) }) });
|
|
652
|
+
}
|
|
653
|
+
else
|
|
654
|
+
for (const i of others) {
|
|
655
|
+
if (key(substVarsE(kappa, { [L]: v(ki), [x]: v(i) })) !== key(tau))
|
|
656
|
+
continue;
|
|
657
|
+
const q = ghostOf(i);
|
|
658
|
+
out.push({ maps: { [q[0]]: q[1] }, term: { op: 'get', map: q[0], key: v(L) } });
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return out;
|
|
662
|
+
});
|
|
663
|
+
let combos = [{ maps: {}, sub: {} }];
|
|
664
|
+
xs.forEach((x, j) => { combos = combos.flatMap(c => options[j].map(o => ({ maps: { ...c.maps, ...o.maps }, sub: { ...c.sub, [x]: o.term } }))).slice(0, 64); });
|
|
665
|
+
return combos;
|
|
666
|
+
}
|
|
508
667
|
function mapGhostsOf(m, law, taken) {
|
|
509
668
|
const x = law.some[0], names = [law.forall, ...(law.keys ?? [])];
|
|
510
669
|
const xs = law.holds.op === 'or' ? law.holds.xs : [law.holds], P = xs.filter(d => namesOf(d).includes(x)), rest = xs.filter(d => !namesOf(d).includes(x));
|
|
@@ -522,14 +681,7 @@ function mapGhostsOf(m, law, taken) {
|
|
|
522
681
|
// (each write key's input at a different one of the law's keys)
|
|
523
682
|
const injections = kv.reduce((acc) => acc.flatMap(a => names.filter(n => !a.includes(n)).map(n => [...a, n])), [[]]);
|
|
524
683
|
// (a value an action selects, compared with constants alone, is no witness)
|
|
525
|
-
const keyed =
|
|
526
|
-
walk([m.step, m.assume ?? null], o => { if (o.op === 'get')
|
|
527
|
-
for (const n of namesOf(o.key))
|
|
528
|
-
keyed.add(n); });
|
|
529
|
-
for (const w2 of Object.values(m.step.maps ?? {}))
|
|
530
|
-
if (!('each' in w2))
|
|
531
|
-
for (const n of namesOf(w2.key))
|
|
532
|
-
keyed.add(n);
|
|
684
|
+
const keyed = keyedOf(m);
|
|
533
685
|
const action = (i) => !keyed.has(i);
|
|
534
686
|
const ghost = (val, inner) => {
|
|
535
687
|
for (const inj of injections) {
|
|
@@ -548,31 +700,100 @@ function mapGhostsOf(m, law, taken) {
|
|
|
548
700
|
// (a ghost of a ghost, after those: the value another write's ghost holds, read at that write's key in this step's
|
|
549
701
|
// request: G[i·N + p] := r where DbResponseSent[i·N + p] is set, W[n·N + p] := G[i·N + p] where ResponseSent is.
|
|
550
702
|
// G's own law: where its write's cell is set, what the write's guard says of r, at G's cell)
|
|
551
|
-
for (const
|
|
552
|
-
if (mp2 === mp
|
|
553
|
-
continue;
|
|
554
|
-
const kv2 = namesOf(w2.key);
|
|
555
|
-
if (!kv2.length || !kv2.every(n => m.inputs.includes(n) && keyed.has(n)))
|
|
703
|
+
for (const mp2 of Object.keys(m.step.maps ?? {})) {
|
|
704
|
+
if (mp2 === mp)
|
|
556
705
|
continue;
|
|
557
|
-
const
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
const ks = kv2.map((_, j) => `g${j}$key`), G = (t) => ({ op: 'get', map: gname, key: t }), K2 = substVarsE(w2.key, Object.fromEntries(kv2.map((n, j) => [n, v(ks[j])])));
|
|
565
|
-
const says = lits.filter(c => c.op !== 'flag' && namesOf(c).includes(val2) && namesOf(c).every(n => n === val2 || kv2.includes(n))).map(c => substVars(c, { [val2]: G(K2), ...Object.fromEntries(kv2.map((n, j) => [n, v(ks[j])])) }));
|
|
566
|
-
if (!says.length)
|
|
567
|
-
continue;
|
|
568
|
-
const inv = { name: `${gname} where ${mp2} is set`, forall: ks[0], ...(ks.length > 1 ? { keys: ks.slice(1) } : {}), holds: { op: 'or', xs: [{ op: 'not', x: { op: '<=', a: k(1), b: { op: 'get', map: mp2, key: K2 } } }, says.length === 1 ? says[0] : { op: 'and', xs: says }] } };
|
|
569
|
-
ghost(G(w2.key), { name: gname, write: { key: w2.key, value: { op: 'if', c: set2, then: v(val2), else: G(w2.key) } }, inv });
|
|
570
|
-
}
|
|
706
|
+
const w2 = m.step.maps[mp2];
|
|
707
|
+
if (!('each' in w2))
|
|
708
|
+
for (const val2 of m.inputs) {
|
|
709
|
+
const g = provenanceOf(m, mp2, val2, taken, keyed);
|
|
710
|
+
if (g)
|
|
711
|
+
ghost({ op: 'get', map: g.name, key: w2.key }, g);
|
|
712
|
+
}
|
|
571
713
|
}
|
|
572
714
|
}
|
|
573
715
|
return out;
|
|
574
716
|
}
|
|
575
717
|
const takenOf = (m) => new Set([...Object.keys(m.maps ?? {}), ...(m.inputMaps ?? []), ...Object.keys(m.fns ?? {})]);
|
|
718
|
+
// The laws about every key with one value ∃x. P made plain by their ghost maps (Skolem functions the step keeps): for
|
|
719
|
+
// each, the first ghost map whose laws hold on the small instances, as proveSome's toMap picks it (the same names), and
|
|
720
|
+
// the law with x at W[k] beside the ghosts' own laws. What the ∀ search takes: a set it proves over the machine with the
|
|
721
|
+
// ghosts, the ghosts the original's (`ok`, kernel.ts's ghostedOk), proves each ∃ law, x at its ghost's cell (GhostMap.lean's
|
|
722
|
+
// forall_some_of_ghostmap); proveSome's ghost maps are the same, for a proof again of the laws as stated.
|
|
723
|
+
// (`ghost`: the ghosts added to a machine the search derives from the rule's (its sums, its divisors), unchecked: `ok`
|
|
724
|
+
// checks them against the original; the laws as proveSome states them, so that the goals the search settled are its again)
|
|
725
|
+
export function skolemOf(m, laws) {
|
|
726
|
+
const taken = takenOf(m), out = [], gs = [];
|
|
727
|
+
const parts = [], one = (m2, q) => ({ orig: m2, vars: {}, maps: { [q.name]: { start: 0, write: q.write } } });
|
|
728
|
+
const ghost = (m2, more = []) => [...parts, ...more].reduce((acc, q) => ghostedMachine(one(acc, q)), m2);
|
|
729
|
+
const plains = {}, witnesses = {}, cands = [];
|
|
730
|
+
const own = (g) => [...(g.inner ? [g.inner] : []), g];
|
|
731
|
+
for (const l of laws) {
|
|
732
|
+
if (!l.some?.length) {
|
|
733
|
+
out.push(l);
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
if (!l.forall || l.witness)
|
|
737
|
+
return undefined;
|
|
738
|
+
// (the value named apart from the machine's names (proveSome renames it), and no binder inside the law that the ghost's
|
|
739
|
+
// cell, read at the law's keys, would be captured by: else no plain form)
|
|
740
|
+
const binders = new Set();
|
|
741
|
+
walk(l.holds, o => { if (o.op === 'all')
|
|
742
|
+
for (const b of [o.key, ...(o.keys ?? [])])
|
|
743
|
+
binders.add(b); });
|
|
744
|
+
if (l.some.some(x => [...m.params, ...m.inputs, ...Object.keys(m.state)].includes(x)) || [l.forall, ...(l.keys ?? [])].some(x => binders.has(x)))
|
|
745
|
+
return undefined;
|
|
746
|
+
const { some: _s, ...plain } = l;
|
|
747
|
+
void _s;
|
|
748
|
+
// (several bound names (a disjunction of ∃s): each a term over ghost maps, binderGhostsOf's, the first whose plain law
|
|
749
|
+
// holds on the small instances)
|
|
750
|
+
if (l.some.length > 1) {
|
|
751
|
+
const holdsOf = (sub) => substC(l.holds, { vars: sub, maps: {}, flags: {} });
|
|
752
|
+
const c = binderGhostsOf(m, l, taken).find(c2 => { try {
|
|
753
|
+
return trueSmall(m, [{ ...plain, holds: holdsOf(c2.sub) }], ghost(m, Object.entries(c2.maps).map(([name, w]) => ({ name, write: w.write }))));
|
|
754
|
+
}
|
|
755
|
+
catch {
|
|
756
|
+
return false;
|
|
757
|
+
} });
|
|
758
|
+
if (!c)
|
|
759
|
+
return undefined;
|
|
760
|
+
for (const [name, w] of Object.entries(c.maps)) {
|
|
761
|
+
parts.push({ name, write: w.write });
|
|
762
|
+
taken.add(name);
|
|
763
|
+
}
|
|
764
|
+
const pl = { ...plain, holds: holdsOf(c.sub) };
|
|
765
|
+
out.push(pl);
|
|
766
|
+
plains[l.name] = { plain: pl, sub: c.sub };
|
|
767
|
+
witnesses[l.name] = [...l.some.map(x => `${x} = ${showE(c.sub[x])}`), ...Object.entries(c.maps).map(([name, w]) => { const u = w.write; return `${name}[${showE(u.key)}] := ${showE(u.value)}`; })].join(', ');
|
|
768
|
+
cands.push(...ghostSupports(m, l, c, ghost(m)));
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
const g = mapGhostsOf(m, l, taken).find(c => { try {
|
|
772
|
+
return trueSmall(m, [...(c.inner ? [c.inner.inv] : []), c.inv, { ...l, holds: c.holds, some: undefined }], ghost(m, own(c)));
|
|
773
|
+
}
|
|
774
|
+
catch {
|
|
775
|
+
return false;
|
|
776
|
+
} });
|
|
777
|
+
if (!g)
|
|
778
|
+
return undefined;
|
|
779
|
+
gs.push(g);
|
|
780
|
+
parts.push(...own(g));
|
|
781
|
+
for (const q of own(g))
|
|
782
|
+
taken.add(q.name);
|
|
783
|
+
const pl = { ...plain, holds: g.holds };
|
|
784
|
+
out.push(pl, ...(g.inner ? [g.inner.inv] : []), g.inv);
|
|
785
|
+
plains[l.name] = { plain: pl, sub: { [l.some[0]]: { op: 'get', map: g.name, key: g.at } } };
|
|
786
|
+
witnesses[l.name] = `w = ${g.name}[${showE(g.at)}], ${g.name}[${showE(g.write.key)}] := ${showE(g.write.value)}`;
|
|
787
|
+
}
|
|
788
|
+
// (each ghost one of the machine it is added to, as kernel.ts's ghostedOk reads it)
|
|
789
|
+
const ok = (m2) => { let at = m2; for (const q of parts) {
|
|
790
|
+
const o = one(at, q);
|
|
791
|
+
if (!ghostedOk(o))
|
|
792
|
+
return false;
|
|
793
|
+
at = ghostedMachine(o);
|
|
794
|
+
} return true; };
|
|
795
|
+
return parts.length ? { ghost: m2 => ghost(m2), laws: out, ok, witnesses, maps: Object.fromEntries(parts.map(q => [q.name, { start: 0, write: q.write }])), plains, cands } : undefined;
|
|
796
|
+
}
|
|
576
797
|
const firstPicks = new Map();
|
|
577
798
|
// The machine with a ghost map (kernel.ts's ghostedMachine of Ghosted, whose ghostedOk it must pass).
|
|
578
799
|
const withMap = (m, g) => {
|
|
@@ -775,8 +996,9 @@ export function proveSome(m, laws, opts = {}) {
|
|
|
775
996
|
}
|
|
776
997
|
for (let round = 0;; round++) {
|
|
777
998
|
const now = current(), t0 = performance.now(), r = round === 0 && first ? { proven: false, ...first }
|
|
778
|
-
// (a ghost map's goals within 2 s: a search asks them with every candidate set, and one without its supports can take
|
|
779
|
-
|
|
999
|
+
// (a ghost map's goals within 2 s: a search asks them with every candidate set, and one without its supports can take
|
|
1000
|
+
// minutes; `mapMs` where the supports are the search's, found over the same map)
|
|
1001
|
+
: p.pick.mode === 'map' ? withDeadline(clock() + (opts.mapMs ?? 2_000), () => proveLaw(now.machine, now.laws, i)) : proveLaw(now.machine, now.laws, i);
|
|
780
1002
|
if (log)
|
|
781
1003
|
console.error(`exists: pass ${pass} round ${round} "${p.law.name}" ${p.pick.mode} ${describe(p, cases).slice(0, 160)}: ${r.proven ? 'proven' : `${r.goal} ${r.stuck}`} (${Math.round(performance.now() - t0)} ms)`);
|
|
782
1004
|
if (r.proven)
|
package/dist/core/extract-c.d.ts
CHANGED
package/dist/core/extract-c.js
CHANGED
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
// operations but the mask above, the comma operator, an assignment or ++ inside an expression, and sizeof.
|
|
27
27
|
import { fromModel } from "./extract.js";
|
|
28
28
|
import { Refusal, pathOf } from "./extract-read.js";
|
|
29
|
-
import { stubbed } from "./extract-step.js";
|
|
29
|
+
import { buildCheck, builtEvidence, compileLimit, stubbed } from "./extract-step.js";
|
|
30
30
|
import { goNames, readGo, unsignedDomains } from "./extract-go.js";
|
|
31
31
|
import { speak } from "./extract-go-check.js";
|
|
32
32
|
import { clangDump, flagsFor, node, readC } from "./c/reader.js";
|
|
@@ -85,7 +85,7 @@ export function extractC(at, given = {}, cpp = false) {
|
|
|
85
85
|
;
|
|
86
86
|
({ c, flags } = readCFile(file, opts, cpp));
|
|
87
87
|
c.pkg.dir = node('node:path').resolve(file).replace(/\/[^/]*$/, '');
|
|
88
|
-
read = readGo(c.pkg, { entry: c.entry, methods: opts.functions?.map(f => f.split('/')[0]) ?? c.methods, ...(opts.onError ? { onError: opts.onError } : {}), ...(opts.fails ? { fails: opts.fails } : {}), ...(opts.strict ? { strict: true } : {}) });
|
|
88
|
+
read = readGo(c.pkg, { entry: c.entry, methods: opts.functions?.map(f => f.split('/')[0]) ?? c.methods, ...(opts.onError ? { onError: opts.onError } : {}), ...(opts.fails ? { fails: opts.fails } : {}), ...(opts.strict ? { strict: true } : {}), ...(opts.answers ? { answers: opts.answers } : {}), ...(opts.skip ? { skip: opts.skip } : {}) });
|
|
89
89
|
}
|
|
90
90
|
catch (e) {
|
|
91
91
|
if (e instanceof Refusal)
|
|
@@ -162,6 +162,8 @@ export function harnessC(file, c, read, opts, cpp) {
|
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
const frames = read.leaves.filter(l => l.kind === 'frame' && !T(l.t).mutex);
|
|
165
|
+
// (the longest method name the request names, in UTF-8 bytes, its buffer: a name of any length is called)
|
|
166
|
+
const M = Math.max(256, ...read.methods.map(x => new TextEncoder().encode(x.name).length + 1));
|
|
165
167
|
const calls = read.methods.map(meth => {
|
|
166
168
|
const lines = [], args = ['o'], outs = [];
|
|
167
169
|
meth.params.forEach((pa, i) => {
|
|
@@ -337,8 +339,8 @@ int main(int argc, char **argv) {
|
|
|
337
339
|
}
|
|
338
340
|
else if (op && !strncmp(op, "\\"get\\"", 5)) { gdp_putf("{\\"state\\":"); gdp_get(objs[k - 1]); gdp_putf("}"); }
|
|
339
341
|
else if (op && !strncmp(op, "\\"call\\"", 6)) {
|
|
340
|
-
const char *mv = gdp_key(line, "m"), *av = gdp_key(line, "a"); char m[
|
|
341
|
-
if (mv && *mv == '"') { const char *e = strchr(mv + 1, '"'); size_t n = e ? (size_t)(e - mv - 1) : 0; if (n >
|
|
342
|
+
const char *mv = gdp_key(line, "m"), *av = gdp_key(line, "a"); char m[${M}] = {0};
|
|
343
|
+
if (mv && *mv == '"') { const char *e = strchr(mv + 1, '"'); size_t n = e ? (size_t)(e - mv - 1) : 0; if (n > ${M - 1}) n = ${M - 1}; memcpy(m, mv + 1, n); }
|
|
342
344
|
gdp_putf("{\\"ok\\":true");${read.answers?.length ? `
|
|
343
345
|
gdp_answers = gdp_key(line, "answers");` : ''}
|
|
344
346
|
const char *bad = gdp_call(objs[k - 1], m, av ? av : "[]");
|
|
@@ -386,23 +388,30 @@ export function checkC(file, flags, c, read, r, sorts, opts, cpp) {
|
|
|
386
388
|
}
|
|
387
389
|
// The same flags; undefined behavior trapped (the rule refuses it, so the code must fail there too); another
|
|
388
390
|
// file's functions linked as stubs that report themselves (the rule refuses their calls: one reached is the
|
|
389
|
-
// code's failure where the rule took the step, a difference)
|
|
391
|
+
// code's failure where the rule took the step, a difference), but those left out (skip, stated or inferred: they
|
|
392
|
+
// change nothing the rule keeps, and nothing reads their value), which do nothing.
|
|
390
393
|
const clang = opts.clang ?? (cpp ? 'clang++' : 'clang'), stubs = `${work}/gdp_stubs.c`;
|
|
391
|
-
const link = (more) => cp.execFileSync(clang, [...(cpp ? ['-x', 'c++'] : []), ...flags, '-w', '-O0', '-fsanitize=undefined', '-fsanitize-trap=undefined', '-fno-sanitize=vptr,function', ...strictFlex(clang), h, ...(more.length ? ['-x', 'c', ...more] : []), '-o', `${work}/harness`,], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 1 << 26 });
|
|
394
|
+
const link = (more) => cp.execFileSync(clang, [...(cpp ? ['-x', 'c++'] : []), ...flags, '-w', '-O0', '-fsanitize=undefined', '-fsanitize-trap=undefined', '-fno-sanitize=vptr,function', ...strictFlex(clang), h, ...(more.length ? ['-x', 'c', ...more] : []), '-o', `${work}/harness`,], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 1 << 26, timeout: compileLimit(), killSignal: 'SIGKILL' });
|
|
392
395
|
const why = (x) => String(x.stderr ?? x.message);
|
|
396
|
+
// (the compiler's errors: where they are in the code's own files, the source's; the linker's alone say nothing of it)
|
|
397
|
+
const base = path.resolve(file).split('/').pop(), errorsOf = (x) => why(x).split('\n').filter(l => /\berror:/.test(l) && !/linker command failed|ld returned/.test(l));
|
|
398
|
+
const ofSource = (l) => !/(^|\/)(harness\.(c|cpp)|gdp_stubs\.c):/.test(l) && (l.includes(`${base}:`) || !l.includes(work));
|
|
393
399
|
try {
|
|
394
400
|
link([]);
|
|
395
401
|
}
|
|
396
402
|
catch (x) {
|
|
397
403
|
const names = [...new Set([...why(x).matchAll(/undefined (?:reference to [`']|symbol: )_?([A-Za-z_]\w*)|"_([A-Za-z_]\w*)", referenced from/g)].map(m => m[1] ?? m[2]))];
|
|
398
|
-
if (!names.length)
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
link([stubs]);
|
|
404
|
+
if (!names.length) {
|
|
405
|
+
const b = buildCheck(clang, () => link([]), errorsOf, ofSource, x);
|
|
406
|
+
if (!('ok' in b))
|
|
407
|
+
return { ...empty, ...builtEvidence(b) };
|
|
403
408
|
}
|
|
404
|
-
|
|
405
|
-
|
|
409
|
+
else {
|
|
410
|
+
const out = new Set(read.leftOut ?? []);
|
|
411
|
+
fs.writeFileSync(stubs, `void gdp_extern_called(const char *);\n${names.map(n => (out.has(n) ? `void ${n}(void) {}` : `void ${n}(void) { gdp_extern_called(${JSON.stringify(n)}); }`)).join('\n')}\n`);
|
|
412
|
+
const b = buildCheck(clang, () => link([stubs]), errorsOf, ofSource);
|
|
413
|
+
if (!('ok' in b))
|
|
414
|
+
return { ...empty, ...builtEvidence(b) };
|
|
406
415
|
}
|
|
407
416
|
}
|
|
408
417
|
// (a harness that doesn't start would leave the pipes unopened: asked to start and stop first)
|
|
@@ -34,7 +34,13 @@ export type Evidence = {
|
|
|
34
34
|
coverage: Coverage;
|
|
35
35
|
ms: number;
|
|
36
36
|
mismatch?: string;
|
|
37
|
+
harness?: string;
|
|
38
|
+
source?: string;
|
|
37
39
|
};
|
|
40
|
+
export declare class HarnessFailure extends Error {
|
|
41
|
+
}
|
|
42
|
+
export declare const noEvidence: () => Evidence;
|
|
43
|
+
export declare function retried<E extends Evidence>(once: () => E, empty: () => E): E;
|
|
38
44
|
export declare function instrument(code: string, file: string, tail?: string, extra?: [number, string][]): {
|
|
39
45
|
js: string;
|
|
40
46
|
arms: Arm[];
|
|
@@ -101,6 +107,7 @@ export type CodeSide = {
|
|
|
101
107
|
} | {
|
|
102
108
|
ok: false;
|
|
103
109
|
error: string;
|
|
110
|
+
outside?: true;
|
|
104
111
|
};
|
|
105
112
|
reduce?: (snap: Snap, e: Ev, args: unknown[], params: Record<string, Raw>) => {
|
|
106
113
|
refused: string;
|
|
@@ -22,6 +22,26 @@ import { seeded, walk } from "./machine.js";
|
|
|
22
22
|
import { allowed } from "./watch.js";
|
|
23
23
|
import { normalize, resolveIn, scriptKind } from "./extract-read.js";
|
|
24
24
|
import * as I from "./extract-ir.js";
|
|
25
|
+
// The check's own failure, not the code's: a request past its time, the harness's transport (anvil's JSON-RPC) failing,
|
|
26
|
+
// the harness gone. Never a difference and never a pass: the whole check runs once more, from a fresh harness, and a
|
|
27
|
+
// second failure is the check's outcome (Evidence.harness).
|
|
28
|
+
export class HarnessFailure extends Error {
|
|
29
|
+
}
|
|
30
|
+
export const noEvidence = () => ({ runs: 0, requests: 0, fromStates: 0, probes: { conditions: 0, witnesses: 0, steps: 0 }, refusedBoth: 0, outside: 0, edge: 0, compiledAgreed: 0, coverage: { arms: 0, hit: 0, missed: [] }, ms: 0 });
|
|
31
|
+
export function retried(once, empty) {
|
|
32
|
+
let why = '';
|
|
33
|
+
for (let i = 0; i < 2; i++) {
|
|
34
|
+
try {
|
|
35
|
+
return once();
|
|
36
|
+
}
|
|
37
|
+
catch (x) {
|
|
38
|
+
if (!(x instanceof HarnessFailure))
|
|
39
|
+
throw x;
|
|
40
|
+
why = x.message;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return { ...empty(), harness: why };
|
|
44
|
+
}
|
|
25
45
|
// --- the code, instrumented and loaded ------------------------------------------------------------------------------
|
|
26
46
|
export function instrument(code, file, tail = '', extra = []) {
|
|
27
47
|
const sf = ts.createSourceFile(file, code, ts.ScriptTarget.ES2022, true, scriptKind(file));
|
|
@@ -120,7 +140,14 @@ export function sandbox(code, file, expose, modules = {}, init, files = {}, extr
|
|
|
120
140
|
const script = new vm.Script('globalThis.__r = (() => { try { return {ok: true, value: __gdp[__f].apply(__self, __args)} } catch (e) { return {ok: false, error: String(e && e.message || e)} } })()');
|
|
121
141
|
const call = (f, self, args) => {
|
|
122
142
|
Object.assign(ctx, { __f: f, __self: self, __args: args });
|
|
123
|
-
|
|
143
|
+
try {
|
|
144
|
+
script.runInContext(ctx, { timeout: 1000 });
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
if (e.code === 'ERR_SCRIPT_EXECUTION_TIMEOUT')
|
|
148
|
+
throw new HarnessFailure(`a request ran past the check's 1000 ms (${e.message})`);
|
|
149
|
+
throw e;
|
|
150
|
+
}
|
|
124
151
|
return ctx.__r;
|
|
125
152
|
};
|
|
126
153
|
return { ctx, counts, arms, call, setNow: n => { now = n; }, get: name => vm.runInContext(`__gdp[${JSON.stringify(name)}]`, ctx) };
|
|
@@ -370,12 +397,24 @@ export function draw(sort, sorts, name, rand, near, bound, strs) {
|
|
|
370
397
|
}
|
|
371
398
|
}
|
|
372
399
|
export function check(code, file, m, r, sorts, opts = {}) {
|
|
400
|
+
return retried(() => checkOnce(code, file, m, r, sorts, opts), noEvidence);
|
|
401
|
+
}
|
|
402
|
+
function checkOnce(code, file, m, r, sorts, opts) {
|
|
373
403
|
const isClass = m.shape === 'class', fns = m.shape === 'class' ? [m.entry] : [...new Set(m.events.map(e => e.call))];
|
|
374
404
|
let answers = {};
|
|
375
405
|
// (the modules whose functions the class calls: each export answers its input, or nothing, by the callee's name)
|
|
376
406
|
const ans = (path) => () => { const n = Object.keys(m.answers ?? {}).find(k => m.answers[k] === path); return n === undefined ? undefined : answers[n]; };
|
|
377
407
|
const mods = { ...opts.modules, ...Object.fromEntries((m.imports ?? []).map(mod => [mod, new Proxy({}, { get: (_, k) => (k === '__esModule' ? true : typeof k === 'string' ? ans(`import:${mod}:${k}`) : undefined) })])) };
|
|
378
|
-
|
|
408
|
+
// (a module that doesn't load, one TypeScript reads but JavaScript refuses, a return outside a function or a class
|
|
409
|
+
// declared twice: no request runs, and the check says why)
|
|
410
|
+
let box;
|
|
411
|
+
try {
|
|
412
|
+
box = sandbox(code, file, fns, mods, m.initSource, opts.files, m.factory ? [[m.factory.at, m.factory.text], ...(m.factory.extra ?? [])] : []);
|
|
413
|
+
}
|
|
414
|
+
catch (e) {
|
|
415
|
+
return { runs: 0, requests: 0, fromStates: 0, probes: { conditions: 0, witnesses: 0, steps: 0 }, refusedBoth: 0, outside: 0, edge: 0, compiledAgreed: 0, coverage: { arms: 0, hit: 0, missed: [] }, ms: 0,
|
|
416
|
+
mismatch: `the code doesn't load: ${String(e?.message ?? e).split('\n')[0]}` };
|
|
417
|
+
}
|
|
379
418
|
// An object a factory makes: its state is what its functions close over, read and set through the two functions the
|
|
380
419
|
// check added to it.
|
|
381
420
|
const stateOf = (self) => (m.factory ? box.call('$get', self, []).value : self);
|
|
@@ -518,6 +557,11 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
518
557
|
try {
|
|
519
558
|
if (isClass) {
|
|
520
559
|
const self = side.self, c = code.call(self, e, args);
|
|
560
|
+
if (!c.ok && c.outside) {
|
|
561
|
+
next.stop = true;
|
|
562
|
+
ev.outside++;
|
|
563
|
+
return undefined;
|
|
564
|
+
}
|
|
521
565
|
if (!c.ok)
|
|
522
566
|
res = { refused: c.error };
|
|
523
567
|
else
|
|
@@ -533,6 +577,8 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
533
577
|
catch (x) {
|
|
534
578
|
if (x instanceof Outside)
|
|
535
579
|
res = { outside: x.message };
|
|
580
|
+
else if (x instanceof HarnessFailure)
|
|
581
|
+
throw x;
|
|
536
582
|
else
|
|
537
583
|
return `${where}: the code's state after the request: ${x.message}`;
|
|
538
584
|
}
|
|
@@ -587,13 +633,16 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
587
633
|
}
|
|
588
634
|
if ('refused' in res && 'refused' in rule) {
|
|
589
635
|
// A refusal leaves the state as it was: the object the code threw from must be as it was too.
|
|
636
|
+
// (what the code's object holds: a result the rule keeps beside, a call's, is no field of it)
|
|
590
637
|
if (isClass) {
|
|
591
638
|
try {
|
|
592
|
-
const after = code.snapOf(side.self, params), d = sameSnap(after, side.snap);
|
|
639
|
+
const after = code.snapOf(side.self, params), d = sameSnap(after, { ...side.snap, leaves: Object.fromEntries(Object.entries(side.snap.leaves).filter(([k]) => k in after.leaves)) });
|
|
593
640
|
if (d)
|
|
594
641
|
return `${where}: ${req} ${from}: the code throws (${res.refused}) after changing the object: ${d}`;
|
|
595
642
|
}
|
|
596
643
|
catch (x) {
|
|
644
|
+
if (x instanceof HarnessFailure)
|
|
645
|
+
throw x;
|
|
597
646
|
return `${where}: ${req} ${from}: the code throws after changing the object (${x.message})`;
|
|
598
647
|
}
|
|
599
648
|
}
|
|
@@ -636,7 +685,9 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
636
685
|
// A class's object: made by its constructor, or set to a snapshot.
|
|
637
686
|
// (a param the precondition fixes from the others, a start's quotient: computed from them)
|
|
638
687
|
const drawParams = () => {
|
|
639
|
-
|
|
688
|
+
// (a param given as a natural (domains) is drawn as one, as an input is)
|
|
689
|
+
const nat = (v, x) => (opts.given?.[v.name] === 'nat' && typeof x === 'number' ? Math.abs(x) : x);
|
|
690
|
+
const ps = Object.fromEntries(m.params.filter(v => !v.derived).map(v => [v.name, v.note?.startsWith('Date.now()') ? rand(10_000) : nat(v, draw(v.sort, sorts, v.name, rand, consts, bound, strs))]));
|
|
640
691
|
for (const v of m.params) {
|
|
641
692
|
const d = v.derived;
|
|
642
693
|
if (d)
|
|
@@ -681,6 +732,8 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
681
732
|
return { side: { snap, self: made.value }, st, ps: ps };
|
|
682
733
|
}
|
|
683
734
|
catch (x) {
|
|
735
|
+
if (x instanceof HarnessFailure)
|
|
736
|
+
throw x;
|
|
684
737
|
return { problem: `new ${m.entry}: ${x.message}` };
|
|
685
738
|
}
|
|
686
739
|
}
|