import copy from pathlib import Path import random import subprocess import sys import tempfile LIMIT = 10**12 MAX = 2**64 - 1 CANDIDATES = ('mailbox.hpp', 'mailbox.cpp') CONTEXT = {'observer.cpp': '#include "mailbox.hpp"\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nnamespace {\nusing mq::Number;\n\nbool number(const std::string &text, Number &out)\n{\n if (text.empty()) return false;\n for (char byte : text) {\n if (byte < \'0\' || byte > \'9\') return false;\n }\n const auto result = std::from_chars(text.data(), text.data() + text.size(), out);\n return result.ec == std::errc{} && result.ptr == text.data() + text.size();\n}\n\nint nibble(char byte)\n{\n if (byte >= \'0\' && byte <= \'9\') return byte - \'0\';\n if (byte >= \'a\' && byte <= \'f\') return byte - \'a\' + 10;\n if (byte >= \'A\' && byte <= \'F\') return byte - \'A\' + 10;\n return -1;\n}\n\nbool decode(const std::string &text, std::string &out)\n{\n if (text == "-") {\n out.clear();\n return true;\n }\n if (text.empty() || text.size() % 2 != 0) return false;\n std::string value;\n for (std::size_t index = 0; index < text.size(); index += 2) {\n const int high = nibble(text[index]);\n const int low = nibble(text[index + 1]);\n if (high < 0 || low < 0) return false;\n value.push_back(static_cast(high * 16 + low));\n }\n out = std::move(value);\n return true;\n}\n\nvoid hex(const std::string &body)\n{\n const char digits[] = "0123456789abcdef";\n if (body.empty()) std::cout << \'-\';\n for (unsigned char byte : body) {\n std::cout << digits[byte >> 4] << digits[byte & 15];\n }\n}\n\nvoid record(const mq::Record &value)\n{\n std::cout << \' \' << value.id << \' \' << value.priority << \' \' << value.ticket\n << \' \' << value.expires << \' \';\n hex(value.body);\n}\n\nmq::Record seeded(Number nonce)\n{\n return {nonce, nonce, nonce, nonce, "seed"};\n}\n\nbool execute(mq::Mailbox (&boxes)[3], unsigned &selected,\n const std::vector &args)\n{\n const std::string &op = args[0];\n mq::Mailbox &box = boxes[selected];\n Number a = 0, b = 0, c = 0, d = 0;\n if (op == "U" && args.size() == 2 && number(args[1], a) && a < 3) {\n selected = static_cast(a);\n std::cout << "U " << a << \'\\n\';\n } else if ((op == "CP" || op == "CC" || op == "MV" || op == "MC") &&\n args.size() == 3 && number(args[1], a) && number(args[2], b) && a < 3 && b < 3) {\n if (op == "CP") boxes[b] = boxes[a];\n if (op == "CC") {\n mq::Mailbox copied(boxes[a]);\n boxes[b] = std::move(copied);\n }\n if (op == "MV") boxes[b] = std::move(boxes[a]);\n if (op == "MC") {\n mq::Mailbox moved(std::move(boxes[a]));\n boxes[b] = std::move(moved);\n }\n std::cout << "R 0\\n";\n } else if (op == "TRAITS" && args.size() == 1) {\n std::cout << "T " << std::is_copy_constructible::value << \' \'\n << std::is_copy_assignable::value << \' \'\n << std::is_move_constructible::value << \' \'\n << std::is_move_assignable::value << \'\\n\';\n } else if (op == "Z" && args.size() == 1) {\n box.reset();\n std::cout << "R 0\\n";\n } else if ((op == "C" || op == "L") && args.size() == 5 &&\n number(args[1], a) && number(args[2], b) && number(args[3], c) && number(args[4], d)) {\n const mq::Config config{a,b,c,d};\n const int status = op == "C" ? box.configure(config) : box.resize(config);\n std::cout << "R " << status << \'\\n\';\n } else if (op == "S" && args.size() == 5 && number(args[1], a) &&\n number(args[2], b) && number(args[3], c)) {\n std::string body;\n if (!decode(args[4], body)) return false;\n Number ticket = 777;\n const int status = box.send({a,b,c,body}, ticket);\n std::cout << "S " << status << \' \' << ticket << \'\\n\';\n } else if ((op == "P" || op == "R") && args.size() == 2 && number(args[1], a)) {\n mq::Record out = seeded(a);\n const int status = op == "P" ? box.peek(out) : box.receive(out);\n std::cout << op << \' \' << status;\n record(out);\n std::cout << \'\\n\';\n } else if (op == "X" && args.size() == 2 && number(args[1], a)) {\n std::cout << "R " << box.cancel(a) << \'\\n\';\n } else if (op == "H" && args.size() == 3 && number(args[1], a) && number(args[2], b)) {\n std::cout << "R " << box.reprioritize(a, b) << \'\\n\';\n } else if (op == "I" && args.size() == 3 && number(args[1], a) && number(args[2], b)) {\n mq::Record out = seeded(b);\n const int status = box.inspect_id(a, out);\n std::cout << "I " << status;\n record(out);\n std::cout << \'\\n\';\n } else if (op == "N" && args.size() == 3 && number(args[1], a) && number(args[2], b)) {\n Number old_expiry = 777;\n const int status = box.renew(a, b, old_expiry);\n std::cout << "N " << status << \' \' << old_expiry << \'\\n\';\n } else if (op == "K" && args.size() == 2 && number(args[1], a)) {\n std::vector ids{777,888};\n const int status = box.cancel_priority(a, ids);\n std::cout << "K " << status << \' \' << ids.size();\n for (Number id : ids) std::cout << \' \' << id;\n std::cout << \'\\n\';\n } else if (op == "A" && args.size() == 2 && number(args[1], a)) {\n std::cout << "R " << box.advance(a) << \'\\n\';\n } else if (op == "G" && args.size() == 2 && number(args[1], a) && a <= 1) {\n box.set_closed(a != 0);\n std::cout << "R 0\\n";\n } else if (op == "D" && args.size() == 4 && number(args[1], a) &&\n number(args[2], b) && number(args[3], c)) {\n std::vector out{seeded(c)};\n const int status = box.drain(a, b, out);\n std::cout << "D " << status << \' \' << out.size();\n for (const auto &value : out) record(value);\n std::cout << \'\\n\';\n } else if (op == "V" && args.size() == 1) {\n std::vector out;\n box.snapshot(out);\n std::cout << "V " << out.size();\n for (const auto &value : out) record(value);\n std::cout << \'\\n\';\n } else if (op == "F" && args.size() == 1) {\n std::cout << "F " << box.closed() << \'\\n\';\n } else if (op == "Q" && args.size() == 1) {\n const mq::View v = box.inspect();\n const mq::Counters &t = v.counters;\n std::cout << "Q " << v.config.count << \' \' << v.config.bytes << \' \' << v.config.body\n << \' \' << v.config.priority << \' \' << v.now << \' \' << v.count << \' \' << v.bytes\n << \' \' << v.next_ticket << \' \' << t.sent << \' \' << t.sent_bytes\n << \' \' << t.received << \' \' << t.received_bytes << \' \' << t.canceled\n << \' \' << t.expired << \' \' << t.reprioritized << \' \' << t.revision\n << \' \' << v.closed << \'\\n\';\n } else if (op == "B" && args.size() >= 2 && number(args[1], a) && a <= 32 &&\n args.size() == 2 + 5 * a) {\n std::vector edits;\n for (std::size_t index = 0; index < a; ++index) {\n const std::size_t start = 2 + 5 * index;\n Number kind = 0;\n mq::Edit edit;\n if (!number(args[start], kind) || kind > static_cast(std::numeric_limits::max()) ||\n !number(args[start + 1], edit.input.id) || !number(args[start + 2], edit.input.priority) ||\n !number(args[start + 3], edit.input.ttl) || !decode(args[start + 4], edit.input.body)) return false;\n edit.kind = static_cast(kind);\n edits.push_back(std::move(edit));\n }\n std::vector tickets{777,888};\n const int status = box.batch(edits, tickets);\n std::cout << "B " << status << \' \' << tickets.size();\n for (Number ticket : tickets) std::cout << \' \' << ticket;\n std::cout << \'\\n\';\n } else {\n return false;\n }\n return true;\n}\n}\n\nint main()\n{\n mq::Mailbox boxes[3];\n unsigned selected = 0;\n std::string line;\n while (std::getline(std::cin, line)) {\n std::istringstream input(line);\n std::vector args;\n std::string word;\n while (input >> word) args.push_back(word);\n if (args.empty() || args[0][0] == \'#\') continue;\n if (!execute(boxes, selected, args)) std::cout << "PROTO\\n";\n }\n return std::cin.bad() ? 2 : 0;\n}\n'} def enc(body): return body.hex() or '-' def dec(text): return b'' if text == '-' else bytes.fromhex(text) def show(record): return '%d %d %d %d %s' % (record['id'],record['p'],record['t'],record['e'],enc(record['b'])) def seed(nonce): return dict(id=nonce,p=nonce,t=nonce,e=nonce,b=b'seed') class Model: def __init__(self, config=(8,1024,256,31)): self.config = tuple(config); self.now = 0; self.next = 1; self.closed = False self.messages = []; self.stats = dict(sent=0,sent_bytes=0,received=0,received_bytes=0,canceled=0,expired=0,reprioritized=0,revision=0) def add(self, name, amount=1): self.stats[name] = min(MAX,self.stats[name]+amount) def bytes(self): return sum(len(r['b']) for r in self.messages) def find(self, ident): return next((r for r in self.messages if r['id'] == ident),None) def ordered(self): return sorted(self.messages,key=lambda r:(-r['p'],r['t'])) def empty(self): return -61 if self.closed else -11 @staticmethod def valid(config): count,capacity,body,priority = config return 1 <= count <= 64 and 1 <= capacity <= 4096 and 1 <= body <= min(256,capacity) and priority <= 31 def configure(self, config): if not self.valid(config): return -22 self.__init__(config); return 0 def resize(self, config): if not self.valid(config): return -22 count,capacity,body,priority = config if len(self.messages)>count or self.bytes()>capacity or any(len(r['b'])>body or r['p']>priority for r in self.messages): return -28 self.config=tuple(config); self.add('revision'); return 0 def send(self, ident, priority, ttl, body): count,capacity,body_limit,priority_limit = self.config if self.closed: return -32,777 if ident == 0 or priority>priority_limit: return -22,777 if len(body)>body_limit: return -90,777 if ttl>LIMIT or ttl and self.now+ttl>LIMIT: return -34,777 if self.find(ident) is not None: return -17,777 if self.next == MAX: return -75,777 if len(self.messages)>=count or self.bytes()+len(body)>capacity: return -28,777 ticket=self.next; self.next+=1 self.messages.append(dict(id=ident,p=priority,t=ticket,e=self.now+ttl if ttl else 0,b=body)) self.add('sent'); self.add('sent_bytes',len(body)); self.add('revision') return 0,ticket def take(self, consume, nonce): if not self.messages: return self.empty(),seed(nonce) record=copy.deepcopy(self.ordered()[0]) if consume: self.messages.remove(record); self.add('received'); self.add('received_bytes',len(record['b'])); self.add('revision') return 0,record def cancel(self, ident): if not ident: return -22 record=self.find(ident) if record is None: return -2 self.messages.remove(record); self.add('canceled'); self.add('revision'); return 0 def reprioritize(self, ident, priority): if not ident or priority>self.config[3]: return -22 record=self.find(ident) if record is None: return -2 record['p']=priority; self.add('reprioritized'); self.add('revision'); return 0 def advance(self, delta): if delta>LIMIT or self.now+delta>LIMIT: return -34 self.now+=delta survivors=[r for r in self.messages if not r['e'] or r['e']>self.now] self.add('expired',len(self.messages)-len(survivors)); self.messages=survivors; self.add('revision'); return 0 def inspect_id(self, ident, nonce): if not ident: return -22,seed(nonce) record=self.find(ident) return (-2,seed(nonce)) if record is None else (0,copy.deepcopy(record)) def renew(self, ident, ttl): if not ident: return -22,777 record=self.find(ident) if record is None: return -2,777 if ttl>LIMIT or ttl and self.now+ttl>LIMIT: return -34,777 old=record['e']; record['e']=self.now+ttl if ttl else 0; self.add('revision'); return 0,old def cancel_priority(self, priority): if priority>self.config[3]: return -22,[777,888] matched=sorted((r for r in self.messages if r['p']==priority),key=lambda r:r['t']) self.messages=[r for r in self.messages if r['p']!=priority] self.add('canceled',len(matched)); self.add('revision'); return 0,[r['id'] for r in matched] def drain(self, limit, budget, nonce): if not 1<=limit<=64: return -22,[seed(nonce)] if budget>4096: return -34,[seed(nonce)] if not self.messages: return self.empty(),[seed(nonce)] ordered=self.ordered() if len(ordered[0]['b'])>budget: return -90,[seed(nonce)] result=[]; used=0 for record in ordered: if len(result)==limit or used+len(record['b'])>budget: break used+=len(record['b']); result.append(copy.deepcopy(record)) ids={r['id'] for r in result}; self.messages=[r for r in self.messages if r['id'] not in ids] self.add('received',len(result)); self.add('received_bytes',used); self.add('revision'); return 0,result def batch(self, edits): if len(edits)>16: return -22,[777,888] tentative=copy.deepcopy(self); tickets=[] for kind,ident,priority,ttl,body in edits: if kind==1: status,ticket=tentative.send(ident,priority,ttl,body) elif kind==2: status=tentative.cancel(ident); ticket=0 elif kind==3: status=tentative.reprioritize(ident,priority); ticket=0 else: status=-22; ticket=0 if status: return status,[777,888] tickets.append(ticket) self.__dict__=tentative.__dict__; return 0,tickets def view(self): values=list(self.config)+[self.now,len(self.messages),self.bytes(),self.next] values += [self.stats[k] for k in ('sent','sent_bytes','received','received_bytes','canceled','expired','reprioritized','revision')] return values+[int(self.closed)] class Suite: def __init__(self): self.boxes=[Model(),Model(),Model()]; self.selected=0; self.commands=[]; self.expected=[]; self.labels=[] def add(self, command, label='state-sequence', clauses='C02-C20'): words=command.split(); op=words[0]; args=words[1:]; box=self.boxes[self.selected] if op=='U': self.selected=int(args[0]); result='U '+args[0] elif op in ('CP','CC','MV','MC'): source,target=map(int,args) if source!=target: self.boxes[target]=copy.deepcopy(self.boxes[source]) if op in ('MV','MC'): self.boxes[source]=Model() result='R 0' elif op=='TRAITS': result='T 1 1 1 1' elif op=='Z': box.__init__(); result='R 0' elif op in ('C','L'): status=(box.configure if op=='C' else box.resize)(list(map(int,args))); result='R '+str(status) elif op=='S': status,ticket=box.send(*map(int,args[:3]),dec(args[3])); result='S %d %d' % (status,ticket) elif op in ('P','R'): status,record=box.take(op=='R',int(args[0])); result=op+' '+str(status)+' '+show(record) elif op=='I': status,record=box.inspect_id(*map(int,args)); result='I '+str(status)+' '+show(record) elif op=='N': status,old=box.renew(*map(int,args)); result='N %d %d' % (status,old) elif op=='K': status,ids=box.cancel_priority(int(args[0])); result='K %d %d' % (status,len(ids))+''.join(' '+str(x) for x in ids) elif op in ('X','H','A'): status={'X':box.cancel,'H':box.reprioritize,'A':box.advance}[op](*map(int,args)); result='R '+str(status) elif op=='G': value=bool(int(args[0])) if value!=box.closed: box.add('revision'); box.closed=value result='R 0' elif op=='F': result='F '+str(int(box.closed)) elif op=='D': status,records=box.drain(*map(int,args)); result='D %d %d' % (status,len(records))+''.join(' '+show(r) for r in records) elif op=='V': records=box.ordered(); result='V '+str(len(records))+''.join(' '+show(r) for r in records) elif op=='Q': result='Q '+' '.join(map(str,box.view())) elif op=='B': n=int(args[0]); edits=[] for i in range(n): at=1+5*i; edits.append((*map(int,args[at:at+4]),dec(args[at+4]))) status,tickets=box.batch(edits); result='B %d %d' % (status,len(tickets))+''.join(' '+str(x) for x in tickets) else: raise ValueError(command) self.commands.append(command); self.expected.append(result); self.labels.append((label,clauses)) def bad(self, command): self.commands.append(command); self.expected.append('PROTO'); self.labels.append(('strict-observer-parse','C15-C18')) def batch(self, edits, label='batch-atomic'): command='B '+str(len(edits)) for kind,ident,priority,ttl,body in edits: command+=' %d %d %d %d %s' % (kind,ident,priority,ttl,enc(body)) self.add(command,label,'C05-C07,C09,C14') def audit(self): self.add('Q','full-state','C02-C14,C20'); self.add('V','ordered-snapshot','C08,C13'); self.add('F','closed-accessor','C11') def cases(): s=Suite(); c=s.add; rng=random.Random(0x6D71018) c('TRAITS','value-type-availability','C03'); s.audit() for op in ('P 81','R 83','D 1 4096 89','I 0 97','I 1 101','N 0 1','N 1 1','X 0','X 1','H 0 1','H 1 32','H 1 1','K 32','K 0','B 0'): c(op,'empty-output-atomicity','C05,C08-C14,C20') c('C 8 64 16 9') for command in ('S 90 2 0 61','S 4 7 0 62','S 2 7 0 63','V','D 3 64 123','Q'): c(command,'public-reproduction','C07-C08,C12-C13') for priority in (0,1,7,31): c('Z'); c('A 19') for ident in (99,1,MAX,12): c(f'S {ident} {priority} 0 00ff') c('P 131'); c('V'); c(f'H 99 {priority}'); c('R 137'); c(f'S 99 {priority} 0 -'); c('D 64 4096 139'); s.audit() c('Z'); c('S 20 1 0 6161'); c('S 10 20 0 62'); c('S 30 1 0 636363') c('H 20 20'); c('V','reprioritize-retains-arrival','C08-C09'); c('H 10 1'); c('H 20 1'); c('V'); c('K 1'); s.audit() for config in ('0 1 1 0','65 4096 256 31','1 0 1 0','1 4097 1 0','1 1 0 0','1 4096 257 0','1 1 2 0','1 1 1 32',f'{MAX} {MAX} {MAX} {MAX}'): c('C '+config,'configure-invalid-atomic','C04'); s.audit() c('C 2 3 3 3'); c('S 1 1 0 6161'); c('S 2 2 0 62') for command in ('S 3 3 0 -','S 1 1 0 -','S 0 99 1000000000001 61616161','S 1 1 1000000000001 61616161','S 1 1 1000000000001 -','S 3 4 0 -'): c(command,'send-error-precedence','C05-C07'); s.audit() c('X 1'); c('S 3 3 0 6161'); c('R 143'); c('S 2 1 0 -'); c('X 2'); c('X 2'); s.audit() c('Z'); c('C 64 4096 256 31') for ident in range(1,17): c(f'S {ident} {ident%3} 0 '+enc(bytes([ident])*256)) c('S 17 31 0 00','byte-capacity-full','C06-C07'); c('L 64 4096 255 31'); c('L 64 4096 256 1'); c('L 15 4096 256 31'); c('L 64 4095 256 31'); s.audit() c('K 1'); c('S 17 31 0 00'); c('D 64 4096 149'); c('C 64 1 1 0') for ident in range(1,65): c(f'S {ident} 0 0 -') c('S 65 0 0 -','count-capacity-empty-bodies','C06-C07'); c('D 64 0 151'); s.audit() c('C 4 10 5 9'); c('S 8 8 0 000102'); c('S 7 7 5 03040506'); c('S 6 6 5 -'); c('S 5 5 6 ff') c('A 4'); s.audit(); c('A 1','inclusive-expiry-not-only-head','C10'); s.audit(); c('S 7 7 0 80818283'); c('S 6 6 0 -'); c('A 1'); s.audit() c('Z'); c('S 1 1 5 00'); c('A 3'); c('N 1 5'); c('I 1 157'); c('A 2'); c('P 163'); c('A 3'); c('I 1 167'); s.audit() c('S 1 1 1 -'); c('N 1 0'); c('A 10'); c('I 1 173'); c('N 0 1000000000001'); c('N 9 1000000000001'); c('N 1 1000000000001'); s.audit() c('C 2 2 2 0'); c(f'A {LIMIT-1}'); c('S 1 0 1 00'); c('S 2 0 2 -'); c('A 1'); c('S 1 0 1 -'); c('S 1 0 0 -'); c('A 1'); c(f'A {MAX}'); c('A 0'); c('N 1 1'); c('N 1 0'); s.audit() c('Z'); c('G 1'); c('G 1'); c('S 0 99 1000000000001 '+enc(b'x'*257)); c('R 179'); c('D 0 4097 181'); c('D 1 4097 191'); c('D 1 0 193'); c('F'); c('G 0'); c('S 1 7 2 00'); c('G 1'); c('H 1 6'); c('N 1 4'); c('P 197'); c('K 6'); c('R 199'); c('G 0'); s.audit() c('Z'); c('S 1 4 0 -'); c('S 2 3 0 0001'); c('S 3 2 0 020304'); c('S 4 1 0 -') c('D 0 0 211'); c('D 65 0 223'); c(f'D {MAX} {MAX} 227'); c('D 1 0 229'); c('D 4 1 233'); c('D 4 4 239'); c('P 241'); c('D 4 3 251'); s.audit() c('Z'); c('C 3 6 3 9'); c('S 1 1 0 61'); c('S 2 2 0 6262'); c('A 11') s.batch([(2,1,MAX,MAX,b'ignored'*50),(1,1,9,3,b'new'),(3,2,9,MAX,b'ignored'*50)],'batch-ordered-success-ignored-fields'); s.audit() for fail in ((1,1,0,0,b''),(2,99,0,0,b''),(3,2,32,0,b''),(0,0,0,0,b''),(1,99,0,LIMIT+1,b''),(1,99,0,0,b'long')): s.batch([(2,2,0,0,b''),(1,77,1,0,b'x'),fail],'rollback-after-mixed-edits'); s.audit() for position in range(16): c('Z'); c('C 64 4096 256 31') edits=[(1,100+i,i%5,0,bytes([i])) for i in range(16)] edits[position]=(9,999,0,0,b'') s.batch(edits,'rollback-every-position'); s.audit(); c('S 999 1 0 00','rollback-ticket-reuse','C07,C14') c('Z'); c('C 64 4096 256 31'); s.batch([(1,i+1,i%5,0,b'') for i in range(16)],'batch-max-success'); s.audit() s.batch([(2,1,0,0,b'')]*17,'batch-count-before-processing'); s.audit() c('G 1'); s.batch([(2,1,0,0,b''),(1,99,1,0,b'')],'closed-batch-rollback'); s.audit(); c('B 0'); s.audit() for mode in ('CP','CC','MV','MC'): c('U 0'); c('C 4 20 7 9'); c('S 100 7 8 00ff'); c('S 3 1 0 7f'); c('A 3'); c('H 3 6'); c('G 1') c('U 1'); c('Z'); c('S 9 1 0 0000'); c(mode+' 0 1','value-transfer','C02-C03'); s.audit() c('G 0'); c('N 100 1'); c('A 1'); c('S 44 9 0 -'); s.audit() c('U 0'); s.audit(); c(mode+' 0 0','self-value-transfer','C03'); s.audit() c('U 2'); c('Z'); c('S 100 0 0 45'); c('A 7'); s.audit(); c('U 0'); s.audit() for _ in range(1000): op=rng.randrange(17); box=s.boxes[s.selected]; nonce=rng.randrange(1,MAX) if op==0: c('U '+str(rng.randrange(3))) elif op==1: c(rng.choice(('CP','CC','MV','MC'))+' %d %d' % (rng.randrange(3),rng.randrange(3))) elif op==2: c('C %d %d %d %d' % (rng.choice((0,1,3,8,64,65)),rng.choice((1,8,64,4096)),rng.choice((1,3,16,256)),rng.choice((0,3,31,32)))) elif op==3: body=bytes(rng.randrange(256) for _ in range(rng.randrange(0,18))) c('S %d %d %d %s' % (rng.choice((0,1,2,3,7,19,MAX)),rng.randrange(0,34),rng.choice((0,1,3,11,LIMIT,LIMIT+1,MAX)),enc(body))) elif op==4: c('A '+str(rng.choice((0,1,3,9,LIMIT,LIMIT+1,MAX)))) elif op==5: c('X '+str(rng.randrange(0,22))) elif op==6: c('H %d %d' % (rng.randrange(0,22),rng.randrange(0,34))) elif op==7: c('N %d %d' % (rng.randrange(0,22),rng.choice((0,1,7,LIMIT,LIMIT+1,MAX)))) elif op==8: c('K '+str(rng.randrange(0,34))) elif op==9: c(rng.choice(('R','P'))+' '+str(nonce)) elif op==10: c('D %d %d %d' % (rng.choice((0,1,2,64,65,MAX)),rng.choice((0,1,8,4096,4097,MAX)),nonce)) elif op==11: c('G '+str(rng.randrange(2))) elif op==12: c('L %d %d %d %d' % (rng.choice((1,2,64)),rng.choice((1,16,4096)),rng.choice((1,8,256)),rng.choice((0,7,31)))) elif op==13: s.batch([(rng.randrange(0,5),rng.randrange(0,22),rng.randrange(0,34),rng.choice((0,1,LIMIT+1)),bytes([rng.randrange(256)])) for _ in range(rng.randrange(0,5))],'random-batch') elif op==14: c('I %d %d' % (rng.randrange(0,22),nonce)) elif op==15: c('Z') else: c('S %d %d 0 -' % (rng.randrange(1,6),rng.randrange(min(31,box.config[3])+1))) s.audit() c('U 0'); c('Z'); c('S 123 7 0 00'); s.audit() for command in ('U','U 3','U -1','U +1','U 1 extra','Q extra','Z 0','S 1 1 0','S 1 1 0 - extra','S -1 1 0 -','S +1 1 0 -','S 1 1 0 0','S 1 1 0 zz','S 1 1 0 00xx',f'S {MAX+1} 1 0 -','S 1 1 1x -','S 1 1 0 0x00','A 1.0','G 2','G 1 2','N 1','I 1 2 3','K 1 extra','D 1 1','B 1 1 1 1 0 - extra','B 2 1 1 1 0 -','B 1 -1 1 1 0 -','B 1 2147483648 1 1 0 -','B 1 1 1 1 0 x0','B 33','CP 0 3','CC 0 -1','MC 0 0 extra','MV 3 0','TRAITS extra','unknown'): s.bad(command); s.audit() c('S 0009 0007 0000 AAfF','strict-complete-decimal-and-hex','C15-C18'); c('V'); c('R 263'); s.audit() return s def main(): suite=cases() scratch=Path(tempfile.mkdtemp(prefix='embedded-mailqueue-',dir=Path.cwd())) for name,content in CONTEXT.items(): (scratch/name).write_text(content) for name in CANDIDATES: (scratch/name).write_bytes((Path(sys.argv[1])/name).read_bytes()) wire_input='\n'.join(suite.commands)+'\n' executable=str(scratch/'observe') # All scratch reads and writes finish before the first untrusted compiler process. build=subprocess.run(['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic','observer.cpp','mailbox.cpp','-o','observe'],cwd=scratch,text=True,capture_output=True,timeout=30) if build.returncode: print('check=build clauses=C01 failed '+build.stderr[-1200:].replace('\n',' ')); return 1 # Wrapper owns the parent directory and reclaims scratch after all children exit. observed=subprocess.run([executable],input=wire_input,cwd=scratch,text=True,capture_output=True,timeout=20) if observed.returncode: print('check=execution clauses=C19 exit=%d stderr=%r' % (observed.returncode,observed.stderr[-200:])); return 1 actual=observed.stdout.splitlines(); mismatches=[] for index in range(max(len(actual),len(suite.expected))): got=actual[index] if index' expected=suite.expected[index] if index' if got!=expected: label,clauses=suite.labels[index] if index