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>
85 lines
3.3 KiB
C++
85 lines
3.3 KiB
C++
// Segment scan + recovery over arbitrary file contents (a torn, corrupt or
|
|
// hostile file in the store directory). Must never crash, and recovery must
|
|
// leave a file that scans clean and is stable under a second recovery.
|
|
//
|
|
// Control byte (first input byte):
|
|
// bit 0 prepend a valid segment header (so the fuzzer starts at the blocks)
|
|
// bit 1 fix up block CRCs as the file is walked, so mutations reach zstd
|
|
// and the record decoder instead of stopping at a checksum
|
|
|
|
#include "strawberry/store/segment.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <filesystem>
|
|
#include <string>
|
|
|
|
#include <unistd.h>
|
|
|
|
using namespace strawberry::store;
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace {
|
|
|
|
void put32(std::string& b, std::size_t at, uint32_t v) {
|
|
for (int i = 0; i < 4; ++i) b[at + i] = static_cast<char>(v >> (8 * i));
|
|
}
|
|
|
|
uint32_t get32(const std::string& b, std::size_t at) {
|
|
uint32_t v = 0;
|
|
for (int i = 0; i < 4; ++i) v |= static_cast<uint32_t>(static_cast<uint8_t>(b[at + i])) << (8 * i);
|
|
return v;
|
|
}
|
|
|
|
void fix_crcs(std::string& file) {
|
|
std::size_t off = kSegmentHeaderBytes;
|
|
while (off + kBlockHeaderBytes <= file.size()) {
|
|
const uint32_t stored = get32(file, off + 12);
|
|
const std::size_t avail = file.size() - off - kBlockHeaderBytes;
|
|
const std::size_t n = stored < avail ? stored : avail;
|
|
put32(file, off + 20, crc32c(file.data() + off + kBlockHeaderBytes, n));
|
|
put32(file, off + 24, crc32c(file.data() + off, 24));
|
|
off += kBlockHeaderBytes + n;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
|
if (size == 0) return 0;
|
|
static const fs::path path = fs::temp_directory_path() / ("sb-fuzz-seg-" + std::to_string(::getpid()) + ".log");
|
|
std::string file;
|
|
if (data[0] & 1) file = encode_segment_header({Stream::telemetry, 1, 2});
|
|
file.append(reinterpret_cast<const char*>(data) + 1, size - 1);
|
|
if ((data[0] & 2) && file.size() >= kSegmentHeaderBytes) fix_crcs(file);
|
|
if (FILE* f = std::fopen(path.c_str(), "wb")) {
|
|
std::fwrite(file.data(), 1, file.size(), f);
|
|
std::fclose(f);
|
|
} else {
|
|
return 0;
|
|
}
|
|
|
|
uint64_t records = 0;
|
|
const ScanReport sealed = scan_segment(path.string(), ScanOptions{}, [&](uint32_t, std::string_view) { ++records; });
|
|
if (sealed.records != records) __builtin_trap();
|
|
|
|
const ScanReport rec = recover_segment(path.string());
|
|
if (!rec.header_ok) {
|
|
if (fs::file_size(path) != file.size()) __builtin_trap(); // non-segments are never touched
|
|
return 0;
|
|
}
|
|
if (!rec.io_error.empty()) return 0;
|
|
// A block that needs a dictionary we do not have is not corruption:
|
|
// recovery deliberately leaves it (and everything after it) alone.
|
|
if (rec.tail_error == BlockError::unknown_dictionary || rec.tail_error == BlockError::out_of_memory) {
|
|
if (fs::file_size(path) != file.size()) __builtin_trap();
|
|
return 0;
|
|
}
|
|
ScanOptions strict;
|
|
strict.skip_corrupt_blocks = false;
|
|
const ScanReport clean = scan_segment(path.string(), strict, nullptr);
|
|
if (clean.torn_tail || clean.valid_bytes != clean.file_bytes || clean.blocks != rec.blocks) __builtin_trap();
|
|
const ScanReport again = recover_segment(path.string());
|
|
if (again.truncated_bytes != 0) __builtin_trap();
|
|
return 0;
|
|
}
|