"""Private specification oracle. Candidate code runs only in the observation child.""" import copy import json from pathlib import Path import random import re import subprocess import sys import tempfile WS = ' \t\r\v\f' CANDIDATES = ['conf_lex.cpp', 'conf_compile.cpp', 'module_session.cpp', 'module_batch.cpp'] def canonical(raw): if not re.fullmatch(r'[A-Za-z0-9_-]{1,64}', raw, flags=re.ASCII): raise ValueError('module') return raw.replace('-', '_') def lex(text): if len(text) > 65536 or any(ord(c) > 127 for c in text): raise ValueError('text') rows = [] assembled = '' waiting = False for physical in text.split('\n'): if physical.endswith('\\'): assembled += physical[:-1] + ' ' waiting = True else: logical = (assembled + physical).strip(WS) assembled = '' waiting = False if logical and not logical.startswith('#'): rows.append(re.split(r'[ \t\r\v\f]+', logical)) if waiting: raise ValueError('continuation') return rows def compile_config(directories, available): if len(directories) > 5 or len(available) > 256: raise ValueError('capacity') available = [canonical(name) for name in available] if len(set(available)) != len(available): raise ValueError('duplicate module') all_files = sum(len(directory) for directory in directories) all_bytes = sum(len(text) for directory in directories for _, text in directory) if all_files > 128 or all_bytes > 262144: raise ValueError('aggregate') selected = {} for directory in directories: if len(directory) > 64 or len({name for name, _ in directory}) != len(directory): raise ValueError('directory') for name, text in directory: if not 1 <= len(name) <= 128 or not re.fullmatch(r'[A-Za-z0-9_.-]*\.conf', name): raise ValueError('filename') if len(text) > 65536 or any(ord(c) > 127 for c in text): raise ValueError('text domain') if name not in selected: selected[name] = text black = set() options = {} dependencies = {} for filename in sorted(selected): for row in lex(selected[filename]): if len(row) < 2: raise ValueError('directive') command, module, *tail = row module = canonical(module) if command == 'blacklist': if tail: raise ValueError('blacklist syntax') black.add(module) elif command == 'options': if not tail or any(not re.fullmatch(r'[A-Za-z0-9_.,=:+/-]+', item) for item in tail): raise ValueError('option syntax') options.setdefault(module, []).extend(tail) elif command == 'softdep': if module in dependencies or not tail: raise ValueError('duplicate softdep') parts = {} side = None for token in tail: if token in ('pre:', 'post:'): if token in parts or (token == 'pre:' and 'post:' in parts): raise ValueError('section order') side = token parts[side] = [] else: if side is None: raise ValueError('section missing') parts[side].append(canonical(token)) if not parts: raise ValueError('sections') dependencies[module] = (parts.get('pre:', []), parts.get('post:', [])) else: raise ValueError('unsupported') return [sorted(available), sorted(selected), sorted(black), [[name, values] for name, values in sorted(options.items())], [[name, pre, post] for name, (pre, post) in sorted(dependencies.items())]] EMPTY_CONFIG = [[], [], [], [], []] class Session: """Build an event script recursively; apply publication separately on each tick.""" def __init__(self, config): self.config = copy.deepcopy(config) self.reset() def reset(self): self.committed = [] self.skipped = [] self.missing = set() self.accepted = self.suppressed = self.failed = self.cancelled = 0 self.discard() def discard(self): self.events = [] self.staged = [] self.pending = set() self.busy = False def snapshot(self): return copy.deepcopy([self.busy, self.committed, self.staged, self.skipped, sorted(self.missing), sorted(self.pending), self.accepted, self.suppressed, self.failed, self.cancelled]) def begin(self, raw, alias): if self.busy: return 3 try: name = canonical(raw) if name not in self.config[0]: raise ValueError('requested missing') except ValueError: return 1 self.discard() self.busy = True if alias and name in self.config[2]: self.events = [(6, name)] return 0 available = set(self.config[0]) loaded = {item[0] for item in self.committed} active = set() dependencies = {n: (pre, post) for n, pre, post in self.config[4]} class Cycle(Exception): pass def visit(module): if module not in available: self.events.append((4, module)) return if module in active: self.events.append((7, module)) raise Cycle() if module in loaded: self.events.append((5, module)) return active.add(module) self.events.append((1, module)) pre, post = dependencies.get(module, ([], [])) for dependency in pre: visit(dependency) self.events.append((2, module)) loaded.add(module) for dependency in post: visit(dependency) self.events.append((3, module)) active.remove(module) try: visit(name) except Cycle: pass return 0 def tick(self): if not self.busy: return 4, 0, '' kind, name = self.events.pop(0) if kind == 6: self.skipped.append(name) self.suppressed += 1 self.discard() elif kind == 7: self.failed += 1 self.discard() return 2, kind, name elif kind == 4: self.pending.add(name) elif kind == 2: options = dict(self.config[3]) self.staged.append([name, options.get(name, [])[:]]) if self.busy and not self.events: self.committed.extend(self.staged) self.missing.update(self.pending) self.accepted += 1 self.discard() return 0, kind, name def cancel(self): if self.busy: self.cancelled += 1 self.discard() def batch(directories, available, requests): error = 1 try: if len(requests) > 256: raise ValueError('request count') names = {canonical(name) for name in available} if any(canonical(raw) not in names for raw, _ in requests): raise ValueError('requested unavailable') config = compile_config(directories, available) session = Session(config) for raw, alias in requests: if session.begin(raw, alias): raise ValueError('begin') while session.busy: error, _, _ = session.tick() if error: return [error, [], [], [], []] return [0, session.committed, session.skipped, sorted(session.missing), config[1]] except ValueError: return [1, [], [], [], []] class Cases: def __init__(self): self.commands = [] self.expected = [] self.labels = [] self.banks = [dict(dirs=[], available=[], session=Session(EMPTY_CONFIG), held=Session(EMPTY_CONFIG).snapshot()) for _ in range(3)] self.selected = 0 @staticmethod def hex(text): return text.encode('latin1').hex() or '-' def emit(self, command, result, label): self.commands.append(command) self.expected.append(result) self.labels.append(label) def config(self, directories, available, label='C07'): bank = self.banks[self.selected] bank['dirs'] = copy.deepcopy(directories) bank['available'] = available[:] rows = ['C %d %d' % (len(directories), len(available))] for directory in directories: rows.append('D %d' % len(directory)) rows.extend('F %s %s' % (self.hex(name), self.hex(text)) for name, text in directory) rows.extend('A ' + self.hex(name) for name in available) try: config = compile_config(directories, available) error = 0 bank['session'] = Session(config) except ValueError: config, error = copy.deepcopy(EMPTY_CONFIG), 1 self.emit('\n'.join(rows), ['C', error, config, True], label) def command(self, op, *args, label='C30'): bank = self.banks[self.selected] session = bank['session'] if op == 'G': self.selected = args[0] result = ['G', self.banks[self.selected]['session'].snapshot()] wire = 'G %d' % args[0] elif op == 'N': try: name, valid = canonical(args[0]), True except ValueError: name, valid = '', False result, wire = ['N', valid, name, True], 'N ' + self.hex(args[0]) elif op == 'L': try: tokens, error = lex(args[0]), 0 except ValueError: tokens, error = [], 1 result, wire = ['L', error, tokens, True], 'L ' + self.hex(args[0]) elif op == 'B': error = session.begin(*args) result = ['B', error, session.snapshot(), True] wire = 'B %s %d' % (self.hex(args[0]), args[1]) elif op == 'T': error, kind, name = session.tick() result, wire = ['T', error, kind, name, session.snapshot()], 'T' elif op == 'P': requests = args[0] result = ['P', *batch(bank['dirs'], bank['available'], requests), True] wire = 'P %d\n%s' % (len(requests), '\n'.join('Q %s %d' % (self.hex(raw), flag) for raw, flag in requests)) else: if op == 'R': session.reset() elif op == 'X': session.cancel() elif op == 'H': bank['held'] = session.snapshot() result, wire = [op, copy.deepcopy(bank['held']) if op == 'J' else session.snapshot()], op self.emit(wire, result, label) def drain(self, label='C27'): while self.banks[self.selected]['session'].busy: self.command('T', label=label) self.command('T', label='C19') def cases(): c = Cases() rng = random.Random(20261017016) # Public cancellation sequence, then independently selected hidden variants. c.config([[('00-boot.conf', 'softdep core post: logger\n')]], ['core', 'logger']) c.command('B', 'core', False, label='C18') c.command('T', label='C24'); c.command('T', label='C25') c.command('S', label='C27'); c.command('X', label='C28'); c.command('S', label='C28') c.command('B', 'logger', False); c.drain() for raw in ['', 'a', 'x-y_Z9', 'A' * 64, 'A' * 65, 'bad.name', '\x00', '\xff', 'x y', 'pre:']: c.command('N', raw, label='C01') for text in ['', '\n', ' #ignored\x00\n', 'a\\', 'a\\\n', 'a\\\nb', 'a\\ \n b', '\t\v\f\ra\t b\r\n# x\\\noptions a wrong', 'x\x00y', 'a # literal', 'x\\\n\\\ny', 'x\xff', 'a' * 65536, 'a' * 65537]: c.command('L', text, label='C08' if '\xff' in text or len(text) > 65536 else 'C09') c.config([[('z.conf', 'options m z=3 z=3\n'), ('a.conf', 'options m k=1 k=2 same same\n')], [('a.conf', 'unsupported ignored syntax\n'), ('b.conf', 'options m b=4\n')]], ['m'], label='C07') c.command('P', [('m', False)], label='C14') c.config([[('x.conf', 'options x-y a=1\noptions x_y a=2 a=2\nblacklist x-y\nblacklist x_y\nsoftdep x-y pre: post:\n')]], ['x-y'], label='C16') c.command('P', [('x_y', True), ('x-y', True), ('x-y', False), ('x_y', False)], label='C13') c.command('B', 'x-y', True, label='C20'); c.command('H'); c.command('X', label='C28'); c.command('J', label='C02') c.command('B', 'x_y', True, label='C20'); c.drain('C20') c.command('B', 'x-y', False, label='C13'); c.drain('C13') c.command('B', 'x_y', False, label='C23'); c.drain('C23') c.command('B', 'missing', False, label='C05'); c.command('B', 'bad.name', True, label='C05') invalid_text = ['options m', 'blacklist m extra', 'BlackList m', 'unknown m', 'softdep m', 'softdep m x pre:', 'softdep m pre: pre:', 'softdep m post: pre:', 'softdep m post: post:', 'softdep m pre: bad.name', 'options m x#y', 'options m "x"', 'options m a\\', 'options m x\x00y', 'blacklist m\x00', 'softdep m pre:\nsoftdep m post:', 'softdep x-y pre:\nsoftdep x_y post:'] for text in invalid_text: c.config([[('bad.conf', text)]], ['m'], label='C12' if not text.startswith('softdep') else 'C16') c.command('P', [], label='C11') for name in ['', 'a', '.conf', 'x/../a.conf', 'A' * 123 + '.conf', 'A' * 124 + '.conf', 'x\x00.conf', '\xff.conf']: c.config([[(name, '#empty')]], ['m'], label='C04') c.config([[('x.conf', '# top')], [('x.conf', '\xff')]], ['m'], label='C04') c.config([[('x.conf', '# a'), ('x.conf', '# b')]], ['m'], label='C03') c.config([[]] * 5, ['m'], label='C03'); c.config([[]] * 6, ['m'], label='C03') c.config([], ['m'] * 2, label='C05'); c.config([], ['x-y', 'x_y'], label='C05') c.config([], ['m%d' % i for i in range(256)], label='C05') c.command('P', [('m0', False)] * 256, label='C31') c.command('P', [('m0', False)] * 257, label='C31') c.config([], ['m%d' % i for i in range(257)], label='C05') for size in [64, 65]: c.config([[('%03d.conf' % i, '# comment') for i in range(size)]], [], label='C03') c.config([[('%03d.conf' % i, '#') for i in range(64)], [('%03d.conf' % i, '#') for i in range(64, 128)]], [], label='C03') c.config([[('%03d.conf' % i, '#') for i in range(64)], [('%03d.conf' % i, '#') for i in range(64, 128)], [('129.conf', '#')]], [], label='C03') c.config([[('x.conf', '#' + 'a' * 65535)]], [], label='C04') c.config([[('x.conf', '#' + 'a' * 65536)]], [], label='C04') for excess in [0, 1]: c.config([[('%d.conf' % i, '#' + 'a' * 65535) for i in range(4)], [('last.conf', '#' if excess else '')]], [], label='C04') # Active post-backedge occurs after a parent emit; all staged output must roll back. text = 'options a z=1 z=2\nsoftdep a pre: missing post: b\nsoftdep b pre: c post: a\nsoftdep missing pre: a\nblacklist b\n' c.config([[('graph.conf', text)]], ['a', 'b', 'c', 'stable'], label='C15') c.command('B', 'stable', False); c.drain() c.command('B', 'a', False); c.command('H') c.drain('C22'); c.command('J', label='C02'); c.command('S', label='C29') c.command('B', 'c', False); c.drain('C29') c.command('P', [('stable', False), ('a', False)], label='C32') c.command('P', [('stable', False)], label='C22') # Missing dependencies do not recurse and dependency blacklist never suppresses. c.config([[('graph.conf', 'blacklist b\nsoftdep a pre: gone b gone post: b tail\nsoftdep gone pre: a\n')]], ['a', 'b'], label='C21') c.command('B', 'a', False); c.command('B', 'bad.name', True, label='C18'); c.drain('C26') c.command('P', [('a', False), ('b', True), ('a', False)], label='C13') # Every cancellation position, including before first action and after pending missing. configs = [('softdep r pre: absent child post: end\noptions r x=1 x=1\n', ['r', 'child', 'end']), ('softdep r pre: child post: child\nsoftdep child pre: post:\n', ['r', 'child'])] for text, available in configs: baseline = Session(compile_config([[('x.conf', text)]], available)) baseline.begin('r', False) length = len(baseline.events) for cut in range(length + 1): c.config([[('x.conf', text)]], available) c.command('B', 'r', False) for _ in range(cut): c.command('T', label='C25') c.command('H', label='C30'); c.command('X', label='C28'); c.command('J', label='C02') c.command('B', 'r', False); c.drain('C27'); c.command('R', label='C17') # Distinct configurations and interleaved transactions cannot share state. for bank in range(3): c.command('G', bank, label='C02') c.config([[('bank.conf', 'options shared bank=%d\n' % bank)]], ['shared'], label='C02') c.command('B', 'shared', False); c.command('T'); c.command('H') for bank in [1, 0, 2]: c.command('G', bank, label='C02'); c.drain('C02'); c.command('J', label='C02') c.command('G', 0) for trial in range(60): names = ['t%d_m%d' % (trial, i) for i in range(rng.randrange(1, 13))] lines = [] for name in names: tokens = ['k=%d' % rng.randrange(4) for _ in range(rng.randrange(1, 5))] lines.append('options %s %s' % (name, ' '.join(tokens))) if rng.randrange(3) == 0: lines.append('blacklist ' + name) pre = [rng.choice(names + ['missing_a', 'missing_b']) for _ in range(rng.randrange(4))] post = [rng.choice(names + ['missing_a', 'missing_b']) for _ in range(rng.randrange(3))] lines.append('softdep %s pre: %s post: %s' % (name, ' '.join(pre), ' '.join(post))) c.config([[('zz.conf', '\n'.join(lines)), ('aa.conf', '# ignored\n')]], names, label='C15') requests = [(rng.choice(names), bool(rng.randrange(2))) for _ in range(10)] c.command('P', requests, label='C32') for name, alias in requests[:4]: c.command('B', name, alias) if rng.randrange(3) == 0: for _ in range(rng.randrange(3)): c.command('T', label='C25') c.command('X', label='C28') c.drain('C29') # A full 256-node pre-chain verifies deterministic depth without timing assumptions. names = ['node_%03d' % i for i in range(256)] text = '\n'.join('softdep %s pre: %s post:' % (names[i], names[i + 1]) for i in range(255)) c.config([[('chain.conf', text)]], names, label='C33') c.command('P', [(names[0], False)], label='C33') c.command('B', names[0], False); c.drain('C33') return c def main(): matrix = cases() context = json.loads((Path(__file__).resolve().parent / 'context.json').read_text()) scratch = Path(tempfile.mkdtemp(prefix='embedded-hard-module-', dir='/tmp')) for name, text in context.items(): destination = scratch / name destination.parent.mkdir(parents=True, exist_ok=True) destination.write_text(text) for name in CANDIDATES: (scratch / name).write_bytes((Path(sys.argv[1]) / name).read_bytes()) result = subprocess.run(['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic', 'observe.cpp', *CANDIDATES, '-o', 'observe'], cwd=scratch, text=True, capture_output=True, timeout=30) if result.returncode: print('FAILED_CHECKS ["C34"] build failed: ' + result.stderr[-1000:].replace('\n', ' ')) return 1 wire = '\n'.join(matrix.commands) + '\n' # shortcut: candidate-owned scratch is retained until container destruction or a # separate trusted-local owner cleanup; never inspect shared scratch after execution. result = subprocess.run([str(scratch / 'observe')], input=wire, cwd=scratch, text=True, encoding='utf-8', errors='replace', capture_output=True, timeout=15) if result.returncode: print('FAILED_CHECKS ["C35"] candidate execution returned %s' % result.returncode) return 1 actual = result.stdout.splitlines() failed = set() details = [] for index in range(max(len(actual), len(matrix.expected))): label = matrix.labels[index] if index < len(matrix.labels) else 'C35' wanted = matrix.expected[index] if index < len(matrix.expected) else '' try: got = json.loads(actual[index]) if index < len(actual) else '' except (ValueError, IndexError): got = actual[index] if index < len(actual) else '' if index >= len(actual) or index >= len(matrix.expected) or got != wanted: failed.add(label) if len(details) < 8: details.append('%s observation=%d got=%r expected=%r' % (label, index, repr(got)[:300], repr(wanted)[:300])) if failed: print('FAILED_CHECKS ' + json.dumps(sorted(failed))) for detail in details: print(detail) return 1 print('PASS') return 0 if __name__ == '__main__': raise SystemExit(main())