leakhunt/tools/probe_rtd3d_total.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

98 lines
3.3 KiB
Python

"""probe_rtd3d_total.py <pid>
Full memory scan for RTD3D GR-view vtable 0x00801A18. Compare to s_Resources
count to detect orphan shells (instances still in memory but not in s_Resources).
Run as Admin (PROCESS_VM_READ on AC client).
"""
import ctypes, ctypes.wintypes as wt, sys, struct
PROCESS_VM_READ = 0x10
PROCESS_QUERY_INFORMATION = 0x400
MEM_COMMIT = 0x1000
MEM_PRIVATE = 0x20000
PAGE_READWRITE = 0x4
PAGE_EXECUTE_READWRITE = 0x40
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
class MBI(ctypes.Structure):
_fields_ = [("BaseAddress", ctypes.c_void_p),
("AllocationBase", ctypes.c_void_p),
("AllocationProtect", wt.DWORD),
("RegionSize", ctypes.c_size_t),
("State", wt.DWORD),
("Protect", wt.DWORD),
("Type", wt.DWORD)]
k.VirtualQueryEx.argtypes = [wt.HANDLE, ctypes.c_void_p, ctypes.POINTER(MBI), ctypes.c_size_t]
k.VirtualQueryEx.restype = ctypes.c_size_t
VTABLES = {
'RTD3D_GR': 0x00801A18,
'RTD3D_DBObj': 0x00801AA8,
'RSD3D_GR': 0x00801A94,
}
S_RESOURCES_M_DATA = 0x008398C4
S_RESOURCES_M_NUM = 0x008398CC
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)
# In-s_Resources count via single read
sres_rtd3d = 0
sres_rsd3d = 0
buf = rd(h, m_data, m_num*4)
if buf:
arr = struct.unpack(f"<{m_num}I", buf)
for e in arr:
if not e: continue
vt = rd_u32(h, e)
if vt == VTABLES['RTD3D_GR']: sres_rtd3d += 1
elif vt == VTABLES['RSD3D_GR']: sres_rsd3d += 1
counts = {name: 0 for name in VTABLES}
addr = 0
regions = 0
mbi = MBI()
while k.VirtualQueryEx(h, addr, ctypes.byref(mbi), ctypes.sizeof(mbi)):
base = mbi.BaseAddress or 0
size = mbi.RegionSize
if (mbi.State == MEM_COMMIT and mbi.Type == MEM_PRIVATE and
(mbi.Protect & 0xFF) in (PAGE_READWRITE, PAGE_EXECUTE_READWRITE)):
regions += 1
# Read in chunks of up to 4 MB
off = 0
while off < size:
chunk = min(4*1024*1024, size - off)
b = rd(h, base + off, chunk)
if b:
n_words = len(b) // 4
arr = struct.unpack(f"<{n_words}I", b[:n_words*4])
for v in arr:
for name, vt in VTABLES.items():
if v == vt:
counts[name] += 1
break
off += chunk
addr = base + size
if addr >= 0x80000000: break
if addr <= (mbi.BaseAddress or 0): break
print(f"pid {pid}: regions={regions}")
print(f" s_Resources: RTD3D={sres_rtd3d} RSD3D={sres_rsd3d}")
for name, cnt in counts.items():
print(f" vtable {name} ({VTABLES[name]:08x}) total in-process: {cnt}")