import assert from 'node:assert/strict'; import {basename} from 'node:path'; import {workerSymbols} from './k2100-duration-capture.mjs'; import {inspectDurationClock} from './k2100-duration-window-proof.mjs'; const words = b => Array.from({length: b.length / 8}, (_, i) => Number(b.readBigUInt64LE(i * 8))); const mask = 0x0000fffffffff000n; const checksum = payload => Buffer.from(payload, 'latin1').reduce((a, b) => (a + b) & 255, 0).toString(16).padStart(2, '0'); function exchange(address, size, before, after) { const hex = n => {const b = Buffer.alloc(size); if (size === 1) b[0] = Number(n); else b.writeBigUInt64LE(BigInt(n)); return b.toString('hex');}; const a = address.toString(16), n = size.toString(16); return [`m${a},${n}`, hex(before), `M${a},${n}:${hex(after)}`, 'OK', `m${a},${n}`, hex(after)]; } export function durationPhysicalPlan(data, symbols, cpuCount) { const plan = Object.entries(workerSymbols).map(([name, size]) => { assert.equal(symbols[name]?.size, size, name); return {name, symbol: name, val: symbols[name].address, size}; }); for (const name of ['OS_K2045_EVIDENCE', 'OS_K2045_BACKEND_LAYOUT', 'OS_K2045_LEASE_LAYOUT', 'OS_K2045_GUARD_LAYOUT']) plan.push({name, symbol: name, ...{val: symbols[name].address, size: symbols[name].size}}); const e = words(data.OS_K2045_EVIDENCE), records = words(data.OS_K2100_WORKER_RECORDS), backend = words(data.OS_K2045_BACKEND_LAYOUT); plan.push({name: 'authority', val: e[100], size: e[101]}, {name: 'lifecycle', val: e[70], size: e[71]}, {name: 'shared', val: e[88], size: e[90]}, {name: 'root', val: e[6], size: 4096}, {name: 'ramL2', val: Number(data.root.readBigUInt64LE(8) & mask), size: 4096}); for (let cpu = 1; cpu < cpuCount; cpu++) { const r = records.slice(cpu * 32, (cpu + 1) * 32); plan.push({name: 'table' + cpu, val: r[20], size: r[21]}, {name: 'stackOwner' + cpu, val: r[22], size: backend[3]}, {name: 'stackPages' + cpu, val: r[5], size: 20480}, {name: 'stackL3' + cpu, val: Number(data.ramL2.readBigUInt64LE(((r[5] >>> 21) & 511) * 8) & mask), size: 4096}); } return plan; } // Bind every captured byte range to its physical source and the ordered QMP // stop/read/continue exchange. During the workload, the debugger may only // acknowledge a checkpoint or open the duration gate; it cannot write results. export function inspectDurationProvenance({physical, symbols, cpuCount, memoryMiB, qmp, timeline, wholeRsp, readArtifact, long}) { const qmpIndex = new Map(), rspIndex = new Map(); for (const [index, row] of qmp.entries()) if (row.message.id !== undefined) { const key = row.direction + ':' + row.message.id; assert.ok(!qmpIndex.has(key)); qmpIndex.set(key, {index, row}); } for (const [index, row] of wholeRsp.entries()) if (row.id !== null) { const key = row.direction + ':' + row.id; assert.ok(!rspIndex.has(key)); rspIndex.set(key, {index, row}); } const captures = [{capture: physical.checkpoints[0], stage: 21000, prefix: 'smp-workers-0', ordinal: 0}, ...physical.windowCheckpoints.map((capture, i) => ({capture, stage: 21101 + i, prefix: 'duration-window-' + (i + 1), ordinal: i + 1})), {capture: physical.checkpoints[1], stage: 21001, prefix: 'smp-workers-1', ordinal: -1}]; let previousQmp = -1, previousRsp = -1, firstQmp, firstRsp, totalReads = 0, totalWrites = 0; const includedQmp = new Set(), includedRsp = new Set(); for (const {capture, stage, prefix, ordinal} of captures) { const data = Object.fromEntries(Object.entries(capture.dumps).map(([name, a]) => [name, readArtifact(a)])); const expectedControl = ordinal === 0 ? 0n : !long ? 2n : ordinal === -1 || ordinal > physical.hostDurationAdmission?.window ? 1n : 0n; assert.equal(data.OS_K2100_HOST_DURATION_CONTROL.readBigUInt64LE(), expectedControl, 'captured host gate agrees with debugger admission'); const plan = durationPhysicalPlan(data, symbols, cpuCount), steps = timeline.filter(row => row.stage === stage); const requests = steps.filter(row => row.kind === 'qmp'), debuggerSteps = steps.filter(row => row.kind === 'rsp'); assert.deepEqual(requests.map(row => row.execute), ['stop', 'query-status', ...plan.map(() => 'pmemsave'), 'cont', 'query-status']); assert.deepEqual(capture.captures.map(row => row.name), plan.map(row => row.name)); assert.deepEqual(Object.keys(capture.dumps), plan.map(row => row.name)); assert.deepEqual(requests.slice(2, -2).map(row => row.id), capture.captures.map(row => row.requestId)); let last = previousQmp; for (const [i, request] of requests.entries()) { const tx = qmpIndex.get('tx:' + request.id), rx = qmpIndex.get('rx:' + request.id); assert.ok(tx && rx && tx.index > last && rx.index > tx.index); assert.equal(tx.row.message.execute, request.execute); assert.equal(rx.row.message.error, undefined); assert.ok(Object.hasOwn(rx.row.message, 'return')); includedQmp.add(request.id); if (request.execute !== 'pmemsave') assert.equal(tx.row.message.arguments, undefined); if (request.execute === 'query-status') assert.equal(rx.row.message.return.status, i === 1 ? 'paused' : 'running'); else assert.deepEqual(rx.row.message.return, {}); if (request.execute === 'pmemsave') { const p = plan[i - 2], actual = capture.captures[i - 2]; for (const key of ['name', 'symbol', 'val', 'size']) assert.equal(actual[key], p[key]); assert.ok(Number.isSafeInteger(p.val) && Number.isSafeInteger(p.size) && p.size > 0); assert.ok(p.val >= 0x40000000 && p.val + p.size <= 0x40000000 + memoryMiB * 1048576); assert.equal(data[p.name].length, p.size); const a = tx.row.message.arguments; assert.deepEqual(Object.keys(a).sort(), ['filename', 'size', 'val']); assert.equal(a.val, p.val); assert.equal(a.size, p.size); assert.equal(basename(a.filename), prefix + '-' + p.name + '.bin'); assert.equal(basename(capture.dumps[p.name].path), basename(a.filename)); totalReads++; } firstQmp ??= tx.index; last = rx.index; } previousQmp = last; const gate = ordinal > 0 ? symbols.OS_K2100_WORKLOAD_WINDOW_ACK.address : symbols.OS_K2100_WORKER_CAPTURE_GATE.address; assert.equal(capture.gateAddress, gate); const expected = ordinal > 0 ? exchange(gate, 8, ordinal - 1, ordinal) : exchange(gate, 1, 0, 1); assert.deepEqual(capture.rsp.filter(row => row.id !== null).map(row => row.payload), expected); if (ordinal === 0 && !long) { expected.push(...exchange(symbols.OS_K2100_HOST_DURATION_CONTROL.address, 8, 0, 1), ...exchange(symbols.OS_K2100_HOST_DURATION_CONTROL.address, 8, 1, 2)); } if (long && ordinal === physical.hostDurationAdmission?.window) { inspectDurationClock(physical.hostDurationAdmission.hostClock, {requireDuration: true}); assert.deepEqual(physical.hostDurationAdmission.hostClock, capture.hostClock); const admission = exchange(symbols.OS_K2100_HOST_DURATION_CONTROL.address, 8, 0, 1); assert.deepEqual(physical.hostDurationAdmission.rsp.filter(row => row.id !== null).map(row => row.payload), admission); expected.push(...admission); } assert.equal(debuggerSteps.length, expected.length); assert.deepEqual(capture.rsp.filter(row => row.id !== null), debuggerSteps.slice(0, 6).map(step => { const item = rspIndex.get(step.direction + ':' + step.id); assert.ok(item); return item.row; })); for (const [i, step] of debuggerSteps.entries()) { assert.equal(step.direction, i % 2 ? 'rx' : 'tx'); const item = rspIndex.get(step.direction + ':' + step.id); assert.ok(item && item.index > previousRsp); const row = item.row; assert.equal(row.payload, expected[i]); assert.equal(row.packet, '$' + row.payload + '#' + checksum(row.payload)); if (i % 2) assert.equal(step.id, debuggerSteps[i - 1].id); if (row.direction === 'tx' && row.payload.startsWith('M')) totalWrites++; includedRsp.add(step.direction + ':' + step.id); firstRsp ??= item.index; previousRsp = item.index; } assert.deepEqual(steps, [...requests.slice(0, -2), ...debuggerSteps, ...requests.slice(-2)], 'all reads before acknowledgement before resume'); } // The clock's conservative running confirmation is an additional read-only // query-status between cont and the runner's ordinary query-status. for (const row of qmp.slice(firstQmp, previousQmp + 1)) if (row.direction === 'tx' && !includedQmp.has(row.message.id)) { assert.equal(row.message.execute, 'query-status', 'no hidden host mutation during workload'); const response = qmpIndex.get('rx:' + row.message.id); assert.ok(response); assert.equal(response.row.message.return.status, 'running'); } for (const row of wholeRsp.slice(firstRsp, previousRsp + 1)) if (row.id !== null) assert.ok(includedRsp.has(row.direction + ':' + row.id), 'no unreported debugger operation during workload'); if (long) assert.ok(physical.hostDurationAdmission?.window > 0, 'one measured host admission'); return {physicalCheckpoints: captures.length, physicalReads: totalReads, permittedGateWrites: totalWrites, resultWrites: 0, qmpOrderBound: true, debuggerOrderBound: true}; }