"""Independent TX state/byte model; submitted C runs only in child processes.""" import json from pathlib import Path import random import subprocess import sys import tempfile def scenarios(): rng = random.Random(0xD4A15) events = [] def add(text): events.append(text) def observe(ids): add('Q') for ident in ids: add('P %d' % ident) add('D %d' % ident) for line in (16, 32, 64): add('R %d' % line) for length in (1, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255, 256): ident = rng.randrange(1, 65536) add('B %d %d' % (ident, length)) add('S %d' % ident) split = max(1, length // 2) data = bytes(rng.randrange(256) for _ in range(split)).hex() add('W %d 0 %s' % (ident, data.upper())) add('P %d' % ident) if split < length: add('S %d' % ident) add('F %d %d %d %d' % (ident, split, length - split, rng.randrange(256))) offset = rng.randrange(length) add('W %d %d %02x' % (ident, offset, rng.randrange(256))) add('S %d' % ident) add('D %d' % ident) add('W %d 0 00' % ident) add('F %d 0 1 0' % ident) add('S %d' % ident) add('X %d' % ident) add('C %d 2' % ident) add('R 0') add('D %d' % ident) add('C %d 0' % ident) observe([ident]) add('C %d 0' % ident) for batch in range(18): ids = rng.sample(range(1, 65536), 5) for ident in ids[:4]: length = rng.choice([1, 17, 33, 65, 129, 256]) add('B %d %d' % (ident, length)) add('F %d 0 %d %d' % (ident, length, rng.randrange(256))) add('S %d' % ident) add('D %d' % ident) add('B %d 1' % ids[4]) add('B %d 1' % ids[0]) order = [ids[3], ids[1], ids[0], ids[2]] for ident in order: add('C %d %d' % (ident, rng.randrange(2))) observe(ids[:4]) # Reuse the same identifiers; coverage from the prior generation is gone. for ident in ids[:4]: add('B %d 5' % ident) add('W %d 1 123456' % ident) add('S %d' % ident) add('P %d' % ident) add('X %d' % ident) add('P %d' % ident) add('Q') # Argument errors must preserve an active draft and pending frame. add('B 1 4'); add('F 1 0 4 19'); add('S 1') add('B 65535 256'); add('W 65535 255 00') bad = ['B 0 1', 'B 65536 1', 'B -1 5', 'B 2 0', 'B 2 257', 'W 0 0 00', 'W 1 -1 00', 'W 1 256 00', 'W 1 3 0000', 'F 65535 0 0 2', 'F 65535 255 2 1', 'F 1 0 1 -1', 'F 1 0 1 256', 'F 999 0 1 7', 'S 0', 'S 999', 'X 999', 'C 65535 0', 'C 0 0', 'C 1 -1', 'P 65536', 'D 0', 'D 999', 'R 17', 'R -1', 'Q 1', 'B 1', 'W 1 0 f', 'W 1 0 zz', 'W 1 0 0x11', 'F 1 0 1 x', 'Z', 'BB 1', 'S 1 extra', 'B 2147483648 1', 'B --1 1'] for event in bad: add(event); observe([1, 65535]) add('C 1 1'); add('X 65535'); observe([1, 65535]) # Reset is also legal with live pending work. add('B 7 3'); add('F 7 0 3 255'); add('S 7') add('R %d' % line); observe([7]); add('C 7 0') events += ['', '# ignored comment', ' # ignored', 'Q'] return events def expected(events): slots = [None] * 4 totals = [0] * 7 outputs = [] def valid_id(value): return 1 <= value <= 65535 def find(ident): return next((i for i, s in enumerate(slots) if s and s['id'] == ident), None) arity = dict(R=1, B=2, W=3, F=4, S=1, C=2, X=1, P=1, D=1, Q=0) for text in events: fields = text.split() if not fields or fields[0].startswith('#'): continue op, *raw = fields args = [] try: if op not in arity or len(raw) != arity[op]: raise ValueError('syntax') for i, token in enumerate(raw): if op == 'W' and i == 2: if not token or len(token) % 2 or len(token) > 512 or any(c not in '0123456789abcdefABCDEF' for c in token): raise ValueError('hex') args.append(bytes.fromhex(token)) else: digits = token[1:] if token.startswith(('+', '-')) else token if not digits or any(c not in '0123456789' for c in digits): raise ValueError('decimal') value = int(token) if not -2147483648 <= value <= 2147483647: raise ValueError('int range') args.append(value) except ValueError: outputs.append('ERROR') continue rc = 0 ident = args[0] if args else 0 index = find(ident) frame = slots[index] if index is not None else None if op == 'R': if ident not in (16, 32, 64): rc = -1 else: slots = [None] * 4 totals = [0] * 7 elif op == 'B': length = args[1] if not valid_id(ident) or not 1 <= length <= 256: rc = -1 elif frame or all(slots): rc = -3 else: index = slots.index(None) slots[index] = dict(id=ident, length=length, state=1, data=bytearray(length), covered=set()) totals[0] += 1 elif op in ('W', 'F'): offset = args[1] data = args[2] if op == 'W' else bytes([args[3]]) * args[2] if 0 <= args[3] <= 255 and 1 <= args[2] <= 256 else b'' length = len(data) if op == 'W' else args[2] if (not valid_id(ident) or not 0 <= offset < 256 or not 1 <= length <= 256 or offset + length > 256 or op == 'F' and not 0 <= args[3] <= 255): rc = -1 elif not frame: rc = -2 elif offset + length > frame['length']: rc = -1 elif frame['state'] != 1: rc = -3 else: frame['data'][offset:offset + length] = data frame['covered'].update(range(offset, offset + length)) totals[1] += 1 elif op in ('S', 'X', 'C', 'P', 'D'): if not valid_id(ident) or op == 'C' and args[1] not in (0, 1): rc = -1 elif not frame: rc = -2 elif op in ('S', 'X') and frame['state'] != 1 or op == 'C' and frame['state'] != 2: rc = -3 elif op == 'S': if len(frame['covered']) != frame['length']: rc = -4 else: frame['state'] = 2 totals[2] += 1 elif op == 'X': slots[index] = None totals[5] += 1 elif op == 'C': totals[3 if args[1] == 0 else 4] += 1 if args[1] == 0: totals[6] += frame['length'] slots[index] = None if op == 'P': values = [rc, frame['state'], index, frame['length'], len(frame['covered'])] if rc == 0 else [rc, 0, 0, 0, 0] outputs.append('FRAME ' + ' '.join(map(str, values))) elif op == 'D': if frame and frame['state'] == 2 and rc == 0: outputs.append('DEVICE 0 %d %d %s' % (ident, frame['length'], frame['data'].hex())) else: outputs.append('DEVICE %d 0 0 -' % (-2 if rc == 0 else rc)) elif op == 'C': data = frame['data'] if rc == 0 and args[1] == 0 else b'' outputs.append('COMPLETE %d %d %d %s' % (rc, ident if rc == 0 else 0, len(data), data.hex() or '-')) elif op == 'Q': live = [sum(s is not None and s['state'] == state for s in slots) for state in (1, 2)] outputs.append('STATS ' + ' '.join(map(str, totals + live))) else: outputs.append('RESULT %s %d' % (op, rc)) return outputs # This child has the compiler/candidate UID, so it can terminate its own process tree. SUPERVISOR = r''' import ctypes,json,os,signal,subprocess,sys,time phase,deadline,command=sys.argv[1],float(sys.argv[2]),sys.argv[3:] input_data=sys.stdin.buffer.read() started=time.monotonic() proc=None stdout=stderr=b'' timed_out=False failure=None adopted_reaped=0 all_reaped=True actual_rc=None try: libc=ctypes.CDLL(None,use_errno=True) if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper setup failed') proc=subprocess.Popen(command,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE,start_new_session=True) try: stdout,stderr=proc.communicate(input_data,timeout=deadline) except subprocess.TimeoutExpired: timed_out=True except Exception as error: failure=type(error).__name__+': '+str(error) except Exception as error: failure=type(error).__name__+': '+str(error) finally: if proc is not None: try: os.killpg(proc.pid,signal.SIGKILL) except ProcessLookupError: pass except OSError as error: all_reaped=False failure='killpg: '+str(error) try: proc.wait(timeout=2) actual_rc=proc.returncode except Exception as error: all_reaped=False failure='wait: '+str(error) until=time.monotonic()+3 while True: try: with open('/proc/self/task/%d/children'%os.getpid()) as stream: children=[int(x) for x in stream.read().split()] for child in children: try: os.kill(child,signal.SIGKILL) except ProcessLookupError: pass while True: try: child,_=os.waitpid(-1,os.WNOHANG) except ChildProcessError: break if child==0: break adopted_reaped+=1 with open('/proc/self/task/%d/children'%os.getpid()) as stream: remaining=stream.read().split() if not remaining: break if time.monotonic()>=until: all_reaped=False break time.sleep(0.01) except Exception as error: all_reaped=False failure='descendant reap: '+str(error) break try: stdout,stderr=proc.communicate(timeout=2) except Exception as error: all_reaped=False failure='pipe drain: '+str(error) group_absent=True if proc is not None: try: os.killpg(proc.pid,0) group_absent=False except ProcessLookupError: pass except OSError: group_absent=False rc=125 if failure or not all_reaped or not group_absent else 124 if timed_out else actual_rc if rc is None: rc=125 sys.stdout.buffer.write(stdout) sys.stdout.buffer.flush() sys.stderr.buffer.write(stderr) sys.stderr.buffer.flush() facts=dict(phase=phase,uid=os.geteuid(),gid=os.getegid(),supervisor_pid=os.getpid(), child_pid=proc.pid if proc else None,deadline=deadline, elapsed_seconds=round(time.monotonic()-started,6),timed_out=timed_out, actual_returncode=actual_rc,returncode=rc,direct_reaped=proc is not None and actual_rc is not None, adopted_reaped=adopted_reaped,all_reaped=all_reaped,group_absent=group_absent, failure=failure) sys.stderr.write('\nEXEC '+json.dumps(facts,sort_keys=True)+'\n') sys.stderr.flush() raise SystemExit(128-rc if rc<0 else rc) ''' def execute(argv, cwd, deadline, phase, data=b''): result = subprocess.run([sys.executable, '-I', '-B', '-c', SUPERVISOR, phase, str(deadline), *map(str, argv)], cwd=cwd, input=data, capture_output=True) # bos_verify already forwards a failing child's stderr with replacement decoding. if result.returncode == 0: sys.stderr.write(result.stderr.decode('utf-8', 'replace')) return result ABSENCE = ("import json,os,sys; p=sys.argv[1]; exists=os.path.lexists(p); " "print(json.dumps(dict(path=p,exists=exists,uid=os.geteuid(),gid=os.getegid()))); " "raise SystemExit(1 if exists else 0)") def main(): events = scenarios() answers = expected(events) public = json.loads(Path(__file__).with_name('support.json').read_text()) source = (Path(sys.argv[1]) / 'tx_engine.c').read_bytes() stdin = ('\n'.join(events) + '\n').encode('utf-8') scratch = Path(tempfile.mkdtemp(prefix='dev-tx-frames-')) work = scratch / 'build' verdict = False # All inputs/expectations/support are ready before the first child starts. # After that point only same-UID children access the scratch filesystem. try: work.mkdir(mode=0o777) work.chmod(0o777) for name, content in public.items(): (work / name).write_text(content) (work / 'tx_engine.c').write_bytes(source) built = execute(['gcc', '-std=c11', '-O2', '-Wall', '-Wextra', '-Werror', '-pedantic', 'tx_engine.c', 'port.c', 'main.c', '-o', 'tx-demo'], work, 35, 'compile') if built.returncode: print('build failed rc=%d' % built.returncode, file=sys.stderr) else: run = execute([str(work / 'tx-demo')], work, 25, 'candidate', stdin) # stdout remains strict UTF-8 and is compared by the original protocol. observed = run.stdout.decode('utf-8').splitlines() verdict = run.returncode == 0 and observed == answers print('observations=%d, returned=%d, process_rc=%d' % (len(answers), len(observed), run.returncode), file=sys.stderr) if not verdict: for i in range(max(len(answers), len(observed))): want = answers[i] if i < len(answers) else '' got = observed[i] if i < len(observed) else '' if want != got: print('row %d: got %r expected %r' % (i + 1, got[:650], want[:650]), file=sys.stderr) break except Exception as error: print('execution failed: %s: %s' % (type(error).__name__, error), file=sys.stderr) verdict = False finally: clean_ok = False absence = None try: clean = execute(['rm', '-rf', '--', str(scratch)], '/', 20, 'cleanup') clean_ok = clean.returncode == 0 probe = execute([sys.executable, '-I', '-c', ABSENCE, str(scratch)], '/', 5, 'absence') absence = json.loads(probe.stdout.decode('utf-8')) clean_ok = clean_ok and probe.returncode == 0 and absence['exists'] is False except Exception as error: clean_ok = False print('cleanup failed: %s: %s' % (type(error).__name__, error), file=sys.stderr) verdict = verdict and clean_ok print('CLEANUP ' + json.dumps(dict(scratch=str(scratch),ok=clean_ok, absence=absence),sort_keys=True),file=sys.stderr) print('PASS' if verdict else 'FAIL') return 0 if verdict else 1 if __name__ == '__main__': raise SystemExit(main())