core/sourcehook/sourcehook.cpp
changeset 739 968e67c7780c
parent 733 2365240861a2
child 768 e6394d70ced0
     1.1 --- a/core/sourcehook/sourcehook.cpp	Fri Dec 17 22:20:10 2010 -0500
     1.2 +++ b/core/sourcehook/sourcehook.cpp	Fri Dec 17 22:22:44 2010 -0500
     1.3 @@ -1,5 +1,5 @@
     1.4  /* ======== SourceHook ========
     1.5 -* Copyright (C) 2004-2008 Metamod:Source Development Team
     1.6 +* Copyright (C) 2004-2010 Metamod:Source Development Team
     1.7  * No warranties of any kind
     1.8  *
     1.9  * License: zlib/libpng
    1.10 @@ -42,372 +42,32 @@
    1.11  	namespace Impl
    1.12  	{
    1.13  		//////////////////////////////////////////////////////////////////////////
    1.14 -		// CProto
    1.15 -		//////////////////////////////////////////////////////////////////////////
    1.16 -		void CProto::Fill(const ProtoInfo *pProto)
    1.17 -		{
    1.18 -			if (pProto == NULL)
    1.19 -				m_Version = -1;
    1.20 -
    1.21 -			m_ParamsPassInfo.clear();
    1.22 -
    1.23 -			if (pProto->paramsPassInfo[0].size == 0)
    1.24 -			{
    1.25 -				// Version 1
    1.26 -				m_Version = 0;
    1.27 -				m_Convention = pProto->convention;
    1.28 -				m_NumOfParams = pProto->numOfParams;
    1.29 -
    1.30 -				m_RetPassInfo.size = pProto->retPassInfo.size;
    1.31 -				m_RetPassInfo.type = pProto->retPassInfo.type;
    1.32 -				m_RetPassInfo.flags = GetRealFlags(pProto->retPassInfo);
    1.33 -
    1.34 -				m_RetPassInfo.pNormalCtor = NULL;
    1.35 -				m_RetPassInfo.pCopyCtor = NULL;
    1.36 -				m_RetPassInfo.pDtor = NULL;
    1.37 -				m_RetPassInfo.pAssignOperator = NULL;
    1.38 -
    1.39 -				
    1.40 -				m_ParamsPassInfo.resize(pProto->numOfParams);
    1.41 -
    1.42 -				for (int i = 1; i <= pProto->numOfParams; ++i)
    1.43 -				{
    1.44 -					m_ParamsPassInfo[i-1].size = pProto->paramsPassInfo[i].size;
    1.45 -					m_ParamsPassInfo[i-1].type = pProto->paramsPassInfo[i].type;
    1.46 -					m_ParamsPassInfo[i-1].flags = GetRealFlags(pProto->paramsPassInfo[i]);
    1.47 -
    1.48 -					m_ParamsPassInfo[i-1].pNormalCtor = NULL;
    1.49 -					m_ParamsPassInfo[i-1].pCopyCtor = NULL;
    1.50 -					m_ParamsPassInfo[i-1].pDtor = NULL;
    1.51 -					m_ParamsPassInfo[i-1].pAssignOperator = NULL;
    1.52 -				}
    1.53 -			}
    1.54 -			else if (pProto->paramsPassInfo[0].size == 1)
    1.55 -			{
    1.56 -				// Version 2
    1.57 -				m_Version = 1;
    1.58 -				m_Convention = pProto->convention;
    1.59 -				m_NumOfParams = pProto->numOfParams;
    1.60 -
    1.61 -				m_RetPassInfo.size = pProto->retPassInfo.size;
    1.62 -				m_RetPassInfo.type = pProto->retPassInfo.type;
    1.63 -				m_RetPassInfo.flags = pProto->retPassInfo.flags;
    1.64 -
    1.65 -				m_RetPassInfo.pNormalCtor = pProto->retPassInfo2.pNormalCtor;
    1.66 -				m_RetPassInfo.pCopyCtor = pProto->retPassInfo2.pCopyCtor;
    1.67 -				m_RetPassInfo.pDtor = pProto->retPassInfo2.pDtor;
    1.68 -				m_RetPassInfo.pAssignOperator = pProto->retPassInfo2.pAssignOperator;
    1.69 -
    1.70 -				m_ParamsPassInfo.resize(pProto->numOfParams);
    1.71 -
    1.72 -				for (int i = 1; i <= pProto->numOfParams; ++i)
    1.73 -				{
    1.74 -					m_ParamsPassInfo[i-1].size = pProto->paramsPassInfo[i].size;
    1.75 -					m_ParamsPassInfo[i-1].type = pProto->paramsPassInfo[i].type;
    1.76 -					m_ParamsPassInfo[i-1].flags = pProto->paramsPassInfo[i].flags;
    1.77 -
    1.78 -					m_ParamsPassInfo[i-1].pNormalCtor = pProto->paramsPassInfo2[i].pNormalCtor;
    1.79 -					m_ParamsPassInfo[i-1].pCopyCtor = pProto->paramsPassInfo2[i].pCopyCtor;
    1.80 -					m_ParamsPassInfo[i-1].pDtor = pProto->paramsPassInfo2[i].pDtor;
    1.81 -					m_ParamsPassInfo[i-1].pAssignOperator = pProto->paramsPassInfo2[i].pAssignOperator;
    1.82 -				}
    1.83 -			}
    1.84 -			else
    1.85 -			{
    1.86 -				// Unknown
    1.87 -				m_Version = -1;
    1.88 -			}
    1.89 -		}
    1.90 -
    1.91 -		// Basic compat test
    1.92 -		// Other than this, we assume that the plugins know what they're doing
    1.93 -		bool CProto::operator == (const CProto &other) const
    1.94 -		{
    1.95 -			if (m_Version < 0 || other.GetVersion() < 0)
    1.96 -				return false;
    1.97 -
    1.98 -			if (m_NumOfParams != other.GetNumOfParams())
    1.99 -				return false;
   1.100 -
   1.101 -			if (m_Convention != ProtoInfo::CallConv_Unknown && other.GetConvention() != ProtoInfo::CallConv_Unknown &&
   1.102 -				m_Convention != other.GetConvention())
   1.103 -				return false;
   1.104 -
   1.105 -			if (GetRealSize(GetRet()) != GetRealSize(other.GetRet()))
   1.106 -				return false;
   1.107 -
   1.108 -			for (int i = 0; i < m_NumOfParams; ++i)
   1.109 -			{
   1.110 -				if (GetRealSize(GetParam(i)) != GetRealSize(other.GetParam(i)))
   1.111 -					return false;
   1.112 -				if (GetParam(i).type != PassInfo::PassType_Unknown && other.GetParam(i).type != PassInfo::PassType_Unknown)
   1.113 -				{
   1.114 -					if (GetParam(i).type != other.GetParam(i).type)
   1.115 -						return false;
   1.116 -					if (GetParam(i).flags != other.GetParam(i).flags)
   1.117 -						return false;
   1.118 -				}
   1.119 -			}
   1.120 -
   1.121 -			return true;
   1.122 -		}
   1.123 -
   1.124 -		bool CProto::ExactlyEqual(const CProto &other) const
   1.125 -		{
   1.126 -			if (m_Version != other.m_Version ||
   1.127 -				m_NumOfParams != other.m_NumOfParams ||
   1.128 -				m_Convention != other.m_Convention ||
   1.129 -				GetRet() != other.GetRet())
   1.130 -			{
   1.131 -				return false;
   1.132 -			}
   1.133 -
   1.134 -			for (int i = 0; i < m_NumOfParams; ++i)
   1.135 -			{
   1.136 -				if(GetParam(i) != other.GetParam(i))
   1.137 -					return false;
   1.138 -			}
   1.139 -
   1.140 -			return true;
   1.141 -		}
   1.142 -		
   1.143 -		//////////////////////////////////////////////////////////////////////////
   1.144 -		// CHookManager
   1.145 -		//////////////////////////////////////////////////////////////////////////
   1.146 -		void CHookManager::SetInfo(int hookman_version, int vtbloffs, int vtblidx,
   1.147 -			ProtoInfo *proto, void *hookfunc_vfnptr)
   1.148 -		{
   1.149 -			m_Version = hookman_version;
   1.150 -			m_VtblOffs = vtbloffs;
   1.151 -			m_VtblIdx = vtblidx;
   1.152 -			m_Proto = proto;
   1.153 -			m_HookfuncVfnptr = hookfunc_vfnptr;
   1.154 -		}
   1.155 -
   1.156 -		//////////////////////////////////////////////////////////////////////////
   1.157  		// CVfnPtrList
   1.158  		//////////////////////////////////////////////////////////////////////////
   1.159  
   1.160 -		CVfnPtr &CVfnPtrList::GetVfnPtr(void *vfnptr)
   1.161 +		CVfnPtr *CVfnPtrList::GetVfnPtr(void *vfnptr)
   1.162  		{
   1.163  			iterator iter = find(vfnptr);
   1.164  			if (iter == end())
   1.165  			{
   1.166 +				// No vfnptr info object found
   1.167 +				// --> create a new one
   1.168  				CVfnPtr newVfnPtr(vfnptr);
   1.169 -				push_back(newVfnPtr);
   1.170 -
   1.171 -				return back();
   1.172 -			}
   1.173 -			else
   1.174 -			{
   1.175 -				return *iter;
   1.176 -			}
   1.177 -		}
   1.178 -		//////////////////////////////////////////////////////////////////////////
   1.179 -		// CVfnPtr
   1.180 -		//////////////////////////////////////////////////////////////////////////
   1.181 -		
   1.182 -		void CVfnPtr::AddHookMan(CHookManager *pHookMan)
   1.183 -		{
   1.184 -			List<CHookManager*>::iterator iter;
   1.185 -
   1.186 -			// Don't accept invalid hook managers
   1.187 -			if (!*pHookMan)
   1.188 -				return;
   1.189 -
   1.190 -			// Check whether this hook manager already exists; if yes, ignore.
   1.191 -			iter = m_HookMans.find(pHookMan);
   1.192 -			if (iter != m_HookMans.end())
   1.193 -				return;
   1.194 -
   1.195 -			// It doesn't -> add it. Add it to the end of its version group.
   1.196 -			for (iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
   1.197 -			{
   1.198 -				if ((*iter)->GetVersion() < pHookMan->GetVersion())
   1.199 -					break;
   1.200 -			}
   1.201 -
   1.202 -			bool isBeginning = iter == m_HookMans.begin();
   1.203 -
   1.204 -			m_HookMans.insert(iter, pHookMan);
   1.205 -
   1.206 -			if (isBeginning)
   1.207 -			{
   1.208 -				pHookMan->IncrRef(this);
   1.209 -				if (m_HookMans.size() > 1)
   1.210 +				if (newVfnPtr.Init())
   1.211  				{
   1.212 -					// If another hookman was used until now but this one is better
   1.213 -					// (which it is because it's the first -> it has a higher version)
   1.214 -					// -> switch!
   1.215 -
   1.216 -					List<CHookManager*>::iterator second = m_HookMans.begin();
   1.217 -					++second;
   1.218 -
   1.219 -					(*second)->DecrRef(this);
   1.220 -				}
   1.221 +					push_back(newVfnPtr);
   1.222  
   1.223 -				// Make sure that this vfnptr points at it
   1.224 -				Patch(pHookMan->GetHookFunc());
   1.225 -			}
   1.226 -		}
   1.227 -
   1.228 -		bool CVfnPtr::HookManRemoved(CHookManager *pHookMan)
   1.229 -		{
   1.230 -			// Don't accept invalid hook managers
   1.231 -			if (!*pHookMan)
   1.232 -				return true;
   1.233 -
   1.234 -			List<CHookManager*>::iterator iter = m_HookMans.find(pHookMan);
   1.235 -			if (iter == m_HookMans.end())
   1.236 -				return true;							// Didn't exist here anyway
   1.237 -
   1.238 -			if (iter == m_HookMans.begin())
   1.239 -			{
   1.240 -				// It is the first one!
   1.241 -				pHookMan->DecrRef(this);
   1.242 -				m_HookMans.erase(iter);
   1.243 -
   1.244 -				if (m_HookMans.empty())
   1.245 -					return false;				// No more hookmans -> let SH delete us
   1.246 -
   1.247 -				// Activate second -> now first hookman
   1.248 -				m_HookMans.front()->IncrRef(this);
   1.249 -				Patch(m_HookMans.front()->GetHookFunc());
   1.250 -			}
   1.251 -			else
   1.252 -			{
   1.253 -				m_HookMans.erase(iter);
   1.254 -			}
   1.255 -			return true;
   1.256 -		}
   1.257 -
   1.258 -		bool CVfnPtr::Patch(void *newValue)
   1.259 -		{
   1.260 -			if (!SetMemAccess(m_Ptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE))
   1.261 -			{
   1.262 -				return false;
   1.263 -			}
   1.264 -
   1.265 -			*reinterpret_cast<void**>(m_Ptr) = newValue;
   1.266 -
   1.267 -			return true;
   1.268 -		}
   1.269 -
   1.270 -		CIface &CVfnPtr::GetIface(void *iface)
   1.271 -		{
   1.272 -			List<CIface>::iterator iter = m_IfaceList.find(iface);
   1.273 -			if (iter == m_IfaceList.end())
   1.274 -			{
   1.275 -				CIface newIface(iface);
   1.276 -				if (iface == NULL)
   1.277 -				{
   1.278 -					m_IfaceList.push_front(newIface);
   1.279 -					return m_IfaceList.front();
   1.280 +					return &(back());
   1.281  				}
   1.282  				else
   1.283  				{
   1.284 -					m_IfaceList.push_back(newIface);
   1.285 -					return m_IfaceList.back();
   1.286 +					// Initialization failed.
   1.287 +					return NULL;
   1.288  				}
   1.289  			}
   1.290  			else
   1.291  			{
   1.292 -				return *iter;
   1.293 -			}
   1.294 -		}
   1.295 -
   1.296 -		//////////////////////////////////////////////////////////////////////////
   1.297 -		// CHookIdManager
   1.298 -		//////////////////////////////////////////////////////////////////////////
   1.299 -		CHookIDManager::CHookIDManager()
   1.300 -		{
   1.301 -		}
   1.302 -
   1.303 -		int CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
   1.304 -			void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
   1.305 -		{
   1.306 -			Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
   1.307 -
   1.308 -			size_t cursize = m_Entries.size();
   1.309 -			for (size_t i = 0; i < cursize; ++i)
   1.310 -			{
   1.311 -				if (m_Entries[i].isfree)
   1.312 -				{
   1.313 -					m_Entries[i] = tmp;
   1.314 -					return static_cast<int>(i) + 1;
   1.315 -				}
   1.316 -			}
   1.317 -
   1.318 -			m_Entries.push_back(tmp);
   1.319 -			return static_cast<int>(m_Entries.size());		// return size() because hookid = id+1 anyway
   1.320 -		}
   1.321 -
   1.322 -		bool CHookIDManager::Remove(int hookid)
   1.323 -		{
   1.324 -			int realid = hookid - 1;
   1.325 -			if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
   1.326 -				return false;
   1.327 -
   1.328 -			m_Entries[realid].isfree = true;
   1.329 -
   1.330 -			// :TODO: remove free ids from back sometimes ??
   1.331 -
   1.332 -			return true;
   1.333 -		}
   1.334 -
   1.335 -		const CHookIDManager::Entry * CHookIDManager::QueryHook(int hookid)
   1.336 -		{
   1.337 -			int realid = hookid - 1;
   1.338 -			if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
   1.339 -				return NULL;
   1.340 -
   1.341 -			return &m_Entries[realid];
   1.342 -		}
   1.343 -
   1.344 -		void CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
   1.345 -			int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
   1.346 -		{
   1.347 -			// oh my god, a lot of parameters...
   1.348 -			size_t cursize = m_Entries.size();
   1.349 -			for (size_t i = 0; i < cursize; ++i)
   1.350 -			{
   1.351 -				if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
   1.352 -					m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
   1.353 -					m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
   1.354 -				{
   1.355 -					output.push_back(static_cast<int>(i) + 1);
   1.356 -				}
   1.357 -			}
   1.358 -		}
   1.359 -
   1.360 -		void CHookIDManager::FindAllHooks(CVector<int> &output)
   1.361 -		{
   1.362 -			size_t cursize = m_Entries.size();
   1.363 -			for (size_t i = 0; i < cursize; ++i)
   1.364 -			{
   1.365 -				if (!m_Entries[i].isfree)
   1.366 -					output.push_back(static_cast<int>(i) + 1);
   1.367 -			}
   1.368 -		}
   1.369 -
   1.370 -		void CHookIDManager::FindAllHooks(CVector<int> &output, Plugin plug)
   1.371 -		{
   1.372 -			size_t cursize = m_Entries.size();
   1.373 -			for (size_t i = 0; i < cursize; ++i)
   1.374 -			{
   1.375 -				if (!m_Entries[i].isfree && m_Entries[i].plug == plug)
   1.376 -					output.push_back(static_cast<int>(i) + 1);
   1.377 -			}
   1.378 -		}
   1.379 -
   1.380 -
   1.381 -		void CHookIDManager::RemoveAll(void *vfnptr)
   1.382 -		{
   1.383 -			size_t cursize = m_Entries.size();
   1.384 -			for (size_t i = 0; i < cursize; ++i)
   1.385 -			{
   1.386 -				if (!m_Entries[i].isfree && m_Entries[i].vfnptr == vfnptr)
   1.387 -					m_Entries[i].isfree = true;
   1.388 +				return &(*iter);
   1.389  			}
   1.390  		}
   1.391  
   1.392 @@ -480,9 +140,18 @@
   1.393  				break;
   1.394  			}
   1.395  
   1.396 -			CVfnPtr &vfnPtr = m_VfnPtrs.GetVfnPtr(cur_vfnptr);
   1.397 -			vfnPtr.AddHookMan(m_HookManList.GetHookMan(hookManager));
   1.398 -			CIface &ifaceinst = vfnPtr.GetIface(adjustediface);
   1.399 +			CVfnPtr *vfnPtr = m_VfnPtrs.GetVfnPtr(cur_vfnptr);
   1.400 +			if (!vfnPtr)
   1.401 +			{
   1.402 +				// Could not create the vfnptr info object.
   1.403 +				// This could be because a thunk generation on GCC
   1.404 +				// has failed. See sourcehook_impl_cvfnptr.cpp
   1.405 +				// for details.
   1.406 +				return false;
   1.407 +			}
   1.408 +
   1.409 +			vfnPtr->AddHookMan(m_HookManList.GetHookMan(hookManager));
   1.410 +			CIface &ifaceinst = vfnPtr->GetIface(adjustediface);
   1.411  
   1.412  			// Add the hook
   1.413  			CHook hook(plug, thisptr_offs, handler, 
   1.414 @@ -588,9 +257,7 @@
   1.415  						ctx_iter->VfnPtrRemoved(&(*vfnptr_iter));
   1.416  					}
   1.417  
   1.418 -					vfnptr_iter->Revert();
   1.419 -
   1.420 -					m_VfnPtrs.erase(vfnptr_iter);
   1.421 +					RevertAndRemoveVfnPtr(vfnptr_iter);
   1.422  				}
   1.423  			}
   1.424  
   1.425 @@ -598,6 +265,52 @@
   1.426  			return true;
   1.427  		}
   1.428  
   1.429 +		List<CVfnPtr>::iterator CSourceHookImpl::RevertAndRemoveVfnPtr(List<CVfnPtr>::iterator vfnptr_iter)
   1.430 +		{
   1.431 +			ICleanupTask *cleanupTask = vfnptr_iter->GetCleanupTask();
   1.432 +
   1.433 +			// Some vfnptrs require cleanup.
   1.434 +			// Concrete case: on GCC, when the original vtable entry is not even
   1.435 +			// we generate an even-aligned thunk to call the original function.
   1.436 +			// If the vfnptr is being removed from a pre hook on the vfnptr
   1.437 +			// we have to delay the cleanup of this thunk until the hook loop is done
   1.438 +			// (because the orig function call mechanism is going to use the thunk).
   1.439 +
   1.440 +			if (cleanupTask != NULL)
   1.441 +			{
   1.442 +				// If this vfnptr is in use in one of the hook loops running at the moment
   1.443 +				// Schedule it for removal on the DEEPEST hook loop.
   1.444 +
   1.445 +				size_t numOfContexts = m_ContextStack.size();
   1.446 +				// m_ContextStack.at(0) is the deepest hook context
   1.447 +				// m_ContextStack.at(size-1) = m_ContextStack.front is the uppermost
   1.448 +
   1.449 +				bool cleanupImmedieately = true;
   1.450 +
   1.451 +				CVfnPtr *vfnPtrObjAddr = &(*vfnptr_iter);
   1.452 +				for (size_t i = 0; i < numOfContexts; ++i)
   1.453 +				{
   1.454 +					CHookContext &context = m_ContextStack.at(i);
   1.455 +					if (context.pVfnPtr == vfnPtrObjAddr)
   1.456 +					{
   1.457 +						// Found a hook context using this vfnptr at the moment.
   1.458 +						context.m_CleanupTask = cleanupTask;
   1.459 +						cleanupImmedieately = false;			// Delay the cleanup
   1.460 +						break;
   1.461 +					}
   1.462 +				}
   1.463 +
   1.464 +				if (cleanupImmedieately)
   1.465 +				{
   1.466 +					cleanupTask->CleanupAndDeleteThis();
   1.467 +				}
   1.468 +			}
   1.469 +
   1.470 +			// Do the work
   1.471 +			vfnptr_iter->Revert();
   1.472 +			return m_VfnPtrs.erase(vfnptr_iter);
   1.473 +		}
   1.474 +
   1.475  		void CSourceHookImpl::SetRes(META_RES res)
   1.476  		{
   1.477  			*m_ContextStack.front().pCurRes = res;
   1.478 @@ -675,10 +388,9 @@
   1.479  					// This vfnptr has no more hook managers
   1.480  					// and asks to be removed.
   1.481  
   1.482 -					vfnptr_iter->Revert();
   1.483 +					m_HookIDMan.RemoveAll(vfnptr_iter->GetPtr());
   1.484  
   1.485 -					m_HookIDMan.RemoveAll(vfnptr_iter->GetPtr());
   1.486 -					vfnptr_iter = m_VfnPtrs.erase(vfnptr_iter);
   1.487 +					vfnptr_iter = RevertAndRemoveVfnPtr(vfnptr_iter);
   1.488  				}
   1.489  				else
   1.490  				{
   1.491 @@ -710,7 +422,12 @@
   1.492  		void CSourceHookImpl::ResetIgnoreHooks(void *vfnptr)
   1.493  		{
   1.494  			if (!m_ContextStack.empty() && m_ContextStack.front().m_State == CHookContext::State_Ignore)
   1.495 -				m_ContextStack.pop();
   1.496 +			{
   1.497 +				// Actually use EndContext
   1.498 +				// instead of m_ContextStack.pop directly
   1.499 +				// because it runs the cleanup task if neccesary
   1.500 +				EndContext(&(m_ContextStack.front()));
   1.501 +			}
   1.502  		}
   1.503  
   1.504  		void *CSourceHookImpl::GetOrigVfnPtrEntry(void *vfnptr)
   1.505 @@ -753,7 +470,7 @@
   1.506  			curCtx.m_State = CHookContext::State_Dead;
   1.507  		}
   1.508  
   1.509 -		IHookContext *CSourceHookImpl::SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry, META_RES *statusPtr,
   1.510 +		IHookContext *CSourceHookImpl::SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origCallAddr, META_RES *statusPtr,
   1.511  			META_RES *prevResPtr, META_RES *curResPtr, const void *origRetPtr, void *overrideRetPtr)
   1.512  		{
   1.513  			CHookContext *pCtx = NULL;
   1.514 @@ -781,7 +498,8 @@
   1.515  					}
   1.516  					else
   1.517  					{
   1.518 -						*origentry = (*vfnptr_iter)->GetOrigEntry();
   1.519 +						*origCallAddr = (*vfnptr_iter)->GetOrigCallAddr();
   1.520 +						oldctx->pVfnPtr = *vfnptr_iter;
   1.521  					}
   1.522  
   1.523  					oldctx->pOrigRet = origRetPtr;
   1.524 @@ -839,7 +557,7 @@
   1.525  			else
   1.526  			{
   1.527  				pCtx->pVfnPtr = *vfnptr_iter;
   1.528 -				*origentry = pCtx->pVfnPtr->GetOrigEntry();
   1.529 +				*origCallAddr = pCtx->pVfnPtr->GetOrigCallAddr();
   1.530  				pCtx->pIface = pCtx->pVfnPtr->FindIface(thisptr);
   1.531  			}
   1.532  
   1.533 @@ -855,6 +573,9 @@
   1.534  
   1.535  		void CSourceHookImpl::EndContext(IHookContext *pCtx)
   1.536  		{
   1.537 +			// Do clean up task, if any is associated with this context
   1.538 +			m_ContextStack.front().DoCleanupTaskAndDeleteIt();
   1.539 +			// Then remove it
   1.540  			m_ContextStack.pop();
   1.541  		}
   1.542  
   1.543 @@ -1085,9 +806,22 @@
   1.544  		{
   1.545  			if (pVfnPtr == vfnptr)
   1.546  			{
   1.547 -				pVfnPtr = NULL;
   1.548 +				// Don't set pVfnPtr = NULL here!
   1.549 +				// It may be used still.
   1.550 +				// RevertAndRemoveVfnPtr uses it to find the hook context
   1.551 +				// to which to attach the cleanup task of the vfnptr.
   1.552 +
   1.553 +				//pVfnPtr = NULL;
   1.554  				m_State = State_Dead;
   1.555  			}
   1.556  		}
   1.557 +
   1.558 +		void CHookContext::DoCleanupTaskAndDeleteIt()
   1.559 +		{
   1.560 +			if (m_CleanupTask != NULL)
   1.561 +			{
   1.562 +				m_CleanupTask->CleanupAndDeleteThis();
   1.563 +			}
   1.564 +		}
   1.565  	}
   1.566  }