leakhunt/tools/probe_rsd3d_lost.py
acbot 57b5e43d0e Initial commit — leak-hunt project complete
Five bugs identified and patched in retail Asheron's Call client:
- v3b: palette refcount over-increment (3-byte NOP at two sites)
- v5: RenderSurface PurgeResource no-op stub (vtable slot 2 thunk)
- v11: two dangling-pointer crash guards (NULL-check + reorder)
- v14: CEnvCell::Destroy ClipPlaneList leak (18-byte JMP to cleanup thunk)
- v22: unpacker stale-pointer SEH guard (whole-function __try/__except)

All five ship in leakfix.dll (117 KB, SHA d282f23c…) which is loaded
by acclient.exe at process start via PE import table patching by
tools/install_leakfix.py.

Controlled 15-client fleet soak: unpatched control died at 26h with
palette exhaustion; all 14 patched clients survived past that point
and reached ≥5-day uptime.

Residual ~15 MB/h growth traced to d3d9.dll's internal slab allocator
(260KB surface backing buffers retained after Release). See REPORT.md
§10 for the full investigation; conclusion is that it's unfixable from
outside d3d9.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 21:07:58 +02:00

71 lines
2.9 KiB
Python

"""probe_rsd3d_lost.py <pid>
Walk s_Resources, find lost (m_bIsLost=1) RenderSurfaceD3D entries (vfptr=0x00801a94),
sample 5 of them, print m_pSurfaceBits state.
v20-active client: m_pSurfaceBits should be NULL on lost entries (RS::Destroy ran)
v20-absent client: m_pSurfaceBits often non-NULL on lost entries (PurgeResource only)
m_pSurfaceBits is at primary+0x114. Entry IS GR-view (= primary + 0x30).
So accessed at entry + (0x114 - 0x30) = entry + 0xE4."""
import ctypes, ctypes.wintypes as wt, sys, struct
PROCESS_VM_READ = 0x10
PROCESS_QUERY_INFORMATION = 0x400
k = ctypes.windll.kernel32
k.OpenProcess.argtypes = [wt.DWORD, wt.BOOL, wt.DWORD]; k.OpenProcess.restype = wt.HANDLE
k.ReadProcessMemory.argtypes = [wt.HANDLE, wt.LPCVOID, wt.LPVOID, ctypes.c_size_t, ctypes.POINTER(ctypes.c_size_t)]
k.ReadProcessMemory.restype = wt.BOOL
S_RESOURCES_M_DATA = 0x008398C4
S_RESOURCES_M_NUM = 0x008398CC
RSD3D_VTABLE = 0x00801a94
def rd(h, va, n):
buf = (ctypes.c_ubyte * n)(); sz = ctypes.c_size_t(0)
if not k.ReadProcessMemory(h, va, buf, n, ctypes.byref(sz)): return None
return bytes(buf[:sz.value])
def rd_u32(h, va):
b = rd(h, va, 4); return struct.unpack('<I', b)[0] if b else None
pid = int(sys.argv[1])
h = k.OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, False, pid)
if not h: print(f"OpenProcess err={ctypes.get_last_error()}"); sys.exit(2)
m_data = rd_u32(h, S_RESOURCES_M_DATA)
m_num = rd_u32(h, S_RESOURCES_M_NUM)
print(f"pid {pid}: s_Resources m_data=0x{m_data:08x} m_num={m_num}")
total = 0; lost = 0; sampled = 0
sb_null = 0; sb_nonnull = 0; nonnull_samples = []
for i in range(min(m_num, 100000)):
entry = rd_u32(h, m_data + i * 4)
if not entry: continue
vt = rd_u32(h, entry)
if vt != RSD3D_VTABLE: continue
total += 1
bIsLost_word = rd_u32(h, entry + 8)
if (bIsLost_word & 0xFF) == 0: continue
lost += 1
# entry = GR-view; primary = entry - 0x30
# Two buffers RS::Destroy frees: primary+0x64 and primary+0x114
# From GR-view: +0x34 and +0xE4
buf1 = rd_u32(h, entry + 0x34) # primary+0x64
buf2 = rd_u32(h, entry + 0xE4) # primary+0x114 (m_pSurfaceBits)
# m_pD3DSurface at primary+0x120 = entry+0xF0
d3d = rd_u32(h, entry + 0xF0)
if buf1 is None or buf2 is None: continue
if buf1 == 0 and buf2 == 0:
sb_null += 1
else:
sb_nonnull += 1
if len(nonnull_samples) < 5:
nonnull_samples.append((entry, buf1, buf2, d3d))
sampled += 1
print(f"RSD3D entries: total={total} lost={lost} sampled-lost={sampled}")
print(f" both buf1+buf2 NULL (Destroy was no-op or ran): {sb_null} ({100*sb_null//max(sampled,1)}%)")
print(f" at least one buf NON-NULL (would benefit from v20): {sb_nonnull} ({100*sb_nonnull//max(sampled,1)}%)")
for entry, b1, b2, d3d in nonnull_samples:
print(f" entry=0x{entry:08x} buf1=0x{b1:08x} buf2=0x{b2:08x} m_pD3DSurface=0x{d3d:08x}")