92 lines
3.0 KiB
C++
92 lines
3.0 KiB
C++
#include "evo/world/chunks.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <span>
|
|
|
|
namespace {
|
|
using namespace evo::world;
|
|
constexpr WorldIdentity identity{7, 9};
|
|
constexpr std::size_t budget = 2000000;
|
|
struct Seeds {
|
|
WorldBytes snapshot, journal;
|
|
};
|
|
Seeds seeds() {
|
|
auto store = ChunkStore::create({}, 2, budget, {{VolumeUnits{1000000}, VolumeUnits{1000000}}});
|
|
if (!store) {
|
|
std::abort();
|
|
}
|
|
const auto view = (**store).load({0, 0, -2});
|
|
if (!view) {
|
|
std::abort();
|
|
}
|
|
std::array<CellCut, 1> cuts{{{{0, 0, -2}, {}, view->revision(), 10}}};
|
|
ExcavationRequest r{1, 1, cuts, 0};
|
|
r.digest = *excavation_digest(r);
|
|
if (!(**store).excavate(r)) {
|
|
std::abort();
|
|
}
|
|
const auto base = (**store).snapshot(identity);
|
|
if (!base) {
|
|
std::abort();
|
|
}
|
|
cuts[0].expected_revision = (**store).view({0, 0, -2})->revision();
|
|
r.command = 2;
|
|
r.digest = *excavation_digest(r);
|
|
const std::array<ExcavationRequest, 1> requests{r};
|
|
const auto journal = make_edit_journal(*base, identity, requests, budget);
|
|
if (!journal) {
|
|
std::abort();
|
|
}
|
|
return {*base, *journal};
|
|
}
|
|
WorldBytes mutate(WorldBytes bytes, std::span<const std::uint8_t> input) {
|
|
// Triples choose bounded offsets and replacement bytes; repair the envelope
|
|
// checksum so fuzz mutations exercise semantic validation as well as framing.
|
|
for (std::size_t i = 0; i + 2 < input.size(); i += 3) {
|
|
const auto index =
|
|
(static_cast<std::size_t>(input[i]) * 256 + input[i + 1]) % (bytes.size() - 8);
|
|
bytes[index] = input[i + 2];
|
|
}
|
|
std::uint64_t hash = 14695981039346656037ULL;
|
|
for (std::size_t i = 0; i < bytes.size() - 8; ++i) {
|
|
hash = (hash ^ bytes[i]) * 1099511628211ULL;
|
|
}
|
|
for (unsigned i = 0; i < 8; ++i) {
|
|
bytes[bytes.size() - 8 + i] = static_cast<std::uint8_t>((hash >> (8U * i)) & 255U);
|
|
}
|
|
return bytes;
|
|
}
|
|
void check_snapshot(std::span<const std::uint8_t> bytes) {
|
|
auto restored = ChunkStore::restore(bytes, identity, budget);
|
|
if (restored) {
|
|
const auto canonical = (**restored).snapshot(identity);
|
|
if (!canonical ||
|
|
!std::equal(canonical->begin(), canonical->end(), bytes.begin(), bytes.end())) {
|
|
std::abort();
|
|
}
|
|
}
|
|
}
|
|
} // namespace
|
|
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
|
|
if (size > 4096) {
|
|
return 0;
|
|
}
|
|
const auto input = std::span{data, size};
|
|
static const auto fixtures = seeds();
|
|
check_snapshot(input);
|
|
const auto mutated = mutate(fixtures.snapshot, input);
|
|
check_snapshot(mutated);
|
|
const auto journal = mutate(fixtures.journal, input);
|
|
auto replay = replay_edit_journal(fixtures.snapshot, journal, identity, budget);
|
|
if (replay) {
|
|
const auto bytes = (**replay).snapshot(identity);
|
|
if (!bytes || !ChunkStore::restore(*bytes, identity, budget)) {
|
|
std::abort();
|
|
}
|
|
}
|
|
return 0;
|
|
}
|