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.
117 lines
5.3 KiB
C++
117 lines
5.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 "strawberry/store/log_store.hpp"
|
|
|
|
#include <cstdlib>
|
|
#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;
|
|
// Private per-process fixture, also usable by the read-only stream scan.
|
|
// Input grammar is unchanged; no new parser or production write path.
|
|
static const fs::path root = [] {
|
|
std::string pattern = (fs::temp_directory_path() / "sb-fuzz-seg-XXXXXX").string();
|
|
if (::mkdtemp(pattern.data()) == nullptr) return fs::path{};
|
|
std::error_code ec;
|
|
fs::create_directories(fs::path(pattern) / "telemetry", ec);
|
|
return ec ? fs::path{} : fs::path(pattern);
|
|
}();
|
|
if (root.empty()) return 0;
|
|
const fs::path path = root / "telemetry" / segment_file_name(1);
|
|
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();
|
|
uint64_t streamed = 0;
|
|
const auto stream = scan_store_read_only(root.string(), Stream::telemetry,
|
|
[&](uint32_t, std::string_view) { ++streamed; });
|
|
if (!stream.exists || !stream.listing_complete || !stream.active_extent_known ||
|
|
stream.listed_segments != 1 || stream.skipped_segments != 0 ||
|
|
streamed != records || stream.report.records != sealed.records ||
|
|
stream.report.header_ok != sealed.header_ok || stream.report.torn_tail != sealed.torn_tail ||
|
|
stream.report.bad_blocks != sealed.bad_blocks || stream.report.malformed_records != sealed.malformed_records)
|
|
__builtin_trap();
|
|
if (sealed.io_error.empty() && !sealed.header_ok && stream.invalid_segments != 1) __builtin_trap();
|
|
// The same scanner must report the decode availability it observed: a block
|
|
// needing a dictionary this build lacks is unavailable, a complete but
|
|
// invalid block header is data that cannot be accepted, and a short tail is
|
|
// neither (an active writer may still be finishing it).
|
|
const bool undecodable = sealed.tail_error == BlockError::unknown_dictionary ||
|
|
sealed.tail_error == BlockError::out_of_memory;
|
|
const bool malformed_tail = !undecodable && sealed.tail_error != BlockError::ok &&
|
|
sealed.tail_error != BlockError::truncated;
|
|
if (stream.unavailable_segments != (undecodable ? 1u : 0u)) __builtin_trap();
|
|
if (stream.malformed_tail_segments != (malformed_tail ? 1u : 0u)) __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;
|
|
}
|