What memory-alignment and byte-order assumptions must survive when serializing binary states?

Date of Record: October 26, 2026

When an autonomous instance or distributed runtime serializes binary state—such as memory-mapped tensor shards, zero-copy IPC buffers, ring-buffer checkpoints, or binary protocol frames—it cannot persist raw in-memory byte representations without locking the exact **endianness (byte-order)**, **struct alignment/padding layout**, **word-width boundaries**, and **IEEE-754 floating-point NaN/subnormal bit patterns** that governed the layout at write time. Emitting an unannotated binary buffer or relying on host-native struct packing (`@` or `=` in `struct` formats without explicit endianness markers) causes silent numerical corruption, `SIGBUS` unaligned memory traps on strict architectures, or cryptographic digest mismatches when a downstream consumer hydrates the state on a heterogeneous node.

---

1. The Architectural Failure Modes of Implicit Binary Layouts

A. Host-Native Endianness Drift (`Little-Endian` vs. `Big-Endian` Network Order)

If an instance serializes multi-byte integers or floats using host-native byte order (`sys.byteorder`) without recording an explicit endianness prefix (`<` for little-endian or `>` for big-endian) and a magic byte-order mark (e.g., `0xFEFF` / `0x1A2B3C4D`), a consumer reading the buffer in a different protocol or architecture context reverses the byte significance (`0x12345678` becomes `0x78563412`). Even on homogeneous x86_64/ARM64 little-endian clusters, mixing network-order (`!`/`>`) framing headers with little-endian (`<`) payload buffers without explicit field-level byte-order descriptors corrupts frame length prefixes and offset pointers.

B. Compiler and Runtime Struct Alignment Padding (`SIGBUS` and Offset Skew)

Compilers and C-struct runtimes insert implicit padding bytes between struct members to satisfy hardware alignment boundaries (e.g., aligning a `uint64` to an 8-byte boundary after a 1-byte `uint8` flag).

  1. **Offset Skew Across ABI Boundaries:** If a producer writes a native C struct containing implicit compiler padding and a consumer reads it using a packed layout (or a different ABI where `double` or `long` alignment differs), every subsequent field offset shifts by 1 to 7 bytes.
  2. **Unaligned Memory Access Traps:** Conversely, if a binary buffer is serialized tightly packed (`stride = 1`) and a downstream consumer `mmap`s the file directly into memory and casts an unaligned byte offset (e.g., offset `0x03`) to a 64-bit pointer or SIMD vector register (`AVX-512` requiring 64-byte alignment, or strict ARM/RISC-V atomic instructions requiring natural 8-byte alignment), the hardware raises an immediate alignment fault (`SIGBUS` / `AlignmentFaultError`).
  3. **Uninitialized Padding Leakage:** Implicit struct padding bytes contain arbitrary stack or heap residue from the producer's memory space. Hashing a raw struct without zero-filling padding bytes breaks deterministic SHA-256 verification and leaks process memory across trust boundaries.

C. Non-Canonical Floating-Point and Pointer Contamination

Binary state buffers that embed raw memory addresses (pointers), host `size_t` / `ssize_t` widths (32-bit vs. 64-bit), or non-canonical IEEE-754 `NaN` payloads (`0x7FF8000000000001` vs. `0xFFF8000000000000`) fail cross-runtime verification because hardware FPU pipelines do not guarantee bit-identical preservation of non-canonical `NaN` payload bits.

---

2. Mandatory Binary Serialization Invariants

Before any binary state artifact is committed to storage or transmitted across an IPC/network boundary, the serialization gate must enforce five deterministic invariants:

  1. **Explicit Endianness Lock:** Every binary schema must forbid native/ambiguous format specifiers (`@` or `=` in Python `struct`) and mandate explicit endianness (`<` little-endian or `>` big-endian) paired with a verifiable 4-byte endianness magic header (`0x43464C45` / `"CFLE"` or `0x43464245` / `"CFBE"`).
  2. **Zero-Ambiguity Field Offsets & Zeroed Padding:** Every field in the binary layout must declare its exact byte offset, byte size, and required alignment modulus ($A \in \{1, 2, 4, 8, 16, 32, 64\}$). Any alignment padding between fields or at the tail of a struct must be explicitly filled with `0x00` bytes so the serialized byte stream is 100% deterministic under SHA-256 hashing.
  3. **Base-Address Alignment Modulus Verification:** If the binary state is intended for zero-copy `mmap` or SIMD tensor hydration, the header must record the required base alignment (e.g., `64`-byte cache-line alignment for AVX-512 / DMA buffers, or `4096`-byte page alignment), and every internal section offset must satisfy $\text{offset} \bmod \text{alignment} == 0$.
  4. **Canonical IEEE-754 Float Normalization:** All floating-point fields must normalize negative zero (`-0.0` $\rightarrow$ `+0.0` unless signed zero is semantically required) and collapse non-canonical quiet/signaling `NaN` bit patterns to a single canonical quiet `NaN` bit pattern (`0x7FF8000000000000` for `float64`, `0x7FC00000` for `float32`) prior to packing.
  5. **Pointer-Free Relative Offset Addressing:** Raw memory addresses (`uintptr_t`) are strictly banned from serialized binary states; all internal references must be encoded as fixed-width unsigned relative byte offsets (`uint64`) validated to fall strictly within $[0, \text{payload\_bytes}]$.

---

3. Zero-Dependency Python Reference Implementation (`binary_layout_guard.py`)

The following standard-library module enforces explicit byte-order locking, alignment modulus verification, deterministic zero-padding, canonical float bit-normalization, and SHA-256 layout warrant generation before persisting or hydrating binary states.

from dataclasses import dataclass
import hashlib
import math
import struct
from typing import Dict, List, Sequence, Tuple, Union


class BinaryStateSerializationError(ValueError):
    """Raised when a binary state violates byte-order, alignment, or canonical bit invariants."""


@dataclass(frozen=True)
class BinaryFieldSpec:
    name: str
    fmt: str          # Single stdlib struct token: 'B', 'H', 'I', 'Q', 'i', 'q', 'f', 'd'
    offset: int       # Explicit byte offset from struct base
    alignment: int    # Required byte alignment (power of two: 1, 2, 4, 8, 16, 32, 64)


@dataclass(frozen=True)
class BinaryStateWarrant:
    schema_id: str
    byte_order: str
    magic_header_hex: str
    struct_stride_bytes: int
    base_alignment_bytes: int
    explicit_padding_bytes: int
    canonical_nan_enforced: bool
    layout_signature_sha256: str
    payload_sha256: str


_ALLOWED_SCALAR_FORMATS: Dict[str, Tuple[int, int]] = {
    "b": (1, 1),
    "B": (1, 1),
    "h": (2, 2),
    "H": (2, 2),
    "i": (4, 4),
    "I": (4, 4),
    "q": (8, 8),
    "Q": (8, 8),
    "f": (4, 4),
    "d": (8, 8),
}

MAGIC_LE = b"CFLE"  # 0x43464C45
MAGIC_BE = b"CFBE"  # 0x43464245


class DeterministicBinaryStateSerializer:
    """Enforces explicit byte-order, natural/SIMD alignment, and zero-filled padding."""

    def __init__(
        self,
        schema_id: str,
        byte_order: str,
        base_alignment_bytes: int,
        fields: Sequence[BinaryFieldSpec],
    ) -> None:
        if not schema_id or not schema_id.strip():
            raise BinaryStateSerializationError("schema_id must be a non-empty identifier.")
        if byte_order not in ("little", "big"):
            raise BinaryStateSerializationError(
                f"Implicit or unsupported byte_order '{byte_order}' rejected; must be 'little' or 'big'."
            )
        if base_alignment_bytes <= 0 or (base_alignment_bytes & (base_alignment_bytes - 1)) != 0:
            raise BinaryStateSerializationError(
                f"base_alignment_bytes ({base_alignment_bytes}) must be a positive power of two."
            )
        if not fields:
            raise BinaryStateSerializationError("Binary schema must declare at least one BinaryFieldSpec.")

        self.schema_id = schema_id.strip()
        self.byte_order = byte_order
        self.endian_prefix = "<" if byte_order == "little" else ">"
        self.magic = MAGIC_LE if byte_order == "little" else MAGIC_BE
        self.base_alignment_bytes = base_alignment_bytes
        self.fields: Tuple[BinaryFieldSpec, ...] = tuple(fields)

        self._validate_and_compute_layout()

    def _validate_and_compute_layout(self) -> None:
        cursor = 0
        padding_total = 0
        seen_names = set()
        descriptor_parts: List[str] = [
            f"schema={self.schema_id}",
            f"endian={self.byte_order}",
            f"base_align={self.base_alignment_bytes}",
        ]

        for f in self.fields:
            if f.name in seen_names:
                raise BinaryStateSerializationError(f"Duplicate field name '{f.name}' in binary schema.")
            seen_names.add(f.name)

            if f.fmt not in _ALLOWED_SCALAR_FORMATS:
                raise BinaryStateSerializationError(
                    f"Field '{f.name}' uses forbidden or host-dependent format '{f.fmt}'."
                )
            byte_width, natural_align = _ALLOWED_SCALAR_FORMATS[f.fmt]

            if f.alignment < natural_align or (f.alignment & (f.alignment - 1)) != 0:
                raise BinaryStateSerializationError(
                    f"Field '{f.name}' alignment ({f.alignment}) is weaker than natural alignment ({natural_align}) or not a power of two."
                )
            if f.alignment > self.base_alignment_bytes:
                raise BinaryStateSerializationError(
                    f"Field '{f.name}' alignment ({f.alignment}) exceeds base_alignment_bytes ({self.base_alignment_bytes})."
                )
            if f.offset < cursor:
                raise BinaryStateSerializationError(
                    f"Field '{f.name}' at offset {f.offset} overlaps preceding field ending at {cursor}."
                )
            if f.offset % f.alignment != 0:
                raise BinaryStateSerializationError(
                    f"Field '{f.name}' offset {f.offset} violates its {f.alignment}-byte alignment boundary."
                )

            gap = f.offset - cursor
            padding_total += gap
            cursor = f.offset + byte_width
            descriptor_parts.append(f"{f.name}:{f.fmt}@{f.offset}/{f.alignment}")

        # Pad total struct stride to a multiple of base_alignment_bytes
        remainder = cursor % self.base_alignment_bytes
        tail_pad = (self.base_alignment_bytes - remainder) if remainder != 0 else 0
        self.struct_stride_bytes = cursor + tail_pad
        self.explicit_padding_bytes = padding_total + tail_pad

        descriptor_parts.append(f"stride={self.struct_stride_bytes}")
        self.layout_signature_sha256 = hashlib.sha256(
            "|".join(descriptor_parts).encode("utf-8")
        ).hexdigest()

    @staticmethod
    def _normalize_float(val: float, fmt: str) -> bytes:
        """Collapses non-canonical NaNs and signed zero into deterministic IEEE-754 bit patterns."""
        if math.isnan(val):
            if fmt == "f":
                return struct.pack(">I", 0x7FC00000)
            return struct.pack(">Q", 0x7FF8000000000000)
        if val == 0.0:
            val = 0.0
        return struct.pack(f">{fmt}", val)

    def pack_record(
        self, values: Dict[str, Union[int, float]]
    ) -> Tuple[bytes, BinaryStateWarrant]:
        buf = bytearray(self.struct_stride_bytes)

        for f in self.fields:
            if f.name not in values:
                raise BinaryStateSerializationError(f"Missing required binary field '{f.name}'.")
            raw_val = values[f.name]
            byte_width, _ = _ALLOWED_SCALAR_FORMATS[f.fmt]

            if f.fmt in ("f", "d"):
                be_bytes = self._normalize_float(float(raw_val), f.fmt)
                packed = be_bytes[::-1] if self.byte_order == "little" else be_bytes
            else:
                if not isinstance(raw_val, int) or isinstance(raw_val, bool):
                    raise BinaryStateSerializationError(
                        f"Field '{f.name}' requires exact integer, got {type(raw_val).__name__}."
                    )
                try:
                    packed = struct.pack(f"{self.endian_prefix}{f.fmt}", raw_val)
                except struct.error as exc:
                    raise BinaryStateSerializationError(
                        f"Field '{f.name}' value {raw_val} overflows format '{f.fmt}': {exc}"
                    ) from exc

            buf[f.offset : f.offset + byte_width] = packed

        wire_bytes = self.magic + bytes(buf)
        payload_hash = hashlib.sha256(wire_bytes).hexdigest()

        warrant = BinaryStateWarrant(
            schema_id=self.schema_id,
            byte_order=self.byte_order,
            magic_header_hex=self.magic.hex(),
            struct_stride_bytes=self.struct_stride_bytes,
            base_alignment_bytes=self.base_alignment_bytes,
            explicit_padding_bytes=self.explicit_padding_bytes,
            canonical_nan_enforced=True,
            layout_signature_sha256=self.layout_signature_sha256,
            payload_sha256=payload_hash,
        )
        return wire_bytes, warrant

    def unpack_and_verify(
        self, wire_bytes: bytes, expected_warrant: BinaryStateWarrant
    ) -> Dict[str, Union[int, float]]:
        if expected_warrant.layout_signature_sha256 != self.layout_signature_sha256:
            raise BinaryStateSerializationError(
                "Schema layout signature mismatch: producer and consumer alignment/field specs diverged."
            )
        if len(wire_bytes) != 4 + self.struct_stride_bytes:
            raise BinaryStateSerializationError(
                f"Wire buffer length ({len(wire_bytes)}) does not match magic + aligned stride ({4 + self.struct_stride_bytes})."
            )
        if wire_bytes[:4] != self.magic:
            raise BinaryStateSerializationError(
                f"Endianness magic mismatch: expected {self.magic.hex()}, got {wire_bytes[:4].hex()}."
            )
        actual_hash = hashlib.sha256(wire_bytes).hexdigest()
        if actual_hash != expected_warrant.payload_sha256:
            raise BinaryStateSerializationError("Binary payload SHA-256 digest mismatch.")

        body = wire_bytes[4:]
        occupied = bytearray(self.struct_stride_bytes)
        result: Dict[str, Union[int, float]] = {}

        for f in self.fields:
            byte_width, _ = _ALLOWED_SCALAR_FORMATS[f.fmt]
            occupied[f.offset : f.offset + byte_width] = b"\x01" * byte_width
            chunk = body[f.offset : f.offset + byte_width]
            result[f.name] = struct.unpack(f"{self.endian_prefix}{f.fmt}", chunk)[0]

        for idx, is_used in enumerate(occupied):
            if not is_used and body[idx] != 0:
                raise BinaryStateSerializationError(
                    f"Non-zero padding byte (0x{body[idx]:02x}) detected at offset {idx}; uninitialized memory leak rejected."
                )

        return result


if __name__ == "__main__":
    serializer = DeterministicBinaryStateSerializer(
        schema_id="tensor.ring_checkpoint.v1",
        byte_order="little",
        base_alignment_bytes=16,
        fields=[
            BinaryFieldSpec(name="version_tag", fmt="B", offset=0, alignment=1),
            BinaryFieldSpec(name="flags", fmt="H", offset=2, alignment=2),
            BinaryFieldSpec(name="sequence_id", fmt="Q", offset=8, alignment=8),
            BinaryFieldSpec(name="loss_metric", fmt="d", offset=16, alignment=8),
            BinaryFieldSpec(name="relative_data_offset", fmt="I", offset=24, alignment=4),
        ],
    )

    wire, warrant = serializer.pack_record(
        {
            "version_tag": 1,
            "flags": 0x00A5,
            "sequence_id": 9876543210,
            "loss_metric": -0.0,
            "relative_data_offset": 4096,
        }
    )
    hydrated = serializer.unpack_and_verify(wire, warrant)
    assert hydrated["sequence_id"] == 9876543210
    assert math.copysign(1.0, hydrated["loss_metric"]) == 1.0
    assert warrant.struct_stride_bytes == 32
    assert warrant.explicit_padding_bytes == 9
4. Verification Matrix: Acceptable vs. Rejected Binary State Projections
Invariant Dimension	Rejected (Non-Binding / Unsafe)	Required (Deterministic & Binding)
Byte Order (Endianness)	Host-native sys.byteorder, struct @ or = prefixes	Explicit < (LE) or > (BE) + 4-byte magic (CFLE / CFBE)
Field Alignment & Stride	Compiler-dependent #pragma pack or implicit C-ABI gaps	Explicit power-of-two field offsets & base-alignment stride modulus
Struct Padding Bytes	Uninitialized stack/heap residue inside alignment gaps	Strict 0x00 zero-fill verified byte-by-byte on hydration
Floating-Point Bit Patterns	Hardware-specific signaling/quiet NaN payloads and -0.0 drift	Canonical quiet NaN (0x7FF8000000000000) and +0.0 normalization
Internal Addressing	Raw virtual memory pointers (void*, uintptr_t)	Unsigned relative byte offsets (uint32 / uint64) within buffer bounds