Skip to content

Commit

Permalink
Fix crash when a detour is removed twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbyte committed Apr 30, 2024
1 parent 59aee07 commit ae559cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
28 changes: 0 additions & 28 deletions BG3Extender/GameHooks/OsirisWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,34 +221,6 @@ void * OsirisWrappers::FindRuleActionCallProc()
return nullptr;
}

void const* ResolveRealFunctionAddress(void const * ptr)
{
auto p = (uint8_t const*)ptr;

// Unconditional jump
if (p[0] == 0xE9) {
int32_t relOffset = *reinterpret_cast<int32_t const *>(p + 1);
return p + relOffset + 5;
}

// Resolve function pointer through relocations
auto end = p + 64;
for (; p < end; p++)
{
// Look for the instruction "cmp qword ptr [rip+xxxxxx], 0"
if (p[0] == 0x48 && p[1] == 0x83 && p[2] == 0x3d && p[6] == 0x00 &&
// Look for the instruction "jmp xxxx"
p[13] == 0xe9)
{
int32_t relOffset = *reinterpret_cast<int32_t const *>(p + 14);
return p + relOffset + 18;
}
}

// Could not find any relocations
return ptr;
}

void OsirisWrappers::FindOsirisGlobals(FARPROC CtorProc)
{
#if 0
Expand Down
28 changes: 28 additions & 0 deletions CoreLib/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,32 @@ bool LoadFile(std::wstring const& path, std::string& body)
return false;
}

void const* ResolveRealFunctionAddress(void const * ptr)
{
auto p = (uint8_t const*)ptr;

// Unconditional jump
if (p[0] == 0xE9) {
int32_t relOffset = *reinterpret_cast<int32_t const *>(p + 1);
return p + relOffset + 5;
}

// Resolve function pointer through relocations
auto end = p + 64;
for (; p < end; p++)
{
// Look for the instruction "cmp qword ptr [rip+xxxxxx], 0"
if (p[0] == 0x48 && p[1] == 0x83 && p[2] == 0x3d && p[6] == 0x00 &&
// Look for the instruction "jmp xxxx"
p[13] == 0xe9)
{
int32_t relOffset = *reinterpret_cast<int32_t const *>(p + 14);
return p + relOffset + 18;
}
}

// Could not find any relocations
return ptr;
}

END_SE()
2 changes: 1 addition & 1 deletion CoreLib/Wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace bg3se {

bool IsWrapped() const
{
return TrampolineFunc != nullptr;
return FuncTrampoline != nullptr;
}

void Wrap(HMODULE Module, char const * ProcName, FuncType NewFunction)
Expand Down

0 comments on commit ae559cb

Please sign in to comment.