117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""libsvulkan2 二进制补丁 (design_v5 §9)。
|
|
|
|
对 venv_sim 内 sapien 包携带的 libsvulkan2.so 施加两处补丁, 使纯软件 Vulkan
|
|
(lavapipe) 环境下设备创建成功且不去噪。符号偏移由 nm -D 在执行时动态定位,
|
|
不硬编码地址; 执行前备份原文件为 libsvulkan2.so.orig; 幂等 (已打补丁则跳过)。
|
|
|
|
1. 扩展名替换: 字符串 VK_KHR_external_semaphore_fd (29 字节) 原位改写为
|
|
VK_KHR_external_memory_fd (26 字节) + \\x00 填充。
|
|
作用: 设备创建在仅软件 Vulkan 环境下通过。
|
|
2. 去噪旁路: 符号 svulkan2::renderer::DenoiserOidn 的 init / allocate / denoise
|
|
三个函数入口改写为 xor eax,eax; ret (返回 0)。
|
|
作用: 规避 OIDN 去噪器在软件 Vulkan 环境下调用空函数指针导致的段错误;
|
|
渲染结果不经去噪, 评测语义不变。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PATCH_CODE = b"\x31\xc0\xc3" # xor eax,eax ; ret
|
|
|
|
OLD_EXTENSION = b"VK_KHR_external_semaphore_fd"
|
|
NEW_EXTENSION = b"VK_KHR_external_memory_fd"
|
|
|
|
|
|
def find_denoiser_symbols(lib_path: Path) -> dict[str, int]:
|
|
"""nm -D -C 解析 DenoiserOidn 的 init/allocate/denoise 三个函数地址。"""
|
|
try:
|
|
output = subprocess.run(
|
|
["nm", "-D", "-C", str(lib_path)],
|
|
check=True,
|
|
capture_output=True,
|
|
text=True,
|
|
).stdout
|
|
except FileNotFoundError:
|
|
sys.exit("[patch] nm not found; install binutils (apt install binutils)")
|
|
symbols = {}
|
|
for line in output.splitlines():
|
|
parts = line.split(maxsplit=2)
|
|
if len(parts) != 3:
|
|
continue
|
|
addr_text, _sym_type, name = parts
|
|
if "DenoiserOidn" not in name:
|
|
continue
|
|
for func in ("init", "allocate", "denoise"):
|
|
if name.endswith(f"::{func}()"):
|
|
symbols[func] = int(addr_text, 16)
|
|
return symbols
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--lib", required=True, help="path to libsvulkan2.so")
|
|
parser.add_argument("--check", action="store_true", help="only report patch status")
|
|
args = parser.parse_args()
|
|
|
|
lib_path = Path(args.lib)
|
|
if not lib_path.is_file():
|
|
sys.exit(f"[patch] library not found: {lib_path}")
|
|
|
|
data = bytearray(lib_path.read_bytes())
|
|
changed = False
|
|
|
|
# --- 补丁 1: 扩展名替换 ---
|
|
offset = data.find(OLD_EXTENSION)
|
|
if offset >= 0:
|
|
replacement = NEW_EXTENSION + b"\x00" * (len(OLD_EXTENSION) - len(NEW_EXTENSION))
|
|
data[offset : offset + len(OLD_EXTENSION)] = replacement
|
|
changed = True
|
|
print(f"[patch] extension string replaced at file offset 0x{offset:x}")
|
|
else:
|
|
already = data.find(NEW_EXTENSION)
|
|
print(
|
|
"[patch] extension string: "
|
|
+ ("already patched" if already >= 0 else "NOT FOUND (skipped)")
|
|
)
|
|
|
|
# --- 补丁 2: DenoiserOidn 去噪旁路 ---
|
|
symbols = find_denoiser_symbols(lib_path)
|
|
if not symbols:
|
|
print("[patch] no DenoiserOidn symbols found (skipped)")
|
|
for func, addr in sorted(symbols.items()):
|
|
if addr <= 0 or addr + len(PATCH_CODE) > len(data):
|
|
print(f"[patch] DenoiserOidn::{func}: address 0x{addr:x} out of range, skipped")
|
|
continue
|
|
current = bytes(data[addr : addr + len(PATCH_CODE)])
|
|
if current == PATCH_CODE:
|
|
print(f"[patch] DenoiserOidn::{func}: already patched (0x{addr:x})")
|
|
continue
|
|
# 保存原字节到备份后写入
|
|
data[addr : addr + len(PATCH_CODE)] = PATCH_CODE
|
|
changed = True
|
|
print(f"[patch] DenoiserOidn::{func}: patched at 0x{addr:x} (was {current.hex()})")
|
|
|
|
if args.check:
|
|
print("[patch] check mode, no write")
|
|
return
|
|
|
|
if changed:
|
|
backup = lib_path.with_name(lib_path.name + ".orig")
|
|
if not backup.exists():
|
|
shutil.copy2(lib_path, backup)
|
|
print(f"[patch] backup written: {backup}")
|
|
lib_path.write_bytes(bytes(data))
|
|
print(f"[patch] written: {lib_path}")
|
|
else:
|
|
print("[patch] nothing to do (idempotent)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|