strawberry/fuzz/fuzz_record_decoder.cpp
trvon adb40e5b62
Add compressed event store
zstd block codec (CRC32C, varint tagged records), segment files with
sealed footers and block index, crash-atomic MANIFEST, LogStore
(lock, recovery, rolls, orphaning, heal with backoff), the evidence
view, and the legacy unqlite store kept for import. Property tests
under injected faults and crashes, a differential test against the old
store, and fuzz targets for every decoder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:17 -06:00

47 lines
1.9 KiB
C++

// Event-store record/field decoding over untrusted bytes (a torn or hostile
// segment). Decoders must never crash, and whatever they accept must
// re-encode to the same bytes (the encoding is canonical for what we write).
#include "strawberry/store/codec.hpp"
#include <cstdint>
#include <string>
#include <string_view>
using namespace strawberry::store;
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
const std::string_view in(reinterpret_cast<const char*>(data), size);
RecordReader records(in);
RecordView rec;
std::string reencoded;
std::size_t accepted = 0;
while (records.next(rec)) {
FieldReader fields(rec.payload);
Field f;
FieldWriter w;
std::size_t field_bytes = 0;
bool canonical = true;
while (fields.next(f)) {
const std::size_t before = w.data().size();
switch (f.type) {
case WireType::varint: w.u64(f.tag, f.value); break;
case WireType::fixed32: w.fixed32(f.tag, static_cast<uint32_t>(f.value)); break;
case WireType::fixed64: w.fixed64(f.tag, f.value); break;
case WireType::bytes: w.bytes(f.tag, f.bytes); break;
}
field_bytes += w.data().size() - before;
}
// Varints may be non-minimal in hostile input (e.g. 0x80 0x00), so only
// a fully parsed payload that is already canonical must match exactly.
if (!fields.error() && field_bytes == rec.payload.size() && w.data() != rec.payload) {
canonical = false;
}
if (!canonical) __builtin_trap();
append_record(reencoded, static_cast<RecordType>(rec.type), rec.payload);
++accepted;
}
// The accepted records are a prefix of the input (modulo varint form).
if (!records.error() && accepted > 0 && reencoded.size() > in.size()) __builtin_trap();
return 0;
}