Skip to content

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

Merged
rmems merged 1 commit into
mainfrom
fix/rm-2204-gguf-tensor-offset-alignment
Oct 20, 2026
Merged

rmems merged 1 commit into
mainfrom
fix/rm-2204-gguf-tensor-offset-alignment

Conversation

@rmems

@rmems rmems commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

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.

Written for commit d9c5c7f. Summary will update on new commits.

View guided diff Turn on auto-fix


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.

…-2204, #120)

finalize_tensor_offsets checked offset/size overflow, past-EOF and overlap,
but not that each tensor's relative_offset is a multiple of general.alignment,
which the GGUF spec and llama.cpp require. Return InvalidLayout naming the
tensor, its offset and the alignment. The check runs after the offset-overflow
guard and before the bounds/overlap checks.

Tests cover a misaligned offset at the default alignment 32, at a declared
alignment 64, and offsets that are aligned at 64 (or 16) but misaligned at
another alignment. The overlap fixture used unaligned offset 4 and now uses
aligned offsets so it still exercises overlap.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XFfzKmA7iVUdRhsaNkdQLs
@codeant-ai

codeant-ai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🤖 CodeAnt AI — Review Status

Status Commit Started (UTC) Finished (UTC)
✅ Reviewed your PR d9c5c7f Oct 19, 2026 · 07:29 07:30

@linear-code

linear-code Bot commented Oct 19, 2026

Copy link
Copy Markdown

RM-2204

@codeant-ai

codeant-ai Bot commented Oct 19, 2026

Copy link
Copy Markdown

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: rmems/engram-parser/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b56329f7-bf51-4dbb-93cc-ed2b3dd8f854
📥 Commits

Reviewing files that changed from the base of the PR and between dbc46cf and d9c5c7f.

📒 Files selected for processing: 3
  • CHANGELOG.md
  • src/gguf/layout.rs
  • tests/gguf_directory_invariants.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

📝 Walkthrough

finalize_tensor_offsets now rejects tensor relative_offset values that are not divisible by general.alignment. Tests cover default and explicitly declared alignments, and the changelog documents the parsing behavior.

Changes: GGUF offset alignment

Layer / File(s) Summary
Enforce tensor offset alignment
src/gguf/layout.rs, tests/gguf_directory_invariants.rs, CHANGELOG.md
The directory contract documents the per-tensor alignment requirement. finalize_tensor_offsets rejects misaligned offsets after overflow validation and before bounds and overlap checks. Tests cover misaligned offsets, valid declared alignments, and overlapping aligned ranges. The changelog records the validation behavior.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: ⚪ Minimal · up to d9c5c

No actionable merge-blocking risk is established; this change appears ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Linked Issues check Warning Issue #120 requires rejection when relative_offset is not a multiple of general.alignment, with an InvalidLayout message that names the tensor, offset, and alignment. The changed `finalize_tenso… Add a checked-in fixture-based regression test for the misaligned-offset case, and include the relevant llama.cpp handcrafted bad-alignment cases or equivalent fixture coverage.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: rejecting GGUF tensor offsets that are not aligned to general.alignment.
Out of Scope Changes check Passed The changes stay within issue #120. They add alignment validation, targeted error assertions, alignment-specific test helpers, and adjust the overlap test so it still tests overlap after alignment val…
Docstring Coverage Passed Docstring coverage is 90.91% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 2 files. (1 skipped: 1 …
Full details: Out of Scope Changes check

Explanation

The changes stay within issue #120. They add alignment validation, targeted error assertions, alignment-specific test helpers, and adjust the overlap test so it still tests overlap after alignment validation. The changelog entry documents the same behavior. No unrelated production behavior or unrelated test area is introduced.

Full details: Docstring Coverage

Explanation

Docstring coverage is 90.91% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 2 files. (1 skipped: 1 unsupported.)

Full details: Linked Issues check

Explanation

Issue #120 requires rejection when relative_offset is not a multiple of general.alignment, with an InvalidLayout message that names the tensor, offset, and alignment. The changed finalize_tensor_offsets behavior and the added tests implement these requirements. The issue also requests a fixture-based regression test and porting the llama.cpp handcrafted bad-alignment cases. The PR adds generated in-memory GGUF tests in tests/gguf_directory_invariants.rs, but it does not add or update a fixture-based regression test. The requested coding test coverage is therefore incomplete.

Resolution

Add a checked-in fixture-based regression test for the misaligned-offset case, and include the relevant llama.cpp handcrafted bad-alignment cases or equivalent fixture coverage.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codeant-ai codeant-ai Bot added the size:L This PR changes 100-499 lines, ignoring generated files label Oct 19, 2026

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

This PR correctly implements GGUF tensor offset alignment validation per the spec and llama.cpp behavior. The core change in finalize_tensor_offsets is well-placed in the check ordering chain (after the checked_add overflow guard, before bounds/overlap checks), and the logic !tensor.relative_offset.is_multiple_of(alignment) is correct — 0 passes as a valid multiple of any alignment, and the parser enforces alignment > 0 && alignment.is_power_of_two() earlier in read_metadata_section.

The test coverage is thorough: rejection at default alignment (16, 33), rejection when declared alignment overrides default (offset 32 @ alignment 64), acceptance at a correctly aligned offset (192 @ alignment 64, rejected @ 128), and acceptance of general.alignment 16 allowing offset 16. The overlapping_payloads_fail_closed fixture update correctly migrates from the formerly-misaligned offset 4 to aligned offsets, preserving the overlap-specific failure mode.

No blocking defects were identified. The changes are safe to merge.


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

@codeant-ai

codeant-ai Bot commented Oct 19, 2026

Copy link
Copy Markdown

CodeAnt PR Risk: Low Risk

  • The PR appears safe to merge; the offset check is narrowly scoped and reports misaligned tensors as invalid layouts.
  • Tests cover default and custom alignments, valid aligned offsets, and overlap checks with aligned offsets.

Assessed commit: d9c5c7f49b65

@rmems
rmems merged commit 55d0b5f into main Oct 20, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gguf: reject tensor relative_offset that is not a multiple of general.alignment

2 participants