Skip to content

fix(cluster): refuse headers whose byte counts or offsets overflow - #488

Merged
ashhart merged 1 commit into
ashhart:mainfrom
chaog992:cluster-header-overflow
Oct 9, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
chaog992:cluster-header-overflow

Conversation

@chaog992

@chaog992 chaog992 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

What this changes

This is the follow-up left open in #460. parseHeader in zig/src/cluster/checkpoint.zig, the cluster's own safetensors reader, multiplied a tensor's dimensions and dtype size and added the header length to its start offset without overflow checks, so a malformed header panicked in Debug and was illegal behavior in the default ReleaseFast build. It also accepted an end offset whose start + bytes overflows 64 bits, leaving that overflow to whatever adds them later. For example, in ReleaseFast a U8 tensor with shape [4611686018427387904, 4] and data_offsets [0, 0] was accepted as empty, because its byte count wrapped to zero.

An entry whose absolute end (8 + header length + end) would overflow the 64-bit range is now BadHeader, which also keeps every tensor's start and start + bytes within 64 bits. The byte count multiplies the dtype size by each dimension with saturating multiplication: a zero dimension anywhere still gives 0, and an overflow stops at 2^64 - 1, which bytes can no longer reach, so it is Inconsistent like any other mismatch. The core reader from #460 multiplies with std.math.mul instead; here that would refuse BF16 [16777216, 16777216, 32768, 0], which the cluster base accepts, because its element count, 2^63, fits in a u64, but times 2 bytes it reaches 2^64 before the trailing zero. A header the base parser accepts in Debug without integer overflow, with every tensor's absolute end within 64 bits, parses to the same tensors.

Tensors are still not checked against their file's size, and the arithmetic that later uses them (the planner's sums of bytes, the loader's column-split offsets) is unchecked; those would be separate changes.

Receipt

  • Environment: base 041d14a9 (1.0.1), fix 02322c1b, Zig 0.17.0, Linux x86_64 under WSL2.
  • Tests: the cluster module is only in the Mac build graph (test-cluster), so on Linux I ran its tests with zig test -lc -O Debug --dep lanes --dep fabric -Mroot=zig/src/cluster/cluster.zig -Mlanes=zig/src/core/lanes/lanes.zig -Mfabric=zig/src/fabric/fabric.zig, and again with -O ReleaseFast. Both give 81 tests: 73 passed, 6 skipped, 2 failed. The base gives 72 passed, 6 skipped and the same 2 failures, status's "the K3 status on four Studios…" and plan_test's "a node whose wired limit is too low…". The skips need TF_K3_DIR, TF_CLUSTER_PROBES or TF_CLUSTER_REPORT.
  • Failing receipt: the new test on the unfixed code:
    Debug:
    panic: integer overflow
    zig/src/cluster/checkpoint.zig:96:19: … in parseHeader (checkpoint.zig)
                count *= t.shape[i];
    ReleaseFast:
    expected error.Inconsistent, found { .{ .name = { 116 }, .file = 0, .start = 81, .bytes = 0, .dtype = .u8, .rank = 2, .shape = { 4611686018427387904, 4, 0, 0 }, .class = .{ .role = .other, .layer = null, .expert = null } } }
    
    When each of the four overflow cases is run individually against the base, the new test fails: in Debug the dimension-product, dtype-size and start-offset cases panic (lines 96, 99 and 90) and the end-offset case is accepted; in ReleaseFast all four are accepted. Two more cases pin the end bound: an absolute end of exactly 2^64 - 1 parses, and one byte beyond it is refused (on the base it panics in Debug and wraps its start to 0 in ReleaseFast). Reverting either the byte-count calculation or the end-bound check makes its corresponding cases fail, an off-by-one in the bound fails one of the two boundary cases, and a checked multiply fails the two empty-tensor cases, which parse on the base.
  • Valid-header compatibility: five headers from TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit at d9d758fb (763 tensors) parse identically before and after, in Debug and ReleaseFast: name, file, start, bytes, dtype, shape and role.
  • Lean check: python3 tools/zig/lean_check.py finds 513 problems on the base and the same 513 with this change; none are in cluster/checkpoint.zig.
  • Format and cross-compilation: zig fmt --check zig/src/cluster/checkpoint.zig flags only the dtype table at lines 11–12, as it does on the base; this change leaves the table alone. The cluster module compiles for aarch64-macos with -mcpu apple_m1 --test-no-exec; this is a compile check, not a macOS test run.
  • Not run: zig build test-cluster and tf-k3-cluster (Mac build graph), cluster runs and model loads. The change only touches header parsing, not inference arithmetic.

@ashhart
ashhart merged commit 232b335 into ashhart:main Oct 9, 2026
@ashhart

ashhart commented Oct 9, 2026

Copy link
Copy Markdown
Owner

Thank you, @chaog992, for the malformed-header cases and the empty-tensor compatibility checks. The parser now rejects overflowing absolute offsets and byte counts while preserving zero-sized tensors and the last valid 64-bit boundary. Your regression passes, and your original commit landed on main.

@chaog992
chaog992 deleted the cluster-header-overflow branch October 12, 2026 14:34
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants