Phase G: §11.4 of the master spec. The C shim provides a BSD-sockets (POSIX) and a Winsock2 (Windows) backend, so attached mode runs on Linux, macOS, and Windows alike.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=int64), | public, | parameter | :: | MAX_FRAME_BYTES | = | 64_int64*1024_int64*1024_int64 |
Upper bound on a single length-prefixed INBOUND frame field (header or payload), enforced before allocation in =cortex_recv_frame=. The Cortex peer is on the loopback TCP port, but it is still untrusted input: without a cap a buggy/compromised peer can request ~4 GiB per field and exhaust memory. 64 MiB is generously above any legitimate request frame (headers are small JSON; bulk binary data rides the COPY_SOLUTION reply, which the worker sizes itself, not an inbound request). This cap deliberately does NOT apply to the send path — see =MAX_SEND_FRAME_BYTES=. |
| integer(kind=int64), | public, | parameter | :: | MAX_SEND_FRAME_BYTES | = | 4294967295_int64 |
Upper bound on an OUTBOUND frame field: the u32 wire length prefix (spec §12.2) can express at most 2^32-1 bytes. Legitimate COPY_SOLUTION replies routinely exceed the 64 MiB inbound cap (any 2D grid past ~1182² with 6 primitives), so the send side must not reuse =MAX_FRAME_BYTES= (audit 2026-07-06 N1). Handlers that build bulk replies pre-check against this limit and return a reply-level error instead of ever tripping the fatal send-side guard. |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(kind=c_char, len=1), | intent(in) | :: | host(*) | |||
| integer(kind=c_int), | value | :: | port |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=c_int), | value | :: | fd | |||
| type(c_ptr), | value | :: | buf | |||
| integer(kind=c_size_t), | value | :: | n |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=c_int), | value | :: | fd | |||
| type(c_ptr), | value | :: | buf | |||
| integer(kind=c_size_t), | value | :: | n |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=c_int), | value | :: | fd |
Decode a 4-byte big-endian length prefix as an UNSIGNED 32-bit value widened to int64. Each byte is masked into int64 before the shift/OR, so a wire value with the high bit set (0x80000000.. 0xFFFFFFFF) decodes as a large positive int64 — not a negative int32 (the desync bug). Range: [0, 4294967295].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int8), | intent(in) | :: | buf(4) |
A decoded frame length is acceptable iff it is non-negative and within the =MAX_FRAME_BYTES= cap. (Non-negativity is structurally guaranteed by =be_u32_to_i64=, but kept explicit so the predicate is correct for any int64 caller.)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(in) | :: | n |
Check that a send-side frame field (header or payload) can be represented by the u32 wire length prefix. Deliberately NOT symmetric with =is_frame_len_valid=: the 64 MiB inbound cap guards against an untrusted peer's request sizes, while outbound frames are sized by the worker itself and legitimately carry bulk COPY_SOLUTION replies far beyond 64 MiB (audit 2026-07-06 N1).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(in) | :: | n |
Parse a "tcp://host:port" URI into host string and integer port. Errors fatally on a malformed URI — bug in the caller.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | uri | |||
| character(len=*), | intent(out) | :: | host | |||
| integer, | intent(out) | :: | port |
Open a TCP loopback connection to the Cortex server.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | uri | |||
| integer, | intent(out) | :: | fd |
Send a length-prefixed frame: 4-byte BE header length, header bytes, 4-byte BE payload length, payload bytes. Both length prefixes are always present (payload length is zero when payload is empty; spec §12.2).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | fd | |||
| integer(kind=int8), | intent(in), | target | :: | header(:) | ||
| integer(kind=int8), | intent(in), | target | :: | payload(:) |
Receive a length-prefixed frame. Both header and payload are returned as allocatable byte arrays (caller owns).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | fd | |||
| integer(kind=int8), | intent(out), | allocatable | :: | header(:) | ||
| integer(kind=int8), | intent(out), | allocatable | :: | payload(:) |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | fd |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | fd | |||
| integer(kind=int8), | intent(in), | target | :: | buf(:) |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | fd | |||
| integer(kind=int8), | intent(inout), | target | :: | buf(:) |
Pack an UNSIGNED 32-bit length prefix from an int64 value in [0, 4294967295]. Takes int64 so a length in (2^31-1, 2^32-1] — legitimate for large COPY_SOLUTION replies — never round-trips through int32 (an out-of-range conversion, processor-dependent). Inverse of =be_u32_to_i64=.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(in) | :: | n | |||
| integer(kind=int8), | intent(out) | :: | out(4) |