AtlatestRepositorysigil-http3

sigil-http3 / tree / scriptsprepare-spike-trace.mjs

1// Apply only to the private Linux dependency; keep the committed pin intact.
2import { execFileSync } from 'node:child_process';
3import { createHash } from 'node:crypto';
4import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
5import { resolve } from 'node:path';
6
7const root = resolve('.deps/sigil-quic');
8const pin = readFileSync('quic.rev', 'utf8').trim();
9if (execFileSync('git', ['-C', root, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim() !== pin)
10 throw new Error('Trace base pin mismatch');
11execFileSync('git', ['-C', root, 'diff', '--exit-code'], { stdio: 'inherit' });
12const originals = new Map();
13const sources = new Map();
14function 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));
21for (const file of ['connection.sgl', 'endpoint.sgl'])
22 change(file, ' (import (sigil crypto)', ' (import (sigil quic spike trace)\n (sigil crypto)');
24change('endpoint.sgl', '(let* ((processed? #f)\n (host',
25 '(let* ((processed? #f)\n (raw (string->utf8 (car received)))\n (host');
26change('endpoint.sgl', ' (when (and address\n', ` (spike-ingress! raw host port
27 (and (quic-endpoint-connection endpoint)
28 (bytevector-length (quic-local-id (quic-endpoint-connection endpoint)))))
29 (unless (and address
30 (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 address
34`);
35change('endpoint.sgl', '(let ((bytes (string->utf8 (car received))))', '(let ((bytes raw))');
36change('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)`);
40change('endpoint.sgl', ' (admit! endpoint bytes address now))',
41` (admit! endpoint bytes address now)
42 (unless (quic-endpoint-connection endpoint)
43 (spike-trace! 'admission-no-connection
44 '(reason initial-not-admitted-or-retry-sent see-retry-out))))`);
45change('endpoint.sgl', ' (cond\n ((and sent',
46` (spike-trace! 'udp-out
47 (list 'host (car target) 'port (cdr target) 'bytes (bytevector-length packet)
48 'sent (or sent 0) 'dcid (spike-hex (quic-output-id output))))
49 (cond
50 ((and sent`);
51change('endpoint.sgl', ' (when (and sent (not (= sent (bytevector-length packet))))',
52` (spike-trace! 'retry-out
53 (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))))`);
57change('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!`);
61change('connection.sgl', ' (set-quic-connection-idle-expired?! connection #t)',
62` (spike-trace! 'idle-expiry
63 (list 'now (quic-connection-now connection) 'start start 'duration duration
64 'pto (connection-pto connection)))
65 (set-quic-connection-idle-expired?! connection #t)`);
66change('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?)`);
69change('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)`);
74change('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)))`);
78change('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.
84change('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-deferred
87 (list 'reason 'read-keys-not-ready 'bytes size
88 'budget-available (and (< (length (connection-level-deferred state)) 16)
89 (<= (+ (connection-level-deferred-bytes state) size) 65536))))
90 (when`);
91change('connection.sgl', " (status (packet-space-received-status space number)))",
92` (status (packet-space-received-status space number)))
93 (spike-trace! 'packet-authenticated
94 (list 'level level 'number number 'replay-status status))
95 (when frames
96 (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)))`);
102change('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-drop
106 (list 'reason 'authenticated-path-policy-or-replay 'number number 'status status)))
107 accepted?))`);
108change('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))`);
112change('connection.sgl', ' (when header\n (when (and (eq? (quic-header-kind header)',
113` (unless header
114 (spike-trace! 'packet-drop '(reason malformed-header-or-unowned-dcid))
115 (spike-reset-check! datagram))
116 (when header
117 (spike-trace! 'packet-header
118 (list 'kind (quic-header-kind header) 'offset offset
119 'dcid (spike-hex (quic-header-destination-id header))))
120 (when (and (eq? (quic-header-kind header)`);
121change('connection.sgl', ' (when admitted?\n (unless (quic-connection-started connection)',
122` (unless admitted?
123 (spike-trace! 'packet-drop
124 (list 'reason
125 (cond
126 ((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.
133change('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! connection
137`);
139const digest = text => createHash('sha256').update(text).digest('hex');
140const manifest = [];
141for (const [path, source] of sources) {
142 writeFileSync(path, source);
143 manifest.push({ path, before: digest(originals.get(path)), after: digest(source) });
145const modulePath = root + '/src/sigil/quic/spike/trace.sgl';
146mkdirSync(root + '/src/sigil/quic/spike', { recursive: true });
147const trace = readFileSync('tools/trace/trace.sgl', 'utf8');
148writeFileSync(modulePath, trace);
149manifest.push({ path: modulePath, after: digest(trace) });
150mkdirSync('build/spike-echo', { recursive: true });
151writeFileSync('build/spike-echo/trace-manifest.json', JSON.stringify({ scope: 'spike-only', base: pin, manifest }, null, 2) + '\n');
152writeFileSync('build/spike-echo/trace.patch', execFileSync('git', ['-C', root, 'diff'], { encoding: 'utf8' }));
153console.log('RESULT private-quic-trace-patch=APPLIED base=' + pin);