fix(gguf): reject tensor offsets not aligned to general.alignment (#120)

Merged
RM
3h ago
+160-6

User description

What changed

finalize_tensor_offsets (src/gguf/layout.rs) now returns the existing InvalidLayout error when a tensor's relative_offset is not a multiple of general.alignment (default 32). The message names the tensor, its offset and the alignment, e.g. tensor 'x' has relative_offset 16, which is not a multiple of general.alignment 32. This matches the GGUF spec and llama.cpp, which engram-parser previously accepted. It is spec strictness, not memory safety.

Check ordering: it runs after the checked_add absolute-offset overflow guard (so u64::MAX still reports overflow) and before the past-EOF and overlap checks.

Tests (tests/gguf_directory_invariants.rs)

  • Misaligned offset (16, and 33) at the default alignment 32 is rejected.
  • Offset 32 with a declared general.alignment of 64 is rejected (aligned at 32, not 64).
  • Offset 192 parses at alignment 64 and is rejected at alignment 128.
  • Offset 16 parses at declared alignment 16.
  • Message assertions cover tensor name, offset, and alignment.
  • The 4 new rejection/acceptance tests that exercise the check fail without the fix (verified by stashing the source change).

One existing test changed: overlapping_payloads_fail_closed used relative offset 4, which is now rejected as misaligned before overlap is reached. It now uses aligned offsets (a 64-byte tensor at 0 and an 8-byte tensor at 32) so it still tests overlap. No checked-in fixture under tests/fixtures was affected.

Verification

  • cargo test --quiet: 139 passed, 0 failed, 2 ignored
  • cargo test --all-features --quiet: 245 passed, 0 failed, 3 ignored
  • cargo clippy --all-targets --all-features -- -D warnings: clean
  • cargo fmt --check: clean

CHANGELOG: added an [Unreleased] section (none existed).

Closes #120
Linear: RM-2204


Generated by Claude Code


Summary by cubic

Rejects GGUF tensor offsets that aren't a multiple of general.alignment (default 32), matching the GGUF spec and llama.cpp. Files that previously parsed now fail with an InvalidLayout naming the tensor, offset, and alignment.

Bug Fixes

  • The check runs after the overflow guard and before the past-EOF and overlap checks.
  • Added tests for misaligned offsets, declared alignments above and below the default, and the error message contents.
  • overlapping_payloads_fail_closed uses aligned offsets so it still exercises overlap.

CodeAnt-AI Description

Reject GGUF files with misaligned tensor offsets

What Changed

  • GGUF parsing now rejects tensors whose offset is not a multiple of the file’s declared alignment, defaulting to 32.
  • The error identifies the tensor, its offset, and the required alignment.

Impact

✅ Misaligned GGUF files rejected during parsing
✅ Declared tensor alignment enforced
✅ Clearer errors identify the offending tensor and offset

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Summary by CodeRabbit

  • Bug Fixes
    • GGUF files with tensor offsets that do not meet the required alignment are now rejected during parsing, with an error identifying the tensor, offset, and required alignment.
    • Offset validation respects the file’s declared alignment, or the default alignment of 32 bytes. Aligned offsets remain valid, while misaligned offsets are rejected even when they fall within the file’s bounds.