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

76 lines
3.3 KiB
Python

"""probe_csurface.py <pid>
Walk s_Resources, find CSurface entries (vfptr=0x007ca4dc — GR-view).
Split live vs lost, sample buffer/field non-NULL counts.
CSurface members (primary-relative):
+0x58 type, +0x5c handler, +0x60 color, +0x64 solid_index, +0x68 indexed_tex_id
+0x6c base1map (ImgTex*) => entry+0x3c (PurgeResource releases this)
+0x70 base1pal (Palette*) => entry+0x40 (Destroy releases — PurgeResource MISSES)
+0x74 translucency, +0x78 luminosity, +0x7c diffuse
+0x80 orig_texture_id, +0x84 orig_palette_id, +0x88 orig_lum, +0x8c orig_diff
"""
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
CSURFACE_GR_VTABLE = 0x007ca4dc
def rd_u32(h, va):
buf = (ctypes.c_ubyte * 4)(); sz = ctypes.c_size_t(0)
if not k.ReadProcessMemory(h, va, buf, 4, ctypes.byref(sz)) or sz.value != 4: return None
return struct.unpack('<I', bytes(buf))[0]
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; live = 0
# Per-field non-NULL counters
lost_base1map = lost_base1pal = 0
live_base1map = live_base1pal = 0
samples = []
for i in range(min(m_num, 200000)):
entry = rd_u32(h, m_data + i * 4)
if not entry: continue
vt = rd_u32(h, entry)
if vt != CSURFACE_GR_VTABLE: continue
total += 1
bIsLost_word = rd_u32(h, entry + 8)
is_lost = (bIsLost_word & 0xFF) != 0
base1map = rd_u32(h, entry + 0x3c) # primary+0x6c
base1pal = rd_u32(h, entry + 0x40) # primary+0x70
handler = rd_u32(h, entry + 0x2c) # primary+0x5c
if base1map is None or base1pal is None: continue
if is_lost:
lost += 1
if base1map != 0: lost_base1map += 1
if base1pal != 0: lost_base1pal += 1
if (base1map != 0 or base1pal != 0) and len(samples) < 5:
samples.append(('LOST', entry, base1map, base1pal, handler))
else:
live += 1
if base1map != 0: live_base1map += 1
if base1pal != 0: live_base1pal += 1
if (base1map != 0 or base1pal != 0) and len(samples) < 10:
samples.append(('LIVE', entry, base1map, base1pal, handler))
print(f"CSurface entries: total={total} live={live} lost={lost}")
print(f" LOST: base1map non-NULL {lost_base1map}/{lost} ({100*lost_base1map//max(lost,1)}%)")
print(f" LOST: base1pal non-NULL {lost_base1pal}/{lost} ({100*lost_base1pal//max(lost,1)}%)")
print(f" LIVE: base1map non-NULL {live_base1map}/{live} ({100*live_base1map//max(live,1)}%)")
print(f" LIVE: base1pal non-NULL {live_base1pal}/{live} ({100*live_base1pal//max(live,1)}%)")
print(f" Samples:")
for status, entry, m, p, h_ in samples:
print(f" [{status}] entry=0x{entry:08x} base1map=0x{m:08x} base1pal=0x{p:08x} handler=0x{h_:08x}")