Skip to content

Commit

Permalink
VM: Fix static table offset locator (and less hacky)
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jan 29, 2024
1 parent 07945d4 commit 1fa2715
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions shared/sdk/REContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,38 @@ namespace sdk {
#if TDB_VER >= 71
if (s_global_context != nullptr && *s_global_context != nullptr) {
auto static_tbl = (REStaticTbl**)((uintptr_t)*s_global_context + s_static_tbl_offset);
while (IsBadReadPtr(*static_tbl, sizeof(void*)) || ((uintptr_t)*static_tbl & (sizeof(void*) - 1)) != 0) {
s_static_tbl_offset -= sizeof(void*);
static_tbl = (REStaticTbl**)((uintptr_t)*s_global_context + s_static_tbl_offset);
spdlog::info("[VM::update_pointers] Static table offset is bad, correcting to {:x}...", s_static_tbl_offset);
if (IsBadReadPtr(*static_tbl, sizeof(void*)) || ((uintptr_t)*static_tbl & (sizeof(void*) - 1)) != 0) {
spdlog::info("[VM::update_pointers] Static table offset is bad, correcting...");

// We are looking for the two arrays, the static field table, and the static field "initialized table"
// The initialized table tells whether a specific entry in the static field table has been initialized or not
// so they both should have the same size, easy to find
for (auto i = sizeof(void*); i < 0x100; i+= sizeof(void*)) {
const auto& ptr = *(REStaticTbl**)((uintptr_t)*s_global_context + (s_type_db_offset - i));

if (IsBadReadPtr(ptr, sizeof(void*)) || ((uintptr_t)ptr & (sizeof(void*) - 1)) != 0) {
continue;
}

spdlog::info("[VM::update_pointers] Examining {:x}", (uintptr_t)ptr);

const auto& potential_count = *(uint32_t*)((uintptr_t)&ptr + sizeof(void*));

if (potential_count < 2000) {
continue;
}

constexpr auto array_size = (sizeof(void*) * 2);
const auto previous_offset = s_type_db_offset - i - array_size;
const auto& previous_ptr = *(REStaticTbl**)((uintptr_t)*s_global_context + previous_offset);
const auto& previous_count = *(uint32_t*)((uintptr_t)&previous_ptr + sizeof(void*));

if (previous_count == potential_count) {
spdlog::info("[VM::update_pointers] Found static table at {:x} (offset {:x})", (uintptr_t)ptr, previous_offset);
s_static_tbl_offset = previous_offset;
break;
}
}
}
}
#endif
Expand Down

0 comments on commit 1fa2715

Please sign in to comment.