diff --git a/src/DllMain.cpp b/src/DllMain.cpp index 97cabaa1..c589935d 100644 --- a/src/DllMain.cpp +++ b/src/DllMain.cpp @@ -33,13 +33,12 @@ static void __fastcall InvalidFunctionPtrCheck_h() {} static bool __fastcall FrameScript_Initialize_h() { // BEFORE the engine tears down the old event table (which it does at - // the start of FrameScript_Initialize), null out our injected event - // names. The engine's teardown at 0x00701A54 calls SMemFree on every - // entry's name; our names are static literals in our DLL and not - // Storm-allocated, so leaving them in place trips the SMem safety - // check on `/reload` and crashes. The check at 0x00701A45 skips the - // free for any entry with NULL name, so this is the supported escape - // hatch. On first boot the cache is empty and this is a no-op. + // the start of FrameScript_Initialize), invalidate our cache. Our + // injected names are Storm allocations, so the engine's + // `SMemFree(entry.name)` teardown loop handles cleanup correctly on + // its own — but the table is rebuilt at a fresh address afterwards, + // and our cached slot indices point into the old layout. Drop the + // writes gate and reset slots so post-rebuild `RetryAll` re-claims. Event::Custom::PrepareForReload(); FrameScript_Initialize_o(); diff --git a/src/Offsets.h b/src/Offsets.h index d1f8900e..d75f773d 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -391,4 +391,27 @@ enum Offsets { // them as NULL during the walk. EVENT_TABLE_VALID_PTR_LO = 0x007FF000, EVENT_TABLE_VALID_PTR_HI = 0x00C00000, + + // Storm allocator — Blizzard's internal C utility library. `SMemAlloc` + // wraps every block with bookkeeping (header magic + size + caller + // file:line, footer magic, plus a global registered-block list); the + // matching `SMemFree` validates against all of that and panics with + // `ERROR #124 SMem3: Pointer does not refer to a valid allocated block + // of memory` if the pointer didn't come from `SMemAlloc`. + // + // Both are `__stdcall`, 4 args, `ret 0x10`. Verified via disassembly + // at the function bodies (push order matches arg order; `ret 0x10` + // pops the 4 stack args). The Storm allocator instance both wrap is + // the singleton at `0x00C51C58`. + // + // void *__stdcall SMemAlloc(size_t size, const char *file, int line, + // int flags); // flags: bit 3 (0x08) = zero-fill + // int __stdcall SMemFree (void *ptr, const char *file, int line, + // int flags); // returns 1 + // + // We use `SMemAlloc` for event-name storage in `Event::Custom` so the + // engine's reload teardown loop (which `SMemFree`s every event entry's + // name) doesn't trip its safety check on our injected pointers. + FUN_STORM_SMEM_ALLOC = 0x006462E0, + FUN_STORM_SMEM_FREE = 0x00646430, }; diff --git a/src/event/Custom.cpp b/src/event/Custom.cpp index a487b2df..51ba4773 100644 --- a/src/event/Custom.cpp +++ b/src/event/Custom.cpp @@ -16,6 +16,7 @@ #include "Offsets.h" #include +#include namespace Event::Custom { @@ -38,6 +39,29 @@ CacheEntry g_cache[MAX_CACHE]; int g_cacheCount = 0; bool g_writesEnabled = false; +// Storm-allocate a copy of `s`. The engine's event table treats every +// `entry.name` as a Storm-owned pointer — its reload teardown loop calls +// `SMemFree(entry.name)` and validates the block against Storm's registered +// allocations (panics with `ERROR #124 SMem3: ...` if the pointer didn't +// come from `SMemAlloc`). So when we inject a name, we must hand it a real +// Storm allocation, not a static literal in our DLL. +// +// We never call `SMemFree` ourselves — the engine's teardown owns the +// lifetime once we've written the pointer into a slot. +char *AllocStormCopy(const char *s) { + if (s == nullptr) + return nullptr; + using SMemAlloc_t = void *(__stdcall *)(size_t size, const char *file, + int line, int flags); + auto fn = reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC); + const size_t len = std::strlen(s); + char *buf = static_cast(fn(len + 1, __FILE__, __LINE__, 0)); + if (buf == nullptr) + return nullptr; + std::memcpy(buf, s, len + 1); + return buf; +} + int TryClaim(const char *eventName) { if (!g_writesEnabled) return -1; @@ -53,7 +77,10 @@ int TryClaim(const char *eventName) { auto **namePtr = reinterpret_cast( base + i * Offsets::EVENT_ENTRY_STRIDE + Offsets::OFF_EVENT_ENTRY_NAME); if (*namePtr == nullptr) { - *namePtr = eventName; + char *stormName = AllocStormCopy(eventName); + if (stormName == nullptr) + return -1; + *namePtr = stormName; return i; } } @@ -90,25 +117,18 @@ void EnableWrites() { g_writesEnabled = true; } void PrepareForReload() { // The engine's teardown at 0x00701A40 walks every entry and calls - // `SMemFree(entry.name)` (line 77 of `FrameScript.cpp`, per the error - // message — the literal `0x4D` push at 0x00701A4C). It has a NULL - // check at 0x00701A45 / `jz 0x00701A59` that skips entries with no - // name. Our injected names are static literals in our DLL and not - // Storm allocations, so we MUST null them out before the engine - // walks the table — otherwise the SMem safety check trips. After - // nulling, drop write privileges and reset the cache slots so we - // re-claim in the rebuilt table after the next `LoadScriptFunctions`. - auto *base = *reinterpret_cast(Offsets::VAR_EVENT_TABLE_BASE_PTR); - if (base != nullptr) { - for (int i = 0; i < g_cacheCount; i++) { - if (g_cache[i].slot >= 0) { - auto **namePtr = reinterpret_cast( - base + g_cache[i].slot * Offsets::EVENT_ENTRY_STRIDE + - Offsets::OFF_EVENT_ENTRY_NAME); - *namePtr = nullptr; - } - } - } + // `SMemFree(entry.name)`, which validates the block came from + // `SMemAlloc`. Our injected names are Storm allocations (see + // `AllocStormCopy`), so the engine's free path handles them + // correctly — we don't need to touch the engine's table here. + // + // We DO still need to invalidate our cache: the engine's teardown + // is followed by a full table rebuild at a fresh allocation, so + // every cached `slot` index points into the old layout and is no + // longer ours. Reset everything to `slot = -1` and drop the writes + // gate; `LoadScriptFunctions_h` will re-enable writes after the + // rebuild, and `RetryAll` (fired by every subsequent + // `RegisterEvent` from Lua) will re-claim and re-allocate. for (int i = 0; i < g_cacheCount; i++) g_cache[i].slot = -1; g_writesEnabled = false; diff --git a/src/event/Custom.h b/src/event/Custom.h index a401e241..279981c1 100644 --- a/src/event/Custom.h +++ b/src/event/Custom.h @@ -46,12 +46,12 @@ void RetryAll(); void EnableWrites(); // Call this BEFORE the engine's `FrameScript_Initialize_o` runs (i.e. -// before each `/reload`). Walks our claimed slots and NULLs out the name -// pointers we wrote in. The engine's reload path iterates every entry -// and `SMemFree`s its name; our names are static literals in our DLL, -// not Storm-allocated, so they'd trip the SMem safety check and crash. -// Also drops the write gate and resets cached slot indices so the new -// table gets fresh claims. +// before each `/reload`). The engine's reload path iterates every entry +// and `SMemFree`s its name; we hand it Storm-allocated copies (see +// `AllocStormCopy` in Custom.cpp), so the free is safe — we don't need +// to touch the engine's table here. What we DO need: drop the writes +// gate and reset cached slot indices, since the engine reallocates the +// table at a fresh address and our previous slot indices are stale. void PrepareForReload(); // Dispatches an event registered via `Register` with two int args