1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/CDetour/detours.cpp Wed Jul 02 13:53:40 2014 -0400
1.3 @@ -0,0 +1,192 @@
1.4 +/**
1.5 +* vim: set ts=4 :
1.6 +* =============================================================================
1.7 +* SourceMod
1.8 +* Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
1.9 +* =============================================================================
1.10 +*
1.11 +* This program is free software; you can redistribute it and/or modify it under
1.12 +* the terms of the GNU General Public License, version 3.0, as published by the
1.13 +* Free Software Foundation.
1.14 +*
1.15 +* This program is distributed in the hope that it will be useful, but WITHOUT
1.16 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 +* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 +* details.
1.19 +*
1.20 +* You should have received a copy of the GNU General Public License along with
1.21 +* this program. If not, see <http://www.gnu.org/licenses/>.
1.22 +*
1.23 +* As a special exception, AlliedModders LLC gives you permission to link the
1.24 +* code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 +* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 +* by the Valve Corporation. You must obey the GNU General Public License in
1.27 +* all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 +* this exception to all derivative works. AlliedModders LLC defines further
1.29 +* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 +* or <http://www.sourcemod.net/license.php>.
1.31 +*
1.32 +* Version: $Id: detours.cpp 248 2008-08-27 00:56:22Z pred $
1.33 +*/
1.34 +
1.35 +#include "detours.h"
1.36 +#include "../asm/asm.h"
1.37 +
1.38 +ISourcePawnEngine *CDetourManager::spengine = NULL;
1.39 +IGameConfig *CDetourManager::gameconf = NULL;
1.40 +
1.41 +void CDetourManager::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf)
1.42 +{
1.43 + CDetourManager::spengine = spengine;
1.44 + CDetourManager::gameconf = gameconf;
1.45 +}
1.46 +
1.47 +CDetour *CDetourManager::CreateDetour(void *callbackfunction, void **trampoline, const char *signame)
1.48 +{
1.49 + CDetour *detour = new CDetour(callbackfunction, trampoline, signame);
1.50 + if (detour)
1.51 + {
1.52 + if (!detour->Init(spengine, gameconf))
1.53 + {
1.54 + delete detour;
1.55 + return NULL;
1.56 + }
1.57 +
1.58 + return detour;
1.59 + }
1.60 +
1.61 + return NULL;
1.62 +}
1.63 +
1.64 +CDetour::CDetour(void *callbackfunction, void **trampoline, const char *signame)
1.65 +{
1.66 + enabled = false;
1.67 + detoured = false;
1.68 + detour_address = NULL;
1.69 + detour_trampoline = NULL;
1.70 + this->signame = signame;
1.71 + this->detour_callback = callbackfunction;
1.72 + spengine = NULL;
1.73 + gameconf = NULL;
1.74 + this->trampoline = trampoline;
1.75 +}
1.76 +
1.77 +bool CDetour::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf)
1.78 +{
1.79 + this->spengine = spengine;
1.80 + this->gameconf = gameconf;
1.81 +
1.82 + if (!CreateDetour())
1.83 + {
1.84 + enabled = false;
1.85 + return enabled;
1.86 + }
1.87 +
1.88 + enabled = true;
1.89 +
1.90 + return enabled;
1.91 +}
1.92 +
1.93 +void CDetour::Destroy()
1.94 +{
1.95 + DeleteDetour();
1.96 + delete this;
1.97 +}
1.98 +
1.99 +bool CDetour::IsEnabled()
1.100 +{
1.101 + return enabled;
1.102 +}
1.103 +
1.104 +bool CDetour::CreateDetour()
1.105 +{
1.106 + if (!gameconf->GetMemSig(signame, &detour_address))
1.107 + {
1.108 + g_pSM->LogError(myself, "Could not locate %s - Disabling detour", signame);
1.109 + return false;
1.110 + }
1.111 +
1.112 + if (!detour_address)
1.113 + {
1.114 + g_pSM->LogError(myself, "Sigscan for %s failed - Disabling detour to prevent crashes", signame);
1.115 + return false;
1.116 + }
1.117 +
1.118 + detour_restore.bytes = copy_bytes((unsigned char *)detour_address, NULL, OP_JMP_SIZE+1);
1.119 +
1.120 + /* First, save restore bits */
1.121 + for (size_t i=0; i<detour_restore.bytes; i++)
1.122 + {
1.123 + detour_restore.patch[i] = ((unsigned char *)detour_address)[i];
1.124 + }
1.125 +
1.126 + JitWriter wr;
1.127 + JitWriter *jit = ≀
1.128 + jit_uint32_t CodeSize = 0;
1.129 +
1.130 + wr.outbase = NULL;
1.131 + wr.outptr = NULL;
1.132 +
1.133 +jit_rewind:
1.134 +
1.135 + /* Patch old bytes in */
1.136 + if (wr.outbase != NULL)
1.137 + {
1.138 + copy_bytes((unsigned char *)detour_address, (unsigned char*)wr.outptr, detour_restore.bytes);
1.139 + }
1.140 + wr.outptr += detour_restore.bytes;
1.141 +
1.142 + /* Return to the original function */
1.143 + jitoffs_t call = IA32_Jump_Imm32(jit, 0);
1.144 + IA32_Write_Jump32_Abs(jit, call, (unsigned char *)detour_address + detour_restore.bytes);
1.145 +
1.146 + if (wr.outbase == NULL)
1.147 + {
1.148 + CodeSize = wr.get_outputpos();
1.149 + wr.outbase = (jitcode_t)spengine->AllocatePageMemory(CodeSize);
1.150 + spengine->SetReadWrite(wr.outbase);
1.151 + wr.outptr = wr.outbase;
1.152 + detour_trampoline = wr.outbase;
1.153 + goto jit_rewind;
1.154 + }
1.155 +
1.156 + spengine->SetReadExecute(wr.outbase);
1.157 +
1.158 + *trampoline = detour_trampoline;
1.159 +
1.160 + return true;
1.161 +}
1.162 +
1.163 +void CDetour::DeleteDetour()
1.164 +{
1.165 + if (detoured)
1.166 + {
1.167 + DisableDetour();
1.168 + }
1.169 +
1.170 + if (detour_trampoline)
1.171 + {
1.172 + /* Free the allocated trampoline memory */
1.173 + spengine->FreePageMemory(detour_trampoline);
1.174 + detour_trampoline = NULL;
1.175 + }
1.176 +}
1.177 +
1.178 +void CDetour::EnableDetour()
1.179 +{
1.180 + if (!detoured)
1.181 + {
1.182 + DoGatePatch((unsigned char *)detour_address, &detour_callback);
1.183 + detoured = true;
1.184 + }
1.185 +}
1.186 +
1.187 +void CDetour::DisableDetour()
1.188 +{
1.189 + if (detoured)
1.190 + {
1.191 + /* Remove the patch */
1.192 + ApplyPatch(detour_address, 0, &detour_restore, NULL);
1.193 + detoured = false;
1.194 + }
1.195 +}