// 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 #include #include #include #include #include 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::quiet_NaN(); break; case 1: options.max_entity_share = -1; break; case 2: options.max_entity_share = std::numeric_limits::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(64, size); ++n) { ProcessExecRecord exec; exec.timestamp_ns = 1'000'000'000LL + static_cast(n) * 61'000'000'000LL; exec.pid = static_cast(100 + n); exec.process_node_id = "process_" + std::to_string(exec.pid) + "_b_1"; exec.exe = std::string("/usr/bin/fuzz_") + static_cast('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(data[i + 1], size - i - 2); replayer.feed(type, std::string_view(reinterpret_cast(data + i + 2), len)); i += 2 + len; } const auto report = replayer.finish(); const auto max = std::max(1, options.max_examples); const auto quota = std::max(1, max / 5); const double share = options.max_entity_share; const auto cap = !std::isfinite(share) || share <= 0 ? 1 : share >= 1 ? max : std::max(1, static_cast(share * static_cast(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(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 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; }