Skip to content

Commit

Permalink
Avoid using the glibc heap for the trampoline
Browse files Browse the repository at this point in the history
Use the known-rwx code allocator of SourcePawn like we do for the other code parts of the detour hooking logic.

There seem to be problems with trying to make the C++ heap executable on CentOS 7+, so avoid using executable heap memory in the first place.

Fixes #11
Fixes #22
  • Loading branch information
peace-maker committed Jun 23, 2021
1 parent fb3cc73 commit 83c6ad5
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions DynamicHooks/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ CHook::CHook(void* pFunc, ICallingConvention* pConvention)
// Determine the number of bytes we need to copy
int iBytesToCopy = copy_bytes(pTarget, NULL, JMP_SIZE);

// Create an array for the bytes to copy + a jump to the rest of the
// Create a buffer for the bytes to copy + a jump to the rest of the
// function.
unsigned char* pCopiedBytes = new unsigned char[iBytesToCopy + JMP_SIZE];
unsigned char* pCopiedBytes = (unsigned char *) smutils->GetScriptingEngine()->AllocatePageMemory(iBytesToCopy + JMP_SIZE);

// Fill the array with NOP instructions
memset(pCopiedBytes, 0x90, iBytesToCopy + JMP_SIZE);

// Copy the required bytes to our array
SetMemPatchable(pCopiedBytes, iBytesToCopy + JMP_SIZE);
copy_bytes(pTarget, pCopiedBytes, JMP_SIZE);

// Write a jump after the copied bytes to the function/bridge + number of bytes to copy
Expand All @@ -94,9 +93,8 @@ CHook::~CHook()
// Copy back the previously copied bytes
copy_bytes((unsigned char *) m_pTrampoline, (unsigned char *) m_pFunc, JMP_SIZE);

// Free the trampoline array
unsigned char* pTrampoline = (unsigned char *)m_pTrampoline;
delete [] pTrampoline;
// Free the trampoline buffer
smutils->GetScriptingEngine()->FreePageMemory(m_pTrampoline);

// Free the asm bridge and new return address
smutils->GetScriptingEngine()->FreePageMemory(m_pBridge);
Expand Down

0 comments on commit 83c6ad5

Please sign in to comment.