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>
657 lines
31 KiB
Python
657 lines
31 KiB
Python
"""Private recursive tree oracle; candidate code is only built/run in a child."""
|
||
import base64
|
||
import copy
|
||
import hashlib
|
||
import json
|
||
from pathlib import Path
|
||
import random
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
|
||
CANDIDATES = ['overlay_catalog.cpp', 'overlay_fixups.cpp', 'overlay_merge.cpp',
|
||
'overlay_apply.cpp', 'overlay_internal.hpp']
|
||
CPP = CANDIDATES[:4]
|
||
COMPONENT = re.compile(rb'[A-Za-z0-9,._+@-]{1,63}\Z')
|
||
PROPERTY = re.compile(rb'[A-Za-z0-9,._+?#-]{1,63}\Z')
|
||
SYMBOL = re.compile(rb'[A-Za-z_][A-Za-z0-9_]*\Z')
|
||
FRAGMENT = re.compile(rb'fragment@[0-9]{1,10}\Z')
|
||
|
||
|
||
class Invalid(Exception):
|
||
pass
|
||
|
||
|
||
def need(condition):
|
||
if not condition:
|
||
raise Invalid()
|
||
|
||
|
||
def path_ok(path):
|
||
if path == b'/':
|
||
return True
|
||
parts = path[1:].split(b'/')
|
||
return (1 <= len(path) <= 1024 and path.startswith(b'/') and len(parts) < 64 and
|
||
all(COMPONENT.fullmatch(part) and part not in (b'.', b'..') for part in parts))
|
||
|
||
|
||
def parent(path):
|
||
return path.rsplit(b'/', 1)[0] or b'/'
|
||
|
||
|
||
def descendants(path, root):
|
||
return path == root or path.startswith(root + b'/')
|
||
|
||
|
||
def protected(path):
|
||
return any(descendants(path, prefix) for prefix in (b'/__symbols__', b'/__fixups__', b'/__local_fixups__'))
|
||
|
||
|
||
def string_path(value):
|
||
need(value.endswith(b'\0') and b'\0' not in value[:-1] and path_ok(value[:-1]))
|
||
return value[:-1]
|
||
|
||
|
||
def u32(value):
|
||
return value.to_bytes(4, 'big')
|
||
|
||
|
||
def cell(value, offset=0):
|
||
need(offset % 4 == 0 and 0 <= offset <= len(value) - 4)
|
||
return int.from_bytes(value[offset:offset + 4], 'big')
|
||
|
||
|
||
class Node:
|
||
def __init__(self, attributes):
|
||
self.attributes = attributes
|
||
self.children = {}
|
||
|
||
|
||
def flatten(root):
|
||
result = {}
|
||
|
||
def visit(node, path):
|
||
result[path] = node
|
||
for name, child in node.children.items():
|
||
visit(child, (b'' if path == b'/' else path) + b'/' + name)
|
||
visit(root, b'/')
|
||
return result
|
||
|
||
|
||
def load(value):
|
||
need(1 <= len(value) <= 512)
|
||
indexed = {}
|
||
for path, properties in value:
|
||
need(path_ok(path) and path not in indexed)
|
||
attributes = {}
|
||
for name, data in properties:
|
||
need(PROPERTY.fullmatch(name) and name not in attributes and len(data) <= 65536)
|
||
attributes[name] = data
|
||
indexed[path] = Node(attributes)
|
||
need(b'/' in indexed)
|
||
for path, node in indexed.items():
|
||
if path == b'/':
|
||
continue
|
||
need(parent(path) in indexed)
|
||
indexed[parent(path)].children[path.rsplit(b'/', 1)[1]] = node
|
||
return indexed[b'/'], indexed
|
||
|
||
|
||
def totals(*values):
|
||
nodes = properties = names = data = 0
|
||
for value in values:
|
||
nodes += len(value)
|
||
for path, props in value:
|
||
names += len(path)
|
||
properties += len(props)
|
||
names += sum(len(key) for key, _ in props)
|
||
data += sum(len(raw) for _, raw in props)
|
||
need(properties <= 4096 and names <= 131072 and data <= 1048576)
|
||
return nodes
|
||
|
||
|
||
def handles(index, ordinary):
|
||
result = {}
|
||
for path in ordinary:
|
||
attributes = index[path].attributes
|
||
if b'phandle' not in attributes:
|
||
continue
|
||
raw = attributes[b'phandle']
|
||
need(len(raw) == 4)
|
||
handle = cell(raw)
|
||
need(0 < handle < 0xffffffff and handle not in result)
|
||
result[handle] = path
|
||
return result
|
||
|
||
|
||
def symbols(index, ordinary):
|
||
if b'/__symbols__' not in index:
|
||
return {}
|
||
attributes = index[b'/__symbols__'].attributes
|
||
need(len(attributes) <= 256)
|
||
result = {}
|
||
for name, raw in attributes.items():
|
||
need(SYMBOL.fullmatch(name))
|
||
path = string_path(raw)
|
||
need(path in ordinary)
|
||
result[name] = path
|
||
return result
|
||
|
||
|
||
def oracle(base_value, overlay_value):
|
||
try:
|
||
need(totals(base_value, overlay_value) <= 768)
|
||
base, b = load(base_value)
|
||
overlay, o = load(overlay_value)
|
||
for path in b:
|
||
need(not descendants(path, b'/__fixups__') and not descendants(path, b'/__local_fixups__'))
|
||
need(not descendants(path, b'/__symbols__') or path == b'/__symbols__')
|
||
ordinary_base = set(b) - {b'/__symbols__'}
|
||
base_handles = handles(b, ordinary_base)
|
||
base_symbols = symbols(b, ordinary_base)
|
||
need(b'phandle' not in overlay.attributes)
|
||
fragments, owners = {}, {}
|
||
for name, node in overlay.children.items():
|
||
if name in (b'__symbols__', b'__fixups__'):
|
||
need(not node.children and all(SYMBOL.fullmatch(key) for key in node.attributes))
|
||
elif name != b'__local_fixups__':
|
||
need(FRAGMENT.fullmatch(name) and set(node.children) == {b'__overlay__'})
|
||
need(set(node.attributes) in ({b'target'}, {b'target-path'}))
|
||
if b'target' in node.attributes:
|
||
need(len(node.attributes[b'target']) == 4)
|
||
else:
|
||
string_path(node.attributes[b'target-path'])
|
||
fragment = b'/' + name
|
||
fragments[fragment] = node
|
||
payload = fragment + b'/__overlay__'
|
||
for path in o:
|
||
if descendants(path, payload):
|
||
owners[path] = fragment
|
||
local_handles = handles(o, owners)
|
||
exports = symbols(o, owners)
|
||
patches, occupied = [], set()
|
||
|
||
def admit(path, name, offset, symbol=None):
|
||
need(path in o and name != b'phandle' and name in o[path].attributes)
|
||
raw = o[path].attributes[name]
|
||
old = cell(raw, offset)
|
||
need(old in local_handles if symbol is None else old == 0xffffffff)
|
||
position = (path, name, offset)
|
||
need(position not in occupied and len(occupied) < 4096)
|
||
occupied.add(position)
|
||
patches.append((position, old, symbol))
|
||
|
||
mirror = b'/__local_fixups__'
|
||
for path, node in o.items():
|
||
if not descendants(path, mirror):
|
||
continue
|
||
target = path[len(mirror):] or b'/'
|
||
need(target in o and (target in owners or not node.attributes))
|
||
for name, raw in node.attributes.items():
|
||
need(name != b'phandle' and name in o[target].attributes and len(raw) % 4 == 0)
|
||
for at in range(0, len(raw), 4):
|
||
admit(target, name, cell(raw, at))
|
||
if b'/__fixups__' in o:
|
||
for symbol, raw in o[b'/__fixups__'].attributes.items():
|
||
need(raw.endswith(b'\0'))
|
||
locations = raw[:-1].split(b'\0')
|
||
for location in locations:
|
||
fields = location.split(b':')
|
||
need(len(fields) == 3)
|
||
path, name, number = fields
|
||
need(path_ok(path) and PROPERTY.fullmatch(name))
|
||
need(re.fullmatch(rb'[0-9]{1,10}', number) and int(number) <= 0xffffffff)
|
||
need(path in owners or (path in fragments and name == b'target' and name in o[path].attributes))
|
||
admit(path, name, int(number), symbol)
|
||
delta = max(base_handles, default=0) + 1
|
||
need(all(handle + delta <= 0xfffffffe for handle in local_handles))
|
||
rewritten = flatten(copy.deepcopy(overlay))
|
||
for handle, path in local_handles.items():
|
||
rewritten[path].attributes[b'phandle'] = u32(handle + delta)
|
||
for (path, name, offset), old, symbol in patches:
|
||
if symbol is None:
|
||
replacement = old + delta
|
||
else:
|
||
need(symbol in base_symbols)
|
||
target = b[base_symbols[symbol]].attributes
|
||
need(b'phandle' in target)
|
||
replacement = cell(target[b'phandle'])
|
||
raw = rewritten[path].attributes[name]
|
||
rewritten[path].attributes[name] = raw[:offset] + u32(replacement) + raw[offset + 4:]
|
||
targets = {}
|
||
for fragment in fragments:
|
||
props = rewritten[fragment].attributes
|
||
if b'target-path' in props:
|
||
target = string_path(props[b'target-path'])
|
||
else:
|
||
handle = cell(props[b'target'])
|
||
need(handle in base_handles)
|
||
target = base_handles[handle]
|
||
need(target in ordinary_base)
|
||
targets[fragment] = target
|
||
mapped = {}
|
||
for source, fragment in owners.items():
|
||
suffix = source[len(fragment) + len(b'/__overlay__'):]
|
||
target = targets[fragment]
|
||
prefix = b'' if target == b'/' and suffix else target
|
||
need(len(prefix) + len(suffix) <= 1024)
|
||
need((0 if target == b'/' else target.count(b'/')) + suffix.count(b'/') < 64)
|
||
destination = prefix + suffix
|
||
need(path_ok(destination) and not protected(destination))
|
||
mapped[source] = destination
|
||
result = copy.deepcopy(base)
|
||
result_index = flatten(result)
|
||
|
||
def recursive_merge(destination, source):
|
||
destination.attributes.update(source.attributes)
|
||
for name, child in source.children.items():
|
||
if name in destination.children:
|
||
recursive_merge(destination.children[name], child)
|
||
else:
|
||
destination.children[name] = copy.deepcopy(child)
|
||
for fragment in sorted(fragments):
|
||
recursive_merge(result_index[targets[fragment]], rewritten[fragment + b'/__overlay__'])
|
||
result_index = flatten(result)
|
||
for name, source in exports.items():
|
||
need(name not in base_symbols and mapped[source] in result_index and not protected(mapped[source]))
|
||
if b'__symbols__' not in result.children:
|
||
result.children[b'__symbols__'] = Node({})
|
||
result.children[b'__symbols__'].attributes[name] = mapped[source] + b'\0'
|
||
final = flatten(result)
|
||
need(len(final) <= 512)
|
||
values = [(path, list(node.attributes.items())) for path, node in final.items()]
|
||
totals(values)
|
||
handles(final, set(final) - {b'/__symbols__'})
|
||
return [[path.hex(), [[name.hex(), raw.hex()] for name, raw in sorted(node.attributes.items())]]
|
||
for path, node in sorted(final.items())]
|
||
except Invalid:
|
||
return None
|
||
|
||
|
||
BASIS = {
|
||
'C01': [1, 5, 16, 17, 34, 37], 'C02': [1, 10, 11, 12, 15, 16, 17, 37],
|
||
'C03': [1, 6, 13, 14, 15, 18, 25, 37], 'C04': [1, 9, 26, 30, 31, 32, 37],
|
||
'C05': [1, 2, 3, 4, 22, 33, 37], 'C06': [1, 5, 6, 21, 22, 37],
|
||
'C07': [1, 7, 8, 9, 10, 19, 22, 37], 'C08': [1, 5, 16, 17, 20, 29, 37],
|
||
'C09': [1, 10, 11, 12, 13, 14, 15, 19, 37], 'C10': [1, 20, 23, 28, 33, 37],
|
||
'C11': [1, 18, 19, 24, 25, 37], 'C12': [1, 7, 21, 26, 27, 28, 29, 37],
|
||
'C13': [1, 3, 4, 15, 16, 23, 26, 31, 32, 35, 37], 'C14': [1, 7, 27, 33, 37],
|
||
'C15': [1, 34, 37], 'C16': [1, 34, 37], 'C17': [1, 16, 17, 18, 26, 27, 30, 33, 34, 37],
|
||
'C18': [36, 37],
|
||
}
|
||
|
||
|
||
def tree(mapping):
|
||
return [(path, list(properties.items())) for path, properties in mapping.items()]
|
||
|
||
|
||
def fragment(target=b'/', attributes=None):
|
||
return tree({b'/': {}, b'/fragment@0': {b'target-path': target + b'\0'},
|
||
b'/fragment@0/__overlay__': attributes or {}})
|
||
|
||
|
||
def attributes(value, path):
|
||
return next(properties for name, properties in value if name == path)
|
||
|
||
|
||
def set_property(value, path, name, raw):
|
||
properties = attributes(value, path)
|
||
properties[:] = [(key, data) for key, data in properties if key != name]
|
||
properties.append((name, raw))
|
||
|
||
|
||
class Cases:
|
||
def __init__(self):
|
||
self.commands, self.expected, self.labels, self.origins = [], [], [], []
|
||
self.held, self.last, self.calls = {}, [], 0
|
||
|
||
def observe(self, expected, label, origin):
|
||
self.expected.append(expected)
|
||
self.labels.append(label)
|
||
self.origins.append(dict(origin, wire_index=len(self.commands) - 1))
|
||
|
||
def r(self, base, overlay, label, alias=None, bank=0):
|
||
self.commands.extend(['A %d' % bank, 'N'])
|
||
for which, value in enumerate((base, overlay)):
|
||
for index, (path, props) in enumerate(value):
|
||
self.commands.append('F %d %s' % (which, path.hex() or '-'))
|
||
for name, raw in props:
|
||
self.commands.append('P %d %d %s %s' % (which, index, name.hex() or '-', raw.hex() or '-'))
|
||
self.calls += 1
|
||
if alias is None:
|
||
operation, inputs = 'R', (base, overlay)
|
||
else:
|
||
operation, inputs = 'Y', ((base, overlay)[alias],) * 2
|
||
self.commands.append(operation if alias is None else 'Y %d' % alias)
|
||
value = oracle(*inputs)
|
||
self.last = [] if value is None else value
|
||
serialized = json.dumps([[[path.hex(), [[name.hex(), raw.hex()] for name, raw in props]]
|
||
for path, props in input_tree] for input_tree in inputs], separators=(',', ':'))
|
||
self.observe([operation, -22 if value is None else 0, copy.deepcopy(self.last), True], label,
|
||
{'operation': operation, 'bank': bank, 'alias_which': alias,
|
||
'input_sha256': hashlib.sha256(serialized.encode()).hexdigest()})
|
||
|
||
def hold(self, slot):
|
||
self.commands.append('H %d' % slot)
|
||
self.held[slot] = copy.deepcopy(self.last)
|
||
|
||
def saved(self, slot, mutate=False):
|
||
original = copy.deepcopy(self.held[slot])
|
||
self.commands.append(('M' if mutate else 'J') + ' %d' % slot)
|
||
if mutate:
|
||
changed = copy.deepcopy(original)
|
||
if changed:
|
||
changed[0][0] = b'/copy'.hex()
|
||
if changed[0][1]:
|
||
changed[0][1][0][1] = '00ff'
|
||
changed[0][1].append([b'COPY'.hex(), '0102'])
|
||
changed.append([b'/new'.hex(), [[b'NEW'.hex(), 'ff00']]])
|
||
expected = ['M', original, changed, copy.deepcopy(original)]
|
||
else:
|
||
expected = ['J', original]
|
||
self.observe(expected, 'C15', {'operation': self.commands[-1], 'held': slot})
|
||
|
||
|
||
def witnesses():
|
||
a = (tree({b'/': {}, b'/outer': {b'phandle': u32(2)}, b'/outer/deep': {b'phandle': u32(40)}}),
|
||
fragment(b'/outer') + [(b'/fragment@0/__overlay__/new', [(b'phandle', u32(1))])])
|
||
local = fragment(b'/', {b'refs': bytes.fromhex('aabbccdd0000000111223344')})
|
||
local.extend([(b'/fragment@0/__overlay__/x', [(b'phandle', u32(1))]),
|
||
(b'/__local_fixups__', []), (b'/__local_fixups__/fragment@0', []),
|
||
(b'/__local_fixups__/fragment@0/__overlay__', [(b'refs', u32(4))])])
|
||
b = (tree({b'/': {b'phandle': u32(2)}}), local)
|
||
base = tree({b'/': {}, b'/ctrl': {b'phandle': u32(10)}, b'/__symbols__': {b'ctrl': b'/ctrl\0'}})
|
||
external = fragment(b'/', {b'refs': b'\xff' * 8})
|
||
external.append((b'/__fixups__', [(b'ctrl', b'/fragment@0/__overlay__:refs:0\0/fragment@0/__overlay__:refs:4\0')]))
|
||
c = (base, external)
|
||
multi = fragment(b'/bus1') + [(b'/fragment@0/__overlay__/alpha', []),
|
||
(b'/fragment@1', [(b'target-path', b'/bus2\0')]), (b'/fragment@1/__overlay__', []),
|
||
(b'/fragment@1/__overlay__/beta', []),
|
||
(b'/__symbols__', [(b'B', b'/fragment@1/__overlay__/beta\0')])]
|
||
d = (tree({b'/': {}, b'/bus1': {}, b'/bus2': {}}), multi)
|
||
return [a, b, c, d]
|
||
|
||
|
||
def cases():
|
||
c, root = Cases(), tree({b'/': {}})
|
||
roots = witnesses()
|
||
for index, (base, overlay) in enumerate(roots):
|
||
c.r(base, overlay, 'C%02d' % (index + 1))
|
||
c.r(root, root, 'C05'); c.hold(0)
|
||
c.r(tree({b'/z': {b'opaque': b'\0\xff'}, b'/': {b'empty': b''}}), root, 'C10')
|
||
c.r(tree({b'/': {}, b'/__symbols__': {}}), root, 'C06')
|
||
for bad in [b'', b'relative', b'//a', b'/a/', b'/a//b', b'/./a', b'/a/../b', b'/bad:name', b'/\0', b'/\xff', b'/' + b'a' * 64]:
|
||
c.r(root + [(bad, [])], root, 'C05')
|
||
c.r([], root, 'C05'); c.r(root, [], 'C05')
|
||
c.r(root + [(b'/missing/child', [])], root, 'C05')
|
||
c.r(root + [(b'/', [])], root, 'C05')
|
||
for bad in [b'', b'a' * 64, b'a@b', b'a:b', b'\0', b'\xff']:
|
||
c.r([(b'/', [(bad, b'opaque')])], root, 'C05')
|
||
c.r([(b'/', [(b'p', b'1'), (b'p', b'2')])], root, 'C05')
|
||
for raw in [b'', b'\0', u32(0), u32(0xffffffff), b'\x01' * 5]:
|
||
c.r(tree({b'/': {b'phandle': raw}}), root, 'C08')
|
||
c.r(root, fragment(attributes={b'phandle': raw}), 'C08')
|
||
c.r(tree({b'/': {b'phandle': u32(1)}, b'/x': {b'phandle': u32(1)}}), root, 'C08')
|
||
for raw in [b'/missing\0', b'/\0\0', b'/', b'\0', b'/__symbols__\0']:
|
||
c.r(tree({b'/': {}, b'/__symbols__': {b's': raw}}), root, 'C06')
|
||
for path in [b'/__symbols__/child', b'/__fixups__', b'/__local_fixups__']:
|
||
base = tree({b'/': {}, b'/__symbols__': {}}) if path.endswith(b'/child') else copy.deepcopy(root)
|
||
c.r(base + [(path, [])], root, 'C06')
|
||
c.r(tree({b'/': {}, b'/__symbols__': {b'phandle': b'/\0'}}), root, 'C06')
|
||
c.r(tree({b'/': {b'linux,phandle': b'\0\xff'}}), root, 'C10')
|
||
for name in [b'random', b'fragment@', b'fragment@x', b'fragment@12345678901']:
|
||
c.r(root, root + [(b'/' + name, [])], 'C07')
|
||
c.r(root, tree({b'/': {b'phandle': u32(1)}}), 'C07')
|
||
c.r(root, [(b'/', []), (b'/fragment@0', [(b'target-path', b'/\0')])], 'C07')
|
||
overlay = fragment() + [(b'/fragment@0/other', [])]
|
||
c.r(root, overlay, 'C07')
|
||
for props in [[], [(b'extra', b'')], [(b'target', u32(1)), (b'target-path', b'/\0')], [(b'target', b'\0')]]:
|
||
overlay = fragment(); overlay[1] = (overlay[1][0], props)
|
||
c.r(root, overlay, 'C07')
|
||
for raw in [b'/', b'/\0\0', b'missing\0', b'/bad/\0']:
|
||
overlay = fragment(); set_property(overlay, b'/fragment@0', b'target-path', raw)
|
||
c.r(root, overlay, 'C07')
|
||
for meta in [b'__symbols__', b'__fixups__']:
|
||
c.r(root, fragment() + [(b'/' + meta, []), (b'/' + meta + b'/child', [])], 'C07')
|
||
c.r(root, fragment() + [(b'/' + meta, [(b'9bad', b'/\0')])], 'C07')
|
||
for target in [b'/fragment@0', b'/', b'/missing']:
|
||
c.r(root, fragment() + [(b'/__symbols__', [(b'S', target + b'\0')])], 'C07')
|
||
local_base, local = roots[1]
|
||
for raw in [b'\0', u32(1), u32(8), u32(12), u32(0xfffffffc), u32(4) * 2]:
|
||
value = copy.deepcopy(local)
|
||
set_property(value, b'/__local_fixups__/fragment@0/__overlay__', b'refs', raw)
|
||
c.r(local_base, value, 'C09')
|
||
value = copy.deepcopy(local)
|
||
set_property(value, b'/__local_fixups__/fragment@0/__overlay__', b'missing', b'')
|
||
c.r(local_base, value, 'C09')
|
||
set_property(value, b'/fragment@0/__overlay__', b'missing', b'')
|
||
c.r(local_base, value, 'C02')
|
||
c.r(root, fragment() + [(b'/__local_fixups__', []), (b'/__local_fixups__/missing', [])], 'C09')
|
||
c.r(root, fragment() + [(b'/__local_fixups__', [(b'p', b'')])], 'C09')
|
||
value = copy.deepcopy(local)
|
||
set_property(value, b'/fragment@0/__overlay__', b'refs', u32(99) * 3)
|
||
c.r(local_base, value, 'C09')
|
||
c.r(root, fragment(attributes={b'phandle': u32(1), b'refs': u32(1) * 2}) +
|
||
[(b'/__local_fixups__', []), (b'/__local_fixups__/fragment@0', []),
|
||
(b'/__local_fixups__/fragment@0/__overlay__', [(b'refs', u32(0) + u32(4))])], 'C02')
|
||
external_base, external = roots[2]
|
||
for raw in [b'', b'\0', b'/fragment@0/__overlay__:refs:0', b'/fragment@0/__overlay__:refs:0\0\0',
|
||
b'/:refs:0\0', b'/__fixups__:ctrl:0\0', b'/fragment@0/__overlay__:phandle:0\0',
|
||
b'/fragment@0/__overlay__:refs:0:0\0']:
|
||
value = copy.deepcopy(external); set_property(value, b'/__fixups__', b'ctrl', raw)
|
||
c.r(external_base, value, 'C09')
|
||
for number in [b'1', b'8', b'-1', b'+0', b'0x0', b'4294967296', b'00000000000', b'0000000000']:
|
||
value = copy.deepcopy(external)
|
||
set_property(value, b'/__fixups__', b'ctrl', b'/fragment@0/__overlay__:refs:' + number + b'\0')
|
||
c.r(external_base, value, 'C03')
|
||
value = copy.deepcopy(external)
|
||
set_property(value, b'/__fixups__', b'ctrl', b'/fragment@0/__overlay__:refs:0\0' * 2)
|
||
c.r(external_base, value, 'C09')
|
||
for raw in [u32(1) * 2, b'\xff' * 3, b'\xff' * 4 + u32(1)]:
|
||
value = copy.deepcopy(external); set_property(value, b'/fragment@0/__overlay__', b'refs', raw)
|
||
c.r(external_base, value, 'C09')
|
||
c.r(root, external, 'C11')
|
||
no_handle = copy.deepcopy(external_base); attributes(no_handle, b'/ctrl').clear()
|
||
c.r(no_handle, external, 'C11')
|
||
for maximum in [0xfffffffc, 0xfffffffd, 0xfffffffe]:
|
||
base = tree({b'/': {b'phandle': u32(maximum)}})
|
||
c.r(base, fragment(), 'C08')
|
||
c.r(base, fragment(attributes={b'phandle': u32(1)}), 'C08')
|
||
target_overlay = fragment(); target_overlay[1] = (b'/fragment@0', [(b'target', u32(0xffffffff))])
|
||
target_overlay.append((b'/__fixups__', [(b'ctrl', b'/fragment@0:target:0\0')]))
|
||
c.r(external_base, target_overlay, 'C11')
|
||
for handle in [0, 1, 10, 0xffffffff]:
|
||
value = fragment(); value[1] = (b'/fragment@0', [(b'target', u32(handle))])
|
||
c.r(external_base, value, 'C11')
|
||
for reserved in [b'__symbols__', b'__fixups__', b'__local_fixups__']:
|
||
value = fragment() + [(b'/fragment@0/__overlay__/' + reserved, [])]
|
||
c.r(root, value, 'C12')
|
||
value = fragment(b'/nested') + [(b'/fragment@0/__overlay__/' + reserved, [(b'p', b'\0\xff')])]
|
||
c.r(tree({b'/': {}, b'/nested': {}}), value, 'C12')
|
||
c.r(tree({b'/': {b'keep': b'K', b'replace': b'ab'}, b'/leaf': {}}),
|
||
fragment(attributes={b'replace': b'\xff\0'}), 'C12')
|
||
multi = fragment(attributes={b'order': b'0'}) + [
|
||
(b'/fragment@2', [(b'target-path', b'/\0')]), (b'/fragment@2/__overlay__', [(b'order', b'2')]),
|
||
(b'/fragment@10', [(b'target-path', b'/\0')]), (b'/fragment@10/__overlay__', [(b'order', b'10')])]
|
||
c.r(root, multi, 'C14')
|
||
value = copy.deepcopy(multi) + [(b'/fragment@0/__overlay__/created', []),
|
||
(b'/fragment@3', [(b'target-path', b'/created\0')]), (b'/fragment@3/__overlay__', [])]
|
||
c.r(root, value, 'C11')
|
||
multi_base, multi = roots[3]
|
||
for name, target in [(b'Root', b'/fragment@1/__overlay__'), (b'Alias', b'/fragment@1/__overlay__/beta')]:
|
||
value = copy.deepcopy(multi); set_property(value, b'/__symbols__', name, target + b'\0')
|
||
c.r(multi_base, value, 'C04')
|
||
c.r(multi_base + [(b'/__symbols__', [(b'B', b'/bus2/beta\0')])], multi, 'C04')
|
||
c.r(multi_base + [(b'/__symbols__', [(b'B', b'/bus2\0')])], multi, 'C04')
|
||
for count in [512, 513]:
|
||
c.r([(b'/', [])] + [(b'/n%d' % i, []) for i in range(count - 1)], root, 'C13')
|
||
base = [(b'/', [])] + [(b'/n%d' % i, []) for i in range(511)]
|
||
value = fragment() + [(b'/fragment@0/__overlay__/n%d' % i, []) for i in range(253)]
|
||
c.r(base, value, 'C13'); c.r(base, value + [(b'/fragment@0/__overlay__/n253', [])], 'C13')
|
||
for count in [510, 511]:
|
||
base = [(b'/', [])] + [(b'/n%d' % i, []) for i in range(count)]
|
||
c.r(base, fragment() + [(b'/fragment@0/__overlay__/new', [])], 'C13')
|
||
for length in [65536, 65537]:
|
||
c.r(tree({b'/': {b'data': b'\xff' * length}}), root, 'C13')
|
||
base = tree({b'/': {b'p%d' % i: bytes([i]) * 65536 for i in range(16)}})
|
||
c.r(base, root, 'C13'); c.r(base, tree({b'/': {b'ignored': b'x'}}), 'C13')
|
||
for count in [4096, 4097]:
|
||
c.r([(b'/', [(b'p%d' % i, b'') for i in range(count)])], root, 'C13')
|
||
for extra in [0, 1]:
|
||
properties = [(b'p%d' % i + b'a' * ((31 if i < 2 else 32) - len(b'p%d' % i)), b'') for i in range(4096)]
|
||
if extra: properties[0] = (properties[0][0] + b'a', b'')
|
||
c.r([(b'/', properties)], root, 'C13')
|
||
for count in [4096, 4097]:
|
||
value = fragment(attributes={b'phandle': u32(1), b'refs': u32(1) * count})
|
||
value.extend([(b'/__local_fixups__', []), (b'/__local_fixups__/fragment@0', []),
|
||
(b'/__local_fixups__/fragment@0/__overlay__', [(b'refs', b''.join(u32(i * 4) for i in range(count)))])])
|
||
c.r(root, value, 'C13')
|
||
for depth_count in [64, 65]:
|
||
c.r([(b'/', [])] + [(b'/' + b'/'.join([b'a'] * i), []) for i in range(1, depth_count)], root, 'C13')
|
||
for last in [47, 48]:
|
||
base = [(b'/', [])]; path = b''
|
||
for i in range(16): path += b'/' + b'a' * 60; base.append((path, []))
|
||
base.append((path + b'/' + b'b' * last, [])); c.r(base, root, 'C13')
|
||
base = [(b'/', [])]; path = b''
|
||
for i in range(16): path += b'/' + b'a' * 60; base.append((path, []))
|
||
c.r(base, fragment(path) + [(b'/fragment@0/__overlay__/' + b'b' * 47, [])], 'C13')
|
||
c.r(base, fragment(path) + [(b'/fragment@0/__overlay__/' + b'b' * 48, [])], 'C13')
|
||
c.r(base, fragment(path) + [(b'/fragment@0/__overlay__/n%d' % i, []) for i in range(140)], 'C13')
|
||
for count in [256, 257]:
|
||
base = tree({b'/': {}, b'/__symbols__': {b'base_%d' % i: b'/\0' for i in range(256)}})
|
||
value = fragment() + [(b'/__symbols__', [(b'new_%d' % i, b'/fragment@0/__overlay__\0') for i in range(count)])]
|
||
c.r(base, value, 'C13')
|
||
for input_value in [root, tree({b'/': {b'opaque': b'\0\xff'}}), [], tree({b'/': {b'phandle': u32(1)}}),
|
||
tree({b'/': {}, b'/ordinary': {}})]:
|
||
c.r(input_value, root, 'C16', alias=0)
|
||
c.r(root, input_value, 'C16', alias=1, bank=1)
|
||
c.r(*roots[2], 'C15'); c.hold(1)
|
||
c.r([], root, 'C15'); c.hold(2)
|
||
c.r(root, root, 'C16', alias=0, bank=1); c.hold(3)
|
||
for slot in range(4): c.saved(slot); c.saved(slot, True)
|
||
rng = random.Random(20261017036)
|
||
for i in range(12):
|
||
base = tree({b'/': {}, b'/bus': {b'phandle': u32(10 + i), b'keep': bytes([i, 0, 255])}})
|
||
value = fragment(b'/bus', {b'phandle': u32(1), b'raw': bytes(rng.randrange(256) for _ in range(9))})
|
||
value.append((b'/__symbols__', [(b'export_%d' % i, b'/fragment@0/__overlay__\0')]))
|
||
rng.shuffle(base); rng.shuffle(value)
|
||
c.r(base, value, 'C17', bank=i % 2)
|
||
# Independent witnesses and cross-boundaries of the same four public roots.
|
||
for maximum in [40, 0x80000000]:
|
||
base = tree({b'/': {b'phandle': u32(1)}, b'/bus': {b'phandle': u32(9)},
|
||
b'/bus/deep': {b'phandle': u32(maximum)}})
|
||
c.r(base, fragment(b'/bus') + [(b'/fragment@0/__overlay__/child', [(b'phandle', u32(3))])], 'C01')
|
||
value = fragment(attributes={b'phandle': u32(1), b'refs': u32(99) + u32(1) + u32(1)})
|
||
value.extend([(b'/__local_fixups__', []), (b'/__local_fixups__/fragment@0', []),
|
||
(b'/__local_fixups__/fragment@0/__overlay__', [(b'refs', u32(8))])])
|
||
c.r(tree({b'/': {b'phandle': u32(200)}}), value, 'C02')
|
||
value = copy.deepcopy(roots[2][1]); set_property(value, b'/fragment@0/__overlay__', b'refs', b'\xff' * 12)
|
||
set_property(value, b'/__fixups__', b'ctrl', b''.join(b'/fragment@0/__overlay__:refs:%d\0' % n for n in (0, 4, 8)))
|
||
c.r(roots[2][0], value, 'C03')
|
||
value = copy.deepcopy(roots[3][1]) + [(b'/fragment@10', [(b'target-path', b'/bus3\0')]),
|
||
(b'/fragment@10/__overlay__', []), (b'/fragment@10/__overlay__/gamma', [])]
|
||
set_property(value, b'/__symbols__', b'C', b'/fragment@10/__overlay__/gamma\0')
|
||
c.r(roots[3][0] + [(b'/bus3', [])], value, 'C04')
|
||
same = fragment(attributes={b'opaque': b'\0\xff'}) + [(b'/fragment@0/__overlay__/leaf', [])]
|
||
c.r(same, root, 'C16', alias=0)
|
||
c.r(root, same, 'C16', alias=1, bank=1)
|
||
base = [(b'/', [])] + [(b'/' + b'/'.join([b'a'] * i), []) for i in range(1, 64)]
|
||
target = base[-1][0]
|
||
c.r(base, fragment(target) + [(b'/fragment@0/__overlay__/child', [])], 'C13')
|
||
base = [(b'/', [])]; target = b''
|
||
for i in range(16): target += b'/' + b'a' * 60; base.append((target, []))
|
||
set_property(base, target, b'phandle', u32(2))
|
||
child = b'b' * 47
|
||
value = [(b'/', []), (b'/fragment@0', [(b'target', u32(2))]), (b'/fragment@0/__overlay__', []),
|
||
(b'/fragment@0/__overlay__/' + child, []),
|
||
(b'/__symbols__', [(b'E', b'/fragment@0/__overlay__/' + child + b'\0')])]
|
||
for extra in [0, 1]:
|
||
adjusted = copy.deepcopy(base)
|
||
remaining = 1048576 - 1025 - 4 + extra
|
||
for i in range(16):
|
||
amount = min(remaining, 65536); remaining -= amount
|
||
set_property(adjusted, b'/', b'data%d' % i, b'\xff' * amount)
|
||
assert remaining == 0
|
||
c.r(adjusted, value, 'C13')
|
||
assert c.calls <= 256, c.calls
|
||
assert set(c.labels) == set(BASIS) - {'C18'}
|
||
return c
|
||
|
||
|
||
def equal(left, right):
|
||
if type(left) is not type(right):
|
||
return False
|
||
if isinstance(left, list):
|
||
return len(left) == len(right) and all(equal(a, b) for a, b in zip(left, right))
|
||
if isinstance(left, dict):
|
||
return left.keys() == right.keys() and all(equal(left[key], right[key]) for key in left)
|
||
return left == right
|
||
|
||
|
||
def main():
|
||
context = json.loads((Path(__file__).resolve().parent / 'context.json').read_text())
|
||
scratch = Path(tempfile.mkdtemp(prefix='embedded-hard-overlay-', dir='/tmp'))
|
||
for name, text in context.items():
|
||
(scratch / name).write_text(text)
|
||
for name in CANDIDATES:
|
||
(scratch / name).write_bytes((Path(sys.argv[1]) / name).read_bytes())
|
||
try:
|
||
build = subprocess.run(['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic',
|
||
'overlay_observe.cpp', *CPP, '-o', 'overlay-observe'], cwd=scratch,
|
||
text=True, encoding='utf-8', errors='replace', capture_output=True, timeout=30)
|
||
except subprocess.TimeoutExpired:
|
||
print('FAILED_CHECKS ["C18"] build timeout')
|
||
return 1
|
||
if build.returncode:
|
||
print('FAILED_CHECKS ["C18"] build failed: ' + build.stderr[-1200:].replace('\n', ' '))
|
||
return 1
|
||
matrix = cases()
|
||
# shortcut: 子进程结束后仅查看管道,由可信 owner/container 清理 scratch;具备受控目录 fd 清理后替换。
|
||
timed_out = False
|
||
try:
|
||
run = subprocess.run([str(scratch / 'overlay-observe')], cwd=scratch,
|
||
input='\n'.join(matrix.commands) + '\n', text=True,
|
||
encoding='utf-8', errors='replace', capture_output=True, timeout=30)
|
||
stdout, returncode = run.stdout, run.returncode
|
||
except subprocess.TimeoutExpired as error:
|
||
timed_out = True
|
||
stdout = error.stdout or b''
|
||
if isinstance(stdout, bytes):
|
||
stdout = stdout.decode('utf-8', 'replace')
|
||
returncode = -9
|
||
actual = stdout.splitlines()
|
||
failures = {'C18'} if timed_out or returncode else set()
|
||
indexes, examples = [], []
|
||
count = max(len(actual), len(matrix.expected))
|
||
for index in range(count):
|
||
label = matrix.labels[index] if index < len(matrix.labels) else 'C18'
|
||
wanted = matrix.expected[index] if index < len(matrix.expected) else '<end>'
|
||
try:
|
||
observed = json.loads(actual[index]) if index < len(actual) else '<missing>'
|
||
except ValueError:
|
||
observed = actual[index]
|
||
if index >= len(actual) or index >= len(matrix.expected) or not equal(observed, wanted):
|
||
failures.add(label)
|
||
indexes.append(index)
|
||
if len(examples) < 8:
|
||
examples.append('%s observation=%d got=%s expected=%s' %
|
||
(label, index, repr(observed)[:220], repr(wanted)[:220]))
|
||
if failures:
|
||
print('FAILED_CHECKS ' + json.dumps(sorted(failures)))
|
||
for example in examples:
|
||
print(example)
|
||
bitmap = bytearray((count + 7) // 8)
|
||
for index in indexes:
|
||
bitmap[index // 8] |= 1 << (index % 8)
|
||
print('MISMATCH_BITMAP ' + json.dumps(dict(observations=count,
|
||
bits_lsb0=base64.b64encode(bitmap).decode(), failed_checks=sorted(failures),
|
||
candidate_exit=returncode, timeout=timed_out), separators=(',', ':')), file=sys.stderr)
|
||
return 1
|
||
print('PASS')
|
||
return 0
|
||
|
||
|
||
if __name__ == '__main__':
|
||
raise SystemExit(main())
|