Faultcore Shared Memory Protocol¶
This document defines the shared binary contract between:
src/faultcore/shm_writer.py(Python writer)faultcore_network/src/shm_contract.rs(Rust contract)faultcore_network/src/shm_runtime.rs(Rust SHM runtime)
Segment¶
Name:
FAULTCORE_CONFIG_SHMor"/faultcore_<pid>_config"Type: POSIX SHM (
/dev/shm/...)Size:
FD table + TID hash table:
(MAX_FDS + MAX_TIDS) * CONFIG_SIZEPolicy state region:
MAX_POLICIES * sizeof(PolicyState)Target rules region:
MAX_TIDS * MAX_TARGET_RULES_PER_TID * sizeof(TargetRule)FD owner region (
fd -> tid_slot):MAX_FDS * sizeof(u64)
Region Layout Diagram¶
flowchart LR
SHM["SHM Segment"]
FD["FD table + TID hash<br/>(FaultcoreConfig rows)"]
PS["Policy state region"]
TR["Target rules region"]
FO["FD owner region<br/>(fd -> tid_slot)"]
SHM --> FD
SHM --> PS
SHM --> TR
SHM --> FO
Diagram focus: top-level SHM memory regions consumed by writer/runtime.
FaultcoreConfig (880 bytes)¶
Endianness: little-endian
Fixed packed layout
Field |
Offset |
Size |
Type |
|---|---|---|---|
|
0 |
4 |
|
|
4 |
8 |
|
|
12 |
8 |
|
|
20 |
8 |
|
|
28 |
8 |
|
|
36 |
8 |
|
|
44 |
8 |
|
|
52 |
8 |
|
|
60 |
8 |
|
|
68 |
8 |
|
|
76 |
8 |
|
|
84 |
8 |
|
|
92 |
8 |
|
|
100 |
8 |
|
|
108 |
8 |
|
|
116 |
8 |
|
|
124 |
8 |
|
|
132 |
8 |
|
|
140 |
8 |
|
|
148 |
8 |
|
|
156 |
8 |
|
|
164 |
8 |
|
|
172 |
8 |
|
|
180 |
8 |
|
|
188 |
8 |
|
|
196 |
8 |
|
|
204 |
8 |
|
|
212 |
8 |
|
|
220 |
8 |
|
|
228 |
8 |
|
|
236 |
8 |
|
|
244 |
8 |
|
|
252 |
8 |
|
|
260 |
8 |
|
|
268 |
8 |
|
|
276 |
8 |
|
|
284 |
8 |
|
|
292 |
8 |
|
|
300 |
8 |
|
|
308 |
8 |
|
|
316 |
8 |
|
|
324 |
8 |
|
|
332 |
8 |
|
|
340 |
8 |
|
|
348 |
8 |
|
|
356 |
8 |
|
|
364 |
8 |
|
|
372 |
4 |
|
|
376 |
8 |
|
|
384 |
8 |
|
|
392 |
16 |
|
|
408 |
32 |
|
|
440 |
32 |
|
|
472 |
8 |
|
|
480 |
8 |
|
|
488 |
8 |
|
|
496 |
8 |
|
|
504 |
8 |
|
|
512 |
8 |
|
|
520 |
8 |
|
|
528 |
8 |
|
|
536 |
8 |
|
|
544 |
8 |
|
|
552 |
8 |
|
|
560 |
8 |
|
|
568 |
8 |
|
|
576 |
8 |
|
|
584 |
8 |
|
|
592 |
8 |
|
|
600 |
8 |
|
|
608 |
64 |
|
|
672 |
8 |
|
|
680 |
32 |
|
|
712 |
8 |
|
|
720 |
32 |
|
|
752 |
8 |
|
|
760 |
8 |
|
|
768 |
8 |
|
|
776 |
8 |
|
|
784 |
8 |
|
|
792 |
8 |
|
|
800 |
8 |
|
|
808 |
8 |
|
|
816 |
64 |
|
Constants:
FAULTCORE_MAGIC = 0xFACC0DECONFIG_SIZE = 880MAX_FDS = 131072MAX_TIDS = 65536MAX_POLICIES = 1024MAX_TARGET_RULES_PER_TID = 8
Payload Mutation Notes¶
Mutation is applied only on stream operations (
send*/recv*) and never onconnector DNS resolution.Uplink mutation is evaluated before the send syscall and reorder staging stores the mutated payload.
Downlink mutation is evaluated after the recv syscall using only the received byte span.
If mutation cannot be applied (invalid params, out-of-bounds, size gate,
dry_run), payload is preserved.payload_mutation_reservedkeeps ABI space for future additions without immediate row-size change.
Target Rules Region¶
TargetRule is a fixed 152-byte row stored in a per-TID-slot table:
Field |
Type |
Notes |
|---|---|---|
|
|
|
|
|
Higher wins |
|
|
|
|
|
IPv4 address (lower 32 bits) |
|
|
CIDR prefix ( |
|
|
|
|
|
|
|
|
reserved |
|
|
|
|
|
unified IP bytes (network order) |
|
|
normalized hostname buffer (NUL padded, final vNext size) |
|
|
normalized SNI buffer (NUL padded, final vNext size) |
Selection semantics for targets[]:
consider first
target_enabledrules;choose matching rule with greatest
priority;ties are resolved by first rule in registration order.
Write/Read Consistency¶
All SHM writers (Python and Rust) use
ruleset_generationas the optimistic publish marker:mark
ruleset_generationas odd during write;write
magic+ payload;publish
ruleset_generationas even when done.
Rust readers validate stable reads using a double-read of
ruleset_generationplus fences.
Consistency Sequence Diagram¶
sequenceDiagram
participant Py as Python writer
participant SHM as SHM row
participant Rs as Rust reader
Py->>SHM: set ruleset_generation = odd
Py->>SHM: write magic + payload
Py->>SHM: set ruleset_generation = even
Rs->>SHM: read generation_before
Rs->>SHM: read payload
Rs->>SHM: read generation_after
alt stable even snapshot
Rs-->>Rs: accept row
else changed or odd
Rs-->>Rs: retry read
end
Diagram focus: odd/even publish protocol over ruleset_generation and reader stability check.
Compatibility Rule¶
Any change in offsets/size must:
update this document,
update Python and Rust together,
keep SHM contract tests green.
Compatibility Update Flow¶
flowchart TD
Change["Offset/size change proposed"] --> Doc["Update docs/shm_protocol.md"]
Doc --> Code["Update Python writer + Rust contract/runtime"]
Code --> Tests["Run SHM contract tests"]
Tests --> Gate{"Tests green?"}
Gate -->|Yes| Merge["Safe to merge"]
Gate -->|No| Fix["Fix layout mismatch and re-test"]
Fix --> Tests
Diagram focus: mandatory synchronization path for SHM schema changes.
Runtime Model over SHM¶
The SHM layout is stable, and runtime consumption is consolidated:
The engine builds
PacketContextby operation (Connect,Send,Recv,DnsLookup).The runtime-stage engine applies operation-aware stages in canonical order
R0..R8with applicability-based skips.All fault decisions flow through a single
LayerDecisionenum.The interceptor only maps
LayerDecisionto return values/errno (syscallsandgetaddrinfo).
This reduces duplicated logic between engine/interceptor and keeps behavior verifiable through mapping tests.
For module-level ownership and dataflow, see docs/architecture.md.