Archived
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:
1 parent
e46a8ba3d4
commit
f2f94fc6e7
7 files changed
+131
-35
No files matched your search
+29
-12
@@ -93,9 +93,17 @@ std::vector<GenFieldSource> gen_sampling_sources(const GenSampling& stated, cons
|
||||
}
|
||||
|
||||
bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampling* out) {
|
||||
Json j;
|
||||
try { j = Json::parse(json_text); }
|
||||
catch (const std::exception& e) { RAD_WARN("%s: %s; ignoring it", src, e.what()); return false; }
|
||||
/* A model-repo file, and container metadata reaches here too. Read it with the container's
|
||||
* bounds: a file that is refused never becomes a tree -- an unbounded read of a hostile one
|
||||
* could overflow this process's stack on the way to the dump or destructor below. */
|
||||
bool over = false, deep = false;
|
||||
Json j = Json::parse_container(json_text, &over, &deep);
|
||||
if (over || deep) {
|
||||
RAD_WARN("%s: %s; ignoring it", src,
|
||||
over ? "carries far too many JSON values" : "nests far too deep");
|
||||
return false;
|
||||
}
|
||||
if (j.is_discarded()) { RAD_WARN("%s: not valid JSON; ignoring it", src); return false; }
|
||||
if (!j.is_object()) { RAD_WARN("%s: not a JSON object; ignoring it", src); return false; }
|
||||
|
||||
/* `do_sample: false` means greedy, and transformers then ignores every other sampler field,
|
||||
@@ -178,12 +186,16 @@ bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampli
|
||||
}
|
||||
|
||||
bool gen_sampling_from_file(const std::string& path, bool required, GenSampling* out) {
|
||||
std::ifstream f(path);
|
||||
if (!f) {
|
||||
std::string text;
|
||||
const int rst = read_text_file(path, text, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) {
|
||||
if (required) RAD_ERR("%s: cannot be read", path.c_str());
|
||||
return false;
|
||||
}
|
||||
std::string text((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("%s: larger than the 256 MiB a container file may be", path.c_str());
|
||||
return false;
|
||||
}
|
||||
return gen_sampling_parse(text, path.c_str(), out);
|
||||
}
|
||||
|
||||
@@ -204,13 +216,18 @@ bool gen_sampling_from_kv(const std::function<const char*(const char*)>& get, Ge
|
||||
}
|
||||
if (std::strcmp(k, "no_repeat_ngram_whitelist") == 0) {
|
||||
/* Not a number: the container's metadata is string-valued, and the whitelist is a
|
||||
* JSON array spelled as JSON text. */
|
||||
try {
|
||||
j[k] = Json::parse(v);
|
||||
} catch (const std::exception& e) {
|
||||
* JSON array spelled as JSON text. Bounded like every other container input. */
|
||||
bool over = false, deep = false;
|
||||
Json w = Json::parse_container(v, &over, &deep);
|
||||
if (over || deep)
|
||||
RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON "
|
||||
"array of ids (%s); ignoring it", k, v, e.what());
|
||||
}
|
||||
"array of ids (%s); ignoring it",
|
||||
k, log_sanitize(v).c_str(),
|
||||
over ? "it carries far too many values" : "it nests far too deep");
|
||||
else if (!w.is_discarded()) j[k] = std::move(w);
|
||||
else
|
||||
RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON "
|
||||
"array of ids; ignoring it", k, v);
|
||||
continue;
|
||||
}
|
||||
if (std::strcmp(k, "suppress_tokens") == 0) {
|
||||
|
||||
+12
-2
@@ -804,9 +804,19 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
/* ---- the config */
|
||||
const std::string cfg_path = dir + "/adapter_config.json";
|
||||
std::string text;
|
||||
if (!read_file(cfg_path, &text))
|
||||
const int rst = read_text_file(cfg_path, text, kMaxContainerFile);
|
||||
if (rst == RAD_E_FORMAT)
|
||||
return fail(err, RAD_E_FORMAT,
|
||||
cfg_path + ": larger than the 256 MiB a container file may be");
|
||||
if (rst != RAD_OK)
|
||||
return fail(err, RAD_E_IO, cfg_path + ": cannot read (a PEFT adapter directory has one)");
|
||||
const ojson cfg = ojson::parse(text, nullptr, false);
|
||||
/* Container-supplied like the generation config, so read with the container's bounds: a
|
||||
* file past them is refused during the read, never grown into a tree to be walked. */
|
||||
bool over = false, deep = false;
|
||||
const ojson cfg = ojson::parse_container(text, &over, &deep);
|
||||
if (over || deep)
|
||||
return fail(err, RAD_E_FORMAT,
|
||||
cfg_path + (over ? ": carries far too many JSON values" : ": nests far too deep"));
|
||||
if (cfg.is_discarded() || !cfg.is_object())
|
||||
return fail(err, RAD_E_FORMAT, cfg_path + ": not a JSON object");
|
||||
auto truthy = [&](const char* k) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "text/gguf.h"
|
||||
|
||||
#include "util/json.h"
|
||||
#include "rad_internal.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
@@ -569,15 +570,26 @@ int read_added(const json& arr, VocabBuild& out, const char* origin, uint64_t id
|
||||
* elsewhere. */
|
||||
void read_config_json(const std::string& dir, VocabBuild& out) {
|
||||
const std::string path = dir + "tokenizer_config.json";
|
||||
std::ifstream f(path);
|
||||
if (!f) {
|
||||
/* Container bytes: size-screened and parsed with the container's bounds, like every other
|
||||
* file a checkpoint carries. (Streaming with operator>> meant an unbounded DOM.) */
|
||||
std::string text;
|
||||
const int rst = read_text_file(path, text, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) {
|
||||
RAD_INFO("tokenizer: no %s; bos/eos ids and the chat template are unset", path.c_str());
|
||||
return;
|
||||
}
|
||||
json c;
|
||||
try { f >> c; }
|
||||
catch (const std::exception& e) {
|
||||
RAD_WARN("tokenizer: %s did not parse (%s); ids left unset", path.c_str(), e.what());
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("tokenizer: %s is larger than the 256 MiB a container file may be; ids left unset",
|
||||
path.c_str());
|
||||
return;
|
||||
}
|
||||
bool over = false, deep = false;
|
||||
json c = json::parse_container(text, &over, &deep);
|
||||
if (over || deep || c.is_discarded()) {
|
||||
RAD_WARN("tokenizer: %s did not read as a bounded JSON document (%s); ids left unset",
|
||||
path.c_str(),
|
||||
over ? "it carries far too many values"
|
||||
: deep ? "it nests far too deep" : "it does not parse");
|
||||
return;
|
||||
}
|
||||
std::unordered_map<std::string, int32_t> id_of;
|
||||
@@ -624,11 +636,15 @@ void read_config_json(const std::string& dir, VocabBuild& out) {
|
||||
* key, as in transformers. */
|
||||
void read_chat_template_file(const std::string& dir, VocabBuild& out) {
|
||||
const std::string path = dir + "chat_template.jinja";
|
||||
std::ifstream f(path);
|
||||
if (!f) return;
|
||||
std::ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
std::string t = ss.str();
|
||||
std::string t;
|
||||
const int rst = read_text_file(path, t, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) return;
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("tokenizer: %s is larger than the 256 MiB a container file may be; template left "
|
||||
"unset",
|
||||
path.c_str());
|
||||
return;
|
||||
}
|
||||
while (!t.empty() && (t.back() == '\n' || t.back() == '\r')) t.pop_back();
|
||||
if (t.empty()) return;
|
||||
RAD_INFO("tokenizer: chat template from %s (%zu bytes)%s", path.c_str(), t.size(),
|
||||
@@ -770,11 +786,20 @@ int interpret_tokenizer_json(const json& j, uint64_t id_limit, const char* origi
|
||||
} /* namespace */
|
||||
|
||||
int parse_tokenizer_json_buf(std::string_view text, const char* origin, VocabBuild& out) {
|
||||
json j;
|
||||
try {
|
||||
j = json::parse(text);
|
||||
} catch (const std::exception& e) {
|
||||
RAD_ERR("%s: not valid JSON: %s", origin, e.what());
|
||||
/* A container-supplied file. It legitimately carries hundreds of thousands of FLAT values
|
||||
* (a vocabulary, the merges), so it reads under the container's generous bounds rather than
|
||||
* the wire's: a hostile one is refused during the read instead of becoming a tree whose
|
||||
* walk or destruction overflows this process's stack. */
|
||||
bool over = false, deep = false;
|
||||
json j = json::parse_container(text, &over, &deep);
|
||||
if (over || deep) {
|
||||
RAD_ERR("%s: %s; a tokenizer file may not carry more than 2^24 values or nest more "
|
||||
"than 256 levels",
|
||||
origin, over ? "carries far too many values" : "nests far too deep");
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
if (j.is_discarded()) {
|
||||
RAD_ERR("%s: not valid JSON", origin);
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
out = VocabBuild{};
|
||||
@@ -800,14 +825,16 @@ int parse_tokenizer_json_buf(std::string_view text, const char* origin, VocabBui
|
||||
|
||||
int parse_tokenizer_json(const char* path, VocabBuild& out) {
|
||||
if (!path) return RAD_E_INVAL;
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if (!f) {
|
||||
std::string text;
|
||||
const int rst = read_text_file(path, text, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) {
|
||||
RAD_ERR("tokenizer: cannot open %s", path);
|
||||
return RAD_E_IO;
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
const std::string text = ss.str();
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("tokenizer: %s is larger than the 256 MiB a container file may be", path);
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
|
||||
RAD_TRY(parse_tokenizer_json_buf(text, path, out));
|
||||
|
||||
|
||||
@@ -249,6 +249,14 @@ Json Json::parse_bounded(std::string_view text, int64_t budget, int max_depth, b
|
||||
return out;
|
||||
}
|
||||
|
||||
Json Json::parse_container(std::string_view text, bool* over, bool* deep) {
|
||||
bool o = false, d = false;
|
||||
Json j = parse_bounded(text, 1LL << 24, 256, &o, &d);
|
||||
if (over) *over = o;
|
||||
if (deep) *deep = d;
|
||||
return j;
|
||||
}
|
||||
|
||||
Json Json::parse(std::string_view text) { return parse(text, nullptr, true, false); }
|
||||
|
||||
Json Json::parse(std::istream& in) {
|
||||
|
||||
@@ -894,6 +894,13 @@ public:
|
||||
* of a tree when a bound trips; returns discarded on a syntax error. */
|
||||
static Json parse_bounded(std::string_view text, int64_t budget, int max_depth, bool* over,
|
||||
bool* deep);
|
||||
/* A JSON file the container, an adapter or a download supplied. The wire's bounded read,
|
||||
* sized generously instead: these files legitimately carry huge FLAT value sets (a
|
||||
* tokenizer's vocabulary), and nothing legitimate nests 256 deep. A file past either bound
|
||||
* comes back discarded, like a syntax error -- deep enough to refuse, never deep enough
|
||||
* to overflow a dump's or a destructor's stack on the way there. */
|
||||
static Json parse_container(std::string_view text, bool* over = nullptr,
|
||||
bool* deep = nullptr);
|
||||
|
||||
private:
|
||||
using difference_type = std::ptrdiff_t;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
/* rad_util.cpp -- logging, string formatting, Geometry and constraint matching. */
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "../rad_internal.h"
|
||||
|
||||
#include <cstdio>
|
||||
@@ -92,6 +95,19 @@ std::string log_sanitize(const std::string& s) {
|
||||
return out;
|
||||
}
|
||||
|
||||
int read_text_file(const std::string& path, std::string& out, size_t cap) {
|
||||
std::error_code ec;
|
||||
const std::uintmax_t sz = std::filesystem::file_size(path, ec);
|
||||
if (ec) return RAD_E_IO;
|
||||
if (sz > cap) return RAD_E_FORMAT; /* refused before the bytes are read */
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if (!f) return RAD_E_IO;
|
||||
std::ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
out = ss.str();
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
void log_emit(Log l, const char* file, int line, const char* fmt, ...) {
|
||||
const char* base = std::strrchr(file, '/');
|
||||
base = base ? base + 1 : file;
|
||||
|
||||
Reference in new issue
Block a user