diff --git a/core/format/checkpoint.cpp b/core/format/checkpoint.cpp index f90df914..0a22d5b1 100644 --- a/core/format/checkpoint.cpp +++ b/core/format/checkpoint.cpp @@ -3622,7 +3622,14 @@ int Checkpoint::bailingmoe3_layouts() { const int64_t emb = meta_.m.n_embd; const char* nexp_s = meta_.get("num_experts"); const int64_t n_expert = nexp_s ? std::stoll(nexp_s) : 0; - if (n_layer < 1 || group < 2 || heads < 1 || latent < 1 || ff < 1 || emb < 1) + /* Every loop below walks to n_layer on the open path, so the counts must be bounded HERE: + * a GGUF claiming 2^62 layers would spend centuries in name probes, never answering, never + * refusing -- a hostile container's hang, not a slow load. No real model of this family is + * near 4096 layers. And the MoE suffix loop starts at the dense prefix, so the prefix must + * lie inside the stack: negative opens a walk of twice the range, beyond the end is a + * container that means nothing by it. */ + if (n_layer < 1 || n_layer > 4096 || dense < 0 || dense > n_layer || + group < 2 || heads < 1 || latent < 1 || ff < 1 || emb < 1) return RAD_E_FORMAT; /* Decode a whole source tensor to f32: a block type row by row, a plain one a copy. */ diff --git a/tests/checkpoint_test.cpp b/tests/checkpoint_test.cpp index 9057cfdc..2b6d3be7 100644 --- a/tests/checkpoint_test.cpp +++ b/tests/checkpoint_test.cpp @@ -3581,4 +3581,43 @@ TEST(single_files_merge_by_name_and_read_their_metadata_elsewhere) { CHECK_EQ(ck.tok_dir(), meta); } +TEST(a_bailing_gguf_must_promise_a_walkable_layer_count) { + /* bailingmoe3_layouts rebuilds the release layout by walking every layer the container + * claims, on the open path and before any plugin runs. A GGUF naming 2^62 layers is + * therefore not a slow load but a permanent hang -- and a negative dense prefix opens a + * second loop of the same order. Both must refuse at open, quickly, RAD_E_FORMAT. */ + const std::string dir = fresh_dir(); + auto bailing_kvs = [](int64_t layers, int64_t dense) { + return std::vector{ + { "general.architecture", 8, 0, { "bailingmoe3" } }, + { "bailingmoe3.block_count", 10, (double)layers, {} }, + { "bailingmoe3.embedding_length", 4, 64, {} }, + { "bailingmoe3.attention.head_count", 4, 4, {} }, + { "bailingmoe3.feed_forward_length", 4, 128, {} }, + { "bailingmoe3.context_length", 4, 4096, {} }, + { "bailingmoe3.kda.head_dim", 4, 128, {} }, + { "bailingmoe3.rope.dimension_count", 4, 64, {} }, + { "bailingmoe3.attention.key_length_mla", 4, 192, {} }, + { "bailingmoe3.attention.value_length_mla", 4, 128, {} }, + { "bailingmoe3.attention.kv_lora_rank", 4, 512, {} }, + { "bailingmoe3.expert_feed_forward_length", 4, 256, {} }, + { "bailingmoe3.leading_dense_block_count", 10, (double)dense, {} }, + }; + }; + const std::string huge = dir + "/huge.gguf"; + write_gguf(huge, bailing_kvs(4611686018427387904LL, 0), {}); + Checkpoint ck; + REQUIRE_EQ(ck.open(huge), RAD_E_FORMAT); + const std::string neg = dir + "/neg.gguf"; + write_gguf(neg, bailing_kvs(4, -4611686018427387904LL), {}); + Checkpoint ck2; + REQUIRE_EQ(ck2.open(neg), RAD_E_FORMAT); + /* The bound must not bite the honest: a small stack opens (no tensors, so the walk finds + * nothing to rebuild, and open returns OK exactly as before). */ + const std::string sane = dir + "/sane.gguf"; + write_gguf(sane, bailing_kvs(4, 1), {}); + Checkpoint ck3; + REQUIRE_EQ(ck3.open(sane), RAD_OK); +} + RAD_TEST_MAIN() diff --git a/tests/gguf_writer.h b/tests/gguf_writer.h index 09dad778..f0923786 100644 --- a/tests/gguf_writer.h +++ b/tests/gguf_writer.h @@ -11,7 +11,7 @@ namespace ggufw { struct GKv { std::string key; - uint32_t type; /* 4 u32, 6 f32, 8 string, 9 array of strings */ + uint32_t type; /* 4 u32, 6 f32, 8 string, 9 array of strings, 10 i64 */ double num = 0; std::vector strs; }; @@ -42,6 +42,7 @@ inline bool write_gguf(const std::string& path, const std::vector& kvs, put_str(&b, kv.key); put_bytes(&b, &kv.type, 4); if (kv.type == 4) { const uint32_t v = (uint32_t)kv.num; put_bytes(&b, &v, 4); } + else if (kv.type == 10) { const int64_t v = (int64_t)kv.num; put_bytes(&b, &v, 8); } else if (kv.type == 6) { const float v = (float)kv.num; put_bytes(&b, &v, 4); } else if (kv.type == 8) put_str(&b, kv.strs.at(0)); else {