XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
"""Source-bound host parser checks only; never access PCI or launch QEMU."""
import hashlib
import json
from pathlib import Path
import re
import shutil
import subprocess
import sys
import time

ROOT = Path(__file__).resolve().parents[2]
INPUTS = ('Source/XFESystem.NativeKernel/GpuHardwareBridge.cs',
          'Source/XFESystem.NativeKernel/Contracts.cs', 'Source/XFESystem.NativeKernel/Kernel.cs',
          'Tools/IndependentKernel/GpuDiscoveryChecks/GpuDiscoveryChecks.csproj',
          'Tools/IndependentKernel/GpuDiscoveryChecks/Program.cs',
          'Tools/IndependentKernel/GpuDiscoveryChecks/HardwareNotInvoked.cs',
          'Tools/IndependentKernel/GpuDiscoveryChecks/Readme.md',
          'Tools/IndependentKernel/GpuDiscoveryEvidence.py',
          'Tools/IndependentKernel/TestGpuDiscoveryEvidence.py',
          'Tools/IndependentKernel/TestGpuHardwareBridge.py',
          'Tools/IndependentKernel/GpuDiscoveryChecks/ProbeGeometryChecks.cs',
          'Source/XFESystem.NativeKernel/NvidiaBootProbe.cs')


def source_hashes():
    return {name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest() for name in INPUTS}


def main():
    if shutil.disk_usage(ROOT).free < 64 * 1024 * 1024:
        raise RuntimeError('Insufficient space for the small isolated host proof')
    directory = ROOT / 'build/IndependentKernel' / ('GpuDiscoveryHost-' + str(time.time_ns()))
    directory.mkdir(parents=True)
    result = {'Kind': 'XfeIndependentGpuDiscoveryHostChecks', 'Passed': False,
              'HostOnly': True, 'GuestExecutionVerified': False, 'GpuExecutionVerified': False,
              'CudaExecutionVerified': False, 'HardwareAccessed': False, 'SourceSha256': source_hashes(),
              'ProofDirectory': str(directory), 'Checks': []}
    try:
        commands = [('ProductionSnapshotParser', ['dotnet', 'run', '--project',
                     str(ROOT / INPUTS[3]), '-c', 'Release', '--artifacts-path', str(directory / 'Artifacts'), '--no-launch-profile']),
                    ('SerialVerifierControls', [sys.executable, str(ROOT / INPUTS[8])])]
        for name, arguments in commands:
            completed = subprocess.run(arguments, cwd=ROOT, capture_output=True, text=True,
                                       encoding='utf-8', errors='replace', timeout=120)
            text = completed.stdout + completed.stderr
            log = directory / (name + '.log'); log.write_text(text, encoding='utf-8')
            count = (re.search(r'GPU_DISCOVERY_HOST_RESULT\|Passed\|(\d+)\|Failed\|0\|HostOnly\|1\|GpuExecution\|0\|Cuda\|0', text)
                     if name == 'ProductionSnapshotParser' else re.search(r'Ran (\d+) tests.*\bOK\b', text, re.S))
            entry = {'Name': name, 'ExitCode': completed.returncode, 'Passed': completed.returncode == 0 and count is not None,
                     'PassedCount': int(count[1]) if count else 0, 'Log': str(log),
                     'LogSha256': hashlib.sha256(log.read_bytes()).hexdigest(), 'Arguments': arguments}
            result['Checks'].append(entry)
            if not entry['Passed']: raise AssertionError(name + ' failed; inspect ' + str(log))
        result['SourceUnchanged'] = result['SourceSha256'] == source_hashes()
        if not result['SourceUnchanged']: raise AssertionError('Parser/check inputs changed during host verification')
        result['PassedCount'] = sum(item['PassedCount'] for item in result['Checks'])
        result['Passed'] = True
    except Exception as error:
        result['Error'] = str(error)
        raise
    finally:
        receipt = directory / 'Result.json'; receipt.write_text(json.dumps(result, indent=2), encoding='utf-8')
        print(str(receipt))


if __name__ == '__main__':
    main()