"""Independent event-sequence oracles. No candidate code is loaded in this process.""" import json import random import re import subprocess import sys import tempfile from pathlib import Path def signal(events): lines=[] pending={};blocked={};mask=owner=0;hook=None seq=[0]*3;sums=[0]*3 received=delivered=coalesced=unsafe=busy=rejected=0 for step,event in enumerate(events,1): def emit(name,a=0,b=0,c=0):lines.append('E %d %s %d %d %d'%(step,name,a,b,c)) def latch(ch,val,s): nonlocal coalesced coalesced+=int(ch in pending) pending[ch]=(val,s) def receive(ch,val): nonlocal received,coalesced received+=1;seq[ch]+=1 if mask>>ch&1: coalesced+=int(ch in blocked);blocked[ch]=(val,seq[ch]) else:latch(ch,val,seq[ch]) k=event[0];a=event[1] if len(event)>1 else 0;b=event[2] if len(event)>2 else 0 valid=True if k=='R' and len(event)==1: pending={};blocked={};mask=owner=0;hook=None;seq=[0]*3;sums=[0]*3 received=delivered=coalesced=unsafe=busy=rejected=0 elif k=='H' and len(event)==1: if owner:valid=False else:owner=1 elif k=='U' and len(event)==1: if owner!=1:valid=False else:owner=0 elif k in ('S','A') and len(event)==3 and 0<=a<3 and -10000<=b<=10000: if k=='S':receive(a,b) else:hook=(a,b) elif k=='M' and len(event)==2 and 0<=a<=7: mask=a for ch in range(3): if ch in blocked and not(mask>>ch&1): val,s=blocked.pop(ch);latch(ch,val,s) elif k=='D' and len(event)==2 and 0<=a<=3: if a and owner:busy+=1 elif a: snap=sorted(pending.items())[:a];owner=2 for ch,(val,s) in snap: if pending.get(ch)==(val,s):pending.pop(ch,None) delivered+=1;sums[ch]+=val;emit('HANDLE',ch,val,s) if hook is not None: pair=hook;hook=None;receive(*pair) owner=0 else:valid=False if not valid:rejected+=1;emit('REJECT',ord(k)) state=[sum(1<1 else 0;b=event[2] if len(event)>2 else 0;valid=True if k=='R' and len(event)==1: queue=[];head=closed=0;workers=[[0,0,0,0] for _ in range(4)] submitted=consumed=rejected=wakes=empty=total=ticket=0 elif k=='Q' and len(event)==3 and 1<=a<=100000 and -10000<=b<=10000: if closed or len(queue)==4:valid=False else: queue.append((a,b));submitted+=1 waiting=[i for i,w in enumerate(workers) if w[0]==1] if waiting:wake(min(waiting,key=lambda i:workers[i][1])) elif k in ('W','N','P') and len(event)==2 and 0<=a<4: if k=='N':wake(a) elif k=='W' and workers[a][0]!=0:valid=False else:poll(a) elif k=='G' and len(event)==1: if not closed: closed=1 for index in range(4):wake(index) else:valid=False if not valid:rejected+=1;emit('REJECT',ord(k)) state=[head,len(queue),closed,submitted,consumed,rejected,wakes,empty,total,ticket]+[x for w in workers for x in w] lines.append('S %d '%step+' '.join(map(str,state))) return lines def lock(events): lines=[];balance=[1000]*4;owners=[-1]*4;tx=[None]*4 started=completed=insufficient=cancelled=blocked=rejected=0 for step,event in enumerate(events,1): def emit(name,a=0,b=0,c=0):lines.append('E %d %s %d %d %d'%(step,name,a,b,c)) def release(slot): for acc in range(3,-1,-1): if owners[acc]==slot:owners[acc]=-1;emit('UNLOCK',slot,acc) k=event[0];a=event[1] if len(event)>1 else 0;valid=True if k=='R' and len(event)==1: balance=[1000]*4;owners=[-1]*4;tx=[None]*4;started=completed=insufficient=cancelled=blocked=rejected=0 elif k=='B' and len(event)==5: slot,src,dst,amount=event[1:] if not(0<=slot<4 and 0<=src<4 and 0<=dst<4 and src!=dst and 1<=amount<=1000000) or tx[slot] is not None:valid=False else:tx[slot]=dict(src=src,dst=dst,amount=amount,phase=0,held=0);started+=1;emit('BEGIN',slot,src,dst) elif k in ('T','C') and len(event)==2 and 0<=a<4: item=tx[a] if item is not None: if k=='C':release(a);tx[a]=None;cancelled+=1;emit('CANCEL',a) elif item['phase']<2: account=sorted([item['src'],item['dst']])[item['phase']] if owners[account]>=0 and owners[account]!=a:blocked+=1;emit('BLOCK',a,account,owners[account]) else:owners[account]=a;item['held']|=1<1 else 0;valid=True if k=='R' and len(event)==1: sessions=[empty() for _ in range(4)];opened=closed=rejected=good=bad=total=0 elif k=='O' and len(event)==3 and isinstance(a,int) and 0<=a<4: try: raw=bytes.fromhex(event[2]) if event[2]!='-' else b'' text=raw.decode('ascii') ok=len(raw)<=256 and re.fullmatch(r'[0-9a-z+,;\-]*',text) is not None except (ValueError,UnicodeError):ok=False if not ok or sessions[a]['active']:valid=False else: s=empty();s.update(active=1,text=text,exhausted=int(not text));sessions[a]=s;opened+=1;emit('OPEN',a,len(text)) elif k in ('N','C') and len(event)==2 and 0<=a<4: s=sessions[a] if not s['active']:valid=False elif k=='C':s['active']=0;closed+=1;emit('CLOSE',a) elif s['exhausted']:emit('EOF',a) else: end=s['text'].find(';',s['position']);end=len(s['text']) if end<0 else end fields=s['text'][s['position']:end].split(',');values=[];ok=True for field in fields: if re.fullmatch(r'[+-]?[0-9]+',field) and -1000<=int(field)<=1000:values.append(int(field)) else:ok=False s['position']=end if end==len(s['text']) else end+1;s['exhausted']=int(end==len(s['text'])) if ok: value=sum(values);s['good']+=1;s['sum']+=value;good+=1;total+=value;emit('ROW',a,len(fields),value) else:s['bad']+=1;bad+=1;emit('BAD',a,len(fields)) else:valid=False if not valid:rejected+=1;emit('REJECT',ord(k)) state=[opened,closed,rejected,good,bad,total] for s in sessions:state.extend([s['active'],len(s['text']),s['position'],s['exhausted'],s['good'],s['bad'],s['sum']]) lines.append('S %d '%step+' '.join(map(str,state))) return lines def callback(events): lines=[];listeners=[[0,0,0] for _ in range(4)];queue=[];head=0;hook=None posted=callbacks=dispatched=rejected=reentered=0 for step,event in enumerate(events,1): def emit(name,a=0,b=0,c=0):lines.append('E %d %s %d %d %d'%(step,name,a,b,c)) def reject(k): nonlocal rejected rejected+=1;emit('REJECT',ord(k)) def action(k,a,b=0): nonlocal posted if k=='B': listeners[a]=[1,b,listeners[a][2]+1];emit('SUB',a,b,listeners[a][2]) elif k=='U': if listeners[a][0]:listeners[a][0]=0;listeners[a][2]+=1;emit('UNSUB',a,listeners[a][2]) elif k=='Q': if len(queue)==8:reject(k) else:posted+=1;queue.append((a,b,posted));emit('POST',posted,a,b) k=event[0];a=event[1] if len(event)>1 else 0;b=event[2] if len(event)>2 else 0 if k=='R' and len(event)==1: listeners=[[0,0,0] for _ in range(4)];queue=[];head=0;hook=None;posted=callbacks=dispatched=rejected=reentered=0 elif k=='B' and len(event)==3 and 0<=a<4 and 0<=b<=7:action(k,a,b) elif k=='U' and len(event)==2 and 0<=a<4:action(k,a) elif k=='Q' and len(event)==3 and 0<=a<3 and -10000<=b<=10000:action(k,a,b) elif k=='A' and len(event)==4: c=event[3] if (a==1 and 0<=b<4 and 0<=c<=7) or (a==2 and 0<=b<4 and c==0) or (a==3 and 0<=b<3 and -10000<=c<=10000) or (a==4 and 0<=b<=8 and c==0):hook=(a,b,c) else:reject(k) elif k=='D' and len(event)==2 and 0<=a<=8: count=min(a,len(queue)) for _ in range(count): channel,value,serial=queue.pop(0);head=(head+1)%8;snapshot=[x[:] for x in listeners] for slot,(active,mask,generation) in enumerate(snapshot): if not active or not(mask>>channel&1) or not listeners[slot][0] or listeners[slot][2]!=generation:continue callbacks+=1;emit('CALL',slot,value,serial) if hook: act,x,y=hook;hook=None if act==4:reentered+=1;emit('REENTER',x) else:action({1:'B',2:'U',3:'Q'}[act],x,y) dispatched+=1 else:reject(k) state=[head,len(queue),posted,callbacks,dispatched,rejected,reentered,hook[0] if hook else 0,0,0]+[x for listener in listeners for x in listener] lines.append('S %d '%step+' '.join(map(str,state))) return lines def make_events(config): kind=config['kind'];rng=random.Random(config['seed']);events=[] def add(*items):events.extend(items) if kind=='signal': add(('R',),('H',),('S',0,19),('S',0,27),('D',3),('U',),('D',3)) for channel in range(3): add(('R',),('S',channel,31),('A',channel,49),('D',3),('D',3),('D',3)) add(('R',),('M',7),('S',2,9),('S',2,17),('S',0,-11),('M',3),('D',1),('M',0),('D',3),('H',),('H',),('U',),('U',),('S',3,4),('M',8),('D',4),('A',-1,0)) for index in range(450): if index%57==0:add(('R',)) k=rng.choice('SSSMDDAHU') if k in 'SA':add((k,rng.randrange(3),rng.randrange(-500,501))) elif k=='M':add((k,rng.randrange(8))) elif k=='D':add((k,rng.randrange(4))) else:add((k,)) elif kind=='cond': add(('R',),('W',0),('N',0),('P',0),('Q',1,7),('P',0),('P',0)) add(('R',),*(('W',i) for i in range(4)),('G',),*(('P',i) for i in range(4)),('G',),('Q',8,9)) add(('R',),('W',0),('W',1),('Q',1,12),('N',1),('P',1),('P',0),('P',0),('G',),('P',0)) for n in range(450): if n%61==0:add(('R',)) k=rng.choice('QQWNNPPPG') if k=='Q':add((k,rng.randrange(1,30),rng.randrange(-100,101))) elif k=='G':add((k,)) else:add((k,rng.randrange(4))) add(('W',4),('P',-1),('Q',0,0),('X',),('G',1)) elif kind=='lock': add(('R',),('B',0,3,0,123),('B',1,0,3,44),('T',0),('T',1),('T',0),('T',1),('T',0),('T',1),('T',1),('T',1)) for n in (0,1,2): add(('R',),('B',0,0,3,78),*(('T',0) for _ in range(n)),('C',0),('B',1,3,0,99),('T',1),('T',1),('T',1)) add(('R',),('B',0,0,2,1001),('T',0),('T',0),('T',0),('B',0,1,2,10),('B',0,1,3,40),('C',0)) for n in range(500): if n%79==0:add(('R',)) k=rng.choice('BBTTTTTC') if k=='B': src,dst=rng.sample(range(4),2);add((k,rng.randrange(4),src,dst,rng.choice([1,1000,1001,rng.randrange(1,1500)]))) else:add((k,rng.randrange(4))) add(('B',0,0,0,1),('B',4,0,1,1),('B',0,0,1,0),('C',-1),('X',)) elif kind=='parser': def enc(text):return text.encode().hex() or '-' add(('R',),('O',0,enc('1,2;3,4;5')),('N',0),('O',1,enc('8;9;10')),('N',1),('N',0),('N',1),('N',0),('N',0)) for text in ['7x,8;9','a;2',';','1,;2','-1000,+1000;0001','1001;-1001;0','+;-;','1;;2;','',',','000000000000001;0']: add(('R',),('O',0,enc(text)),*(('N',0) for _ in range(6)),('C',0)) for n in range(70): add(('R',)) for slot in range(4): rows=[] for _ in range(rng.randrange(1,7)): fields=[str(rng.randrange(-1000,1001)) for _ in range(rng.randrange(1,5))] if rng.randrange(4)==0:fields[-1]+=rng.choice(['x','a',',']) rows.append(','.join(fields)) add(('O',slot,enc(';'.join(rows)))) for _ in range(30):add(('N',rng.randrange(4))) add(('R',),('O',0,'zz'),('O',0,'00'),('O',4,'31'),('N',0),('C',0),('O',0,'31'),('O',0,'32')) else: add(('R',),('B',0,7),('B',1,7),('Q',0,12),('A',2,1,0),('D',1)) add(('R',),('B',0,7),*(('Q',i%3,i*17) for i in range(8)),('A',3,2,999),('D',1),('D',8)) for hook in [(1,1,7),(2,1,0),(3,1,44),(4,8,0)]: add(('R',),('B',0,7),('B',1,7),('Q',0,4),('Q',1,9),('A',*hook),('D',2),('D',8)) for n in range(500): if n%71==0:add(('R',)) k=rng.choice('BBUQQQADDD') if k=='B':add((k,rng.randrange(4),rng.randrange(8))) elif k=='U':add((k,rng.randrange(4))) elif k=='Q':add((k,rng.randrange(3),rng.randrange(-1000,1001))) elif k=='D':add((k,rng.randrange(9))) else: act=rng.randrange(1,5) if act==1:add((k,act,rng.randrange(4),rng.randrange(8))) elif act==2:add((k,act,rng.randrange(4),0)) elif act==3:add((k,act,rng.randrange(3),rng.randrange(-1000,1001))) else:add((k,act,rng.randrange(9),0)) add(('B',4,7),('Q',3,0),('D',9),('A',4,2,1),('X',)) return events def main(): candidate=Path(sys.argv[1]);config=json.loads((Path(__file__).parent/'cases.json').read_text()) events=make_events(config);oracle={'signal':signal,'cond':cond,'lock':lock,'parser':parser,'callback':callback}[config['kind']] wanted=oracle(events);stdin=''.join(' '.join(map(str,event))+'\n' for event in events) scratch=Path(tempfile.mkdtemp(prefix='embedded3-concurrency-')) for name,content in config['readonly'].items():(scratch/name).write_text(content) for name in config['candidate_files']:(scratch/name).write_bytes((candidate/name).read_bytes()) built=subprocess.run(['make','-s'],cwd=scratch,capture_output=True,text=True,timeout=20) if built.returncode: print('build failed: '+built.stderr[-3000:]);return 1 # Shared scratch is never accessed by the supervisor after starting the candidate. completed=subprocess.run([str(scratch/'concurrency-demo')],cwd=scratch,input=stdin,capture_output=True,text=True,timeout=15) if completed.stderr:print(completed.stderr[-3000:],file=sys.stderr,end='') if completed.returncode:print('candidate exit %d'%completed.returncode);return 1 got=completed.stdout.splitlines() for index in range(max(len(got),len(wanted))): actual=got[index] if index' correct=wanted[index] if index' if actual!=correct:print('observation %d mismatch: got %s; expected %s'%(index+1,actual,correct));return 1 print('PASS');return 0 if __name__=='__main__':raise SystemExit(main())