1.1 --- a/sourcehook/sourcehook.cpp Thu Apr 28 17:24:39 2005 +0000
1.2 +++ b/sourcehook/sourcehook.cpp Fri Apr 29 20:29:46 2005 +0000
1.3 @@ -16,93 +16,38 @@
1.4 #include <algorithm>
1.5 #include "sourcehook_impl.h"
1.6
1.7 -#if SH_SYS == SH_SYS_WIN32
1.8 -# include <windows.h>
1.9 -#elif SH_SYS == SH_SYS_LINUX
1.10 -# include <sys/mman.h>
1.11 -# include <limits.h>
1.12 -# include <unistd.h>
1.13 -# ifndef PAGESIZE
1.14 -# define PAGESIZE 4096
1.15 -# endif
1.16 -#endif
1.17 -
1.18 -#if SH_RUNTIME_CODEGEN == 1
1.19 -# if SH_COMP == SH_COMP_GCC
1.20 -# define SH_CCC_CODESIZE 15
1.21 - // :TODO:
1.22 - //void SH_CCC_MakeGate(void *origthisptr, unsigned char* &ptr, void *origvtable, int i)
1.23 - //{
1.24 - //}
1.25 -# elif SH_COMP == SH_COMP_MSVC
1.26 -# define SH_CCC_CODESIZE 19
1.27 - void SH_CCC_MakeGate(void *origthisptr, void *ccthisptr, unsigned char* ptr, void *origfunc)
1.28 - {
1.29 - // --Subtract old this pointer to get the offset
1.30 - // 00417EDF 81 E9 A1 7B 33 01 sub ecx,1337BA1h
1.31 - *ptr++ = 0x81;
1.32 - *ptr++ = 0xE9;
1.33 - *reinterpret_cast<void**>(ptr) = ccthisptr;
1.34 - ptr += sizeof(void*);
1.35 - // --Add it to the new this pointer
1.36 - // 00417EE5 81 C1 37 13 37 13 add ecx,13371337h
1.37 - *ptr++ = 0x81;
1.38 - *ptr++ = 0xC1;
1.39 - *reinterpret_cast<void**>(ptr) = origthisptr;
1.40 - ptr += sizeof(void*);
1.41 - // --Prepare address
1.42 - // 00417EEB B8 EF BE AD DE mov eax,0DEADBEEFh
1.43 - *ptr++ = 0xB8;
1.44 - *reinterpret_cast<void**>(ptr) = origfunc;
1.45 - ptr += sizeof(void*);
1.46 - // -- Do actual jump
1.47 - // 00417EF0 FF E0 jmp eax
1.48 - *ptr++ = 0xFF;
1.49 - *ptr++ = 0xE0;
1.50 - }
1.51 -# endif
1.52 -#endif
1.53 -
1.54 -
1.55 namespace SourceHook
1.56 {
1.57 CSourceHookImpl::CSourceHookImpl()
1.58 {
1.59 - // Get page size
1.60 -#if SH_SYS == SH_SYS_WIN32
1.61 - SYSTEM_INFO sysinfo;
1.62 - GetSystemInfo(&sysinfo);
1.63 - m_PageSize = sysinfo.dwPageSize;
1.64 -#elif SH_SYS == SH_SYS_LINUX
1.65 - m_PageSize = PAGESIZE;
1.66 -#else
1.67 -# error Unsupported system
1.68 -#endif
1.69 }
1.70
1.71 CSourceHookImpl::~CSourceHookImpl()
1.72 {
1.73 -
1.74 }
1.75
1.76 bool CSourceHookImpl::IsPluginInUse(Plugin plug)
1.77 {
1.78 // Iterate through all hook managers which are in this plugin
1.79 - // Iterate through their hooks
1.80 + // Iterate through their vfnptrs, ifaces, hooks
1.81 // If a hook from an other plugin is found, return true
1.82 // Return false otherwise
1.83 #define TMP_CHECK_LIST(name) \
1.84 - for (iter3 = iter2->name.begin(); iter3 != iter2->name.end(); ++iter3) \
1.85 - if (iter3->plug == plug) \
1.86 + for (hook_iter = iface_iter->name.begin(); hook_iter != iface_iter->name.end(); ++hook_iter) \
1.87 + if (hook_iter->plug == plug) \
1.88 return true;
1.89
1.90 - for (HookManInfoList::iterator iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
1.91 + for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
1.92 {
1.93 - if (iter->plug == plug && !iter->ifaces.empty())
1.94 + if (hmil_iter->plug != plug)
1.95 + continue;
1.96 + for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->vfnptrs.begin();
1.97 + vfnptr_iter != hmil_iter->vfnptrs.end(); ++vfnptr_iter)
1.98 {
1.99 - for (std::list<HookManagerInfo::Iface>::iterator iter2 = iter->ifaces.begin(); iter2 != iter->ifaces.end(); ++iter2)
1.100 + for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
1.101 + iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
1.102 {
1.103 - std::list<HookManagerInfo::Iface::Hook>::iterator iter3;
1.104 + std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hook_iter;
1.105 TMP_CHECK_LIST(hooks_pre);
1.106 TMP_CHECK_LIST(hooks_post);
1.107 }
1.108 @@ -116,24 +61,29 @@
1.109 {
1.110 // 1) Manually remove all hooks by this plugin
1.111 std::list<RemoveHookInfo> hookstoremove;
1.112 -
1.113 - for (HookManInfoList::iterator iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
1.114 +#define TMP_CHECK_LIST(name, ispost) \
1.115 + for (hook_iter = iface_iter->name.begin(); hook_iter != iface_iter->name.end(); ++hook_iter) \
1.116 + if (hook_iter->plug == plug) \
1.117 + hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->ptr, \
1.118 + hmil_iter->func, hook_iter->handler, ispost))
1.119 + for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
1.120 {
1.121 - for (std::list<HookManagerInfo::Iface>::iterator iter2 = iter->ifaces.begin(); iter2 != iter->ifaces.end();
1.122 - ++iter2)
1.123 + for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->vfnptrs.begin();
1.124 + vfnptr_iter != hmil_iter->vfnptrs.end(); ++vfnptr_iter)
1.125 {
1.126 - for (std::list<HookManagerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_pre.begin(); iter3 != iter2->hooks_pre.end(); ++iter3)
1.127 - if (iter3->plug == plug)
1.128 - hookstoremove.push_back(RemoveHookInfo(iter3->plug, iter2->ptr, iter->func, iter3->handler, false));
1.129 -
1.130 - for (std::list<HookManagerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_post.begin(); iter3 != iter2->hooks_post.end(); ++iter3)
1.131 - if (iter3->plug == plug)
1.132 - hookstoremove.push_back(RemoveHookInfo(iter3->plug, iter2->ptr, iter->func, iter3->handler, true));
1.133 + for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
1.134 + iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
1.135 + {
1.136 + std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hook_iter;
1.137 + TMP_CHECK_LIST(hooks_pre, false);
1.138 + TMP_CHECK_LIST(hooks_post, true);
1.139 + }
1.140 }
1.141 }
1.142 +#undef TMP_CHECK_LIST
1.143
1.144 for (std::list<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
1.145 - RemoveHook(rmiter->plug, rmiter->iface, rmiter->hpf, rmiter->handler, rmiter->post);
1.146 + RemoveHook(*rmiter);
1.147
1.148 // 2) Other plugins may use hook managers in this plugin.
1.149 // Get a list of hook managers that are in this plugin and are used by other plugins
1.150 @@ -141,16 +91,16 @@
1.151
1.152 HookManInfoList tmphookmans;
1.153 bool erase = false;
1.154 - for (HookManInfoList::iterator iter = m_HookMans.begin(); iter != m_HookMans.end();
1.155 - erase ? iter=m_HookMans.erase(iter) : ++iter)
1.156 + for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end();
1.157 + erase ? hmil_iter=m_HookMans.erase(hmil_iter) : ++hmil_iter)
1.158 {
1.159 - if (iter->plug == plug)
1.160 + if (hmil_iter->plug == plug)
1.161 {
1.162 - if (!iter->ifaces.empty())
1.163 + if (!hmil_iter->vfnptrs.empty())
1.164 {
1.165 // All hooks by this plugin are already removed
1.166 - // So if there is an iface, it has to be used by an other plugin
1.167 - tmphookmans.push_back(*iter);
1.168 + // So if there is a vfnptr, it has to be used by an other plugin
1.169 + tmphookmans.push_back(*hmil_iter);
1.170 }
1.171 erase = true;
1.172 }
1.173 @@ -159,29 +109,28 @@
1.174 }
1.175
1.176 // For each hook manager:
1.177 - for (HookManInfoList::iterator iter = tmphookmans.begin(); iter != tmphookmans.end(); ++iter)
1.178 + for (HookManInfoList::iterator hmil_iter = tmphookmans.begin(); hmil_iter != tmphookmans.end(); ++hmil_iter)
1.179 {
1.180 // Find a suitable hook manager in an other plugin
1.181 HookManInfoList::iterator newHookMan = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
1.182 - iter->proto, iter->vtbl_offs, iter->vtbl_idx, iter->thisptr_offs);
1.183 - if (newHookMan == m_HookMans.end())
1.184 - {
1.185 - // This should _never_ happen.
1.186 - // If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
1.187 - SH_ASSERT(0);
1.188 - }
1.189 + hmil_iter->proto, hmil_iter->vtbl_offs, hmil_iter->vtbl_idx);
1.190 +
1.191 + // This should _never_ happen.
1.192 + // If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
1.193 + SH_ASSERT(newHookMan != m_HookMans.end(),
1.194 + "Could not find a suitable hook manager in an other plugin!");
1.195
1.196 - // AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo/thisptroffs
1.197 - SH_ASSERT(newHookMan->plug != plug);
1.198 + // AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
1.199 + SH_ASSERT(newHookMan->plug != plug, "New hook manager from same plugin!");
1.200
1.201 - // The first hooker should be always used - so the new hook manager has to be empty
1.202 - SH_ASSERT(newHookMan->ifaces.empty());
1.203 + // The first hook manager should be always used - so the new hook manager has to be empty
1.204 + SH_ASSERT(newHookMan->vfnptrs.empty(), "New hook manager not empty!");
1.205
1.206 - // Move the ifaces from the old hook manager to the new one
1.207 - newHookMan->ifaces = iter->ifaces;
1.208 + // Move the vfnptrs from the old hook manager to the new one
1.209 + newHookMan->vfnptrs = hmil_iter->vfnptrs;
1.210
1.211 // Unregister the old one, register the new one
1.212 - iter->func(HA_Unregister, NULL);
1.213 + hmil_iter->func(HA_Unregister, NULL);
1.214 newHookMan->func(HA_Register, &(*newHookMan));
1.215 }
1.216 }
1.217 @@ -189,43 +138,49 @@
1.218 void CSourceHookImpl::CompleteShutdown()
1.219 {
1.220 std::list<RemoveHookInfo> hookstoremove;
1.221 -
1.222 - for (HookManInfoList::iterator iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
1.223 +#define TMP_CHECK_LIST(name, ispost) \
1.224 + for (hook_iter = iface_iter->name.begin(); hook_iter != iface_iter->name.end(); ++hook_iter) \
1.225 + hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->ptr, \
1.226 + hmil_iter->func, hook_iter->handler, ispost))
1.227 + for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
1.228 {
1.229 - for (std::list<HookManagerInfo::Iface>::iterator iter2 = iter->ifaces.begin(); iter2 != iter->ifaces.end();
1.230 - ++iter2)
1.231 + for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->vfnptrs.begin();
1.232 + vfnptr_iter != hmil_iter->vfnptrs.end(); ++vfnptr_iter)
1.233 {
1.234 - for (std::list<HookManagerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_pre.begin(); iter3 != iter2->hooks_pre.end(); ++iter3)
1.235 - hookstoremove.push_back(RemoveHookInfo(iter3->plug, iter2->ptr, iter->func, iter3->handler, false));
1.236 -
1.237 - for (std::list<HookManagerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_post.begin(); iter3 != iter2->hooks_post.end(); ++iter3)
1.238 - hookstoremove.push_back(RemoveHookInfo(iter3->plug, iter2->ptr, iter->func, iter3->handler, true));
1.239 + for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
1.240 + iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
1.241 + {
1.242 + std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hook_iter;
1.243 + TMP_CHECK_LIST(hooks_pre, false);
1.244 + TMP_CHECK_LIST(hooks_post, true);
1.245 + }
1.246 }
1.247 }
1.248 +#undef TMP_CHECK_LIST
1.249
1.250 for (std::list<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
1.251 - RemoveHook(rmiter->plug, rmiter->iface, rmiter->hpf, rmiter->handler, rmiter->post);
1.252 + RemoveHook(*rmiter);
1.253
1.254 m_HookMans.clear();
1.255 }
1.256
1.257 - bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int ifacesize, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.258 + bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.259 {
1.260 + void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
1.261 // 1) Get info about the hook manager
1.262 HookManagerInfo tmp;
1.263 if (myHookMan(HA_GetInfo, &tmp) != 0)
1.264 return false;
1.265
1.266 // Add the proposed hook manager to the _end_ of the list if the plugin doesn't have a hook manager with this proto/vo/vi registered
1.267 - HookManInfoList::iterator hookmaniter;
1.268 - for (hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
1.269 + HookManInfoList::iterator hkmi_iter;
1.270 + for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
1.271 {
1.272 - if (hookmaniter->plug == plug && strcmp(hookmaniter->proto, tmp.proto) == 0 &&
1.273 - hookmaniter->vtbl_offs == tmp.vtbl_offs && hookmaniter->vtbl_idx == tmp.vtbl_idx &&
1.274 - hookmaniter->thisptr_offs == tmp.thisptr_offs)
1.275 + if (hkmi_iter->plug == plug && strcmp(hkmi_iter->proto, tmp.proto) == 0 &&
1.276 + hkmi_iter->vtbl_offs == tmp.vtbl_offs && hkmi_iter->vtbl_idx == tmp.vtbl_idx)
1.277 break;
1.278 }
1.279 - if (hookmaniter == m_HookMans.end())
1.280 + if (hkmi_iter == m_HookMans.end())
1.281 {
1.282 // No such hook manager from this plugin yet, add it!
1.283 tmp.func = myHookMan;
1.284 @@ -234,47 +189,70 @@
1.285 }
1.286
1.287 // Then, search for a suitable hook manager (from the beginning)
1.288 - HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(), tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx, tmp.thisptr_offs);
1.289 - SH_ASSERT(hookman != m_HookMans.end());
1.290 + HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(), tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx);
1.291 + SH_ASSERT(hookman != m_HookMans.end(), "No hookman found - but if there was none, we've just added one!");
1.292
1.293 // Tell it to store the pointer if it's not already active
1.294 - if (hookman->ifaces.empty())
1.295 + if (hookman->vfnptrs.empty())
1.296 hookman->func(HA_Register, &(*hookman));
1.297
1.298 - std::list<HookManagerInfo::Iface>::iterator ifsiter = std::find(hookman->ifaces.begin(), hookman->ifaces.end(), iface);
1.299 + void **cur_vtptr = *reinterpret_cast<void***>(
1.300 + reinterpret_cast<char*>(adjustediface) + tmp.vtbl_offs);
1.301 + void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.vtbl_idx);
1.302
1.303 - if (ifsiter == hookman->ifaces.end())
1.304 + HookManagerInfo::VfnPtrListIter vfnptr_iter = std::find(
1.305 + hookman->vfnptrs.begin(), hookman->vfnptrs.end(), cur_vfnptr);
1.306 +
1.307 + if (vfnptr_iter == hookman->vfnptrs.end())
1.308 {
1.309 - HookManagerInfo::Iface ifs;
1.310 - ifs.ptr = iface;
1.311 - ifs.callclass = GetCallClass(iface, ifacesize);
1.312 - void *vtableptr = *reinterpret_cast<void**>(reinterpret_cast<char*>(iface) + hookman->vtbl_offs);
1.313 - SetMemAccess(vtableptr, sizeof(void*) * hookman->vtbl_idx, SH_MEM_READ | SH_MEM_WRITE);
1.314 - ifs.orig_entry = (*reinterpret_cast<void***>(reinterpret_cast<char*>(iface) + hookman->vtbl_offs))[hookman->vtbl_idx];
1.315 - (*reinterpret_cast<void***>(reinterpret_cast<char*>(iface) + hookman->vtbl_offs))[hookman->vtbl_idx] =
1.316 - (*reinterpret_cast<void***>(reinterpret_cast<char*>(hookman->hookfunc_inst) + hookman->hookfunc_vtbl_offs))[hookman->hookfunc_vtbl_idx];
1.317 - hookman->ifaces.push_back(ifs);
1.318 - ifsiter = hookman->ifaces.end();
1.319 - --ifsiter;
1.320 + // Add a new one
1.321 + HookManagerInfo::VfnPtr vfp;
1.322 + vfp.vfnptr = cur_vfnptr;
1.323 + vfp.orig_entry = *reinterpret_cast<void**>(cur_vfnptr);
1.324 + hookman->vfnptrs.push_back(vfp);
1.325 +
1.326 + // Alter vtable entry
1.327 + SetMemAccess(cur_vtptr, sizeof(void*) * tmp.vtbl_idx, SH_MEM_READ | SH_MEM_WRITE);
1.328 + *reinterpret_cast<void**>(cur_vfnptr) = *reinterpret_cast<void**>(hookman->hookfunc_vfnptr);
1.329 +
1.330 + // Make vfnptr_iter point to the new element
1.331 + vfnptr_iter = hookman->vfnptrs.end();
1.332 + --vfnptr_iter;
1.333 }
1.334 - HookManagerInfo::Iface::Hook hookinfo;
1.335 +
1.336 + HookManagerInfo::VfnPtr::IfaceListIter iface_iter = std::find(
1.337 + vfnptr_iter->ifaces.begin(), vfnptr_iter->ifaces.end(), adjustediface);
1.338 + if (iface_iter == vfnptr_iter->ifaces.end())
1.339 + {
1.340 + // Add a new one
1.341 + HookManagerInfo::VfnPtr::Iface ifs;
1.342 + ifs.ptr = adjustediface;
1.343 + vfnptr_iter->ifaces.push_back(ifs);
1.344 +
1.345 + // Make iface_iter point to the new element
1.346 + iface_iter = vfnptr_iter->ifaces.end();
1.347 + --iface_iter;
1.348 + }
1.349 +
1.350 + // Add the hook
1.351 + HookManagerInfo::VfnPtr::Iface::Hook hookinfo;
1.352 hookinfo.handler = handler;
1.353 hookinfo.plug = plug;
1.354 hookinfo.paused = false;
1.355 if (post)
1.356 - ifsiter->hooks_post.push_back(hookinfo);
1.357 + iface_iter->hooks_post.push_back(hookinfo);
1.358 else
1.359 - ifsiter->hooks_pre.push_back(hookinfo);
1.360 + iface_iter->hooks_pre.push_back(hookinfo);
1.361
1.362
1.363 // Now that it is done, check whether we have to update any callclasses
1.364 for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
1.365 {
1.366 - if (cciter->iface == iface)
1.367 + if (cciter->cc.ptr == iface)
1.368 {
1.369 - ApplyCallClassPatch(*cciter, tmp.vtbl_offs, tmp.vtbl_idx, ifsiter->orig_entry);
1.370 + ApplyCallClassPatch(*cciter, tmp.vtbl_offs, tmp.vtbl_idx, vfnptr_iter->orig_entry);
1.371
1.372 - // We can assume that there is no more callclass with the same iface
1.373 + // We can assume that there is no more callclass for the same iface
1.374 break;
1.375 }
1.376 }
1.377 @@ -282,7 +260,18 @@
1.378 return true;
1.379 }
1.380
1.381 - bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.382 + bool CSourceHookImpl::RemoveHook(RemoveHookInfo info)
1.383 + {
1.384 + return RemoveHook(info.plug, info.iface, info.hookman, info.handler, info.post);
1.385 + }
1.386 +
1.387 + bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.388 + {
1.389 + return RemoveHook(plug, reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs),
1.390 + myHookMan, handler, post);
1.391 + }
1.392 +
1.393 + bool CSourceHookImpl::RemoveHook(Plugin plug, void *adjustediface, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.394 {
1.395 HookManagerInfo tmp;
1.396 if (myHookMan(HA_GetInfo, &tmp) != 0)
1.397 @@ -290,196 +279,110 @@
1.398
1.399 // Find the hook manager and the hook
1.400 HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
1.401 - tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx, tmp.thisptr_offs);
1.402 + tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx);
1.403 if (hookman == m_HookMans.end())
1.404 return false;
1.405
1.406 - for (std::list<HookManagerInfo::Iface>::iterator ifaceiter = hookman->ifaces.begin(); ifaceiter != hookman->ifaces.end(); ++ifaceiter)
1.407 + void **cur_vtptr = *reinterpret_cast<void***>(
1.408 + reinterpret_cast<char*>(adjustediface) + tmp.vtbl_offs);
1.409 + void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.vtbl_idx);
1.410 +
1.411 + for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->vfnptrs.begin();
1.412 + vfnptr_iter != hookman->vfnptrs.end(); ++vfnptr_iter)
1.413 {
1.414 - if (ifaceiter->ptr == iface)
1.415 + if (vfnptr_iter->vfnptr == cur_vfnptr)
1.416 {
1.417 - std::list<HookManagerInfo::Iface::Hook> &hooks = post ? ifaceiter->hooks_post : ifaceiter->hooks_pre;
1.418 - bool erase;
1.419 - for (std::list<HookManagerInfo::Iface::Hook>::iterator hookiter = hooks.begin();
1.420 - hookiter != hooks.end(); erase ? hookiter = hooks.erase(hookiter) : ++hookiter)
1.421 - {
1.422 - erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler);
1.423 - if (erase)
1.424 - hookiter->handler->DeleteThis(); // Make the _plugin_ delete the handler object
1.425 - }
1.426 - if (ifaceiter->hooks_post.empty() && ifaceiter->hooks_pre.empty())
1.427 + for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
1.428 + iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
1.429 {
1.430 - // Deactivate the hook
1.431 - (*reinterpret_cast<void***>(reinterpret_cast<char*>(ifaceiter->ptr) +
1.432 - hookman->vtbl_offs))[hookman->vtbl_idx]= ifaceiter->orig_entry;
1.433 -
1.434 - // Release the callclass
1.435 - ReleaseCallClass(ifaceiter->callclass);
1.436 + std::list<HookManagerInfo::VfnPtr::Iface::Hook> &hooks =
1.437 + post ? iface_iter->hooks_post : iface_iter->hooks_pre;
1.438
1.439 - // Remove the iface info
1.440 - hookman->ifaces.erase(ifaceiter);
1.441 + bool erase;
1.442 + for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = hooks.begin();
1.443 + hookiter != hooks.end(); erase ? hookiter = hooks.erase(hookiter) : ++hookiter)
1.444 + {
1.445 + erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler);
1.446 + if (erase)
1.447 + hookiter->handler->DeleteThis(); // Make the _plugin_ delete the handler object
1.448 + }
1.449 + if (iface_iter->hooks_post.empty() && iface_iter->hooks_pre.empty())
1.450 + {
1.451 + vfnptr_iter->ifaces.erase(iface_iter);
1.452 + if (vfnptr_iter->ifaces.empty())
1.453 + {
1.454 + // Deactivate the hook
1.455 + *reinterpret_cast<void**>(vfnptr_iter->vfnptr) = vfnptr_iter->orig_entry;
1.456 +
1.457 + hookman->vfnptrs.erase(vfnptr_iter);
1.458
1.459 - if (hookman->ifaces.empty())
1.460 - hookman->func(HA_Unregister, NULL);
1.461 + if (hookman->vfnptrs.empty())
1.462 + {
1.463 + // Unregister the hook manager
1.464 + hookman->func(HA_Unregister, NULL);
1.465 + }
1.466 + }
1.467 + }
1.468 }
1.469 - // :TODO: Better return value? Or none?
1.470 return true;
1.471 }
1.472 }
1.473 return false;
1.474 }
1.475
1.476 - void *CSourceHookImpl::GetCallClass(void *iface, size_t size)
1.477 + GenericCallClass *CSourceHookImpl::GetCallClass(void *iface)
1.478 {
1.479 for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
1.480 {
1.481 - if (cciter->iface == iface)
1.482 + if (cciter->cc.ptr == iface)
1.483 {
1.484 ++cciter->refcounter;
1.485 - return cciter->ptr;
1.486 + return &cciter->cc;
1.487 }
1.488 }
1.489
1.490 - // The layout of callclasses is:
1.491 - // Copy of the class; vtable entries pointing to gates
1.492 - // Pointer to the corresponding CallClass object
1.493 - // Copy of the class; vtable entries pointing to original functions
1.494 + CallClassInfo tmp;
1.495 + tmp.refcounter = 1;
1.496 + tmp.cc.ptr = iface;
1.497
1.498 - CallClass tmp;
1.499 - char *ptr = new char[size * 2 + sizeof(void*)];
1.500 - tmp.ptr = reinterpret_cast<void*>(ptr);
1.501 - memcpy(reinterpret_cast<void*>(ptr), iface, size);
1.502 - memcpy(reinterpret_cast<void*>(ptr + size + sizeof(void*)), iface, size);
1.503 - tmp.iface = iface;
1.504 - tmp.size = size;
1.505 - tmp.refcounter = 1;
1.506 -
1.507 - // Go through _all_ hooks and apply any needed patches
1.508 for (HookManInfoList::iterator hookman = m_HookMans.begin(); hookman != m_HookMans.end(); ++hookman)
1.509 - {
1.510 - for (std::list<HookManagerInfo::Iface>::iterator ifaceiter = hookman->ifaces.begin(); ifaceiter != hookman->ifaces.end(); ++ifaceiter)
1.511 - {
1.512 - if (ifaceiter->ptr == iface)
1.513 - {
1.514 - if (!ApplyCallClassPatch(tmp, hookman->vtbl_offs, hookman->vtbl_idx, ifaceiter->orig_entry))
1.515 - {
1.516 - FreeCallClass(tmp);
1.517 - return NULL;
1.518 - }
1.519 - }
1.520 - }
1.521 - }
1.522 + for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->vfnptrs.begin();
1.523 + vfnptr_iter != hookman->vfnptrs.end(); ++vfnptr_iter)
1.524 + for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
1.525 + iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
1.526 + if (iface_iter->ptr == iface)
1.527 + ApplyCallClassPatch(tmp, hookman->vtbl_offs, hookman->vtbl_idx,
1.528 + vfnptr_iter->orig_entry);
1.529 +
1.530 m_CallClasses.push_back(tmp);
1.531 - // The object has to be followed by the pointer to the vtable info object
1.532 - *reinterpret_cast<void**>(ptr + size) = &m_CallClasses.back();
1.533 - return tmp.ptr;
1.534 + return &m_CallClasses.back().cc;
1.535 }
1.536
1.537 - void CSourceHookImpl::ReleaseCallClass(void *ptr)
1.538 + void CSourceHookImpl::ReleaseCallClass(GenericCallClass *ptr)
1.539 {
1.540 Impl_CallClassList::iterator iter = std::find(m_CallClasses.begin(), m_CallClasses.end(), ptr);
1.541 if (iter == m_CallClasses.end())
1.542 return;
1.543 --iter->refcounter;
1.544 if (iter->refcounter < 1)
1.545 - {
1.546 - FreeCallClass(*iter);
1.547 m_CallClasses.erase(iter);
1.548 - }
1.549 - }
1.550 -
1.551 - void CSourceHookImpl::FreeCallClass(CallClass &cc)
1.552 - {
1.553 - for (CallClass::VTableList::iterator vtbliter = cc.vtables.begin(); vtbliter != cc.vtables.end(); ++vtbliter)
1.554 - {
1.555 -#if SH_RUNTIME_CODEGEN == 1
1.556 - // Delete generated callgates
1.557 - for (std::list<int>::iterator cgiter = vtbliter->patches.begin(); cgiter != vtbliter->patches.end(); ++cgiter)
1.558 - {
1.559 - char *cgptr = reinterpret_cast<char**>(vtbliter->ptr)[*cgiter];
1.560 - delete [] cgptr;
1.561 - }
1.562 -#endif
1.563 - delete [] reinterpret_cast<char*>(vtbliter->ptr);
1.564 - }
1.565 - delete [] reinterpret_cast<char*>(cc.ptr);
1.566 }
1.567
1.568 - bool CSourceHookImpl::ApplyCallClassPatch(CallClass &cc, int vtbl_offs, int vtbl_idx, void *orig_entry)
1.569 + void CSourceHookImpl::ApplyCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx, void *orig_entry)
1.570 {
1.571 - char *ptr = reinterpret_cast<char*>(cc.ptr);
1.572 - void *vtable = *reinterpret_cast<void**>(ptr + vtbl_offs);
1.573 -
1.574 - // Check whether we already know that vtable
1.575 - CallClass::VTableList::iterator vtbliter = std::find(cc.vtables.begin(),
1.576 - cc.vtables.end(), reinterpret_cast<void*>(vtable));
1.577 - int actvtablesize=MAX_VTABLE_LEN;
1.578 - if (vtbliter == cc.vtables.end())
1.579 - {
1.580 - CallClass::VTable newvtableinfo;
1.581 - int pagesnum = (MAX_VTABLE_LEN % m_PageSize == 0) ? MAX_VTABLE_LEN / m_PageSize : MAX_VTABLE_LEN / m_PageSize + 1;
1.582 - // Set all pages in the range to readwrite
1.583 - // :TODO: Fix this!
1.584 - char *pagebegin = reinterpret_cast<char*>(vtable) - (reinterpret_cast<int>(vtable) % m_PageSize);
1.585 - for (int ipage = 0; ipage < pagesnum; ++ipage)
1.586 - {
1.587 - if (!SetMemAccess(reinterpret_cast<void*>(pagebegin + ipage * m_PageSize), 1,
1.588 - SH_MEM_READ | SH_MEM_WRITE))
1.589 - {
1.590 - // We can't go here anymore
1.591 - actvtablesize = static_cast<int>((pagebegin + ipage * m_PageSize) - reinterpret_cast<char*>(vtable));
1.592 - break;
1.593 - }
1.594 - }
1.595 - if (actvtablesize < 1)
1.596 - {
1.597 - // We can't access the vtable -> Quit
1.598 - return false;
1.599 - }
1.600 - // Create a new vtable
1.601 - newvtableinfo.ptr = reinterpret_cast<void*>(new char[actvtablesize]);
1.602 - // Fill it with the information from the old vtable
1.603 - memcpy(newvtableinfo.ptr, vtable, actvtablesize);
1.604 - newvtableinfo.actsize = actvtablesize;
1.605 - // Set the pointer in the object and add it to the list of already known vtables
1.606 - *reinterpret_cast<void**>(ptr + vtbl_offs) = newvtableinfo.ptr;
1.607 - cc.vtables.push_back(newvtableinfo);
1.608 -
1.609 - vtbliter = cc.vtables.end();
1.610 - --vtbliter;
1.611 - // :TODO: When not codegen, patch the other vtable?
1.612 - }
1.613 -
1.614 - // Check whether we already have this patch
1.615 - if (std::find(vtbliter->patches.begin(), vtbliter->patches.end(), vtbl_idx) == vtbliter->patches.end())
1.616 - {
1.617 - // No -> apply it
1.618 -
1.619 - // Get a call gate
1.620 - void *callgate = NULL;
1.621 -#if SH_RUNTIME_CODEGEN == 1
1.622 - unsigned char *cggen = new unsigned char[SH_CCC_CODESIZE];
1.623 - //SH_CCC_MakeGate(cc.iface, cc.ptr, cggen, orig_entry);
1.624 - callgate = (void*)cggen;
1.625 - SetMemAccess(callgate, SH_CCC_CODESIZE, SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC);
1.626 -#else
1.627 - // :TODO:
1.628 -#error Not supported yet!
1.629 -#endif
1.630 - vtbliter->patches.push_back(vtbl_idx);
1.631 - reinterpret_cast<void**>(vtbliter->ptr)[vtbl_idx] = callgate;
1.632 - }
1.633 - return true;
1.634 + OrigFuncs &tmpvec = cc.cc.vt[vtbl_offs];
1.635 + if (tmpvec.size() <= (size_t)vtbl_idx)
1.636 + tmpvec.resize(vtbl_idx+1);
1.637 + tmpvec[vtbl_idx] = orig_entry;
1.638 }
1.639
1.640 -
1.641 CSourceHookImpl::HookManInfoList::iterator CSourceHookImpl::FindHookMan(HookManInfoList::iterator begin,
1.642 - HookManInfoList::iterator end, const char *proto, int vtblofs, int vtblidx, int thisptrofs)
1.643 + HookManInfoList::iterator end, const char *proto, int vtblofs, int vtblidx)
1.644 {
1.645 for (HookManInfoList::iterator hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
1.646 {
1.647 - if (strcmp(hookmaniter->proto, proto) == 0 && hookmaniter->vtbl_offs == vtblofs && hookmaniter->vtbl_idx == vtblidx &&
1.648 - hookmaniter->thisptr_offs == thisptrofs)
1.649 + if (strcmp(hookmaniter->proto, proto) == 0 && hookmaniter->vtbl_offs == vtblofs &&
1.650 + hookmaniter->vtbl_idx == vtblidx)
1.651 break;
1.652 }
1.653 return hookmaniter;
1.654 @@ -489,38 +392,42 @@
1.655 {
1.656 // Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to paused
1.657 for (HookManInfoList::iterator hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
1.658 - for (std::list<HookManagerInfo::Iface>::iterator ifaceiter = hookmaniter->ifaces.begin();
1.659 - ifaceiter != hookmaniter->ifaces.end(); ++ifaceiter)
1.660 - {
1.661 - for (std::list<HookManagerInfo::Iface::Hook>::iterator hookiter = ifaceiter->hooks_pre.begin();
1.662 - hookiter != ifaceiter->hooks_pre.end(); ++hookiter)
1.663 - if (plug == hookiter->plug)
1.664 - hookiter->paused = true;
1.665 + for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->vfnptrs.begin();
1.666 + vfnptr_iter != hookmaniter->vfnptrs.end(); ++vfnptr_iter)
1.667 + for (HookManagerInfo::VfnPtr::IfaceListIter ifaceiter = vfnptr_iter->ifaces.begin();
1.668 + ifaceiter != vfnptr_iter->ifaces.end(); ++ifaceiter)
1.669 + {
1.670 + for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_pre.begin();
1.671 + hookiter != ifaceiter->hooks_pre.end(); ++hookiter)
1.672 + if (plug == hookiter->plug)
1.673 + hookiter->paused = true;
1.674
1.675 - for (std::list<HookManagerInfo::Iface::Hook>::iterator hookiter = ifaceiter->hooks_post.begin();
1.676 - hookiter != ifaceiter->hooks_post.end(); ++hookiter)
1.677 - if (plug == hookiter->plug)
1.678 - hookiter->paused = true;
1.679 - }
1.680 + for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_post.begin();
1.681 + hookiter != ifaceiter->hooks_post.end(); ++hookiter)
1.682 + if (plug == hookiter->plug)
1.683 + hookiter->paused = true;
1.684 + }
1.685 }
1.686
1.687 void CSourceHookImpl::UnpausePlugin(Plugin plug)
1.688 {
1.689 - // Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to normal
1.690 + // Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to paused
1.691 for (HookManInfoList::iterator hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
1.692 - for (std::list<HookManagerInfo::Iface>::iterator ifaceiter = hookmaniter->ifaces.begin();
1.693 - ifaceiter != hookmaniter->ifaces.end(); ++ifaceiter)
1.694 - {
1.695 - for (std::list<HookManagerInfo::Iface::Hook>::iterator hookiter = ifaceiter->hooks_pre.begin();
1.696 - hookiter != ifaceiter->hooks_pre.end(); ++hookiter)
1.697 - if (plug == hookiter->plug)
1.698 - hookiter->paused = false;
1.699 + for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->vfnptrs.begin();
1.700 + vfnptr_iter != hookmaniter->vfnptrs.end(); ++vfnptr_iter)
1.701 + for (HookManagerInfo::VfnPtr::IfaceListIter ifaceiter = vfnptr_iter->ifaces.begin();
1.702 + ifaceiter != vfnptr_iter->ifaces.end(); ++ifaceiter)
1.703 + {
1.704 + for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_pre.begin();
1.705 + hookiter != ifaceiter->hooks_pre.end(); ++hookiter)
1.706 + if (plug == hookiter->plug)
1.707 + hookiter->paused = false;
1.708
1.709 - for (std::list<HookManagerInfo::Iface::Hook>::iterator hookiter = ifaceiter->hooks_post.begin();
1.710 - hookiter != ifaceiter->hooks_post.end(); ++hookiter)
1.711 - if (plug == hookiter->plug)
1.712 - hookiter->paused = false;
1.713 - }
1.714 + for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_post.begin();
1.715 + hookiter != ifaceiter->hooks_post.end(); ++hookiter)
1.716 + if (plug == hookiter->plug)
1.717 + hookiter->paused = false;
1.718 + }
1.719 }
1.720
1.721 void CSourceHookImpl::SetRes(META_RES res)