util: one bounded read for the files a container supplies

tokenizer.json, gen_config.json, an adapter's adapter_config.json and
the container's generation.* whitelist are all container-supplied, and
all read through the unbounded parser: a hostile file was refused only
by running out of stack -- the tree it grows is walked, dumped and
destroyed recursively on the way.

Json::parse_container is the wire's bounded read sized for files
instead: 2^24 values, because a tokenizer's vocabulary legitimately
carries a million flat strings, and 256 levels, because nothing
legitimate nests deeper. A file past either bound comes back discarded,
like a syntax error, and the four readers map that onto the failure
paths they already had -- ignore with a warning, refuse the adapter,
refuse the vocab. The bound fires during the read; no hostile tree
exists to be walked.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:09 +00:00
1 parent e46a8ba3d4
commit f2f94fc6e7
7 files changed
+131 -35

No files matched your search

+11
View File
@@ -35,6 +35,17 @@ uint64_t log_last_line_ns();
* Multi-byte UTF-8 is left alone; only control bytes are replaced. */
std::string log_sanitize(const std::string& s);
/* The whole text of a file, refused BEFORE it is read when it is bigger than `cap`. Files a
* container supplies are screened by size as well as by JSON budget: the bounded read counts
* values, not bytes, and the slurp used to be the unbounded step -- a multi-gigabyte
* tokenizer.json grew the string before anything said no. RAD_E_IO when unreadable,
* RAD_E_FORMAT when past the cap; nothing is read then. */
int read_text_file(const std::string& path, std::string& out, size_t cap);
/* What one container-supplied JSON file may be: past the largest real tokenizer.json by wide
* margin, far below what turns a load into an OOM. Weight files are not read this way. */
inline constexpr size_t kMaxContainerFile = 1u << 28; /* 256 MiB */
/* Rank tags on log lines. Per-rank components (pools, planner, mover, sampler) print the same
* line once per rank, so under TP each line needs its rank. LogRank sets the tag as a scope
* because the printers are several layers down, where threading a rank argument through every