// Rarity state decoder over untrusted bytes: never crashes; what it accepts // re-encodes to the same bytes and scores safely. Each input is tried as a // whole file and, with a valid checksum appended, as a file body (so the // fuzzer reaches the parser behind the checksum). #include "strawberry/hash_utils.hpp" #include "strawberry/learn/rarity.hpp" #include #include #include using namespace strawberry; using namespace strawberry::learn; namespace { void check(std::string_view file) { RarityOptions small; small.width_log2 = 4; // small sketches, so short inputs can be valid small.depth = 1; small.first_seen_capacity = 8; RarityModel m(small); int64_t saved = 0; std::string error; if (m.decode_state(file, saved, error)) { if (m.encode_state(saved) != file) __builtin_trap(); const auto s = m.surprisal(TupleKind::parent_child, "a > b"); if (!std::isfinite(s.bits) || s.bits < 0) __builtin_trap(); m.advance_to(saved); (void) m.first_seen("k", saved); } else if (error.empty()) { __builtin_trap(); } } } // namespace extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { const std::string_view in(reinterpret_cast(data), size); check(in); std::string body(in); body += HashUtils::sha256_data(data, size); check(body); return 0; }