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

214 lines
8.8 KiB
C++

#include "overlay_internal.hpp"
#include <algorithm>
namespace overlay_relocation {
namespace detail {
namespace {
bool alphanumeric(unsigned char byte) {
return (byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') ||
(byte >= '0' && byte <= '9');
}
bool node_character(unsigned char byte) {
return alphanumeric(byte) || byte == ',' || byte == '.' || byte == '_' ||
byte == '+' || byte == '@' || byte == '-';
}
bool fragment_name(const std::string& path) {
const std::string prefix = "/fragment@";
if (path.compare(0, prefix.size(), prefix) != 0) return false;
const std::size_t digits = path.size() - prefix.size();
if (digits == 0 || digits > 10) return false;
return std::all_of(path.begin() + static_cast<std::ptrdiff_t>(prefix.size()), path.end(),
[](unsigned char byte) { return byte >= '0' && byte <= '9'; });
}
bool collect_handles(Catalog& catalog) {
for (auto item = catalog.ordinary.rbegin(); item != catalog.ordinary.rend(); ++item) {
const std::string& path = *item;
const Properties& properties = catalog.nodes.at(path);
const auto found = properties.find("phandle");
if (found == properties.end()) continue;
if (found->second.size() != 4) return false;
const std::uint32_t handle = word(found->second, 0);
if (handle == 0 || handle == 0xffffffffu) return false;
if (!catalog.handles.emplace(handle, path).second) return false;
}
return true;
}
bool read_symbols(Catalog& catalog, bool exports) {
const auto found = catalog.nodes.find("/__symbols__");
if (found == catalog.nodes.end()) return true;
if (found->second.size() > 256) return false;
for (const auto& property : found->second) {
std::string path;
if (!symbol_valid(property.first) || !path_string(property.second, path)) return false;
if (catalog.ordinary.count(path) == 0) return false;
if (exports && catalog.owner.count(path) == 0) return false;
catalog.symbols.emplace(property.first, std::move(path));
}
return true;
}
} // namespace
bool increase(std::size_t& count, std::size_t amount, std::size_t limit) {
if (amount > limit || count > limit - amount) return false;
count += amount;
return true;
}
std::size_t depth(std::string_view path) {
if (path == "/") return 1;
return 1 + static_cast<std::size_t>(std::count(path.begin(), path.end(), '/'));
}
bool path_valid(std::string_view path) {
if (path.empty() || path.size() > 1024 || path.front() != '/') return false;
if (path == "/") return true;
if (path.back() == '/' || depth(path) > 64) return false;
std::size_t start = 1;
while (start < path.size()) {
const std::size_t end = path.find('/', start);
const std::size_t length = (end == std::string_view::npos ? path.size() : end) - start;
if (length == 0 || length > 63) return false;
const std::string_view component = path.substr(start, length);
if (component == "." || component == "..") return false;
for (unsigned char byte : component) if (!node_character(byte)) return false;
if (end == std::string_view::npos) break;
start = end + 1;
}
return true;
}
bool property_valid(std::string_view name) {
if (name.empty() || name.size() > 63) return false;
for (unsigned char byte : name) {
if (!alphanumeric(byte) && byte != ',' && byte != '.' && byte != '_' &&
byte != '+' && byte != '?' && byte != '#' && byte != '-') return false;
}
return true;
}
bool symbol_valid(std::string_view name) {
if (name.empty() || name.size() > 63) return false;
const unsigned char first = static_cast<unsigned char>(name.front());
if (!((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_')) {
return false;
}
for (unsigned char byte : name) if (!alphanumeric(byte) && byte != '_') return false;
return true;
}
bool within(std::string_view path, std::string_view root) {
return path == root || (path.size() > root.size() &&
path.compare(0, root.size(), root) == 0 && path[root.size()] == '/');
}
bool protected_path(std::string_view path) {
return within(path, "/__symbols__") || within(path, "/__fixups__") ||
within(path, "/__local_fixups__");
}
std::string parent_path(const std::string& path) {
const std::size_t slash = path.rfind('/');
return slash == 0 ? "/" : path.substr(0, slash);
}
std::uint32_t word(const Bytes& bytes, std::size_t offset) {
std::uint32_t result = 0;
for (std::size_t i = 0; i < 4; ++i) result = (result << 8) | bytes[offset + i];
return result;
}
void write_word(Bytes& bytes, std::size_t offset, std::uint32_t value) {
for (unsigned i = 0; i < 4; ++i) bytes[offset + i] = static_cast<std::uint8_t>(value >> (24 - i * 8));
}
bool path_string(const Bytes& bytes, std::string& result) {
if (bytes.size() < 2 || bytes.size() > 1025 || bytes.back() != 0) return false;
if (std::find(bytes.begin(), bytes.end() - 1, 0) != bytes.end() - 1) return false;
const std::string_view view(reinterpret_cast<const char*>(bytes.data()), bytes.size() - 1);
if (!path_valid(view)) return false;
result.assign(view);
return true;
}
bool input_tree(const Tree& input, Catalog& result, Budget& combined) {
if (input.nodes.empty() || input.nodes.size() > 512 ||
!increase(combined.nodes, input.nodes.size(), 768)) return false;
for (const Node& node : input.nodes) {
if (!path_valid(node.path) || !increase(combined.names, node.path.size(), 131072)) return false;
if (!increase(combined.properties, node.properties.size(), 4096)) return false;
Properties properties;
for (const Property& property : node.properties) {
if (!property_valid(property.name) || property.value.size() > 65536 ||
!increase(combined.names, property.name.size(), 131072) ||
!increase(combined.values, property.value.size(), 1048576)) return false;
if (!properties.emplace(property.name, property.value).second) return false;
}
if (!result.nodes.emplace(node.path, std::move(properties)).second) return false;
}
if (result.nodes.count("/") != 1) return false;
for (const auto& node : result.nodes) {
if (node.first != "/" && result.nodes.count(parent_path(node.first)) == 0) return false;
}
return true;
}
bool base_catalog(Catalog& catalog) {
for (const auto& node : catalog.nodes) {
if (within(node.first, "/__fixups__") || within(node.first, "/__local_fixups__")) return false;
if (within(node.first, "/__symbols__")) {
if (node.first != "/__symbols__") return false;
} else {
catalog.ordinary.insert(node.first);
}
}
return collect_handles(catalog) && read_symbols(catalog, false);
}
bool overlay_catalog(Catalog& catalog) {
if (catalog.nodes.at("/").count("phandle") != 0) return false;
for (const auto& node : catalog.nodes) {
if (node.first == "/" || parent_path(node.first) != "/") continue;
if (node.first == "/__symbols__" || node.first == "/__fixups__" ||
node.first == "/__local_fixups__") continue;
if (!fragment_name(node.first)) return false;
catalog.fragments.push_back(node.first);
}
for (const std::string& fragment : catalog.fragments) {
const Properties& properties = catalog.nodes.at(fragment);
if (properties.size() != 1) return false;
const auto& target = *properties.begin();
if (target.first == "target") {
if (target.second.size() != 4) return false;
} else if (target.first == "target-path") {
std::string path;
if (!path_string(target.second, path)) return false;
} else {
return false;
}
const std::string payload = fragment + "/__overlay__";
if (catalog.nodes.count(payload) == 0) return false;
for (const auto& node : catalog.nodes) {
if (parent_path(node.first) == fragment && node.first != payload) return false;
if (within(node.first, payload)) {
catalog.ordinary.insert(node.first);
catalog.owner.emplace(node.first, fragment);
}
}
}
for (const auto& node : catalog.nodes) {
if ((within(node.first, "/__symbols__") && node.first != "/__symbols__") ||
(within(node.first, "/__fixups__") && node.first != "/__fixups__")) return false;
if (node.first == "/__fixups__") {
for (const auto& property : node.second) if (!symbol_valid(property.first)) return false;
}
}
return collect_handles(catalog) && read_symbols(catalog, true);
}
} // namespace detail
} // namespace overlay_relocation