All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
6.9 KiB
C++
153 lines
6.9 KiB
C++
#include "overlay_internal.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <limits>
|
|
|
|
namespace overlay_relocation {
|
|
namespace detail {
|
|
namespace {
|
|
using Position = std::tuple<std::string, std::string, std::uint32_t>;
|
|
|
|
const Bytes* target_property(const Catalog& overlay, const std::string& path,
|
|
const std::string& property) {
|
|
const auto node = overlay.nodes.find(path);
|
|
if (node == overlay.nodes.end()) return nullptr;
|
|
const auto found = node->second.find(property);
|
|
return found == node->second.end() ? nullptr : &found->second;
|
|
}
|
|
|
|
bool valid_position(const Bytes& bytes, std::uint32_t offset) {
|
|
return offset % 4 == 0 && offset <= bytes.size() && bytes.size() - offset >= 4;
|
|
}
|
|
|
|
bool add_patch(std::vector<Patch>& patches, std::set<Position>& positions, Patch patch) {
|
|
if (patches.size() >= 4096) return false;
|
|
if (!positions.emplace(patch.path, patch.property, patch.offset).second) return false;
|
|
patches.push_back(std::move(patch));
|
|
return true;
|
|
}
|
|
|
|
bool decimal_offset(std::string_view text, std::uint32_t& value) {
|
|
if (text.empty() || text.size() > 10) return false;
|
|
std::uint64_t number = 0;
|
|
for (unsigned char byte : text) {
|
|
if (byte < '0' || byte > '9') return false;
|
|
number = number * 10 + (byte - '0');
|
|
if (number > std::numeric_limits<std::uint32_t>::max()) return false;
|
|
}
|
|
value = static_cast<std::uint32_t>(number);
|
|
return true;
|
|
}
|
|
|
|
bool parse_location(const Bytes& bytes, std::size_t start, std::size_t end, Patch& patch) {
|
|
const char* begin = reinterpret_cast<const char*>(bytes.data()) + start;
|
|
const std::string_view text(begin, end - start);
|
|
const std::size_t first = text.find(':');
|
|
if (first == std::string_view::npos) return false;
|
|
const std::size_t second = text.find(':', first + 1);
|
|
if (second == std::string_view::npos || text.find(':', second + 1) != std::string_view::npos) {
|
|
return false;
|
|
}
|
|
const std::string_view path = text.substr(0, first);
|
|
const std::string_view property = text.substr(first + 1, second - first - 1);
|
|
if (!path_valid(path) || !property_valid(property) || property == "phandle" ||
|
|
!decimal_offset(text.substr(second + 1), patch.offset)) return false;
|
|
patch.path.assign(path);
|
|
patch.property.assign(property);
|
|
return true;
|
|
}
|
|
|
|
bool local_patches(const Catalog& overlay, std::vector<Patch>& patches,
|
|
std::set<Position>& positions) {
|
|
const std::string mirror = "/__local_fixups__";
|
|
for (const auto& node : overlay.nodes) {
|
|
if (!within(node.first, mirror)) continue;
|
|
const std::string target = node.first == mirror ? "/" : node.first.substr(mirror.size());
|
|
if (overlay.nodes.count(target) == 0) return false;
|
|
if (!node.second.empty() && overlay.ordinary.count(target) == 0) return false;
|
|
for (const auto& property : node.second) {
|
|
if (property.first == "phandle" || property.second.size() % 4 != 0) return false;
|
|
const Bytes* original = target_property(overlay, target, property.first);
|
|
if (original == nullptr) return false;
|
|
for (std::size_t i = 0; i < property.second.size(); i += 4) {
|
|
const std::uint32_t offset = word(property.second, i);
|
|
if (!valid_position(*original, offset)) return false;
|
|
const std::uint32_t handle = word(*original, offset);
|
|
if (overlay.handles.count(handle) == 0) return false;
|
|
if (!add_patch(patches, positions, {target, property.first, offset, handle, {}})) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool external_patches(const Catalog& overlay, std::vector<Patch>& patches,
|
|
std::set<Position>& positions) {
|
|
const auto metadata = overlay.nodes.find("/__fixups__");
|
|
if (metadata == overlay.nodes.end()) return true;
|
|
for (const auto& property : metadata->second) {
|
|
const Bytes& list = property.second;
|
|
if (list.empty() || list.back() != 0) return false;
|
|
std::size_t start = 0;
|
|
while (start < list.size()) {
|
|
const auto terminator = std::find(list.begin() + static_cast<std::ptrdiff_t>(start), list.end(), 0);
|
|
const std::size_t end = static_cast<std::size_t>(terminator - list.begin());
|
|
if (end == start || end == list.size()) return false;
|
|
Patch patch;
|
|
if (!parse_location(list, start, end, patch)) return false;
|
|
const bool payload = overlay.ordinary.count(patch.path) != 0;
|
|
const bool fragment_target = patch.property == "target" &&
|
|
std::binary_search(overlay.fragments.begin(), overlay.fragments.end(), patch.path);
|
|
if (!payload && !fragment_target) return false;
|
|
const Bytes* original = target_property(overlay, patch.path, patch.property);
|
|
if (original == nullptr || !valid_position(*original, patch.offset)) return false;
|
|
patch.original = word(*original, patch.offset);
|
|
if (patch.original != 0xffffffffu) return false;
|
|
patch.symbol = property.first;
|
|
if (!add_patch(patches, positions, std::move(patch))) return false;
|
|
start = end + 1;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
bool collect_patches(const Catalog& overlay, std::vector<Patch>& patches) {
|
|
std::set<Position> positions;
|
|
return local_patches(overlay, patches, positions) && external_patches(overlay, patches, positions);
|
|
}
|
|
|
|
bool relocate(const Catalog& base, const Catalog& overlay,
|
|
const std::vector<Patch>& patches, Nodes& working) {
|
|
const std::uint64_t maximum = base.handles.empty() ? 0 : base.handles.rbegin()->first;
|
|
const std::uint64_t delta = maximum + 1;
|
|
for (const auto& definition : overlay.handles) {
|
|
if (definition.first + delta > 0xfffffffeull) return false;
|
|
}
|
|
working = overlay.nodes;
|
|
for (const auto& definition : overlay.handles) {
|
|
Bytes& bytes = working.at(definition.second).at("phandle");
|
|
write_word(bytes, 0, static_cast<std::uint32_t>(definition.first + delta));
|
|
}
|
|
for (const Patch& patch : patches) {
|
|
if (!patch.symbol.empty()) continue;
|
|
Bytes& bytes = working.at(patch.path).at(patch.property);
|
|
write_word(bytes, patch.offset, static_cast<std::uint32_t>(patch.original + delta));
|
|
}
|
|
for (const Patch& patch : patches) {
|
|
if (patch.symbol.empty()) continue;
|
|
const auto symbol = base.symbols.find(patch.symbol);
|
|
if (symbol == base.symbols.end()) return false;
|
|
const Properties& properties = base.nodes.at(symbol->second);
|
|
const auto definition = properties.find("phandle");
|
|
if (definition == properties.end()) return false;
|
|
Bytes& bytes = working.at(patch.path).at(patch.property);
|
|
write_word(bytes, patch.offset, word(definition->second, 0));
|
|
}
|
|
return true;
|
|
}
|
|
} // namespace detail
|
|
} // namespace overlay_relocation
|