Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
157 lines
5.4 KiB
Python
157 lines
5.4 KiB
Python
"""Read-only probe for Mentor's private CBB Verify exports.
|
|
|
|
The probe deliberately stops at export discovery. It never calls
|
|
``VerifyReuseBlock`` or ``UnVerifyReuseBlock`` because those functions use a
|
|
private 32-bit MFC ``CFilename`` ABI that has not been reconstructed safely.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
from typing import Any, Mapping
|
|
|
|
|
|
KNOWN_EXPORTS = (
|
|
"IsVerifiedReuseBlock",
|
|
"GetStatusRBInContextOfCL",
|
|
"VerifyReuseBlock",
|
|
"UnVerifyReuseBlock",
|
|
"GetReusableCircuitVerificationStatus",
|
|
)
|
|
|
|
|
|
class ProbeResponseError(ValueError):
|
|
"""The helper returned a response outside the probe contract."""
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExportInfo:
|
|
name: str
|
|
found: bool
|
|
address: str
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExportProbeResult:
|
|
dll_path: str
|
|
architecture: str
|
|
exports: tuple[ExportInfo, ...]
|
|
write_enabled: bool
|
|
write_reason: str
|
|
response: Mapping[str, Any]
|
|
|
|
def export(self, name: str) -> ExportInfo:
|
|
for item in self.exports:
|
|
if item.name == name:
|
|
return item
|
|
raise KeyError(name)
|
|
|
|
|
|
def _require_mapping(value: Any, label: str) -> Mapping[str, Any]:
|
|
if not isinstance(value, Mapping):
|
|
raise ProbeResponseError(f"探测响应字段 {label} 不是对象")
|
|
return value
|
|
|
|
|
|
def parse_export_probe(raw: str) -> ExportProbeResult:
|
|
try:
|
|
payload = json.loads(raw)
|
|
except json.JSONDecodeError as error:
|
|
raise ProbeResponseError(f"探测响应不是 JSON: {error}") from error
|
|
payload = _require_mapping(payload, "root")
|
|
if payload.get("ok") is not True or payload.get("probe") != "exports":
|
|
raise ProbeResponseError(str(payload.get("error") or "导出探测失败"))
|
|
architecture = str(payload.get("architecture") or "").lower()
|
|
if architecture != "x86":
|
|
raise ProbeResponseError(f"导出探测桥架构异常: {architecture or 'empty'}")
|
|
dll_path = str(payload.get("dll_path") or "").strip()
|
|
if not dll_path:
|
|
raise ProbeResponseError("导出探测未返回 DLL 路径")
|
|
|
|
write_call = _require_mapping(payload.get("write_call"), "write_call")
|
|
enabled = write_call.get("enabled")
|
|
if enabled is not False:
|
|
raise ProbeResponseError("写接口探测桥不能启用写调用")
|
|
reason = str(write_call.get("reason") or "").strip()
|
|
if not reason:
|
|
raise ProbeResponseError("写接口探测必须说明未启用原因")
|
|
|
|
raw_exports = _require_mapping(payload.get("exports"), "exports")
|
|
exports: list[ExportInfo] = []
|
|
for name in KNOWN_EXPORTS:
|
|
item = _require_mapping(raw_exports.get(name), f"exports.{name}")
|
|
found = item.get("found")
|
|
if not isinstance(found, bool):
|
|
raise ProbeResponseError(f"导出 {name} 的 found 字段非法")
|
|
address = str(item.get("address") or "")
|
|
if found and not address:
|
|
raise ProbeResponseError(f"导出 {name} 已找到但没有地址")
|
|
exports.append(ExportInfo(name=name, found=found, address=address))
|
|
|
|
return ExportProbeResult(
|
|
dll_path=dll_path,
|
|
architecture=architecture,
|
|
exports=tuple(exports),
|
|
write_enabled=False,
|
|
write_reason=reason,
|
|
response=dict(payload),
|
|
)
|
|
|
|
|
|
def default_probe_path() -> Path:
|
|
configured = os.environ.get("CBB_VERIFY_PROBE", "").strip()
|
|
if configured:
|
|
return Path(configured).expanduser()
|
|
package_root = Path(__file__).resolve().parent
|
|
frozen_root = Path(getattr(sys, "_MEIPASS", package_root))
|
|
candidates = (
|
|
package_root / "verify_probe32.exe",
|
|
package_root / "bridge" / "VerifyProbe32.exe",
|
|
Path(sys.executable).resolve().parent / "tools" / "status_bridge" / "bin" / "VerifyProbe32.exe",
|
|
frozen_root / "tools" / "status_bridge" / "bin" / "VerifyProbe32.exe",
|
|
package_root.parent.parent / "tools" / "status_bridge" / "bin" / "VerifyProbe32.exe",
|
|
)
|
|
for candidate in candidates:
|
|
if candidate.is_file():
|
|
return candidate
|
|
return candidates[0]
|
|
|
|
|
|
def probe_exports(
|
|
executable: Path | None = None,
|
|
*,
|
|
env: Mapping[str, str] | None = None,
|
|
timeout_seconds: float = 10,
|
|
) -> ExportProbeResult:
|
|
helper = Path(executable) if executable is not None else default_probe_path()
|
|
if not helper.is_file():
|
|
raise FileNotFoundError(f"找不到 Verify 导出探测桥: {helper}")
|
|
child_env = os.environ.copy()
|
|
child_env.update(dict(env or {}))
|
|
try:
|
|
process = subprocess.run(
|
|
[str(helper), "--probe-exports"],
|
|
capture_output=True,
|
|
text=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
timeout=max(0.1, float(timeout_seconds)),
|
|
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
|
env=child_env,
|
|
check=False,
|
|
)
|
|
except subprocess.TimeoutExpired as error:
|
|
raise ProbeResponseError("导出探测桥响应超时") from error
|
|
lines = [line.strip() for line in process.stdout.splitlines() if line.strip()]
|
|
if process.returncode != 0:
|
|
detail = (process.stderr or process.stdout).strip() or f"退出码 {process.returncode}"
|
|
raise ProbeResponseError(f"导出探测桥失败: {detail}")
|
|
if not lines:
|
|
raise ProbeResponseError("导出探测桥没有返回 JSON")
|
|
return parse_export_probe(lines[0])
|