gdp-wired 0.55.1 → 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 +48 -0
- 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 +182 -7
- 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
|
@@ -93,7 +93,6 @@ const KEYED = Object.fromEntries([
|
|
|
93
93
|
...['delete', 'deletecache', 'remove', 'deletebyid', 'del', 'evict', 'invalidate'].map(n => [n, 'delete'])
|
|
94
94
|
]);
|
|
95
95
|
const keyShape = (m) => KEYED[m.replace(/^async_?|_?async$/gi, '').replace(/[_-]/g, '').toLowerCase()];
|
|
96
|
-
// --- the executor ---------------------------------------------------------------------------------------------------
|
|
97
96
|
export class Stepper {
|
|
98
97
|
mod;
|
|
99
98
|
opts;
|
|
@@ -103,6 +102,52 @@ export class Stepper {
|
|
|
103
102
|
params = [];
|
|
104
103
|
notes = new Set();
|
|
105
104
|
refusals = [];
|
|
105
|
+
safety;
|
|
106
|
+
safetyCutSites;
|
|
107
|
+
safetyDeadSites;
|
|
108
|
+
registerSafety(site) {
|
|
109
|
+
const key = `${site.kind}:${site.where}`;
|
|
110
|
+
if (!this.safety?.has(key))
|
|
111
|
+
this.safety?.set(key, { ...site, condition: I.F });
|
|
112
|
+
return this.safety?.get(key);
|
|
113
|
+
}
|
|
114
|
+
markSafetyDead(node, seenFns = new Set()) {
|
|
115
|
+
if (!this.safety || !node || typeof node !== 'object')
|
|
116
|
+
return;
|
|
117
|
+
if (Array.isArray(node)) {
|
|
118
|
+
for (const x of node)
|
|
119
|
+
this.markSafetyDead(x, seenFns);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const o = node;
|
|
123
|
+
if (o.site && typeof o.site === 'object') {
|
|
124
|
+
const s = o.site;
|
|
125
|
+
this.safetyDeadSites?.add(`${s.kind}:${s.where}`);
|
|
126
|
+
this.registerSafety(s);
|
|
127
|
+
}
|
|
128
|
+
if (Array.isArray(o.sites))
|
|
129
|
+
for (const s of o.sites) {
|
|
130
|
+
this.safetyDeadSites?.add(`${s.kind}:${s.where}`);
|
|
131
|
+
this.registerSafety(s);
|
|
132
|
+
}
|
|
133
|
+
if (o.e === 'gcall' && typeof o.fn === 'string' && !seenFns.has(o.fn)) {
|
|
134
|
+
seenFns.add(o.fn);
|
|
135
|
+
const f = this.mod.funcs[o.fn];
|
|
136
|
+
if (f)
|
|
137
|
+
this.markSafetyDead(f.body, seenFns);
|
|
138
|
+
}
|
|
139
|
+
for (const v of Object.values(o))
|
|
140
|
+
this.markSafetyDead(v, seenFns);
|
|
141
|
+
}
|
|
142
|
+
safetyCut(p, at, kind, bad) {
|
|
143
|
+
const site = at.site;
|
|
144
|
+
if (!site || site.kind !== kind)
|
|
145
|
+
return refuse(at, `missing typed C ${kind} site`);
|
|
146
|
+
const alarm = this.registerSafety(site);
|
|
147
|
+
this.safetyCutSites?.add(`${site.kind}:${site.where}`);
|
|
148
|
+
alarm.condition = I.or(alarm.condition, I.and(this.cond(p), bad));
|
|
149
|
+
return this.assume(p, I.not(bad));
|
|
150
|
+
}
|
|
106
151
|
fails = [];
|
|
107
152
|
failPaths = [];
|
|
108
153
|
maps = [];
|
|
@@ -1272,8 +1317,13 @@ export class Stepper {
|
|
|
1272
1317
|
if (a === b)
|
|
1273
1318
|
return a;
|
|
1274
1319
|
// (a number keeps its type)
|
|
1275
|
-
if (a.t === 'num' && b.t === 'num')
|
|
1320
|
+
if (a.t === 'num' && b.t === 'num') {
|
|
1321
|
+
if (this.safety) {
|
|
1322
|
+
const [al, ah] = this.safetyBounds(a, at), [bl, bh] = this.safetyBounds(b, at);
|
|
1323
|
+
return { t: 'num', e: I.nite(c, a.e, b.e), ...(a.ty ? { ty: a.ty } : {}), range: [Math.min(al, bl), Math.max(ah, bh)] };
|
|
1324
|
+
}
|
|
1276
1325
|
return { t: 'num', e: I.nite(c, a.e, b.e), ...(a.ty ? { ty: a.ty } : {}) };
|
|
1326
|
+
}
|
|
1277
1327
|
if (firm(a) || firm(b))
|
|
1278
1328
|
return this.iteFirm(c, a, b, at);
|
|
1279
1329
|
if (keyV(a) === keyV(b))
|
|
@@ -1550,15 +1600,23 @@ export class Stepper {
|
|
|
1550
1600
|
block(ss, p) {
|
|
1551
1601
|
let live = p, ft;
|
|
1552
1602
|
const rets = [], brk = [];
|
|
1553
|
-
for (
|
|
1554
|
-
|
|
1603
|
+
for (let i = 0; i < ss.length; i++) {
|
|
1604
|
+
const s = ss[i];
|
|
1605
|
+
if (!live) {
|
|
1606
|
+
if (this.safety)
|
|
1607
|
+
this.markSafetyDead(ss.slice(i));
|
|
1555
1608
|
break;
|
|
1609
|
+
}
|
|
1556
1610
|
// (a path a failure ended inside an expression: nothing after it runs)
|
|
1557
1611
|
if (dead(live)) {
|
|
1612
|
+
if (this.safety)
|
|
1613
|
+
this.markSafetyDead(ss.slice(i));
|
|
1558
1614
|
live = undefined;
|
|
1559
1615
|
break;
|
|
1560
1616
|
}
|
|
1561
1617
|
if (s.s === 'fallthrough') {
|
|
1618
|
+
if (this.safety)
|
|
1619
|
+
this.markSafetyDead(ss.slice(i + 1));
|
|
1562
1620
|
ft = live;
|
|
1563
1621
|
live = undefined;
|
|
1564
1622
|
break;
|
|
@@ -1600,6 +1658,10 @@ export class Stepper {
|
|
|
1600
1658
|
this.arm(s.then);
|
|
1601
1659
|
if (!(c.op === 'const' && c.v))
|
|
1602
1660
|
this.arm(s.else);
|
|
1661
|
+
if (this.safety && c.op === 'const' && !c.v)
|
|
1662
|
+
this.markSafetyDead(s.then);
|
|
1663
|
+
if (this.safety && c.op === 'const' && c.v)
|
|
1664
|
+
this.markSafetyDead(s.else);
|
|
1603
1665
|
const t = c.op === 'const' && !c.v ? undefined : this.block(s.then, this.assume(q0, c));
|
|
1604
1666
|
const e = c.op === 'const' && c.v ? undefined : this.block(s.else, this.assume(q0, I.not(c)));
|
|
1605
1667
|
const lives = [t?.live, e?.live].filter((x) => !!x);
|
|
@@ -3334,24 +3396,49 @@ export class Stepper {
|
|
|
3334
3396
|
const m = 2 ** ty.bits, f = { ...v.bits, A: v.bits.A % m, B: v.bits.B % m };
|
|
3335
3397
|
return [{ t: 'num', e: f.A === v.bits.A && f.B === v.bits.B ? v.e : this.ofBits(f, x), ty, bits: f }, q];
|
|
3336
3398
|
}
|
|
3399
|
+
if (this.safety) {
|
|
3400
|
+
if (!ty || v.t !== 'num')
|
|
3401
|
+
return refuse(x, String(x.why));
|
|
3402
|
+
const [lo, hi] = this.safetyRange(ty, x), [fromLo, fromHi] = this.safetyBounds(v, x);
|
|
3403
|
+
if (lo <= fromLo && fromHi <= hi)
|
|
3404
|
+
return [{ t: 'num', e: v.e, ty, range: [fromLo, fromHi] }, q];
|
|
3405
|
+
if (ty.signed)
|
|
3406
|
+
return refuse(x, 'a conversion with a potentially out-of-range signed result (implementation-defined)');
|
|
3407
|
+
return [{ t: 'num', e: this.safetyWrapUnsigned(v.e, BigInt(fromLo), BigInt(fromHi), hi + 1), ty, range: [lo, hi] }, q];
|
|
3408
|
+
}
|
|
3337
3409
|
if (ty && v.t === 'num')
|
|
3338
3410
|
return [{ t: 'num', e: this.wrap(v.e, ty.ub ? { bits: ty.bits, signed: ty.signed, name: ty.name, t: ty.t } : ty, x, q), ty }, q];
|
|
3339
3411
|
return refuse(x, v.t === 'opaque' && v.ask ? `${String(x.why)}: of ${v.why}` : String(x.why));
|
|
3340
3412
|
}
|
|
3341
3413
|
case 'binop': {
|
|
3342
|
-
const [a, q] = this.ev(x.x, p), [b, q2] = this.ev(x.y, q);
|
|
3343
|
-
|
|
3414
|
+
const [a, q] = this.ev(x.x, p), [b, q2] = this.ev(x.y, q), op = String(x.op);
|
|
3415
|
+
if (this.safety && !['==', '!=', '<', '<=', '>', '>='].includes(op))
|
|
3416
|
+
return this.safetyArith(op, a, b, x, q2);
|
|
3417
|
+
return [this.binop(op, a, b, x, q2), q2];
|
|
3344
3418
|
}
|
|
3345
3419
|
case 'andalso': {
|
|
3346
3420
|
// The right side runs only where the left doesn't decide: its failures and effects only there.
|
|
3347
3421
|
const and = x.op === '&&', [a, q] = this.ev(x.x, p), ab = this.boolv(a, x.x), c = and ? ab : I.not(ab);
|
|
3422
|
+
if (this.safety && c.op === 'const' && !c.v) {
|
|
3423
|
+
this.markSafetyDead(x.y);
|
|
3424
|
+
return [{ t: 'bool', e: ab }, q];
|
|
3425
|
+
}
|
|
3348
3426
|
const [b, q2] = this.ev(x.y, this.assume(q, c)), bb = this.boolv(b, x.y);
|
|
3427
|
+
if (this.safety)
|
|
3428
|
+
return [{ t: 'bool', e: and ? I.and(ab, bb) : I.or(ab, bb) }, this.merge(q2, this.assume(q, I.not(c)), x)];
|
|
3349
3429
|
return [{ t: 'bool', e: and ? I.and(ab, bb) : I.or(ab, bb) }, q2.heap === q.heap ? q : this.merge({ ...q2, pc: [...q.pc, c] }, q, x)];
|
|
3350
3430
|
}
|
|
3351
3431
|
case 'unop': {
|
|
3352
3432
|
const [a, q] = this.ev(x.x, p), op = String(x.op);
|
|
3353
3433
|
if (op === '!')
|
|
3354
3434
|
return [{ t: 'bool', e: I.not(this.boolv(a, x)) }, q];
|
|
3435
|
+
if (this.safety) {
|
|
3436
|
+
if (op === '+')
|
|
3437
|
+
return [a, q];
|
|
3438
|
+
if (op === '-')
|
|
3439
|
+
return this.safetyArith('-', { t: 'num', e: I.num(0) }, a, x, q);
|
|
3440
|
+
return refuse(x, `unsupported C safety unary operator: ${op}`);
|
|
3441
|
+
}
|
|
3355
3442
|
if (op === '-') {
|
|
3356
3443
|
if (a.t !== 'num')
|
|
3357
3444
|
return refuse(x, `- of a ${a.t}`);
|
|
@@ -3460,6 +3547,45 @@ export class Stepper {
|
|
|
3460
3547
|
const [b, q] = this.ev(x.s, p), [i, q2] = this.ev(x.i, q);
|
|
3461
3548
|
return this.listGet(b, i, x, q2);
|
|
3462
3549
|
}
|
|
3550
|
+
case 'table': {
|
|
3551
|
+
if (!this.safety)
|
|
3552
|
+
return refuse(x, 'table lookup requires C safety mode');
|
|
3553
|
+
const dims = x.dims, vals = x.vals, sites = x.sites, idxExprs = x.indices, isBool = Boolean(x.isBool);
|
|
3554
|
+
if (!dims?.length || dims.length !== sites?.length || dims.length !== idxExprs?.length)
|
|
3555
|
+
return refuse(x, 'malformed C safety table lookup');
|
|
3556
|
+
let q = p;
|
|
3557
|
+
const idxs = [];
|
|
3558
|
+
for (let k = 0; k < dims.length; k++) {
|
|
3559
|
+
const [iv, q2] = this.ev(idxExprs[k], q), idx = this.numv(iv, idxExprs[k]);
|
|
3560
|
+
const inside = I.and(I.cmp('<=', I.num(0), idx), I.cmp('<', idx, I.num(dims[k])));
|
|
3561
|
+
q = this.safetyCut(q2, { ...x, site: sites[k] }, 'bounds', I.not(inside));
|
|
3562
|
+
idxs.push(idx);
|
|
3563
|
+
}
|
|
3564
|
+
const total = dims.reduce((a, b) => a * b, 1);
|
|
3565
|
+
if (vals.length !== total)
|
|
3566
|
+
return refuse(x, 'C safety table size mismatch');
|
|
3567
|
+
const buildN = (k, off, stride) => {
|
|
3568
|
+
if (k === dims.length)
|
|
3569
|
+
return I.num(vals[off]);
|
|
3570
|
+
const next = stride / dims[k];
|
|
3571
|
+
let acc = buildN(k + 1, off + (dims[k] - 1) * next, next);
|
|
3572
|
+
for (let j = dims[k] - 2; j >= 0; j--)
|
|
3573
|
+
acc = I.nite(I.cmp('=', idxs[k], I.num(j)), buildN(k + 1, off + j * next, next), acc);
|
|
3574
|
+
return acc;
|
|
3575
|
+
};
|
|
3576
|
+
const buildB = (k, off, stride) => {
|
|
3577
|
+
if (k === dims.length)
|
|
3578
|
+
return vals[off] ? I.T : I.F;
|
|
3579
|
+
const next = stride / dims[k];
|
|
3580
|
+
let acc = buildB(k + 1, off + (dims[k] - 1) * next, next);
|
|
3581
|
+
for (let j = dims[k] - 2; j >= 0; j--)
|
|
3582
|
+
acc = I.bite(I.cmp('=', idxs[k], I.num(j)), buildB(k + 1, off + j * next, next), acc);
|
|
3583
|
+
return acc;
|
|
3584
|
+
};
|
|
3585
|
+
if (isBool)
|
|
3586
|
+
return [{ t: 'bool', e: buildB(0, 0, total) }, q];
|
|
3587
|
+
return [{ t: 'num', e: buildN(0, 0, total), ty: x.ity, range: [Math.min(...vals), Math.max(...vals)] }, q];
|
|
3588
|
+
}
|
|
3463
3589
|
case 'pop': {
|
|
3464
3590
|
const [s, q] = this.ev(x.s, p);
|
|
3465
3591
|
if (s.t === 'list' && x.ok) {
|
|
@@ -3522,8 +3648,11 @@ export class Stepper {
|
|
|
3522
3648
|
if (cv.t === 'opaque')
|
|
3523
3649
|
return [cv, q];
|
|
3524
3650
|
const c = this.truthy(cv, x.c, q);
|
|
3525
|
-
if (c.op === 'const')
|
|
3651
|
+
if (c.op === 'const') {
|
|
3652
|
+
if (this.safety)
|
|
3653
|
+
this.markSafetyDead(c.v ? x.b : x.a);
|
|
3526
3654
|
return this.ev((c.v ? x.a : x.b), q);
|
|
3655
|
+
}
|
|
3527
3656
|
const [a, qa] = this.ev(x.a, this.assume(q, c)), [b, qb] = this.ev(x.b, this.assume(q, I.not(c)));
|
|
3528
3657
|
return [this.ite(c, a, b, x), this.merge(qa, qb, x)];
|
|
3529
3658
|
}
|
|
@@ -5012,6 +5141,120 @@ export class Stepper {
|
|
|
5012
5141
|
return refuse(x, `a ${a.t === 'list' ? 'slice' : 'map'} compared with nil (nil and empty are one ${a.t === 'list' ? 'list' : 'map'} in the subset)`);
|
|
5013
5142
|
return refuse(x, `${a.t === 'opaque' && a.ask ? a.why : `a ${a.t}`} compared with ${b.t === 'opaque' && b.ask ? b.why : `a ${b.t}`}`);
|
|
5014
5143
|
}
|
|
5144
|
+
// Model only widths and intermediates representable exactly by the number-based IR.
|
|
5145
|
+
safetyRange(ty, at) {
|
|
5146
|
+
if (!ty || ty.bits < 1 || ty.bits > 32 || ty.mod)
|
|
5147
|
+
return refuse(at, 'an integer type outside the exact C safety subset (1–32 bits)');
|
|
5148
|
+
return rangeOf(ty);
|
|
5149
|
+
}
|
|
5150
|
+
safetyBounds(v, at) {
|
|
5151
|
+
if (v.e.op === 'const' && Number.isSafeInteger(v.e.v))
|
|
5152
|
+
return [v.e.v, v.e.v];
|
|
5153
|
+
if (v.range)
|
|
5154
|
+
return v.range;
|
|
5155
|
+
return this.safetyRange(v.ty, at);
|
|
5156
|
+
}
|
|
5157
|
+
safetyModulo(e, m, lo) {
|
|
5158
|
+
if (e.op === 'const')
|
|
5159
|
+
return I.num(Number((BigInt(e.v) % BigInt(m) + BigInt(m)) % BigInt(m)));
|
|
5160
|
+
if (lo !== undefined && lo >= 0)
|
|
5161
|
+
return I.modk(m, e);
|
|
5162
|
+
// The DSL's remainder is natural. Keep both operands in the natural domain.
|
|
5163
|
+
const r = I.modk(m, I.neg(e));
|
|
5164
|
+
return I.nite(I.cmp('<', e, I.num(0)), I.nite(I.cmp('=', r, I.num(0)), I.num(0), I.sub(I.num(m), r)), I.modk(m, e));
|
|
5165
|
+
}
|
|
5166
|
+
safetyWrapUnsigned(e, lo, hi, mod) {
|
|
5167
|
+
const M = BigInt(mod);
|
|
5168
|
+
if (e.op === 'const')
|
|
5169
|
+
return I.num(Number(((BigInt(e.v) % M) + M) % M));
|
|
5170
|
+
if (0n <= lo && hi < M)
|
|
5171
|
+
return e;
|
|
5172
|
+
if (0n <= lo && hi < 2n * M)
|
|
5173
|
+
return I.nite(I.cmp('<=', I.num(mod), e), I.sub(e, I.num(mod)), e);
|
|
5174
|
+
if (-M <= lo && hi < M)
|
|
5175
|
+
return I.nite(I.cmp('<', e, I.num(0)), I.add(e, I.num(mod)), e);
|
|
5176
|
+
if (lo >= 0n)
|
|
5177
|
+
return I.modk(mod, e);
|
|
5178
|
+
return this.safetyModulo(e, mod);
|
|
5179
|
+
}
|
|
5180
|
+
safetyArith(op, a, b, at, p) {
|
|
5181
|
+
if (a.t !== 'num' || b.t !== 'num')
|
|
5182
|
+
return refuse(at, `C safety arithmetic on ${a.t} and ${b.t}`);
|
|
5183
|
+
const ty = at.ity, [lo, hi] = this.safetyRange(ty, at), zero = I.num(0);
|
|
5184
|
+
const [al, ah] = this.safetyBounds(a, at), [bl, bh] = this.safetyBounds(b, at);
|
|
5185
|
+
if (al < lo || ah > hi || bl < lo || bh > hi)
|
|
5186
|
+
return refuse(at, 'arithmetic operands not converted to the C operation type');
|
|
5187
|
+
if (op === '&') {
|
|
5188
|
+
if (b.e.op !== 'const')
|
|
5189
|
+
return refuse(at, 'bitwise & by a nonconstant mask');
|
|
5190
|
+
const m = b.e.v + 1;
|
|
5191
|
+
if (m <= 1 || m > 2 ** 31 || !Number.isInteger(Math.log2(m)))
|
|
5192
|
+
return refuse(at, 'bitwise & with other than a positive power-of-two mask');
|
|
5193
|
+
return [{ t: 'num', e: this.safetyModulo(a.e, m, al), ty, range: [0, m - 1] }, p];
|
|
5194
|
+
}
|
|
5195
|
+
if (op === '/' || op === '%') {
|
|
5196
|
+
if (b.e.op !== 'const')
|
|
5197
|
+
return refuse(at, `${op} by a nonconstant divisor (no exact successful continuation in the subset)`);
|
|
5198
|
+
const d = b.e.v;
|
|
5199
|
+
// Both operators first require the quotient to be representable, even when the remainder is zero.
|
|
5200
|
+
const bad = d === 0 ? I.T : ty.signed && d === -1 ? (al > lo ? I.F : I.cmp('=', a.e, I.num(lo))) : I.F;
|
|
5201
|
+
const q = this.safetyCut(p, at, 'division', bad);
|
|
5202
|
+
const e = d === 0 ? zero : a.e.op === 'const' ? I.num(Number(op === '/' ? BigInt(a.e.v) / BigInt(d) : BigInt(a.e.v) % BigInt(d)))
|
|
5203
|
+
: op === '%' ? (al >= 0 ? I.modk(Math.abs(d), a.e) : this.rem(a.e, Math.abs(d)))
|
|
5204
|
+
: d < 0 ? I.neg(al >= 0 ? I.divk(-d, a.e) : this.quot(a.e, -d)) : (al >= 0 ? I.divk(d, a.e) : this.quot(a.e, d));
|
|
5205
|
+
let outLo = 0, outHi = 0;
|
|
5206
|
+
if (d !== 0) {
|
|
5207
|
+
if (op === '%') {
|
|
5208
|
+
const ad = Math.abs(d) - 1;
|
|
5209
|
+
outLo = al >= 0 ? 0 : Math.max(lo, al, -ad);
|
|
5210
|
+
outHi = ah <= 0 ? 0 : Math.min(hi, ah, ad);
|
|
5211
|
+
}
|
|
5212
|
+
else {
|
|
5213
|
+
const nal = ty.signed && d === -1 ? Math.max(al, lo + 1) : al;
|
|
5214
|
+
if (nal <= ah) {
|
|
5215
|
+
const q1 = Math.trunc(nal / d), q2 = Math.trunc(ah / d);
|
|
5216
|
+
outLo = Math.max(lo, Math.min(q1, q2));
|
|
5217
|
+
outHi = Math.min(hi, Math.max(q1, q2));
|
|
5218
|
+
}
|
|
5219
|
+
}
|
|
5220
|
+
}
|
|
5221
|
+
const range = bad.op === 'const' && !bad.v ? [outLo, outHi] : [Math.min(0, outLo), Math.max(0, outHi)];
|
|
5222
|
+
return [{ t: 'num', e: I.nite(bad, zero, e), ty, range }, q];
|
|
5223
|
+
}
|
|
5224
|
+
let resLoB, resHiB;
|
|
5225
|
+
switch (op) {
|
|
5226
|
+
case '+':
|
|
5227
|
+
resLoB = BigInt(al) + BigInt(bl);
|
|
5228
|
+
resHiB = BigInt(ah) + BigInt(bh);
|
|
5229
|
+
break;
|
|
5230
|
+
case '-':
|
|
5231
|
+
resLoB = BigInt(al) - BigInt(bh);
|
|
5232
|
+
resHiB = BigInt(ah) - BigInt(bl);
|
|
5233
|
+
break;
|
|
5234
|
+
case '*': {
|
|
5235
|
+
const xs = [al, ah].flatMap(l => [bl, bh].map(r => BigInt(l) * BigInt(r)));
|
|
5236
|
+
resLoB = xs.reduce((u, v) => (v < u ? v : u));
|
|
5237
|
+
resHiB = xs.reduce((u, v) => (v > u ? v : u));
|
|
5238
|
+
break;
|
|
5239
|
+
}
|
|
5240
|
+
default: return refuse(at, `unsupported C safety arithmetic operator: ${op}`);
|
|
5241
|
+
}
|
|
5242
|
+
const max = BigInt(Number.MAX_SAFE_INTEGER);
|
|
5243
|
+
if (resLoB < -max || resHiB > max)
|
|
5244
|
+
return refuse(at, 'a C product whose mathematical intermediate may exceed the exact IR range');
|
|
5245
|
+
const e = op === '+' ? I.add(a.e, b.e) : op === '-' ? I.sub(a.e, b.e) : I.mul(a.e, b.e);
|
|
5246
|
+
if (!ty.signed) {
|
|
5247
|
+
if (BigInt(lo) <= resLoB && resHiB <= BigInt(hi))
|
|
5248
|
+
return [{ t: 'num', e, ty, range: [Number(resLoB), Number(resHiB)] }, p];
|
|
5249
|
+
return [{ t: 'num', e: this.safetyWrapUnsigned(e, resLoB, resHiB, hi + 1), ty, range: [lo, hi] }, p];
|
|
5250
|
+
}
|
|
5251
|
+
const below = resLoB < BigInt(lo) ? I.cmp('<', e, I.num(lo)) : I.F;
|
|
5252
|
+
const above = resHiB > BigInt(hi) ? I.cmp('<', I.num(hi), e) : I.F;
|
|
5253
|
+
const bad = I.or(below, above);
|
|
5254
|
+
const outLo = Math.max(lo, Number(resLoB)), outHi = Math.min(hi, Number(resHiB));
|
|
5255
|
+
const range = bad.op === 'const' && !bad.v ? [outLo, outHi] : outLo <= outHi ? [Math.min(0, outLo), Math.max(0, outHi)] : [0, 0];
|
|
5256
|
+
return [{ t: 'num', e: I.nite(bad, zero, e), ty, range }, this.safetyCut(p, at, 'overflow', bad)];
|
|
5257
|
+
}
|
|
5015
5258
|
// Integer arithmetic at the type's width: the exact result, wrapped as the type wraps it (or refused, at 64 bits).
|
|
5016
5259
|
arith(op, a, b, x, p) {
|
|
5017
5260
|
// (other code's value left out, computed with: named, so the next run answers it)
|
|
@@ -5266,11 +5509,20 @@ export class Stepper {
|
|
|
5266
5509
|
}
|
|
5267
5510
|
inside(s, i, at, p) {
|
|
5268
5511
|
const o = this.listObj(s, at, p), idx = this.numv(i, at), len = this.listLen(s, at, p), inside = I.and(I.cmp('<=', I.num(0), idx), I.cmp('<', idx, len));
|
|
5269
|
-
this.
|
|
5512
|
+
if (this.safety) {
|
|
5513
|
+
if (o.decl.len === undefined)
|
|
5514
|
+
return refuse(at, 'an access without a fixed C array extent');
|
|
5515
|
+
// listGet/listSet already apply the defined-prefix cut returned as `inside`.
|
|
5516
|
+
this.safetyCut(p, at, 'bounds', I.not(inside));
|
|
5517
|
+
}
|
|
5518
|
+
else
|
|
5519
|
+
this.refuseWhere(p, I.not(inside), at, o.decl.len !== undefined ? `an index outside [0, ${o.decl.len}) (undefined behavior)` : 'an index out of range (a panic)');
|
|
5270
5520
|
return [o, idx, inside];
|
|
5271
5521
|
}
|
|
5272
5522
|
listGet(s, i, at, p) {
|
|
5273
|
-
const [o, idx, inside] = this.inside(s, i, at, p)
|
|
5523
|
+
const [o, idx, inside] = this.inside(s, i, at, p);
|
|
5524
|
+
// The term may be nested in another guard: use a valid placeholder off its defined prefix.
|
|
5525
|
+
const e = { op: 'cell', map: o.decl.name, key: { op: 'num', e: this.safety ? I.nite(inside, idx, I.num(0)) : idx }, field: '$item' };
|
|
5274
5526
|
return [o.decl.fields[0].sort.k === 'num' ? { t: 'num', e: e, ty: at.ty } : { t: 'bool', e: e }, this.assume(p, inside)];
|
|
5275
5527
|
}
|
|
5276
5528
|
listSet(s, i, v, at, p) {
|
package/dist/core/extract.d.ts
CHANGED
|
@@ -80,6 +80,16 @@ export type Name = {
|
|
|
80
80
|
export declare function namesOf(m: Model): Name[];
|
|
81
81
|
export declare function extract(at: Source, opts?: ExtractOptions): Extraction;
|
|
82
82
|
export declare function fromModel(model: Model, file: string, domains: Record<string, 'nat' | 'int'> | undefined, notes: string[], run: (rule: Rule, sorts: Sorts) => Evidence): Extraction;
|
|
83
|
+
export declare function fromSafetyModel(model: Model, file: string, domains?: Record<string, 'nat' | 'int'>): {
|
|
84
|
+
ok: boolean;
|
|
85
|
+
rule?: Rule;
|
|
86
|
+
invariants?: Law[];
|
|
87
|
+
source?: string;
|
|
88
|
+
refusals: {
|
|
89
|
+
where: string;
|
|
90
|
+
what: string;
|
|
91
|
+
}[];
|
|
92
|
+
};
|
|
83
93
|
export declare const HARNESS_FAILED = "the check's harness failed, twice (a request past its time or its transport, not the code): ";
|
|
84
94
|
export declare const toSource: (x: Extraction) => string;
|
|
85
95
|
export type { Evidence, Model, Term };
|
package/dist/core/extract.js
CHANGED
|
@@ -44,7 +44,7 @@ import * as I from "./extract-ir.js";
|
|
|
44
44
|
import { PASS } from "./extract-ir.js";
|
|
45
45
|
import { Refusal, placed, sourceOf, syntaxError, tooDeep } from "./extract-read.js";
|
|
46
46
|
import { isFactory, isSlice, readClass, readFactory, readFunctions, readSlice, readStandalone } from "./extract-shapes.js";
|
|
47
|
-
import { kindOf, mapEmpty, mapKind, natOK, pb, pidx, pk, pnum, pterm, sortsOf } from "./extract-print.js";
|
|
47
|
+
import { kindOf, mapEmpty, mapKind, natOK, nonneg, pb, pidx, pk, pnum, pterm, sortsOf } from "./extract-print.js";
|
|
48
48
|
import { check } from "./extract-check.js";
|
|
49
49
|
import { extractOrm } from "./extract-orm.js";
|
|
50
50
|
// (a rule read is proven from the same import: proveRule(x.rule!, [x.rule!.law(...)]))
|
|
@@ -202,6 +202,35 @@ export function fromModel(model, file, domains, notes, run) {
|
|
|
202
202
|
attachSource(rule, evidence, invariants);
|
|
203
203
|
return { ok: true, rule, ...(invariants.length ? { invariants } : {}), source, model, sorts, evidence, refusals: [], notes };
|
|
204
204
|
}
|
|
205
|
+
// Internal builder for the C safety first-fault monitor machine: evaluates the
|
|
206
|
+
// monitored model and its invariants without marking the monitor rule as an
|
|
207
|
+
// unchecked normal source extraction (C safety uses its own strict AST subset
|
|
208
|
+
// gate, independent checkLaws/checkKInduction, and ASan/UBSan replay, and never
|
|
209
|
+
// exposes this rule via Extraction.rule).
|
|
210
|
+
export function fromSafetyModel(model, file, domains) {
|
|
211
|
+
let sorts, body, source;
|
|
212
|
+
try {
|
|
213
|
+
const s = sortsOf(model, domains);
|
|
214
|
+
sorts = s.sorts;
|
|
215
|
+
datesInRange(model, sorts);
|
|
216
|
+
body = builderOf(model, sorts);
|
|
217
|
+
source = moduleOf(model, body, file);
|
|
218
|
+
}
|
|
219
|
+
catch (e) {
|
|
220
|
+
if (e instanceof Refusal)
|
|
221
|
+
return { ok: false, refusals: [{ where: e.where, what: e.what }] };
|
|
222
|
+
throw e;
|
|
223
|
+
}
|
|
224
|
+
let rule, invariants;
|
|
225
|
+
try {
|
|
226
|
+
rule = evaluate(body);
|
|
227
|
+
invariants = evaluateLaws(rule, invariantsOf(model, sorts));
|
|
228
|
+
}
|
|
229
|
+
catch (e) {
|
|
230
|
+
return { ok: false, source, refusals: [{ where: file, what: `the rule printed doesn't build: ${e.message}` }] };
|
|
231
|
+
}
|
|
232
|
+
return { ok: true, rule, invariants, source, refusals: [] };
|
|
233
|
+
}
|
|
205
234
|
// The check's own failure (twice): no verdict on the code, neither a difference nor a pass.
|
|
206
235
|
export const HARNESS_FAILED = 'the check\'s harness failed, twice (a request past its time or its transport, not the code): ';
|
|
207
236
|
// A Date holds a time within ±8.64e15 ms; past it the code's Date is invalid (NaN). The rule refuses a step that would
|
|
@@ -523,8 +552,13 @@ function hoist(m, P, roots) {
|
|
|
523
552
|
// (a product of integers kept at 0 or above only by the conditions the path is under: printed where it is read,
|
|
524
553
|
// under them, never hoisted out of them)
|
|
525
554
|
let guarded = false;
|
|
526
|
-
walk(node, o => {
|
|
527
|
-
|
|
555
|
+
walk(node, o => {
|
|
556
|
+
const ctx0 = { m, sorts: P.sorts, atoms: [] };
|
|
557
|
+
if (o.op === 'mul' && !(natOK(o.a, ctx0) && natOK(o.b, ctx0)))
|
|
558
|
+
guarded = true;
|
|
559
|
+
if (o.op === 'modk' && !natOK(o.a, ctx0) && !nonneg(o.a, ctx0))
|
|
560
|
+
guarded = true;
|
|
561
|
+
});
|
|
528
562
|
if (guarded)
|
|
529
563
|
continue;
|
|
530
564
|
if (!isBool(node, m)) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function answerSignature(source: string, constants?: Record<string, number | string | boolean>): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Spelling used by C's answer bindings: exact integer constants or a placeholder
|
|
2
|
+
// for a source argument that is not part of the key. Split only at outer commas.
|
|
3
|
+
export function answerSignature(source, constants) {
|
|
4
|
+
const args = [];
|
|
5
|
+
let depth = 0, start = 0;
|
|
6
|
+
for (let i = 0; i < source.length; i++) {
|
|
7
|
+
const ch = source[i];
|
|
8
|
+
if (ch === '(' || ch === '[')
|
|
9
|
+
depth++;
|
|
10
|
+
else if (ch === ')' || ch === ']')
|
|
11
|
+
depth--;
|
|
12
|
+
else if (ch === ',' && depth === 0) {
|
|
13
|
+
args.push(source.slice(start, i).trim());
|
|
14
|
+
start = i + 1;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (source.slice(start).trim())
|
|
18
|
+
args.push(source.slice(start).trim());
|
|
19
|
+
return args.map(a => {
|
|
20
|
+
const k = /^-?\d+$/.test(a) ? Number(a) : constants && /^[A-Za-z_]\w*$/.test(a) && Object.hasOwn(constants, a) ? constants[a] : undefined;
|
|
21
|
+
return typeof k === 'number' && Number.isSafeInteger(k) ? String(k).replace('-', 'm') : 'x';
|
|
22
|
+
}).join('_');
|
|
23
|
+
}
|
package/dist/core/laws.js
CHANGED
|
@@ -31,6 +31,7 @@ import { Choice, Int, ItemsRef, Str, Term, all, any, forall, no, not, yes } from
|
|
|
31
31
|
import { copyVariable } from "./dsl-binders.js";
|
|
32
32
|
import { attachNativeLaw } from "./source-check.js";
|
|
33
33
|
import { nativeLaw } from "./native-law.js";
|
|
34
|
+
import { answerSignature } from "./law-answer.js";
|
|
34
35
|
import { agreement, isolation, noninterference } from "./relational.js";
|
|
35
36
|
import { CalendarError, businessDays } from "./calendar.js";
|
|
36
37
|
export { businessDays } from "./calendar.js";
|
|
@@ -734,7 +735,7 @@ function path(e, c, names, dry) {
|
|
|
734
735
|
// (a call answered per constant arguments, C's: f(em, BUS_OFF) its answer f_x_18, by the code's constants)
|
|
735
736
|
const call0 = e.segs[0];
|
|
736
737
|
if (call0 && 'call' in call0 && call0.args && names.some(x => x.role === 'answer' && x.code.startsWith(`${e.head}_`))) {
|
|
737
|
-
const sig =
|
|
738
|
+
const sig = answerSignature(call0.args, c.consts);
|
|
738
739
|
const per = names.find(x => x.role === 'answer' && x.code === `${e.head}_${sig}`);
|
|
739
740
|
if (!per)
|
|
740
741
|
throw new LawError(`${e.head}(${call0.args}): no answer of the code for these arguments (its answers: ${names.filter(x => x.role === 'answer' && x.code.startsWith(`${e.head}_`)).map(x => x.code).join(', ')})`);
|
|
@@ -764,7 +765,7 @@ function path(e, c, names, dry) {
|
|
|
764
765
|
// (an answered call is one value a step, whatever its arguments: written f(...) or f())
|
|
765
766
|
if (hit.role === 'answer' && next && 'call' in next && next.args) {
|
|
766
767
|
// (a call answered per constant arguments, C's: f(em, BUS_OFF) its answer f_x_18, by the code's constants)
|
|
767
|
-
const sig =
|
|
768
|
+
const sig = answerSignature(next.args, c.consts);
|
|
768
769
|
const per = names.find(x => x.role === 'answer' && x.code === `${code}_${sig}`);
|
|
769
770
|
if (per) {
|
|
770
771
|
c.roles.add(per.role);
|
|
@@ -1107,17 +1108,3 @@ function split(s, sep) {
|
|
|
1107
1108
|
}
|
|
1108
1109
|
return [...out, rest].map(t => t.trim()).filter(Boolean);
|
|
1109
1110
|
}
|
|
1110
|
-
// (a call's arguments as written, split at the commas outside parentheses)
|
|
1111
|
-
function splitArgs(s) { const out = []; let d = 0, cur = ''; for (const ch of s) {
|
|
1112
|
-
if (ch === '(' || ch === '[')
|
|
1113
|
-
d++;
|
|
1114
|
-
else if (ch === ')' || ch === ']')
|
|
1115
|
-
d--;
|
|
1116
|
-
if (ch === ',' && !d) {
|
|
1117
|
-
out.push(cur.trim());
|
|
1118
|
-
cur = '';
|
|
1119
|
-
}
|
|
1120
|
-
else
|
|
1121
|
-
cur += ch;
|
|
1122
|
-
} if (cur.trim())
|
|
1123
|
-
out.push(cur.trim()); return out; }
|
package/dist/core/native-law.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { nativeSame } from "./source-check.js";
|
|
2
|
+
import { answerSignature } from "./law-answer.js";
|
|
2
3
|
const NONE = Symbol('language None');
|
|
3
4
|
const truthy = (value) => value !== NONE && Boolean(value);
|
|
4
5
|
const object = (x) => x !== null && (typeof x === 'object' || typeof x === 'function');
|
|
@@ -62,9 +63,14 @@ export function nativeLaw(expression, opts, parse, constants = {}, knownNames =
|
|
|
62
63
|
fields.push(segment.f);
|
|
63
64
|
}
|
|
64
65
|
const ordinary = Object.hasOwn(observation.bindings, e.head) || object(state) && e.head in state || Object.hasOwn(observation.params, e.head);
|
|
66
|
+
const segment = e.segs[fields.length - 1];
|
|
67
|
+
const specific = segment && 'call' in segment && segment.args ? `${fields.join('.')}_${answerSignature(segment.args, constants)}` : undefined;
|
|
68
|
+
let unsupportedArgs = false;
|
|
65
69
|
const matches = readings.flatMap(reading => {
|
|
66
70
|
if (reading.method && reading.method !== observation.method)
|
|
67
71
|
return [];
|
|
72
|
+
if (specific && reading.kind === 'answer' && reading.code === specific && reading.key === specific)
|
|
73
|
+
return [{ reading, consumed: fields.length }];
|
|
68
74
|
const parts = reading.code.split('.');
|
|
69
75
|
let consumed;
|
|
70
76
|
if (parts.every((part, i) => fields[i] === part))
|
|
@@ -76,12 +82,14 @@ export function nativeLaw(expression, opts, parse, constants = {}, knownNames =
|
|
|
76
82
|
const next = e.segs[consumed], called = next && 'call' in next;
|
|
77
83
|
if (ordinary && !called || reading.kind === 'clock' && called)
|
|
78
84
|
return [];
|
|
79
|
-
if (called && next.args)
|
|
80
|
-
|
|
85
|
+
if (called && next.args) {
|
|
86
|
+
unsupportedArgs = true;
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
81
89
|
return [{ reading, consumed: consumed + (called ? 1 : 0) }];
|
|
82
90
|
}).sort((a, b) => b.consumed - a.consumed);
|
|
83
91
|
if (!matches.length)
|
|
84
|
-
return undefined;
|
|
92
|
+
return unsupportedArgs ? unsupported('specific arguments on an observed one-value-per-step answer') : undefined;
|
|
85
93
|
const selected = matches[0];
|
|
86
94
|
if (matches.some(other => other.consumed === selected.consumed && (other.reading.key !== selected.reading.key || other.reading.kind !== selected.reading.kind)))
|
|
87
95
|
return unsupported(`ambiguous observed answer ${e.head}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gdp-wired",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.56.0",
|
|
4
4
|
"description": "An automatic prover for stateful rules (budgets, caps, windows, workflows, policies): laws proven with no proof to write, checked again in Lean, counterexamples found on the GPU, the rule shipped as code",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"verification",
|