Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
ple: check the table's size arithmetic before using it (#865)
Niko1221e79b8fc14382
diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cppindex 8a705639f..0e404d973 100644--- a/src/kernels/ngram.cpp+++ b/src/kernels/ngram.cpp@@ -11,6 +11,7 @@#include <cmath>#include <cstdio>#include <cstring>+#include <limits>#if !defined(_WIN32)#include <sys/mman.h>#endif@@ -252,7 +253,6 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO// 110 B rows go through the exact same generic path (ple_reader_test --selftest covers both 90 and 110 B// rows: straddling, caching, in-flight tickets, keep-alive). This refusal was stale.impl_->n_rows = t->shape[1];- impl_->data = impl_->file->tensor_data(*t);// THE CHECK THAT MAKES THE OFFSET FALSIFIABLE. The manifest's `shard2_tensor.offset` is 0, but that is// the offset within the GGUF's DATA SECTION: the file's first 192 bytes are a header, and reading at 0@@ -262,10 +262,18 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO// assumed. A wrong data offset would leave a different remainder.// A shard may hold other tensors too (Swift 1.5's shard 1 holds layers 0-12 and the table): the table must// then fit inside the file at its own offset; alone in its shard (the original's shard 2) it fills it exactly.+ // The size arithmetic is checked before it is used (#865): a header that claims enough rows to wrap the 64-bit+ // product, or a data section that starts past the end of the file, must be refused, not compared after wrapping.+ if (impl_->n_rows > std::numeric_limits<uint64_t>::max() / impl_->rb ||+ impl_->file->data_start() > impl_->file->file_size()) {+ err = "PLE table size overflow or invalid data offset in " + gguf_path;+ close();+ return false;+ }const uint64_t need = impl_->n_rows * (uint64_t) impl_->rb;const uint64_t have = impl_->file->file_size() - impl_->file->data_start();const bool alone = impl_->file->tensors().size() == 1;- if (alone ? need != have : t->offset + need > have) {+ if (t->offset > have || need > have - t->offset || (alone && (t->offset != 0 || need != have))) {char buf[256];std::snprintf(buf, sizeof buf,"PLE table size mismatch: %llu rows x %d B = %llu at offset %llu, but the file holds %llu from "@@ -277,6 +285,7 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoOclose();return false;}+ impl_->data = impl_->file->tensor_data(*t); // only a table that fits the file is mappedif (io.mode == PleIo::Direct) {// The parse above is the validated source of the offset; the mapping itself is not kept, so no page of// the table can enter this process's working set or the file cache through it.@@ -358,6 +367,8 @@ void PleTable::close() {impl_->n_rows = 0;impl_->rb = PLE_ROW_BYTES;impl_->fmt = &ple_format_info(PleFormat::IQ4_NL);+ impl_->scale = 1.0f;+ impl_->bytes_read = 0;}bool PleTable::is_open() const { return impl_->data != nullptr || impl_->reader.is_open(); }diff --git a/src/ngram/ple_reader_test.cpp b/src/ngram/ple_reader_test.cppindex 6baaf1eeb..28baeea02 100644--- a/src/ngram/ple_reader_test.cpp+++ b/src/ngram/ple_reader_test.cpp@@ -87,7 +87,8 @@ uint32_t gguf_type_id(const char* name) {return 0xFFFFFFFFu;}-std::string write_format_table(const std::string& dir, const k::PleFormatInfo& f, uint32_t rows) {+// `claimed_rows` is the row count the header states; `rows` rows are actually written (equal unless a test lies)+std::string write_format_table(const std::string& dir, const k::PleFormatInfo& f, uint32_t rows, uint64_t claimed_rows = 0) {const std::string path = dir + "/ple_format_selftest.gguf";std::ofstream out(path, std::ios::binary);if (!out) { std::fprintf(stderr, "cannot write %s\n", path.c_str()); return {}; }@@ -108,7 +109,7 @@ std::string write_format_table(const std::string& dir, const k::PleFormatInfo& fhead += str("strata.ple.scale") + le(6u, 4) + le(sb, 4); // 6 = FLOAT32}head += str("per_layer_token_embd.weight") + le(2u, 4);- head += le((uint64_t) k::PLE_HEAD_DIM, 8) + le((uint64_t) rows, 8);+ head += le((uint64_t) k::PLE_HEAD_DIM, 8) + le(claimed_rows != 0 ? claimed_rows : (uint64_t) rows, 8);head += le(gguf_type_id(f.name), 4) + le(0ull, 8);while (head.size() % 32) head += '\0';out.write(head.data(), (std::streamsize) head.size());@@ -168,7 +169,36 @@ void format_round_trip(const std::string& dir, const k::PleFormatInfo& f, uint32std::filesystem::remove(path);}+/// A table whose header does not match the file is refused with a message, never read past its end (#865): one row+/// short, one row long (a single-tensor shard must be filled exactly), and a row count that wraps the 64-bit size.+void bad_sizes_are_refused(const std::string& dir) {+ for (int i = 0; i < k::ple_format_count(); ++i) {+ const k::PleFormatInfo& f = k::ple_formats()[i];+ struct { const char* what; uint32_t rows; uint64_t claimed; } cases[] = {+ {"claims one row more than the file holds", 500, 501},+ {"claims one row less than the file holds", 500, 499},+ {"claims a row count that wraps 2^64", 8, (uint64_t) 0x4000000000000000ull / 3 * 2},+ };+ for (const auto& c : cases) {+ const std::string path = write_format_table(dir, f, c.rows, c.claimed);+ if (path.empty()) { CHECK(false, "%s: no table was written", f.name); continue; }+ for (k::PleIo mode : {k::PleIo::Direct, k::PleIo::Mmap}) {+ k::PleTable t;+ std::string err;+ k::PleIoOptions io;+ io.mode = mode;+ io.cache_rows = 0;+ CHECK(!t.open(path, err, io), "%s: a table that %s was accepted", f.name, c.what);+ CHECK(!err.empty(), "%s: refused without a message (%s)", f.name, c.what);+ CHECK(!t.is_open(), "%s: left open after refusing (%s)", f.name, c.what);+ }+ std::filesystem::remove(path);+ }+ }+}+void all_formats_round_trip(const std::string& dir) {+ bad_sizes_are_refused(dir);CHECK(k::ple_format_count() > 0, "no formats");for (int i = 0; i < k::ple_format_count(); ++i) {const k::PleFormatInfo& f = k::ple_formats()[i];