[PR #2288] [CLOSED] [Other] bound GGUF header reads and report malformed metadata #3964

Closed
opened 2026-11-07 15:32:03 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2288
Author: @KeyanHu-git
Created: 10/13/2026
Status: ❌ Closed

Base: main ← Head: fix/1919-gguf-header-bounds


📝 Commits (5)

  • 762585f tools: validate GGUF headers and tensor extents
  • 2309e11 tools: derive GGUF extent from header and tensor payloads
  • 3732b4e merge current main for GGUF directory validation
  • e90ce85 Limit GGUF fix to bounded header parsing and preserve tensor compatibility
  • d4222d6 Preserve valid GGUF alignment and verify parser callers

📊 Changes

2 files changed (+235 additions, -27 deletions)

View changed files

📝 tools/gguf_reader.py (+59 -27)
➕ tools/test_gguf_reader_malformed.py (+176 -0)

📄 Description

Problem and result

A GGUF header with an unknown metadata type or an impossible length can raise KeyError or OverflowError inside the reader. Setup expects a parsing error, so these files produce a traceback instead of its existing diagnostic naming the broken shard.

The reader now checks declared reads and counts against the file and reports parsing failures as ValueError. Setup can reject the shard through its normal error path, leaving the file intact.

The malformed-header cases in #1919. The unknown tensor-size policy established by #92 is preserved. The metadata-only shard case raised in this PR's review is covered by the current upstream setup check and included in validation.

Approach

Strings, scalars, arrays and tensor dimensions share a bounded read operation. Counts are checked before constructing a read or iterating over entries; metadata type IDs are checked before decoding their values. The reader adds the filename once when reporting the error.

Alignment follows the GGUF specification: a positive multiple of 8. Both 24-byte and 64-byte alignment are covered. This corrects the draft's overly strict power-of-two requirement.

The diff is confined to the Python header reader and its regression tests. Tensor geometry support, setup's treatment of readable unknown tensor types, and tensor-payload validation retain their existing behavior.

Validation

Base: 011c9e60 (0.2.2 / engine 0.1.47). Final revision: d4222d69. Python 3.12 on macOS and Windows.

python -m unittest tools.test_gguf_reader_malformed tools.test_shards \
  tools.test_setup_shard_check tools.test_conversation_cache_isolation -v

Passed: all 34 tests on both systems. The checks cover malformed lengths/types/counts, truncated headers, setup's actual whole_shard() and check_shards() error paths, every supported scalar type and its array form, strings and empty arrays, metadata-only files without alignment padding, explicit alignment, and readable unknown tensor types. Existing shard, pack and generated control-vector fixtures also pass. git diff --check is clean.

A read-only comparison of the two installed Coder IQ1_M shards also produced identical metadata, tensor names/shapes/types/offsets, alignment and data start with the original and final readers:

Shard File size Metadata keys Tensors Parsed result
00001 29,608,446,496 bytes 67 1,223 Identical
00002 28,800,138,432 bytes 3 1 Identical

Only headers were read; the model files were unchanged.

Failed: the same tests on unmodified main report 11 errors and 3 failed alignment subtests on both systems. These are the negative controls; the final patch passes them. A check of 24-byte alignment also exposed the earlier draft's compatibility regression, which is corrected in this revision.

Not run: GPU model execution; this change only parses Python-side headers. The malformed files are synthetic fixtures, including the two reproduced in #1919.

Limits

This reader still does not implement nested metadata arrays; it now reports that limitation as a parsing error. It also cannot calculate payload sizes for every future tensor type. This PR keeps those existing support boundaries and does not add a new model-admission policy.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2288 **Author:** [@KeyanHu-git](https://github.com/KeyanHu-git) **Created:** 10/13/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/1919-gguf-header-bounds` --- ### 📝 Commits (5) - [`762585f`](https://github.com/Niko1221/Strata/commit/762585f77cf9be462efc3e76fd3b2208324d1309) tools: validate GGUF headers and tensor extents - [`2309e11`](https://github.com/Niko1221/Strata/commit/2309e112d50853306cc4c8508e7cdbf88f5cb337) tools: derive GGUF extent from header and tensor payloads - [`3732b4e`](https://github.com/Niko1221/Strata/commit/3732b4e4496f4ebe09030c42c14c9f77ef9a9c2c) merge current main for GGUF directory validation - [`e90ce85`](https://github.com/Niko1221/Strata/commit/e90ce85a0c25404da54f4df4db5605b3f69e2268) Limit GGUF fix to bounded header parsing and preserve tensor compatibility - [`d4222d6`](https://github.com/Niko1221/Strata/commit/d4222d69b5dcca194cecf2a420301e3f0bb445c9) Preserve valid GGUF alignment and verify parser callers ### 📊 Changes **2 files changed** (+235 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `tools/gguf_reader.py` (+59 -27) ➕ `tools/test_gguf_reader_malformed.py` (+176 -0) </details> ### 📄 Description ## Problem and result A GGUF header with an unknown metadata type or an impossible length can raise `KeyError` or `OverflowError` inside the reader. Setup expects a parsing error, so these files produce a traceback instead of its existing diagnostic naming the broken shard. The reader now checks declared reads and counts against the file and reports parsing failures as `ValueError`. Setup can reject the shard through its normal error path, leaving the file intact. ## Related discussion The malformed-header cases in #1919. The unknown tensor-size policy established by #92 is preserved. The metadata-only shard case raised in this PR's review is covered by the current upstream setup check and included in validation. ## Approach Strings, scalars, arrays and tensor dimensions share a bounded read operation. Counts are checked before constructing a read or iterating over entries; metadata type IDs are checked before decoding their values. The reader adds the filename once when reporting the error. Alignment follows the [GGUF specification](https://github.com/ggml-org/ggml/blob/master/docs/gguf.md#required): a positive multiple of 8. Both 24-byte and 64-byte alignment are covered. This corrects the draft's overly strict power-of-two requirement. The diff is confined to the Python header reader and its regression tests. Tensor geometry support, setup's treatment of readable unknown tensor types, and tensor-payload validation retain their existing behavior. ## Validation Base: `011c9e60` (0.2.2 / engine 0.1.47). Final revision: `d4222d69`. Python 3.12 on macOS and Windows. ```sh python -m unittest tools.test_gguf_reader_malformed tools.test_shards \ tools.test_setup_shard_check tools.test_conversation_cache_isolation -v ``` **Passed:** all 34 tests on both systems. The checks cover malformed lengths/types/counts, truncated headers, setup's actual `whole_shard()` and `check_shards()` error paths, every supported scalar type and its array form, strings and empty arrays, metadata-only files without alignment padding, explicit alignment, and readable unknown tensor types. Existing shard, pack and generated control-vector fixtures also pass. `git diff --check` is clean. A read-only comparison of the two installed Coder IQ1_M shards also produced identical metadata, tensor names/shapes/types/offsets, alignment and data start with the original and final readers: | Shard | File size | Metadata keys | Tensors | Parsed result | |---|---:|---:|---:|---| | 00001 | 29,608,446,496 bytes | 67 | 1,223 | Identical | | 00002 | 28,800,138,432 bytes | 3 | 1 | Identical | Only headers were read; the model files were unchanged. **Failed:** the same tests on unmodified main report 11 errors and 3 failed alignment subtests on both systems. These are the negative controls; the final patch passes them. A check of 24-byte alignment also exposed the earlier draft's compatibility regression, which is corrected in this revision. **Not run:** GPU model execution; this change only parses Python-side headers. The malformed files are synthetic fixtures, including the two reproduced in #1919. ## Limits This reader still does not implement nested metadata arrays; it now reports that limitation as a parsing error. It also cannot calculate payload sizes for every future tensor type. This PR keeps those existing support boundaries and does not add a new model-admission policy. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:32:03 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3964
No description provided.