mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4060
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/2475
Author: @PiMatyja
Created: 10/14/2026
Status: ❌ Closed
Base:
main← Head:metal-gguf-hardening📝 Commits (1)
ef5595bMetal: the GGUF reader refuses malformed headers with a message instead of crashing or exiting (#1919)📊 Changes
4 files changed (+200 additions, -8 deletions)
View changed files
📝
metal/src/gguf_lite.cpp(+32 -8)📝
metal/src/ref_quant.cpp(+9 -0)📝
metal/src/ref_quant.h(+1 -0)➕
metal/tests/gguf_lite_test.cpp(+158 -0)📄 Description
Title
Issue: Refs #1919 (malformed GGUF headers). #2288 does the same for setup's Python reader (tools/gguf_reader.py); this PR is the Metal engine's C++ reader only.
Summary
metal/src/gguf_lite.cppreads the header of every GGUF the Metal engine opens (the model shard and the MTP file,metal/src/engine/window.cpp). On a malformed or crafted header it could read outside the mapping, abort, exit the process (ref::block_bytescallsstd::exit(2)on an unknown tensor type), allocate without bound, or accept tensors that lie outside the file, which the engine then reads. Now every such header is refused with a message fromFile::open, and every valid file loads as before: same tensors, types, shapes, offsets, sizes and metadata.What changed
metal/src/gguf_lite.cppReader::get/Reader::str: the bounds checks compare lengths, not pointers (p + ncould wrap). After the first failed read every read returns 0, so no garbage count is used after a short read.fstatis checked; an empty file is refused.gguf.cpprefuses them too). A numeric array whose length runs past the end is refused before the read pointer moves.n_dimsabove 4 (GGML_MAX_DIMS) is refused before the shape is read.general.alignmentmust be a power of two in the uint32 range (the spec's type; llama.cpp also requires a power of two). No other upper bound.block_elems/block_bytesare called; offset + size must lie inside the file (the same rule as llama.cpp's loader, "data is not within the file bounds").metal/src/ref_quant.h/.cpp:ref::known_type(t), true for exactly the typesblock_bytesknows.block_elemsfalls back to 256 for any type, so it cannot be used for this.metal/tests/gguf_lite_test.cpp(new, picked up by thetests/*.cppglob, no GPU): builds crafted files byte by byte and checks each is refused with the expected message, plus valid tiny files that must load with the right offsets and sizes.Left alone: GGUF v2 is still refused; duplicate tensor names (the last wins, as before); tensor offsets that are not a multiple of the alignment (not a bounds problem); rows that are not whole blocks (the size rounds down, as before); the engine's own use of a tensor's shape (
window.cpp,stream.cpp); the CUDA / HIP / SYCL reader (include/strata/artifact/gguf_reader.hpp). Onlymetal/files change.Extra Notes
How it was checked
macOS on Apple Silicon (arm64), Apple clang,
cmake -S metal -B build -DCMAKE_BUILD_TYPE=Release && make -C build gguf_lite_test(metal-cpp not needed for it). Base:296b59d(v0.1.44.1).The same test also passes built with
-fsanitize=address,undefined.-Wall -Wextra: no warnings in the changed files.Before and after
The same cases against the unchanged
gguf_lite.cpp, each in its own child process so that a crash or exit does not stop the run (a child asking for more than 1 GB was stopped):std::length_error:p + nwraps, the check passes)general.alignment0general.alignment48, 2^31+1, 2^32 (u64), -64 (i32)ref: unknown type)Valid files load the same
known_typeaccepts (F32, F16, BF16, Q4_0, Q5_0, Q5_1, Q8_0, Q3_K, Q4_K, Q5_K, Q6_K, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ3_S, IQ4_NL, IQ4_XS, each as 1-D, 2-D and 3-D, plus one more Q4_0), with string, int and float arrays in the metadata, at the default alignment and at 16384 (the alignmentmetal/tools/metal_repack.pywrites), and a copy of the second with that extra tensor's type set to Q2_0 (42). Before and after print the same table for all three (name, type, shape, offset, bytes, every number and string key). For the first two the tensor table also matches gguf-py's ownGGUFReader(data offset and byte count).models/ggml-vocab-*.gguf: no tensors, large token string arrays): the same metadata before and after. The one v2 file (aquila) is refused before and after.Not checked: a full model shard and the MTP file on a Mac. The reader only reads headers, so the gguf-py files above cover the same code; a load of a real pack with this change is the remaining check.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.