"""Independent integer model; submitted C is compiled and run only in children.""" import json from pathlib import Path import random import subprocess import sys import tempfile MASK = (1 << 32) - 1 MAX64 = (1 << 64) - 1 def valid_program(words): if not 1 <= len(words) <= 12: return False for index, word in enumerate(words): op, operand = divmod(word, 256) if index == len(words) - 1: if word != 0: return False elif not 1 <= op <= 6 or (op == 4 and operand > 31): return False return True def evaluate(words, value): for word in words[:-1]: op, byte = divmod(word, 256) signed = byte if byte < 128 else byte - 256 if op == 1: value += signed elif op == 2: value ^= byte elif op == 3: value *= byte elif op == 4 and byte: value = (value << byte) | ((value & MASK) >> (32 - byte)) elif op == 5: value = signed elif op == 6: value &= byte value &= MASK return value def parse(line): fields = line.split() if not fields or len(fields[0]) != 1: return None kind = fields[0] if kind == 'R': return (kind, []) if len(fields) == 1 else None if kind not in 'IPDE' or len(fields) < 2: return None numbers = [] for field in fields[1:]: if not field or any(ch < '0' or ch > '9' for ch in field): return None value = int(field) if value > MAX64: return None numbers.append(value) if numbers[0] >= 4: return None if kind == 'I': if len(numbers) < 3 or not 1 <= numbers[1] <= 12: return None if len(numbers) != 2 + numbers[1] or max(numbers[2:]) > 65535: return None elif kind == 'P': if len(numbers) < 5 or numbers[2] > 11 or not 1 <= numbers[3] <= 12: return None if len(numbers) != 4 + numbers[3] or max(numbers[4:]) > 65535: return None elif kind == 'D' and len(numbers) != 2: return None elif kind == 'E' and (len(numbers) != 2 or numbers[1] > MASK): return None return kind, numbers class Model: def __init__(self): self.reset() def reset(self): self.slots = [dict(active=0, generation=0, words=[]) for _ in range(4)] self.counts = dict.fromkeys(('serial', 'installs', 'patches', 'drops', 'executions', 'writes', 'sync_calls', 'sync_words', 'rejected'), 0) def apply(self, command): if command is None: self.counts['rejected'] += 1 return -4, 0, 0 kind, args = command if kind == 'R': self.reset() return 0, 0, 0 slot = self.slots[args[0]] rc, value, retired = 0, 0, 0 words = None width = 0 if kind == 'I': words = args[2:] width = len(words) if not valid_program(words): rc = -3 elif kind == 'P': first, width = args[2:4] if not slot['active']: rc = -2 elif args[1] != slot['generation']: rc = -1 elif first + width > len(slot['words']): rc = -3 else: words = slot['words'][:] words[first:first + width] = args[4:] if not valid_program(words): rc = -3 elif kind == 'D': if not slot['active']: rc = -2 elif args[1] != slot['generation']: rc = -1 elif not slot['active']: rc = -2 else: value = evaluate(slot['words'], args[1]) retired = len(slot['words']) self.counts['executions'] += 1 if rc: self.counts['rejected'] += 1 elif kind in 'IPD': slot['generation'] += 1 self.counts['serial'] += 1 counter = {'I': 'installs', 'P': 'patches', 'D': 'drops'}[kind] self.counts[counter] += 1 if kind == 'D': slot['active'] = 0 slot['words'] = [] else: slot['active'] = 1 slot['words'] = words self.counts['writes'] += width self.counts['sync_calls'] += 1 self.counts['sync_words'] += width return rc, value, retired def observe(self, sequence, result): lines = ['O %d %d %d %d' % (sequence, *result)] lines.append('S %d ' % sequence + ' '.join(map(str, self.counts.values()))) for index, slot in enumerate(self.slots): lines.append('B %d %d %d %d %d' % (sequence, index, slot['active'], len(slot['words']), slot['generation'])) suffix = ''.join(' %d' % word for word in slot['words']) lines.extend('%s %d %d%s' % (view, sequence, index, suffix) for view in ('D', 'X')) return lines def expected(lines): model = Model() output = [] sequence = 0 for line in lines: clean = line.rstrip('\r') if len(line) <= 1023 and (not clean.strip() or clean.lstrip().startswith('#')): continue sequence += 1 command = parse(clean) if len(line) <= 1023 else None output.extend(model.observe(sequence, model.apply(command))) return output def scenarios(): rng = random.Random(0xC0DE710) lines = [ '# public one-slot replay', 'R', 'I 0 4 257 770 1031 0', 'E 0 11', 'P 0 1 1 1 771', 'E 0 11', '# cross-slot publication tokens', 'R', 'I 0 4 257 770 1031 0', 'I 1 2 1535 0', 'P 0 1 2 1 1024', 'E 0 4294967295', 'P 1 1 0 1 1280', 'E 1 888', 'D 0 2', 'D 0 3', 'I 0 1 0', 'E 0 4294967295', 'P 0 4 0 1 0', '# program validation and no partial writes', 'R', 'I 2 3 511 1024 0', 'P 2 1 1 2 1031 0', 'P 2 2 0 2 0 1792', 'E 2 2147483648', 'P 2 1 0 1 65535', 'P 3 0 11 12 ' + ' '.join(['65535'] * 12), 'I 2 2 1056 0', 'I 2 2 256 1', 'I 2 2 0 0', 'I 2 1 256', 'P 2 2 11 1 0', 'E 2 127', '# every operand boundary, long-to-short replacement and reuse', 'R', 'I 3 12 384 511 512 767 768 1023 1024 1055 1280 1535 1791 0', 'E 3 4294967295', 'P 3 1 10 2 1536 0', 'E 3 4294967295', 'I 3 1 0', 'E 3 65535', 'D 3 3', 'I 3 2 1535 0', 'E 3 0', '# parser boundaries (stderr is unrestricted)', '', ' # ignored', 'Z', 'R 1', 'I', 'I 4 1 0', 'I 0 0', 'I 0 13 ' + ' '.join(['0'] * 13), 'I 0 1 65536', 'I 0 1 0 extra', 'I +0 1 0', 'I 0x0 1 0', 'I 0 1 -1', 'I 0 01 00000', 'P 0 18446744073709551616 0 1 0', 'P 0 18446744073709551615 0 1 0', 'P 0 1 12 1 0', 'P 0 1 0 0', 'P 0 1 0 1 0 extra', 'D 0 0 extra', 'E 0 4294967296', 'E 0 -1', 'E 0 0 #', 'R' + ' ' * 1023, 'R', '\tI\t0\t2\t511\t0\r', 'E 0 0\r', ] tracker = Model() for line in lines: if line.strip() and not line.lstrip().startswith('#'): tracker.apply(parse(line.rstrip('\r')) if len(line) <= 1023 else None) def append(tokens): line = ' '.join(map(str, tokens)) lines.append(line) tracker.apply(parse(line)) def random_word(): op = rng.randrange(1, 7) byte = rng.randrange(32 if op == 4 else 256) return op * 256 + byte for phase in range(10): append(['R']) for slot in range(4): count = rng.randrange(1, 13) append(['I', slot, count] + [random_word() for _ in range(count - 1)] + [0]) for _ in range(90): slot = rng.randrange(4) current = tracker.slots[slot] kind = rng.choice('IIIPPPPPDEEEE') if kind == 'I': count = rng.choice([1, 2, 12, rng.randrange(1, 13)]) words = [random_word() for _ in range(count - 1)] + [0] if rng.randrange(7) == 0: words[rng.randrange(count)] = rng.choice([0, 1792, 1056, 65535]) append(['I', slot, count] + words) elif kind == 'P': generation = current['generation'] if rng.randrange(5) == 0: generation += rng.choice([1, 3, 101]) if current['active']: first = rng.randrange(len(current['words'])) count = rng.randrange(1, len(current['words']) - first + 1) words = [random_word() for _ in range(count)] if first + count == len(current['words']): words[-1] = 0 if rng.randrange(6) == 0: words[rng.randrange(count)] = 65535 else: first, count, words = 0, 1, [0] append(['P', slot, generation, first, count] + words) elif kind == 'D': append(['D', slot, current['generation'] + (1 if rng.randrange(4) == 0 else 0)]) else: append(['E', slot, rng.choice([0, MASK, 1 << 31, rng.randrange(1 << 32)])]) # Final no-LF command exercises EOF handling without changing its semantics. lines.extend(['R', 'I 0 2 511 0', 'E 0 0']) return lines # Execution-only supervisor: no model, cases, assertions or expected values. # It runs under the same UID as its children, so timeout kill/reap needs no CAP_KILL. EXECUTOR = r""" import ctypes, json, os, signal, subprocess, sys, time phase, deadline = sys.argv[1], float(sys.argv[2]) argv = sys.argv[3:] process = None output = error = b'' code = 125 facts = dict(phase=phase, uid=os.getuid(), gid=os.getgid(), groups=os.getgroups(), deadline=deadline, timed_out=False, reaped=False, adopted_reaped=0, descendants_reaped=False) try: libc = ctypes.CDLL(None, use_errno=True) if libc.prctl(36, 1, 0, 0, 0) != 0: raise OSError(ctypes.get_errno(), 'subreaper unavailable') data = sys.stdin.buffer.read() process = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True) facts['pid'] = process.pid try: output, error = process.communicate(data, timeout=deadline) except subprocess.TimeoutExpired: facts['timed_out'] = True try: os.killpg(process.pid, signal.SIGKILL) except ProcessLookupError: pass output, error = process.communicate(timeout=3) facts['returncode'] = process.returncode facts['reaped'] = process.returncode is not None code = 124 if facts['timed_out'] else process.returncode if code < 0: code = 128 - code except BaseException as exception: facts['exception'] = type(exception).__name__ if process is not None: try: os.killpg(process.pid, signal.SIGKILL) except ProcessLookupError: pass try: output, error = process.communicate(timeout=3) facts['returncode'] = process.returncode facts['reaped'] = process.returncode is not None except BaseException as reap_error: facts['reap_exception'] = type(reap_error).__name__ code = 125 finally: # Any orphaned descendants belong to this fresh supervisor. Kill and reap # them as this same UID, including children that created another group. until = time.monotonic() + 3 while time.monotonic() < until: try: while True: pid, status = os.waitpid(-1, os.WNOHANG) if pid == 0: break facts['adopted_reaped'] += 1 except ChildProcessError: facts['descendants_reaped'] = True break try: with open('/proc/self/task/%d/children' % os.getpid()) as stream: children = [int(pid) for pid in stream.read().split()] for pid in children: try: os.kill(pid, signal.SIGKILL) except ProcessLookupError: pass except OSError as exception: facts['children_exception'] = type(exception).__name__ break time.sleep(0.01) if not facts['descendants_reaped']: code = 125 sys.stdout.buffer.write(output) sys.stdout.buffer.flush() sys.stderr.write(error.decode('utf-8', 'replace')) facts['supervisor_returncode'] = code sys.stderr.write('\nCW_EXEC ' + json.dumps(facts, sort_keys=True) + '\n') raise SystemExit(code) """ CLEANUP = 'import shutil,sys; shutil.rmtree(sys.argv[1])' ABSENCE = ("import json,os,sys; exists=os.path.lexists(sys.argv[1]); " "print(json.dumps(dict(path=sys.argv[1],exists=exists,uid=os.getuid(),gid=os.getgid()))); " "raise SystemExit(1 if exists else 0)") def execute(phase, argv, cwd, timeout, data=b''): completed = subprocess.run([sys.executable, '-I', '-c', EXECUTOR, phase, str(timeout), *argv], cwd=cwd, input=data, capture_output=True) sys.stderr.write(completed.stderr.decode('utf-8', 'replace')) return completed def main(): lines = scenarios() answers = expected(lines) context = json.loads(Path(__file__).with_name('context.json').read_text()) scratch = Path(tempfile.mkdtemp(prefix='dev-code-window-')) failure = None try: build = scratch / 'build' build.mkdir(mode=0o777) build.chmod(0o777) for name, text in context.items(): destination = build / name destination.parent.mkdir(parents=True, exist_ok=True) destination.write_text(text) (build / 'window.c').write_bytes((Path(sys.argv[1]) / 'window.c').read_bytes()) for path in build.rglob('*'): if path.is_dir(): path.chmod(0o777) stdin = '\n'.join(lines).encode('ascii') executable = str(build / 'code-window') sys.stderr.write('CW_SCRATCH ' + str(scratch) + '\n') # All files, inputs and expectations exist before this first child. # Below here the parent never reads or writes candidate-controlled paths. compiled = execute('compile', ['make', '-s'], str(build), 35) if compiled.returncode: failure = 'build failed: %d' % compiled.returncode else: completed = execute('candidate', [executable], str(build), 20, stdin) if completed.returncode: failure = 'candidate process failed: %d' % completed.returncode else: try: observed = completed.stdout.decode('ascii').splitlines() except UnicodeDecodeError: failure = 'candidate stdout violates ASCII observation protocol' else: if observed != answers: mismatch = next((i for i, pair in enumerate(zip(observed, answers)) if pair[0] != pair[1]), min(len(observed), len(answers))) failure = 'observation mismatch at row %d' % (mismatch + 1) except Exception as error: failure = 'execution failed: ' + type(error).__name__ finally: try: cleanup = execute('cleanup', [sys.executable, '-I', '-c', CLEANUP, str(scratch)], '/', 15) if cleanup.returncode: failure = 'candidate-identity cleanup failed: %d' % cleanup.returncode except Exception as error: failure = 'candidate-identity cleanup exception: ' + type(error).__name__ try: absent = execute('absence', [sys.executable, '-I', '-c', ABSENCE, str(scratch)], '/', 5) sys.stderr.write('CW_ABSENCE ' + absent.stdout.decode('utf-8', 'replace') + '\n') if absent.returncode: failure = 'candidate-identity absence failed: %d' % absent.returncode except Exception as error: failure = 'candidate-identity absence exception: ' + type(error).__name__ print(failure or 'PASS') return 1 if failure else 0 if __name__ == '__main__': raise SystemExit(main())