Implement M7.4a checkpoint publication and verified recovery

ADR-0061 publishes environmental-v1 full checkpoints. Manifest v1 is a
fixed 280-byte record (identity, chain, lease, versions, build, zero
RNG/history declarations, durability policy, payload length/SHA-256)
that PostgreSQL parses and digests itself. Schema v3 adds region_commits
with a digest CHECK and a composite head FK, owner-managed retained
builds, fenced publish/load functions and owner-only genesis; it drops
the bare v2 head kernel and fails closed while v2 heads exist.

Linux evo::region_store adds OpenSSL SHA-256, an immutable
content-addressed filesystem object store (temp+fsync+link, no
overwrite/delete, O_NOFOLLOW exact-length verified reads), and
publication (validate -> upload -> read back -> fenced transaction ->
receipt check) and recovery (re-digest, allowlist, verify, full restore).
Portable evo::region_checkpoint holds the codec and payload validation.

Validated: Windows CTest 56/56; WSL Release, clang-tidy and ASan/UBSan
private-PostgreSQL suites; four SQL and one host mutation each caught.
M7.4 stays open for 7.4b (lineage archive codec and history binding).
No Docker/Gitea CI, remote store, process kill or deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-28 15:25:27 -04:00
co-authored by Claude Opus 5.5
parent 07116446c6
commit 5d4f59f901
32 changed files with 2253 additions and 145 deletions
+200
View File
@@ -0,0 +1,200 @@
#include "evo/persistence/object_store.hpp"
#include "evo/persistence/sha256.hpp"
#include <atomic>
#include <cerrno>
#include <fcntl.h>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
namespace evo::persistence {
namespace {
constexpr std::size_t maximum_object_limit = std::size_t{64} * 1024 * 1024;
std::atomic<std::uint64_t> temporary_counter{0};
class Descriptor {
public:
explicit Descriptor(int fd) noexcept : fd_(fd) {}
Descriptor(const Descriptor&) = delete;
Descriptor& operator=(const Descriptor&) = delete;
~Descriptor() {
if (fd_ >= 0)
static_cast<void>(::close(fd_));
}
[[nodiscard]] int get() const noexcept {
return fd_;
}
[[nodiscard]] int release() noexcept {
const int fd = fd_;
fd_ = -1;
return fd;
}
// Explicit close so a deferred write error is observed.
[[nodiscard]] bool close() noexcept {
const int fd = release();
return fd < 0 || ::close(fd) == 0;
}
private:
int fd_;
};
std::string key_name(const Sha256Digest& d) {
constexpr char digits[] = "0123456789abcdef";
std::string out;
out.reserve(64);
for (const auto b : d) {
out.push_back(digits[b >> 4U]);
out.push_back(digits[b & 0x0FU]);
}
return out;
}
// Opens or creates a private subdirectory without following symlinks.
int subdirectory(int parent, const std::string& name) {
bool created = false;
if (::mkdirat(parent, name.c_str(), 0700) == 0)
created = true;
else if (errno != EEXIST)
return -1;
const int fd = ::openat(parent, name.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
if (fd >= 0 && created && ::fsync(parent) != 0) {
static_cast<void>(::close(fd));
return -1;
}
return fd;
}
bool write_all(int fd, std::span<const std::uint8_t> bytes) {
while (!bytes.empty()) {
const auto n = ::write(fd, bytes.data(), bytes.size());
if (n < 0 && errno == EINTR)
continue;
if (n <= 0)
return false;
bytes = bytes.subspan(static_cast<std::size_t>(n));
}
return true;
}
// Exact-length regular file whose content has the expected digest.
std::expected<std::vector<std::uint8_t>, ObjectError> read_verified(int directory,
const VerifiedObject& object) {
const auto name = key_name(object.sha256);
const Descriptor fd{::openat(directory, name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)};
if (fd.get() < 0)
return std::unexpected{errno == ENOENT ? ObjectError::missing
: errno == ELOOP ? ObjectError::corrupt
: ObjectError::io};
struct stat info{};
if (::fstat(fd.get(), &info) != 0)
return std::unexpected{ObjectError::io};
// Size is checked before any allocation.
if (!S_ISREG(info.st_mode) || info.st_size < 0 ||
static_cast<std::uint64_t>(info.st_size) != object.bytes)
return std::unexpected{ObjectError::corrupt};
std::vector<std::uint8_t> bytes(static_cast<std::size_t>(object.bytes));
std::size_t done = 0;
while (done < bytes.size()) {
const auto n = ::read(fd.get(), bytes.data() + done, bytes.size() - done);
if (n < 0 && errno == EINTR)
continue;
if (n < 0)
return std::unexpected{ObjectError::io};
if (n == 0)
return std::unexpected{ObjectError::corrupt}; // Shrunk after fstat.
done += static_cast<std::size_t>(n);
}
std::uint8_t extra{};
if (::read(fd.get(), &extra, 1) != 0)
return std::unexpected{ObjectError::corrupt}; // Grew after fstat.
const auto digest = sha256(bytes);
if (!digest)
return std::unexpected{ObjectError::io};
if (*digest != object.sha256)
return std::unexpected{ObjectError::corrupt};
return bytes;
}
} // namespace
FilesystemObjectStore::FilesystemObjectStore(std::filesystem::path directory, int descriptor,
std::size_t maximum)
: directory_(std::move(directory)), descriptor_(descriptor), maximum_(maximum) {}
FilesystemObjectStore::~FilesystemObjectStore() {
static_cast<void>(::close(descriptor_));
}
std::expected<std::unique_ptr<FilesystemObjectStore>, ObjectError>
FilesystemObjectStore::open(const std::filesystem::path& trusted_root, RegionKey key,
std::size_t max_object_bytes) {
if (key.world == 0 || key.region == 0 || max_object_bytes == 0 ||
max_object_bytes > maximum_object_limit || trusted_root.empty())
return std::unexpected{ObjectError::invalid};
const Descriptor root{
::open(trusted_root.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)};
if (root.get() < 0)
return std::unexpected{ObjectError::invalid};
const auto world = "w" + std::to_string(key.world);
const auto region = "r" + std::to_string(key.region);
const Descriptor world_fd{subdirectory(root.get(), world)};
if (world_fd.get() < 0)
return std::unexpected{ObjectError::io};
Descriptor region_fd{subdirectory(world_fd.get(), region)};
if (region_fd.get() < 0)
return std::unexpected{ObjectError::io};
return std::unique_ptr<FilesystemObjectStore>(new FilesystemObjectStore(
trusted_root / world / region, region_fd.release(), max_object_bytes));
}
std::expected<VerifiedObject, ObjectError>
FilesystemObjectStore::put(std::span<const std::uint8_t> bytes) const {
if (bytes.empty())
return std::unexpected{ObjectError::invalid};
if (bytes.size() > maximum_)
return std::unexpected{ObjectError::bounds};
const auto digest = sha256(bytes);
if (!digest)
return std::unexpected{ObjectError::io};
const VerifiedObject object{*digest, bytes.size()};
const auto existing = [&]() -> std::expected<VerifiedObject, ObjectError> {
auto stored = read_verified(descriptor_, object);
if (!stored)
return std::unexpected{stored.error()};
return object;
};
// Immutable: identical content is reused; anything else under the key is corrupt.
if (auto found = existing(); found || found.error() != ObjectError::missing)
return found;
const auto name = key_name(*digest);
const auto temporary = ".tmp-" + name + "-" + std::to_string(::getpid()) + "-" +
std::to_string(temporary_counter.fetch_add(1));
Descriptor fd{::openat(descriptor_, temporary.c_str(),
O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600)};
if (fd.get() < 0)
return std::unexpected{ObjectError::io};
const auto discard = [&] { static_cast<void>(::unlinkat(descriptor_, temporary.c_str(), 0)); };
if (!write_all(fd.get(), bytes) || ::fsync(fd.get()) != 0 || !fd.close()) {
discard();
return std::unexpected{ObjectError::io};
}
// link() never replaces an existing name, unlike rename().
if (::linkat(descriptor_, temporary.c_str(), descriptor_, name.c_str(), 0) != 0) {
const bool raced = errno == EEXIST;
discard();
if (raced)
return existing();
return std::unexpected{ObjectError::io};
}
discard();
if (::fsync(descriptor_) != 0)
return std::unexpected{ObjectError::io};
return object;
}
std::expected<std::vector<std::uint8_t>, ObjectError>
FilesystemObjectStore::get(const VerifiedObject& object) const {
if (object.bytes == 0)
return std::unexpected{ObjectError::invalid};
if (object.bytes > maximum_)
return std::unexpected{ObjectError::bounds};
return read_verified(descriptor_, object);
}
} // namespace evo::persistence