@milkenjoyer/toolchain 0.11.0 → 0.13.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 +190 -17
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -0
- package/dist/conformance.d.mts +18 -0
- package/dist/conformance.mjs +31 -0
- package/dist/effect-plugin.mjs +1 -1
- package/dist/exceptions-Dybl9xpc.mjs +11 -0
- package/dist/exceptions.mjs +1 -1
- package/dist/foldkit-plugin.mjs +1 -1
- package/dist/formal.d.mts +131 -3
- package/dist/formal.mjs +1006 -146
- package/dist/guard.mjs +2 -2
- package/dist/knip.mjs +2 -1
- package/dist/{lint-JSnPsGO1.mjs → lint-IhmtnBbx.mjs} +3 -3
- package/dist/lint.mjs +1 -1
- package/dist/plugin.mjs +1 -1
- package/dist/proof.mjs +1 -1
- package/dist/trace-file-KuPRWgwt.mjs +17 -0
- package/dist/verdict.d.mts +1 -1
- package/dist/verdict.mjs +1 -1
- package/dist/{verification-p2rSx5lc.d.mts → verification-FgQFU1YN.d.mts} +1 -1
- package/dist/verification.d.mts +1 -1
- package/dist/verification.mjs +1 -1
- package/package.json +12 -3
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/cfg.ts +58 -1
- package/src/formal/checks.ts +57 -52
- package/src/formal/coverage.ts +79 -0
- package/src/formal/gate.ts +74 -0
- package/src/formal/git.ts +38 -0
- package/src/formal/proof-run.ts +77 -0
- package/src/formal/proofs.ts +226 -0
- package/src/formal/prover.ts +291 -0
- package/src/formal/semantics.ts +110 -0
- package/src/formal/specs.ts +103 -31
- package/src/formal/tla.ts +60 -19
- package/src/formal/trace-file.ts +35 -0
- package/src/formal/traces.ts +293 -0
- package/src/formal/violations.ts +76 -0
- package/src/formal/z3.ts +321 -0
- package/src/formal.ts +10 -3
- package/dist/exceptions-Bq-9MxoR.mjs +0 -3
- package/dist/{effect-plugin-kYxB-5LJ.mjs → effect-plugin-BXUx9PPu.mjs} +1 -1
- package/dist/{plugin-BoBRGtGf.mjs → plugin-CwxhPtui.mjs} +1 -1
- package/dist/{verification-6s2chr4X.mjs → verification-oAc1Q6I9.mjs} +1 -1
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { basename, dirname, relative } from "node:path";
|
|
3
|
+
|
|
4
|
+
import * as Clock from "effect/Clock";
|
|
5
|
+
import * as Console from "effect/Console";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import { FileSystem } from "effect/FileSystem";
|
|
8
|
+
import * as Option from "effect/Option";
|
|
9
|
+
import { ChildProcess } from "effect/process";
|
|
10
|
+
import * as Schema from "effect/Schema";
|
|
11
|
+
import * as Stream from "effect/Stream";
|
|
12
|
+
|
|
13
|
+
import { derive } from "./cfg.ts";
|
|
14
|
+
import type { Check } from "./checks.ts";
|
|
15
|
+
import { Gate } from "./gate.ts";
|
|
16
|
+
import { FormalError, Tla } from "./tla.ts";
|
|
17
|
+
import { FORMAL_TRACES, receiptPath, sorted } from "./trace-file.ts";
|
|
18
|
+
import { sandbox } from "./violations.ts";
|
|
19
|
+
const quote = (s: string) => JSON.stringify(s);
|
|
20
|
+
const RawState = Schema.Record(Schema.String, Schema.Json);
|
|
21
|
+
const Raw = Schema.Array(RawState);
|
|
22
|
+
const decodeAction = Schema.decodeUnknownSync(
|
|
23
|
+
Schema.Struct({
|
|
24
|
+
name: Schema.String,
|
|
25
|
+
context: Schema.optionalKey(
|
|
26
|
+
Schema.NullOr(Schema.Record(Schema.String, Schema.Json))
|
|
27
|
+
),
|
|
28
|
+
location: Schema.optionalKey(Schema.Json),
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
export const normalize = (
|
|
32
|
+
text: string,
|
|
33
|
+
plan: {
|
|
34
|
+
count: number;
|
|
35
|
+
depth: number;
|
|
36
|
+
init: string;
|
|
37
|
+
actions: ReadonlyMap<string, number>;
|
|
38
|
+
variables: ReadonlyArray<string>;
|
|
39
|
+
constants: Record<string, Schema.Json>;
|
|
40
|
+
}
|
|
41
|
+
): string => {
|
|
42
|
+
const lines =
|
|
43
|
+
text === ""
|
|
44
|
+
? []
|
|
45
|
+
: (text.endsWith("\n") ? text.slice(0, -1) : text).split("\n");
|
|
46
|
+
if (lines.length !== plan.count)
|
|
47
|
+
throw new Error(
|
|
48
|
+
`only ${lines.length} of ${plan.count} behaviors reached depth ${plan.depth}`
|
|
49
|
+
);
|
|
50
|
+
return (
|
|
51
|
+
lines
|
|
52
|
+
.map((line, index) => {
|
|
53
|
+
const states = Schema.decodeSync(Schema.fromJsonString(Raw))(line);
|
|
54
|
+
if (states.length !== plan.depth)
|
|
55
|
+
throw new Error(
|
|
56
|
+
`trace ${index} has ${states.length} states; expected ${plan.depth}`
|
|
57
|
+
);
|
|
58
|
+
const steps = states.map((s, i) => {
|
|
59
|
+
if (s.formal_tick !== i)
|
|
60
|
+
throw new Error(`trace ${index} state ${i} has wrong formal_tick`);
|
|
61
|
+
const action = decodeAction(s._action);
|
|
62
|
+
const name = action.name.replace(/^Formal_/u, "");
|
|
63
|
+
if (
|
|
64
|
+
action.name !== `Formal_${name}` ||
|
|
65
|
+
(i === 0 ? name !== plan.init : !plan.actions.has(name))
|
|
66
|
+
)
|
|
67
|
+
throw new Error(
|
|
68
|
+
`trace ${index} state ${i} has unknown action ${action.name}`
|
|
69
|
+
);
|
|
70
|
+
const args = action.context ?? {};
|
|
71
|
+
if (
|
|
72
|
+
Object.keys(args).length !== (i === 0 ? 0 : plan.actions.get(name))
|
|
73
|
+
)
|
|
74
|
+
throw new Error(`trace ${index} state ${i} has wrong action arity`);
|
|
75
|
+
const keys = Object.keys(s).toSorted();
|
|
76
|
+
const expected = [
|
|
77
|
+
...plan.variables,
|
|
78
|
+
"_action",
|
|
79
|
+
"formal_tick",
|
|
80
|
+
].toSorted();
|
|
81
|
+
if (JSON.stringify(keys) !== JSON.stringify(expected))
|
|
82
|
+
throw new Error(`trace ${index} state ${i} has wrong variables`);
|
|
83
|
+
return {
|
|
84
|
+
action: name,
|
|
85
|
+
args,
|
|
86
|
+
state: Object.fromEntries(
|
|
87
|
+
Object.entries(s).filter(
|
|
88
|
+
([k]) => k !== "_action" && k !== "formal_tick"
|
|
89
|
+
)
|
|
90
|
+
),
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
return JSON.stringify(
|
|
94
|
+
sorted({ index, constants: plan.constants, steps })
|
|
95
|
+
);
|
|
96
|
+
})
|
|
97
|
+
.join("\n") + "\n"
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
export const traces: Check = (repo) =>
|
|
101
|
+
Effect.gen(function* () {
|
|
102
|
+
const fs = yield* FileSystem;
|
|
103
|
+
const tla = yield* Tla;
|
|
104
|
+
const gate = yield* Gate;
|
|
105
|
+
const problems: { file: string; message: string }[] = [];
|
|
106
|
+
for (const m of repo.models) {
|
|
107
|
+
if (m.entry.mode !== "gate" || !m.entry.traces) continue;
|
|
108
|
+
const t = m.entry.traces;
|
|
109
|
+
const result = yield* gate.run(m);
|
|
110
|
+
if (result._tag === "Failed") continue;
|
|
111
|
+
const finding = yield* Effect.scoped(
|
|
112
|
+
Effect.gen(function* () {
|
|
113
|
+
const section = (k: string) =>
|
|
114
|
+
m.config.cfg.sections.find((s) => s.keyword === k)?.text;
|
|
115
|
+
const spec = section("SPECIFICATION");
|
|
116
|
+
const pair =
|
|
117
|
+
spec !== undefined
|
|
118
|
+
? m.semantics.specifications.get(spec)
|
|
119
|
+
: { init: section("INIT"), next: section("NEXT") };
|
|
120
|
+
if (pair?.init === undefined || pair.next === undefined)
|
|
121
|
+
return "cannot derive zero-argument INIT/NEXT from config";
|
|
122
|
+
const { init, next } = pair;
|
|
123
|
+
const names = [
|
|
124
|
+
...new Set(result.coverage.actions.map((a) => a.name)),
|
|
125
|
+
];
|
|
126
|
+
const actions = new Map<string, number>();
|
|
127
|
+
for (const name of names) {
|
|
128
|
+
const op = m.semantics.operators.get(name);
|
|
129
|
+
if (name === next || !op || op.params.some((p) => p.arity !== 0))
|
|
130
|
+
return `cannot wrap action ${name}: unnamed, INSTANCE'd, or higher-order action`;
|
|
131
|
+
actions.set(name, op.params.length);
|
|
132
|
+
}
|
|
133
|
+
for (const name of Object.keys(t.steer ?? {}))
|
|
134
|
+
if (!actions.has(name)) return `steer key ${name} is not an action`;
|
|
135
|
+
if (
|
|
136
|
+
[...m.semantics.operators.keys(), ...m.semantics.variables].some(
|
|
137
|
+
(n) =>
|
|
138
|
+
n === "FormalTraces" ||
|
|
139
|
+
n === "formal_tick" ||
|
|
140
|
+
n.startsWith("Formal_")
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
return "generated trace name collides with spec";
|
|
144
|
+
const dir = yield* sandbox(repo, m);
|
|
145
|
+
const raw = `${dir}/raw.ndjson`;
|
|
146
|
+
const module = basename(m.path, ".tla");
|
|
147
|
+
const slash = "\\";
|
|
148
|
+
const defs = names.map((name) => {
|
|
149
|
+
const params =
|
|
150
|
+
m.semantics.operators.get(name)?.params.map((p) => p.name) ?? [];
|
|
151
|
+
const args = params.length ? `(${params.join(", ")})` : "";
|
|
152
|
+
const keep = t.steer?.[name];
|
|
153
|
+
return `Formal_${name}${args} == Formal_M!${name}${args} /${slash} formal_tick' = formal_tick + 1${keep === undefined ? "" : ` /${slash} Formal_TLC!RandomElement(1 .. ${keep}) = 1`}`;
|
|
154
|
+
});
|
|
155
|
+
const wrapper = [
|
|
156
|
+
"---- MODULE FormalTraces ----",
|
|
157
|
+
`EXTENDS ${module}`,
|
|
158
|
+
"VARIABLE formal_tick",
|
|
159
|
+
`Formal_M == INSTANCE ${module}`,
|
|
160
|
+
"Formal_TLC == INSTANCE TLC",
|
|
161
|
+
"Formal_Ext == INSTANCE TLCExt",
|
|
162
|
+
"Formal_IO == INSTANCE IOUtils",
|
|
163
|
+
`Formal_${init} == Formal_M!${init} /${slash} formal_tick = 0`,
|
|
164
|
+
...defs,
|
|
165
|
+
'Formal_Count == Formal_TLC!TLCGet("stats").traces',
|
|
166
|
+
`Formal_Fresh == 1 ${slash}notin DOMAIN Formal_TLC!TLCGet("all") ${slash}/ Formal_TLC!TLCGet(1) # Formal_Count`,
|
|
167
|
+
`Formal_Export == (Formal_TLC!TLCGet("level") = ${t.depth} /${slash} Formal_Fresh) =>`,
|
|
168
|
+
` /${slash} Formal_TLC!TLCSet(1, Formal_Count)`,
|
|
169
|
+
` /${slash} Formal_IO!Serialize(<<Formal_Ext!Trace>>, ${quote(raw)}, [format |-> "NDJSON", charset |-> "UTF-8", openOptions |-> <<"WRITE", "CREATE", "APPEND">>])`,
|
|
170
|
+
"====",
|
|
171
|
+
"",
|
|
172
|
+
].join("\n");
|
|
173
|
+
yield* fs.writeFileString(`${dir}/FormalTraces.tla`, wrapper);
|
|
174
|
+
const cfg = derive(m.config.cfg, {
|
|
175
|
+
simulation: {
|
|
176
|
+
init,
|
|
177
|
+
next,
|
|
178
|
+
overrides: [
|
|
179
|
+
`${init} <- Formal_${init}`,
|
|
180
|
+
...names.map((n) => `${n} <- Formal_${n}`),
|
|
181
|
+
],
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
yield* fs.writeFileString(`${dir}/FormalTraces.cfg`, cfg);
|
|
185
|
+
const run = yield* tla.tlc({
|
|
186
|
+
spec: `${dir}/FormalTraces.tla`,
|
|
187
|
+
config: `${dir}/FormalTraces.cfg`,
|
|
188
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
189
|
+
mode: {
|
|
190
|
+
_tag: "Simulate",
|
|
191
|
+
num: t.count,
|
|
192
|
+
depth: t.depth,
|
|
193
|
+
seed: t.seed,
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
if (run._tag === "TimedOut")
|
|
197
|
+
return `simulation timed out after ${m.entry.timeoutSeconds} s`;
|
|
198
|
+
if (
|
|
199
|
+
run.code !== 0 ||
|
|
200
|
+
run.messages.some((x) => x.severity >= 1 && x.severity <= 3)
|
|
201
|
+
)
|
|
202
|
+
return `simulation exited ${run.code}: ${run.messages
|
|
203
|
+
.filter((x) => x.severity >= 1 && x.severity <= 3)
|
|
204
|
+
.map((x) => x.text)
|
|
205
|
+
.join("; ")}`;
|
|
206
|
+
const text = (yield* fs.exists(raw))
|
|
207
|
+
? yield* fs.readFileString(raw)
|
|
208
|
+
: "";
|
|
209
|
+
const normalized = yield* Effect.try({
|
|
210
|
+
try: () =>
|
|
211
|
+
normalize(text, {
|
|
212
|
+
count: t.count,
|
|
213
|
+
depth: t.depth,
|
|
214
|
+
init,
|
|
215
|
+
actions,
|
|
216
|
+
variables: m.semantics.variables,
|
|
217
|
+
constants: Object.fromEntries(
|
|
218
|
+
m.config.cfg.constants.flatMap((c) =>
|
|
219
|
+
c._tag === "Value"
|
|
220
|
+
? [
|
|
221
|
+
[
|
|
222
|
+
c.name,
|
|
223
|
+
/^-?\d+$/u.test(c.value)
|
|
224
|
+
? Number(c.value)
|
|
225
|
+
: c.value,
|
|
226
|
+
],
|
|
227
|
+
]
|
|
228
|
+
: []
|
|
229
|
+
)
|
|
230
|
+
),
|
|
231
|
+
}),
|
|
232
|
+
catch: (e) =>
|
|
233
|
+
new FormalError({
|
|
234
|
+
message: e instanceof Error ? e.message : String(e),
|
|
235
|
+
}),
|
|
236
|
+
});
|
|
237
|
+
const file = `${dir}/traces.ndjson`;
|
|
238
|
+
yield* fs.writeFileString(file, normalized);
|
|
239
|
+
let owner = dirname(t.conformance);
|
|
240
|
+
while (
|
|
241
|
+
owner !== "." &&
|
|
242
|
+
!repo.tracked.includes(`${owner}/package.json`)
|
|
243
|
+
)
|
|
244
|
+
owner = dirname(owner);
|
|
245
|
+
const started = yield* Clock.currentTimeMillis;
|
|
246
|
+
const conformance = yield* Effect.scoped(
|
|
247
|
+
Effect.gen(function* () {
|
|
248
|
+
const p = yield* ChildProcess.make(
|
|
249
|
+
"vp",
|
|
250
|
+
["test", "run", relative(owner, t.conformance)],
|
|
251
|
+
{
|
|
252
|
+
cwd: `${repo.root}/${owner}`,
|
|
253
|
+
env: { [FORMAL_TRACES]: file },
|
|
254
|
+
extendEnv: true,
|
|
255
|
+
stdin: "ignore",
|
|
256
|
+
killSignal: "SIGKILL",
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
return yield* Effect.all(
|
|
260
|
+
[
|
|
261
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
262
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
263
|
+
p.exitCode,
|
|
264
|
+
],
|
|
265
|
+
{ concurrency: "unbounded" }
|
|
266
|
+
);
|
|
267
|
+
}).pipe(Effect.timeoutOption(`${m.entry.timeoutSeconds} seconds`))
|
|
268
|
+
);
|
|
269
|
+
if (Option.isNone(conformance))
|
|
270
|
+
return `conformance test ${t.conformance} timed out`;
|
|
271
|
+
if (Number(conformance.value[2]) !== 0) {
|
|
272
|
+
yield* Console.log(
|
|
273
|
+
(conformance.value[0] + conformance.value[1]).slice(-4000)
|
|
274
|
+
);
|
|
275
|
+
return `conformance test ${t.conformance} failed (vp test exited ${conformance.value[2]})`;
|
|
276
|
+
}
|
|
277
|
+
const receipt = receiptPath(file);
|
|
278
|
+
const replayed = (yield* fs.exists(receipt))
|
|
279
|
+
? (yield* fs.readFileString(receipt)).trim()
|
|
280
|
+
: "0";
|
|
281
|
+
if (replayed !== String(t.count))
|
|
282
|
+
return `conformance test replayed ${replayed} of ${t.count} traces; register them with replayTraces`;
|
|
283
|
+
yield* Console.log(
|
|
284
|
+
`traces: ${m.path}: ${t.count} traces of ${t.depth} states (sha256 ${createHash("sha256").update(normalized).digest("hex")}), conformance passed in ${((yield* Clock.currentTimeMillis) - started) / 1000} s`
|
|
285
|
+
);
|
|
286
|
+
return undefined;
|
|
287
|
+
})
|
|
288
|
+
).pipe(Effect.catch((e) => Effect.succeed(e.message)));
|
|
289
|
+
if (finding !== undefined)
|
|
290
|
+
problems.push({ file: m.path, message: finding });
|
|
291
|
+
}
|
|
292
|
+
return problems;
|
|
293
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { dirname, basename } from "node:path";
|
|
2
|
+
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import { FileSystem } from "effect/FileSystem";
|
|
5
|
+
|
|
6
|
+
import { derive, parseCfg } from "./cfg.ts";
|
|
7
|
+
import type { Model, Repo } from "./specs.ts";
|
|
8
|
+
import { Tla } from "./tla.ts";
|
|
9
|
+
export const sandbox = Effect.fn("formal.sandbox")(function* (
|
|
10
|
+
repo: Repo,
|
|
11
|
+
model: Model
|
|
12
|
+
) {
|
|
13
|
+
const fs = yield* FileSystem;
|
|
14
|
+
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "formal-model-" });
|
|
15
|
+
for (const file of repo.tracked.filter(
|
|
16
|
+
(f) => dirname(f) === dirname(model.path) && f.endsWith(".tla")
|
|
17
|
+
))
|
|
18
|
+
yield* fs.copyFile(`${repo.root}/${file}`, `${dir}/${basename(file)}`);
|
|
19
|
+
return dir;
|
|
20
|
+
});
|
|
21
|
+
export const held = Symbol("held");
|
|
22
|
+
export const violate = Effect.fn("formal.violate")(function* (
|
|
23
|
+
repo: Repo,
|
|
24
|
+
m: Model,
|
|
25
|
+
target: string,
|
|
26
|
+
edit?: Model["entry"]["breaks"][number]
|
|
27
|
+
) {
|
|
28
|
+
return yield* Effect.scoped(
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
const fs = yield* FileSystem;
|
|
31
|
+
const tla = yield* Tla;
|
|
32
|
+
const dir = yield* sandbox(repo, m);
|
|
33
|
+
let cfg = m.config.cfg;
|
|
34
|
+
if (edit) {
|
|
35
|
+
const file = edit.file ?? m.path;
|
|
36
|
+
const text = yield* fs.readFileString(`${repo.root}/${file}`);
|
|
37
|
+
const changed = text.replace(edit.find, edit.replace);
|
|
38
|
+
if (file === m.config.path) {
|
|
39
|
+
const parsed = parseCfg(changed);
|
|
40
|
+
if ("_tag" in parsed) return parsed.message;
|
|
41
|
+
cfg = parsed;
|
|
42
|
+
} else yield* fs.writeFileString(`${dir}/${basename(file)}`, changed);
|
|
43
|
+
}
|
|
44
|
+
const level =
|
|
45
|
+
m.semantics.operators.get(target)?.level ??
|
|
46
|
+
(cfg.properties.includes(target) ? 3 : 1);
|
|
47
|
+
const config = `${dir}/Check.cfg`;
|
|
48
|
+
yield* fs.writeFileString(
|
|
49
|
+
config,
|
|
50
|
+
derive(cfg, { target: { name: target, level } })
|
|
51
|
+
);
|
|
52
|
+
const run = yield* tla.tlc({
|
|
53
|
+
spec: `${dir}/${basename(m.path)}`,
|
|
54
|
+
config,
|
|
55
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
56
|
+
mode: { _tag: "Check", coverage: false },
|
|
57
|
+
});
|
|
58
|
+
if (run._tag === "TimedOut")
|
|
59
|
+
return `TLC timed out after ${m.entry.timeoutSeconds} s`;
|
|
60
|
+
if (run.code === 0) return held;
|
|
61
|
+
const violated = [2107, 2108, 2110, 2112, 2116];
|
|
62
|
+
if (
|
|
63
|
+
[12, 13].includes(run.code) &&
|
|
64
|
+
run.messages.some((x) => violated.includes(x.code)) &&
|
|
65
|
+
!run.messages.some(
|
|
66
|
+
(x) =>
|
|
67
|
+
x.severity >= 1 &&
|
|
68
|
+
x.severity <= 3 &&
|
|
69
|
+
![...violated, 2121, 2264].includes(x.code)
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
return undefined;
|
|
73
|
+
return `TLC exited ${run.code}; ${run.messages.find((x) => x.severity >= 1 && x.severity <= 3)?.text ?? "expected a violation of " + target}`;
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
});
|
package/src/formal/z3.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
import { init, killThreads } from "z3-solver";
|
|
5
|
+
import type { Arith, BitVec, Bool, Context, Expr, Solver } from "z3-solver";
|
|
6
|
+
|
|
7
|
+
import { invalid, message } from "./proof-run.ts";
|
|
8
|
+
import type { Domain, Outcome, Request } from "./proof-run.ts";
|
|
9
|
+
|
|
10
|
+
type Term = Arith | BitVec | Bool;
|
|
11
|
+
type Body = {
|
|
12
|
+
readonly assume?: (...args: unknown[]) => unknown;
|
|
13
|
+
readonly model: (...args: unknown[]) => unknown;
|
|
14
|
+
readonly code: (...args: unknown[]) => unknown;
|
|
15
|
+
readonly holds: (...args: unknown[]) => unknown;
|
|
16
|
+
};
|
|
17
|
+
type Tuple = Record<string, number>;
|
|
18
|
+
const random = (path: string) => {
|
|
19
|
+
let seed = 0xcbf29ce484222325n;
|
|
20
|
+
const mask = (1n << 64n) - 1n;
|
|
21
|
+
for (const byte of new TextEncoder().encode(path))
|
|
22
|
+
seed = ((seed ^ BigInt(byte)) * 0x100000001b3n) & mask;
|
|
23
|
+
return (min: number, max: number) => {
|
|
24
|
+
const span = BigInt(max) - BigInt(min) + 1n;
|
|
25
|
+
const limit = (1n << 64n) - ((1n << 64n) % span);
|
|
26
|
+
for (;;) {
|
|
27
|
+
seed = (seed + 0x9e3779b97f4a7c15n) & mask;
|
|
28
|
+
let z = seed;
|
|
29
|
+
z = ((z ^ (z >> 30n)) * 0xbf58476d1ce4e5b9n) & mask;
|
|
30
|
+
z = ((z ^ (z >> 27n)) * 0x94d049bb133111ebn) & mask;
|
|
31
|
+
z ^= z >> 31n;
|
|
32
|
+
if (z < limit) return Number(BigInt(min) + (z % span));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const corners = (domains: ReadonlyArray<Domain>) => {
|
|
37
|
+
let tuples: Tuple[] = [{}];
|
|
38
|
+
for (const d of domains) {
|
|
39
|
+
const values = [...new Set([d.min, d.min + 1, d.max - 1, d.max])].filter(
|
|
40
|
+
(n) => n >= d.min && n <= d.max
|
|
41
|
+
);
|
|
42
|
+
tuples = tuples
|
|
43
|
+
.flatMap((at) => values.map((n) => ({ ...at, [d.name]: n })))
|
|
44
|
+
.slice(0, 1000);
|
|
45
|
+
}
|
|
46
|
+
return tuples;
|
|
47
|
+
};
|
|
48
|
+
const bits = async (
|
|
49
|
+
Z3: Context,
|
|
50
|
+
claims: ReadonlyArray<Bool>
|
|
51
|
+
): Promise<string | undefined> => {
|
|
52
|
+
if (claims.length === 0) return "";
|
|
53
|
+
const parts = claims.map((c) =>
|
|
54
|
+
Z3.If(c, Z3.BitVec.val(1, 1), Z3.BitVec.val(0, 1))
|
|
55
|
+
);
|
|
56
|
+
const first = parts[0];
|
|
57
|
+
if (!first) return "";
|
|
58
|
+
let layer: BitVec[] = parts;
|
|
59
|
+
while (layer.length > 1000) {
|
|
60
|
+
const next: BitVec[] = [];
|
|
61
|
+
for (let i = 0; i < layer.length; i += 1000) {
|
|
62
|
+
const group = layer.slice(i, i + 1000);
|
|
63
|
+
const head = group[0];
|
|
64
|
+
if (head) next.push(group.length === 1 ? head : Z3.Concat(...group));
|
|
65
|
+
}
|
|
66
|
+
layer = next;
|
|
67
|
+
}
|
|
68
|
+
const term = layer.length === 1 ? (layer[0] ?? first) : Z3.Concat(...layer);
|
|
69
|
+
const result = await Z3.simplify(term);
|
|
70
|
+
return Z3.isBitVecVal(result)
|
|
71
|
+
? result.value().toString(2).padStart(parts.length, "0")
|
|
72
|
+
: undefined;
|
|
73
|
+
};
|
|
74
|
+
export const prove = async (
|
|
75
|
+
request: Request,
|
|
76
|
+
body: Body,
|
|
77
|
+
domains: ReadonlyArray<Domain>,
|
|
78
|
+
started: number
|
|
79
|
+
): Promise<Outcome> => {
|
|
80
|
+
const api = await init();
|
|
81
|
+
const makeContext = api.Context;
|
|
82
|
+
const em: unknown = api.em;
|
|
83
|
+
try {
|
|
84
|
+
const Z3 = makeContext("main");
|
|
85
|
+
const symbols: Record<string, Arith | BitVec> = {};
|
|
86
|
+
const domain: Bool[] = [];
|
|
87
|
+
for (const d of domains) {
|
|
88
|
+
if (d.kind === "uint") symbols[d.name] = Z3.BitVec.const(d.name, d.bits);
|
|
89
|
+
else {
|
|
90
|
+
const term = Z3.Int.const(d.name);
|
|
91
|
+
symbols[d.name] = term;
|
|
92
|
+
domain.push(term.ge(d.min), term.le(d.max));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const isTerm = (value: unknown): value is Term =>
|
|
96
|
+
Z3.isArith(value) || Z3.isBitVec(value) || Z3.isBool(value);
|
|
97
|
+
const output = body.model(symbols, Z3);
|
|
98
|
+
const claim = body.holds(symbols, output, Z3);
|
|
99
|
+
const assumption = body.assume
|
|
100
|
+
? body.assume(symbols, Z3)
|
|
101
|
+
: Z3.Bool.val(true);
|
|
102
|
+
if (!Z3.isBool(claim) || !Z3.isBool(assumption))
|
|
103
|
+
return invalid("holds and assume must return Bool terms");
|
|
104
|
+
const scalar = Z3.isExpr(output);
|
|
105
|
+
const record = scalar
|
|
106
|
+
? { value: output }
|
|
107
|
+
: Schema.decodeUnknownSync(Schema.Record(Schema.String, Schema.Unknown))(
|
|
108
|
+
output
|
|
109
|
+
);
|
|
110
|
+
const sorts = new Set<string>();
|
|
111
|
+
const seen = new Set<number>();
|
|
112
|
+
const walk = (expr: Expr): void => {
|
|
113
|
+
if (seen.has(expr.id())) return;
|
|
114
|
+
seen.add(expr.id());
|
|
115
|
+
sorts.add(String(expr.sort.name()));
|
|
116
|
+
if (Z3.isApp(expr) || Z3.isQuantifier(expr))
|
|
117
|
+
for (const child of expr.children()) walk(child);
|
|
118
|
+
};
|
|
119
|
+
for (const root of [assumption, claim, ...Object.values(record)])
|
|
120
|
+
if (Z3.isExpr(root)) walk(root);
|
|
121
|
+
const main = new Z3.Solver();
|
|
122
|
+
main.add(...domain, assumption, Z3.Not(claim));
|
|
123
|
+
if (["Real", "FloatingPoint", "RoundingMode"].some((s) => sorts.has(s)))
|
|
124
|
+
return invalid(
|
|
125
|
+
"the claim uses Real or Float arithmetic; money is whole numbers, so state it over Int or BitVec"
|
|
126
|
+
);
|
|
127
|
+
const terms: [string, Term][] = [];
|
|
128
|
+
for (const [key, term] of Object.entries(record)) {
|
|
129
|
+
if (!isTerm(term))
|
|
130
|
+
return invalid(
|
|
131
|
+
"model must return an Int, BitVec, Bool or a record of those terms"
|
|
132
|
+
);
|
|
133
|
+
terms.push([key, term]);
|
|
134
|
+
}
|
|
135
|
+
if (terms.length === 0)
|
|
136
|
+
return invalid("model must return at least one term");
|
|
137
|
+
const substitutions = (at: Tuple): [Expr, Expr][] =>
|
|
138
|
+
domains.map((d) => {
|
|
139
|
+
const symbol = symbols[d.name];
|
|
140
|
+
if (!symbol) throw new Error(`missing symbol ${d.name}`);
|
|
141
|
+
return [
|
|
142
|
+
symbol,
|
|
143
|
+
d.kind === "uint"
|
|
144
|
+
? Z3.BitVec.val(at[d.name] ?? 0, d.bits)
|
|
145
|
+
: Z3.Int.val(at[d.name] ?? 0),
|
|
146
|
+
];
|
|
147
|
+
});
|
|
148
|
+
const valueOf = async (at: Tuple) => {
|
|
149
|
+
const values: Record<string, string | number | boolean> = {};
|
|
150
|
+
for (const [key, term] of terms) {
|
|
151
|
+
const value = await Z3.simplify(
|
|
152
|
+
Z3.substitute(term, ...substitutions(at))
|
|
153
|
+
);
|
|
154
|
+
values[key] =
|
|
155
|
+
Z3.isIntVal(value) || Z3.isBitVecVal(value)
|
|
156
|
+
? Number(value.value())
|
|
157
|
+
: Z3.isTrue(value)
|
|
158
|
+
? true
|
|
159
|
+
: Z3.isFalse(value)
|
|
160
|
+
? false
|
|
161
|
+
: value.sexpr();
|
|
162
|
+
}
|
|
163
|
+
return scalar ? values.value : values;
|
|
164
|
+
};
|
|
165
|
+
const draw = random(relative(request.root, request.proof));
|
|
166
|
+
const corner = corners(domains);
|
|
167
|
+
const accepted: Tuple[] = [];
|
|
168
|
+
let drawn = 0;
|
|
169
|
+
while (accepted.length < request.samples && drawn < 100 * request.samples) {
|
|
170
|
+
const batch: Tuple[] = [];
|
|
171
|
+
const count = Math.min(
|
|
172
|
+
request.samples - accepted.length,
|
|
173
|
+
100 * request.samples - drawn
|
|
174
|
+
);
|
|
175
|
+
for (let i = 0; i < count; i++) {
|
|
176
|
+
batch.push(
|
|
177
|
+
corner[drawn] ??
|
|
178
|
+
Object.fromEntries(domains.map((d) => [d.name, draw(d.min, d.max)]))
|
|
179
|
+
);
|
|
180
|
+
drawn++;
|
|
181
|
+
}
|
|
182
|
+
const selected = body.assume
|
|
183
|
+
? await bits(
|
|
184
|
+
Z3,
|
|
185
|
+
batch.map((at) => {
|
|
186
|
+
const term = Z3.substitute(assumption, ...substitutions(at));
|
|
187
|
+
if (!Z3.isBool(term))
|
|
188
|
+
throw new Error("assume did not produce a Bool");
|
|
189
|
+
return term;
|
|
190
|
+
})
|
|
191
|
+
)
|
|
192
|
+
: "1".repeat(batch.length);
|
|
193
|
+
if (selected === undefined)
|
|
194
|
+
return invalid("assume has no value on the sampled inputs");
|
|
195
|
+
batch.forEach((at, i) => {
|
|
196
|
+
if (selected[i] === "1") accepted.push(at);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (accepted.length < request.samples)
|
|
200
|
+
return invalid(
|
|
201
|
+
`assume accepted ${accepted.length} of ${drawn} drawn inputs; need ${request.samples}`
|
|
202
|
+
);
|
|
203
|
+
const equalities: Bool[] = [];
|
|
204
|
+
const codes: unknown[] = [];
|
|
205
|
+
for (const at of accepted) {
|
|
206
|
+
let code: unknown;
|
|
207
|
+
try {
|
|
208
|
+
code = body.code(at);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return { _tag: "Throws", at, message: message(error) };
|
|
211
|
+
}
|
|
212
|
+
codes.push(code);
|
|
213
|
+
const fields = Schema.decodeUnknownOption(
|
|
214
|
+
Schema.Record(Schema.String, Schema.Unknown)
|
|
215
|
+
)(code);
|
|
216
|
+
const values = scalar
|
|
217
|
+
? { value: code }
|
|
218
|
+
: fields._tag === "Some"
|
|
219
|
+
? fields.value
|
|
220
|
+
: undefined;
|
|
221
|
+
const eq: Bool[] = [];
|
|
222
|
+
if (values === undefined || Object.keys(values).length !== terms.length)
|
|
223
|
+
return { _tag: "Disagrees", at, code, model: await valueOf(at) };
|
|
224
|
+
for (const [key, term] of terms) {
|
|
225
|
+
const value = values[key];
|
|
226
|
+
const substituted = Z3.substitute(term, ...substitutions(at));
|
|
227
|
+
if (Z3.isBool(substituted) && typeof value === "boolean")
|
|
228
|
+
eq.push(substituted.eq(value));
|
|
229
|
+
else if (
|
|
230
|
+
(Z3.isArith(substituted) || Z3.isBitVec(substituted)) &&
|
|
231
|
+
typeof value === "number" &&
|
|
232
|
+
Number.isSafeInteger(value) &&
|
|
233
|
+
(!Z3.isBitVec(substituted) ||
|
|
234
|
+
(value >= 0 && BigInt(value) < 1n << BigInt(substituted.size())))
|
|
235
|
+
)
|
|
236
|
+
eq.push(
|
|
237
|
+
substituted.eq(
|
|
238
|
+
Z3.isBitVec(substituted)
|
|
239
|
+
? Z3.BitVec.val(value, substituted.size())
|
|
240
|
+
: Z3.Int.val(value)
|
|
241
|
+
)
|
|
242
|
+
);
|
|
243
|
+
else return { _tag: "Disagrees", at, code, model: await valueOf(at) };
|
|
244
|
+
}
|
|
245
|
+
equalities.push(Z3.And(...eq));
|
|
246
|
+
}
|
|
247
|
+
const same = await bits(Z3, equalities);
|
|
248
|
+
if (same === undefined) {
|
|
249
|
+
for (const [i, eq] of equalities.entries()) {
|
|
250
|
+
const value = await Z3.simplify(eq);
|
|
251
|
+
if (!Z3.isTrue(value) && !Z3.isFalse(value))
|
|
252
|
+
return invalid(
|
|
253
|
+
`the model has no value at ${JSON.stringify(accepted[i])}`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
return invalid("the model has no value on the sampled inputs");
|
|
257
|
+
}
|
|
258
|
+
const mismatch = same.indexOf("0");
|
|
259
|
+
const at = accepted[mismatch];
|
|
260
|
+
if (at)
|
|
261
|
+
return {
|
|
262
|
+
_tag: "Disagrees",
|
|
263
|
+
at,
|
|
264
|
+
code: codes[mismatch],
|
|
265
|
+
model: await valueOf(at),
|
|
266
|
+
};
|
|
267
|
+
const fresh: Record<string, Term> = {};
|
|
268
|
+
for (const [key, term] of terms)
|
|
269
|
+
fresh[key] = Z3.isBool(term)
|
|
270
|
+
? Z3.Bool.fresh()
|
|
271
|
+
: Z3.isBitVec(term)
|
|
272
|
+
? Z3.FreshConst(term.sort)
|
|
273
|
+
: Z3.Int.fresh();
|
|
274
|
+
const freshClaim = body.holds(symbols, scalar ? fresh.value : fresh, Z3);
|
|
275
|
+
if (!Z3.isBool(freshClaim)) return invalid("holds must return a Bool term");
|
|
276
|
+
const vacuity = new Z3.Solver();
|
|
277
|
+
vacuity.add(...domain, assumption, Z3.Not(freshClaim));
|
|
278
|
+
const check = async (solver: Solver) => {
|
|
279
|
+
const left =
|
|
280
|
+
request.timeoutSeconds * 1000 - (performance.now() - started);
|
|
281
|
+
if (left <= 0) return "unknown";
|
|
282
|
+
solver.set("timeout", Math.max(1, Math.floor(left)));
|
|
283
|
+
return await solver.check();
|
|
284
|
+
};
|
|
285
|
+
const unknown = (solver: Solver): Outcome => ({
|
|
286
|
+
_tag: "Unknown",
|
|
287
|
+
reason:
|
|
288
|
+
performance.now() - started >= request.timeoutSeconds * 1000
|
|
289
|
+
? "timeout"
|
|
290
|
+
: solver.reasonUnknown(),
|
|
291
|
+
seconds: (performance.now() - started) / 1000,
|
|
292
|
+
});
|
|
293
|
+
const meaningful = await check(vacuity);
|
|
294
|
+
if (meaningful === "unsat")
|
|
295
|
+
return invalid(
|
|
296
|
+
"holds is true for any output, so it says nothing about the code"
|
|
297
|
+
);
|
|
298
|
+
if (meaningful === "unknown") return unknown(vacuity);
|
|
299
|
+
const result = await check(main);
|
|
300
|
+
if (result === "unknown") return unknown(main);
|
|
301
|
+
if (result === "sat") {
|
|
302
|
+
const at: Tuple = {};
|
|
303
|
+
const model = main.model();
|
|
304
|
+
for (const [name, term] of Object.entries(symbols)) {
|
|
305
|
+
const value = model.eval(term, true);
|
|
306
|
+
if (Z3.isIntVal(value) || Z3.isBitVecVal(value))
|
|
307
|
+
at[name] = Number(value.value());
|
|
308
|
+
}
|
|
309
|
+
return { _tag: "Counterexample", at };
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
_tag: "Proved",
|
|
313
|
+
detail: `z3 unsat, code agrees on ${accepted.length} samples`,
|
|
314
|
+
seconds: (performance.now() - started) / 1000,
|
|
315
|
+
};
|
|
316
|
+
} catch (error) {
|
|
317
|
+
return invalid(message(error));
|
|
318
|
+
} finally {
|
|
319
|
+
await killThreads(em);
|
|
320
|
+
}
|
|
321
|
+
};
|