amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

369 lines
15 KiB
Python

import copy
import json
import os
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
FILES = ['stack_domain.h', 'number.c', 'port_policy.c', 'stack_attr.c', 'console.c', 'main.c']
LIMIT = 1099511627776
MAX = (1 << 64) - 1
def rounded(port, request):
if port is None or not port[0] <= request <= port[2]:
return None
result = ((request + port[1] - 1) // port[1]) * port[1]
return result if result <= port[2] else None
def policy(minimum, quantum, ceiling, has_default, default):
if has_default not in (0, 1) or not 1 <= minimum <= ceiling <= LIMIT or not 1 <= quantum <= ceiling:
return None
result = rounded((minimum, quantum, ceiling, 0), minimum if has_default == 0 else default)
return None if result is None else (minimum, quantum, ceiling, result)
def number(data):
if not data or any(byte < 48 or byte > 57 for byte in data):
return None
value = 0
for byte in data:
value = value * 10 + byte - 48
if value > MAX:
return None
return value
def attr(port):
return {'port': port, 'size': port[3], 'revision': 1, 'explicit': 0}
def change(state, operation, request=0):
if state is None:
return 22, state
size = rounded(state['port'], request) if operation == 's' else state['port'][3]
if size is None or state['revision'] == MAX:
return 22, state
state = dict(state, size=size, revision=state['revision'] + 1, explicit=int(operation == 's'))
return 0, state
class RawCases:
def __init__(self):
self.ports = [None, None]
self.attrs = [None, None]
self.lines = []
self.expected = []
def add(self, op, *args):
values = dict(rc=0, v=9001, rev=9002, x=9003, input_same=1)
mutable_p = None
mutable_a = None
destroyed = None
if op == 'p':
index, minimum, quantum, ceiling, has_default, default, mask = args
result = policy(minimum, quantum, ceiling, has_default, default)
if mask or result is None:
values['rc'] = 22
else:
self.ports[index] = result
mutable_p = index
elif op in ('g', 'r'):
index, request, mask, seed = args
result = None if self.ports[index] is None else self.ports[index][3]
if op == 'r':
result = rounded(self.ports[index], request)
values['v'] = seed
if mask or result is None:
values['rc'] = 22
else:
values['v'] = result
elif op == 'i':
index, source, mask = args
if mask or self.ports[source] is None:
values['rc'] = 22
else:
self.attrs[index] = attr(self.ports[source])
mutable_a = index
elif op == 'c':
index, source, mask = args
if mask or self.attrs[source] is None:
values['rc'] = 22
elif index != source:
self.attrs[index] = copy.deepcopy(self.attrs[source])
mutable_a = index
elif op == 's':
index, request, mask = args
if mask:
values['rc'] = 22
else:
values['rc'], self.attrs[index] = change(self.attrs[index], 's', request)
if values['rc'] == 0:
mutable_a = index
elif op == 'a':
index, mask, seed = args
values.update(v=seed, rev=seed+1, x=seed % 97)
if mask or self.attrs[index] is None:
values['rc'] = 22
else:
a = self.attrs[index]
values.update(v=a['size'], rev=a['revision'], x=a['explicit'])
elif op in ('d', 't'):
index, mask = args
if mask or self.attrs[index] is None:
values['rc'] = 22
elif op == 'd':
self.attrs[index] = None
mutable_a = index
destroyed = index
else:
values['rc'], self.attrs[index] = change(self.attrs[index], 't')
if values['rc'] == 0:
mutable_a = index
elif op == 'n':
data, mask, seed = args
result = number(data)
values['v'] = seed
if mask or result is None:
values['rc'] = 22
else:
values['v'] = result
args = (len(data), data.hex() or '00', mask, seed)
self.lines.append(' '.join(map(str, (op, *args))))
self.expected.append((values, mutable_p, mutable_a, destroyed))
def inspect(self):
for index in range(2):
self.add('g', index, 0, 0, 17001 + index)
self.add('a', index, 0, 27001 + index)
def raw_cases():
c = RawCases()
c.inspect()
for seed in (0, 7, MAX - 1):
for mask in range(4):
c.add('g', 0, 0, mask, seed)
c.add('r', 0, 1, mask, seed)
for mask in range(16):
c.add('a', 0, mask, seed)
for mask in range(4):
c.add('i', 0, 0, mask)
c.add('c', 0, 1, mask)
for op in ('s', 't', 'd'):
for mask in (0, 1):
if op == 's': c.add(op, 0, 16384, mask)
else: c.add(op, 0, mask)
configurations = [
(16384,4096,1048576,0,0), (17,8,100,0,MAX), (17,8,100,1,33),
(17,8,100,1,0), (17,8,100,1,16), (17,8,100,1,97),
(0,8,100,0,0), (1,0,100,0,0), (100,8,99,0,0),
(1,101,100,0,0), (1,1,LIMIT+1,0,0), (1,1,LIMIT,2,1),
(1,1,LIMIT,-1,1), (1,1,LIMIT,1,MAX), (LIMIT,1,LIMIT,0,0),
(LIMIT,7,LIMIT,0,0), (1,LIMIT,LIMIT,0,0), (1,1,1,1,1),
(1,MAX,MAX,0,0), (MAX,1,MAX,0,0), (1,1,0,0,0)]
for i, conf in enumerate(configurations):
index = i % 2
c.add('p', index, *conf, 0)
c.inspect()
c.add('i', index, index, 0)
c.inspect()
c.add('p',0,17,8,100,0,0,0)
c.add('p',1,100,7,1000,1,123,0)
c.add('i',0,0,0)
c.add('i',1,1,0)
for index in range(2):
for mask in (1,2,3):
c.add('p',index,17,8,100,1,0,1)
c.add('g',index,0,mask,11000+mask)
c.add('r',index,42,mask,12000+mask)
c.add('i',index,1-index,mask)
c.add('c',index,1-index,mask)
for mask in range(1,16):
c.add('a',index,mask,8000+mask)
c.add('s',index,42,1)
c.add('t',index,1)
c.add('d',index,1)
c.inspect()
rng = random.Random(220059)
for _ in range(95):
index = rng.randrange(2)
request = rng.choice([0,1,16,17,18,23,24,25,96,97,100,101,119,126,999,1000,MAX])
c.add('r',index,request,0,rng.randrange(10000))
c.add('s',index,request,0)
c.inspect()
if rng.randrange(6) == 0:
c.add('c',1-index,index,0)
c.add('p',index,17+index,8,100,0,MAX,0)
c.inspect()
if rng.randrange(9) == 0:
c.add('t',index,0)
c.inspect()
for index in range(2):
c.add('c',index,index,0)
c.add('d',index,0)
c.add('d',index,0)
c.add('c',1-index,index,0)
c.add('i',index,index,0)
c.inspect()
# Reach the size domain's maximum in two independently owned contexts.
for index in range(2):
c.add('p',index,1,1,LIMIT,1,LIMIT-index,0)
c.add('i',index,index,0)
c.add('s',index,LIMIT,0)
c.inspect()
c.add('p',index,17,8,100,0,0,0)
c.add('t',index,0)
c.inspect()
c.add('d',index,0)
c.add('i',index,index,0)
c.inspect()
inputs = [b'',b'0',b'000000',b'18446744073709551615',b'18446744073709551616',
b'9'*100,b'0'*16383+b'7',b'0'*16384,b'+1',b'-1',b' 1',b'1\x00',b'1\n']
inputs += [bytes([byte]) for byte in range(256)]
inputs += [b'12'+bytes([byte])+b'34' for byte in range(256) if not 48<=byte<=57]
for data in inputs:
for seed in (0, MAX-1): c.add('n',data,0,seed)
for mask in (1,2,3):
c.add('n',b'42',mask,31)
c.add('n',b'',mask,77)
return ('\n'.join(c.lines)+'\n').encode(), c.expected
def console_model(data):
current = policy(16384,4096,1048576,0,0)
slots = [None]*8
responses = []
lines = data.split(b'\n')
if lines[-1] == b'': lines.pop()
for line in lines:
response = dict(rc=22,size=0,revision=0,explicit=0)
tokens = re.split(b'[ \t\r]+', line.strip(b' \t\r'))
if len(line) > 16384 or not tokens or tokens == [b'']:
responses.append(response);continue
command = tokens[0]
if command == b'P' and len(tokens)==5:
nums = [number(t) for t in tokens[1:4]]
default = 0 if tokens[4]==b'-' else number(tokens[4])
if None not in nums and default is not None:
p = policy(*nums,int(tokens[4]!=b'-'),default)
if p is not None: current=p
response.update(rc=0 if p is not None else 22,size=current[3])
elif command in (b'I',b'S',b'Q',b'C',b'R',b'D'):
expected_count = 3 if command in (b'S',b'C') else 2
nums = [number(t) for t in tokens[1:]]
if len(tokens)==expected_count and None not in nums and nums[0]<8 and (command!=b'C' or nums[1]<8):
i=nums[0];rc=0
if command==b'I': slots[i]=attr(current)
elif command==b'S': rc,slots[i]=change(slots[i],'s',nums[1])
elif command==b'R': rc,slots[i]=change(slots[i],'t')
elif command==b'C':
if slots[nums[1]] is None: rc=22
else: slots[i]=copy.deepcopy(slots[nums[1]])
elif command==b'D':
if slots[i] is None: rc=22
else: slots[i]=None
elif slots[i] is None: rc=22
response['rc']=rc
if slots[i] is not None:
response.update({key:slots[i][key] for key in ('size','revision','explicit')})
responses.append(response)
return responses
def console_cases():
smoke=b'P 16384 4096 1048576 -\nI 0\nS 0 25000\nQ 0\nC 1 0\nS 0 40960\nQ 1\n'
regression=b'Q 0\nP 17 8 100 -\nI 0\nS 0 25\nC 1 0\nP 100 7 1000 123\nI 2\nR 1\nQ 0\nQ 2\nP 17 8 100 0\nI 3\nS 2 1001\nD 0\nD 0\nC 0 1\nC 0 0\nQ 0\n'
malformed=b'\n \t\r\nP 1 1 10\nP 1 1 10 1 extra\nP 1\v1 10 -\nP 1\f1 10 -\nI +1\nI -0\nI 8\nI 18446744073709551616\nI 1\x00\nI 0 extra\nS 0 0\nX 0\n'
long_lines=b'I '+b'0'*16382+b'\nQ 0\nI '+b'0'*16383+b'\nQ 0\n'+b'P 1 1 10 '+b'0'*16370+b'1\nI 7\nQ 7\n'
huge=b'P 1 1 1099511627776 -\nI 0\nS 0 1099511627776\nC 7 0\nP 1099511627776 7 1099511627776 -\nR 7\nD 0\nI 0\nS 0 1099511627777\nQ 7'
rng=random.Random(59022)
mixed=[b'P 17 8 100 -']
for _ in range(180):
cmd=rng.choice([b'I',b'Q',b'S',b'C',b'R',b'D'])
i=rng.randrange(8)
parts=[cmd,str(i).encode()]
if cmd==b'S':parts.append(str(rng.choice([0,16,17,18,24,25,96,97,100,MAX])).encode())
elif cmd==b'C':parts.append(str(rng.randrange(8)).encode())
mixed.append(b' \t'.join(parts))
return [b'',smoke,regression,malformed,long_lines,huge,b'\n'.join(mixed)+b'\n',b' I 000\r\t\nS 00 '+b'0'*16000+b'25000\nQ 0']
def json_stream(raw):
text=raw.decode('utf-8')
def invalid_constant(value):
raise ValueError("invalid JSON constant")
decoder=json.JSONDecoder(parse_constant=invalid_constant)
result=[];pos=0
while pos<len(text):
while pos<len(text) and text[pos] in ' \t\r\n':pos+=1
if pos==len(text):break
value,pos=decoder.raw_decode(text,pos)
if pos<len(text) and text[pos] not in " \t\r\n":
raise ValueError("JSON object separator")
result.append(value)
return result
def require_int_object(observed, expected):
if not isinstance(observed,dict):return False
return all(type(observed.get(key)) is int and observed[key]==value for key,value in expected.items())
def main():
candidate=Path(sys.argv[1])
# Read all candidate bytes and create every compiler input before any child.
contents={name:(candidate/name).read_bytes() for name in FILES}
observer=Path(__file__).with_name('observer.c').read_bytes()
raw_input,raw_expected=raw_cases()
cli_inputs=console_cases()
cli_expected=[console_model(data) for data in cli_inputs]
work=Path(tempfile.mkdtemp(prefix='stack-domain-',dir=Path.cwd()))
for name,data in contents.items():(work/name).write_bytes(data)
(work/'observer.c').write_bytes(observer)
wrappers=[]
for index,name in enumerate(FILES[1:]):
wrapper='observe_'+str(index)+'.c'
(work/wrapper).write_text('#define main sd_candidate_main\n#include "'+name+'"\n')
wrappers.append(wrapper)
input_error_fd=os.open("/",os.O_RDONLY)
output_error_fd=os.open("/dev/full",os.O_WRONLY)
def run(argv,data=None):
completed=subprocess.run(argv,cwd=work,input=data,capture_output=True,timeout=12)
if completed.returncode:
sys.stderr.write(completed.stderr.decode('utf-8','replace')[-2000:])
raise ValueError('candidate process failed')
return completed.stdout
flags=['cc','-std=c11','-O2','-Wall','-Wextra','-Werror']
run(flags+FILES[1:]+['-o','service'])
run(flags+wrappers+['observer.c','-o','observe'])
checks=0
for executable in (['./service'],['./observe','console']):
for data,expected in zip(cli_inputs,cli_expected):
raw=run(executable,data)
if data==b'' and raw!=b'':raise ValueError('empty stdin output')
observed=json_stream(raw)
if len(observed)!=len(expected):raise ValueError('console response count')
for actual,wanted in zip(observed,expected):
if not require_int_object(actual,wanted):raise ValueError('console contract')
checks+=1
failed_input=subprocess.run(executable,cwd=work,stdin=input_error_fd,capture_output=True,timeout=12)
failed_output=subprocess.run(executable,cwd=work,input=b'I 0\n',stdout=output_error_fd,stderr=subprocess.PIPE,timeout=12)
if failed_input.returncode==0 or failed_output.returncode==0:raise ValueError('console I/O failure result')
checks+=2
os.close(input_error_fd)
os.close(output_error_fd)
observed=json_stream(run(['./observe'],raw_input))
if len(observed)!=len(raw_expected):raise ValueError('helper response count')
for actual,(wanted,mutable_p,mutable_a,destroyed) in zip(observed,raw_expected):
if not require_int_object(actual,wanted):raise ValueError('helper result')
for field,mutable in [('p',mutable_p),('a',mutable_a)]:
flags=actual.get(field)
if not isinstance(flags,list) or len(flags)!=2:raise ValueError('helper state observations')
for i,value in enumerate(flags):
if type(value) is not int or value not in (0,1) or (i!=mutable and value!=1):
raise ValueError('helper object preservation')
if destroyed is not None:
z=actual.get('zero')
if not isinstance(z,list) or len(z)!=2 or type(z[destroyed]) is not int or z[destroyed]!=1:
raise ValueError('destroy result')
checks+=1
print('PASS')
print('behavior observations='+str(checks),file=sys.stderr)
if __name__=='__main__':
try:main()
except (ValueError,UnicodeError,subprocess.SubprocessError,OSError) as error:
print('FAIL: '+str(error))
raise SystemExit(1)