data-gen-bpu/scripts/jail_probe.sh

52 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT=$(cd "$(dirname "$0")/.." && pwd)
export PROBE_MODE="${2:-e2e}"
export PROBE_WORK_ROOT=$(python3 -c 'import yaml,os,sys; print(os.path.expanduser(yaml.safe_load(open(sys.argv[1]))["paths"].get("work_root") or ""))' "$ROOT/config.yaml")
exec "$ROOT/scripts/agent_jail.sh" "$1" "${2:-e2e}" -- python3 - <<'PY'
import os, subprocess, uuid
from pathlib import Path
rd=Path(os.environ['BENCH_ROLLOUT_DIR']); bench=Path(os.environ['BENCH_ROOT'])
jd=rd.parent.parent
fails=[]
def check(label, ok):
print(('PASS ' if ok else 'FAIL ')+label, flush=True)
if not ok: fails.append(label)
def unreadable(p):
try: p.read_bytes(); return False
except OSError: return True
def unwritable(p):
# Never truncate a real existing file, even on a broken jail.
try:
fd=os.open(p, (os.O_WRONLY | os.O_APPEND) if p.exists() else (os.O_WRONLY | os.O_CREAT | os.O_EXCL), 0o600); os.close(fd); return False
except OSError: return True
other=jd.parent/'cls--resnet18--nash-m--ptq--int8'/'oracle'/'result.json'
check('other job oracle hidden', unreadable(other))
check('own oracle mode policy', unreadable(jd/'oracle/result.json') if os.environ['PROBE_MODE'] != 'repair' else not unreadable(jd/'oracle/result.json'))
check('sibling hidden', unreadable(jd/'rollouts/00/claim.json'))
check('job input readable', not unreadable(jd/'job.json'))
if (jd/'data/README').exists():
check('job data readable', not unreadable(jd/'data/README'))
work=Path(os.environ['PROBE_WORK_ROOT'])
own=work/jd.parent.name/jd.name/('r'+rd.name)/'probe-output'
if own.exists():
check('own work readable and read-only',not unreadable(own) and unwritable(own))
check('other work hidden',unreadable(own.parent.parent/'r00/probe-output'))
check('state sqlite hidden', unreadable(bench/'state/jobs.sqlite'))
check('real state sqlite hidden', unreadable(Path.home()/'bench-state/jobs.sqlite'))
for p in [bench/'registry/gates.yaml', bench/'harness/gates.py', other, bench/'state/x', Path.home()/'bench-state/x',rd/'rollout.json']:
check('write denied '+str(p), unwritable(p))
# Only create a NEW workspace artifact.
p=rd/'workspace'/('probe-'+uuid.uuid4().hex)
try:
with p.open('x') as f: f.write('probe')
check('workspace writable', True)
except OSError: check('workspace writable', False)
r=subprocess.run([str(bench/'bin/bench'),'stage','status','nonexistent'],capture_output=True,text=True)
check('stdlib stage status executes',r.returncode==1 and 'handle 不存在' in r.stderr and 'harness' not in r.stderr)
r=subprocess.run(['mount','-o','remount,bind,rw',str(rd)],capture_output=True)
check('cannot undo read-only mounts',r.returncode!=0)
check('host processes hidden',len(list(Path('/proc').glob('[0-9]*')))<15)
raise SystemExit(bool(fails))
PY