Archived
diag: the baseline and corpus files are read like the reports they are
Both files diagnose consumes are written by other runs and pasted between people: --diagnose-baseline is an earlier report, the corpus a forwarded file. Neither was read like a supplied file. The baseline got the unbounded Json::parse, and compare_outputs then walked it with throwing accessors -- a member it does not expect throws, and nothing between run_diagnose and main catches -- so a crafted "earlier report" aborted a run with std::terminate after hours of benchmarking work. The corpus got a hand-rolled unbounded fread loop and a raw parser per line: a deep line recursed the parse to a stack overflow before the fingerprint check could ever run. Now: capped read and parse_container on both (the corpus's size refusal lands before the bytes are read), a per-line over/deep refusal that names the line, and one try around the comparison so any structure the comparison does not expect is the refusal it should be -- "the baseline is not shaped like a report", parity not compared, the run keeps its own results. diagnose_test pins the deep line. Found by the 2.1.0 review pass (both security axes, independently).
This commit is contained in:
1 parent
5c66eecc8e
commit
d893fd05d1
3 files changed
+46
-18
No files matched your search
+10
-8
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "diag/diag.h"
|
#include "diag/diag.h"
|
||||||
|
|
||||||
|
#include "rad_internal.h"
|
||||||
#include "rad_runtime.h"
|
#include "rad_runtime.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -943,15 +944,13 @@ std::vector<CorpusEntry> builtin_corpus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int load_corpus(const std::string& path, std::vector<CorpusEntry>* out, std::string* why) {
|
int load_corpus(const std::string& path, std::vector<CorpusEntry>* out, std::string* why) {
|
||||||
FILE* f = std::fopen(path.c_str(), "rb");
|
/* A corpus is a file someone forwarded: the capped read and bounded parse every supplied
|
||||||
if (!f) {
|
* JSON file gets -- the size refusal lands before the bytes are read. */
|
||||||
*why = "cannot open " + path;
|
std::string text;
|
||||||
|
if (read_text_file(path, text, kMaxContainerFile) != RAD_OK) {
|
||||||
|
*why = "cannot open " + path + ", or it carries more than the largest accepted JSON file";
|
||||||
return RAD_E_IO;
|
return RAD_E_IO;
|
||||||
}
|
}
|
||||||
std::string text;
|
|
||||||
char b[65536];
|
|
||||||
for (size_t n; (n = std::fread(b, 1, sizeof b, f)) > 0;) text.append(b, n);
|
|
||||||
std::fclose(f);
|
|
||||||
std::vector<CorpusEntry> got;
|
std::vector<CorpusEntry> got;
|
||||||
size_t line_no = 0;
|
size_t line_no = 0;
|
||||||
for (size_t pos = 0; pos <= text.size();) {
|
for (size_t pos = 0; pos <= text.size();) {
|
||||||
@@ -962,11 +961,14 @@ int load_corpus(const std::string& path, std::vector<CorpusEntry>* out, std::str
|
|||||||
++line_no;
|
++line_no;
|
||||||
const size_t a = line.find_first_not_of(" \t\r");
|
const size_t a = line.find_first_not_of(" \t\r");
|
||||||
if (a == std::string::npos || line[a] == '#') continue;
|
if (a == std::string::npos || line[a] == '#') continue;
|
||||||
const json j = json::parse(line, nullptr, /*allow_exceptions=*/false);
|
bool over = false, deep = false;
|
||||||
|
const json j = json::parse_container(line, &over, &deep);
|
||||||
auto bad = [&](const char* what) {
|
auto bad = [&](const char* what) {
|
||||||
*why = path + ":" + std::to_string(line_no) + ": " + what;
|
*why = path + ":" + std::to_string(line_no) + ": " + what;
|
||||||
return RAD_E_INVAL;
|
return RAD_E_INVAL;
|
||||||
};
|
};
|
||||||
|
if (over) return bad("carries far too many values for one prompt line");
|
||||||
|
if (deep) return bad("nests far too deep for one prompt line");
|
||||||
if (j.is_discarded() || !j.is_object()) return bad("not a JSON object");
|
if (j.is_discarded() || !j.is_object()) return bad("not a JSON object");
|
||||||
if (!j.contains("category") || !j["category"].is_string() || j["category"].get<std::string>().empty())
|
if (!j.contains("category") || !j["category"].is_string() || j["category"].get<std::string>().empty())
|
||||||
return bad("needs a non-empty string \"category\"");
|
return bad("needs a non-empty string \"category\"");
|
||||||
|
|||||||
+28
-10
@@ -652,19 +652,37 @@ int Engine::run_diagnose() {
|
|||||||
|
|
||||||
bool parity_ok = true;
|
bool parity_ok = true;
|
||||||
if (!cfg_.diagnose_baseline.empty()) {
|
if (!cfg_.diagnose_baseline.empty()) {
|
||||||
std::ifstream f(cfg_.diagnose_baseline);
|
/* A baseline is a report written by another run -- often on another machine, pasted
|
||||||
const json base = json::parse(f, nullptr, /*allow_exceptions=*/false);
|
* between people: read it like a supplied file (capped read, bounded parse), and treat
|
||||||
|
* structure the comparison does not expect as a refusal, not as a crash. compare_outputs
|
||||||
|
* reads its baseline with throwing accessors, and nothing between here and main catches:
|
||||||
|
* a structurally hostile baseline used to abort a run that had already done its work. */
|
||||||
std::string why;
|
std::string why;
|
||||||
json cmp;
|
json cmp;
|
||||||
if (!f.is_open() || base.is_discarded()) {
|
std::string base_text;
|
||||||
|
const json base = read_text_file(cfg_.diagnose_baseline, base_text, kMaxContainerFile) == RAD_OK
|
||||||
|
? json::parse_container(base_text)
|
||||||
|
: json();
|
||||||
|
if (base.is_discarded()) {
|
||||||
why = "the baseline file is not readable JSON";
|
why = "the baseline file is not readable JSON";
|
||||||
} else if (diag::compare_outputs(rep, base, &cmp, &why) == RAD_OK) {
|
} else {
|
||||||
cmp["baseline"] = "an earlier report";
|
int cmp_rc = RAD_E_FORMAT;
|
||||||
rep["parity"] = cmp;
|
try {
|
||||||
parity_ok = cmp["verdict"] == "identical" && cmp["acceptance_verdict"] == "no drop";
|
cmp_rc = diag::compare_outputs(rep, base, &cmp, &why);
|
||||||
RAD_INFO("diagnose: parity %s, %lld of %lld requests identical; draft acceptance: %s",
|
} catch (const json::exception& e) {
|
||||||
cmp["verdict"].get<std::string>().c_str(), (long long)cmp["identical"].get<int64_t>(),
|
why = std::string("the baseline is not shaped like a report: ") + e.what();
|
||||||
(long long)cmp["compared"].get<int64_t>(), cmp["acceptance_verdict"].get<std::string>().c_str());
|
cmp_rc = RAD_E_FORMAT;
|
||||||
|
}
|
||||||
|
if (cmp_rc == RAD_OK) {
|
||||||
|
cmp["baseline"] = "an earlier report";
|
||||||
|
rep["parity"] = cmp;
|
||||||
|
parity_ok = cmp["verdict"] == "identical" && cmp["acceptance_verdict"] == "no drop";
|
||||||
|
RAD_INFO("diagnose: parity %s, %lld of %lld requests identical; draft acceptance: %s",
|
||||||
|
cmp["verdict"].get<std::string>().c_str(),
|
||||||
|
(long long)cmp["identical"].get<int64_t>(),
|
||||||
|
(long long)cmp["compared"].get<int64_t>(),
|
||||||
|
cmp["acceptance_verdict"].get<std::string>().c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!why.empty()) {
|
if (!why.empty()) {
|
||||||
rep["parity"] = { { "baseline", "an earlier report" },
|
rep["parity"] = { { "baseline", "an earlier report" },
|
||||||
|
|||||||
@@ -464,6 +464,14 @@ TEST(corpus_file_reads_messages_and_prompts) {
|
|||||||
TEST(corpus_file_refuses_a_bad_line_and_names_it) {
|
TEST(corpus_file_refuses_a_bad_line_and_names_it) {
|
||||||
std::vector<CorpusEntry> c;
|
std::vector<CorpusEntry> c;
|
||||||
std::string why;
|
std::string why;
|
||||||
|
{
|
||||||
|
/* A line nesting past what a prompt line nests: the raw parser recursed for this, and
|
||||||
|
* the corpus -- a forwarded file -- is exactly the supplied input that stays bounded. */
|
||||||
|
TmpFile f(std::string("{\"category\": \"c\", \"prompt\": \"x\", \"tail\": ") +
|
||||||
|
std::string(1200, '[') + std::string(1200, ']') + "}\n");
|
||||||
|
CHECK_EQ(load_corpus(f.path, &c, &why), RAD_E_INVAL);
|
||||||
|
CHECK(why.find("nests far too deep") != std::string::npos);
|
||||||
|
}
|
||||||
{
|
{
|
||||||
TmpFile f("{\"category\": \"code\", \"prompt\": \"a\"}\n{\"prompt\": \"no category\"}\n");
|
TmpFile f("{\"category\": \"code\", \"prompt\": \"a\"}\n{\"prompt\": \"no category\"}\n");
|
||||||
CHECK_EQ(load_corpus(f.path, &c, &why), RAD_E_INVAL);
|
CHECK_EQ(load_corpus(f.path, &c, &why), RAD_E_INVAL);
|
||||||
|
|||||||
Reference in new issue
Block a user