strawberry/fuzz/fuzz_legacy_kv.cpp
trvon dc50f06f8d
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Drop unqlite-cpp and import legacy databases
Nothing shipped used the library after the event store landed. A small
reader/writer for its file format (src/store/legacy_kv) replaces it for
the legacy code, and the submodule, subproject and library test are
removed.

A writer open now imports a legacy database.db file into the event
store: indicators are replayed in an order consistent with the old
hash/rule indexes, the result is verified against the old file, the
old file is kept as database.db.migrated-<ts>, and an import
interrupted between its final renames is finished on the next open.
Read-only handles read a legacy file directly and switch to the store
once it is imported. Ids re-stored under another hash are reported
(their old index entries cannot be carried over).

Tests: legacy import properties I1-I6 (locked and corrupt files are
left untouched), the differential test now shares its comparisons, and
fuzz_legacy_kv covers the file parser. A copy of a real 137 KB legacy
database imported cleanly into 7 KB.

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

38 lines
1.3 KiB
C++

// Legacy (unqlite-format) database files are parsed by the daemon on its
// first start (import). A corrupt or truncated file must be rejected, never
// crash or allocate beyond the file's own size; an accepted file re-saves to
// an equivalent map.
#include "../src/store/legacy_kv.hpp"
#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <string>
#include <unistd.h>
using namespace strawberry;
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
static const std::string dir = (std::filesystem::temp_directory_path() / ("sb-fuzz-kv-" + std::to_string(::getpid()))).string();
std::filesystem::create_directories(dir);
const std::string path = dir + "/db";
if (FILE* f = std::fopen(path.c_str(), "wb")) {
std::fwrite(data, 1, size, f);
std::fclose(f);
}
auto loaded = legacy_kv::database::load(path);
if (!loaded) return 0;
{
auto db = legacy_kv::database::open(path + ".copy", legacy_kv::open_mode::create);
if (!db) __builtin_trap();
for (const auto& [k, v] : *loaded) {
if (!db->store(k, v)) __builtin_trap();
}
}
auto again = legacy_kv::database::load(path + ".copy");
std::filesystem::remove(path + ".copy");
if (!again || *again != *loaded) __builtin_trap();
return 0;
}