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>
419 lines
30 KiB
Python
419 lines
30 KiB
Python
import base64
|
|
|
|
import bz2
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import math
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
import random
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
RECOVERY_SECONDS = 3
|
|
|
|
RECORD_WIRE = False
|
|
|
|
SUPERVISOR = r'''
|
|
import base64,bz2,ctypes,hashlib,json,os,signal,subprocess,sys,time
|
|
cfg=json.loads(sys.argv[1]); start=time.monotonic(); errors=[]; tracked={}; signals={}; waits=[]
|
|
leader=None; leader_start=None; output=b''; diagnostic=b''; timed_out=False; drained=False
|
|
reaped=False; leader_rc=None; remaining=[]; pg_absent=False
|
|
|
|
def fault(op, error):
|
|
errors.append([op,type(error).__name__,str(error)])
|
|
|
|
def exact(pid):
|
|
try:
|
|
with open('/proc/%d/stat'%pid,'rb') as stream: data=stream.read()
|
|
words=data[data.rfind(b')')+2:].split()
|
|
return {'pid':pid,'state':words[0].decode('ascii'),'ppid':int(words[1]),'pg':int(words[2]),'start':int(words[19])}
|
|
except (FileNotFoundError,ProcessLookupError): return None
|
|
|
|
def table_now():
|
|
result={}
|
|
for name in os.listdir('/proc'):
|
|
if name.isascii() and name.isdigit():
|
|
value=exact(int(name))
|
|
if value is not None: result[value['pid']]=value
|
|
return result
|
|
|
|
def closure(table, supervisor, remembered):
|
|
roots={supervisor}
|
|
for pid,old in remembered.items():
|
|
current=table.get(pid)
|
|
if current and current['start']==old['start'] and current['state'] not in ('Z','X'):
|
|
roots.add(pid)
|
|
found={}
|
|
changed=True
|
|
while changed:
|
|
changed=False
|
|
for pid,current in table.items():
|
|
if pid!=supervisor and pid not in found and (current['ppid'] in roots or pid in roots):
|
|
found[pid]=current; roots.add(pid); changed=True
|
|
return found
|
|
|
|
def discover():
|
|
table=table_now(); found=closure(table,os.getpid(),tracked)
|
|
for pid,current in found.items():
|
|
if pid not in tracked or tracked[pid]['start']!=current['start']:
|
|
tracked[pid]=dict(current)
|
|
return table,found
|
|
|
|
def send_fresh(pid, token):
|
|
current=exact(pid)
|
|
if current is None or current['start']!=token['start'] or current['state'] in ('Z','X'): return
|
|
os.kill(pid,signal.SIGKILL)
|
|
signals[(pid,token['start'])]=signals.get((pid,token['start']),0)+1
|
|
|
|
def own_wait():
|
|
global reaped,leader_rc
|
|
if leader is not None:
|
|
value=leader.poll()
|
|
if value is not None:
|
|
leader_rc=leader.wait(timeout=0)
|
|
reaped=True
|
|
|
|
def adopted_wait():
|
|
if leader is not None and not reaped: return
|
|
while True:
|
|
try: pid,status=os.waitpid(-1,os.WNOHANG)
|
|
except ChildProcessError: break
|
|
if pid==0: break
|
|
waits.append([pid,None,os.waitstatus_to_exitcode(status)])
|
|
|
|
supervisor_start=exact(os.getpid())['start']
|
|
try:
|
|
libc=ctypes.CDLL(None,use_errno=True)
|
|
if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')
|
|
leader=subprocess.Popen(cfg['argv'],cwd=cfg['cwd'],stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
|
|
info=exact(leader.pid)
|
|
if info is None: raise RuntimeError('leader identity disappeared before capture')
|
|
leader_start=info['start']; tracked[leader.pid]=info
|
|
payload=sys.stdin.buffer.read()
|
|
first=True
|
|
while True:
|
|
try:
|
|
output,diagnostic=leader.communicate(input=payload if first else None,timeout=0.04)
|
|
drained=True; own_wait(); break
|
|
except subprocess.TimeoutExpired:
|
|
first=False
|
|
own_wait()
|
|
discover()
|
|
if time.monotonic()-start>=cfg['deadline']:
|
|
timed_out=True; break
|
|
except BaseException as error:
|
|
fault('execute',error)
|
|
finally:
|
|
recovery_end=time.monotonic()+cfg['recovery']
|
|
while True:
|
|
try: own_wait()
|
|
except BaseException as error: fault('own_wait',error)
|
|
found={}
|
|
try: _,found=discover()
|
|
except BaseException as error: fault('discover',error)
|
|
for pid,token in list(found.items()):
|
|
try: send_fresh(pid,token)
|
|
except (FileNotFoundError,ProcessLookupError): pass
|
|
except BaseException as error: fault('signal',error)
|
|
try: own_wait()
|
|
except BaseException as error: fault('own_wait',error)
|
|
try: adopted_wait()
|
|
except BaseException as error: fault('adopted_wait',error)
|
|
if leader is not None and not drained:
|
|
try:
|
|
output,diagnostic=leader.communicate(timeout=0.03)
|
|
drained=True; own_wait()
|
|
except subprocess.TimeoutExpired: pass
|
|
except BaseException as error: fault('drain',error)
|
|
try:
|
|
table,found=discover()
|
|
remaining=[[p,v['start'],v['state']] for p,v in found.items()]
|
|
pg_absent=leader is not None and not any(v['pg']==leader.pid for v in table.values())
|
|
except BaseException as error:
|
|
fault('absence',error); remaining=[['unknown']]; pg_absent=False
|
|
if reaped and drained and not remaining and pg_absent: break
|
|
if time.monotonic()>=recovery_end:
|
|
fault('recovery',RuntimeError('deadline with unconfirmed closure')); break
|
|
time.sleep(0.01)
|
|
|
|
meta={'phase':cfg['phase'],'nonce':cfg['nonce'],'argv':cfg['argv'],
|
|
'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),
|
|
'supervisor_pid':os.getpid(),'supervisor_start':supervisor_start,
|
|
'leader_pid':None if leader is None else leader.pid,'leader_start':leader_start,
|
|
'leader_rc':leader_rc,'leader_reaped':reaped,'timeout':timed_out,
|
|
'controlled_rc':124 if timed_out else leader_rc,'elapsed':time.monotonic()-start,
|
|
'deadline':cfg['deadline'],'recovery':cfg['recovery'],
|
|
'pg_absent':pg_absent,'remaining':remaining,'exceptions':errors,
|
|
'descendants':[[p,t['start'],signals.get((p,t['start']),0)]
|
|
for p,t in tracked.items() if leader is None or p!=leader.pid],
|
|
'adopted_waits':waits,
|
|
'stdout_len':len(output),'stdout_sha256':hashlib.sha256(output).hexdigest(),
|
|
'stderr_len':len(diagnostic),'stderr_sha256':hashlib.sha256(diagnostic).hexdigest()}
|
|
wire={'meta':meta,'stdout':base64.b64encode(bz2.compress(output)).decode('ascii'),
|
|
'stderr':base64.b64encode(bz2.compress(diagnostic)).decode('ascii')}
|
|
sys.stdout.write(json.dumps(wire,separators=(',',':'))+'\n')
|
|
'''
|
|
|
|
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
|
|
|
|
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nsys.exit(1)"
|
|
|
|
def identity_int(value, minimum=0):
|
|
return type(value) is int and value >= minimum
|
|
|
|
def validate_meta(meta, config, out, err):
|
|
required = {'phase','nonce','argv','uid','gid','groups','supervisor_pid','supervisor_start',
|
|
'leader_pid','leader_start','leader_rc','leader_reaped','timeout','controlled_rc',
|
|
'elapsed','deadline','recovery','pg_absent','remaining','exceptions','descendants',
|
|
'stdout_len','stdout_sha256','stderr_len','stderr_sha256','adopted_waits'}
|
|
if type(meta) is not dict or not required <= meta.keys():
|
|
raise ValueError('missing stage metadata')
|
|
if meta['phase'] != config['phase'] or meta['nonce'] != config['nonce'] or meta['argv'] != config['argv']:
|
|
raise ValueError('stage binding conflict')
|
|
for name in ('uid','gid','supervisor_pid','supervisor_start','leader_pid','leader_start'):
|
|
if not identity_int(meta[name], 1 if name not in ('uid','gid') else 0):
|
|
raise ValueError('invalid identity '+name)
|
|
if meta['supervisor_pid'] == meta['leader_pid']:
|
|
raise ValueError('supervisor and leader identical')
|
|
isolated = os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534'
|
|
expected_identity = (65534,65534,[]) if isolated else (os.getuid(),os.getgid(),os.getgroups())
|
|
if type(meta['groups']) is not list or any(type(x) is not int or x < 0 for x in meta['groups']):
|
|
raise ValueError('invalid groups')
|
|
if (meta['uid'],meta['gid'],meta['groups']) != expected_identity:
|
|
raise ValueError('execution identity conflict')
|
|
if type(meta['deadline']) is not int or meta['deadline'] != config['deadline']:
|
|
raise ValueError('deadline conflict')
|
|
if type(meta['recovery']) is not int or meta['recovery'] != RECOVERY_SECONDS:
|
|
raise ValueError('recovery conflict')
|
|
elapsed = meta['elapsed']
|
|
if type(elapsed) not in (int,float) or not math.isfinite(elapsed) or not 0 <= elapsed <= config['deadline']+RECOVERY_SECONDS+2:
|
|
raise ValueError('invalid elapsed')
|
|
for name in ('timeout','leader_reaped','pg_absent'):
|
|
if type(meta[name]) is not bool:
|
|
raise ValueError('invalid boolean '+name)
|
|
for name in ('leader_rc','controlled_rc'):
|
|
if type(meta[name]) is not int:
|
|
raise ValueError('invalid return code '+name)
|
|
if meta['controlled_rc'] != (124 if meta['timeout'] else meta['leader_rc']):
|
|
raise ValueError('return code conflict')
|
|
for name in ('remaining','exceptions','descendants','adopted_waits'):
|
|
if type(meta[name]) is not list:
|
|
raise ValueError('invalid list '+name)
|
|
for row in meta['descendants']:
|
|
if type(row) is not list or len(row) != 3 or not all(identity_int(x,1) for x in row[:2]) or not identity_int(row[2]):
|
|
raise ValueError('invalid descendant record')
|
|
for row in meta['adopted_waits']:
|
|
if type(row) is not list or len(row) != 3 or not identity_int(row[0],1) or row[1] is not None or type(row[2]) is not int:
|
|
raise ValueError('invalid adopted wait record')
|
|
for name,data in (('stdout',out),('stderr',err)):
|
|
if not identity_int(meta[name+'_len']) or meta[name+'_len'] != len(data):
|
|
raise ValueError('invalid IO length')
|
|
if type(meta[name+'_sha256']) is not str or meta[name+'_sha256'] != hashlib.sha256(data).hexdigest():
|
|
raise ValueError('invalid IO digest')
|
|
if meta['exceptions'] or meta['remaining'] or not meta['leader_reaped'] or not meta['pg_absent']:
|
|
raise ValueError('stage recovery or exception failure')
|
|
if meta['timeout'] or meta['leader_rc'] != 0 or meta['controlled_rc'] != 0:
|
|
raise ValueError('controlled timeout' if meta['timeout'] else 'leader nonzero exit')
|
|
|
|
def execute_stage(phase, argv, cwd, deadline, payload, evidence):
|
|
nonce = os.urandom(8).hex()
|
|
config = dict(phase=phase,argv=argv,cwd=str(cwd),deadline=deadline,recovery=RECOVERY_SECONDS,nonce=nonce)
|
|
wrapper_argv = [sys.executable,'-I','-c',SUPERVISOR,json.dumps(config,separators=(',',':'))]
|
|
raw = subprocess.run(wrapper_argv,input=payload,capture_output=True,timeout=deadline+RECOVERY_SECONDS+5)
|
|
# Preserve actual wrapper facts before touching or trusting its wire protocol.
|
|
entry = dict(phase=phase,nonce=nonce,wrapper_rc=raw.returncode,
|
|
wire_len=len(raw.stdout),wire_sha256=hashlib.sha256(raw.stdout).hexdigest(),
|
|
wrapper_stderr_len=len(raw.stderr),wrapper_stderr_sha256=hashlib.sha256(raw.stderr).hexdigest())
|
|
if RECORD_WIRE:
|
|
entry['wire_ascii'] = raw.stdout.decode('ascii')
|
|
entry['wrapper_stderr_bz2'] = base64.b64encode(bz2.compress(raw.stderr)).decode('ascii')
|
|
evidence.append(entry)
|
|
if raw.returncode != 0:
|
|
raise ValueError(phase+' supervisor nonzero')
|
|
wire = json.loads(raw.stdout)
|
|
out = bz2.decompress(base64.b64decode(wire['stdout'],validate=True))
|
|
err = bz2.decompress(base64.b64decode(wire['stderr'],validate=True))
|
|
meta = wire['meta']
|
|
if not RECORD_WIRE: entry['meta'] = meta
|
|
validate_meta(meta,config,out,err)
|
|
return out,err
|
|
|
|
SUPPORT = {'include/live_status.h': '#ifndef LIVE_STATUS_H\n#define LIVE_STATUS_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n\n#define LS_MAX_SAMPLES 64u\n#define LS_MAX_ENTRIES 64u\n#define LS_MAX_EVENTS (LS_MAX_SAMPLES + 2u)\n\ntypedef enum {\n LS_WAITING = 0,\n LS_READY = 1,\n LS_FAULT = 2,\n LS_EXHAUSTED = 3,\n LS_INVALID = 4\n} ls_outcome;\n\ntypedef struct {\n uint32_t ready_mask;\n uint32_t fault_mask;\n unsigned max_samples;\n} ls_request;\n\ntypedef struct {\n ls_outcome outcome;\n unsigned samples;\n uint32_t status;\n uint32_t payload;\n} ls_result;\n\ntypedef struct {\n void *context;\n uint32_t (*read_status)(void *context);\n uint32_t (*read_payload)(void *context);\n void (*acknowledge)(void *context);\n} ls_port;\n\ntypedef struct {\n ls_request request;\n ls_port port;\n ls_result result;\n uint32_t retained_status;\n unsigned retained_valid;\n} ls_session;\n\nls_outcome ls_session_begin(ls_session *session, const ls_port *port,\n const ls_request *request);\nls_outcome ls_session_step(ls_session *session);\nls_result ls_wait(const ls_port *port, const ls_request *request);\nint ls_request_valid(const ls_request *request);\nconst char *ls_outcome_name(ls_outcome outcome);\n\ntypedef struct {\n uint32_t status;\n uint32_t payload;\n} ls_publication;\n\ntypedef struct {\n unsigned kind;\n uint32_t value;\n} ls_event;\n\ntypedef struct {\n ls_publication publications[LS_MAX_ENTRIES];\n size_t length;\n size_t next;\n size_t current;\n ls_event events[LS_MAX_EVENTS];\n size_t event_count;\n unsigned dropped_events;\n unsigned acknowledgements;\n} ls_script;\n\nint ls_script_set(ls_script *script, const ls_publication *values, size_t count);\nvoid ls_script_rewind(ls_script *script);\nls_port ls_script_port(ls_script *script);\nint ls_script_load(FILE *stream, ls_script *script, char *error, size_t capacity);\nvoid ls_script_dump(FILE *stream, const ls_script *script);\n\ntypedef struct {\n uint64_t completed;\n uint64_t ready;\n uint64_t faulty;\n uint64_t exhausted;\n uint64_t invalid;\n uint64_t samples;\n unsigned min_samples;\n unsigned max_samples;\n} ls_statistics;\n\nvoid ls_statistics_reset(ls_statistics *stats);\nvoid ls_statistics_add(ls_statistics *stats, const ls_result *result);\nvoid ls_statistics_print(FILE *stream, const ls_statistics *stats);\nvoid ls_result_print(FILE *stream, unsigned iteration, const ls_result *result);\nint ls_parse_u32(const char *text, uint32_t *value);\nint ls_parse_count(const char *text, unsigned maximum, unsigned *value);\n#endif\n', 'src/script_port.c': '#include "live_status.h"\n#include <inttypes.h>\n#include <string.h>\n\nstatic void record(ls_script *script, unsigned kind, uint32_t value)\n{\n if (script->event_count >= LS_MAX_EVENTS) {\n script->dropped_events++;\n return;\n }\n script->events[script->event_count].kind = kind;\n script->events[script->event_count].value = value;\n script->event_count++;\n}\n\nstatic uint32_t read_status(void *context)\n{\n ls_script *script = context;\n uint32_t status;\n script->current = script->next;\n status = script->publications[script->current].status;\n if (script->next + 1u < script->length) {\n script->next++;\n }\n record(script, 1u, status);\n return status;\n}\n\nstatic uint32_t read_payload(void *context)\n{\n ls_script *script = context;\n uint32_t payload = script->publications[script->current].payload;\n record(script, 2u, payload);\n return payload;\n}\n\nstatic void acknowledge(void *context)\n{\n ls_script *script = context;\n script->acknowledgements++;\n record(script, 3u, (uint32_t)script->current);\n}\n\nvoid ls_script_rewind(ls_script *script)\n{\n script->next = 0u;\n script->current = 0u;\n script->event_count = 0u;\n script->dropped_events = 0u;\n script->acknowledgements = 0u;\n}\n\nint ls_script_set(ls_script *script, const ls_publication *values, size_t count)\n{\n if (script == NULL || values == NULL || count == 0u || count > LS_MAX_ENTRIES) {\n return 0;\n }\n memcpy(script->publications, values, count * sizeof(*values));\n script->length = count;\n ls_script_rewind(script);\n return 1;\n}\n\nls_port ls_script_port(ls_script *script)\n{\n ls_port port;\n port.context = script;\n port.read_status = read_status;\n port.read_payload = read_payload;\n port.acknowledge = acknowledge;\n return port;\n}\n\nstatic int load_error(char *error, size_t capacity, unsigned line, const char *reason)\n{\n if (error != NULL && capacity != 0u) {\n snprintf(error, capacity, "line %u: %s", line, reason);\n }\n return 0;\n}\n\nint ls_script_load(FILE *stream, ls_script *script, char *error, size_t capacity)\n{\n ls_publication values[LS_MAX_ENTRIES];\n char line[160];\n unsigned line_number = 0u;\n size_t count = 0u;\n while (fgets(line, sizeof(line), stream) != NULL) {\n char left[64], right[64], extra[2];\n int fields;\n size_t length;\n line_number++;\n length = strlen(line);\n if (length != 0u && line[length - 1u] != \'\\n\' && !feof(stream)) {\n return load_error(error, capacity, line_number, "publication line too long");\n }\n fields = sscanf(line, " %63s %63s %1s", left, right, extra);\n if (fields == EOF) {\n continue;\n }\n if (left[0] == \'#\') {\n continue;\n }\n if (fields != 2) {\n return load_error(error, capacity, line_number, "expected status and payload");\n }\n if (count == LS_MAX_ENTRIES) {\n return load_error(error, capacity, line_number, "too many publications");\n }\n if (!ls_parse_u32(left, &values[count].status) ||\n !ls_parse_u32(right, &values[count].payload)) {\n return load_error(error, capacity, line_number, "invalid unsigned word");\n }\n count++;\n }\n if (ferror(stream)) {\n return load_error(error, capacity, line_number, "script read failed");\n }\n if (count == 0u) {\n return load_error(error, capacity, line_number, "empty script");\n }\n return ls_script_set(script, values, count);\n}\n\nvoid ls_script_dump(FILE *stream, const ls_script *script)\n{\n size_t index;\n for (index = 0u; index < script->event_count; index++) {\n const ls_event *event = &script->events[index];\n const char *kind = event->kind == 1u ? "status" :\n event->kind == 2u ? "payload" : "ack";\n fprintf(stream, " %zu %s=%" PRIu32 "\\n", index, kind, event->value);\n }\n if (script->dropped_events != 0u) {\n fprintf(stream, " dropped=%u\\n", script->dropped_events);\n }\n}\n', 'src/report.c': '#include "live_status.h"\n#include <inttypes.h>\n#include <limits.h>\n#include <string.h>\n\nint ls_parse_u32(const char *text, uint32_t *value)\n{\n uint32_t number = 0u;\n unsigned base = 10u;\n const unsigned char *cursor = (const unsigned char *)text;\n if (cursor == NULL || value == NULL || *cursor == \'\\0\') {\n return 0;\n }\n if (cursor[0] == \'0\' && (cursor[1] == \'x\' || cursor[1] == \'X\')) {\n base = 16u;\n cursor += 2;\n }\n if (*cursor == \'\\0\') {\n return 0;\n }\n while (*cursor != \'\\0\') {\n unsigned digit;\n if (*cursor >= \'0\' && *cursor <= \'9\') {\n digit = (unsigned)(*cursor - \'0\');\n } else if (*cursor >= \'a\' && *cursor <= \'f\') {\n digit = (unsigned)(*cursor - \'a\') + 10u;\n } else if (*cursor >= \'A\' && *cursor <= \'F\') {\n digit = (unsigned)(*cursor - \'A\') + 10u;\n } else {\n return 0;\n }\n if (digit >= base || number > (UINT32_MAX - digit) / base) {\n return 0;\n }\n number = number * base + digit;\n cursor++;\n }\n *value = number;\n return 1;\n}\n\nint ls_parse_count(const char *text, unsigned maximum, unsigned *value)\n{\n uint32_t parsed;\n if (!ls_parse_u32(text, &parsed) || parsed == 0u || parsed > maximum) {\n return 0;\n }\n *value = (unsigned)parsed;\n return 1;\n}\n\nvoid ls_statistics_reset(ls_statistics *stats)\n{\n memset(stats, 0, sizeof(*stats));\n stats->min_samples = UINT_MAX;\n}\n\nvoid ls_statistics_add(ls_statistics *stats, const ls_result *result)\n{\n stats->completed++;\n stats->samples += result->samples;\n if (result->samples < stats->min_samples) {\n stats->min_samples = result->samples;\n }\n if (result->samples > stats->max_samples) {\n stats->max_samples = result->samples;\n }\n switch (result->outcome) {\n case LS_READY:\n stats->ready++;\n break;\n case LS_FAULT:\n stats->faulty++;\n break;\n case LS_EXHAUSTED:\n stats->exhausted++;\n break;\n default:\n stats->invalid++;\n break;\n }\n}\n\nvoid ls_statistics_print(FILE *stream, const ls_statistics *stats)\n{\n fprintf(stream, "summary sessions=%" PRIu64 " ready=%" PRIu64\n " fault=%" PRIu64 " exhausted=%" PRIu64 " invalid=%" PRIu64 "\\n",\n stats->completed, stats->ready, stats->faulty, stats->exhausted, stats->invalid);\n fprintf(stream, "samples total=%" PRIu64 " min=%u max=%u\\n", stats->samples,\n stats->completed == 0u ? 0u : stats->min_samples, stats->max_samples);\n}\n\nvoid ls_result_print(FILE *stream, unsigned iteration, const ls_result *result)\n{\n fprintf(stream, "session=%u outcome=%s samples=%u status=%" PRIu32\n " payload=%" PRIu32 "\\n", iteration, ls_outcome_name(result->outcome),\n result->samples, result->status, result->payload);\n}\n', 'tools/observer.c': '#include "live_status.h"\n#include <inttypes.h>\n#include <stdio.h>\n\nint main(void)\n{\n unsigned count, index;\n if (scanf("%u", &count) != 1) return 2;\n for (index = 0u; index < count; index++) {\n unsigned mode, budget, length, item;\n uint32_t ready, fault;\n ls_publication entries[LS_MAX_ENTRIES];\n ls_script script;\n ls_port port;\n ls_request request;\n ls_result result;\n ls_session session;\n unsigned terminal_unchanged = 1u;\n if (scanf("%u %" SCNu32 " %" SCNu32 " %u %u",\n &mode, &ready, &fault, &budget, &length) != 5) return 2;\n if (length == 0u || length > LS_MAX_ENTRIES) return 2;\n for (item = 0u; item < length; item++) {\n if (scanf("%" SCNu32 " %" SCNu32,\n &entries[item].status, &entries[item].payload) != 2) return 2;\n }\n ls_script_set(&script, entries, length);\n port = ls_script_port(&script);\n request.ready_mask = ready;\n request.fault_mask = fault;\n request.max_samples = budget;\n if (mode == 0u) {\n result = ls_wait(&port, &request);\n } else {\n unsigned steps = 0u;\n size_t before;\n ls_result saved;\n ls_session_begin(&session, &port, &request);\n while (session.result.outcome == LS_WAITING && steps < LS_MAX_SAMPLES + 2u) {\n ls_session_step(&session);\n steps++;\n }\n result = session.result;\n before = script.event_count;\n saved = session.result;\n ls_session_step(&session);\n terminal_unchanged = before == script.event_count &&\n saved.outcome == session.result.outcome && saved.samples == session.result.samples &&\n saved.status == session.result.status && saved.payload == session.result.payload;\n }\n printf("R %u %u %u %" PRIu32 " %" PRIu32 " %u %u %zu %u",\n index, (unsigned)result.outcome, result.samples, result.status, result.payload,\n script.acknowledgements, terminal_unchanged, script.event_count, script.dropped_events);\n for (item = 0u; item < script.event_count; item++) {\n printf(" %u %" PRIu32, script.events[item].kind, script.events[item].value);\n }\n putchar(\'\\n\');\n }\n return 0;\n}\n'}
|
|
|
|
"""Independent port contract model: no candidate or reference execution."""
|
|
import random
|
|
|
|
|
|
def expected(case):
|
|
mode,ready,fault,budget,entries=case
|
|
result=[4,0,0,0]
|
|
trace=[]
|
|
if ready==0 or ready & fault or not 1<=budget<=64:
|
|
return result,trace
|
|
result=[0,0,0,0]
|
|
for sample in range(budget):
|
|
index=min(sample,len(entries)-1)
|
|
status,payload=entries[index]
|
|
trace.append([1,status])
|
|
result[1]+=1
|
|
result[2]=status
|
|
if status & fault:
|
|
result[0]=2
|
|
break
|
|
if status & ready == ready:
|
|
trace.extend([[2,payload],[3,index]])
|
|
result[0]=1
|
|
result[3]=payload
|
|
break
|
|
if result[1]==budget:
|
|
result[0]=3
|
|
return result,trace
|
|
|
|
|
|
def cases():
|
|
result=[]
|
|
def add(ready,fault,budget,entries):
|
|
for mode in (0,1): result.append((mode,ready,fault,budget,entries))
|
|
add(1,128,4,[(0,9),(0,10),(1,11),(128,12)])
|
|
add(3,128,4,[(1,20),(2,21),(3,22)])
|
|
add(1,128,1,[(1,0)])
|
|
add(1,128,1,[(0,0)])
|
|
add(3,128,3,[(1,5),(2,6),(0,7)])
|
|
add(1,128,4,[(129,87)])
|
|
add(0x80000000,1,64,[(0xffffffff,0xffffffff)])
|
|
add(0x80000000,1,64,[(0x40000000,3)]*63+[(0x80000000,0xffffffff)])
|
|
add(1,128,3,[(0,1),(0,2),(0,3),(1,4)])
|
|
add(3,0,64,[(2,17)])
|
|
add(1,0,2,[(0x100,3),(0x101,4)])
|
|
for ready,fault,budget in [(0,0,5),(1,1,5),(1,0,0),(1,0,65)]:
|
|
add(ready,fault,budget,[(1,99)])
|
|
rng=random.Random(0xE156A11)
|
|
for i in range(72):
|
|
bits=rng.sample(range(32),rng.randint(1,4))
|
|
ready=sum(1<<bit for bit in bits)
|
|
unused=[bit for bit in range(32) if bit not in bits]
|
|
fault=0 if i%4==0 else 1<<rng.choice(unused)
|
|
budget=rng.choice([1,2,3,8,16,64])
|
|
entries=[]
|
|
for j in range(rng.randint(1,12)):
|
|
status=rng.getrandbits(32)
|
|
if i%3==0 and j==0: status &= ~ready
|
|
if i%5==0: status &= ~fault
|
|
entries.append((status,rng.getrandbits(32)))
|
|
add(ready,fault,budget,entries)
|
|
return result
|
|
|
|
|
|
def encode(values):
|
|
lines=[str(len(values))]
|
|
for mode,ready,fault,budget,entries in values:
|
|
lines.append(' '.join(map(str,[mode,ready,fault,budget,len(entries)])))
|
|
lines.extend('%d %d'%entry for entry in entries)
|
|
return ('\n'.join(lines)+'\n').encode('ascii')
|
|
|
|
|
|
COMPILER_SECONDS = 25
|
|
CANDIDATE_SECONDS = 10
|
|
CLEANUP_SECONDS = 5
|
|
ABSENCE_SECONDS = 5
|
|
|
|
|
|
def decimal_token(token):
|
|
import re
|
|
if re.fullmatch(rb'[+-]?[0-9]+',token) is None:
|
|
raise ValueError('nondecimal output token')
|
|
negative=token[:1]==b'-'
|
|
digits=token[1:] if token[:1] in (b'+',b'-') else token
|
|
digits=digits.lstrip(b'0') or b'0'
|
|
if len(digits)>10:
|
|
raise ValueError('observable integer out of range')
|
|
return -int(digits) if negative else int(digits)
|
|
|
|
|
|
def compare(raw,values):
|
|
import re
|
|
lines=raw.split(b'\n')
|
|
if lines[-1:]==[b'']: lines.pop()
|
|
if len(lines)!=len(values):
|
|
raise ValueError('raw line count mismatch')
|
|
for index,(line,case) in enumerate(zip(lines,values)):
|
|
if line.endswith(b'\r'): line=line[:-1]
|
|
fields=re.split(rb'[ \t]+',line.strip(b' \t'))
|
|
if not fields or fields[0]!=b'R':
|
|
raise ValueError('raw record tag mismatch')
|
|
actual=[decimal_token(token) for token in fields[1:]]
|
|
result,trace=expected(case)
|
|
wanted=[index,*result,int(result[0]==1),1,len(trace),0]
|
|
wanted.extend(value for event in trace for value in event)
|
|
if actual!=wanted:
|
|
difference=next((i for i,(a,b) in enumerate(zip(actual,wanted)) if a!=b),min(len(actual),len(wanted)))
|
|
raise ValueError('raw behavior mismatch case=%d field=%d'%(index,difference))
|
|
|
|
|
|
def main():
|
|
evidence=[]
|
|
failures=[]
|
|
scratch=None
|
|
business=False
|
|
cleaned=False
|
|
absent=False
|
|
try:
|
|
values=cases()
|
|
payload=encode(values)
|
|
candidate=(Path(sys.argv[1])/'src/live_status.c').read_bytes()
|
|
scratch=Path(tempfile.mkdtemp(prefix='dev-live-status-'))
|
|
materials=dict(SUPPORT)
|
|
materials['src/live_status.c']=candidate
|
|
for name,content in materials.items():
|
|
path=scratch/name
|
|
path.parent.mkdir(parents=True,exist_ok=True,mode=0o755)
|
|
path.parent.chmod(0o777)
|
|
path.write_bytes(content.encode('utf-8') if isinstance(content,str) else content)
|
|
path.chmod(0o644)
|
|
compiler=['gcc','-std=c11','-O2','-Wall','-Wextra','-Wpedantic','-Iinclude',
|
|
'src/live_status.c','src/script_port.c','src/report.c','tools/observer.c','-o','observer']
|
|
execute_stage('compiler',compiler,scratch,COMPILER_SECONDS,b'',evidence)
|
|
output,diagnostics=execute_stage('candidate',[str(scratch/'observer')],scratch,CANDIDATE_SECONDS,payload,evidence)
|
|
compare(output,values)
|
|
business=True
|
|
except BaseException as error:
|
|
failures.append(['business',type(error).__name__,str(error)])
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/tmp',CLEANUP_SECONDS,b'',evidence)
|
|
cleaned=True
|
|
except BaseException as error:
|
|
failures.append(['cleanup',type(error).__name__,str(error)])
|
|
try:
|
|
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/tmp',ABSENCE_SECONDS,b'',evidence)
|
|
absent=True
|
|
except BaseException as error:
|
|
failures.append(['absence',type(error).__name__,str(error)])
|
|
report=dict(stages=evidence,failures=failures,business=business,cleaned=cleaned,absent=absent)
|
|
packed=base64.b64encode(bz2.compress(json.dumps(report,separators=(',',':')).encode())).decode('ascii')
|
|
print('EXEC_BZ2:'+packed,file=sys.stderr)
|
|
if business and cleaned and absent and not failures:
|
|
print('PASS')
|
|
return 0
|
|
print('FAIL '+('; '.join(row[0]+':'+row[1]+':'+row[2] for row in failures) or 'unconfirmed execution'))
|
|
return 1
|
|
|
|
|
|
if __name__=='__main__':
|
|
sys.exit(main())
|