forked from mtusi2026/mtusi2026lab5
90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
import base64
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
folder = Path(__file__).parent
|
|
fixtures = {
|
|
p.name: p.read_bytes()
|
|
for p in Path("evidence/final-validation-2026-11-07/fixtures").glob("*.png")
|
|
}
|
|
data = fixtures["gray16-40mp.png"]
|
|
result, offset = data[:8], 8
|
|
while offset < len(data):
|
|
size = int.from_bytes(data[offset : offset + 4], "big") + 12
|
|
if data[offset + 4 : offset + 8] != b"tRNS":
|
|
result += data[offset : offset + size]
|
|
offset += size
|
|
fixtures["gray16-opaque-40mp.png"] = result
|
|
fixtures["sample.png"] = Path("tests/fixtures/sample.png").read_bytes()
|
|
bootstrap = """
|
|
import base64,json,sys,subprocess,time
|
|
from pathlib import Path
|
|
p=json.load(sys.stdin)
|
|
d=Path('/tmp/png-memory-validation')
|
|
d.mkdir(exist_ok=True)
|
|
for name,data in p['fixtures'].items():
|
|
(d/name).write_bytes(base64.b64decode(data))
|
|
def command(name):
|
|
return [sys.executable,'-c',p['code'],str(d/name),str(d/'sample.png'),'false' if 'opaque' in name else 'true']
|
|
results=[]
|
|
for name in sorted(p['fixtures']):
|
|
if name=='sample.png':
|
|
continue
|
|
output=subprocess.check_output(command(name),text=True,timeout=65)
|
|
row=json.loads(output)
|
|
results.append(row)
|
|
print(json.dumps({'case':row}),flush=True)
|
|
children=[subprocess.Popen(command('rgb16-40mp.png'),stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True) for _ in range(2)]
|
|
parallel=[]
|
|
try:
|
|
for child in children:
|
|
out,err=child.communicate(timeout=65)
|
|
assert child.returncode==0,(child.returncode,err)
|
|
parallel.append(json.loads(out))
|
|
finally:
|
|
for child in children:
|
|
if child.poll() is None:
|
|
child.kill()
|
|
child.wait()
|
|
overlap=any(max(a['started_monotonic'],b['started_monotonic'])<min(a['finished_monotonic'],b['finished_monotonic']) for a in parallel[0]['versions'] for b in parallel[1]['versions'])
|
|
assert overlap
|
|
peak=int(Path('/sys/fs/cgroup/memory.peak').read_text())
|
|
limit=int(Path('/sys/fs/cgroup/memory.max').read_text())
|
|
assert peak<limit
|
|
print(json.dumps({'result':'PASS','cases':results,'concurrent':parallel,'render_overlap':overlap,'cgroup_peak':peak,'cgroup_limit':limit,'memory_events':Path('/sys/fs/cgroup/memory.events').read_text()}),flush=True)
|
|
"""
|
|
payload = {
|
|
"code": (folder / "linux-child.py").read_text(),
|
|
"fixtures": {name: base64.b64encode(data).decode() for name, data in fixtures.items()},
|
|
}
|
|
output = folder / "linux.json"
|
|
if output.exists():
|
|
raise SystemExit("Result exists; do not repeat completed evidence")
|
|
process = subprocess.Popen(
|
|
["docker", "compose", "exec", "-T", "worker", "python", "-c", bootstrap],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
)
|
|
process.stdin.write(json.dumps(payload))
|
|
process.stdin.close()
|
|
lines = []
|
|
for line in process.stdout:
|
|
print(line, end="", flush=True)
|
|
lines.append(line)
|
|
code = process.wait()
|
|
report = {
|
|
"source_sha": subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip(),
|
|
"exit_code": code,
|
|
"input_sha256": {name: hashlib.sha256(data).hexdigest() for name, data in fixtures.items()},
|
|
}
|
|
if code == 0:
|
|
report.update(json.loads(lines[-1]))
|
|
else:
|
|
report["result"] = "FAIL"
|
|
output.write_text(json.dumps(report, indent=2) + "\n")
|
|
raise SystemExit(code)
|