|
| 1 | +/** |
| 2 | +* vim: set ts=4 : |
| 3 | +* ============================================================================= |
| 4 | +* SourceMod |
| 5 | +* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. |
| 6 | +* ============================================================================= |
| 7 | +* |
| 8 | +* This program is free software; you can redistribute it and/or modify it under |
| 9 | +* the terms of the GNU General Public License, version 3.0, as published by the |
| 10 | +* Free Software Foundation. |
| 11 | +* |
| 12 | +* This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 | +* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 15 | +* details. |
| 16 | +* |
| 17 | +* You should have received a copy of the GNU General Public License along with |
| 18 | +* this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +* |
| 20 | +* As a special exception, AlliedModders LLC gives you permission to link the |
| 21 | +* code of this program (as well as its derivative works) to "Half-Life 2," the |
| 22 | +* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
| 23 | +* by the Valve Corporation. You must obey the GNU General Public License in |
| 24 | +* all respects for all other code used. Additionally, AlliedModders LLC grants |
| 25 | +* this exception to all derivative works. AlliedModders LLC defines further |
| 26 | +* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
| 27 | +* or <http://www.sourcemod.net/license.php>. |
| 28 | +* |
| 29 | +* Version: $Id$ |
| 30 | +*/ |
| 31 | + |
| 32 | +#include "detours.h" |
| 33 | +#include <asm/asm.h> |
| 34 | + |
| 35 | +CPageAlloc GenBuffer::ms_Allocator(16); |
| 36 | + |
| 37 | +CDetour *CDetourManager::CreateDetour(void *callbackfunction, void **trampoline, void *addr) |
| 38 | +{ |
| 39 | + CDetour *detour = new CDetour(callbackfunction, trampoline); |
| 40 | + if (detour) |
| 41 | + { |
| 42 | + if (!detour->Init(addr)) |
| 43 | + { |
| 44 | + delete detour; |
| 45 | + return NULL; |
| 46 | + } |
| 47 | + |
| 48 | + return detour; |
| 49 | + } |
| 50 | + |
| 51 | + return NULL; |
| 52 | +} |
| 53 | + |
| 54 | +CDetour::CDetour(void *callbackfunction, void **trampoline) |
| 55 | +{ |
| 56 | + enabled = false; |
| 57 | + detoured = false; |
| 58 | + detour_address = NULL; |
| 59 | + detour_trampoline = NULL; |
| 60 | + this->detour_callback = callbackfunction; |
| 61 | + this->trampoline = trampoline; |
| 62 | +} |
| 63 | + |
| 64 | +bool CDetour::Init(void *addr) |
| 65 | +{ |
| 66 | + detour_address = addr; |
| 67 | + |
| 68 | + if (!CreateDetour()) |
| 69 | + { |
| 70 | + enabled = false; |
| 71 | + return enabled; |
| 72 | + } |
| 73 | + |
| 74 | + enabled = true; |
| 75 | + |
| 76 | + return enabled; |
| 77 | +} |
| 78 | + |
| 79 | +void CDetour::Destroy() |
| 80 | +{ |
| 81 | + DeleteDetour(); |
| 82 | + delete this; |
| 83 | +} |
| 84 | + |
| 85 | +bool CDetour::IsEnabled() |
| 86 | +{ |
| 87 | + return enabled; |
| 88 | +} |
| 89 | + |
| 90 | +void *CDetour::GetTargetAddr() |
| 91 | +{ |
| 92 | + return detour_address; |
| 93 | +} |
| 94 | + |
| 95 | +bool CDetour::CreateDetour() |
| 96 | +{ |
| 97 | + if (!detour_address) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /* |
| 103 | + * Determine how many bytes to save from target function. |
| 104 | + * We want 5 for our detour jmp, but it could require more. |
| 105 | + */ |
| 106 | + detour_restore.bytes = copy_bytes((unsigned char *)detour_address, NULL, OP_JMP_SIZE); |
| 107 | + |
| 108 | + /* First, save restore bits */ |
| 109 | + memcpy(detour_restore.patch, (unsigned char *)detour_address, detour_restore.bytes); |
| 110 | + |
| 111 | + /* Patch old bytes in */ |
| 112 | + codegen.alloc(detour_restore.bytes); |
| 113 | + copy_bytes((unsigned char *)detour_address, codegen.GetData(), detour_restore.bytes); |
| 114 | + |
| 115 | + /* Return to the original function */ |
| 116 | + jitoffs_t call = IA32_Jump_Imm32(&codegen, 0); |
| 117 | + IA32_Write_Jump32_Abs(&codegen, call, (unsigned char *)detour_address + detour_restore.bytes); |
| 118 | + |
| 119 | + codegen.SetRE(); |
| 120 | + |
| 121 | + *trampoline = codegen.GetData(); |
| 122 | + |
| 123 | + return true; |
| 124 | +} |
| 125 | + |
| 126 | +void CDetour::DeleteDetour() |
| 127 | +{ |
| 128 | + if (detoured) |
| 129 | + { |
| 130 | + DisableDetour(); |
| 131 | + } |
| 132 | + |
| 133 | + if (detour_trampoline) |
| 134 | + { |
| 135 | + /* Free the allocated trampoline memory */ |
| 136 | + codegen.clear(); |
| 137 | + detour_trampoline = NULL; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +void CDetour::EnableDetour() |
| 142 | +{ |
| 143 | + if (!detoured) |
| 144 | + { |
| 145 | + DoGatePatch((unsigned char *)detour_address, detour_callback); |
| 146 | + detoured = true; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void CDetour::DisableDetour() |
| 151 | +{ |
| 152 | + if (detoured) |
| 153 | + { |
| 154 | + /* Remove the patch */ |
| 155 | + ApplyPatch(detour_address, 0, &detour_restore, NULL); |
| 156 | + detoured = false; |
| 157 | + } |
| 158 | +} |
0 commit comments