mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-15 23:50:38 +00:00
Crash #132 at 0x00768323 (FUN_REGION_GET_RECT reading [5+0x3C]): Show lays a frame out before its children leave their layout deferral, so its OnSizeChanged ran; the ScrollBox handler read a region's size, and laying that region out reached an anchor point on a still-deferred child. The point getter (0x007A2F90 / 0x007A3070) ends the relative's deferral with FUN_REGION_LAYOUT_REALIZE(0), which ran the child's OnSizeChanged; that script re-anchored the region, freeing the point, and the getter then read the point again (0x007A2FC5) from freed heap. The getters now end the deferral themselves with the point marked busy. The point's deleting destructor (0x00767D80, the only free path) flags a busy point; a flagged getter returns the layout's undefined value without touching the point, and the point walk (0x007671A0 / 0x007671F0) is run again with the current anchors, up to ten times. Otherwise the engine's own getter reads the value, with the deferred bit hidden so a script that hid the relative again cannot start a second resolve. No hooks: the three getters/destructor are patched in the relative-point vtable (0x0081C44C, their only reference), and the six edge resolvers' calls to the walks are redirected. Adds Common::RedirectSlot for the vtable entries.
117 lines
4.0 KiB
C++
117 lines
4.0 KiB
C++
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
#include "Common.h"
|
|
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <windows.h>
|
|
|
|
namespace Common {
|
|
|
|
bool PatchBytes(void *dst, const void *src, size_t len) {
|
|
DWORD oldProt = 0;
|
|
if (!VirtualProtect(dst, len, PAGE_EXECUTE_READWRITE, &oldProt))
|
|
return false;
|
|
|
|
std::memcpy(dst, src, len);
|
|
FlushInstructionCache(GetCurrentProcess(), dst, len);
|
|
|
|
DWORD dummy = 0;
|
|
VirtualProtect(dst, len, oldProt, &dummy);
|
|
return true;
|
|
}
|
|
|
|
bool RedirectCall(uintptr_t site, uintptr_t expected, const void *target) {
|
|
const auto *code = reinterpret_cast<const uint8_t *>(site);
|
|
if (code[0] != 0xE8)
|
|
return false;
|
|
const auto callee = [site](int32_t rel) { return site + 5 + static_cast<uintptr_t>(rel); };
|
|
int32_t rel = 0;
|
|
std::memcpy(&rel, code + 1, sizeof rel);
|
|
const uintptr_t to = reinterpret_cast<uintptr_t>(target);
|
|
if (callee(rel) == to)
|
|
return true;
|
|
if (callee(rel) != expected)
|
|
return false;
|
|
rel = static_cast<int32_t>(to - (site + 5));
|
|
return PatchBytes(reinterpret_cast<uint8_t *>(site) + 1, &rel, sizeof rel);
|
|
}
|
|
|
|
bool RedirectJcc(uintptr_t site, uintptr_t expected, const void *target) {
|
|
const auto *code = reinterpret_cast<const uint8_t *>(site);
|
|
if (code[0] != 0x0F || (code[1] & 0xF0) != 0x80)
|
|
return false;
|
|
const auto dest = [site](int32_t rel) { return site + 6 + static_cast<uintptr_t>(rel); };
|
|
int32_t rel = 0;
|
|
std::memcpy(&rel, code + 2, sizeof rel);
|
|
const uintptr_t to = reinterpret_cast<uintptr_t>(target);
|
|
if (dest(rel) == to)
|
|
return true;
|
|
if (dest(rel) != expected)
|
|
return false;
|
|
rel = static_cast<int32_t>(to - (site + 6));
|
|
return PatchBytes(reinterpret_cast<uint8_t *>(site) + 2, &rel, sizeof rel);
|
|
}
|
|
|
|
bool RedirectSlot(uintptr_t slot, uintptr_t expected, const void *target) {
|
|
uintptr_t current = 0;
|
|
std::memcpy(¤t, reinterpret_cast<const void *>(slot), sizeof current);
|
|
const uintptr_t to = reinterpret_cast<uintptr_t>(target);
|
|
if (current == to)
|
|
return true;
|
|
if (current != expected)
|
|
return false;
|
|
return PatchBytes(reinterpret_cast<void *>(slot), &to, sizeof to);
|
|
}
|
|
|
|
bool ReplaceWithCall(uintptr_t site, const uint8_t *original, size_t len, const void *target) {
|
|
uint8_t code[64];
|
|
if (len < 5 || len > sizeof code)
|
|
return false;
|
|
code[0] = 0xE8;
|
|
const int32_t rel = static_cast<int32_t>(reinterpret_cast<uintptr_t>(target) - (site + 5));
|
|
std::memcpy(code + 1, &rel, sizeof rel);
|
|
std::memset(code + 5, 0x90, len - 5);
|
|
const void *at = reinterpret_cast<const void *>(site);
|
|
if (std::memcmp(at, code, len) == 0)
|
|
return true;
|
|
if (std::memcmp(at, original, len) != 0)
|
|
return false;
|
|
return PatchBytes(reinterpret_cast<void *>(site), code, len);
|
|
}
|
|
|
|
void BoundedCopy(char *dst, const char *src, size_t n) {
|
|
if (dst == nullptr || n == 0)
|
|
return;
|
|
size_t i = 0;
|
|
for (; src != nullptr && src[i] != '\0' && i + 1 < n; ++i)
|
|
dst[i] = src[i];
|
|
dst[i] = '\0';
|
|
}
|
|
|
|
void EnsureParentDirs(const char *relPath) {
|
|
std::string accum;
|
|
for (const char *p = relPath; *p != '\0'; ++p) {
|
|
if (*p == '\\' || *p == '/') {
|
|
if (!accum.empty())
|
|
CreateDirectoryA(accum.c_str(), nullptr);
|
|
accum.push_back('\\');
|
|
} else {
|
|
accum.push_back(*p);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Common
|