AtlatestRepositorysigil-http3
sigil-http3 / tree / scriptsprepare-spike-trace.mjs
1
// Apply only to the private Linux dependency; keep the committed pin intact.2
import { execFileSync } from 'node:child_process';3
import { createHash } from 'node:crypto';4
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';5
import { resolve } from 'node:path';7
const root = resolve('.deps/sigil-quic');8
const pin = readFileSync('quic.rev', 'utf8').trim();9
if (execFileSync('git', ['-C', root, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim() !== pin)10
throw new Error('Trace base pin mismatch');11
execFileSync('git', ['-C', root, 'diff', '--exit-code'], { stdio: 'inherit' });12
const originals = new Map();13
const sources = new Map();14
function change(file, before, after) {15
const path = root + '/src/sigil/quic/' + file;16
const source = sources.get(path) ?? readFileSync(path, 'utf8');17
originals.set(path, originals.get(path) ?? source);18
if (source.split(before).length !== 2) throw new Error('Trace anchor must occur once: ' + file + ' ' + before.slice(0, 80));19
sources.set(path, source.replace(before, after));20
}21
for (const file of ['connection.sgl', 'endpoint.sgl'])22
change(file, ' (import (sigil crypto)', ' (import (sigil quic spike trace)\n (sigil crypto)');24
change('endpoint.sgl', '(let* ((processed? #f)\n (host',25
'(let* ((processed? #f)\n (raw (string->utf8 (car received)))\n (host');26
change('endpoint.sgl', ' (when (and address\n', ` (spike-ingress! raw host port27
(and (quic-endpoint-connection endpoint)28
(bytevector-length (quic-local-id (quic-endpoint-connection endpoint)))))29
(unless (and address30
(eq? (quic-address-family address) (quic-endpoint-family endpoint))31
(receive-peer? endpoint (quic-address-host address) port))32
(spike-trace! 'udp-drop '(reason invalid-address-family-or-preconfirmation-source)))33
(when (and address34
`);35
change('endpoint.sgl', '(let ((bytes (string->utf8 (car received))))', '(let ((bytes raw))');36
change('endpoint.sgl', ' (when (<= 1 (bytevector-length bytes) packet-datagram-limit)',37
` (unless (<= 1 (bytevector-length bytes) packet-datagram-limit)38
(spike-trace! 'udp-drop '(reason datagram-size-limit)))39
(when (<= 1 (bytevector-length bytes) packet-datagram-limit)`);40
change('endpoint.sgl', ' (admit! endpoint bytes address now))',41
` (admit! endpoint bytes address now)42
(unless (quic-endpoint-connection endpoint)43
(spike-trace! 'admission-no-connection44
'(reason initial-not-admitted-or-retry-sent see-retry-out))))`);45
change('endpoint.sgl', ' (cond\n ((and sent',46
` (spike-trace! 'udp-out47
(list 'host (car target) 'port (cdr target) 'bytes (bytevector-length packet)48
'sent (or sent 0) 'dcid (spike-hex (quic-output-id output))))49
(cond50
((and sent`);51
change('endpoint.sgl', ' (when (and sent (not (= sent (bytevector-length packet))))',52
` (spike-trace! 'retry-out53
(list 'host (quic-address-host address) 'port (quic-address-port address)54
'sent (or sent 0) 'bytes (bytevector-length packet)))55
(when (and sent (not (= sent (bytevector-length packet))))`);57
change('connection.sgl', ' (let ((pto (connection-pto connection)))\n (set-quic-connection-ready!',58
` (spike-close! failure peer? silent? (quic-idle-expired? connection) (quic-connection-now connection))59
(let ((pto (connection-pto connection)))60
(set-quic-connection-ready!`);61
change('connection.sgl', ' (set-quic-connection-idle-expired?! connection #t)',62
` (spike-trace! 'idle-expiry63
(list 'now (quic-connection-now connection) 'start start 'duration duration64
'pto (connection-pto connection)))65
(set-quic-connection-idle-expired?! connection #t)`);66
change('connection.sgl', ' (packet-space-record-sent! space number size now tracked?)',67
` (spike-frame! 'tx-sealed frame target-address now)68
(packet-space-record-sent! space number size now tracked?)`);69
change('connection.sgl', " ((eq? opened 'discard) #f)",70
` ((eq? opened 'discard)71
(spike-trace! 'packet-drop '(reason packet-open-exception-before-authentication))72
(spike-reset-check! datagram)73
#f)`);74
change('connection.sgl', ' (set-quic-connection-failed-packets! connection (+ 1 (quic-connection-failed-packets connection)))',75
` (spike-trace! 'packet-drop '(reason current-and-next-key-authentication-failed))76
(spike-reset-check! datagram)77
(set-quic-connection-failed-packets! connection (+ 1 (quic-connection-failed-packets connection)))`);78
change('connection.sgl', ' (unless (packet-space-discarded? space)\n (if (not (connection-level-read-keys state))',79
` (when (packet-space-discarded? space)80
(spike-trace! 'packet-drop (list 'reason 'discarded-packet-space 'level level)))81
(unless (packet-space-discarded? space)82
(if (not (connection-level-read-keys state))`);83
// Keep defer semantics intact and distinguish it from a failed decrypt.84
change('connection.sgl', ' (let ((size (- (quic-header-end header) (quic-header-start header))))\n (when',85
` (let ((size (- (quic-header-end header) (quic-header-start header))))86
(spike-trace! 'packet-deferred87
(list 'reason 'read-keys-not-ready 'bytes size88
'budget-available (and (< (length (connection-level-deferred state)) 16)89
(<= (+ (connection-level-deferred-bytes state) size) 65536))))90
(when`);91
change('connection.sgl', " (status (packet-space-received-status space number)))",92
` (status (packet-space-received-status space number)))93
(spike-trace! 'packet-authenticated94
(list 'level level 'number number 'replay-status status))95
(when frames96
(for-each (lambda (frame)97
(spike-frame! 'rx-decoded frame source (quic-connection-now connection))) frames))98
(unless frames (spike-trace! 'packet-drop '(reason authenticated-frame-work-limit)))99
(when (and frames (not (quic-connection-frame-handler connection))100
(any frame-needs-handler? frames))101
(spike-trace! 'packet-drop '(reason application-handler-not-attached)))`);102
change('connection.sgl', ' (observe-received! connection source level number frames status credit))',103
` (let ((accepted? (observe-received! connection source level number frames status credit)))104
(unless accepted?105
(spike-trace! 'packet-drop106
(list 'reason 'authenticated-path-policy-or-replay 'number number 'status status)))107
accepted?))`);108
change('connection.sgl', ' (when (and (not (quic-closed? connection)) (<= 1 (bytevector-length datagram) packet-datagram-limit))',109
` (when (quic-closed? connection)110
(spike-trace! 'packet-drop (list 'reason 'already-closed 'idle-expired (quic-idle-expired? connection))))111
(when (and (not (quic-closed? connection)) (<= 1 (bytevector-length datagram) packet-datagram-limit))`);112
change('connection.sgl', ' (when header\n (when (and (eq? (quic-header-kind header)',113
` (unless header114
(spike-trace! 'packet-drop '(reason malformed-header-or-unowned-dcid))115
(spike-reset-check! datagram))116
(when header117
(spike-trace! 'packet-header118
(list 'kind (quic-header-kind header) 'offset offset119
'dcid (spike-hex (quic-header-destination-id header))))120
(when (and (eq? (quic-header-kind header)`);121
change('connection.sgl', ' (when admitted?\n (unless (quic-connection-started connection)',122
` (unless admitted?123
(spike-trace! 'packet-drop124
(list 'reason125
(cond126
((not (memq (quic-header-kind header) '(initial handshake application))) 'unsupported-header-kind)127
((not (receive-source? connection address (quic-header-kind header))) 'source-not-eligible-before-confirmation)128
((not (routed-header? connection header)) 'dcid-or-source-id-route-mismatch)129
(else 'undersized-client-initial)))))130
(when admitted?131
(unless (quic-connection-started connection)`);132
// Remember an authenticated peer transport reset token without printing it.133
change('connection.sgl', ' (set-quic-connection-network! connection\n',134
` (let ((token (assoc 2 (quic-peer-parameters connection))))135
(when token (spike-remember-reset! (cdr token))))136
(set-quic-connection-network! connection137
`);139
const digest = text => createHash('sha256').update(text).digest('hex');140
const manifest = [];141
for (const [path, source] of sources) {142
writeFileSync(path, source);143
manifest.push({ path, before: digest(originals.get(path)), after: digest(source) });144
}145
const modulePath = root + '/src/sigil/quic/spike/trace.sgl';146
mkdirSync(root + '/src/sigil/quic/spike', { recursive: true });147
const trace = readFileSync('tools/trace/trace.sgl', 'utf8');148
writeFileSync(modulePath, trace);149
manifest.push({ path: modulePath, after: digest(trace) });150
mkdirSync('build/spike-echo', { recursive: true });151
writeFileSync('build/spike-echo/trace-manifest.json', JSON.stringify({ scope: 'spike-only', base: pin, manifest }, null, 2) + '\n');152
writeFileSync('build/spike-echo/trace.patch', execFileSync('git', ['-C', root, 'diff'], { encoding: 'utf8' }));153
console.log('RESULT private-quic-trace-patch=APPLIED base=' + pin);