amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

633 lines
31 KiB
Python

"""Independent owned-buffer oracle; submitted C runs only in child processes."""
import base64
import hashlib
import json
import math
import os
from pathlib import Path
import random
import re
import select
import subprocess
import sys
import tempfile
import time
import zlib
SUPERVISOR = r'''
import base64, ctypes, hashlib, json, os, selectors, signal, subprocess, sys, time
PR_SET_CHILD_SUBREAPER = 36
PR_GET_CHILD_SUBREAPER = 37
def stamp(pid):
try:
raw = open('/proc/%d/stat' % pid, 'rb').read()
except (FileNotFoundError, ProcessLookupError):
return None
tail = raw.rsplit(b') ', 1)[1].split()
return {'pid':pid,'state':tail[0].decode(),'ppid':int(tail[1]),
'pgid':int(tail[2]),'start':int(tail[19])}
def table():
result = {}
for name in os.listdir('/proc'):
if name.isdigit():
item = stamp(int(name))
if item is not None: result[item['pid']] = item
return result
def live(item):
return item is not None and item['state'] not in ('Z','X')
def descendants(parent, known, scan):
selected = {parent}
changed = True
while changed:
changed = False
for pid,item in scan.items():
if live(item) and item['ppid'] in selected and pid not in selected:
selected.add(pid); changed=True
for pid,start in list(known.items()):
item = scan.get(pid)
if item is not None and item['start'] == start and live(item):
selected.add(pid)
selected.discard(parent)
return {pid:scan[pid] for pid in selected if pid in scan and live(scan[pid])}
def fresh_signal(pid, start, kind, errors, events):
try:
current = stamp(pid)
if current is not None and current['start'] == start and live(current):
os.kill(pid,kind)
events.append([pid,start,int(kind),'sent'])
return True
except (FileNotFoundError,ProcessLookupError):
return False
except Exception as exc:
errors.append('signal:%s:%s' % (type(exc).__name__,str(exc)[:90]))
events.append([pid,start,int(kind),'stale_or_error'])
return False
def main():
config=json.loads(sys.argv[1]); payload=sys.stdin.buffer.read()
argv=config['argv']; cwd=config['cwd']; deadline=config['deadline']
errors=[]; events=[]; adopted=[]; own=os.getpid(); selfstat=stamp(own)
if selfstat is None: raise RuntimeError('supervisor identity unavailable')
libc=ctypes.CDLL(None, use_errno=True)
if libc.prctl(PR_SET_CHILD_SUBREAPER,1,0,0,0) != 0:
raise OSError(ctypes.get_errno(),'prctl subreaper')
current=ctypes.c_int(0)
if libc.prctl(PR_GET_CHILD_SUBREAPER,ctypes.byref(current),0,0,0) != 0 or current.value != 1:
raise RuntimeError('subreaper confirmation failed')
started=time.monotonic(); process=None; known={}; output=bytearray(); diagnostic=bytearray()
sent=0; timeout=False; stdin_open=False; stdout_open=False; stderr_open=False
leader_start=None; selector=selectors.DefaultSelector()
try:
process=subprocess.Popen(argv,cwd=cwd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,start_new_session=True)
leader=stamp(process.pid)
if leader is None: raise RuntimeError('leader identity unavailable')
leader_start=leader['start']; known[process.pid]=leader_start
os.set_blocking(process.stdout.fileno(),False)
os.set_blocking(process.stderr.fileno(),False)
selector.register(process.stdout,selectors.EVENT_READ,'out'); stdout_open=True
selector.register(process.stderr,selectors.EVENT_READ,'err'); stderr_open=True
if payload:
os.set_blocking(process.stdin.fileno(),False)
selector.register(process.stdin,selectors.EVENT_WRITE,'in'); stdin_open=True
else:
process.stdin.close()
while True:
process.poll()
scan=table(); group=descendants(own,known,scan)
for pid,item in group.items(): known[pid]=item['start']
if process.returncode is not None and not stdout_open and not stderr_open and not stdin_open:
break
if time.monotonic()-started >= deadline:
timeout=True; break
for key,_ in selector.select(0.03):
kind=key.data; stream=key.fileobj
if kind=='in':
try: n=os.write(stream.fileno(),payload[sent:sent+65536])
except BrokenPipeError:
n=0; errors.append('stdin_broken_pipe')
selector.unregister(stream);stream.close();stdin_open=False
else:
sent+=n
if sent==len(payload):
selector.unregister(stream);stream.close();stdin_open=False
else:
try: chunk=os.read(stream.fileno(),65536)
except BlockingIOError: continue
if chunk:
(output if kind=='out' else diagnostic).extend(chunk)
else:
selector.unregister(stream);stream.close()
if kind=='out': stdout_open=False
else: stderr_open=False
except Exception as exc:
errors.append('run:%s:%s' % (type(exc).__name__,str(exc)[:100]))
finally:
# Reap the own Popen leader before any adopted child. Continue until the
# live identity closure is empty; a closed pipe is not proof of exit.
end=time.monotonic()+3.0
while process is not None and time.monotonic()<end:
process.poll()
for pid in list(known):
if pid==process.pid: continue
try:
got,status=os.waitpid(pid,os.WNOHANG)
if got: adopted.append([got,known[pid],status])
except ChildProcessError: pass
except ProcessLookupError: pass
try:
scan=table();group=descendants(own,known,scan)
except Exception as exc:
errors.append('proc:%s' % type(exc).__name__);group={}
for pid,item in group.items(): known[pid]=item['start']
if process.returncode is not None and not group: break
sig=signal.SIGTERM if time.monotonic()<end-2.2 else signal.SIGKILL
for pid,item in group.items():
fresh_signal(pid,item['start'],sig,errors,events)
process.poll()
time.sleep(0.02)
if process is not None: process.poll()
for key in list(selector.get_map().values()):
stream=key.fileobj
if key.data in ('out','err'):
try:
while True:
chunk=os.read(stream.fileno(),65536)
if not chunk: break
(output if key.data=='out' else diagnostic).extend(chunk)
except BlockingIOError: pass
except OSError as exc: errors.append('drain:%s' % type(exc).__name__)
try: selector.unregister(stream);stream.close()
except OSError as exc: errors.append('close:%s' % type(exc).__name__)
selector.close()
scan=table(); active=descendants(own,known,scan)
for pid,item in active.items(): known[pid]=item['start']
leader_rc=process.returncode if process is not None else None
result={'supervisor_pid':own,'supervisor_start':selfstat['start'],
'leader_pid':process.pid if process is not None else None,
'leader_start':leader_start,'argv':argv,'cwd':cwd,'deadline':deadline,
'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),
'elapsed':time.monotonic()-started,'timeout':timeout,
'leader_rc':leader_rc,'rc':124 if timeout else leader_rc,
'leader_reaped':leader_rc is not None,
'pg_absent':not any(item['pgid']==process.pid for item in active.values()) if process else False,
'descendants_absent':not active,
'descendants':[(pid,item['start']) for pid,item in active.items()],
'errors':errors,'input_len':len(payload),'input_sha':hashlib.sha256(payload).hexdigest(),
'sent_len':sent,'sent_sha':hashlib.sha256(payload[:sent]).hexdigest(),
'stdout_len':len(output),'stdout_sha':hashlib.sha256(output).hexdigest(),
'stderr_len':len(diagnostic),'stderr_sha':hashlib.sha256(diagnostic).hexdigest(),
'stdout_b64':base64.b64encode(output).decode(),
'stderr_b64':base64.b64encode(diagnostic).decode(),
'nonce':None,'signals':events,'adopted_waits':adopted}
sys.stdout.write(json.dumps(result,separators=(',',':')))
if __name__=='__main__': main()
'''
WATCHDOG = r'''
import json, os, signal, sys, time
def stat(pid):
try: raw=open('/proc/%d/stat'%pid,'rb').read()
except (FileNotFoundError,ProcessLookupError): return None
fields=raw.rsplit(b') ',1)[1].split()
return (pid,fields[0].decode(),int(fields[1]),int(fields[19]))
def live(item):
return item is not None and item[1] not in ('Z','X')
def snapshot():
found={}
for name in os.listdir('/proc'):
if name.isdigit():
item=stat(int(name))
if item is not None:found[item[0]]=item
return found
def closure(root,known,found):
active={root}
for pid,start in known.items():
item=found.get(pid)
if live(item) and item[3]==start:active.add(pid)
changed=True
while changed:
changed=False
for pid,item in found.items():
if live(item) and item[2] in active and pid not in active:
active.add(pid);changed=True
return {pid:found[pid] for pid in active if pid in found and live(found[pid])}
def send(item,kind,signals,errors):
pid,_,_,start=item
try:
current=stat(pid)
if live(current) and current[3]==start:
os.kill(pid,kind)
signals.append([pid,start,int(kind),'sent'])
return
signals.append([pid,start,int(kind),'stale'])
except (FileNotFoundError,ProcessLookupError):
signals.append([pid,start,int(kind),'exited'])
except Exception as exc:
errors.append('signal:%s:%s'%(type(exc).__name__,str(exc)[:90]))
def main():
root=int(sys.argv[1]);start=int(sys.argv[2]);deadline=float(sys.argv[3]);
own=os.getpid();ownstat=stat(own);known={root:start};signals=[];errors=[]
began=time.monotonic();observed=False;timed_out=False;active={}
first=snapshot();item=first.get(root)
if live(item) and item[3]==start:observed=True
sys.stdout.write('READY\n');sys.stdout.flush()
while True:
try:
found=snapshot();current=found.get(root)
if live(current) and current[3]==start:
observed=True
active=closure(root,known,found)
else:
active=closure(-1,known,found)
for pid,member in active.items():known[pid]=member[3]
if observed and not active:break
elapsed=time.monotonic()-began
if elapsed>=deadline:
timed_out=True
kind=signal.SIGTERM if elapsed<deadline+0.4 else signal.SIGKILL
for member in active.values():send(member,kind,signals,errors)
if elapsed>=deadline+3.0:break
except Exception as exc:
errors.append('scan:%s:%s'%(type(exc).__name__,str(exc)[:90]))
if time.monotonic()-began>=deadline+3.0:break
time.sleep(0.025)
found=snapshot();active=closure(-1,known,found)
result={'watchdog_pid':own,'watchdog_start':ownstat[3] if ownstat else None,
'target_pid':root,'target_start':start,'deadline':deadline,
'elapsed':time.monotonic()-began,'observed':observed,
'timed_out':timed_out,'absence':not active,
'active':[[pid,item[3]] for pid,item in active.items()],
'signals':signals,'errors':errors,'uid':os.getuid(),'gid':os.getgid(),
'groups':os.getgroups()}
sys.stdout.write(json.dumps(result,separators=(',',':')))
if __name__=='__main__':main()
'''
def sha(data):
return hashlib.sha256(data).hexdigest()
def nonce_cases():
rng = random.Random(1397)
commands = ['OPEN 0 128', 'PLAN 0 1 128', 'CHECK 0', 'PROBE 0 128',
'FILL 0 7', 'PLAN 0 32 8', 'CHECK 0', 'PROBE 0 16',
'OPEN 1 128', 'COPY 0 1 16', 'PLAN 1 3 1',
'PROBE 1 16', 'DROP 0', 'CHECK 0', 'PROBE 0 1',
'PLAN 0 64 1', 'DROP 0', 'RESET', 'CHECK 1',
'OPEN 8 10', 'PLAN 1 0 0', 'RESET 0', 'BOGUS 0',
'OPEN 0 -1', 'OPEN 0 4097', 'CHECK 1']
for cycle in range(10):
ident = rng.randrange(4)
capacity = rng.choice([1, 2, 31, 32, 33, 64, 65, 128, 129, 256, 512])
alignment = rng.choice([1, 2, 3, 4, 8, 16, 24, 32, 64, 127, 256])
requested = rng.choice([0, 1, 2, 16, capacity//2, capacity, 4096])
commands += [f'OPEN {ident} {capacity}', f'FILL {ident} {rng.randrange(256)}',
f'PLAN {ident} {alignment} {requested}', f'CHECK {ident}',
f'PROBE {ident} {rng.choice([0, 1, 8, capacity])}']
if cycle % 3 == 0:
commands += [f'OPEN {ident} {capacity+1}', f'CHECK {ident}']
if cycle % 4 == 0:
commands += [f'DROP {ident}', f'CHECK {ident}']
commands += ['OPEN 2 4096', 'FILL 2 255', 'PLAN 2 1 +'+'0'*5000, 'PROBE 2 4096',
'OPEN 3 4096', 'COPY 2 3 4096', 'PLAN 3 1 4096',
'PROBE 3 4096', 'RESET', 'CHECK 2', 'CHECK 3', 'DROP 3']
return commands
def decimal(token):
if not re.fullmatch(rb'[+-]?[0-9]+', token):
raise ValueError('nondecimal output')
sign = -1 if token[:1] == b'-' else 1
digits = token.lstrip(b'+-').lstrip(b'0') or b'0'
if len(digits) > 20:
raise ValueError('decimal out of documented range')
return sign * int(digits)
def parse_lines(output):
if not output.endswith(b'\n'):
raise ValueError('output missing LF')
rows=[]
for raw in output.split(b'\n')[:-1]:
if not raw or any(c not in (9,32) and (c<33 or c>126) for c in raw):
raise ValueError('invalid output character')
fields=[part for part in re.split(rb'[ \t]+',raw.strip(b' \t')) if part]
rows.append((fields[0], [decimal(token) for token in fields[1:]]))
return rows
def oracle(commands, rows, input_bytes):
if len(rows) != len(commands)+1:
raise ValueError('response count')
slots=[{'live':False,'gen':0,'cap':0,'data':bytearray(),'view':None} for _ in range(8)]
for index,(command,row) in enumerate(zip(commands,rows),1):
if command in {'OPEN 8 10','PLAN 1 0 0','RESET 0','BOGUS 0',
'OPEN 0 -1','OPEN 0 4097'}:
if row!=(b'E',[]):
raise ValueError('invalid command row %d'%index)
continue
parts=command.split(); op=parts[0]; fields=[decimal(x.encode('ascii')) for x in parts[1:]]
tag,observed=row
expected=None
if op=='OPEN':
ident,cap=fields;slot=slots[ident]
slot.update(live=True,gen=slot['gen']+1,cap=cap,data=bytearray(cap))
expected=(b'O',[ident,cap,slot['gen']])
elif op=='PLAN':
ident,alignment,requested=fields;slot=slots[ident]
if tag!=b'P' or len(observed)!=9 or observed[0]!=ident:
raise ValueError('PLAN shape')
residue=observed[1]
if not 0<=residue<alignment or (not slot['live'] and residue!=0):
raise ValueError('observer residue')
offset=(-residue)%alignment
valid=slot['live'] and offset<=slot['cap'] and requested<=slot['cap']-offset
if valid:
span=slot['cap']-offset
slot['view']=(slot['gen'],offset,span)
expected=(b'P',[ident,residue,0,offset,span,1,1,1,1])
else:
slot['view']=None
expected=(b'P',[ident,residue,-1,0,0,0,0,0,0])
elif op=='CHECK':
ident=fields[0];slot=slots[ident];view=slot['view']
usable=int(slot['live'] and view is not None and view[0]==slot['gen'])
expected=(b'C',[ident,usable])
elif op=='DROP':
ident=fields[0];slot=slots[ident];released=int(slot['live'])
slot['live']=False; expected=(b'D',[ident,released])
elif op=='FILL':
ident,seed=fields;slot=slots[ident];ok=int(slot['live'])
if ok:slot['data']=bytearray((seed+17*i)%256 for i in range(slot['cap']))
expected=(b'F',[ident,ok])
elif op=='PROBE':
ident,count=fields;slot=slots[ident];view=slot['view']
usable=slot['live'] and view is not None and view[0]==slot['gen'] and count<=view[2]
digest=0
if usable:
for byte in slot['data'][view[1]:view[1]+count]:digest=(digest*33+byte)%65521
expected=(b'T',[ident,0 if usable else -1,digest])
elif op=='COPY':
source,destination,count=fields;left=slots[source];right=slots[destination];view=left['view']
ok=left['live'] and right['live'] and view is not None and view[0]==left['gen'] and count<=view[2] and count<=right['cap']
if ok:
copied=bytes(left['data'][view[1]:view[1]+count])
right['data'][:count]=copied
expected=(b'X',[source,destination,int(ok)])
elif op=='RESET':
released=sum(int(slot['live']) for slot in slots)
for slot in slots:slot['live']=False
expected=(b'Z',[released])
if row!=(expected[0],expected[1]):
raise ValueError('row %d got %r expected %r'%(index,row,expected))
tag,observed=rows[-1]
value=2166136261
for byte in input_bytes:value=((value^byte)*16777619)&0xffffffff
if (tag,observed)!=(b'I',[len(input_bytes),value]):
raise ValueError('actual stdin observation')
return len(rows)
RAW_STAGES=[]
def process_start(pid):
raw=Path('/proc/%d/stat'%pid).read_bytes()
return int(raw.rsplit(b') ',1)[1].split()[19])
def stage(argv,cwd,payload,deadline,label):
config={'argv':argv,'cwd':str(cwd),'deadline':deadline}
wrapper=subprocess.Popen([sys.executable,'-I','-B','-c',SUPERVISOR,
json.dumps(config,separators=(',',':'))],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
wrapper_pid=wrapper.pid
try:
wrapper_start=process_start(wrapper_pid)
except Exception:
wrapper.communicate(b'',timeout=deadline+9)
raise
watchdog=subprocess.Popen([sys.executable,'-I','-B','-c',WATCHDOG,
str(wrapper_pid),str(wrapper_start),str(deadline+3)],
stdin=subprocess.DEVNULL,stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
watchdog_pid=watchdog.pid
try:
watchdog_start=process_start(watchdog_pid)
except Exception:
wrapper.communicate(b'',timeout=deadline+9)
watchdog.communicate(timeout=deadline+7)
raise
ready,_,_=select.select([watchdog.stdout],[],[],3)
if not ready or watchdog.stdout.readline()!=b'READY\n':
# The wrapper is still waiting for stdin at this point. Close it so it
# can reach its own bounded cleanup; this stage cannot be accepted.
try:wrapper.communicate(b'',timeout=deadline+9)
except subprocess.TimeoutExpired:pass
raise RuntimeError(label+' watchdog did not establish observation')
try:
raw,wrapper_stderr=wrapper.communicate(payload,timeout=deadline+9)
except subprocess.TimeoutExpired as exc:
# The same-UID watchdog was armed before input was forwarded. Wait for
# its bounded fresh-PID rescue, then collect the actual wrapper result.
try:watchdog_tail,watchdog_stderr=watchdog.communicate(timeout=4)
except subprocess.TimeoutExpired:
watchdog_tail=b'';watchdog_stderr=b''
try:
raw,wrapper_stderr=wrapper.communicate(timeout=2)
except subprocess.TimeoutExpired:
partial=exc.stdout or b''
RAW_STAGES.append({'stage':label,'wrapper_pid':wrapper_pid,
'wrapper_start':wrapper_start,'wrapper_rc':None,
'wire_len':len(partial),'wire_sha':sha(partial),
'watchdog_pid':watchdog_pid,'watchdog_start':watchdog_start,
'watchdog_len':len(watchdog_tail),
'watchdog_sha':sha(watchdog_tail)})
raise RuntimeError(label+' wrapper remained after same-UID rescue')
else:
watchdog_tail,watchdog_stderr=watchdog.communicate(timeout=deadline+7)
RAW_STAGES.append({'stage':label,'wrapper_pid':wrapper_pid,'wrapper_start':wrapper_start,
'wrapper_rc':wrapper.returncode,
'wire_len':len(raw),'wire_sha':sha(raw),
'wrapper_stderr_len':len(wrapper_stderr),
'wrapper_stderr_sha':sha(wrapper_stderr),
'watchdog_pid':watchdog_pid,'watchdog_start':watchdog_start,
'watchdog_rc':watchdog.returncode,'watchdog_len':len(watchdog_tail),
'watchdog_sha':sha(watchdog_tail),'watchdog_stderr_len':len(watchdog_stderr),
'watchdog_stderr_sha':sha(watchdog_stderr)})
if watchdog.returncode!=0:
raise RuntimeError(label+' watchdog rc='+str(watchdog.returncode))
watchdog_wire=json.loads(watchdog_tail)
if (watchdog_wire.get('watchdog_pid')!=watchdog_pid or
watchdog_wire.get('watchdog_start')!=watchdog_start or
watchdog_wire.get('target_pid')!=wrapper_pid or
watchdog_wire.get('target_start')!=wrapper_start or
watchdog_wire.get('deadline')!=deadline+3 or
type(watchdog_wire.get('observed')) is not bool or
type(watchdog_wire.get('timed_out')) is not bool or
type(watchdog_wire.get('absence')) is not bool or
not watchdog_wire['observed'] or watchdog_wire['timed_out'] or
not watchdog_wire['absence'] or
watchdog_wire.get('errors') or watchdog_wire.get('active')):
RAW_STAGES[-1]['watchdog_facts']=watchdog_wire
raise ValueError(label+' watchdog identity/absence')
if (type(watchdog_wire.get('elapsed')) not in (int,float) or
not math.isfinite(watchdog_wire['elapsed']) or
not 0<=watchdog_wire['elapsed']<=deadline+7):
raise ValueError(label+' watchdog elapsed')
for key in ('watchdog_pid','watchdog_start','target_pid','target_start'):
if type(watchdog_wire.get(key)) is not int or watchdog_wire[key]<=0:
raise ValueError(label+' watchdog PID/start type')
if type(watchdog_wire.get('deadline')) is not float:
raise ValueError(label+' watchdog deadline type')
if (type(watchdog_wire.get('uid')) is not int or
type(watchdog_wire.get('gid')) is not int or
type(watchdog_wire.get('groups')) is not list or
any(type(item) is not int for item in watchdog_wire['groups'])):
raise ValueError(label+' watchdog identity type')
if (type(watchdog_wire.get('signals')) is not list or
type(watchdog_wire.get('errors')) is not list or
type(watchdog_wire.get('active')) is not list):
raise ValueError(label+' watchdog trace type')
isolated=os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534'
expected_uid=65534 if isolated else os.geteuid()
expected_gid=65534 if isolated else os.getegid()
expected_groups=[] if isolated else os.getgroups()
if (watchdog_wire.get('uid'),watchdog_wire.get('gid'),watchdog_wire.get('groups'))!=(expected_uid,expected_gid,expected_groups):
raise ValueError(label+' watchdog execution identity')
if wrapper.returncode != 0:
raise RuntimeError('%s supervisor rc=%s stdout=%s stderr=%s detail=%s'%(label,wrapper.returncode,
sha(raw),sha(wrapper_stderr),wrapper_stderr[-400:].decode('utf-8','replace')))
wire=json.loads(raw)
out=base64.b64decode(wire.pop('stdout_b64'),validate=True)
err=base64.b64decode(wire.pop('stderr_b64'),validate=True)
if sha(out)!=wire.get('stdout_sha') or len(out)!=wire.get('stdout_len'):
raise ValueError(label+' stdout binding')
if sha(err)!=wire.get('stderr_sha') or len(err)!=wire.get('stderr_len'):
raise ValueError(label+' stderr binding')
if wire.get('argv')!=argv or wire.get('cwd')!=str(cwd) or wire.get('deadline')!=deadline:
raise ValueError(label+' argv/cwd/deadline')
if any(type(wire.get(key)) is not int or wire[key]<=0 for key in
('supervisor_pid','supervisor_start','leader_pid','leader_start')):
raise ValueError(label+' PID/start')
if wire['supervisor_pid']==wire['leader_pid']:
raise ValueError(label+' PID collision')
if wire['supervisor_pid']!=wrapper_pid or wire['supervisor_start']!=wrapper_start:
raise ValueError(label+' wrapper PID/start mismatch')
isolated=os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534'
expected_uid=65534 if isolated else os.geteuid()
expected_gid=65534 if isolated else os.getegid()
expected_groups=[] if isolated else os.getgroups()
if (wire['uid'],wire['gid'],wire['groups'])!=(expected_uid,expected_gid,expected_groups):
raise ValueError(label+' execution identity mismatch')
for key in ('uid','gid'):
if type(wire.get(key)) is not int:
raise ValueError(label+' identity')
if type(wire.get('groups')) is not list or any(type(item) is not int for item in wire['groups']):
raise ValueError(label+' groups')
if type(wire.get('timeout')) is not bool:
raise ValueError(label+' timeout type')
if type(wire.get('leader_rc')) is not int or not wire.get('leader_reaped'):
raise ValueError(label+' leader reap')
if type(wire.get('elapsed')) not in (int,float) or not math.isfinite(wire['elapsed']) or not 0<=wire['elapsed']<=deadline+4:
raise ValueError(label+' elapsed')
for key in ('input_len','sent_len','stdout_len','stderr_len'):
if type(wire.get(key)) is not int or wire[key]<0:
raise ValueError(label+' IO length')
if wire['input_len']!=len(payload) or wire['input_sha']!=sha(payload):
raise ValueError(label+' input')
if wire['sent_len']>len(payload) or wire['sent_sha']!=sha(payload[:wire['sent_len']]):
raise ValueError(label+' actual sent')
for key in ('leader_reaped','pg_absent','descendants_absent'):
if type(wire.get(key)) is not bool:
raise ValueError(label+' closure type')
if type(wire.get('errors')) is not list or type(wire.get('descendants')) is not list:
raise ValueError(label+' exception/presence')
if any(type(item) is not str for item in wire['errors']):
raise ValueError(label+' exception type')
if wire['descendants_absent'] != (len(wire['descendants'])==0):
raise ValueError(label+' descendant closure')
if wire.get('nonce','missing') is not None:
raise ValueError(label+' nonce')
if wire['timeout']:
if type(wire.get('rc')) is not int or wire['rc']!=124:
raise ValueError(label+' timeout rc')
elif type(wire.get('rc')) is not int or wire['rc']!=wire.get('leader_rc'):
raise ValueError(label+' leader rc')
compact={'stage':label,'wrapper_pid':wrapper_pid,'wrapper_start':wrapper_start,
'wrapper_rc':wrapper.returncode,'wire_len':len(raw),
'wire_sha':sha(raw),'wrapper_stderr_len':len(wrapper_stderr),
'wrapper_stderr_sha':sha(wrapper_stderr),'watchdog':watchdog_wire,
'watchdog_pid':watchdog_pid,'watchdog_start':watchdog_start,
'watchdog_rc':watchdog.returncode,'watchdog_len':len(watchdog_tail),
'watchdog_sha':sha(watchdog_tail),'facts':wire}
return wire,out,err,compact
def emit(facts):
packed=base64.b85encode(zlib.compress(json.dumps(facts,separators=(',',':')).encode(),9)).decode()
print('OWNED1 '+packed,file=sys.stderr)
def main():
candidate=Path(sys.argv[1]); scratch=Path(tempfile.mkdtemp(prefix='owned-offset-'))
records=[]; problems=[]; checks=0; made=False
try:
support=json.loads(Path(__file__).with_name('support.json').read_text())
for name,text in support.items():
(scratch/name).write_text(text)
(scratch/'planner.c').write_bytes((candidate/'planner.c').read_bytes())
commands=nonce_cases(); payload=('\n'.join(commands)+'\n').encode('ascii')
made=True
compile_argv=['gcc','-std=c11','-O2','-Wall','-Wextra','-Werror',
'owner.c','protocol.c','observer.c','planner.c','-o','owned-demo']
facts,out,err,record=stage(compile_argv,scratch,b'',25,'compile');records.append(record)
if facts['rc']!=0 or facts['timeout'] or facts['errors'] or not facts['leader_reaped'] or not facts['pg_absent'] or not facts['descendants_absent']:
problems.append('compile rc=%s timeout=%s errors=%s'%(facts['rc'],facts['timeout'],facts['errors']))
else:
facts,out,err,record=stage([str(scratch/'owned-demo')],scratch,payload,25,'candidate');records.append(record)
if facts['rc']!=0 or facts['timeout'] or facts['errors'] or not facts['leader_reaped'] or not facts['pg_absent'] or not facts['descendants_absent']:
problems.append('candidate rc=%s timeout=%s errors=%s'%(facts['rc'],facts['timeout'],facts['errors']))
elif facts['sent_len']!=len(payload):
problems.append('candidate stdin incomplete')
else:
try:checks=oracle(commands,parse_lines(out),payload)
except ValueError as exc:problems.append(str(exc)[:300])
except Exception as exc:
problems.append('prepare/execute '+type(exc).__name__+': '+str(exc)[:1000])
finally:
try:
facts,out,err,record=stage(['rm','-rf','--',str(scratch)],Path('/tmp'),b'',15,'cleanup');records.append(record)
if facts['rc']!=0 or facts['timeout'] or facts['errors'] or not facts['leader_reaped'] or not facts['pg_absent'] or not facts['descendants_absent']:
problems.append('cleanup rc=%s timeout=%s errors=%s'%(facts['rc'],facts['timeout'],facts['errors']))
except Exception as exc:
problems.append('cleanup '+type(exc).__name__+': '+str(exc)[:160])
try:
checker='import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: print("ABSENT")\nelse: print("PRESENT")\n'
facts,out,err,record=stage([sys.executable,'-I','-c',checker,str(scratch)],Path('/tmp'),b'',10,'absence');records.append(record)
if facts['rc']!=0 or facts['timeout'] or facts['errors'] or not facts['leader_reaped'] or not facts['pg_absent'] or not facts['descendants_absent'] or out!=b'ABSENT\n':
problems.append('absence rc=%s output_sha=%s'%(facts['rc'],sha(out)))
except Exception as exc:
problems.append('absence '+type(exc).__name__+': '+str(exc)[:160])
emit({'records':records,'raw_stages':RAW_STAGES,'problems':problems,'scratch':str(scratch),
'observations':checks,'nonce_protocol':'none'})
if problems:
print(problems[0]);return 1
print('PASS');return 0
if __name__=='__main__':raise SystemExit(main())