#include "reloadable_host/checkpoint.hpp" #include #include #include #include namespace ReloadableHost { namespace { constexpr std::array kMagic{ std::byte{'V'}, std::byte{'C'}, std::byte{'H'}, std::byte{'K'}}; [[nodiscard]] Void::Result::Error invalid(std::string message) { return Void::Result::Error{Void::Result::ErrorKind::corrupt, std::move(message)}; } void append_u32(std::vector& out, std::uint32_t value) { for (int shift = 24; shift >= 0; shift -= 8) { out.push_back(static_cast((value >> shift) & 0xFFU)); } } void append_u64(std::vector& out, std::uint64_t value) { for (int shift = 56; shift >= 0; shift -= 8) { out.push_back(static_cast((value >> shift) & 0xFFU)); } } [[nodiscard]] bool read_u32(std::span in, std::size_t& pos, std::uint32_t& value) noexcept { if (pos + 4 > in.size()) { return false; } value = 0; for (int i = 0; i < 4; ++i) { value = (value << 8) | std::to_integer(in[pos + i]); } pos += 4; return true; } [[nodiscard]] bool read_u64(std::span in, std::size_t& pos, std::uint64_t& value) noexcept { if (pos + 8 > in.size()) { return false; } value = 0; for (int i = 0; i < 8; ++i) { value = (value << 8) | std::to_integer(in[pos + i]); } pos += 8; return true; } } // namespace Void::Result::Result> encode_checkpoint(const CheckpointState& state, CheckpointLimits limits) { if (state.workflow.size() > limits.max_workflow_bytes || state.workflow.size() > 0xFFFFFFFFULL) { return std::unexpected{ invalid("Checkpoint: workflow exceeds the size limit")}; } std::vector out; out.reserve(4 + 4 + 8 + 4 + state.workflow.size() + 8); out.insert(out.end(), kMagic.begin(), kMagic.end()); append_u32(out, kCurrentCheckpointVersion); append_u64(out, state.counter); append_u32(out, static_cast(state.workflow.size())); for (const char ch : state.workflow) { out.push_back(static_cast(ch)); } append_u64(out, state.generation); if (out.size() > limits.max_bytes) { return std::unexpected{ invalid("Checkpoint: encoded state exceeds the size limit")}; } return out; } Void::Result::Result decode_checkpoint(std::span bytes, CheckpointLimits limits) { if (bytes.size() > limits.max_bytes || bytes.size() < kMagic.size() + 4 + 8) { return std::unexpected{ invalid("Checkpoint: truncated or oversized payload")}; } if (!std::equal(kMagic.begin(), kMagic.end(), bytes.begin())) { return std::unexpected{invalid("Checkpoint: bad magic")}; } std::size_t pos = kMagic.size(); std::uint32_t version = 0; if (!read_u32(bytes, pos, version)) { return std::unexpected{invalid("Checkpoint: truncated version")}; } if (version == 0 || version > kCurrentCheckpointVersion) { return std::unexpected{invalid( "Checkpoint: unsupported schema version")}; } CheckpointState state; if (!read_u64(bytes, pos, state.counter)) { return std::unexpected{invalid("Checkpoint: truncated counter")}; } if (version >= 2) { std::uint32_t workflow_size = 0; if (!read_u32(bytes, pos, workflow_size) || workflow_size > limits.max_workflow_bytes || pos + workflow_size > bytes.size()) { return std::unexpected{ invalid("Checkpoint: invalid workflow payload")}; } state.workflow.assign( reinterpret_cast(bytes.data() + pos), workflow_size); pos += workflow_size; } if (version >= 3 && !read_u64(bytes, pos, state.generation)) { return std::unexpected{ invalid("Checkpoint: truncated generation")}; } if (pos != bytes.size()) { return std::unexpected{ invalid("Checkpoint: trailing bytes after payload")}; } return state; } } // namespace ReloadableHost