diff --git a/src/Offsets.h b/src/Offsets.h index 6b1d8aae..7c6db2dd 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -361,6 +361,23 @@ enum Offsets { OFF_FONTSTRING_FONT_HOLDER = 0xCC, // font-reference sub-object (SetFont `this`) OFF_FONTSTRING_FONT_OBJECT = 0xD0, // font object pointer (holder + 4) OFF_REGION_ANCHOR = 0x24, // LayoutFrame anchor sub-object (SetPoint `this` / relativeTo base) + + // --- GetPoint dangling-anchor guard (Frame::GetPointGuard) --- + // `Script_GetPoint` walks a region's 9 anchor-node slots at + // region+0x28. Each node with vtable VAR_ANCHOR_VTABLE_WITH_RELATIVE + // carries a RAW `relativeTo` pointer at +0x0C (the anchor target's + // LayoutFrame inner = target CFrame base + OFF_REGION_ANCHOR). When the + // target frame is freed the pointer is never cleared; GetPoint then does + // `MOV EAX,[relativeTo-0x24+4]` at 0x007A2452 → ERROR #132. The rel-less + // anchor node type (vtable 0x0081C42C, 12 bytes) has NO relativeTo field + // and its readers never deref one, so only 0x44C nodes can dangle. + FUN_GET_POINT = 0x007A2340, // engine Script_GetPoint (Region registry idx 15); delegate target + FUN_REGION_CLEAR_POINT_BY_RELATIVE = 0x00767E70, // __thiscall(region+OFF_REGION_ANCHOR, relativeTo, doRelayout): frees+NULLs every anchor node on this region whose GetRelativeTo()==relativeTo (pointer compare, never derefs the target). The engine's own "my anchor target was removed" cleanup — the one NOT called on target destruction, which is the bug. + OFF_REGION_ANCHOR_ARRAY = 0x28, // region base + 0x28 = anchor-node slot[0] (= OFF_REGION_ANCHOR + 4) + REGION_ANCHOR_SLOT_COUNT = 9, // one slot per FRAMEPOINT_* enum (0..8) + OFF_ANCHOR_NODE_RELATIVE_TO = 0x0C, // raw relativeTo ptr within a 0x44C anchor node + VAR_ANCHOR_VTABLE_WITH_RELATIVE = 0x0081C44C, // .rdata vtable of anchor nodes that carry a relativeTo (vs rel-less 0x0081C42C) + DRAWLAYER_ARTWORK = 2, FRAMEPOINT_TOPLEFT = 0, FRAMEPOINT_TOPRIGHT = 2, diff --git a/src/debug/GetPointCrash.cpp b/src/debug/GetPointCrash.cpp new file mode 100644 index 00000000..55f9b65e --- /dev/null +++ b/src/debug/GetPointCrash.cpp @@ -0,0 +1,162 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . +// +// Deterministic reproducer for the notorious 1.12.1 `GetPoint` crash +// (ERROR #132, ACCESS_VIOLATION at 0x007A2452). Debug-only. +// +// The crash: `Script_GetPoint` (Region method registry idx 15, VA +// 0x007A2340) walks a frame's anchor nodes at `object + 0x28` (9 slots of +// 4 bytes). Each populated slot is a 0x14-byte node +// `{vtable=0x0081C44C, xOfs@+0x04, yOfs@+0x08, relativeTo@+0x0C, relPoint@+0x10}`. +// `relativeTo` is the anchor TARGET's CLayoutFrame-inner pointer +// (= target CFrame base + 0x24), stored RAW with no weak reference. When +// the target frame is freed the pointer is never cleared, and GetPoint does: +// +// 007a2429 CALL [EAX+0xC] ; node vtable[3] -> returns node.relativeTo +// 007a2430 ADD EAX,-0x24 ; EAX = relativeTo - 0x24 = target CFrame base +// 007a244f MOV EBX,[EBP-4] ; EBX = target CFrame base (DANGLING) +// 007a2452 MOV EAX,[EBX+4] ; <-- reads refcount at base+4 -> #132 +// +// The fault address is always `relativeTo - 0x20`. This cannot be produced +// from pure Lua on a stock 1.12 client, because `CreateFrame` frames are +// never freed — the dangling pointer only arises when the engine frees a +// frame across a world transition (e.g. UIParent recreation) or another +// DLL frees one. So we arm the exact condition from C: take a frame that +// already has a live anchor (call `frame:SetPoint(...)` first), and rewrite +// that node's `relativeTo` to a pointer whose `-0x20` is unreadable. +// +// Usage — arm and fire in ONE macro, on a HIDDEN frame: +// /run TestF=CreateFrame("Frame","TestF",UIParent); TestF:Hide(); TestF:SetPoint("CENTER"); _classicapi_ArmGetPointCrash(TestF); print(TestF:GetPoint(1)) +// +// Both halves of that matter. The armed `relativeTo` is read by MORE than +// GetPoint: the same node's vtable[1]/[2] (`GetWidth`/`GetHeight`, +// FUN_007A2F90 / FUN_007A3070) deref it at `[relativeTo + 0x3C]` during the +// layout pass, with no guard of their own. Arming a VISIBLE frame and then +// firing from a second macro lets the render pass reach the anchor first — +// it crashes at 0x007A2FB8 (layout) instead of 0x007A2452 (GetPoint), +// testing the wrong reader. Hiding the frame keeps it out of the layout +// pass, and keeping arm+GetPoint in one macro leaves no frame boundary +// between them. +// +// Modes (optional 2nd arg, default 0): +// 0 decommitted page — reads a reserved-but-uncommitted page; reproduces +// the exact #132 signature (fault addr ends ...FFE8, +// like the field dumps' 0x16C0FFE8). +// 1 low address — relativeTo = 0x24 -> reads 0x00000004 (the other +// common #132 flavor, "referenced memory at 0x4"). +// 2 null relativeTo — sets relativeTo = 0; GetPoint takes its own safe +// "no relativeTo" branch. Does NOT crash even +// unpatched — the control case that proves a valid +// no-anchor-target read still returns cleanly. + +#include "Game.h" +#include "Offsets.h" + +#include + +#include + +namespace Debug::GetPointCrash { + +namespace { + +// Anchor node layout — see the file header. The vtable constant is the +// engine's anchor-object vtable in .rdata; checking it guards against +// corrupting a slot that somehow isn't a real anchor node. +constexpr uintptr_t kAnchorVtable = 0x0081C44C; +constexpr int kAnchorArrayOffset = 0x28; // object + 0x28 = slot[0] +constexpr int kAnchorSlots = 9; +constexpr int kNodeRelativeTo = 0x0C; + +// Reserved-but-uncommitted address space, allocated once and reused. Any +// read into it faults with ACCESS_VIOLATION, exactly like the decommitted +// CFrame page in the real crash. 64KB granularity means the base ends in +// 0x0000, so placing the synthetic target base at +0xFFE4 makes GetPoint's +// `[relativeTo-0x20]` read land at ...FFE8 — matching the dumps. +void *g_reserved = nullptr; + +uintptr_t DecommittedInner() { + if (g_reserved == nullptr) + g_reserved = VirtualAlloc(nullptr, 0x11000, MEM_RESERVE, PAGE_NOACCESS); + if (g_reserved == nullptr) + return 0x24; // fall back to the low-address flavor if reservation fails + // target CFrame base = reserved + 0xFFE4; inner = base + 0x24. + // GetPoint reads [inner - 0x24 + 4] = [base + 4] = reserved + 0xFFE8. + return reinterpret_cast(g_reserved) + 0xFFE4 + 0x24; +} + +// `_classicapi_ArmGetPointCrash(frame [, mode])` — arms `frame`'s first +// live anchor node so the next `frame:GetPoint(index)` faults. Returns +// (faultAddress, armedSlotIndex). Raises a Lua error (no crash) if the +// frame has no anchor yet or the slot isn't a recognizable anchor node. +int __fastcall Script_ArmGetPointCrash(void *L) { + void *obj = Game::Lua::ResolveRegion(L, 1); // raises if arg1 isn't a region + if (obj == nullptr) + return 0; + + const int mode = + Game::Lua::IsNumber(L, 2) ? static_cast(Game::Lua::ToNumber(L, 2)) : 0; + + auto *base = static_cast(obj); + uint8_t *node = nullptr; + int slotIdx = -1; + for (int i = 0; i < kAnchorSlots; ++i) { + auto *n = Game::Read(base, kAnchorArrayOffset + i * 4); + if (n != nullptr) { + node = n; + slotIdx = i; + break; + } + } + if (node == nullptr) { + Game::Lua::Error( + L, "_classicapi_ArmGetPointCrash: frame has no anchor; call SetPoint first"); + return 0; + } + if (Game::Read(node, 0) != kAnchorVtable) { + Game::Lua::Error(L, + "_classicapi_ArmGetPointCrash: slot is not an anchor node"); + return 0; + } + + uintptr_t badInner; + switch (mode) { + case 1: + badInner = 0x24; // reads [0x24 - 0x20] = 0x00000004 + break; + case 2: + badInner = 0; // safe "no relativeTo" branch — control case, no crash + break; + default: + badInner = DecommittedInner(); + break; + } + Game::Ref(node, kNodeRelativeTo) = badInner; + + // Report the address the next GetPoint will dereference (relativeTo - 0x20). + const double faultAddr = badInner ? static_cast(badInner - 0x20) : 0.0; + Game::Lua::PushNumber(L, faultAddr); + Game::Lua::PushNumber(L, static_cast(slotIdx)); + return 2; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterGlobalFunction("_classicapi_ArmGetPointCrash", + &Script_ArmGetPointCrash); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Debug::GetPointCrash diff --git a/src/frame/GetPointGuard.cpp b/src/frame/GetPointGuard.cpp new file mode 100644 index 00000000..dbc069cf --- /dev/null +++ b/src/frame/GetPointGuard.cpp @@ -0,0 +1,170 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . +// +// Neuters the notorious 1.12 `GetPoint` crash (ERROR #132 at 0x007A2452). +// +// The bug: an anchor node (`region+0x28 + point*4`, vtable +// VAR_ANCHOR_VTABLE_WITH_RELATIVE) stores its `relativeTo` target as a RAW +// pointer at +0x0C with no weak reference. When the target frame is freed, +// the engine cleans the DYING frame's own anchors but never the anchors on +// OTHER frames that point AT it — so the pointer dangles. `GetPoint` then +// does `MOV EAX,[relativeTo-0x24+4]` (read the target's refcount) and faults +// on the freed/decommitted page. The layout readers `GetWidth`/`GetHeight` +// (the same node's vtable[1]/[2]) deref the same dangling `relativeTo` +// without a guard too, so nulling the pointer only moves the crash — the +// node has to go. +// +// Fix (no MinHook): re-register `GetPoint` on the Region method registry. +// FUN_REGISTER_FRAME_METHODS pushes to the FRONT of the registry's hash +// bucket and the dispatcher walks front-first, so the most recent +// registration wins; the engine registers the Region table as the first +// call inside FUN_LOAD_SCRIPT_FUNCTIONS and ModuleAutoRegister runs after +// the original, so ours wins on every /reload — same mechanism as the +// `Texture::SetDesaturated` override. +// +// Our GetPoint pre-scans the region's anchor nodes; any whose `relativeTo` +// target is no longer readable is handed to the engine's OWN +// FUN_REGION_CLEAR_POINT_BY_RELATIVE — the cleanup the engine runs when an +// anchor target is legitimately removed, and precisely the call that is +// MISSING on target destruction. It frees the node and NULLs the slot by +// pointer comparison, never dereferencing the dead target. Then we delegate +// to the stock GetPoint, which now walks only live anchors. This also +// protects the layout readers, since the dead node is gone for good. +// +// Why DELETE the node rather than just NULL its relativeTo — confirmed against +// 3.3.5 (Frostmourne, Script_GetPoint 0x0049D950). Blizzard fixed this by +// restructuring the anchor: 3.3.5's `CFramePoint` is a 16-byte POD with no +// vtable — {xOfs@0, yOfs@4, relativeTo@8, relPoint-byte + FLAGS @0xC} — and +// on destruction `FUN_0048B130` walks the dying frame's dependents and calls +// `CFramePoint::Invalidate` (`FUN_0049C7F0`) on each referencing node, which +// zeroes relativeTo and sets flag bit 0x800 IN PLACE, leaving the node +// allocated. Every reader then gates on 0x800 before touching relativeTo +// (verified in GetPoint 0x0049D9BF, GetNumPoints 0x0049D8B0, +// FUN_0048A200 and FUN_0048B130 itself), so a destroyed target degrades to +// "anchor ignored" instead of a wild deref. 3.3.5 also makes every "no +// target" state first-class: the CSimpleTop singleton ([0x00B499A8]) for +// screen-anchored (GetPoint pushes nil), flag 0x100 for no-relativeTo. +// +// 1.12 CANNOT copy that: its node has no flags word, so there is nowhere to +// put 0x800 and no bit for readers to check. Its only representation for a +// dead anchor is the EMPTY SLOT — which is exactly what +// FUN_REGION_CLEAR_POINT_BY_RELATIVE produces. That is also why nulling +// relativeTo here would be wrong rather than merely incomplete: a live 0x44C +// node with relativeTo == 0 is a state the engine never creates, and the +// layout readers would fault on `[0 + 0x3C]`. +// +// Reproduce / verify with `_classicapi_ArmGetPointCrash` (debug/GetPointCrash.cpp): +// /run TestF=CreateFrame("Frame","TestF",UIParent); TestF:SetPoint("CENTER"); _classicapi_ArmGetPointCrash(TestF) +// /run print(TestF:GetPoint(1)) -- pre-fix: #132 ; post-fix: nil (anchor cleaned) + +#include "Game.h" +#include "Offsets.h" +#include "debug/Log.h" + +#include + +#include + +namespace Frame::GetPointGuard { + +namespace { + +using GetPoint_t = int(__fastcall *)(void *L); +using ClearPointByRelative_t = void(__thiscall *)(void *layout, void *relativeTo, + int doRelayout); + +// SEH-guarded readability probe. Standalone with only raw pointers and no +// C++ objects in scope, per the MSVC __try/__except constraint. A fault on +// any of the probed bytes means the region behind the pointer is gone. +bool ProbeReadable(const void *p, int n) { + __try { + const volatile uint8_t *q = static_cast(p); + for (int i = 0; i < n; ++i) + (void)q[i]; + return true; + } __except (EXCEPTION_EXECUTE_HANDLER) { + return false; + } +} + +// GetPoint reads the target CFrame at base+0 (vtable), +4 (refcount) and +// +8 (lua ref index); probe that header. base = relativeTo - OFF_REGION_ANCHOR. +constexpr int kTargetHeaderProbe = 0x10; + +// Walk `region`'s anchor slots and drop any whose relativeTo target is dead, +// so the stock GetPoint (and the layout readers) only ever see live anchors. +void CleanDanglingAnchors(void *region) { + auto *base = static_cast(region); + for (int i = 0; i < Offsets::REGION_ANCHOR_SLOT_COUNT; ++i) { + auto *node = Game::Read( + base, Offsets::OFF_REGION_ANCHOR_ARRAY + i * 4); + if (node == nullptr) + continue; + // Only relativeTo-carrying nodes can dangle; the rel-less node type + // has no target field and its readers never deref one. Checking the + // vtable also guards against corrupting an unrecognized slot. + if (Game::Read(node, 0) != Offsets::VAR_ANCHOR_VTABLE_WITH_RELATIVE) + continue; + auto *rel = + Game::Read(node, Offsets::OFF_ANCHOR_NODE_RELATIVE_TO); + // rel == 0 never occurs on a live 0x44C node (SetPoint/SetAllPoints + // always store a real target and error otherwise). Leave it to the + // engine's own null-relativeTo path; do NOT run the cleanup with a + // NULL target, which would also match — and delete — legit rel-less + // anchors (whose GetRelativeTo likewise returns 0). + if (rel == nullptr) + continue; + auto *target = rel - Offsets::OFF_REGION_ANCHOR; // inner - 0x24 = CFrame base + if (ProbeReadable(target, kTargetHeaderProbe)) + continue; // live target — anchor is fine + + // Dangling. Hand the dead target to the engine's own removal path; it + // frees every anchor on this region pointing at `rel` and NULLs the + // slot(s). doRelayout = 0: don't kick a layout cascade from inside a + // getter — the frame relayouts on the next natural pass. + auto ClearByRel = reinterpret_cast( + Offsets::FUN_REGION_CLEAR_POINT_BY_RELATIVE); + ClearByRel(base + Offsets::OFF_REGION_ANCHOR, rel, 0); + Debug::Log::Printf( + "[GetPointGuard] cleaned dangling anchor: region=%p slot=%d relativeTo=%p", + region, i, static_cast(rel)); + } +} + +int __fastcall GetPoint_h(void *L) { + // Resolve exactly as the engine does (same type-id var 0x00CF0C3C), but + // without raising — let the stock GetPoint emit the canonical error text + // for a bad `self`. Clean only when we actually hold a region. + void *region = Game::Lua::ResolveRegion(L, 1, /*raiseError=*/false); + if (region != nullptr) + CleanDanglingAnchors(region); + + auto engineGetPoint = reinterpret_cast(Offsets::FUN_GET_POINT); + return engineGetPoint(L); +} + +const Game::Lua::FrameMethodEntry g_methods[] = { + {"GetPoint", &GetPoint_h}, +}; + +void RegisterLuaFunctions() { + Game::Lua::RegisterFrameMethods( + reinterpret_cast(Offsets::VAR_REGION_METHOD_REGISTRY), g_methods, + static_cast(sizeof(g_methods) / sizeof(g_methods[0]))); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Frame::GetPointGuard