sourcehook/sourcehook.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 07 18:24:08 2005 +0000 (2005-07-07)
changeset 87 4d1f04a1aee1
parent 86 0c0c0ab2e9ba
child 88 2586c2eb900e
permissions -rw-r--r--
Oops
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * Contributors: Scott "Damaged Soul" Ehlert
     9 * ============================
    10 */
    11 
    12 /**
    13  * @file sourcehook.cpp
    14  * @brief Contains the implementation of the SourceHook API
    15 */
    16 
    17 #include <algorithm>
    18 #include "sourcehook_impl.h"
    19 
    20 namespace SourceHook
    21 {
    22 	CSourceHookImpl::CSourceHookImpl()
    23 	{
    24 	}
    25 
    26 	CSourceHookImpl::~CSourceHookImpl()
    27 	{
    28 	}
    29 
    30 	int CSourceHookImpl::GetIfaceVersion()
    31 	{
    32 		return SH_IFACE_VERSION;
    33 	}
    34 
    35 	int CSourceHookImpl::GetImplVersion()
    36 	{
    37 		return SH_IMPL_VERSION;
    38 	}
    39 
    40 	bool CSourceHookImpl::IsPluginInUse(Plugin plug)
    41 	{
    42 		// Iterate through all hook managers which are in this plugin
    43 		// Iterate through their vfnptrs, ifaces, hooks
    44 		// If a hook from an other plugin is found, return true
    45 		// Return false otherwise
    46 #define TMP_CHECK_LIST(name) \
    47 	for (hook_iter = iface_iter->name.begin(); hook_iter != iface_iter->name.end(); ++hook_iter) \
    48 		if (hook_iter->plug == plug) \
    49 			return true;
    50 
    51 		for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
    52 		{
    53 			if (hmil_iter->plug != plug)
    54 				continue;
    55 			for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->vfnptrs.begin();
    56 				vfnptr_iter != hmil_iter->vfnptrs.end(); ++vfnptr_iter)
    57 			{
    58 				for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
    59 					iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
    60 				{
    61 					std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hook_iter;
    62 					TMP_CHECK_LIST(hooks_pre);
    63 					TMP_CHECK_LIST(hooks_post);
    64 				}
    65 			}
    66 		}
    67 #undef TMP_CHECK_LIST
    68 		return false;
    69 	}
    70 
    71 	void CSourceHookImpl::UnloadPlugin(Plugin plug)
    72 	{
    73 		// 1) Manually remove all hooks by this plugin
    74 		std::list<RemoveHookInfo> hookstoremove;
    75 		HookManInfoList::iterator hmil_iter;
    76 
    77 #define TMP_CHECK_LIST(name, ispost) \
    78 	for (hook_iter = iface_iter->name.begin(); hook_iter != iface_iter->name.end(); ++hook_iter) \
    79 		if (hook_iter->plug == plug) \
    80 			hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->ptr, \
    81 			hook_iter->thisptr_offs, hmil_iter->func, hook_iter->handler, ispost))
    82 		for (hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
    83 		{
    84 			for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->vfnptrs.begin();
    85 				vfnptr_iter != hmil_iter->vfnptrs.end(); ++vfnptr_iter)
    86 			{
    87 				for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
    88 					iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
    89 				{
    90 					std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hook_iter;
    91 					TMP_CHECK_LIST(hooks_pre, false);
    92 					TMP_CHECK_LIST(hooks_post, true);
    93 				}
    94 			}
    95 		}
    96 #undef TMP_CHECK_LIST
    97 
    98 		for (std::list<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
    99 			RemoveHook(*rmiter);
   100 
   101 		// 2) Other plugins may use hook managers in this plugin.
   102 		// Get a list of hook managers that are in this plugin and are used by other plugins
   103 		// Delete all hook managers that are in this plugin
   104 
   105 		HookManInfoList tmphookmans;
   106 		bool erase = false;
   107 		for (hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end();
   108 			erase ? hmil_iter=m_HookMans.erase(hmil_iter) : ++hmil_iter)
   109 		{
   110 			if (hmil_iter->plug == plug)
   111 			{
   112 				if (!hmil_iter->vfnptrs.empty())
   113 				{
   114 					// All hooks by this plugin are already removed
   115 					// So if there is a vfnptr, it has to be used by an other plugin
   116 					tmphookmans.push_back(*hmil_iter);
   117 				}
   118 				erase = true;
   119 			}
   120 			else
   121 				erase = false;
   122 		}
   123 		
   124 		// For each hook manager:
   125 		for (hmil_iter = tmphookmans.begin(); hmil_iter != tmphookmans.end(); ++hmil_iter)
   126 		{
   127 			// Find a suitable hook manager in an other plugin
   128 			HookManInfoList::iterator newHookMan = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
   129 				hmil_iter->proto, hmil_iter->vtbl_offs, hmil_iter->vtbl_idx);
   130 
   131 			// This should _never_ happen.
   132 			// If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
   133 			SH_ASSERT(newHookMan != m_HookMans.end(),
   134 				"Could not find a suitable hook manager in an other plugin!");
   135 
   136 			// AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
   137 			SH_ASSERT(newHookMan->plug != plug, "New hook manager from same plugin!");
   138 
   139 			// The first hook manager should be always used - so the new hook manager has to be empty
   140 			SH_ASSERT(newHookMan->vfnptrs.empty(), "New hook manager not empty!");
   141 
   142 			// Move the vfnptrs from the old hook manager to the new one
   143 			newHookMan->vfnptrs = hmil_iter->vfnptrs;
   144 
   145 			// Unregister the old one, register the new one
   146 			hmil_iter->func(HA_Unregister, NULL);
   147 			newHookMan->func(HA_Register, &(*newHookMan));
   148 
   149 			// zOMG BAIL, here is part of what you wanted:
   150 
   151 			// Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
   152 			// or whatever
   153 			for (HookManagerInfo::VfnPtrListIter vfnptr_iter = newHookMan->vfnptrs.begin();
   154 				vfnptr_iter != newHookMan->vfnptrs.end(); ++vfnptr_iter)
   155 			{
   156 				// And DEREFERENCE newHookMan->hookfunc_vfnptr!
   157 				// otherwise it will be executing the vtable... had to find out the hard way
   158 				*reinterpret_cast<void**>(vfnptr_iter->vfnptr) = *reinterpret_cast<void**>(newHookMan->hookfunc_vfnptr);
   159 			}
   160 
   161 			// That should fix it, bail!
   162 		}
   163 	}
   164 
   165 	void CSourceHookImpl::CompleteShutdown()
   166 	{
   167 		std::list<RemoveHookInfo> hookstoremove;
   168 #define TMP_CHECK_LIST(name, ispost) \
   169 	for (hook_iter = iface_iter->name.begin(); hook_iter != iface_iter->name.end(); ++hook_iter) \
   170 		hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->ptr, \
   171 		hook_iter->thisptr_offs, hmil_iter->func, hook_iter->handler, ispost))
   172 		for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
   173 		{
   174 			for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->vfnptrs.begin();
   175 				vfnptr_iter != hmil_iter->vfnptrs.end(); ++vfnptr_iter)
   176 			{
   177 				for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
   178 					iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
   179 				{
   180 					std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hook_iter;
   181 					TMP_CHECK_LIST(hooks_pre, false);
   182 					TMP_CHECK_LIST(hooks_post, true);
   183 				}
   184 			}
   185 		}
   186 #undef TMP_CHECK_LIST
   187 
   188 		for (std::list<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
   189 			RemoveHook(*rmiter);
   190 
   191 		m_HookMans.clear();
   192 	}
   193 
   194 	bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
   195 	{
   196 		void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
   197 		// 1) Get info about the hook manager
   198 		HookManagerInfo tmp;
   199 		if (myHookMan(HA_GetInfo, &tmp) != 0)
   200 			return false;
   201 
   202 		// 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
   203 		HookManInfoList::iterator hkmi_iter;
   204 		for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
   205 		{
   206 			if (hkmi_iter->plug == plug && strcmp(hkmi_iter->proto, tmp.proto) == 0 &&
   207 				hkmi_iter->vtbl_offs == tmp.vtbl_offs && hkmi_iter->vtbl_idx == tmp.vtbl_idx)
   208 				break;
   209 		}
   210 		if (hkmi_iter == m_HookMans.end())
   211 		{
   212 			// No such hook manager from this plugin yet, add it!
   213 			tmp.func = myHookMan;
   214 			tmp.plug = plug;
   215 			m_HookMans.push_back(tmp);
   216 		}
   217 
   218 		// Then, search for a suitable hook manager (from the beginning)
   219 		HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(), tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx);
   220 		SH_ASSERT(hookman != m_HookMans.end(), "No hookman found - but if there was none, we've just added one!");
   221 
   222 		// Tell it to store the pointer if it's not already active
   223 		if (hookman->vfnptrs.empty())
   224 			hookman->func(HA_Register, &(*hookman));
   225 
   226 		void **cur_vtptr = *reinterpret_cast<void***>(
   227 			reinterpret_cast<char*>(adjustediface) + tmp.vtbl_offs);
   228 		void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.vtbl_idx);
   229 
   230 		HookManagerInfo::VfnPtrListIter vfnptr_iter = std::find(
   231 			hookman->vfnptrs.begin(), hookman->vfnptrs.end(), cur_vfnptr);
   232 
   233 		if (vfnptr_iter == hookman->vfnptrs.end())
   234 		{
   235 			// Add a new one
   236 			HookManagerInfo::VfnPtr vfp;
   237 			vfp.vfnptr = cur_vfnptr;
   238 			vfp.orig_entry = *reinterpret_cast<void**>(cur_vfnptr);
   239 
   240 			// Alter vtable entry
   241 			if (!SetMemAccess(cur_vtptr, sizeof(void*) * (tmp.vtbl_idx + 1), SH_MEM_READ | SH_MEM_WRITE))
   242 				return false;
   243 			
   244 			*reinterpret_cast<void**>(cur_vfnptr) = *reinterpret_cast<void**>(hookman->hookfunc_vfnptr);
   245 
   246 			hookman->vfnptrs.push_back(vfp);
   247 			
   248 			// Make vfnptr_iter point to the new element
   249 			vfnptr_iter = hookman->vfnptrs.end();
   250 			--vfnptr_iter;
   251 
   252 			// Now that it is done, check whether we have to update any callclasses
   253 			ApplyCallClassPatches(adjustediface, tmp.vtbl_offs, tmp.vtbl_idx, vfp.orig_entry);
   254 		}
   255 
   256 		HookManagerInfo::VfnPtr::IfaceListIter iface_iter = std::find(
   257 			vfnptr_iter->ifaces.begin(), vfnptr_iter->ifaces.end(), adjustediface);
   258 		if (iface_iter == vfnptr_iter->ifaces.end())
   259 		{
   260 			// Add a new one
   261 			HookManagerInfo::VfnPtr::Iface ifs;
   262 			ifs.ptr = adjustediface;
   263 			vfnptr_iter->ifaces.push_back(ifs);
   264 
   265 			// Make iface_iter point to the new element
   266 			iface_iter = vfnptr_iter->ifaces.end();
   267 			--iface_iter;
   268 		}
   269 
   270 		// Add the hook
   271 		HookManagerInfo::VfnPtr::Iface::Hook hookinfo;
   272 		hookinfo.handler = handler;
   273 		hookinfo.plug = plug;
   274 		hookinfo.paused = false;
   275 		hookinfo.thisptr_offs = thisptr_offs;
   276 		if (post)
   277    			iface_iter->hooks_post.push_back(hookinfo);
   278 		else
   279 			iface_iter->hooks_pre.push_back(hookinfo);
   280 
   281 
   282 		return true;
   283 	}
   284 
   285 	bool CSourceHookImpl::RemoveHook(RemoveHookInfo info)
   286 	{
   287 		return RemoveHook(info.plug, info.iface, info.thisptr_offs, info.hookman, info.handler, info.post);
   288 	}
   289 
   290 	bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
   291 	{
   292 		void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
   293 		HookManagerInfo tmp;
   294 		if (myHookMan(HA_GetInfo, &tmp) != 0)
   295 			return false;
   296 
   297 		// Find the hook manager and the hook
   298 		HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(), 
   299 			tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx);
   300 		if (hookman == m_HookMans.end())
   301 			return false;
   302 
   303 		if (IsBadReadPtr(reinterpret_cast<char*>(adjustediface) + tmp.vtbl_offs, sizeof(char)))
   304 		{
   305 			hookman->vfnptrs.clear();
   306 			hookman->func(HA_Unregister, NULL);
   307 			return true;
   308 		}
   309 
   310 		void **cur_vtptr = *reinterpret_cast<void***>(
   311 			reinterpret_cast<char*>(adjustediface) + tmp.vtbl_offs);
   312 		void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.vtbl_idx);
   313 
   314 		HookManagerInfo::VfnPtrListIter vfnptr_iter = std::find(hookman->vfnptrs.begin(), hookman->vfnptrs.end(), cur_vfnptr);
   315 		if (vfnptr_iter == hookman->vfnptrs.end())
   316 			return false;
   317 
   318 		for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
   319 			iface_iter != vfnptr_iter->ifaces.end();)
   320 		{
   321 			std::list<HookManagerInfo::VfnPtr::Iface::Hook> &hooks =
   322 				post ? iface_iter->hooks_post : iface_iter->hooks_pre;
   323 
   324 			bool erase;
   325 			for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = hooks.begin();
   326 				hookiter != hooks.end(); erase ? hookiter = hooks.erase(hookiter) : ++hookiter)
   327 			{
   328 				erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler) &&
   329 					hookiter->thisptr_offs == thisptr_offs;
   330 				if (erase)
   331 					hookiter->handler->DeleteThis();			// Make the _plugin_ delete the handler object
   332 			}
   333 			if (iface_iter->hooks_post.empty() && iface_iter->hooks_pre.empty())
   334 			{
   335 				iface_iter = vfnptr_iter->ifaces.erase(iface_iter);
   336 				if (vfnptr_iter->ifaces.empty())
   337 				{
   338 					// Deactivate the hook
   339 					*reinterpret_cast<void**>(vfnptr_iter->vfnptr) = vfnptr_iter->orig_entry;
   340 					
   341 					hookman->vfnptrs.erase(vfnptr_iter);
   342 
   343 					// Remove callclass patch
   344 					for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
   345 						if (cciter->cc.ptr == adjustediface)
   346 							RemoveCallClassPatch(*cciter, tmp.vtbl_offs, tmp.vtbl_idx);
   347 
   348 					if (hookman->vfnptrs.empty())
   349 					{
   350 						// Unregister the hook manager
   351 						hookman->func(HA_Unregister, NULL);
   352 					}
   353 
   354 					// Don't try to continue looping through ifaces
   355 					// - the list is already invalid
   356 					return true;
   357 				}
   358 			}
   359 			else
   360 				++iface_iter;
   361 		}
   362 		return true;
   363 	}
   364 
   365 	GenericCallClass *CSourceHookImpl::GetCallClass(void *iface, size_t size)
   366 	{
   367 		for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
   368 		{
   369 			if (cciter->cc.ptr == iface && cciter->cc.objsize == size)
   370 			{
   371 				++cciter->refcounter;
   372 				return &cciter->cc;
   373 			}
   374 		}
   375 
   376 		CallClassInfo tmp;
   377 		tmp.refcounter = 1;
   378 		tmp.cc.ptr = iface;
   379 		tmp.cc.objsize = size;
   380 
   381 		ApplyCallClassPatches(tmp);
   382 
   383 		m_CallClasses.push_back(tmp);
   384 		return &m_CallClasses.back().cc;
   385 	}
   386 
   387 	void CSourceHookImpl::ReleaseCallClass(GenericCallClass *ptr)
   388 	{
   389 		Impl_CallClassList::iterator iter = std::find(m_CallClasses.begin(), m_CallClasses.end(), ptr);
   390 		if (iter == m_CallClasses.end())
   391 			return;
   392 		--iter->refcounter;
   393 		if (iter->refcounter < 1)
   394 			m_CallClasses.erase(iter);
   395 	}
   396 
   397 	void CSourceHookImpl::ApplyCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx, void *orig_entry)
   398 	{
   399 		OrigFuncs &tmpvec = cc.cc.vt[vtbl_offs];
   400 		if (tmpvec.size() <= (size_t)vtbl_idx)
   401 			tmpvec.resize(vtbl_idx+1);
   402 		tmpvec[vtbl_idx] = orig_entry;
   403 	}
   404 
   405 	void CSourceHookImpl::ApplyCallClassPatches(CallClassInfo &cc)
   406 	{
   407 		for (HookManInfoList::iterator hookman = m_HookMans.begin(); hookman != m_HookMans.end(); ++hookman)
   408 		{
   409 			for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->vfnptrs.begin();
   410 				vfnptr_iter != hookman->vfnptrs.end(); ++vfnptr_iter)
   411 			{
   412 				for (HookManagerInfo::VfnPtr::IfaceListIter iface_iter = vfnptr_iter->ifaces.begin();
   413 					iface_iter != vfnptr_iter->ifaces.end(); ++iface_iter)
   414 				{
   415 					if (iface_iter->ptr >= cc.cc.ptr &&
   416 						iface_iter->ptr < (reinterpret_cast<char*>(cc.cc.ptr) + cc.cc.objsize))
   417 					{
   418 						ApplyCallClassPatch(cc, static_cast<int>(reinterpret_cast<char*>(iface_iter->ptr) - 
   419 							reinterpret_cast<char*>(cc.cc.ptr)) + hookman->vtbl_offs,
   420 							hookman->vtbl_idx, vfnptr_iter->orig_entry);
   421 					}
   422 				}
   423 			}
   424 		}
   425 	}
   426 
   427 	void CSourceHookImpl::ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry)
   428 	{
   429 		for (Impl_CallClassList::iterator cc_iter = m_CallClasses.begin(); cc_iter != m_CallClasses.end();
   430 			++cc_iter)
   431 		{
   432 			if (ifaceptr >= cc_iter->cc.ptr &&
   433 				ifaceptr < (reinterpret_cast<char*>(cc_iter->cc.ptr) + cc_iter->cc.objsize))
   434 			{
   435 				ApplyCallClassPatch(*cc_iter, static_cast<int>(reinterpret_cast<char*>(ifaceptr) - 
   436 					reinterpret_cast<char*>(cc_iter->cc.ptr)) + vtbl_offs, vtbl_idx, orig_entry);
   437 			}
   438 		}
   439 	}
   440 
   441 	void CSourceHookImpl::RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx)
   442 	{
   443 		for (Impl_CallClassList::iterator cc_iter = m_CallClasses.begin(); cc_iter != m_CallClasses.end();
   444 			++cc_iter)
   445 		{
   446 			if (ifaceptr >= cc_iter->cc.ptr &&
   447 				ifaceptr < (reinterpret_cast<char*>(cc_iter->cc.ptr) + cc_iter->cc.objsize))
   448 			{
   449 				RemoveCallClassPatch(*cc_iter, vtbl_offs, vtbl_idx);
   450 			}
   451 		}
   452 	}
   453 
   454 	void CSourceHookImpl::RemoveCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx)
   455 	{
   456 		OrigVTables::iterator iter = cc.cc.vt.find(vtbl_offs);
   457 		if (iter != cc.cc.vt.end())
   458 		{
   459 			if (iter->second.size() > (size_t)vtbl_idx)
   460 			{
   461 				iter->second[vtbl_idx] = 0;
   462 				// Free some memory if possible
   463 				OrigFuncs::reverse_iterator riter;
   464 				for (riter = iter->second.rbegin(); riter != iter->second.rend(); ++riter)
   465 				{
   466 					if (*riter != 0)
   467 						break;
   468 				}
   469 				iter->second.resize(iter->second.size() - (riter - iter->second.rbegin()));
   470 				if (!iter->second.size())
   471 					cc.cc.vt.erase(iter);
   472 			}
   473 		}
   474 	}
   475 
   476 #ifdef __linux__
   477 	// Windows has an implentation for this already, but Linux does not :(
   478 	static bool CSourceHookImpl::IsBadReadPtr(const void *ptr, size_t len)
   479 	{
   480 		void(*prevHandler)(int sig);
   481 		g_BadReadCalled = true;
   482 
   483 		if (setjmp(g_BadReadJmpBuffer))
   484 			return true;
   485 
   486 		prevHandler = signal(SIGSEGV, BadReadHandler);
   487 
   488 		volatile const char *p = reinterpret_cast<const char*>(ptr);
   489 		char dummy;
   490 
   491 		for (size_t i = 0; i < len; i++)
   492 			dummy = p[i];
   493 
   494 		g_BadReadCalled = false;
   495 
   496 		signal(SIGSEGV, prevHandler);
   497 
   498 		return false;
   499 	}
   500 
   501 	static void CSourceHookImpl::BadReadHandler(int sig)
   502 	{
   503 		if (m_BadReadCalled)
   504 			longjmp(m_BadReadJmpBuf, 1);
   505 	}
   506 #endif
   507 
   508 	CSourceHookImpl::HookManInfoList::iterator CSourceHookImpl::FindHookMan(HookManInfoList::iterator begin,
   509 		HookManInfoList::iterator end, const char *proto, int vtblofs, int vtblidx)
   510 	{
   511 		HookManInfoList::iterator hookmaniter; 
   512 		for (hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
   513 		{
   514 			if (strcmp(hookmaniter->proto, proto) == 0 && hookmaniter->vtbl_offs == vtblofs &&
   515 				hookmaniter->vtbl_idx == vtblidx)
   516 				break;
   517 		}
   518 		return hookmaniter;
   519 	}
   520 
   521 	void CSourceHookImpl::PausePlugin(Plugin plug)
   522 	{
   523 		// Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to paused
   524 		for (HookManInfoList::iterator hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
   525 			for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->vfnptrs.begin();
   526 				vfnptr_iter != hookmaniter->vfnptrs.end(); ++vfnptr_iter)
   527 				for (HookManagerInfo::VfnPtr::IfaceListIter ifaceiter = vfnptr_iter->ifaces.begin();
   528 					ifaceiter != vfnptr_iter->ifaces.end(); ++ifaceiter)
   529 				{
   530 					for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_pre.begin();
   531 						hookiter != ifaceiter->hooks_pre.end(); ++hookiter)
   532 						if (plug == hookiter->plug)
   533 							hookiter->paused = true;
   534 
   535 					for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_post.begin();
   536 						hookiter != ifaceiter->hooks_post.end(); ++hookiter)
   537 						if (plug == hookiter->plug)
   538 							hookiter->paused = true;
   539 				}
   540 	}
   541 
   542 	void CSourceHookImpl::UnpausePlugin(Plugin plug)
   543 	{
   544 		// Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to paused
   545 		for (HookManInfoList::iterator hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
   546 			for (HookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->vfnptrs.begin();
   547 				vfnptr_iter != hookmaniter->vfnptrs.end(); ++vfnptr_iter)
   548 				for (HookManagerInfo::VfnPtr::IfaceListIter ifaceiter = vfnptr_iter->ifaces.begin();
   549 					ifaceiter != vfnptr_iter->ifaces.end(); ++ifaceiter)
   550 				{
   551 					for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_pre.begin();
   552 						hookiter != ifaceiter->hooks_pre.end(); ++hookiter)
   553 						if (plug == hookiter->plug)
   554 							hookiter->paused = false;
   555 
   556 					for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hookiter = ifaceiter->hooks_post.begin();
   557 						hookiter != ifaceiter->hooks_post.end(); ++hookiter)
   558 						if (plug == hookiter->plug)
   559 							hookiter->paused = false;
   560 				}
   561 	}
   562 
   563 	void CSourceHookImpl::SetRes(META_RES res)
   564 	{
   565 		m_CurRes = res;
   566 	}
   567 
   568 	META_RES CSourceHookImpl::GetPrevRes()
   569 	{
   570 		return m_PrevRes;
   571 	}
   572 
   573 	META_RES CSourceHookImpl::GetStatus()
   574 	{
   575 		return m_Status;
   576 	}
   577 
   578 	const void *CSourceHookImpl::GetOrigRet()
   579 	{
   580 		return m_OrigRet;
   581 	}
   582 
   583 	const void *CSourceHookImpl::GetOverrideRet()
   584 	{
   585 		return m_OverrideRet;
   586 	}
   587 
   588 	META_RES &CSourceHookImpl::GetCurResRef()
   589 	{
   590 		return m_CurRes;
   591 	}
   592 
   593 	META_RES &CSourceHookImpl::GetPrevResRef()
   594 	{
   595 		return m_PrevRes;
   596 	}
   597 
   598 	META_RES &CSourceHookImpl::GetStatusRef()
   599 	{
   600 		return m_Status;
   601 	}
   602 
   603 	void * &CSourceHookImpl::GetIfacePtrRef()
   604 	{
   605 		return m_IfacePtr;
   606 	}
   607 
   608 	void CSourceHookImpl::SetOrigRet(const void *ptr)
   609 	{
   610 		m_OrigRet = ptr;
   611 	}
   612 
   613 	void CSourceHookImpl::SetOverrideRet(const void *ptr)
   614 	{
   615 		m_OverrideRet = ptr;
   616 	}
   617 
   618 	void *CSourceHookImpl::GetIfacePtr()
   619 	{
   620 		return m_IfacePtr;
   621 	}
   622 }