amos 913d3c367f Add the usable task catalog and admit batch 6 tasks
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>
2026-10-18 17:43:15 +08:00

123 lines
5.5 KiB
C++

#include "overlay_internal.hpp"
namespace overlay_relocation {
namespace detail {
bool fragment_targets(const Catalog& base, const Catalog& overlay,
const Nodes& working, std::map<std::string, std::string>& targets) {
for (const std::string& fragment : overlay.fragments) {
const auto& target = *working.at(fragment).begin();
std::string path;
if (target.first == "target-path") {
if (!path_string(target.second, path) || base.ordinary.count(path) == 0) return false;
} else {
const std::uint32_t handle = word(target.second, 0);
if (handle == 0 || handle == 0xffffffffu) return false;
const auto definition = base.handles.find(handle);
if (definition == base.handles.end()) return false;
path = definition->second;
}
targets.emplace(fragment, std::move(path));
}
return true;
}
bool mapped_path(const std::string& source, const std::string& fragment,
const std::string& target, std::string& result) {
const std::size_t prefix_length = fragment.size() + sizeof("/__overlay__") - 1;
if (source.size() < prefix_length) return false;
const std::string_view suffix(source.data() + prefix_length, source.size() - prefix_length);
const std::size_t target_length = target == "/" && !suffix.empty() ? 0 : target.size();
if (target_length > 1024 || suffix.size() > 1024 - target_length) return false;
std::size_t added_depth = 0;
for (char byte : suffix) if (byte == '/') ++added_depth;
if (added_depth > 64 || depth(target) > 64 - added_depth) return false;
result.clear();
result.reserve(target_length + suffix.size());
if (target_length != 0) result.append(target);
result.append(suffix);
return path_valid(result) && !protected_path(result);
}
namespace {
bool merge_payloads(const Catalog& overlay, const Nodes& working,
const std::map<std::string, std::string>& targets, Nodes& result) {
for (const std::string& fragment : overlay.fragments) {
for (const auto& owner : overlay.owner) {
if (owner.second != fragment) continue;
std::string destination;
if (!mapped_path(owner.first, fragment, targets.at(fragment), destination)) return false;
Properties& properties = result[destination];
for (const auto& property : working.at(owner.first)) {
properties[property.first] = property.second;
}
}
}
return true;
}
bool publish_symbols(const Catalog& base, const Catalog& overlay,
const std::map<std::string, std::string>& targets, Nodes& result) {
if (base.symbols.size() > 256 || overlay.symbols.size() > 256 ||
base.symbols.size() + overlay.symbols.size() > 512) return false;
for (const auto& symbol : overlay.symbols) {
if (base.symbols.count(symbol.first) != 0) return false;
const std::string& fragment = overlay.owner.at(symbol.second);
std::string destination;
if (!mapped_path(symbol.second, fragment, targets.begin()->second, destination)) return false;
if (result.count(destination) == 0 || protected_path(destination)) return false;
Bytes bytes(destination.begin(), destination.end());
bytes.push_back(0);
result["/__symbols__"].emplace(symbol.first, std::move(bytes));
}
return true;
}
} // namespace
bool merge(const Catalog& base, const Catalog& overlay, const Nodes& working,
const std::map<std::string, std::string>& targets, Nodes& result) {
result = base.nodes;
return merge_payloads(overlay, working, targets, result) &&
publish_symbols(base, overlay, targets, result) && final_valid(result);
}
bool final_valid(const Nodes& result) {
if (result.empty() || result.size() > 512 || result.count("/") == 0) return false;
Budget budget;
std::set<std::uint32_t> definitions;
for (const auto& node : result) {
if (!path_valid(node.first) || !increase(budget.names, node.first.size(), 131072) ||
!increase(budget.properties, node.second.size(), 4096)) return false;
if (node.first != "/" && result.count(parent_path(node.first)) == 0) return false;
for (const auto& property : node.second) {
if (!property_valid(property.first) || property.second.size() > 65536 ||
!increase(budget.names, property.first.size(), 131072) ||
!increase(budget.values, property.second.size(), 1048576)) return false;
if (node.first != "/__symbols__" && property.first == "phandle") {
if (property.second.size() != 4) return false;
const std::uint32_t handle = word(property.second, 0);
if (handle == 0 || handle == 0xffffffffu || !definitions.insert(handle).second) return false;
}
}
}
const auto symbols = result.find("/__symbols__");
if (symbols != result.end() && symbols->second.size() > 512) return false;
return true;
}
Tree output_tree(const Nodes& result) {
Tree value;
value.nodes.reserve(result.size());
for (const auto& node : result) {
Node output;
output.path = node.first;
output.properties.reserve(node.second.size());
for (const auto& property : node.second) {
output.properties.push_back({property.first, property.second});
}
value.nodes.push_back(std::move(output));
}
return value;
}
} // namespace detail
} // namespace overlay_relocation