gdp-wired 0.55.0 → 0.56.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 +55 -2
- package/dist/core/c/reader.d.ts +74 -2
- package/dist/core/c/reader.js +3144 -121
- package/dist/core/c/safety.d.ts +98 -0
- package/dist/core/c/safety.js +642 -0
- package/dist/core/cli.d.ts +5 -0
- package/dist/core/cli.js +256 -10
- package/dist/core/extract-c.d.ts +8 -0
- package/dist/core/extract-c.js +114 -15
- package/dist/core/extract-go-check.js +6 -0
- package/dist/core/extract-go.d.ts +15 -1
- package/dist/core/extract-go.js +102 -4
- package/dist/core/extract-print.js +4 -2
- package/dist/core/extract-step.d.ts +22 -0
- package/dist/core/extract-step.js +261 -9
- package/dist/core/extract.d.ts +10 -0
- package/dist/core/extract.js +37 -3
- package/dist/core/law-answer.d.ts +1 -0
- package/dist/core/law-answer.js +23 -0
- package/dist/core/laws.js +3 -16
- package/dist/core/native-law.js +11 -3
- package/package.json +1 -1
package/dist/core/cli.js
CHANGED
|
@@ -7,6 +7,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
7
7
|
}
|
|
8
8
|
return path;
|
|
9
9
|
};
|
|
10
|
+
import { cleanupCSafetySnapshots } from "./c/reader.js";
|
|
10
11
|
import { fiberRun } from "./fiber-run.js";
|
|
11
12
|
import { securityTemplates } from "./security.js";
|
|
12
13
|
import { proveLeadsTo as rawLeadsTo, proveRule as rawRule, proveSteps as rawSteps } from "./dsl.js";
|
|
@@ -45,6 +46,8 @@ function args(argv) {
|
|
|
45
46
|
o.propose = false;
|
|
46
47
|
else if (a === '--security')
|
|
47
48
|
o.security = true;
|
|
49
|
+
else if (a === '--safety')
|
|
50
|
+
o.safety = true;
|
|
48
51
|
else if (a === '--require-review')
|
|
49
52
|
o.requireReview = true;
|
|
50
53
|
else if (a.startsWith('--')) {
|
|
@@ -307,6 +310,10 @@ const searchOf = (v) => v.stopped && v.termination && !(v.proven && v.checked) &
|
|
|
307
310
|
? { stopped: true, termination: v.termination, ...(v.context ? { context: v.context } : {}) } : {};
|
|
308
311
|
const whyOf = (v, semantic) => searchOf(v).termination && v.translation !== 'unchecked' && v.translation !== 'error'
|
|
309
312
|
? terminationWhy(v.termination) : semantic;
|
|
313
|
+
function safetyReport(x, requested) {
|
|
314
|
+
const report = x.safety;
|
|
315
|
+
return report ?? (requested ? { why: 'the extractor did not provide a C safety model' } : undefined);
|
|
316
|
+
}
|
|
310
317
|
function* expandInitializers(t, o, laws, rows) {
|
|
311
318
|
if (!rows.some(r => r.x.initializers))
|
|
312
319
|
return rows;
|
|
@@ -347,6 +354,98 @@ function* expandInitializers(t, o, laws, rows) {
|
|
|
347
354
|
}
|
|
348
355
|
return out;
|
|
349
356
|
}
|
|
357
|
+
// Extract C comment bodies (`// ...` and `/* ... */`), applying C phase-2 line splicing (`\<newline>`)
|
|
358
|
+
// and skipping string literals, char literals, and C23 preprocessing numbers (`1'000`, `0xDE'AD`).
|
|
359
|
+
function cCommentTexts(rawSrc) {
|
|
360
|
+
const src = rawSrc.replace(/\\\r?\n/g, '');
|
|
361
|
+
const out = [];
|
|
362
|
+
const isIdent = (c) => !!c && /[A-Za-z0-9_$]/.test(c);
|
|
363
|
+
const isDigit = (c) => !!c && c >= '0' && c <= '9';
|
|
364
|
+
const isPpCont = (c) => !!c && /[0-9A-Za-z_.]/.test(c);
|
|
365
|
+
for (let i = 0; i < src.length; i++) {
|
|
366
|
+
const ch = src[i];
|
|
367
|
+
if ((isDigit(ch) && !isIdent(src[i - 1])) || (ch === '.' && isDigit(src[i + 1]))) {
|
|
368
|
+
let j = i + (ch === '.' ? 2 : 1);
|
|
369
|
+
while (j < src.length) {
|
|
370
|
+
const c = src[j];
|
|
371
|
+
if ((c === 'e' || c === 'E' || c === 'p' || c === 'P') && (src[j + 1] === '+' || src[j + 1] === '-')) {
|
|
372
|
+
j += 2;
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (c === "'" && /[0-9A-Za-z_]/.test(src[j + 1] ?? '')) {
|
|
376
|
+
j += 2;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (isPpCont(c)) {
|
|
380
|
+
j++;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
i = j - 1;
|
|
386
|
+
}
|
|
387
|
+
else if (ch === '"' || ch === "'") {
|
|
388
|
+
const q = ch;
|
|
389
|
+
for (i++; i < src.length; i++) {
|
|
390
|
+
if (src[i] === '\\') {
|
|
391
|
+
i++;
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (src[i] === q || src[i] === '\n')
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else if (ch === '/' && src[i + 1] === '/') {
|
|
399
|
+
const start = i + 2;
|
|
400
|
+
while (i < src.length && src[i] !== '\n')
|
|
401
|
+
i++;
|
|
402
|
+
out.push(src.slice(start, i));
|
|
403
|
+
}
|
|
404
|
+
else if (ch === '/' && src[i + 1] === '*') {
|
|
405
|
+
const start = i + 2;
|
|
406
|
+
const end = src.indexOf('*/', start);
|
|
407
|
+
if (end < 0) {
|
|
408
|
+
out.push(src.slice(start));
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
out.push(src.slice(start, end));
|
|
412
|
+
i = end + 1;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return out;
|
|
416
|
+
}
|
|
417
|
+
// Normalize Unicode whitespace (including NBSP and newlines), block-comment leading `*`, and
|
|
418
|
+
// fullwidth/compatibility colons so lookalike directives are never silently missed.
|
|
419
|
+
const normalizeSafetyText = (s) => s.replace(/[\uFF1A\uFE55\uFE13\u2236]/g, ':')
|
|
420
|
+
.replace(/\r?\n[ \t]*\*+[ \t]*/g, ' ')
|
|
421
|
+
.replace(/[\s\u00A0\u1680\u2000-\u200B\u202F\u205F\u3000\uFEFF]+/g, ' ');
|
|
422
|
+
// Accept `// gdp: safety`, `/* gdp: safety */`, multiline `/* gdp:\n safety */`, backslash-continued
|
|
423
|
+
// `// ...\n gdp: safety`, case/punctuation variants, and trailing text, while rejecting assignments.
|
|
424
|
+
const SAFETY_TAG_RE = /\bgdp[ \t\r\n]*:[ \t\r\n]*safety\b(?![ \t]*=)/i;
|
|
425
|
+
const SAFETY_LOOKALIKE_RE = /\bgdp(?:\s*:\s*|\s+)safe\w*(?:\s*=\s*\S+)?/i;
|
|
426
|
+
const SAFETY_LOOKALIKE_NOTE_RE = /looks like a gdp: safety directive/;
|
|
427
|
+
const hasSafetyTag = (t) => t.lang === 'c' && t.files.some(f => cCommentTexts(fs.readFileSync(f, 'utf8')).some(c => SAFETY_TAG_RE.test(c)));
|
|
428
|
+
const safetyLookalikeNote = (t) => {
|
|
429
|
+
if (t.lang !== 'c' || hasSafetyTag(t))
|
|
430
|
+
return undefined;
|
|
431
|
+
for (const f of t.files) {
|
|
432
|
+
for (const c of cCommentTexts(fs.readFileSync(f, 'utf8'))) {
|
|
433
|
+
const m = SAFETY_LOOKALIKE_RE.exec(normalizeSafetyText(c));
|
|
434
|
+
if (m) {
|
|
435
|
+
const shown = /^gdp: safety$/i.test(m[0].trim()) ? c.trim().slice(0, 60) : m[0].trim();
|
|
436
|
+
return `${path.basename(f)}: comment looks like a gdp: safety directive (${JSON.stringify(shown)}) but did not match 'gdp: safety'; write '// gdp: safety' or pass --safety to enable C safety checking`;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return undefined;
|
|
441
|
+
};
|
|
442
|
+
const safetyDisabledBy = (cfg, entry) => cfg.all.safety === false || (!!entry && cfg.rules[entry]?.safety === false);
|
|
443
|
+
const safetyOverrideRefusal = (t) => ({
|
|
444
|
+
ok: false,
|
|
445
|
+
refusals: [{ where: t.at, what: 'gdp.toml disables safety (safety = false), conflicting with the in-source gdp: safety directive' }],
|
|
446
|
+
notes: [],
|
|
447
|
+
safety: { why: 'gdp.toml cannot silently override an in-source gdp: safety directive' },
|
|
448
|
+
});
|
|
350
449
|
function* rulesOf(t, o, laws) {
|
|
351
450
|
const got = yield* rulesOf0(t, o, laws);
|
|
352
451
|
if (o.entry)
|
|
@@ -369,9 +468,15 @@ function* rulesOf(t, o, laws) {
|
|
|
369
468
|
void _k;
|
|
370
469
|
void _f;
|
|
371
470
|
const keep = [...new Set(laws.flatMap(l => l.expr.match(/[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*/g) ?? []).flatMap(k => (k.includes('::') ? [k, ...k.split('::')] : [k])))];
|
|
471
|
+
const safetyTag = hasSafetyTag(t);
|
|
372
472
|
const extra = [];
|
|
373
|
-
for (const entry of more)
|
|
374
|
-
|
|
473
|
+
for (const entry of more) {
|
|
474
|
+
if (safetyTag && safetyDisabledBy(cfg, entry)) {
|
|
475
|
+
extra.push({ entry, x: safetyOverrideRefusal(t) });
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
extra.push({ entry, x: yield { target: t, options: { ...merge(merge({ ...shared, keep }, cfg.rules[entry] || {}), flags), ...(safetyTag ? { safety: true } : {}), entry } } });
|
|
479
|
+
}
|
|
375
480
|
// (the first rule, read with no entry named, kept only where it read something or nothing else was)
|
|
376
481
|
return yield* expandInitializers(t, o, laws, [...got.filter(r => r.x.ok || r.entry || !extra.length), ...extra]);
|
|
377
482
|
}
|
|
@@ -391,10 +496,15 @@ function* rulesOf0(t, o, laws) {
|
|
|
391
496
|
void _k;
|
|
392
497
|
// (the names the laws read, so an argument a law reads stays in the rule though the code ignores it)
|
|
393
498
|
const keep = [...new Set(laws.flatMap(l => l.expr.match(/[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*/g) ?? []).flatMap(k => (k.includes('::') ? [k, ...k.split('::')] : [k])))];
|
|
394
|
-
const
|
|
499
|
+
const safetyTag = hasSafetyTag(t);
|
|
500
|
+
const optsFor = (entry) => ({ ...merge(merge({ ...shared, keep }, (entry && cfg.rules[entry]) || {}), flags), ...(safetyTag ? { safety: true } : {}) });
|
|
395
501
|
const one = function* (entry) {
|
|
502
|
+
if (safetyTag && safetyDisabledBy(cfg, entry))
|
|
503
|
+
return { entry, x: safetyOverrideRefusal(t) };
|
|
396
504
|
const x = yield { target: t, options: { ...optsFor(entry), ...(entry ? { entry } : {}) } };
|
|
397
|
-
const found = x.model?.name;
|
|
505
|
+
const found = x.model?.name ?? x.entry;
|
|
506
|
+
if (!entry && found && safetyTag && safetyDisabledBy(cfg, found))
|
|
507
|
+
return { entry: found, x: safetyOverrideRefusal(t) };
|
|
398
508
|
return !entry && found && cfg.rules[found] ? { entry: found, x: yield { target: t, options: { ...optsFor(found), entry: found } } } : { entry, x };
|
|
399
509
|
};
|
|
400
510
|
const manyOf = function* (entries) { const rows = []; for (const entry of entries)
|
|
@@ -598,9 +708,17 @@ function* checkPipeline(t, o) {
|
|
|
598
708
|
// Different initial states must never overwrite one another's certificates.
|
|
599
709
|
const ctx = ctor !== undefined && baseCtx.cert ? { ...baseCtx, cert: path.join(baseCtx.cert, `initializer-${initializerIndex}`) } : baseCtx;
|
|
600
710
|
let x = x0;
|
|
601
|
-
const name = entry ?? (x.model?.name || undefined);
|
|
711
|
+
const name = entry ?? (x.model?.name || x.entry || undefined);
|
|
602
712
|
// (a target that is the working directory, a crate's or a package's: `.`)
|
|
603
713
|
const r = { file: path.relative(proc.cwd(), t.at) || '.', lang: LANG_NAME[t.lang], ...(name ? { entry: name } : {}), ...(ctor !== undefined ? { ctor } : {}), ok: x.ok, refusals: x.refusals, notes: x.notes, names: x.names ?? [], laws: [], proposed: [], ...(x.source ? { source: x.source } : {}) };
|
|
714
|
+
const safety = safetyReport(x, o.safety === true || hasSafetyTag(t));
|
|
715
|
+
if (safety)
|
|
716
|
+
r.safety = safety;
|
|
717
|
+
else if (o.safety !== true) {
|
|
718
|
+
const warn = safetyLookalikeNote(t);
|
|
719
|
+
if (warn && !r.notes.includes(warn))
|
|
720
|
+
r.notes.push(warn);
|
|
721
|
+
}
|
|
604
722
|
outs.push(r);
|
|
605
723
|
// (the laws of this rule: all where it is the file's one, else those in its class or on it or its methods, and those
|
|
606
724
|
// outside every class and function)
|
|
@@ -680,11 +798,12 @@ function* checkPipeline(t, o) {
|
|
|
680
798
|
// (from any state, the laws about states as one inductive set: the largest the stated ones make, each law the
|
|
681
799
|
// kernel's step breaks taken out in turn (Houdini), the others proven together; one taken out is judged alone)
|
|
682
800
|
const inductive = from && x.rule && !sys ? houdini(x, stated.filter(m => twice(m).length === 1 && kindOf(m) === 'law'), o.ms, ctx, o.work, o.watchdogMs) : undefined;
|
|
801
|
+
const siblings = !from && !sys ? stated.filter(m => twice(m).length === 1) : undefined;
|
|
683
802
|
const outsOf = [];
|
|
684
803
|
for (const i of order) {
|
|
685
804
|
const l = stated[i];
|
|
686
805
|
outsOf[i] = twice(l).length > 1 ? { name: l.name, expr: l.expr, where: l.where, verdict: 'not read', why: `a law of this name is stated twice (${twice(l).join(', ')}): a law is known by its name`, ms: 0 }
|
|
687
|
-
: sys ? proveInEnv(x, l, o.ms, proven, { ...ctx, sys, entry: name }) : prove(x, l, o.ms, proven, { ...ctx, entry: name, ...(from ? { from, inductive, together: stated.filter(m => twice(m).length === 1 && kindOf(m) === 'law' && inductive.has(m.name)) } : {}) });
|
|
806
|
+
: sys ? proveInEnv(x, l, o.ms, proven, { ...ctx, sys, entry: name }) : prove(x, l, o.ms, proven, { ...ctx, entry: name, ...(from ? { from, inductive, together: stated.filter(m => twice(m).length === 1 && kindOf(m) === 'law' && inductive.has(m.name)) } : siblings ? { siblings } : {}) });
|
|
688
807
|
}
|
|
689
808
|
r.laws = outsOf;
|
|
690
809
|
if (o.security) {
|
|
@@ -754,12 +873,15 @@ function houdini(x, ls, ms, opts, work, watchdogMs) {
|
|
|
754
873
|
// an interchange document whose meta holds them.
|
|
755
874
|
// The rule a law is proven on: the extraction's, declaring the divisors the law reads that the code doesn't (x // k,
|
|
756
875
|
// x % k: floor division and remainder by a constant, known by their facts), its machine made again with them.
|
|
876
|
+
function divisorsOf(expr) {
|
|
877
|
+
return [...new Set([...expr.matchAll(/(?:\/\/|%)\s*(\d+)/g)].map(m => Number(m[1])).filter(k => k >= 1))].sort((a, b) => a - b);
|
|
878
|
+
}
|
|
757
879
|
function ruleFor(x, expr) {
|
|
758
880
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
759
881
|
const r = x.rule;
|
|
760
882
|
if (!r)
|
|
761
883
|
return x;
|
|
762
|
-
const ks =
|
|
884
|
+
const ks = divisorsOf(expr);
|
|
763
885
|
return ks.length ? { ...x, rule: r.withDivisors(...ks) } : x;
|
|
764
886
|
}
|
|
765
887
|
function prove(x0, l, ms, proven, ctx, work = ctx?.work) {
|
|
@@ -857,6 +979,72 @@ function prove(x0, l, ms, proven, ctx, work = ctx?.work) {
|
|
|
857
979
|
verdict = 'unknown';
|
|
858
980
|
if (v.translation === 'error')
|
|
859
981
|
verdict = 'not read';
|
|
982
|
+
let jointCertInvs;
|
|
983
|
+
// Fallback only: when an ordinary reachable-state law ends unknown (neither proven nor refuted), retry it jointly
|
|
984
|
+
// with the other laws stated on the same rule within the time left in this law's own budget. Credit only this law,
|
|
985
|
+
// and only if the kernel checks the whole joint family (including this law).
|
|
986
|
+
if (verdict === 'unknown' && b && !ctx?.from && work === undefined && !v.proven && !v.run
|
|
987
|
+
&& !(v.stopped && v.termination?.kind === 'wall-deadline')
|
|
988
|
+
&& v.translation !== 'unchecked' && v.translation !== 'error' && (ctx?.siblings?.length ?? 0) > 1) {
|
|
989
|
+
const budgetCap = ctx.watchdogMs !== undefined ? Math.min(ms, ctx.watchdogMs) : ms;
|
|
990
|
+
const remaining = Math.floor(budgetCap - (cliNow() - t0));
|
|
991
|
+
if (remaining >= 1) {
|
|
992
|
+
const targetDivs = divisorsOf(l.expr).join(',');
|
|
993
|
+
const jointInvs = [...inv];
|
|
994
|
+
const jointSteps = [];
|
|
995
|
+
const addedStated = new Set();
|
|
996
|
+
let siblingCount = 0;
|
|
997
|
+
let targetInJoint = false;
|
|
998
|
+
for (const m of ctx.siblings) {
|
|
999
|
+
if (m.name === l.name) {
|
|
1000
|
+
if (b.kind === 'law')
|
|
1001
|
+
jointInvs.push(b.law);
|
|
1002
|
+
else
|
|
1003
|
+
jointSteps.push(b.law);
|
|
1004
|
+
addedStated.add(m.name);
|
|
1005
|
+
targetInJoint = true;
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
if (isLeadsTo(m.expr) || isTwoRuns(m.expr) || divisorsOf(m.expr).join(',') !== targetDivs)
|
|
1009
|
+
continue;
|
|
1010
|
+
let sb;
|
|
1011
|
+
try {
|
|
1012
|
+
sb = buildLaw(x, m, ctx);
|
|
1013
|
+
}
|
|
1014
|
+
catch {
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
1017
|
+
if (sb.kind === 'law')
|
|
1018
|
+
jointInvs.push(sb.law);
|
|
1019
|
+
else
|
|
1020
|
+
jointSteps.push(sb.law);
|
|
1021
|
+
addedStated.add(m.name);
|
|
1022
|
+
siblingCount++;
|
|
1023
|
+
}
|
|
1024
|
+
if (b.kind === 'step') {
|
|
1025
|
+
for (const p of proven)
|
|
1026
|
+
if (!addedStated.has(p.name))
|
|
1027
|
+
jointInvs.push(p);
|
|
1028
|
+
}
|
|
1029
|
+
if (targetInJoint && siblingCount >= 1) {
|
|
1030
|
+
const jointOpts = { search: { ms: remaining }, ...(!syncControl || ctx.watchdogMs !== undefined ? { watchdogMs: remaining } : {}) };
|
|
1031
|
+
try {
|
|
1032
|
+
const jv = jointSteps.length
|
|
1033
|
+
? proveSteps(x.rule, jointInvs, jointSteps, jointOpts)
|
|
1034
|
+
: proveRule(x.rule, jointInvs, jointOpts);
|
|
1035
|
+
if (jv.proven && jv.checked && jv.translation !== 'unchecked' && jv.translation !== 'error') {
|
|
1036
|
+
v = jv;
|
|
1037
|
+
verdict = 'proven';
|
|
1038
|
+
jointCertInvs = jointInvs;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
catch (e) {
|
|
1042
|
+
if (e && typeof e === 'object' && e.name === 'Refusal')
|
|
1043
|
+
throw e;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
860
1048
|
if (ctx?.from && v.proven && v.assumes)
|
|
861
1049
|
v.why = `proven only in states a run from the start reaches (from = "any" asks for every state the laws allow): it needs ${v.assumes.laws.join('; ') || 'k-induction'}`;
|
|
862
1050
|
// (a run the front end replays on the code: broken only where the code runs it so)
|
|
@@ -879,7 +1067,8 @@ function prove(x0, l, ms, proven, ctx, work = ctx?.work) {
|
|
|
879
1067
|
const more = v.found?.laws?.map(f => f.law) ?? [];
|
|
880
1068
|
const meta = { producer: 'gdp-wired check', file: ctx.file, ...(ctx.entry ? { entry: ctx.entry } : {}), law: l.name, expr: l.expr, ...(l.where ? { where: l.where } : {}), cites };
|
|
881
1069
|
const doc = v.k ? undefined : two ? certify(two.rule, [...v.given ?? [], ...more, ...two.laws], two.stepLaws, meta)
|
|
882
|
-
:
|
|
1070
|
+
: jointCertInvs ? certify(x.rule, [...jointCertInvs, ...more], b.kind === 'step' ? [b.law] : [], meta)
|
|
1071
|
+
: b.kind === 'law' ? certify(x.rule, [...inv, ...more, b.law], [], meta) : certify(x.rule, [...inv, ...proven, ...more], [b.law], meta);
|
|
883
1072
|
if (doc) {
|
|
884
1073
|
fs.mkdirSync(ctx.cert, { recursive: true });
|
|
885
1074
|
cert = path.join(ctx.cert, `${[ctx.entry ?? path.basename(ctx.file), l.name].join('.').replace(/[^\w.-]+/g, '-')}.json`);
|
|
@@ -1124,6 +1313,7 @@ export const proposedLaws = (x, lang, ms = 2000) => propose(x, lang, { ms, propo
|
|
|
1124
1313
|
const ROLE = { state: 'state', param: 'made with', input: 'request', answer: 'answered', result: 'result', failed: 'failed', method: 'method', clock: 'clock', const: 'constant' };
|
|
1125
1314
|
function printRule(r, o) {
|
|
1126
1315
|
out(`${r.file}${r.entry ? `: ${r.entry}` : ''}${r.ctor !== undefined ? ` [ctor ${r.ctor}]` : ''} (${r.lang})`);
|
|
1316
|
+
const safetyWarnings = r.notes.filter(n => SAFETY_LOOKALIKE_NOTE_RE.test(n));
|
|
1127
1317
|
if (!r.ok) {
|
|
1128
1318
|
for (const f of r.refusals)
|
|
1129
1319
|
out(` ${paint('yellow', `refused at ${f.where}:`)} ${f.what}`);
|
|
@@ -1144,6 +1334,8 @@ function printRule(r, o) {
|
|
|
1144
1334
|
for (const n of r.names)
|
|
1145
1335
|
out(` ${paint('dim', `${label(n).padEnd(w)} ${ROLE[n.role].padEnd(9)} ${n.rule}${n.none ? `${n.rule ? ', ' : ''}None: ${n.none}` : ''}${n.map ? ` (cells: ${n.map.fields.join(', ')}${n.map.tpl ? `; key ${JSON.stringify(n.map.tpl)}` : ''})` : ''}${n.role === 'const' ? String(n.value) : ''}`)}`);
|
|
1146
1336
|
}
|
|
1337
|
+
for (const n of safetyWarnings)
|
|
1338
|
+
out(` ${paint('yellow', 'warning:')} ${n}`);
|
|
1147
1339
|
const law = (l, proposed) => {
|
|
1148
1340
|
out(` ${paint(VERDICT[l.verdict], l.verdict)}${' '.repeat(Math.max(0, 9 - l.verdict.length))}${l.name}${l.from ? ' (from any state)' : ''}${proposed ? '' : ` ${paint('dim', `(${l.where})`)}`}`);
|
|
1149
1341
|
if (!proposed && l.verdict !== 'proven')
|
|
@@ -1185,6 +1377,45 @@ function printRule(r, o) {
|
|
|
1185
1377
|
for (const l of r.laws)
|
|
1186
1378
|
law(l, false);
|
|
1187
1379
|
}
|
|
1380
|
+
if (r.safety) {
|
|
1381
|
+
out(' C safety (first-fault model; broken obligations replayed under ASan/UBSan):');
|
|
1382
|
+
if ('why' in r.safety)
|
|
1383
|
+
out(` ${paint('yellow', 'unchecked')}: ${r.safety.why}`);
|
|
1384
|
+
else {
|
|
1385
|
+
const color = (s) => s === 'proven' ? 'green' : s === 'broken' ? 'red' : 'yellow';
|
|
1386
|
+
const cov = r.safety.coveredFunctions.length ? r.safety.coveredFunctions.join(', ') : 'none';
|
|
1387
|
+
const helpers = r.safety.coveredHelpers?.length ? `; static helpers inlined: ${r.safety.coveredHelpers.join(', ')}` : '';
|
|
1388
|
+
out(` ${paint(color(r.safety.status), r.safety.status)} (covers: ${cov}${helpers})`);
|
|
1389
|
+
if (r.safety.macroConfig) {
|
|
1390
|
+
const mc = r.safety.macroConfig;
|
|
1391
|
+
const defs = mc.defines.length ? mc.defines.join(', ') : 'none';
|
|
1392
|
+
const incs = mc.includeDirs.length ? mc.includeDirs.join(', ') : 'none';
|
|
1393
|
+
const compiler = mc.compiler
|
|
1394
|
+
? `; compiler: ${mc.compiler}${mc.compilerPath ? ` (${mc.compilerPath})` : ''}`
|
|
1395
|
+
: mc.compilerPath ? `; compiler: ${mc.compilerPath}` : '';
|
|
1396
|
+
const libc = mc.libc ? `; libc: ${mc.libc}` : '';
|
|
1397
|
+
const userF = mc.userFilesSha256
|
|
1398
|
+
? `; user files: ${mc.userFiles?.length ?? 0} (sha256:${mc.userFilesSha256})`
|
|
1399
|
+
: '';
|
|
1400
|
+
const sysHdrs = mc.systemHeadersSha256
|
|
1401
|
+
? `; system headers: ${mc.systemHeaders?.length ?? 0} (sha256:${mc.systemHeadersSha256})`
|
|
1402
|
+
: '';
|
|
1403
|
+
const envNote = mc.envScrubbed ? 'scrubbed' : 'inherited';
|
|
1404
|
+
out(` macro config: target: ${mc.target}${compiler}${libc}${userF}${sysHdrs}; std: ${mc.std} (__STDC_VERSION__=${mc.stdcVersion}); env: ${envNote}; -I flags: ${incs}; -D flags: ${defs}; NDEBUG=${mc.ndebugDefined ? 'defined' : 'undefined'}, __OPTIMIZE__=${mc.optimizeDefined ? 'defined' : 'undefined'}, DEBUG=${mc.debugDefined ? 'defined' : 'undefined'}`);
|
|
1405
|
+
}
|
|
1406
|
+
if (r.safety.uncheckedFunctions?.length)
|
|
1407
|
+
out(` ${paint('yellow', 'unchecked functions:')} ${r.safety.uncheckedFunctions.join(', ')}`);
|
|
1408
|
+
for (const s of r.safety.issues) {
|
|
1409
|
+
out(` ${paint(color(s.status), s.status)}: ${s.kind} at ${s.where}`);
|
|
1410
|
+
if (s.replay?.requests.length)
|
|
1411
|
+
out(` run: ${s.replay.requests.join(' -> ')}`);
|
|
1412
|
+
for (const line of s.replay?.reports ?? [])
|
|
1413
|
+
out(` sanitizer: ${line}`);
|
|
1414
|
+
}
|
|
1415
|
+
for (const a of r.safety.assumptions)
|
|
1416
|
+
out(` ${a}`);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1188
1419
|
if (r.security) {
|
|
1189
1420
|
out(' security candidates (NOT accepted policy; review the requirement and authentication boundary):');
|
|
1190
1421
|
for (const l of r.security)
|
|
@@ -1198,7 +1429,7 @@ function printRule(r, o) {
|
|
|
1198
1429
|
if (r.brokenProposed)
|
|
1199
1430
|
out(` (${r.brokenProposed} more proposed and broken by a run: not shown)`);
|
|
1200
1431
|
}
|
|
1201
|
-
if (r.ok && !r.laws.length)
|
|
1432
|
+
if (r.ok && !r.safety && !r.laws.length)
|
|
1202
1433
|
out(` no law stated: write one as \`${COMMENT[Object.keys(LANG_NAME).find(k => LANG_NAME[k] === r.lang)]} gdp: law <name>: <expr>\` over the names above`);
|
|
1203
1434
|
void o;
|
|
1204
1435
|
out();
|
|
@@ -1217,6 +1448,7 @@ the clause a law answers: \`gdp: cite <law name>: <clause>\` (drafted from the c
|
|
|
1217
1448
|
--cert <dir> writes each proven law's certificate (its meta the citations); --from any (or from = "any" in gdp.toml, for every
|
|
1218
1449
|
rule or one) proves every law from every state the laws allow, for code that steps the rule from states it is handed. Exit code: 1 when a stated law is broken, 2 when
|
|
1219
1450
|
one couldn't be checked.
|
|
1451
|
+
--safety (or a C file's \`// gdp: safety\`) checks undefined-behavior obligations in the supported C subset and replays broken obligations under ASan/UBSan.
|
|
1220
1452
|
--security proposes authorization, tenant-write isolation, workflow, quota and ledger candidates over the code's names.
|
|
1221
1453
|
Candidates require policy review, show broken runs too, and do not affect the exit code. Arbitrary input identities are
|
|
1222
1454
|
not authenticated identities. State the intended policy with gdp: law comments; use gdp: env for concurrent isolation.`;
|
|
@@ -1241,7 +1473,10 @@ export async function check(targets, given = {}, each) {
|
|
|
1241
1473
|
}
|
|
1242
1474
|
const stated = all.flatMap(r => r.laws);
|
|
1243
1475
|
// A sampled source translation cannot turn an unknown proof into a successful check.
|
|
1244
|
-
|
|
1476
|
+
const safetyLookalike = all.some(r => r.notes.some(n => SAFETY_LOOKALIKE_NOTE_RE.test(n)));
|
|
1477
|
+
const uncheckedSafety = safetyLookalike || all.some(r => r.safety && ('why' in r.safety || r.safety.status === 'unchecked' || r.safety.status === 'unproven'));
|
|
1478
|
+
const brokenSafety = all.some(r => r.safety && !('why' in r.safety) && r.safety.status === 'broken');
|
|
1479
|
+
return { rules: all, exit: uncheckedSafety || stated.some(l => l.verdict === 'unknown' || l.translation === 'unchecked' || l.translation === 'error') ? 2 : brokenSafety || stated.some(l => l.verdict === 'broken') ? 1 : stated.some(l => l.verdict === 'not read') || o.security && all.some(r => !r.ok) ? 2 : 0 };
|
|
1245
1480
|
}
|
|
1246
1481
|
// --- draft: laws from a clause, for a person to review ----------------------------------------------------------------
|
|
1247
1482
|
// gdp-wired draft <file> --clause <text> | --clause-file <f> --cite <ref> [--llm <command> | --answer <file>] [--by <who>]:
|
|
@@ -1325,5 +1560,16 @@ export async function main(argv) {
|
|
|
1325
1560
|
return code;
|
|
1326
1561
|
}
|
|
1327
1562
|
if (import.meta.url === `file://${proc.argv[1]}` || /\/(cli\.(ts|js)|gdp-wired)$/.test(proc.argv[1] ?? '')) {
|
|
1563
|
+
let sigExiting = false;
|
|
1564
|
+
const onSignal = (code) => () => {
|
|
1565
|
+
if (sigExiting)
|
|
1566
|
+
return;
|
|
1567
|
+
sigExiting = true;
|
|
1568
|
+
cleanupCSafetySnapshots();
|
|
1569
|
+
proc.exit?.(code);
|
|
1570
|
+
};
|
|
1571
|
+
proc.on?.('SIGINT', onSignal(130));
|
|
1572
|
+
proc.on?.('SIGTERM', onSignal(143));
|
|
1573
|
+
proc.on?.('exit', () => { cleanupCSafetySnapshots(); });
|
|
1328
1574
|
main(proc.argv.slice(2)).then(c => { proc.exitCode = c; }, e => { out(`gdp-wired: ${e.message}`); proc.exitCode = 2; });
|
|
1329
1575
|
}
|
package/dist/core/extract-c.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type { Sorts } from './extract-print.js';
|
|
|
5
5
|
import { type GoRead } from './extract-go.js';
|
|
6
6
|
import { type GoCheckOptions, type GoEvidence, type GoLeaf } from './extract-go-check.js';
|
|
7
7
|
import { type CRead } from './c/reader.js';
|
|
8
|
+
import { type CSafetyResult } from './c/safety.js';
|
|
9
|
+
export { cSafetyArtifactOf, verifyCSafetyProof, type CSafetyArtifact, type CSafetyIssue, type CSafetyKind, type CSafetyMacroConfig, type CSafetyProofBundle, type CSafetyReplay, type CSafetyResult, type CSafetyRun, type CSafetyStatus } from './c/safety.js';
|
|
8
10
|
export { minimizeRun, minimizeRunDetailed, proveRule, proveSteps } from './dsl.js';
|
|
9
11
|
export type COptions = {
|
|
10
12
|
entry?: string;
|
|
@@ -24,6 +26,10 @@ export type COptions = {
|
|
|
24
26
|
onError?: 'refuse' | 'discard';
|
|
25
27
|
domains?: Record<string, 'nat' | 'int'>;
|
|
26
28
|
check?: GoCheckOptions;
|
|
29
|
+
/** First-fault C safety analysis of the covered subset, with ASan/UBSan replay on broken obligations. */
|
|
30
|
+
safety?: boolean;
|
|
31
|
+
/** Optional prover search budget per safety obligation (ms, default 10000). */
|
|
32
|
+
safetySearchMs?: number;
|
|
27
33
|
};
|
|
28
34
|
export declare function readCFile(file: string, opts: COptions, cpp?: boolean): {
|
|
29
35
|
c: CRead;
|
|
@@ -31,9 +37,11 @@ export declare function readCFile(file: string, opts: COptions, cpp?: boolean):
|
|
|
31
37
|
};
|
|
32
38
|
export declare function extractC(at: string | FileURL, given?: COptions, cpp?: boolean): Extraction & {
|
|
33
39
|
leaves?: GoLeaf[];
|
|
40
|
+
safety?: CSafetyResult;
|
|
34
41
|
};
|
|
35
42
|
export declare const extractCpp: (at: string | FileURL, opts?: COptions) => Extraction & {
|
|
36
43
|
leaves?: GoLeaf[];
|
|
44
|
+
safety?: CSafetyResult;
|
|
37
45
|
};
|
|
38
46
|
export declare function harnessC(file: string, c: CRead, read: GoRead, opts: COptions, cpp: boolean): string;
|
|
39
47
|
export declare function checkC(file: string, flags: string[], c: CRead, read: GoRead, r: Rule, sorts: Sorts, opts: COptions, cpp: boolean): GoEvidence;
|