format: bound the geometry the bailing layout products are built from

The layer-count cap made the walk safe; the arithmetic was still open.
The rebuild multiplies container-supplied widths in int64 -- heads *
(nope + rope) rows for the q_b split, heads * 256 * latent for the
fused kv -- and hands the products to sized allocations and a
comparison. A wrapped q_b product that coincidentally equalled the
tensor's rows would run the split at a wrong per-head boundary (the
bytes stayed the file's own, but the weights went to the wrong names);
on the 32-bit tier, an overflowed element count truncates through
size_t to a small allocation the copy then overruns.

The family gate now caps what these products are built from -- heads
and the MLA rank at 4096, rope at 256 dimensions, the full query width
at 65536, the layer group within the stack; the shipped geometry is
128/64/512 with tens of heads -- and the q_b row product is computed
with overflow checks instead of trusted. checkpoint_test pins the wide
heads and rank against the honest small stack.

Found by the 2.1.0 review pass (axis: hostile model files); widened
past the reported site to the fused-kv product the same axes cleared
only as file-bounded.
This commit is contained in:
fl0l0u committed 2026-11-15 22:02:27 +00:00
1 parent 3bf9dc9593
commit 5e2e596644
2 files changed
+36 -9

No files matched your search

+23 -6
View File
@@ -452,8 +452,14 @@ int meta_from_gguf(GgufFile& g, MetaOwn* out) {
* config.json names, so the reader re-spells them here and one plugin serves both
* formats. eps and rope_theta take the generic paths above. Four of the family's
* design choices the GGUF does not carry are Ling 3.0's, stated as constants. */
if (gi("kda.head_dim", 0) <= 0 || gi("rope.dimension_count", 0) <= 0 ||
gi("attention.key_length_mla", 0) <= gi("rope.dimension_count", 0))
/* Bounds on the family gates too (see bailingmoe3_layouts): the layout rebuild's
* products -- heads * (nope + rope) rows, the fused kv's heads * 256 * latent -- are
* int64 products of these values handed to sized allocations, so the widths they are
* built from may not roam free. 128/64 is the shipped geometry. */
if (gi("kda.head_dim", 0) <= 0 || gi("kda.head_dim", 0) > 512 ||
gi("rope.dimension_count", 0) <= 0 || gi("rope.dimension_count", 0) > 256 ||
gi("attention.key_length_mla", 0) <= gi("rope.dimension_count", 0) ||
gi("attention.key_length_mla", 0) > 65536)
return RAD_E_FORMAT;
out->kv("num_attention_heads", fmt("%lld", (long long)m.n_head));
/* The typed head_dim the generic pass fell back to (the MLA query width) is not the
@@ -3627,9 +3633,15 @@ int Checkpoint::bailingmoe3_layouts() {
* 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. */
* container that means nothing by it. The head and rank caps are the same discipline one
* order down: the rebuild's products (heads * (nope + rope) rows, heads * 256 * latent
* bytes of fused kv) are computed in int64 and handed to vectors sized through size_t --
* on the 32-bit tier an overflowed product truncates to a small allocation the copy then
* overruns. No family model is near 4096 heads or ranks; 256 rope dimensions is already
* far past what the rope kernel addresses. */
if (n_layer < 1 || n_layer > 4096 || dense < 0 || dense > n_layer ||
group < 2 || heads < 1 || latent < 1 || ff < 1 || emb < 1)
group < 2 || group > n_layer || heads < 1 || heads > 4096 ||
latent < 1 || latent > 4096 || 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. */
@@ -3700,9 +3712,14 @@ int Checkpoint::bailingmoe3_layouts() {
const char* nps = meta_.get("qk_nope_head_dim"), *rps = meta_.get("qk_rope_head_dim");
const int64_t qn = nps ? std::stoll(nps) : 0, qr = rps ? std::stoll(rps) : 0;
const SrcTensor* qb = find(fmt("blk.%lld.attn_q_b.weight", (long long)l));
/* The row product is checked, not trusted: heads and the two rope widths arrive from
* the container, and a wrapped product that happened to equal the tensor's rows would
* run the split at the wrong per-head boundary. */
int64_t w = 0, qb_rows = 0;
if (qb && qb->shape.size() == 2 && qb->shape[1] > 0 && qn > 0 && qr > 0 &&
qb->shape[0] == heads * (qn + qr)) {
const int64_t ql = qb->shape[1], w = qn + qr;
!__builtin_add_overflow(qn, qr, &w) && !__builtin_mul_overflow(heads, w, &qb_rows) &&
qb->shape[0] == qb_rows) {
const int64_t ql = qb->shape[1];
std::vector<float> q3, nblk, rblk;
RAD_TRY(whole(qb, q3));
nblk.assign((size_t)(heads * qn) * ql, 0.0f);
+13 -3
View File
@@ -3587,19 +3587,19 @@ TEST(a_bailing_gguf_must_promise_a_walkable_layer_count) {
* 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) {
auto bailing_kvs = [](int64_t layers, int64_t dense, int64_t heads = 4, int64_t latent = 512) {
return std::vector<GKv>{
{ "general.architecture", 8, 0, { "bailingmoe3" } },
{ "bailingmoe3.block_count", 10, (double)layers, {} },
{ "bailingmoe3.embedding_length", 4, 64, {} },
{ "bailingmoe3.attention.head_count", 4, 4, {} },
{ "bailingmoe3.attention.head_count", 10, (double)heads, {} },
{ "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.attention.kv_lora_rank", 10, (double)latent, {} },
{ "bailingmoe3.expert_feed_forward_length", 4, 256, {} },
{ "bailingmoe3.leading_dense_block_count", 10, (double)dense, {} },
};
@@ -3618,6 +3618,16 @@ TEST(a_bailing_gguf_must_promise_a_walkable_layer_count) {
write_gguf(sane, bailing_kvs(4, 1), {});
Checkpoint ck3;
REQUIRE_EQ(ck3.open(sane), RAD_OK);
/* The widths the layout products are built from are bounded too: past the caps they are
* vector sizes and wrapped row products, not geometry. */
const std::string wide = dir + "/wide.gguf";
write_gguf(wide, bailing_kvs(4, 1, 4611686018427387904LL), {});
Checkpoint ck4;
REQUIRE_EQ(ck4.open(wide), RAD_E_FORMAT);
const std::string deep_rank = dir + "/deep_rank.gguf";
write_gguf(deep_rank, bailing_kvs(4, 1, 4, 4611686018427387904LL), {});
Checkpoint ck5;
REQUIRE_EQ(ck5.open(deep_rank), RAD_E_FORMAT);
}
RAD_TEST_MAIN()