"""Independent stdin/API model; no candidate code runs in this process.""" import copy import hashlib import json from pathlib import Path import random import re import subprocess import sys import tempfile MAX = 4294967295 LOW = -(1 << 63) HIGH = (1 << 63) - 1 FILES = ['registers.cpp', 'sequencer.cpp', 'views.cpp', 'console.cpp'] def encoded(status, values=()): return (' '.join([status, str(len(values))] + [str(x) for x in values]) + '\n').encode('ascii') class Model: def __init__(self): self.devices = {s: [[[0, 0, 0, 0], [], []] for _ in range(8)] for s in 'ab'} @staticmethod def gates(device): for i, status in enumerate(['CLOCK', 'RESET', 'ENABLE']): if device[0][i] & 1 == 0: return status return 'OK' @staticmethod def put(device, reg, value): words, fifo, audit = device if reg == 1 and value & 1 and words[0] & 1 == 0: return 'CLOCK' if reg == 2 and value & 1: if words[0] & 1 == 0: return 'CLOCK' if words[1] & 1 == 0: return 'RESET' if reg == 3: status = Model.gates(device) if status != 'OK': return status if len(fifo) == 16: return 'FULL' words[reg] = value if reg == 1 and value & 1 == 0: words[2] &= MAX - 1 words[3] = 0 fifo.clear() if reg == 3: fifo.append(value) audit.append((reg, value)) audit[:] = audit[-32:] return 'OK' def operate(self, selector, operation, args): id = args[0] if not 0 <= id <= 7: return encoded('ID') device = self.devices[selector][id] words, fifo, audit = device if operation in ('read', 'write'): reg = args[1] if not 0 <= reg <= 3: return encoded('REG') if operation == 'read': return encoded('OK', [words[reg]]) value = args[2] if not 0 <= value <= MAX: return encoded('WORD') trial = copy.deepcopy(device) status = self.put(trial, reg, value) if status == 'OK': self.devices[selector][id] = trial return encoded(status) if operation == 'clear': self.devices[selector][id] = [[0, 0, 0, 0], [], []] return encoded('OK') if operation == 'snapshot': return encoded('OK', words + [len(fifo)] + fifo) if operation == 'history': count = args[1] if not 0 <= count <= 32: return encoded('COUNT') entries = audit[-count:] if count else [] return encoded('OK', [v for pair in entries for v in pair]) if operation == 'configure': vals = args[1:] if any(not 0 <= word <= MAX for word in vals): return encoded('WORD') for value, status in zip(vals, ['CLOCK', 'RESET', 'ENABLE']): if value & 1 == 0: return encoded(status) trial = copy.deepcopy(device) for reg, value in [(1, 0), (0, vals[0]), (1, vals[1]), (2, vals[2]), (3, vals[3])]: status = self.put(trial, reg, value) if status != 'OK': return encoded(status) self.devices[selector][id] = trial return encoded('OK') if operation == 'burst': vals = args[1:] if len(vals) > 16: return encoded('COUNT') if any(not 0 <= value <= MAX for value in vals): return encoded('WORD') if not vals: return encoded('OK') status = self.gates(device) if status != 'OK': return encoded(status) if len(vals) + len(fifo) > 16: return encoded('FULL') for value in vals: self.put(device, 3, value) return encoded('OK') if operation == 'drain': count = args[1] if not 0 <= count <= 16: return encoded('COUNT') if count == 0: return encoded('OK') status = self.gates(device) if status != 'OK': return encoded(status) if len(fifo) < count: return encoded('SHORT') result = fifo[:count] del fifo[:count] return encoded('OK', result) raise ValueError(operation) def text(self, line): tokens = line.split() if len(tokens) < 2 or tokens[0] not in ('a', 'b'): return encoded('SYNTAX') action = tokens[1] arities = {'read': 2, 'write': 3, 'configure': 5, 'drain': 2, 'history': 2, 'snapshot': 1, 'clear': 1} if action == 'burst': if len(tokens) < 3: return encoded('SYNTAX') elif action not in arities or len(tokens) != arities[action] + 2: return encoded('SYNTAX') numbers = tokens[2:] if any(re.fullmatch(r'-?[0-9]+', v) is None for v in numbers): return encoded('SYNTAX') args = [int(v) for v in numbers] if any(v < LOW or v > HIGH for v in args): return encoded('SYNTAX') return self.operate(tokens[0], action, args) def scenes(): result = [] def add(name, lines, mode='D', final=True): model = Model() if mode == 'E': expected = b''.join(encoded(line.split()[0], [int(x) for x in line.split()[1:]]) for line in lines) else: expected = b''.join(model.text(line) for line in lines) content = '\n'.join([mode] + lines) + ('\n' if final else '') result.append((name, content.encode('ascii'), expected, len(lines))) def observe(lines, id=0, s='a'): lines += [f'{s} read {id} {r}' for r in range(4)] lines += [f'{s} snapshot {id}', f'{s} history {id} 32'] def checked(lines, command, id=0, s='a'): lines.append(command) observe(lines, id, s) add('smoke', ['a configure 0 1 1 1 43981', 'a read 0 3', 'a snapshot 0', 'a history 0 32'], 'T') lines=[] for id in range(8): observe(lines,id) for id in range(8): lines.append(f'a configure {id} 1 1 1 {100+id}') for id in range(8): observe(lines,id) lines.append(f'a clear {id}') for id in (0, 7): for reg in range(4): lines.append(f'a read {id} {reg}') checked(lines,f'a configure {id} 1 3 5 71',id) for reg in range(4): for word in (0, MAX): lines += [f'a configure {id} 1 1 1 87'] checked(lines,f'a write {id} {reg} {word}',id) checked(lines,f'a clear {id}',id) checked(lines,f'a clear {id}',id) add('api_endpoints',lines) lines=['a configure 0 1 1 1 199'] for word in (2, MAX-1, 2147483648, 0): lines += ['a write 0 0 '+str(word)] checked(lines,'a write 0 3 93') checked(lines,'a burst 0 19 29') checked(lines,'a drain 0 1') lines += ['a write 0 0 1'] add('clock_bit',lines) lines=['a configure 0 7 9 11 123'] methods={'read':['0'],'write':['0','1'],'configure':['1','1','1','5'], 'burst':['8'],'drain':['1'],'history':['32'],'snapshot':[],'clear':[]} for action, tail in methods.items(): for id in (-1,8,LOW,HIGH): checked(lines,' '.join(['a',action,str(id)]+tail)) for action in ('read','write'): for reg in (-1,4,LOW,HIGH): tail=' 5' if action=='write' else '' checked(lines,f'a {action} 0 {reg}{tail}') add('id_reg_domains',lines) lines=['a configure 0 7 9 11 345'] for bad in (-1, MAX+1, LOW, HIGH): for reg in range(4): checked(lines,f'a write 0 {reg} {bad}') for slot in range(4): vals=[1,1,1,456];vals[slot]=bad checked(lines,'a configure 0 '+' '.join(map(str,vals))) checked(lines,f'a burst 0 9 {bad} 3') for slot in range(4): for word in (0,MAX): vals=[1,1,1,99];vals[slot]=word checked(lines,'a configure 0 '+' '.join(map(str,vals))) add('word_domains',lines) lines=[] for id in (0,7): lines += [f'a configure {id} 1 1 1 333'] checked(lines,f'a burst {id}',id) for action,top in [('drain',16),('history',32)]: for count in (-1,top+1,LOW,HIGH,0): checked(lines,f'a {action} {id} {count}',id) lines += [f'a drain {id} 1'] checked(lines,f'a burst {id} 0 {MAX}',id) checked(lines,f'a drain {id} 2',id) checked(lines,f'a burst {id} '+' '.join(map(str,range(16))),id) checked(lines,f'a drain {id} 16',id) checked(lines,f'a burst {id} '+' '.join(['1']*17),id) for i in range(40): lines.append(f'a write {id} 0 {i|1}') checked(lines,f'a history {id} 32',id) lines += [f'a write {id} 0 0'] checked(lines,f'a burst {id}',id) checked(lines,f'a drain {id} 0',id) add('count_domains',lines) lines=['a configure 0 1 1 1 12'] for line in ['a write -1 -1 -1','a write 0 -1 -1','a configure 0 0 -1 0 0', 'a configure 0 0 0 0 0','a configure 0 1 0 0 0','a configure 0 1 1 0 0', 'a burst 0 '+' '.join(['-1']*17),'a drain -1 -1','a history -1 -1']: checked(lines,line) lines += ['a drain 0 1','a burst 0 '+' '.join(['91']*16)] checked(lines,'a write 0 3 72') checked(lines,'a burst 0 1') lines += ['a write 0 0 2'] checked(lines,'a write 0 3 91') lines += ['a write 0 0 1','a write 0 1 0'] checked(lines,'a write 0 2 1') checked(lines,'a write 0 3 91') lines += ['a write 0 1 1'] checked(lines,'a write 0 3 91') checked(lines,'a burst 0 -1') lines += ['a write 0 2 1'] checked(lines,'a drain 0 1') add('atomic_priority',lines) lines=[] for id in (0,7): lines += [f'a configure {id} 3 5 7 {101+id}',f'b configure {id} 9 11 13 {201+id}'] observe(lines,id,'a');observe(lines,id,'b') for s in 'ab': checked(lines,f'{s} write {id} 3 {301+id}',id,s) observe(lines,id,'a' if s=='b' else 'b') lines += [f'a clear {id}'] observe(lines,id,'b');observe(lines,id,'a') add('instances',lines) lines=['a configure 0 3 5 7 55','a drain 0 1'] for round in range(4): checked(lines,'a burst 0 '+' '.join(str(round*16+i) for i in range(16))) checked(lines,'a history 0 1') checked(lines,'a drain 0 16') checked(lines,'a configure 0 17 19 21 999') checked(lines,'a configure 0 23 25 27 0') add('fifo_audit',lines) lines=['a configure 0 3 5 4294967295 101','a write 0 3 9'] checked(lines,'a write 0 1 4294967294') lines += ['a write 0 2 4294967294','a write 0 0 2'] checked(lines,'a write 0 1 2') checked(lines,'a write 0 2 2') checked(lines,'a write 0 1 3') checked(lines,'a write 0 2 3') add('reset_bits',lines) rng=random.Random(803981) lines=[] pool=[0,1,2,3,7,MAX,MAX-1,-1,MAX+1] for i in range(500): s=rng.choice('ab');id=rng.choice([0,1,7]) action=rng.choice(['write','configure','burst','drain','history','clear','read']) if action=='write': tail=[rng.randrange(4),rng.choice(pool)] elif action=='configure':tail=[rng.choice(pool) for _ in range(4)] elif action=='burst':tail=[rng.choice(pool) for _ in range(rng.randrange(5))] elif action=='drain':tail=[rng.choice([0,1,16,17])] elif action=='history':tail=[rng.choice([0,1,32,33])] elif action=='read':tail=[rng.randrange(4)] else:tail=[] checked(lines,' '.join(map(str,[s,action,id]+tail)),id,s) add('mixed',lines) lines=['',' \t ','c read 0 0','a bogus 0','a read','a read 0 0 1', 'a write 0 0 +1','a write 0 0 1x','a read 0 '+str(HIGH+1), 'a read 0 '+str(LOW-1),'a read 0 --1','a burst','a configure 0 1 1 1', 'a write 0 0 1.0','a snapshot 0 0','a clear 0 0','a drain 0', 'a history 0 0 0','a configure 0 1 1 1 2 3', ' a\tconfigure\t000 0001 01 1 81\t ', 'a read -0 03','a burst 0 -0 0001','a drain 0 0003', 'a history 0 32','a snapshot 0', 'a read 0 0'+' '*1014] initial = lines lines = ['a configure 0 3 5 7 81', 'b configure 0 9 11 13 91'] for line in initial: lines.append(line) observe(lines,0,'a') observe(lines,0,'b') fixed = ['a read 0 0', 'a write 0 3 4', 'a configure 0 1 1 1 5', 'a drain 0 1', 'a history 0 1', 'a snapshot 0', 'a clear 0'] for line in fixed: tokens = line.split() for invalid in [' '.join(tokens[:-1]), line+' 99']: lines.append(invalid) observe(lines,0,'a') observe(lines,0,'b') for invalid in ['a burst 0 12 x', 'a write 0 3 9223372036854775807', 'a write 0 3 -9223372036854775808']: lines.append(invalid) observe(lines,0,'a') observe(lines,0,'b') add('text_syntax',lines,'T') lines=['a configure 0 3 5 7 81','b configure 0 9 11 13 91', 'a write 0 3 0','a read 0 3','b read 0 3', 'a burst 0 0 4294967295','a drain 0 4', 'a history 0 32','a snapshot 0','b snapshot 0', 'a write 0 0 2','a write 0 3 17', 'a read 0 3','a snapshot 0','a history 0 32', 'b burst 0 4294967295 0','b drain 0 3','b read 0 3', 'a clear 0','a snapshot 0','a history 0 32', 'a read 0 0','a read 0 1','a read 0 2','a read 0 3', 'b snapshot 0','b history 0 32','b clear 0', 'b snapshot 0','b history 0 32','a clear 0'] add('text_verbs',lines,'T') lines=['a configure 0 1 1 1 91']+['a read 0 3']*4094+['a history 0 32'] add('command_limit',lines,'T') add('empty',[],'T') add('no_final_lf',['a configure 7 1 1 1 0','b configure 7 1 1 1 4294967295','a read 7 3','b read 7 3'],'T',False) add('encode',['OK','WORD','OK 0 4294967295','CUSTOM 1 2 3'],'E') return result def main(): source = Path(sys.argv[1]) trusted = Path(__file__).resolve().parent # The platform supplies candidate files only. Immutable headers are captured in observer.cc's companion data below. header = HEADER console_header = CONSOLE_HEADER cases = scenes() contents = {name: (source / name).read_bytes() for name in FILES} observer = (trusted / 'observer.cc').read_bytes() scratch = Path(tempfile.mkdtemp(prefix='clock-model-', dir=Path.cwd())) for name, content in contents.items(): (scratch / name).write_bytes(content) (scratch / 'model.hpp').write_text(header) (scratch / 'console.hpp').write_text(console_header) (scratch / 'observer.cc').write_bytes(observer) # All trusted filesystem I/O is complete before the first compiler executes. argv=['g++','-std=c++17','-O2',*[str(scratch/name) for name in FILES],str(scratch/'observer.cc'),'-o',str(scratch/'observer')] proc = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate(timeout=30) evidence={'v':1,'build':{'pid':proc.pid,'argv':argv,'rc':proc.returncode,'stdout':out.decode('utf8','replace'),'stderr':err.decode('utf8','replace')},'run_argv':[str(scratch/'observer')],'scenes':[]} failures=[] if proc.returncode: failures.append('build') else: for name, data, expected, count in cases: run=subprocess.Popen([str(scratch/'observer')],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE) try: actual, diagnostics=run.communicate(data,timeout=3) code=run.returncode except subprocess.TimeoutExpired: run.kill();actual,diagnostics=run.communicate();code=run.returncode reasons=[] if code:reasons.append('exit') if diagnostics:reasons.append('stderr') if actual!=expected:reasons.append('bytes') if reasons:failures.append(name+':'+','.join(reasons)) evidence['scenes'].append([name,run.pid,code,len(actual),len(diagnostics),count,','.join(reasons) or 'ok',hashlib.sha256(actual).hexdigest()[:12],diagnostics.decode('utf8','replace')[:20]]) message=json.dumps(evidence,separators=(',',':')) sys.stderr.write(message+'\n') print('FAIL '+(';'.join(failures)) if failures else 'PASS') return bool(failures) HEADER = '#ifndef CLOCK_MODEL_HPP\n#define CLOCK_MODEL_HPP\n#include \n#include \n#include \n#include \n#include \nnamespace clockmodel {\nusing Arg = std::int64_t;\nusing Word = std::uint32_t;\nstruct Reply {\n std::string status;\n std::vector values;\n};\nstruct Event {\n Word reg;\n Word value;\n};\nstruct Device {\n std::array regs{};\n std::deque fifo;\n std::deque events;\n};\nclass Bus {\npublic:\n Bus();\n Reply read(Arg id, Arg reg) const;\n Reply write(Arg id, Arg reg, Arg value);\n Reply configure(Arg id, Arg clock, Arg reset, Arg enable, Arg data);\n Reply burst(Arg id, const std::vector& values);\n Reply drain(Arg id, Arg count);\n Reply snapshot(Arg id) const;\n Reply history(Arg id, Arg count) const;\n Reply clear(Arg id);\nprivate:\n std::array devices_;\n static bool valid_id(Arg id);\n static bool valid_reg(Arg reg);\n static bool valid_word(Arg value);\n static bool valid_count(Arg count, Arg maximum);\n static bool clock_on(const Device& device);\n static bool reset_released(const Device& device);\n static bool enabled(const Device& device);\n static Reply error(const std::string& code);\n static Reply success();\n static Reply single(Word value);\n static void record(Device& device, Word reg, Word value);\n static std::string data_permission(const Device& device);\n static std::string apply(Device& device, Word reg, Word value);\n static std::string set_clock(Device& device, Word value);\n static std::string set_reset(Device& device, Word value);\n static std::string set_enable(Device& device, Word value);\n static std::string set_data(Device& device, Word value);\n static std::vector export_snapshot(const Device& device);\n static std::vector export_history(const Device& device, std::size_t count);\n};\n}\n#endif\n' CONSOLE_HEADER = '#ifndef CLOCK_CONSOLE_HPP\n#define CLOCK_CONSOLE_HPP\n#include "model.hpp"\nnamespace clockmodel {\nReply command(Bus& first, Bus& second, const std::string& line);\nstd::string encode(const Reply& reply);\n}\n#endif\n' if __name__ == '__main__': sys.exit(main())