Reuse the existing resolver/sampler for opt-in aggregate training summaries. Reject incomplete or undecodable snapshots without usable partial reports, retain best-effort readers elsewhere, and document cooperative extent checks rather than atomic snapshot guarantees. Include command-table/help, JSON, fixture and corruption regressions; no model training or promotion.
109 lines
5.4 KiB
C++
109 lines
5.4 KiB
C++
// Replay arbitrary records AND bounded valid streams so sampler mutation
|
|
// paths receive coverage even when wire-format mutations do not decode.
|
|
#include "strawberry/learn/replay.hpp"
|
|
#include "strawberry/telemetry.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <string_view>
|
|
|
|
using namespace strawberry;
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
|
learn::ReplayOptions options;
|
|
options.max_window_events = 4;
|
|
options.max_tracked_processes = 8;
|
|
options.top = 3;
|
|
options.rarity.width_log2 = 6;
|
|
options.rarity.first_seen_capacity = 4;
|
|
options.warmup_ns = 0;
|
|
options.collect_examples = true;
|
|
options.max_examples = 1 + size % 20;
|
|
options.max_entity_share = 0.25;
|
|
if (size != 0) {
|
|
switch (data[0] % 8) {
|
|
case 0: options.max_entity_share = std::numeric_limits<double>::quiet_NaN(); break;
|
|
case 1: options.max_entity_share = -1; break;
|
|
case 2: options.max_entity_share = std::numeric_limits<double>::infinity(); break;
|
|
case 3: options.max_entity_share = 1.5; break;
|
|
default: break;
|
|
}
|
|
}
|
|
options.sample_seed = size;
|
|
// Exercise evaluation-duration arithmetic too, including timestamp
|
|
// fields whose decoded value differs from the index time.
|
|
options.inject.emplace_back();
|
|
options.inject.back().name = "fuzz";
|
|
options.inject_copies = 1;
|
|
learn::Replayer replayer(options);
|
|
for (std::size_t n = 0; n < std::min<std::size_t>(64, size); ++n) {
|
|
ProcessExecRecord exec;
|
|
exec.timestamp_ns = 1'000'000'000LL + static_cast<int64_t>(n) * 61'000'000'000LL;
|
|
exec.pid = static_cast<uint32_t>(100 + n);
|
|
exec.process_node_id = "process_" + std::to_string(exec.pid) + "_b_1";
|
|
exec.exe = std::string("/usr/bin/fuzz_") + static_cast<char>('a' + (data[n] >> 1) % 6);
|
|
exec.cmdline = exec.exe;
|
|
exec.source = "fuzz";
|
|
replayer.feed(store::RecordType::process_exec, encode_process_exec(exec));
|
|
if ((data[n] & 1) != 0) {
|
|
ScanResultRecord scan;
|
|
scan.timestamp_ns = exec.timestamp_ns;
|
|
scan.pid = exec.pid;
|
|
scan.process_node_id = exec.process_node_id;
|
|
scan.rules = {"Teacher"};
|
|
replayer.feed(store::RecordType::scan_result, encode_scan_result(scan));
|
|
}
|
|
}
|
|
// Original arbitrary wire-format input: [type byte][length byte][payload].
|
|
std::size_t i = 0;
|
|
while (i + 2 <= size) {
|
|
static constexpr store::RecordType kTypes[] = {
|
|
store::RecordType::process_exec, store::RecordType::connect, store::RecordType::ptrace,
|
|
store::RecordType::module_load, store::RecordType::file_write, store::RecordType::scan_result,
|
|
store::RecordType::gap, store::RecordType::rule_match};
|
|
const auto type = kTypes[data[i] % (sizeof(kTypes) / sizeof(kTypes[0]))];
|
|
const std::size_t len = std::min<std::size_t>(data[i + 1], size - i - 2);
|
|
replayer.feed(type, std::string_view(reinterpret_cast<const char*>(data + i + 2), len));
|
|
i += 2 + len;
|
|
}
|
|
const auto report = replayer.finish();
|
|
const auto max = std::max<std::size_t>(1, options.max_examples);
|
|
const auto quota = std::max<std::size_t>(1, max / 5);
|
|
const double share = options.max_entity_share;
|
|
const auto cap = !std::isfinite(share) || share <= 0 ? 1 : share >= 1 ? max :
|
|
std::max<std::size_t>(1, static_cast<std::size_t>(share * static_cast<double>(max)));
|
|
if (report.top_windows.size() > options.top || report.examples.size() > max) __builtin_trap();
|
|
std::size_t positives = 0;
|
|
for (const auto& e : report.examples) positives += e.label > 0.5f;
|
|
if (positives > std::min<uint64_t>(report.example_positives, quota)) __builtin_trap();
|
|
if (report.example_positives != 0 && positives == 0) __builtin_trap();
|
|
// Independent bounded oracle for the new anonymous concentration facts.
|
|
// It must describe the actual retained sample, not configured capacity.
|
|
std::array<std::size_t, 20> counts{};
|
|
std::size_t entities = 0;
|
|
for (std::size_t a = 0; a < report.examples.size(); ++a) {
|
|
bool prior = false;
|
|
for (std::size_t b = 0; b < a; ++b) prior |= report.examples[b].entity == report.examples[a].entity;
|
|
if (prior) continue;
|
|
std::size_t n = 1;
|
|
for (std::size_t b = a + 1; b < report.examples.size(); ++b)
|
|
n += report.examples[b].entity == report.examples[a].entity;
|
|
if (n > cap) __builtin_trap();
|
|
counts[entities++] = n;
|
|
}
|
|
std::sort(counts.begin(), counts.end(), std::greater<>());
|
|
if (!report.examples_collected || report.sample.entity_cap != cap || report.sample.positive_quota != quota ||
|
|
report.sample.capacity != max || report.sample.entities != entities ||
|
|
report.sample.retained_positive != positives || report.sample.seed != options.sample_seed ||
|
|
report.example_positives > report.example_windows || report.examples.size() > report.example_windows)
|
|
__builtin_trap();
|
|
for (std::size_t n = 0; n < report.sample.top_entity_counts.size(); ++n)
|
|
if (report.sample.top_entity_counts[n] != counts[n]) __builtin_trap();
|
|
// In-memory replay is not a checked current-manifest snapshot.
|
|
if (!learn::replay_training_summary_json(report).empty()) __builtin_trap();
|
|
(void) learn::replay_report_json(report);
|
|
return 0;
|
|
}
|