"""Disposable software VM: no NIC, host shares, writable disks or hardware virtualization.""" import gzip import hashlib import json import re import shutil import stat import threading import time import uuid from pathlib import Path from src.adapters.sandbox.docker import DockerSandbox, CommandRunner from src.modules.skill.domain.models import SkillError, canonical, digest from src.modules.execution.pdf_dependency import overlay as dependency_overlay, COORDINATE, VERSION, SHA512 SLOT = threading.BoundedSemaphore(1) VM_LAUNCH_V8 = {'machine':'q35,accel=tcg','cpu':'max','memory_mb':1024,'vcpus':1, 'kernel_arguments':'console=ttyS0 quiet panic=1 noapic rdinit=/init'} def cpio(entries): """Small newc overlay, with explicit mode/ownership; never extracts on the host.""" data = bytearray() for index, (name, content, mode) in enumerate([*entries, ('TRAILER!!!', b'', 0)]): encoded = name.encode() + b'\0' fields = [index + 1, mode, 0, 0, 1, 0, len(content), 0, 0, 0, 0, len(encoded), 0] data.extend(('070701' + ''.join(f'{v:08x}' for v in fields)).encode()) data.extend(encoded) data.extend(b'\0' * (-len(data) % 4)) data.extend(content) data.extend(b'\0' * (-len(data) % 4)) return bytes(data) class QemuSandbox(DockerSandbox): execution_mode = 'qemu' def __init__(self, root, config, *, executable, image_root, command=None): super().__init__(root, config, executable=executable, command=command or CommandRunner()) self.image_root = Path(image_root).resolve() self.control = Path(__file__).parents[2] / 'modules/execution/container' self.worker = self.control / 'vm_worker.py' self.probe_file = self.control / 'probe.py' self.manifest_path = self.image_root / 'manifest.json' for name,worker,probe in [('manifest-v9.json','vm_worker_v7.py','probe_v3.py'),('manifest-v8.json','vm_worker_v6.py','probe_v3.py'),('manifest-v7.json','vm_worker_v6.py','probe_v3.py'),('manifest-v6.json','vm_worker_v5.py','probe_v2.py'),('manifest-v5.json','vm_worker_v4.py','probe_v2.py'),('manifest-v4.json','vm_worker_v3.py','probe_v2.py'),('manifest-v3.json','vm_worker_v2.py','probe_v2.py'),('manifest-v2.json','vm_worker_v2.py','probe.py')]: candidate_path=self.image_root/name if candidate_path.is_file(): try: candidate=json.loads(candidate_path.read_text()) if self.config.image=='zkcoding-qemu@sha256:'+digest(candidate): self.manifest_path=candidate_path;self.worker=self.control/worker;self.probe_file=self.control/probe break except (ValueError,OSError):pass @property def command_timeout(self): if self.manifest_path.name in {'manifest-v7.json','manifest-v8.json','manifest-v9.json'}: return 420 if self.config.dependency_profile == 'pdfbox-3.0.8' else 300 return 210 if self.config.dependency_profile == 'pdfbox-3.0.8' else 150 @property def kernel_arguments(self): return VM_LAUNCH_V8['kernel_arguments'] if self.manifest_path.name in {'manifest-v8.json','manifest-v9.json'} else 'console=ttyS0 quiet panic=1 rdinit=/init' @staticmethod def known_boot_failure(output): return ('Kernel panic - not syncing: IO-APIC + timer doesn\'t work!' in output and not any(marker in output for marker in ('ZKCODING_VM_STAGE=','ZKCODING_VM_RESULT=','ZKCODING_VM_READY='))) def preflight(self): try: manifest = json.loads(self.manifest_path.read_text()) paths = {'qemu': Path(self.executable), 'kernel': self.image_root / 'kernel', 'rootfs': self.image_root / 'rootfs.cpio.gz', 'worker': self.worker, 'probe': self.probe_file} actual = {name: hashlib.sha256(path.read_bytes()).hexdigest() for name, path in paths.items()} if self.manifest_path.name in {'manifest-v8.json','manifest-v9.json'}:actual['launch']=VM_LAUNCH_V8 if manifest != actual or self.config.image != 'zkcoding-qemu@sha256:' + digest(actual): raise ValueError() except Exception: raise SkillError('ISOLATION_IMAGE_MISMATCH', '软件虚拟机镜像/控制器校验失败。', 503) from None dependency_overlay(self.config) @property def runtime_contract(self): value = super().runtime_contract if self.config.dependency_profile != 'jdk': value['dependencies'] = [{'coordinate': COORDINATE, 'version': VERSION, 'sha512': SHA512}] value['dependency_profile'] = self.config.dependency_profile return value def build(self, run_id, source, cancelled): self.name(run_id) self.preflight() if digest(source['files']) != source['source_digest']: raise SkillError('SOURCE_DIGEST_MISMATCH', '源码摘要不符。', 409) if not SLOT.acquire(blocking=False): raise SkillError('SANDBOX_CAPACITY_EXCEEDED', '本地虚拟机容量已占用。', 409) try: folder = self.root / (run_id + '-' + uuid.uuid4().hex) folder.mkdir() nonce = uuid.uuid4().hex entries = [] dirs = {'input', 'runner', 'root'} for path, content in source['files'].items(): approved_path = (bool(re.fullmatch(r'(src/main/java/[A-Za-z0-9_/.]+\.java|docs/[A-Za-z0-9_/.]+\.md)', path)) or (path == 'website/index.html' and self.config.skill_ids in [['webapp-java-development'],['requirement-java-business']])) if not approved_path or any(p in {'', '.', '..'} for p in path.split('/')): raise SkillError('PATH_NOT_ALLOWED', '源码路径越界。') dest = Path('input') / path dirs.update(p.as_posix() for p in dest.parents if p.as_posix() != '.') entries.append((dest.as_posix(), content.encode(), stat.S_IFREG | 0o644)) entries.extend([('runner/probe.py', self.probe_file.read_bytes(), stat.S_IFREG | 0o644), ('runner/vm_worker.py', self.worker.read_bytes(), stat.S_IFREG | 0o600), ('root/job.json', canonical({'nonce': nonce, 'config': self.config.model_dump()}).encode(), stat.S_IFREG | 0o600)]) overlay = [(d, b'', stat.S_IFDIR | (0o700 if d == 'root' else 0o755)) for d in sorted(dirs, key=lambda s: (s.count('/'), s))] + entries + dependency_overlay(self.config) initrd = folder / 'job.cpio.gz' with initrd.open('wb') as f: with (self.image_root / 'rootfs.cpio.gz').open('rb') as base: shutil.copyfileobj(base, f) f.write(gzip.compress(cpio(overlay), mtime=0)) args = [self.executable, '-machine', 'q35,accel=tcg', '-cpu', 'max', '-m', '1024', '-smp', '1', '-nodefaults', '-no-user-config', '-nic', 'none', '-display', 'none', '-monitor', 'none', '-serial', 'stdio', '-no-reboot', '-kernel', str(self.image_root / 'kernel'), '-initrd', str(initrd), '-append', self.kernel_arguments] try: phase_started=time.monotonic();phase='environment';timings={} sink=getattr(self,'phase_sink',None) if sink:sink(run_id,phase,'started','收到执行请求,正在启动隔离环境;尚无编译或测试结论。') def observed(line): nonlocal phase_started,phase if line.startswith('ZKCODING_VM_STAGE='): try:next_phase=json.loads(line.split('=',1)[1])['stage'] except (ValueError,KeyError,TypeError):return if next_phase not in {'build','test','preview'}:return elif line.startswith('ZKCODING_VM_RESULT='):next_phase=None else:return elapsed=max(0,time.monotonic()-phase_started) timings[phase]=timings.get(phase,0)+elapsed if sink:sink(run_id,phase,'completed',f'控制器阶段输出已返回,耗时 {elapsed:.3f} 秒;通过与否以可信回执为准。') phase_started=time.monotonic();phase=next_phase if phase and sink:sink(run_id,phase,'started','隔离控制器报告已进入此阶段;尚未宣布通过。') for attempt in range(2 if getattr(self,'allow_boot_retry',True) else 1): if cancelled():raise SkillError('RUN_CANCELLED','验证已取消。',409) code, output = self.command(args, timeout=self.command_timeout, cancelled=cancelled, **({'on_line':observed} if isinstance(self.command,CommandRunner) else {})) (folder / f'boot-attempt-{attempt+1}.log').write_text(output[-16000:],encoding='utf-8') if cancelled():raise SkillError('RUN_CANCELLED','验证已取消。',409) known=(self.manifest_path.name in {'manifest-v8.json','manifest-v9.json'} and self.known_boot_failure(output)) if known and attempt==0 and getattr(self,'allow_boot_retry',True) and not cancelled(): event=getattr(self,'event_sink',None) if event:event(run_id,'recovering','内核尚未进入执行器;保留启动失败日志,同一源码重启独立虚拟机一次,不调用模型。') continue if known: raise SkillError('SANDBOX_BOOT_FAILED','独立虚拟机内核启动失败,未运行生成代码。',503, details={'failure_category':'kernel_boot','generated_code_started':False,'boot_attempts':attempt+1}) break finally: # Generated input/protected suite are ephemeral; retain only bounded receipts. initrd.unlink(missing_ok=True) (folder / 'boot.log').write_text(output[-16000:], encoding='utf-8') lines = [line.split('ZKCODING_VM_RESULT=', 1)[1] for line in output.splitlines() if line.startswith('ZKCODING_VM_RESULT=')] if code or len(lines) != 1: raise SkillError('VM_RESULT_UNKNOWN', '虚拟机没有返回唯一可信回执。', 503) result = json.loads(lines[0]) if result.pop('nonce', None) != nonce or result.get('error'): raise SkillError('VM_RESULT_UNKNOWN', '虚拟机回执校验失败。', 503) if result.get('build', {}).get('passed') is not True: raise SkillError('JAVA_BUILD_FAILED', 'Java 编译失败。', details={'diagnostics': result.get('build', {}).get('diagnostics', '')}) classes = result['build'].get('classes_digest', '') if not re.fullmatch(r'[a-f0-9]{64}', classes): raise SkillError('INVALID_SANDBOX_RECEIPT', '编译摘要无效。', 503) result.update(source_digest=source['source_digest'], config_digest=digest(self.config),host_phase_seconds=timings) (folder / 'receipt.json').write_text(canonical(result), encoding='utf-8') return {'source_digest': source['source_digest'], 'classes_digest': classes, 'image': self.config.image, 'suite_digest': self.suite_digest, 'config_digest': digest(self.config), 'execution_mode': 'qemu', 'container': self.name(run_id), 'stage': 'build', 'passed': True, 'receipt_ref': folder.name, 'receipt_digest': digest(result), 'isolation': {'network': 'none', 'host_mounts': [], 'memory_mb': 1024, 'vcpu': 1, 'disposable': True}} finally: SLOT.release() def probe(self, run_id, candidate, stage, cancelled): ref = candidate.get('receipt_ref', '') if (stage not in {'test', 'preview'} or not re.fullmatch(re.escape(run_id) + '-[a-f0-9]{32}', ref) or candidate.get('container') != self.name(run_id) or candidate.get('config_digest') != digest(self.config)): raise SkillError('EVIDENCE_BINDING_MISMATCH', '虚拟机证据绑定不符。', 409) if cancelled(): raise SkillError('RUN_CANCELLED', '验证已取消。', 409) result = json.loads((self.root / ref / 'receipt.json').read_text(encoding='utf-8')) if digest(result) != candidate['receipt_digest'] or result['source_digest'] != candidate['source_digest']: raise SkillError('EVIDENCE_BINDING_MISMATCH', '回执摘要不符。', 409) evidence = result[stage] if evidence['classes_digest'] != candidate['classes_digest']: raise SkillError('EVIDENCE_BINDING_MISMATCH', '编译产物变化。', 409) if evidence['passed'] is not True: raise SkillError('JAVA_TEST_FAILED' if stage == 'test' else 'JAVA_PREVIEW_FAILED', '独立 HTTP 验证失败。', details={'failures': evidence.get('failures', []),**({'startup':result['startup']} if result.get('startup') else {})}) return dict(candidate, stage=stage, cases=evidence['cases'], passed_cases=evidence['passed_cases'], preview_scope='disposable-vm-loopback-http') def cleanup(self, run_id): self.name(run_id) # Synchronous command lifecycle waits for exit or kills the VM before returning. return True