// Trace DSL: parsing, errors, materialization. A failure aborts with the case. // // P1 the standard traces parse; each copy's node ids name its trace and copy // P2 malformed input is rejected with the line number // P3 materialized records decode, with pids above any real pid_max // P4 the training traces parse and share no trace name or timeline with // the evaluation set (a model must not be judged on what it trained on) #include "strawberry/learn/trace.hpp" #include "strawberry/proc_read.hpp" #include #include #include #include using namespace strawberry; using namespace strawberry::learn; namespace { #define CHECK(cond) \ do { \ if (!(cond)) { \ std::cerr << "CHECK FAILED: " #cond " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \ std::abort(); \ } \ } while (0) void test_standard() { const auto text = read_small_file(std::string(STRAWBERRY_SOURCE_ROOT) + "/config/traces/standard.trace"); CHECK(text.has_value()); std::vector traces; std::string error; CHECK(compile_traces(*text, traces, error)); CHECK(traces.size() == 6); for (const auto& t : traces) { const auto records = materialize_trace(t, 1'800'000'000LL * 1'000'000'000, 37); CHECK(records.size() == t.steps.size()); for (const auto& r : records) { if (r.type != store::RecordType::process_exec) continue; ProcessExecRecord e; CHECK(decode_process_exec(r.payload, e)); CHECK(e.pid >= kTracePidBase && e.source == "trace"); std::string name; uint32_t instance = 0; CHECK(parse_trace_node(e.process_node_id, name, instance) && name == t.name && instance == 37); if (!e.parent_node_id.empty()) CHECK(parse_trace_node(e.parent_node_id, name, instance)); } } std::string name; uint32_t instance = 0; CHECK(!parse_trace_node("process_100_b_1", name, instance)); std::cout << "P1 standard traces, P3 records: PASSED" << std::endl; } void test_training_set() { const auto load = [](const char* name) { const auto text = read_small_file(std::string(STRAWBERRY_SOURCE_ROOT) + "/config/traces/" + name); CHECK(text.has_value()); std::vector traces; std::string error; if (!compile_traces(*text, traces, error)) std::cerr << name << ": " << error << std::endl; CHECK(error.empty()); return traces; }; const auto evaluation = load("standard.trace"); const auto training = load("training.trace"); CHECK(training.size() >= 5); std::set names, execs; for (const auto& t : evaluation) { names.insert(t.name); // A timeline is identified by its sequence of executed programs. std::string shape; for (const auto& step : t.steps) { if (step.verb == TraceStep::Verb::exec) shape += step.exe + ";"; } execs.insert(shape); } for (const auto& t : training) { CHECK(!names.contains(t.name)); std::string shape; for (const auto& step : t.steps) { if (step.verb == TraceStep::Verb::exec) shape += step.exe + ";"; } CHECK(!shape.empty() && !execs.contains(shape)); } std::cout << "P4 training traces (" << training.size() << ") disjoint from evaluation: PASSED" << std::endl; } void rejects(const std::string& text, const std::string& want) { std::vector traces; std::string error; if (compile_traces(text, traces, error) || error.find(want) == std::string::npos || !traces.empty()) { std::cerr << "expected error containing '" << want << "' for:\n" << text << "\ngot: " << error << std::endl; std::abort(); } } void test_errors() { rejects("+0s exec a /bin/x", "line 1: event before any"); rejects("trace t\n+0s connect a 1.2.3.4 80", "line 2: unknown alias 'a'"); rejects("trace t\n+0s exec a bin/x", "absolute path"); rejects("trace t\n+0s exec a /bin/x parent=b", "unknown parent alias"); rejects("trace t\n+11m exec a /bin/x", "bad offset"); rejects("trace t\n+601s exec a /bin/x", "bad offset"); rejects("trace t\n+0s exec a /bin/x \"unterminated", "unterminated quote"); rejects("trace t\n+0s exec a /bin/x\n+1s connect a 1.2.3.4 70000", "bad port"); rejects("trace t\n+0s exec a /bin/x\n+1s write a /tmp/x elsewhere \"l\"", "persistence|drop"); rejects("trace t\n+0s fly a", "unknown verb"); rejects("trace t\ntrace t\n+0s exec a /bin/x", "duplicate trace"); rejects("trace t", "has no events"); rejects("trace bad name", "expected: trace "); std::vector traces; std::string error; CHECK(compile_traces("# only a comment\n\ntrace t # trailing comment\n+0s exec a \"/bin/x y\" \"x \\\"q\\\"\"\n", traces, error)); CHECK(traces.size() == 1 && traces[0].steps[0].exe == "/bin/x y" && traces[0].steps[0].cmdline == "x \"q\""); std::cout << "P2 errors: PASSED" << std::endl; } } // namespace int main() { test_standard(); test_training_set(); test_errors(); return 0; }