gdp-wired 0.44.0 → 0.46.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 +58 -7
- package/dist/core/bitsets.d.ts +2 -0
- package/dist/core/bitsets.js +23 -0
- package/dist/core/bmc.js +6 -2
- package/dist/core/c/reader.d.ts +9 -1
- package/dist/core/c/reader.js +445 -38
- package/dist/core/calendar.d.ts +29 -0
- package/dist/core/calendar.js +82 -0
- package/dist/core/check.js +3 -2
- package/dist/core/cli.d.ts +4 -0
- package/dist/core/cli.js +335 -39
- package/dist/core/compose.js +3 -1
- package/dist/core/counts.d.ts +5 -2
- package/dist/core/counts.js +335 -88
- package/dist/core/cycles.js +8 -1
- package/dist/core/cyclic.d.ts +1 -0
- package/dist/core/cyclic.js +13 -7
- package/dist/core/data-synth.d.ts +3 -0
- package/dist/core/data-synth.js +252 -0
- package/dist/core/data.d.ts +8 -2
- package/dist/core/data.js +1113 -172
- package/dist/core/discover.js +3 -1
- package/dist/core/draft.d.ts +19 -0
- package/dist/core/draft.js +53 -0
- package/dist/core/dsl.d.ts +39 -3
- package/dist/core/dsl.js +250 -31
- package/dist/core/effect-prims.js +2 -2
- package/dist/core/effect.d.ts +1 -4
- package/dist/core/effect.js +1 -15
- package/dist/core/ex/reader.exs +17 -1
- package/dist/core/exists.d.ts +16 -1
- package/dist/core/exists.js +447 -50
- package/dist/core/extract-c.d.ts +2 -0
- package/dist/core/extract-c.js +185 -31
- package/dist/core/extract-check.d.ts +7 -0
- package/dist/core/extract-check.js +115 -3
- package/dist/core/extract-ex.js +3 -3
- package/dist/core/extract-go-check.d.ts +1 -1
- package/dist/core/extract-go-check.js +21 -13
- package/dist/core/extract-go.d.ts +1 -0
- package/dist/core/extract-go.js +58 -9
- package/dist/core/extract-ir.d.ts +1 -0
- package/dist/core/extract-ir.js +15 -0
- package/dist/core/extract-jvm.d.ts +1 -1
- package/dist/core/extract-jvm.js +65 -14
- package/dist/core/extract-orm.d.ts +12 -0
- package/dist/core/extract-orm.js +244 -0
- package/dist/core/extract-print.js +23 -1
- package/dist/core/extract-py.d.ts +1 -0
- package/dist/core/extract-py.js +4 -4
- package/dist/core/extract-rb.js +3 -3
- package/dist/core/extract-read.d.ts +25 -0
- package/dist/core/extract-read.js +60 -5
- package/dist/core/extract-rs.js +22 -20
- package/dist/core/extract-shapes.js +22 -0
- package/dist/core/extract-sol.d.ts +10 -1
- package/dist/core/extract-sol.js +89 -30
- package/dist/core/extract-step.d.ts +201 -4
- package/dist/core/extract-step.js +1514 -111
- package/dist/core/extract-swift.js +24 -13
- package/dist/core/extract.d.ts +9 -0
- package/dist/core/extract.js +88 -7
- package/dist/core/fiber-run.d.ts +5 -0
- package/dist/core/fiber-run.js +18 -0
- 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.d.ts +4 -1
- package/dist/core/infer.js +1020 -246
- package/dist/core/jvm/JavaReader.java +33 -8
- package/dist/core/jvm/Jvm.java +20 -1
- package/dist/core/jvm/KtReader.java +59 -6
- package/dist/core/laws.d.ts +10 -3
- package/dist/core/laws.js +193 -14
- package/dist/core/machine.d.ts +16 -3
- package/dist/core/machine.js +212 -28
- package/dist/core/nonterm.js +14 -1
- package/dist/core/orm/harness.js +61 -0
- package/dist/core/orm-fixtures/schema.prisma +5 -0
- package/dist/core/py/harness.py +260 -21
- package/dist/core/py/reader.py +278 -39
- package/dist/core/rb/reader.rb +52 -5
- package/dist/core/reader-fixtures/README.md +28 -0
- package/dist/core/reader-fixtures/c-693042.cpp +8 -0
- package/dist/core/reader-fixtures/go-685506.go +14 -0
- package/dist/core/reader-fixtures/jvm-724362.kt +8 -0
- package/dist/core/reader-fixtures/jvm-737430.kt +7 -0
- package/dist/core/reader-fixtures/jvm-765282.kt +3 -0
- package/dist/core/reader-fixtures/swift-735726.swift +15 -0
- package/dist/core/reader-fixtures/swift-735990.swift +15 -0
- package/dist/core/reader-fixtures/swift-739158.swift +4 -0
- package/dist/core/reader-fixtures/swift-744834.swift +15 -0
- package/dist/core/reader-fixtures/swift-746946.swift +31 -0
- package/dist/core/reader-fixtures/typescript-673062.ts.txt +2 -0
- package/dist/core/relation.d.ts +1 -1
- package/dist/core/relation.js +29 -2
- package/dist/core/relational.js +4 -2
- package/dist/core/rs/src/main.rs +18 -2
- package/dist/core/sample-cache.d.ts +8 -0
- package/dist/core/sample-cache.js +46 -0
- package/dist/core/security.d.ts +38 -0
- package/dist/core/security.js +88 -0
- package/dist/core/sol/harness.py +63 -7
- package/dist/core/sol-concrete.d.ts +23 -0
- package/dist/core/sol-concrete.js +195 -0
- package/dist/core/solver.js +8 -5
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +50 -7
- package/dist/core/system.d.ts +62 -1
- package/dist/core/system.js +263 -4
- package/dist/core/transfer.js +39 -9
- package/dist/core/translate.js +39 -10
- package/dist/core/watch.js +51 -6
- package/dist/interchange/check.js +10 -10
- package/dist/interchange/format.d.ts +4 -0
- package/dist/interchange/format.js +24 -6
- package/dist/kernel/cdcl.d.ts +5 -0
- package/dist/kernel/cdcl.js +88 -10
- package/dist/kernel/epr.js +141 -29
- package/dist/kernel/kernel.d.ts +31 -1
- package/dist/kernel/kernel.js +391 -101
- package/dist/kernel/model-sequence.d.ts +3 -0
- package/dist/kernel/model-sequence.js +34 -0
- package/dist/kernel/prove.d.ts +30 -1
- package/dist/kernel/prove.js +481 -71
- package/dist/kernel/reader.d.ts +3 -0
- package/dist/kernel/reader.js +101 -14
- package/dist/kernel/share.d.ts +1 -0
- package/dist/kernel/share.js +27 -0
- package/dist/lean-kernel/emit.d.ts +9 -3
- package/dist/lean-kernel/emit.js +200 -66
- package/dist/lean-kernel/lean-terms.js +5 -0
- package/dist/lean-kernel/library.js +10 -10
- package/package.json +7 -2
package/dist/core/extract-c.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export type COptions = {
|
|
|
19
19
|
fails?: Record<string, string>;
|
|
20
20
|
answers?: Record<string, string>;
|
|
21
21
|
skip?: string[];
|
|
22
|
+
leave?: string[];
|
|
23
|
+
cut?: string[];
|
|
22
24
|
onError?: 'refuse' | 'discard';
|
|
23
25
|
domains?: Record<string, 'nat' | 'int'>;
|
|
24
26
|
check?: GoCheckOptions;
|
package/dist/core/extract-c.js
CHANGED
|
@@ -26,11 +26,26 @@
|
|
|
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
|
-
import { clangDump, flagsFor, node, readC } from "./c/reader.js";
|
|
32
|
+
import { clangDump, constantsOf, flagsFor, node, readC } from "./c/reader.js";
|
|
33
33
|
export { proveRule, proveSteps } from "./dsl.js";
|
|
34
|
+
// (C++ that includes little, embedded code's without the standard library: its whole dump is small, read whole; else
|
|
35
|
+
// only the class's, its headers' being hundreds of MB)
|
|
36
|
+
const smallTUs = new Map();
|
|
37
|
+
function smallTU(file, flags, clang) {
|
|
38
|
+
const k = `${clang} ${flags.join(' ')} ${file}`;
|
|
39
|
+
if (!smallTUs.has(k)) {
|
|
40
|
+
try {
|
|
41
|
+
smallTUs.set(k, node('node:child_process').execFileSync(clang, ['-x', 'c++', ...flags, '-E', '-P', file], { encoding: 'utf8', maxBuffer: 1 << 26, stdio: ['ignore', 'pipe', 'ignore'] }).length < 400_000);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
smallTUs.set(k, false);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return smallTUs.get(k);
|
|
48
|
+
}
|
|
34
49
|
export function readCFile(file, opts, cpp = false) {
|
|
35
50
|
// (no entry, C++: each class the file declares tried, its dump read for its name only)
|
|
36
51
|
if (!opts.entry && cpp) {
|
|
@@ -58,10 +73,51 @@ export function readCFile(file, opts, cpp = false) {
|
|
|
58
73
|
throw new Refusal(file, ok.length ? `several classes write their state (${ok.map(r => r.c.entry).join(', ')}): name one (entry)` : `no class whose methods write its state${why.length ? ` (${why.join('; ')})` : ''}: name the entry`);
|
|
59
74
|
return ok[0];
|
|
60
75
|
}
|
|
61
|
-
const flags0 = [...flagsFor(file, opts.compileCommands), ...(opts.flags ?? [])], flags = cpp && !flags0.some(f => f.startsWith('-std=')) ? [...flags0, '-std=c++17'] : flags0, { tu, target } = clangDump(file, flags, opts.clang ?? (cpp ? 'clang++' : 'clang'), cpp, cpp ? opts.entry : undefined);
|
|
76
|
+
const flags0 = [...flagsFor(file, opts.compileCommands), ...(opts.flags ?? [])], flags = cpp && !flags0.some(f => f.startsWith('-std=')) ? [...flags0, '-std=c++17'] : flags0, { tu, target } = clangDump(file, flags, opts.clang ?? (cpp ? 'clang++' : 'clang'), cpp, cpp && !smallTU(file, flags, opts.clang ?? 'clang++') ? opts.entry : undefined);
|
|
77
|
+
// (C++, its dump filtered to the class: each variable outside it the class reads, a namespace's constant, dumped too)
|
|
78
|
+
if (cpp && opts.entry && !smallTU(file, flags, opts.clang ?? 'clang++')) {
|
|
79
|
+
const have = new Set(), want = new Map();
|
|
80
|
+
const walk = (n) => { if (n.kind === 'VarDecl' && n.id)
|
|
81
|
+
have.add(n.id); const d = n.referencedDecl; if (n.kind === 'DeclRefExpr' && d?.kind === 'VarDecl')
|
|
82
|
+
want.set(d.id, d.name); for (const c of n.inner ?? [])
|
|
83
|
+
walk(c); };
|
|
84
|
+
walk(tu);
|
|
85
|
+
// (and each struct a method takes by reference or pointer, the caller's object: its fields read as inputs)
|
|
86
|
+
const recs = new Set(), haveRec = new Set();
|
|
87
|
+
const walkP = (n) => { if (/Record/.test(n.kind) && n.name)
|
|
88
|
+
haveRec.add(n.name); if (n.kind === 'ParmVarDecl' && n.type) {
|
|
89
|
+
const b = n.type.qualType.replace(/\b(const|volatile|struct|class)\b|[&*]/g, '').trim();
|
|
90
|
+
if (/^[A-Za-z_]\w*$/.test(b))
|
|
91
|
+
recs.add(b);
|
|
92
|
+
} for (const c of n.inner ?? [])
|
|
93
|
+
walkP(c); };
|
|
94
|
+
walkP(tu);
|
|
95
|
+
for (const name of [...recs].filter(r => !haveRec.has(r))) {
|
|
96
|
+
try {
|
|
97
|
+
const more = clangDump(file, flags, opts.clang ?? 'clang++', cpp, name).tu.inner ?? [];
|
|
98
|
+
tu.inner = [...(tu.inner ?? []), ...more.filter(m => /^(CXX)?RecordDecl$/.test(m.kind) && m.name === name && m.completeDefinition)];
|
|
99
|
+
}
|
|
100
|
+
catch { /* read where it is, as before */ }
|
|
101
|
+
}
|
|
102
|
+
for (const name of new Set([...want].filter(([id]) => !have.has(id)).map(([, n]) => n))) {
|
|
103
|
+
try {
|
|
104
|
+
const more = clangDump(file, flags, opts.clang ?? 'clang++', cpp, name).tu.inner ?? [];
|
|
105
|
+
const ids = [...want].filter(([id, n]) => n === name && !have.has(id)).map(([id]) => id);
|
|
106
|
+
for (const m of more)
|
|
107
|
+
if (m.kind === 'VarDecl' && m.name === name)
|
|
108
|
+
for (const id of ids)
|
|
109
|
+
tu.inner = [...(tu.inner ?? []), { ...m, id }];
|
|
110
|
+
}
|
|
111
|
+
catch { /* read where it is, as before */ }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
62
114
|
// (no entry: the file's one struct its functions write, each struct of the dump tried)
|
|
63
115
|
if (!opts.entry) {
|
|
64
|
-
|
|
116
|
+
// (each struct by its tag or a typedef's name, an unnamed one's typedef too, that a function of the file takes a
|
|
117
|
+
// pointer to first)
|
|
118
|
+
const firsts = new Set((tu.inner ?? []).filter(n => n.kind === 'FunctionDecl' && n.inner?.some(c => c.kind === 'CompoundStmt')).map(n => n.inner.find(c => c.kind === 'ParmVarDecl')?.type?.qualType.replace(/\b(const|volatile|struct)\b|\*/g, '').trim()).filter((t) => !!t));
|
|
119
|
+
const typedefNames = (tu.inner ?? []).filter(n => n.kind === 'TypedefDecl' && (n.inner ?? []).some(c => /Record|Elaborated/.test(c.kind))).map(n => String(n.name));
|
|
120
|
+
const names = [...new Set([...(tu.inner ?? []).filter(n => n.kind === 'RecordDecl' && n.name && n.completeDefinition).map(n => String(n.name)), ...typedefNames])].filter(n => firsts.has(n));
|
|
65
121
|
const ok = names.filter(n => { try {
|
|
66
122
|
const c = readC(file, n, tu, target, cpp, opts.root);
|
|
67
123
|
readGo(c.pkg, { methods: c.methods });
|
|
@@ -72,11 +128,27 @@ export function readCFile(file, opts, cpp = false) {
|
|
|
72
128
|
return false;
|
|
73
129
|
throw e;
|
|
74
130
|
} });
|
|
131
|
+
// (none: the file's globals, where its functions keep the state there)
|
|
132
|
+
if (!ok.length && !cpp) {
|
|
133
|
+
const stem = file.replace(/^.*\//, '').replace(/\.[^.]*$/, '');
|
|
134
|
+
try {
|
|
135
|
+
const r = readCFile(file, { ...opts, entry: stem }, cpp);
|
|
136
|
+
readGo(r.c.pkg, { methods: r.c.methods, ...(opts.onError ? { onError: opts.onError } : {}) });
|
|
137
|
+
return r;
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
if (!(e instanceof Refusal))
|
|
141
|
+
throw e;
|
|
142
|
+
// (its globals are its state, and something in them is refused: that, not the structs)
|
|
143
|
+
if (!/^no global of /.test(e.what))
|
|
144
|
+
throw new Refusal(e.where, `${e.what} (the file's globals read as its state, ${stem}: no struct its functions write)`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
75
147
|
if (ok.length !== 1)
|
|
76
148
|
throw new Refusal(file, ok.length ? `several structs written by the file's functions (${ok.join(', ')}): name one (entry)` : 'no struct the file\'s functions write: name the entry');
|
|
77
149
|
return readCFile(file, { ...opts, entry: ok[0] }, cpp);
|
|
78
150
|
}
|
|
79
|
-
return { c: readC(file, opts.entry, tu, target, cpp, opts.root, Object.fromEntries((opts.functions ?? []).filter(f => f.includes('/')).map(f => [f.split('/')[0], Number(f.split('/')[1])]))), flags };
|
|
151
|
+
return { c: readC(file, opts.entry, tu, target, cpp, opts.root, Object.fromEntries((opts.functions ?? []).filter(f => f.includes('/')).map(f => [f.split('/')[0], Number(f.split('/')[1])])), (opts.functions ?? opts.methods ?? []).map(f => f.split('/')[0]), opts.leave ?? [], [...Object.keys(opts.answers ?? {}), ...(opts.cut ?? [])]), flags };
|
|
80
152
|
}
|
|
81
153
|
export function extractC(at, given = {}, cpp = false) {
|
|
82
154
|
const notes = [], file = pathOf(at), fns = given.methods ?? given.functions, opts = { ...given, ...(fns ? { functions: fns } : {}) };
|
|
@@ -98,8 +170,12 @@ export function extractC(at, given = {}, cpp = false) {
|
|
|
98
170
|
read.model.laws = read.model.laws?.map(l => (l.includes(`"${n} within its type's range"`) ? l.replace(/\.atLeast\(-\d+\)/, '.atLeast(0)') : l));
|
|
99
171
|
if (!opts.entry)
|
|
100
172
|
read.model.notes.unshift(`the entry inferred: ${c.entry}, ${cpp ? 'the class whose methods write its state' : 'the struct the file\'s functions write'}`);
|
|
173
|
+
// (the names the laws read that the code defines as constants: its enum constants, else clang's value of each)
|
|
174
|
+
// (a qualified one, C++'s vehicle_status_s::ARMING_STATE_ARMED, by its law's spelling, vehicle_status_s.ARMING_STATE_ARMED)
|
|
175
|
+
const keep = (given.keep ?? []).filter(n => !(n in c.consts)), found = keep.length ? constantsOf(file, flags, opts.clang ?? (cpp ? 'clang++' : 'clang'), cpp, keep) : {};
|
|
176
|
+
const consts = { ...c.consts, ...Object.fromEntries(Object.entries(found).map(([k, v]) => [k.replaceAll('::', '.'), v])) };
|
|
101
177
|
const x = fromModel(read.model, `${c.entry}.${cpp ? 'cpp' : 'c'}`, { ...unsignedDomains(read), ...signedDomains(c.pkg, read), ...opts.domains }, notes, (rule, sorts) => checkC(file, flags, c, read, rule, sorts, opts, cpp));
|
|
102
|
-
return { ...x, leaves: read.leaves, names: goNames(read) };
|
|
178
|
+
return { ...x, leaves: read.leaves, names: goNames(read), ...(Object.keys(consts).length ? { consts } : {}) };
|
|
103
179
|
}
|
|
104
180
|
// A signed field (and a signed result) is an integer, never inferred a natural: its range law reads below 0.
|
|
105
181
|
function signedDomains(pkg, read) {
|
|
@@ -127,8 +203,9 @@ export function harnessC(file, c, read, opts, cpp) {
|
|
|
127
203
|
const pkg = c.pkg, T = (t) => ty(pkg, t), q = JSON.stringify, robs = [];
|
|
128
204
|
// A leaf's place: o->a.b in C; in C++ each member through a pointer to it the explicit instantiation of gdp_rob takes
|
|
129
205
|
// (access isn't checked there), so private members are read and set as the class's own code does.
|
|
206
|
+
const G = c.globals;
|
|
130
207
|
const acc = (path) => {
|
|
131
|
-
if (!cpp)
|
|
208
|
+
if (!cpp || G)
|
|
132
209
|
return `o->${path.join('.')}`;
|
|
133
210
|
let e = '(*o)', t = pkg.types[0].t;
|
|
134
211
|
for (const f of path) {
|
|
@@ -158,18 +235,28 @@ export function harnessC(file, c, read, opts, cpp) {
|
|
|
158
235
|
}
|
|
159
236
|
else if (l.kind === 'bool') {
|
|
160
237
|
get.push(`gdp_putf("%s\\"%s\\":%s", sep, ${k}, ${a} ? "true" : "false"); sep = ",";`);
|
|
161
|
-
set.push(`if ((v = gdp_key(st, ${k}))) { if (!strncmp(v, "true", 4)) ${a} = 1; else if (!strncmp(v, "false", 5)) ${a} = 0; else return "${l.name}: not a boolean"; }`);
|
|
238
|
+
set.push(`if ((v = gdp_key(st, ${k}))) { if (!strncmp(v, "true", 4)) ${a} = ${T(l.t).nullflag ? '(void *)1' : '1'}; else if (!strncmp(v, "false", 5)) ${a} = 0; else return "${l.name}: not a boolean"; }`);
|
|
162
239
|
}
|
|
163
240
|
}
|
|
164
241
|
const frames = read.leaves.filter(l => l.kind === 'frame' && !T(l.t).mutex);
|
|
242
|
+
// (a field left out that points to an object other code owns, a buffer: a zeroed one of its own, as the code's setup
|
|
243
|
+
// would give)
|
|
244
|
+
const et = T(pkg.types[0].t), leftPtrs = cpp || G ? [] : (opts.leave ?? []).filter(f => { const fd = et.fields?.find(g => g.name === f); return fd && T(fd.t).k === 'ptr' && ['struct', 'int', 'bool'].includes(T(T(fd.t).elem).k); });
|
|
245
|
+
// (the longest method name the request names, in UTF-8 bytes, its buffer: a name of any length is called)
|
|
246
|
+
const M = Math.max(256, ...read.methods.map(x => new TextEncoder().encode(x.name).length + 1));
|
|
165
247
|
const calls = read.methods.map(meth => {
|
|
166
|
-
const lines = [], args = ['o'], outs = [];
|
|
248
|
+
const lines = [], args = ['o'], outs = [], given = [];
|
|
167
249
|
meth.params.forEach((pa, i) => {
|
|
250
|
+
// (a global the request gives: set before the call, not passed)
|
|
251
|
+
const push = (a) => { if (G?.inputs.includes(pa.name) && i >= meth.params.length - G.inputs.filter(n => meth.params.some(q => q.name === n)).length)
|
|
252
|
+
given.push([pa.name, a]);
|
|
253
|
+
else
|
|
254
|
+
args.push(a); };
|
|
168
255
|
const t = T(pa.t), raw = `gdp_idx(a, ${i})`;
|
|
169
256
|
if (pa.arg.k === 'input') {
|
|
170
257
|
lines.push(`${t.go} a${i}; memset(&a${i}, 0, sizeof a${i}); const char *r${i} = ${raw}; if (!r${i}) return "no argument ${i}";`);
|
|
171
258
|
lines.push(t.k === 'int' ? intIn(`a${i}`, `r${i}`, pa.t, `return "argument ${i} outside its type"`) : `a${i} = !strncmp(r${i}, "true", 4);`);
|
|
172
|
-
|
|
259
|
+
push(`a${i}`);
|
|
173
260
|
}
|
|
174
261
|
else if (pa.arg.k === 'rec' && t.k === 'ptr' && T(t.elem).box) {
|
|
175
262
|
// (an out-parameter: a local of its type, its address given, what is left in it reported)
|
|
@@ -185,25 +272,26 @@ export function harnessC(file, c, read, opts, cpp) {
|
|
|
185
272
|
const ft = st.fields.find(g => g.name === f).t;
|
|
186
273
|
lines.push(`if ((v = gdp_key(r${i}, ${q(f)}))) ${T(ft).k === 'int' ? intIn(`a${i}.${f}`, 'v', ft, `return "argument ${i}.${f} outside its type"`) : `a${i}.${f} = !strncmp(v, "true", 4);`}`);
|
|
187
274
|
}
|
|
188
|
-
|
|
275
|
+
push(byValue || t.go?.trim().endsWith('&') ? `a${i}` : `&a${i}`);
|
|
189
276
|
}
|
|
190
277
|
else
|
|
191
278
|
lines.push(`return "an argument the harness can't make";`);
|
|
192
279
|
});
|
|
193
280
|
const rt = meth.results[0], fails = opts.fails?.[meth.name]?.trim();
|
|
194
|
-
const callee = cpp ? `o->${meth.name}(${args.slice(1).join(', ')})` : `${meth.name}(${args.join(', ')})`;
|
|
281
|
+
const callee = G ? `${meth.name}(${args.slice(1).join(', ')})` : cpp ? `o->${meth.name}(${args.slice(1).join(', ')})` : `${meth.name}(${args.join(', ')})`;
|
|
195
282
|
const invoke = rt === undefined ? `${callee};` : `${cpp ? 'auto' : T(rt).go} r0 = ${callee}; gdp_has = 1; ${T(rt).k === 'int' ? `gdp_outv = ${T(rt).signed === false ? '(long long)' : ''}r0; gdp_outu = ${T(rt).signed === false ? 1 : 0};` : 'gdp_outb = r0; gdp_isb = 1;'}`;
|
|
196
283
|
const failed = fails !== undefined && rt !== undefined ? `if (r0 ${fails.replace(/^(==|!=|<=|>=|<|>)\s*/, '$1 ')}) gdp_err = 1;` : '';
|
|
197
284
|
// (with out-parameters, the result is a record: the value returned and what each holds)
|
|
198
285
|
const rec = outs.length ? `gdp_rec = 1; gdp_putf(",\\"out\\":{"); ${[...(rt !== undefined ? [['result', 'r0', rt]] : []), ...outs].map(([n, v, et], j) => `gdp_putf("${j ? ',' : ''}\\"${n}\\":"); ${T(et).k === 'int' ? `gdp_putf(${T(et).signed === false ? '"%llu", (unsigned long long)' : '"%lld", (long long)'}(${v}))` : `gdp_putf("%s", ${v} ? "true" : "false")`};`).join(' ')} gdp_putf("},\\"hasOut\\":true");` : '';
|
|
199
|
-
|
|
286
|
+
const load = G ? `gdp_load(o); ${given.map(([n, a]) => `memcpy((void *)&${n}, (const void *)&${a}, sizeof ${n});`).join(' ')}\n ` : '';
|
|
287
|
+
return `if (!strcmp(m, ${q(meth.name)})) {\n ${lines.join('\n ')}\n memcpy(gdp_pre, (void *)o, sizeof(gdp_T));\n ${load}int sg = sigsetjmp(gdp_jb, 1);\n if (!sg) { gdp_armed = 1; gdp_watch(1); ${cpp ? 'try { ' : ''}${invoke} ${failed} gdp_watch(0); gdp_armed = 0; ${rec}${cpp ? ' } catch (...) { gdp_watch(0); gdp_armed = 0; gdp_sig = SIGUSR2; }' : ''} } else { gdp_watch(0); gdp_sig = sg; }\n ${G ? 'gdp_store(o);\n ' : ''}return gdp_after(o);\n }`;
|
|
200
288
|
});
|
|
201
289
|
const frameLines = frames.map(l => { const e = acc(l.path); return `if (memcmp((char *)&${e}, (char *)gdp_pre + ((char *)&${e} - (char *)o), sizeof ${e})) return ${q(l.path.join('.'))};`; });
|
|
202
290
|
// The object, made as the code makes it: C's zeroed; C++'s constructed (in zeroed memory) with the rule's parameters.
|
|
203
291
|
const cps = read.ctor ?? [], make = `static gdp_T *gdp_make(const char *line, const char **bad) {
|
|
204
292
|
const char *a = gdp_key(line, "a"), *v; (void)a; (void)v;
|
|
205
|
-
${cps.map((pa, i) => `${T(pa.t).go} c${i} = 0; if (!(v = a ? gdp_idx(a, ${i}) : 0)) { *bad = "no argument ${i}"; return 0; } ${T(pa.t).k === 'int' ? intIn(`c${i}`, 'v', pa.t, `{ *bad = "argument ${i} outside its type"; return 0; }`) : `c${i} = !strncmp(v, "true", 4);`}`).join('\n ')}
|
|
206
|
-
void *mem = calloc(1, sizeof(gdp_T));
|
|
293
|
+
${cps.map((pa, i) => `${T(pa.t).go} c${i}${cpp ? '{}' : ' = 0'}; if (!(v = a ? gdp_idx(a, ${i}) : 0)) { *bad = "no argument ${i}"; return 0; } ${T(pa.t).k === 'int' ? intIn(`c${i}`, 'v', pa.t, `{ *bad = "argument ${i} outside its type"; return 0; }`) : `c${i} = !strncmp(v, "true", 4);`}`).join('\n ')}
|
|
294
|
+
void *mem = calloc(1, sizeof(gdp_T));${G ? '\n memcpy(mem, &gdp_init, sizeof(gdp_T));' : ''}${leftPtrs.map(f => `\n ((gdp_T *)mem)->${f} = calloc(1, sizeof *((gdp_T *)mem)->${f});`).join('')}
|
|
207
295
|
return ${cpp ? `new (mem) gdp_T(${cps.map((_, i) => `c${i}`).join(', ')})` : '(gdp_T *)mem'};
|
|
208
296
|
}`;
|
|
209
297
|
return `// Generated by gdp-wired's ${cpp ? 'C++' : 'C'} check (core/extract-c.ts): the file's code is called as it is.
|
|
@@ -228,8 +316,15 @@ static long long gdp_answer(const char *name) {
|
|
|
228
316
|
#include <signal.h>
|
|
229
317
|
#include <setjmp.h>
|
|
230
318
|
#include <errno.h>
|
|
319
|
+
#include <sys/time.h>
|
|
231
320
|
${cpp ? '#include <new>\n#include <type_traits>\n' : '#include <stdbool.h>\n'}
|
|
232
|
-
|
|
321
|
+
${G ? `// The file's globals, the state: an object of them each request loads, and stores back after it.
|
|
322
|
+
${G.externs.map(n => `__typeof__(${n}) ${n};`).join('\n')}
|
|
323
|
+
typedef struct { ${G.state.map(n => `${cpp ? `std::remove_cv_t<decltype(${n})>` : `__typeof__(${n})`} ${n};`).join(' ')} } gdp_G;
|
|
324
|
+
static void gdp_load(const gdp_G *o) { ${G.state.map(n => `memcpy((void *)&${n}, (const void *)&o->${n}, sizeof ${n});`).join(' ')} }
|
|
325
|
+
static void gdp_store(gdp_G *o) { ${G.state.map(n => `memcpy((void *)&o->${n}, (const void *)&${n}, sizeof ${n});`).join(' ')} }
|
|
326
|
+
static gdp_G gdp_init;
|
|
327
|
+
` : ''}typedef ${c.cname} gdp_T;
|
|
233
328
|
static unsigned char gdp_pre[sizeof(gdp_T)];
|
|
234
329
|
${robs.length ? `template <typename Tag, auto M> struct gdp_rob { friend auto gdp_ptr(Tag) { return M; } };\n${robs.map((k, i) => `struct gdp_tag${i} { friend auto gdp_ptr(gdp_tag${i}); };\ntemplate struct gdp_rob<gdp_tag${i}, ${k}>;`).join('\n')}` : ''}
|
|
235
330
|
static char *gdp_buf; static size_t gdp_n, gdp_cap;
|
|
@@ -285,6 +380,8 @@ static const char *gdp_set(gdp_T *o, const char *st) {
|
|
|
285
380
|
static sigjmp_buf gdp_jb; static volatile sig_atomic_t gdp_armed;
|
|
286
381
|
static int gdp_sig, gdp_err, gdp_has, gdp_outu, gdp_isb, gdp_rec; static long long gdp_outv; static bool gdp_outb;
|
|
287
382
|
static void gdp_on(int s) { if (gdp_armed) { gdp_armed = 0; siglongjmp(gdp_jb, s); } signal(s, SIG_DFL); raise(s); }
|
|
383
|
+
// (a call that doesn't return within 2 s, an endless loop: the code's failure, as the rule's)
|
|
384
|
+
static void gdp_watch(int on) { struct itimerval t; memset(&t, 0, sizeof t); t.it_value.tv_sec = on ? 2 : 0; setitimer(ITIMER_REAL, &t, 0); }
|
|
288
385
|
static const char *gdp_frame(gdp_T *o) {
|
|
289
386
|
(void)o;
|
|
290
387
|
${frameLines.join('\n ')}
|
|
@@ -296,7 +393,7 @@ ${cpp ? 'extern "C" ' : ''}void gdp_extern_called(const char *n) { gdp_extern =
|
|
|
296
393
|
static const char *gdp_after(gdp_T *o) {
|
|
297
394
|
const char *f = gdp_frame(o);
|
|
298
395
|
if (f) { gdp_putf(",\\"frame\\":\\"%s\\"", f); }
|
|
299
|
-
if (gdp_sig) gdp_putf(",\\"panic\\":\\"%s\\"", gdp_sig == SIGABRT ? "SIGABRT (abort, or an assertion failing)" : gdp_sig == SIGTRAP || gdp_sig == SIGILL ? "undefined behavior (the sanitizer's trap)" : gdp_sig == SIGUSR1 ? "another file's function called" : gdp_sig == SIGUSR2 ? "an exception thrown" : "a signal");
|
|
396
|
+
if (gdp_sig) gdp_putf(",\\"panic\\":\\"%s\\"", gdp_sig == SIGABRT ? "SIGABRT (abort, or an assertion failing)" : gdp_sig == SIGTRAP || gdp_sig == SIGILL ? "undefined behavior (the sanitizer's trap)" : gdp_sig == SIGUSR1 ? "another file's function called" : gdp_sig == SIGUSR2 ? "an exception thrown" : gdp_sig == SIGALRM ? "the code didn't return (an endless loop)" : "a signal");
|
|
300
397
|
else if (gdp_err) gdp_putf(",\\"err\\":\\"its result is its failure (%lld)\\"", gdp_outv);
|
|
301
398
|
if (!gdp_sig && gdp_has && !gdp_rec) { if (gdp_isb) gdp_putf(",\\"out\\":%s,\\"hasOut\\":true", gdp_outb ? "true" : "false"); else if (gdp_outu) gdp_putf(",\\"out\\":\\"%llu\\",\\"hasOut\\":true", (unsigned long long)gdp_outv); else gdp_putf(",\\"out\\":\\"%lld\\",\\"hasOut\\":true", gdp_outv); }
|
|
302
399
|
if (${opts.onError === 'discard' ? 1 : 0} && (gdp_sig || gdp_err)) memcpy((void *)o, gdp_pre, sizeof(gdp_T));
|
|
@@ -311,11 +408,11 @@ static const char *gdp_call(gdp_T *o, const char *m, const char *a) {
|
|
|
311
408
|
return "no such function";
|
|
312
409
|
}
|
|
313
410
|
int main(int argc, char **argv) {
|
|
314
|
-
if (argc == 2 && !strcmp(argv[1], "--probe")) return 0;
|
|
411
|
+
${G ? 'gdp_store(&gdp_init);\n ' : ''}if (argc == 2 && !strcmp(argv[1], "--probe")) return 0;
|
|
315
412
|
if (argc < 3) return 2;
|
|
316
413
|
FILE *in = fopen(argv[1], "r"), *out = fopen(argv[2], "w");
|
|
317
414
|
if (!in || !out) return 2;
|
|
318
|
-
int sigs[] = {SIGABRT, SIGTRAP, SIGILL, SIGFPE, SIGSEGV, SIGBUS};
|
|
415
|
+
int sigs[] = {SIGABRT, SIGTRAP, SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGALRM};
|
|
319
416
|
for (unsigned i = 0; i < sizeof sigs / sizeof *sigs; i++) signal(sigs[i], gdp_on);
|
|
320
417
|
gdp_T **objs = 0; size_t nobj = 0;
|
|
321
418
|
char *line = 0; size_t lcap = 0;
|
|
@@ -337,8 +434,8 @@ int main(int argc, char **argv) {
|
|
|
337
434
|
}
|
|
338
435
|
else if (op && !strncmp(op, "\\"get\\"", 5)) { gdp_putf("{\\"state\\":"); gdp_get(objs[k - 1]); gdp_putf("}"); }
|
|
339
436
|
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 >
|
|
437
|
+
const char *mv = gdp_key(line, "m"), *av = gdp_key(line, "a"); char m[${M}] = {0};
|
|
438
|
+
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
439
|
gdp_putf("{\\"ok\\":true");${read.answers?.length ? `
|
|
343
440
|
gdp_answers = gdp_key(line, "answers");` : ''}
|
|
344
441
|
const char *bad = gdp_call(objs[k - 1], m, av ? av : "[]");
|
|
@@ -365,6 +462,34 @@ function strictFlex(clang) {
|
|
|
365
462
|
}
|
|
366
463
|
return flexOK.get(clang) ? ['-fstrict-flex-arrays=3'] : [];
|
|
367
464
|
}
|
|
465
|
+
// A demangled C++ function's own name: `void events::send<A, B>(unsigned int, ...)` is send, `A::f(int) const` f.
|
|
466
|
+
function shortName(d) {
|
|
467
|
+
let depth = 0, end = -1;
|
|
468
|
+
for (let i = 0; i < d.length; i++) {
|
|
469
|
+
const ch = d[i];
|
|
470
|
+
if (ch === '<')
|
|
471
|
+
depth++;
|
|
472
|
+
else if (ch === '>')
|
|
473
|
+
depth--;
|
|
474
|
+
else if (ch === '(' && depth === 0) {
|
|
475
|
+
end = i;
|
|
476
|
+
break;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (end < 0)
|
|
480
|
+
return undefined;
|
|
481
|
+
let head = d.slice(0, end), out = '', dd = 0;
|
|
482
|
+
for (const ch of head) {
|
|
483
|
+
if (ch === '<')
|
|
484
|
+
dd++;
|
|
485
|
+
else if (ch === '>')
|
|
486
|
+
dd--;
|
|
487
|
+
else if (dd === 0)
|
|
488
|
+
out += ch;
|
|
489
|
+
}
|
|
490
|
+
head = out.trim().split(/\s+/).pop() ?? '';
|
|
491
|
+
return head.split('::').pop() || undefined;
|
|
492
|
+
}
|
|
368
493
|
// --- the check ------------------------------------------------------------------------------------------------------------
|
|
369
494
|
export function checkC(file, flags, c, read, r, sorts, opts, cpp) {
|
|
370
495
|
const fs = node('node:fs'), cp = node('node:child_process'), os = node('node:os'), path = node('node:path');
|
|
@@ -389,22 +514,51 @@ export function checkC(file, flags, c, read, r, sorts, opts, cpp) {
|
|
|
389
514
|
// code's failure where the rule took the step, a difference), but those left out (skip, stated or inferred: they
|
|
390
515
|
// change nothing the rule keeps, and nothing reads their value), which do nothing.
|
|
391
516
|
const clang = opts.clang ?? (cpp ? 'clang++' : 'clang'), stubs = `${work}/gdp_stubs.c`;
|
|
392
|
-
const
|
|
517
|
+
const run = (cc, args) => cp.execFileSync(cc, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 1 << 26, timeout: compileLimit(), killSignal: 'SIGKILL' });
|
|
518
|
+
// (the stubs are C, compiled apart: the project's flags, a C++ -std among them, are the harness's)
|
|
519
|
+
const link = (more) => { for (const m of more)
|
|
520
|
+
run(clang.replace(/\+\+$/, ''), ['-w', '-c', '-x', 'c', m, '-o', `${m}.o`]); return run(clang, [...(cpp ? ['-x', 'c++'] : []), ...flags, '-w', '-O0', '-fsanitize=undefined', '-fsanitize-trap=undefined', '-fno-sanitize=vptr,function', ...strictFlex(clang), h, ...(more.length ? ['-x', 'none', ...more.map(m => `${m}.o`)] : []), '-o', `${work}/harness`]); };
|
|
393
521
|
const why = (x) => String(x.stderr ?? x.message);
|
|
522
|
+
// (the compiler's errors: where they are in the code's own files, the source's; the linker's alone say nothing of it)
|
|
523
|
+
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));
|
|
524
|
+
const ofSource = (l) => !/(^|\/)(harness\.(c|cpp)|gdp_stubs\.c):/.test(l) && (l.includes(`${base}:`) || !l.includes(work));
|
|
394
525
|
try {
|
|
395
526
|
link([]);
|
|
396
527
|
}
|
|
397
528
|
catch (x) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
529
|
+
// (C's names: a C++ one is demangled, `A::f(int)`, below)
|
|
530
|
+
const names = [...new Set([...why(x).matchAll(/undefined (?:reference to [`']|symbol: )_?([A-Za-z_]\w*)(?=['`]|$)|"_([A-Za-z_]\w*)", referenced from/gm)].map(m => m[1] ?? m[2]))];
|
|
531
|
+
// (C++: the linker names other code's functions demangled, `A::f(int) const`: their own names from the object's
|
|
532
|
+
// undefined symbols, each matched by its demangled form; a stub is named so, `void _ZNK1A1fEi(void)`)
|
|
533
|
+
const demangled = new Set([...why(x).matchAll(/"([^"]+)", referenced from|undefined reference to [`']([^'`]+)'/g)].map(m => m[1] ?? m[2]).filter(d => /[:(]/.test(d)));
|
|
534
|
+
const base = new Map();
|
|
535
|
+
if (cpp && demangled.size) {
|
|
536
|
+
try {
|
|
537
|
+
run(clang, ['-x', 'c++', ...flags, '-w', '-O0', '-fsanitize=undefined', '-fsanitize-trap=undefined', '-fno-sanitize=vptr,function', ...strictFlex(clang), '-c', h, '-o', `${work}/harness.o`]);
|
|
538
|
+
const und = run('nm', ['-u', `${work}/harness.o`]).split('\n').map(l => l.trim().replace(/^U\s+/, '')).filter(Boolean);
|
|
539
|
+
const out = cp.execFileSync('c++filt', [], { input: und.join('\n'), encoding: 'utf8' }).split('\n');
|
|
540
|
+
und.forEach((m, i) => { const d = out[i]?.trim() ?? ''; if (demangled.has(d) || demangled.has(d.replace(/^_/, ''))) {
|
|
541
|
+
const n = m.replace(/^_(?=_Z)/, '');
|
|
542
|
+
if (/^\w+$/.test(n)) {
|
|
543
|
+
names.push(n);
|
|
544
|
+
base.set(n, shortName(d) ?? n);
|
|
545
|
+
}
|
|
546
|
+
} });
|
|
547
|
+
}
|
|
548
|
+
catch { /* the names the linker gave */ }
|
|
549
|
+
}
|
|
550
|
+
if (!names.length) {
|
|
551
|
+
const b = buildCheck(clang, () => link([]), errorsOf, ofSource, x);
|
|
552
|
+
if (!('ok' in b))
|
|
553
|
+
return { ...empty, ...builtEvidence(b) };
|
|
405
554
|
}
|
|
406
|
-
|
|
407
|
-
|
|
555
|
+
else {
|
|
556
|
+
// (left out by its name, or by its name and constant arguments, CO_error_x_1_27: answered per arguments)
|
|
557
|
+
const left = new Set(read.leftOut ?? []), out = { has: (n) => left.has(n) || [...left].some(k => k.startsWith(`${n}_`) && /^[xm\d_]+$/.test(k.slice(n.length + 1))) };
|
|
558
|
+
fs.writeFileSync(stubs, `void gdp_extern_called(const char *);\n${names.map(n => (out.has(n) || out.has(base.get(n) ?? '') ? `void ${n}(void) {}` : `void ${n}(void) { gdp_extern_called(${JSON.stringify(base.get(n) ?? n)}); }`)).join('\n')}\n`);
|
|
559
|
+
const b = buildCheck(clang, () => link([stubs]), errorsOf, ofSource);
|
|
560
|
+
if (!('ok' in b))
|
|
561
|
+
return { ...empty, ...builtEvidence(b) };
|
|
408
562
|
}
|
|
409
563
|
}
|
|
410
564
|
// (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[];
|
|
@@ -127,5 +133,6 @@ export declare function drive(m: Model, r: Rule, sorts: Sorts, code: CodeSide, o
|
|
|
127
133
|
}): Evidence;
|
|
128
134
|
export declare function conditionsOf(m: Model, writes?: boolean): B[];
|
|
129
135
|
export declare function condAt(c: B, env: Record<string, unknown>): ReturnType<Term['lt']>;
|
|
136
|
+
export declare function tiesIn(m: Model, s: Snap, raw: Record<string, Raw>): boolean;
|
|
130
137
|
export declare function evalTerm(t: N | B | I.L, env: Record<string, Raw>, cell?: (map: string, key: Raw, field: string) => Raw): Raw;
|
|
131
138
|
export {};
|
|
@@ -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);
|
|
@@ -538,6 +577,8 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
538
577
|
catch (x) {
|
|
539
578
|
if (x instanceof Outside)
|
|
540
579
|
res = { outside: x.message };
|
|
580
|
+
else if (x instanceof HarnessFailure)
|
|
581
|
+
throw x;
|
|
541
582
|
else
|
|
542
583
|
return `${where}: the code's state after the request: ${x.message}`;
|
|
543
584
|
}
|
|
@@ -592,13 +633,16 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
592
633
|
}
|
|
593
634
|
if ('refused' in res && 'refused' in rule) {
|
|
594
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)
|
|
595
637
|
if (isClass) {
|
|
596
638
|
try {
|
|
597
|
-
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)) });
|
|
598
640
|
if (d)
|
|
599
641
|
return `${where}: ${req} ${from}: the code throws (${res.refused}) after changing the object: ${d}`;
|
|
600
642
|
}
|
|
601
643
|
catch (x) {
|
|
644
|
+
if (x instanceof HarnessFailure)
|
|
645
|
+
throw x;
|
|
602
646
|
return `${where}: ${req} ${from}: the code throws after changing the object (${x.message})`;
|
|
603
647
|
}
|
|
604
648
|
}
|
|
@@ -688,6 +732,8 @@ export function drive(m, r, sorts, code, opts = {}) {
|
|
|
688
732
|
return { side: { snap, self: made.value }, st, ps: ps };
|
|
689
733
|
}
|
|
690
734
|
catch (x) {
|
|
735
|
+
if (x instanceof HarnessFailure)
|
|
736
|
+
throw x;
|
|
691
737
|
return { problem: `new ${m.entry}: ${x.message}` };
|
|
692
738
|
}
|
|
693
739
|
}
|
|
@@ -959,17 +1005,83 @@ function carry(m, raw, inputs, st, params, dec) {
|
|
|
959
1005
|
const dflt = (map, field) => { const f = m.maps.find(d => d.name === map)?.fields.find(x => x.name === field); return field === 'has' || f?.sort.k === 'bool' ? false : f?.sort.k === 'choice' ? f.sort.values[0] : 0; };
|
|
960
1006
|
const cell = (map, key, field) => (lists[map] ? lists[map][key][field] : dec[map]?.[String(key)]?.[field] ?? dflt(map, field));
|
|
961
1007
|
for (const d of m.folds ?? []) {
|
|
1008
|
+
if (d.loop?.cond) {
|
|
1009
|
+
carryLoop(d, raw, st, params, lists, dec, cell);
|
|
1010
|
+
continue;
|
|
1011
|
+
}
|
|
962
1012
|
// (the passes: a carried list's length, or one past a list of the state's last place)
|
|
963
1013
|
const env = { ...params, ...st.leaves, ...raw }, runs = [];
|
|
964
1014
|
const n = d.state ? Math.max(-1, ...Object.entries(dec[d.list] ?? {}).filter(([, c]) => c.has).map(([k]) => Number(k))) + 1 : (lists[d.list] ?? []).length;
|
|
965
1015
|
const at = (t, i) => evalTerm(t.e, i === undefined ? env : { ...env, [I.PASS]: i }, cell);
|
|
966
1016
|
runs.push(Object.fromEntries(d.fields.map(f => [f.name, at(d.init[f.name])])));
|
|
967
1017
|
lists[d.run] = runs;
|
|
1018
|
+
// (from the last place down: the start at n, each pass's output at its own place)
|
|
1019
|
+
if (d.back) {
|
|
1020
|
+
const out = Array.from({ length: n + 1 });
|
|
1021
|
+
out[n] = runs[0];
|
|
1022
|
+
lists[d.run] = out;
|
|
1023
|
+
for (let i = n - 1; i >= 0; i--)
|
|
1024
|
+
out[i] = Object.fromEntries(d.fields.map(f => [f.name, at(d.step[f.name], i)]));
|
|
1025
|
+
raw[d.run] = out;
|
|
1026
|
+
continue;
|
|
1027
|
+
}
|
|
968
1028
|
for (let i = 0; i < n; i++)
|
|
969
1029
|
runs.push(Object.fromEntries(d.fields.map(f => [f.name, at(d.step[f.name], i)])));
|
|
970
1030
|
raw[d.run] = runs;
|
|
971
1031
|
}
|
|
972
1032
|
}
|
|
1033
|
+
// A while loop's run over lists by an order, as its passes compute it: each list's held keys sorted by the order (ties
|
|
1034
|
+
// in place order: the rule refuses there, an edge), then from the start each pass where the condition holds.
|
|
1035
|
+
function carryLoop(d, raw, st, params, lists, dec, cell) {
|
|
1036
|
+
const l = d.loop, env = { ...params, ...st.leaves, ...raw }, runs = [];
|
|
1037
|
+
const places = new Map();
|
|
1038
|
+
for (const g of l.drains ?? []) {
|
|
1039
|
+
const keys = Object.keys(dec[g.map] ?? {}).map(Number).filter(k => evalTerm(g.held, { ...env, kl$: k }, cell) === true).sort((a, b) => a - b);
|
|
1040
|
+
const parts = (k) => g.by.map(b => ({ v: evalTerm(b.e, { ...env, kl$: k }, cell), desc: b.desc }));
|
|
1041
|
+
keys.sort((x, y) => { const a = parts(x), b = parts(y); for (let j = 0; j < a.length; j++) {
|
|
1042
|
+
const c = a[j].v - b[j].v;
|
|
1043
|
+
if (c)
|
|
1044
|
+
return a[j].desc ? -c : c;
|
|
1045
|
+
} return x - y; });
|
|
1046
|
+
lists[g.ord] = keys.map(k => ({ k }));
|
|
1047
|
+
raw[g.ord] = lists[g.ord];
|
|
1048
|
+
raw[`${g.ord}_len`] = keys.length;
|
|
1049
|
+
env[`${g.ord}_len`] = keys.length;
|
|
1050
|
+
places.set(g.rk, new Map(keys.map((k, i) => [k, i])));
|
|
1051
|
+
raw[g.rk] = places.get(g.rk);
|
|
1052
|
+
}
|
|
1053
|
+
const cell2 = (map, key, field) => (places.has(map) ? places.get(map).get(key) ?? 0 : cell(map, key, field));
|
|
1054
|
+
const at = (t, i) => evalTerm(t.e, { ...env, [I.PASS]: i }, cell2);
|
|
1055
|
+
lists[d.run] = runs;
|
|
1056
|
+
runs.push(Object.fromEntries(d.fields.filter(f => d.init[f.name]).map(f => [f.name, at(d.init[f.name], 0)])));
|
|
1057
|
+
for (let i = 0; evalTerm(l.cond, { ...env, [I.PASS]: i }, cell2) === true && i < 100_000; i++)
|
|
1058
|
+
runs.push(Object.fromEntries(d.fields.filter(f => d.step[f.name]).map(f => [f.name, at(d.step[f.name], i)])));
|
|
1059
|
+
raw[d.run] = runs;
|
|
1060
|
+
if (d.len.op === 'var')
|
|
1061
|
+
raw[d.len.name] = runs.length - 1;
|
|
1062
|
+
}
|
|
1063
|
+
// Whether two held items of a list a while loop takes by an order tie under it (the rule's claim stops there).
|
|
1064
|
+
export function tiesIn(m, s, raw) {
|
|
1065
|
+
for (const d of m.folds ?? [])
|
|
1066
|
+
for (const g of d.loop?.drains ?? []) {
|
|
1067
|
+
// (only where the loop runs: the request its method's, on its branch)
|
|
1068
|
+
const cell0 = (map, key, field) => (field === 'has' ? !!s.maps[map]?.[String(key)] : s.maps[map]?.[String(key)]?.[field] ?? 0);
|
|
1069
|
+
if (d.loop.entry && evalTerm(d.loop.entry, { ...s.leaves, ...raw }, cell0) !== true)
|
|
1070
|
+
continue;
|
|
1071
|
+
const cells = s.maps[g.map] ?? {}, cell = (map, key, field) => (field === 'has' ? !!s.maps[map]?.[String(key)] : s.maps[map]?.[String(key)]?.[field] ?? 0);
|
|
1072
|
+
const seen = new Set();
|
|
1073
|
+
for (const k of Object.keys(cells).map(Number)) {
|
|
1074
|
+
const env = { ...s.leaves, ...raw, kl$: k };
|
|
1075
|
+
if (evalTerm(g.held, env, cell) !== true)
|
|
1076
|
+
continue;
|
|
1077
|
+
const key = JSON.stringify(g.by.map(b => evalTerm(b.e, env, cell)));
|
|
1078
|
+
if (seen.has(key))
|
|
1079
|
+
return true;
|
|
1080
|
+
seen.add(key);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
return false;
|
|
1084
|
+
}
|
|
973
1085
|
// A term at concrete values (a constant field over the params, a start, the precondition; with `cell`, a state's
|
|
974
1086
|
// map cells and list items too).
|
|
975
1087
|
export function evalTerm(t, env, cell) {
|
package/dist/core/extract-ex.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Refusal, pathOf } from "./extract-read.js";
|
|
1
|
+
import { Refusal, pathOf, readerRun, statedRefusal } from "./extract-read.js";
|
|
2
2
|
import { asset, extractStep, node } from "./extract-step.js";
|
|
3
3
|
export { proveRule, proveSteps } from "./dsl.js";
|
|
4
4
|
const here = (f) => asset(`ex/${f}`);
|
|
@@ -6,10 +6,10 @@ export function readElixir(file, module, elixir = 'elixir') {
|
|
|
6
6
|
const cp = node('node:child_process'), path = node('node:path');
|
|
7
7
|
let out;
|
|
8
8
|
try {
|
|
9
|
-
out = cp.execFileSync(elixir, [here('reader.exs'), JSON.stringify({ file: path.resolve(file), module: module ?? null })], { encoding: 'utf8', maxBuffer: 1 << 29, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
9
|
+
out = cp.execFileSync(elixir, [here('reader.exs'), JSON.stringify({ file: path.resolve(file), module: module ?? null })], { encoding: 'utf8', maxBuffer: 1 << 29, stdio: ['ignore', 'pipe', 'pipe'], ...readerRun() });
|
|
10
10
|
}
|
|
11
11
|
catch (e) {
|
|
12
|
-
throw new Refusal(file, `the Elixir reader didn't run (${String(e.stderr ?? e.message).split('\n').filter(Boolean).slice(0, 2).join(' | ')}): elixir is needed to extract Elixir`);
|
|
12
|
+
throw new Refusal(file, statedRefusal(e) ?? `the Elixir reader didn't run (${String(e.stderr ?? e.message).split('\n').filter(Boolean).slice(0, 2).join(' | ')}): elixir is needed to extract Elixir`);
|
|
13
13
|
}
|
|
14
14
|
return JSON.parse(out);
|
|
15
15
|
}
|
|
@@ -5,7 +5,7 @@ import { type GoOptions, type GoPkg, type GoRead } from './extract-go.js';
|
|
|
5
5
|
export type GoLeaf = {
|
|
6
6
|
name: string;
|
|
7
7
|
path: string[];
|
|
8
|
-
kind: 'int' | 'bool' | 'str' | 'big' | 'bignil' | 'map' | 'mapnil' | 'list' | 'frame';
|
|
8
|
+
kind: 'int' | 'bool' | 'str' | 'big' | 'bignil' | 'map' | 'mapnil' | 'list' | 'frame' | 'sink';
|
|
9
9
|
t: number;
|
|
10
10
|
go: string;
|
|
11
11
|
};
|