Files
ssw-mpc/build/run_qemu.py
T
admin 54781b3f83
ci / gate (push) Waiting to run
refactor: 项目改名 ssw-mpc,crate 统一到 mpc-* 前缀
- 项目与文档标题:nax-rs → ssw-mpc(尚数网-多方安全计算),二进制 nax → mpc
- 18 个 crate 及目录 nax-* → mpc-*,Rust 引用 nax_* → mpc_*
- 环境变量、C 符号、构建/部署脚本:NAX_* → MPC_*、libnax_ray.so → libmpc_ray.so
- 保留协议域分隔常量与 refs/nax 参考实现路径,维持 golden 向量与历史数据兼容
- 新增 [profile.dev] incremental=false
2026-11-05 15:06:12 +08:00

167 lines
5.5 KiB
Python

#!/usr/bin/env python3
"""P3 OP-TEE QEMU 验证:在 cube-sandbox 的 toolchain 模板里跑全链。
用法(key 经 xop 注入环境,不进 argv):
xop --secret CUBE_API_KEY=llm_key -- python3 build/run_qemu.py < /dev/null
⚠ cube-auth-gate/2.0 的兼容补丁:强制 Accept-Encoding: identity。
⚠ SDK commands.run() 默认 read=None,必须显式设 timeout。
⚠ traffic token 只在 create 返回一次,立即落盘备用。
"""
import os
import sys
import time
os.environ.setdefault("CUBE_API_URL", "http://192.168.1.30:3000")
os.environ.setdefault("CUBE_PROXY_NODE_IP", "192.168.1.30")
os.environ.setdefault("CUBE_PROXY_PORT_HTTP", "80")
import requests as _requests
_orig_req = _requests.Session.request
def _req_no_gzip(self, method, url, **kw):
headers = dict(kw.pop("headers", None) or {})
headers["Accept-Encoding"] = "identity"
return _orig_req(self, method, url, headers=headers, **kw)
_requests.Session.request = _req_no_gzip
try:
import httpx as _httpx
_orig_hreq = _httpx.Client.request
def _hreq_no_gzip(self, method, url, **kw):
headers = dict(kw.pop("headers", None) or {})
headers.setdefault("Accept-Encoding", "identity")
return _orig_hreq(self, method, url, headers=headers, **kw)
_httpx.Client.request = _hreq_no_gzip
except Exception:
pass
from cubesandbox import Sandbox
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BUILD_TIMEOUT = 3600 # 前台跑构建,最多 60 分钟
def make_src_tgz() -> bytes:
import io
import tarfile
def _filter(ti: tarfile.TarInfo):
parts = ti.name.split("/")
if "target" in parts or ".git" in parts or "__pycache__" in parts:
return None
return ti
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
for sub in ("crates/mpc-core", "crates/mpc-tee"):
tf.add(os.path.join(REPO, sub), arcname=sub, filter=_filter)
tf.add(
os.path.join(REPO, "build", "p3_optee_qemu.sh"),
arcname="p3_optee_qemu.sh",
)
return buf.getvalue()
def run_cmd(sb, cmd, timeout=30):
"""带超时的命令执行,打印 stderr 便于调试。"""
r = sb.commands.run(cmd, timeout=timeout)
if r.stdout.strip():
print(r.stdout.rstrip(), flush=True)
if r.stderr.strip():
print(f" [stderr] {r.stderr.rstrip()[:200]}", flush=True)
return r
def main():
print("== 创建 toolchain 沙箱 (16C/16G, 2h) ==", flush=True)
sb = Sandbox.create(
template="toolchain", timeout=7200, network={"allow_public_traffic": False}
)
print(f" sandbox_id = {sb.sandbox_id}", flush=True)
token = sb._data.get("trafficAccessToken", "")
token_file = f"/tmp/sb_token_{sb.sandbox_id}.txt"
with open(token_file, "w") as f:
f.write(token)
print(f" token → {token_file}", flush=True)
try:
# 上传
print("== 上传源码 ==", flush=True)
tgz = make_src_tgz()
print(f" src.tgz = {len(tgz) / 1e6:.2f} MB", flush=True)
run_cmd(sb, "mkdir -p /root/src")
sb.files.write("/root/src/src.tgz", tgz)
run_cmd(sb, "tar xzf /root/src/src.tgz -C /root/src && ls /root/src")
# 前台跑构建(不 nohup),输出直接打出来
print("== 构建开始(前台,最多 60 分钟)==", flush=True)
t0 = time.time()
try:
r = sb.commands.run(
"cd /root && SRC=/root/src/crates bash /root/src/p3_optee_qemu.sh; echo EXIT_CODE=$?",
timeout=BUILD_TIMEOUT,
)
print(r.stdout[-3000:] if len(r.stdout) > 3000 else r.stdout, flush=True)
if r.stderr.strip():
print(f"[stderr] {r.stderr[-1000:]}", flush=True)
except Exception as e:
elapsed = int(time.time() - t0)
print(f"\n⚠ 构建异常 ({elapsed}s): {e}", flush=True)
# 尝试取日志
try:
r2 = sb.commands.run("tail -30 /root/build.log 2>/dev/null", timeout=15)
print("build.log 尾部:", flush=True)
print(r2.stdout, flush=True)
except Exception:
pass
elapsed = int(time.time() - t0)
print(f"\n== 构建耗时 {elapsed}s ==", flush=True)
# 检查判据
try:
r = sb.commands.run("tail -40 /root/run/nw.log 2>/dev/null", timeout=15)
if r.stdout.strip():
print("正常世界日志:", flush=True)
print(r.stdout, flush=True)
except Exception:
pass
# 判据:ALL OK 在 nw.log
try:
r = sb.commands.run("grep -c 'ALL OK' /root/run/nw.log 2>/dev/null", timeout=10)
count = r.stdout.strip()
if count and int(count) > 0:
print("\n✅ P3 QEMU 验证通过!(ALL OK)", flush=True)
try:
sb.kill()
except Exception:
pass
try:
os.unlink(token_file)
except Exception:
pass
return 0
except Exception:
pass
print("\n❌ P3 QEMU 验证失败", flush=True)
print(f" 沙箱保留: {sb.sandbox_id}", flush=True)
print(f" token: {token_file}", flush=True)
return 1
finally:
pass # 失败时保留沙箱供调试,成功时已在上方销毁
if __name__ == "__main__":
sys.exit(main())