From f06117964692814ebb87fc7a2fa25b59152250df Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sun, 1 Nov 2026 23:03:11 -0600 Subject: [PATCH] refactor(region): one region destruction signal; inline icons drop the vtable guess Frame::RegionDestruction co-hooks SimpleRegionDestruct (0x77F6F0), the destructor body every region ends in, and runs its listeners first. It has exactly three callers: the base region's deleting destructor (0x77F6C6), ~CSimpleTexture (0x76FD40, a tail JMP; its only caller is the deleting destructor 0x76FD10) and ~CSimpleFontString (0x770E50, a tail JMP; called by its deleting destructor 0x770E20 and by a subclass's, 0x789320). The texture and font string vtables (0x81C718, 0x81C778) are written only by their constructor and destructor, so no inlined destructor skips the hook. It replaces FontString::Destruction, the font-string-only hook from the previous commit; the duration text binding now listens here. InlineTexturePool kept icon state keyed by font string pointer and judged a key alive by its first word matching a vtable it had seen painted. That guess had a hole: a destroyed font string's block goes back to the region pool and can come back as another font string, vtable and all, and the dead key's icons then served the new one. It also read possibly-freed memory on every tick. Now a destroyed key takes its entry with it at once, so the address starts clean; its icon regions live on with their parent frame and are hidden on the next tick, never inside the destructor that reported the death. A destroyed icon region is dropped wherever it is held, and a live key that loses one is placed again. The learned-vtable sets, IsLiveFontString and IsLiveRegion are gone. The SEH latch stays, for the engine region calls the tick makes from paint-time data. --- src/Offsets.h | 19 +- src/duration/TextBinding.cpp | 12 +- .../RegionDestruction.cpp} | 20 +- .../RegionDestruction.h} | 19 +- src/text/InlineTexturePool.cpp | 182 +++++++----------- 5 files changed, 114 insertions(+), 138 deletions(-) rename src/{fontstring/Destruction.cpp => frame/RegionDestruction.cpp} (68%) rename src/{fontstring/Destruction.h => frame/RegionDestruction.h} (51%) diff --git a/src/Offsets.h b/src/Offsets.h index fcfab279..2bb9184d 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -612,12 +612,19 @@ enum Offsets { VAR_SIMPLEFONTSTRING_CLASS_TAG = 0x00846544, // ".?AVCSimpleFontString@@" (alloc debug tag) FUN_REGION_POOL_ALLOC = 0x00760450, // __thiscall(pool, zeroInit=0, tag, line=-2) -> raw mem FUN_SIMPLEFONTSTRING_CTOR = 0x00770D30, // __thiscall(mem, parent, layer, sublayer) -> fs - // CSimpleFontString::~CSimpleFontString — `void __thiscall(fs)`, plain RET: - // frees the text, drops the font, ends in the region destructor. Its - // deleting destructor (0x00770E20, which then returns the block to the - // region pool) calls it. Decompiled; benilla-re ui/fonttext. - // `FontString::Destruction` hooks it. - FUN_FONTSTRING_DESTRUCTOR = 0x00770E50, + // SimpleRegionDestruct, CSimpleRegion's destructor body — `void + // __thiscall(region)`, plain RET: unparents the region, frees its two + // arrays, then the script region destructor. Every region's destruction + // reaches it, and it has exactly three callers: the base region's deleting + // destructor (0x0077F6C6), ~CSimpleTexture (0x0076FD40, tail JMP; called + // only by its deleting destructor 0x0076FD10) and ~CSimpleFontString + // (0x00770E50, tail JMP; called by its deleting destructor 0x00770E20 and + // a subclass's, 0x00789320). The texture and font string vtables are + // written only by their constructor and destructor, so no inlined + // destructor skips it. The blocks then go back to the region pool, where + // they can come back as new regions. Xrefs + decompiles; benilla-re + // ui/regions. `Frame::RegionDestruction` hooks it. + FUN_REGION_DESTRUCT = 0x0077F6F0, FUN_REGION_SET_NAME = 0x0076C650, // __thiscall(region, name) — registers _G[name] FUN_FONTSTRING_SET_FONT = 0x00770C60, // __thiscall(fs + OFF_FONTSTRING_FONT_HOLDER, fontObject) // CSimpleFontString::SetFont — `uint32_t __thiscall(fs, const char *path, diff --git a/src/duration/TextBinding.cpp b/src/duration/TextBinding.cpp index bec63476..98680c5f 100644 --- a/src/duration/TextBinding.cpp +++ b/src/duration/TextBinding.cpp @@ -42,14 +42,14 @@ // before its OnUpdate pass and draw, so the text shows in the same frame as a // timer bar's fill from that pass; at the tail it would show a frame late. // They set the text only when it differs from the font string's own. Destroyed font strings are -// dropped through FontString::Destruction. Not ported: the color curves +// dropped through Frame::RegionDestruction. Not ported: the color curves // (SetTextColorCurve, GetTextColorCurve, ClearTextColorCurve, // GetFormattedTextColor). #include "Game.h" #include "Offsets.h" #include "duration/Object.h" -#include "fontstring/Destruction.h" +#include "frame/RegionDestruction.h" #include "text/NumericFormatter.h" #include "tick/FrameTick.h" #include "time/Clock.h" @@ -93,7 +93,7 @@ struct Binding { double lastUpdate = -INFINITY; }; -// Every live binding, for FontString::Destruction; and the enabled ones, +// Every live binding, for Frame::RegionDestruction; and the enabled ones, // each holding a reference. std::vector g_all; std::vector g_active; @@ -259,13 +259,13 @@ void OnTick() { const Tick::FrameTick::AutoSubscribeBefore _tickSub{&OnTick}; -void OnFontStringDestroyed(void *fontString) { +void OnRegionDestroyed(void *region) { for (Binding *b : g_all) - if (b->fontString == fontString) + if (b->fontString == region) b->fontString = nullptr; } -const FontString::Destruction::AutoSubscribe _destructionSub{&OnFontStringDestroyed}; +const Frame::RegionDestruction::AutoSubscribe _destructionSub{&OnRegionDestroyed}; // ---- the Lua object ------------------------------------------------------------- diff --git a/src/fontstring/Destruction.cpp b/src/frame/RegionDestruction.cpp similarity index 68% rename from src/fontstring/Destruction.cpp rename to src/frame/RegionDestruction.cpp index 9fd3d0bf..9d74cd42 100644 --- a/src/fontstring/Destruction.cpp +++ b/src/frame/RegionDestruction.cpp @@ -11,31 +11,31 @@ // You should have received a copy of the GNU General Public License along with // ClassicAPI. If not, see . -#include "fontstring/Destruction.h" +#include "frame/RegionDestruction.h" #include "Game.h" #include "Offsets.h" -namespace FontString::Destruction { +namespace Frame::RegionDestruction { namespace { AutoSubscribe *g_head = nullptr; -using Destructor_t = void(__fastcall *)(void *fontString); -Destructor_t g_origDestructor = nullptr; +using Destruct_t = void(__fastcall *)(void *region); +Destruct_t g_origDestruct = nullptr; -void __fastcall Destructor_h(void *fontString) { +void __fastcall Destruct_h(void *region) { for (const AutoSubscribe *s = g_head; s != nullptr; s = s->next) - s->fn(fontString); - g_origDestructor(fontString); + s->fn(region); + g_origDestruct(region); } -const Game::HookAutoRegister _hook{Offsets::FUN_FONTSTRING_DESTRUCTOR, reinterpret_cast(&Destructor_h), - reinterpret_cast(&g_origDestructor)}; +const Game::HookAutoRegister _hook{Offsets::FUN_REGION_DESTRUCT, reinterpret_cast(&Destruct_h), + reinterpret_cast(&g_origDestruct)}; } // namespace AutoSubscribe::AutoSubscribe(Listener fn) : fn(fn), next(g_head) { g_head = this; } -} // namespace FontString::Destruction +} // namespace Frame::RegionDestruction diff --git a/src/fontstring/Destruction.h b/src/frame/RegionDestruction.h similarity index 51% rename from src/fontstring/Destruction.h rename to src/frame/RegionDestruction.h index 0281c7fa..8dd81a62 100644 --- a/src/fontstring/Destruction.h +++ b/src/frame/RegionDestruction.h @@ -13,13 +13,18 @@ #pragma once -// Listeners run when a font string is destroyed, before its destructor body -// (CSimpleFontString::~CSimpleFontString). The block then goes back to the -// region pool and can come back as another font string, so a module that keeps -// a font string pointer must drop it here. Same pattern as Frame::Destruction. -namespace FontString::Destruction { +// Listeners run when a region (a texture or a font string) is destroyed, from +// the region destructor body every region type ends in (SimpleRegionDestruct). +// By then the subclass part is torn down, so a listener may only use the +// pointer as a key. The block then goes back to the region pool and can come +// back as another region, so a module that keeps a region pointer must drop it +// here. Usually this runs inside a frame's destructor (regions die with their +// frame), so a listener must not change other regions or frames either: drop +// the pointer, and defer any work to a tick. Same pattern as +// Frame::Destruction. +namespace Frame::RegionDestruction { -using Listener = void (*)(void *fontString); +using Listener = void (*)(void *region); struct AutoSubscribe { explicit AutoSubscribe(Listener fn); @@ -27,4 +32,4 @@ struct AutoSubscribe { AutoSubscribe *next; }; -} // namespace FontString::Destruction +} // namespace Frame::RegionDestruction diff --git a/src/text/InlineTexturePool.cpp b/src/text/InlineTexturePool.cpp index c23c0e0c..d5a6d164 100644 --- a/src/text/InlineTexturePool.cpp +++ b/src/text/InlineTexturePool.cpp @@ -25,6 +25,7 @@ #include "Game.h" #include "Offsets.h" #include "frame/Region.h" +#include "frame/RegionDestruction.h" #include "text/PtrProbe.h" #include "tick/FrameTick.h" @@ -48,68 +49,6 @@ using ShowHide_t = void(__fastcall *)(void *tex); // LAA-aware pointer sanity probe: Text::LooksReadable (text/PtrProbe.h). -// Vtables seen at object+0x00 of REAL, live fontstrings — learned at paint time -// (see LearnFontStringVtable / QueuePlacements). A fontstring is polymorphic, so -// its vtable is a fixed .rdata pointer per class. We LEARN rather than hardcode -// because chat (ScrollingMessageFrame's internal lines), tooltip, and addon -// fontstrings are different classes with different vtables — a single hardcoded -// value filtered live chat icons out entirely. -std::unordered_set g_fsVtables; - -// A vtable pointer that lands in the WoW.exe image (.rdata) — engine C++ classes -// (fontstrings, our CSimpleTexture regions) carry their vtable there. Gates what -// the learned-vtable sets admit so a stray value never poisons them. -bool InWowImage(uintptr_t vt) { return vt >= 0x00400000u && vt < 0x00D2B000u; } - -// Record a live fontstring's vtable. Called only from QueuePlacements, which the -// paint hook invokes with a fontstring the engine is actively drawing — so -// *(void**)fs is a valid vtable here. The module-range gate is belt-and-braces -// against ever poisoning the set with a stray value. -void LearnFontStringVtable(const void *fs) { - if (!LooksReadable(fs)) - return; - const uintptr_t vt = *reinterpret_cast(fs); - if (InWowImage(vt)) - g_fsVtables.insert(vt); -} - -// True only for a LIVE fontstring: its object+0x00 vtable matches one learned -// from a genuinely-painted fontstring. A bare VA-range check is not enough — a -// freed fontstring whose heap memory is reused (as a string buffer, …) still -// passes it, and reading a garbage "parent" (fs+0x9C) from such a stale key -// crashed the region attach (FUN_0076a750 dereferencing *(parent+0x1B4), parent -// = ASCII "-Rac" string bytes). Reused memory never carries a fontstring vtable. -// The vtable read is safe: the range check bounds the pointer, and these object -// pages stay committed (we already read fs+0xC8 on the same pointer). -bool IsLiveFontString(const void *fs) { - if (!LooksReadable(fs)) - return false; - return g_fsVtables.count(*reinterpret_cast(fs)) != 0; -} - -// Same technique, for our pooled CSimpleTexture regions. Learned from -// CreateRegion's freshly-ctor'd object (the ctor writes the vtable). A region is -// parented to the fontstring's PARENT FRAME, so it outlives the fontstring — EXCEPT -// when that parent frame itself dies, which frees the fontstring AND its child -// regions together. The eviction path runs with a non-live fontstring, so it can't -// assume the regions survived; this gate keeps its Hide off a region whose memory -// was freed and recycled (a bare pointer there would fault). -std::unordered_set g_regionVtables; - -void LearnRegionVtable(const void *tex) { - if (!LooksReadable(tex)) - return; - const uintptr_t vt = *reinterpret_cast(tex); - if (InWowImage(vt)) - g_regionVtables.insert(vt); -} - -bool IsLiveRegion(const void *tex) { - if (!LooksReadable(tex)) - return false; - return g_regionVtables.count(*reinterpret_cast(tex)) != 0; -} - using SetTexCoord_t = void(__thiscall *)(void *tex, const float *coords4); using SetParentAndLayer_t = void(__thiscall *)(void *region, void *parentFrame, int layer, int show); @@ -150,6 +89,59 @@ struct FsPlacements { std::unordered_map g_fsIcons; uint32_t g_maintainTick = 0; +// Every pooled icon region still alive, so OnRegionDestroyed can tell ours +// from the rest in one lookup. +std::unordered_set g_iconRegions; + +// The regions of keys destroyed since the last tick. Their shown regions are +// hidden there, never from inside the destructor that reported the death. +std::vector> g_orphans; + +// Drops `tex` from `regions`. For a live key's list, the regions after it now +// serve the wrong placements, so the key is re-placed. +void DropRegion(std::vector ®ions, void *tex, FsPlacements *np) { + for (size_t i = 0; i < regions.size(); ++i) { + if (regions[i].tex != tex) + continue; + regions.erase(regions.begin() + static_cast(i)); + if (np != nullptr) { + if (static_cast(i) < np->shown) + --np->shown; + np->dirty = true; + } + return; + } +} + +// Frame::RegionDestruction. A destroyed key fontstring takes its entry with it +// at once: its block goes back to the region pool and can come back as an +// unrelated fontstring at the same address, which must start with no entry. +// Its regions live on, parented to its parent frame (unless that frame is the +// one dying, when they are reported here too), and are hidden at the next +// tick. A destroyed icon region is dropped wherever it is held. This runs +// inside a frame's destructor, so it changes no region itself. +// +// ACCEPTED LEAK: an orphan's regions are hidden, not reclaimed. They stay on +// their parent frame until it dies (/reload frees all of them). Chat and +// tooltip fontstrings are a reused, never-destroyed set, so only destroyed +// transient |T fontstrings leave regions behind. +void OnRegionDestroyed(void *region) { + const auto it = g_fsIcons.find(region); + if (it != g_fsIcons.end()) { + g_orphans.push_back(std::move(it->second.regions)); + g_fsIcons.erase(it); + return; + } + if (g_iconRegions.erase(region) == 0) + return; + for (auto &entry : g_fsIcons) + DropRegion(entry.second.regions, region, &entry.second); + for (std::vector &orphan : g_orphans) + DropRegion(orphan, region, nullptr); +} + +const Frame::RegionDestruction::AutoSubscribe _regionDestructionSub{&OnRegionDestroyed}; + // Latched off by Maintain's SEH guard on a fault; re-armed on /reload (see // PrepareForReload). Mirrors InlineTexture.cpp's g_inlineEnabled paint latch. bool g_maintainDisabled = false; @@ -218,7 +210,7 @@ void *CreateRegion(void *parent) { void *tex = Frame::Region::CreateTexture(parent, Offsets::DRAWLAYER_ARTWORK, 1); if (tex == nullptr) return nullptr; - LearnRegionVtable(tex); // for the eviction-path liveness gate + g_iconRegions.insert(tex); return tex; } @@ -320,39 +312,17 @@ void ApplyDirtyBranch(void *fs, FsPlacements &np, uint32_t fsColor) { // SEH frame (MSVC won't allow __try in a function that needs object unwinding). void MaintainImpl() { ++g_maintainTick; + // Hide the regions of keys destroyed since the last tick (see + // OnRegionDestroyed). Every region still listed is alive. + for (const std::vector &orphan : g_orphans) + for (const IconRegion &r : orphan) + Hide(r.tex); + g_orphans.clear(); + for (auto it = g_fsIcons.begin(); it != g_fsIcons.end();) { void *fs = it->first; FsPlacements &np = it->second; - // Stale-key eviction (the crash fix). A destroyed fontstring leaves a - // dangling key here; its heap memory may be reused as unrelated data, so - // a VA-range check is not enough — verify the live-fontstring vtable. If - // it is gone, hide our regions and erase the entry. This also stops dead - // keys from accumulating: only live fontstrings survive, and the - // chat/tooltip pools reuse a bounded set of them. - if (!IsLiveFontString(fs)) { - // The regions usually outlive the fontstring (they're parented to its - // parent FRAME, which persists while lines churn). But if the fontstring - // died because that parent frame died, the regions died with it — so - // gate each Hide on the region's OWN vtable liveness, else Hide would - // write to freed/recycled region memory. A dead region needs no hiding - // (it's already gone); a live one still gets hidden so it can't ghost. - for (int i = 0; i < np.shown && i < static_cast(np.regions.size()); ++i) - if (IsLiveRegion(np.regions[static_cast(i)].tex)) - Hide(np.regions[static_cast(i)].tex); - // ACCEPTED LEAK: erasing the entry drops np.regions without freeing - // the pooled CSimpleTextures — they stay (hidden) on their parent - // frame until it dies (/reload frees all of them). We do NOT reclaim - // them, on purpose: any reclaim (a free list, or an explicit destroy) - // would reuse/free a region pointer whose parent frame may already be - // gone — a use-after-free on the exact path this liveness check was - // added to stop crashing. The leak is bounded and near-zero in - // practice: chat/tooltip fontstrings are a reused, never-evicted set, - // so only genuinely-destroyed transient |T fontstrings ever land here. - it = g_fsIcons.erase(it); - continue; - } - // Freshness expiry: a LIVE painted icon line re-queues every paint, so // its touch stamp stays current. A parked/orphaned fs (recycled chat // line whose node never rebuilds and may not even be walked) stops @@ -372,8 +342,8 @@ void MaintainImpl() { // Mirror the fontstring's own visibility EVERY tick, not just on dirty: // expired/hidden chat lines get their fs Hidden by the message frame, // and our regions are parented to the CHAT FRAME (regions can't parent - // regions), so they don't inherit the line's own hide. fs is a live - // fontstring here (checked above), so this read is safe. + // regions), so they don't inherit the line's own hide. Every key is a + // live fontstring (OnRegionDestroyed drops a destroyed one at once). const bool fsShown = *reinterpret_cast(reinterpret_cast(fs) + Offsets::OFF_REGION_ACTUALLY_SHOWN) != 0; @@ -448,12 +418,11 @@ void MaintainImpl() { } } -// SEH latch mirroring InlineTexture.cpp's SafeFlush. Maintain's liveness probe -// dereferences a fontstring pointer whose object *may* have been freed (see -// IsLiveFontString) — a residual the design accepts. The paint path is already -// guarded; this closes the matching hole on the tick path, so a fault disables -// maintenance for the session instead of crashing the client. A /reload re-arms -// it (PrepareForReload). The __try body holds no C++ objects — see MaintainImpl. +// SEH latch mirroring InlineTexture.cpp's SafeFlush: the tick drives engine +// region calls (SetTexture with addon-supplied paths, SetPoint, Show) from +// paint-time data, so a fault disables maintenance for the session instead of +// crashing the client. A /reload re-arms it (PrepareForReload). The __try body +// holds no C++ objects — see MaintainImpl. void Maintain() { if (g_maintainDisabled) return; @@ -476,8 +445,6 @@ void ApplyNowImpl(void *fs) { FsPlacements &np = it->second; if (!np.dirty) return; // deduped re-queue: already applied, nothing to do - if (!IsLiveFontString(fs)) - return; // Match Maintain's own visibility gate (actually-shown latch). The SMF's // per-line Show realizes this synchronously before we run, so a visible // line passes; a line not yet shown is left dirty for the tick path. @@ -514,11 +481,6 @@ static const Tick::FrameTick::AutoSubscribe _tick{&Maintain}; void QueuePlacements(void *fs, std::vector &&icons) { if (fs == nullptr) return; - // fs is a fontstring the engine is actively painting — learn its class - // vtable so Maintain's liveness check recognizes it (and stale, reused-memory - // keys, which never carry one). Must precede the early returns below so an - // entry's vtable is always known before Maintain evaluates it. - LearnFontStringVtable(fs); auto it = g_fsIcons.find(fs); if (it == g_fsIcons.end()) { if (icons.empty()) @@ -539,8 +501,8 @@ void QueuePlacements(void *fs, std::vector &&icons) { } void ApplyNow(void *fs, std::vector &&icons) { - // Queue first (learns the fs vtable, sets want+dirty+touch, dedups an - // unchanged re-queue), then apply synchronously for this fs. Deduped + // Queue first (sets want+dirty+touch, dedups an unchanged re-queue), then + // apply synchronously for this fs. Deduped // re-queues leave the entry not-dirty, so SafeApplyNow no-ops — a // per-paint steady state costs one compare, same as QueuePlacements. QueuePlacements(fs, std::move(icons)); @@ -551,6 +513,8 @@ void PrepareForReload() { // The reload teardown frees the icon regions (they die with their parent // frames); only forget the pointers — never Hide/free them. g_fsIcons.clear(); + g_iconRegions.clear(); + g_orphans.clear(); g_maintainDisabled = false; // re-arm the SEH latch for the fresh UI }