|
drifter01620@184
|
1 |
/** |
|
drifter01620@184
|
2 |
* vim: set ts=4 : |
|
drifter01620@184
|
3 |
* ============================================================================= |
|
drifter01620@184
|
4 |
* SourceMod |
|
drifter01620@184
|
5 |
* Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved. |
|
drifter01620@184
|
6 |
* ============================================================================= |
|
drifter01620@184
|
7 |
* |
|
drifter01620@184
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
drifter01620@184
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
drifter01620@184
|
10 |
* Free Software Foundation. |
|
drifter01620@184
|
11 |
* |
|
drifter01620@184
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
drifter01620@184
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
drifter01620@184
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
drifter01620@184
|
15 |
* details. |
|
drifter01620@184
|
16 |
* |
|
drifter01620@184
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
drifter01620@184
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
drifter01620@184
|
19 |
* |
|
drifter01620@184
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
drifter01620@184
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
drifter01620@184
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
drifter01620@184
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
drifter01620@184
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
drifter01620@184
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
drifter01620@184
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
drifter01620@184
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
drifter01620@184
|
28 |
* |
|
drifter01620@184
|
29 |
* Version: $Id: detours.cpp 248 2008-08-27 00:56:22Z pred $ |
|
drifter01620@184
|
30 |
*/ |
|
drifter01620@184
|
31 |
|
|
drifter01620@184
|
32 |
#include "detours.h" |
|
drifter01620@184
|
33 |
#include "../asm/asm.h" |
|
drifter01620@184
|
34 |
|
|
drifter01620@184
|
35 |
ISourcePawnEngine *CDetourManager::spengine = NULL; |
|
drifter01620@184
|
36 |
IGameConfig *CDetourManager::gameconf = NULL; |
|
drifter01620@184
|
37 |
|
|
drifter01620@184
|
38 |
void CDetourManager::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf) |
|
drifter01620@184
|
39 |
{ |
|
drifter01620@184
|
40 |
CDetourManager::spengine = spengine; |
|
drifter01620@184
|
41 |
CDetourManager::gameconf = gameconf; |
|
drifter01620@184
|
42 |
} |
|
drifter01620@184
|
43 |
|
|
drifter01620@184
|
44 |
CDetour *CDetourManager::CreateDetour(void *callbackfunction, void **trampoline, const char *signame) |
|
drifter01620@184
|
45 |
{ |
|
drifter01620@184
|
46 |
CDetour *detour = new CDetour(callbackfunction, trampoline, signame); |
|
drifter01620@184
|
47 |
if (detour) |
|
drifter01620@184
|
48 |
{ |
|
drifter01620@184
|
49 |
if (!detour->Init(spengine, gameconf)) |
|
drifter01620@184
|
50 |
{ |
|
drifter01620@184
|
51 |
delete detour; |
|
drifter01620@184
|
52 |
return NULL; |
|
drifter01620@184
|
53 |
} |
|
drifter01620@184
|
54 |
|
|
drifter01620@184
|
55 |
return detour; |
|
drifter01620@184
|
56 |
} |
|
drifter01620@184
|
57 |
|
|
drifter01620@184
|
58 |
return NULL; |
|
drifter01620@184
|
59 |
} |
|
drifter01620@184
|
60 |
|
|
drifter01620@184
|
61 |
CDetour::CDetour(void *callbackfunction, void **trampoline, const char *signame) |
|
drifter01620@184
|
62 |
{ |
|
drifter01620@184
|
63 |
enabled = false; |
|
drifter01620@184
|
64 |
detoured = false; |
|
drifter01620@184
|
65 |
detour_address = NULL; |
|
drifter01620@184
|
66 |
detour_trampoline = NULL; |
|
drifter01620@184
|
67 |
this->signame = signame; |
|
drifter01620@184
|
68 |
this->detour_callback = callbackfunction; |
|
drifter01620@184
|
69 |
spengine = NULL; |
|
drifter01620@184
|
70 |
gameconf = NULL; |
|
drifter01620@184
|
71 |
this->trampoline = trampoline; |
|
drifter01620@184
|
72 |
} |
|
drifter01620@184
|
73 |
|
|
drifter01620@184
|
74 |
bool CDetour::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf) |
|
drifter01620@184
|
75 |
{ |
|
drifter01620@184
|
76 |
this->spengine = spengine; |
|
drifter01620@184
|
77 |
this->gameconf = gameconf; |
|
drifter01620@184
|
78 |
|
|
drifter01620@184
|
79 |
if (!CreateDetour()) |
|
drifter01620@184
|
80 |
{ |
|
drifter01620@184
|
81 |
enabled = false; |
|
drifter01620@184
|
82 |
return enabled; |
|
drifter01620@184
|
83 |
} |
|
drifter01620@184
|
84 |
|
|
drifter01620@184
|
85 |
enabled = true; |
|
drifter01620@184
|
86 |
|
|
drifter01620@184
|
87 |
return enabled; |
|
drifter01620@184
|
88 |
} |
|
drifter01620@184
|
89 |
|
|
drifter01620@184
|
90 |
void CDetour::Destroy() |
|
drifter01620@184
|
91 |
{ |
|
drifter01620@184
|
92 |
DeleteDetour(); |
|
drifter01620@184
|
93 |
delete this; |
|
drifter01620@184
|
94 |
} |
|
drifter01620@184
|
95 |
|
|
drifter01620@184
|
96 |
bool CDetour::IsEnabled() |
|
drifter01620@184
|
97 |
{ |
|
drifter01620@184
|
98 |
return enabled; |
|
drifter01620@184
|
99 |
} |
|
drifter01620@184
|
100 |
|
|
drifter01620@184
|
101 |
bool CDetour::CreateDetour() |
|
drifter01620@184
|
102 |
{ |
|
drifter01620@184
|
103 |
if (!gameconf->GetMemSig(signame, &detour_address)) |
|
drifter01620@184
|
104 |
{ |
|
drifter01620@184
|
105 |
g_pSM->LogError(myself, "Could not locate %s - Disabling detour", signame); |
|
drifter01620@184
|
106 |
return false; |
|
drifter01620@184
|
107 |
} |
|
drifter01620@184
|
108 |
|
|
drifter01620@184
|
109 |
if (!detour_address) |
|
drifter01620@184
|
110 |
{ |
|
drifter01620@184
|
111 |
g_pSM->LogError(myself, "Sigscan for %s failed - Disabling detour to prevent crashes", signame); |
|
drifter01620@184
|
112 |
return false; |
|
drifter01620@184
|
113 |
} |
|
drifter01620@184
|
114 |
|
|
drifter01620@184
|
115 |
detour_restore.bytes = copy_bytes((unsigned char *)detour_address, NULL, OP_JMP_SIZE+1); |
|
drifter01620@184
|
116 |
|
|
drifter01620@184
|
117 |
/* First, save restore bits */ |
|
drifter01620@184
|
118 |
for (size_t i=0; i<detour_restore.bytes; i++) |
|
drifter01620@184
|
119 |
{ |
|
drifter01620@184
|
120 |
detour_restore.patch[i] = ((unsigned char *)detour_address)[i]; |
|
drifter01620@184
|
121 |
} |
|
drifter01620@184
|
122 |
|
|
drifter01620@184
|
123 |
JitWriter wr; |
|
drifter01620@184
|
124 |
JitWriter *jit = ≀ |
|
drifter01620@184
|
125 |
jit_uint32_t CodeSize = 0; |
|
drifter01620@184
|
126 |
|
|
drifter01620@184
|
127 |
wr.outbase = NULL; |
|
drifter01620@184
|
128 |
wr.outptr = NULL; |
|
drifter01620@184
|
129 |
|
|
drifter01620@184
|
130 |
jit_rewind: |
|
drifter01620@184
|
131 |
|
|
drifter01620@184
|
132 |
/* Patch old bytes in */ |
|
drifter01620@184
|
133 |
if (wr.outbase != NULL) |
|
drifter01620@184
|
134 |
{ |
|
drifter01620@184
|
135 |
copy_bytes((unsigned char *)detour_address, (unsigned char*)wr.outptr, detour_restore.bytes); |
|
drifter01620@184
|
136 |
} |
|
drifter01620@184
|
137 |
wr.outptr += detour_restore.bytes; |
|
drifter01620@184
|
138 |
|
|
drifter01620@184
|
139 |
/* Return to the original function */ |
|
drifter01620@184
|
140 |
jitoffs_t call = IA32_Jump_Imm32(jit, 0); |
|
drifter01620@184
|
141 |
IA32_Write_Jump32_Abs(jit, call, (unsigned char *)detour_address + detour_restore.bytes); |
|
drifter01620@184
|
142 |
|
|
drifter01620@184
|
143 |
if (wr.outbase == NULL) |
|
drifter01620@184
|
144 |
{ |
|
drifter01620@184
|
145 |
CodeSize = wr.get_outputpos(); |
|
drifter01620@184
|
146 |
wr.outbase = (jitcode_t)spengine->AllocatePageMemory(CodeSize); |
|
drifter01620@184
|
147 |
spengine->SetReadWrite(wr.outbase); |
|
drifter01620@184
|
148 |
wr.outptr = wr.outbase; |
|
drifter01620@184
|
149 |
detour_trampoline = wr.outbase; |
|
drifter01620@184
|
150 |
goto jit_rewind; |
|
drifter01620@184
|
151 |
} |
|
drifter01620@184
|
152 |
|
|
drifter01620@184
|
153 |
spengine->SetReadExecute(wr.outbase); |
|
drifter01620@184
|
154 |
|
|
drifter01620@184
|
155 |
*trampoline = detour_trampoline; |
|
drifter01620@184
|
156 |
|
|
drifter01620@184
|
157 |
return true; |
|
drifter01620@184
|
158 |
} |
|
drifter01620@184
|
159 |
|
|
drifter01620@184
|
160 |
void CDetour::DeleteDetour() |
|
drifter01620@184
|
161 |
{ |
|
drifter01620@184
|
162 |
if (detoured) |
|
drifter01620@184
|
163 |
{ |
|
drifter01620@184
|
164 |
DisableDetour(); |
|
drifter01620@184
|
165 |
} |
|
drifter01620@184
|
166 |
|
|
drifter01620@184
|
167 |
if (detour_trampoline) |
|
drifter01620@184
|
168 |
{ |
|
drifter01620@184
|
169 |
/* Free the allocated trampoline memory */ |
|
drifter01620@184
|
170 |
spengine->FreePageMemory(detour_trampoline); |
|
drifter01620@184
|
171 |
detour_trampoline = NULL; |
|
drifter01620@184
|
172 |
} |
|
drifter01620@184
|
173 |
} |
|
drifter01620@184
|
174 |
|
|
drifter01620@184
|
175 |
void CDetour::EnableDetour() |
|
drifter01620@184
|
176 |
{ |
|
drifter01620@184
|
177 |
if (!detoured) |
|
drifter01620@184
|
178 |
{ |
|
drifter01620@184
|
179 |
DoGatePatch((unsigned char *)detour_address, &detour_callback); |
|
drifter01620@184
|
180 |
detoured = true; |
|
drifter01620@184
|
181 |
} |
|
drifter01620@184
|
182 |
} |
|
drifter01620@184
|
183 |
|
|
drifter01620@184
|
184 |
void CDetour::DisableDetour() |
|
drifter01620@184
|
185 |
{ |
|
drifter01620@184
|
186 |
if (detoured) |
|
drifter01620@184
|
187 |
{ |
|
drifter01620@184
|
188 |
/* Remove the patch */ |
|
drifter01620@184
|
189 |
ApplyPatch(detour_address, 0, &detour_restore, NULL); |
|
drifter01620@184
|
190 |
detoured = false; |
|
drifter01620@184
|
191 |
} |
|
drifter01620@184
|
192 |
} |