core/sourcehook/sourcehook_impl_cvfnptr.cpp
changeset 739 968e67c7780c
parent 733 2365240861a2
child 767 fd3ff8d792ce
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sourcehook/sourcehook_impl_cvfnptr.cpp	Fri Dec 17 22:22:44 2010 -0500
     1.3 @@ -0,0 +1,253 @@
     1.4 +/* ======== SourceHook ========
     1.5 +* Copyright (C) 2004-2010 Metamod:Source Development Team
     1.6 +* No warranties of any kind
     1.7 +*
     1.8 +* License: zlib/libpng
     1.9 +*
    1.10 +* Author(s): Pavol "PM OnoTo" Marko
    1.11 +* ============================
    1.12 +*/
    1.13 +
    1.14 +#include "sourcehook_impl.h"
    1.15 +
    1.16 +namespace SourceHook
    1.17 +{
    1.18 +	namespace Impl
    1.19 +	{
    1.20 +		CPageAlloc CVfnPtr::ms_AlignedPageAllocator(8);
    1.21 +
    1.22 +		CVfnPtr::CVfnPtr(void *ptr)
    1.23 +			: m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(m_Ptr)),
    1.24 +			m_OrigCallThunk(NULL)
    1.25 +		{
    1.26 +		}
    1.27 +
    1.28 +		CVfnPtr::~CVfnPtr()
    1.29 +		{
    1.30 +			if (!m_HookMans.empty())
    1.31 +				m_HookMans.front()->DecrRef(this);
    1.32 +		}
    1.33 +
    1.34 +		bool CVfnPtr::Init()
    1.35 +		{
    1.36 +			// Initalize the vfn ptr info object.
    1.37 +
    1.38 +			// If we're running on GCC and the original vtable entry is odd
    1.39 +			// we have a problem.
    1.40 +			// The hook functions use non-virtual member function pointers to
    1.41 +			// call the original function (see sourcehook.hxx, SH_SETUP_MFP and SH_CALL_ORIG)
    1.42 +			// GCC has the same format for virtual and non-virtual MFPs:
    1.43 +			// virtual ones have an odd value as "funcptr" where the offset into the vtable is encoded
    1.44 +			// non-virtual ones have the direct function pointer as "funcptr", assuming that it is even
    1.45 +
    1.46 +			// When m_OrigEntry is odd, GCC's runtime MFP calling code interpretes it as a virtual
    1.47 +			// function call though we want to call the function in a non-virtual way
    1.48 +			// (the original function call mechanism between the pre and post hook loop has to bypass
    1.49 +			// the virtual calling mechanism in order to call the original function).
    1.50 +
    1.51 +#if SH_COMP==SH_COMP_GCC
    1.52 +			if ((((ptrdiff_t)m_OrigEntry) & 1) != 0)
    1.53 +			{
    1.54 +				// Odd orig entry.
    1.55 +				if (SH_PTRSIZE != 4)
    1.56 +				{
    1.57 +					// We only have code for IA32 atm!
    1.58 +					return false;
    1.59 +				}
    1.60 +
    1.61 +				// Generate a new thunk
    1.62 +				m_OrigCallThunk = ms_AlignedPageAllocator.Alloc(5);
    1.63 +				ms_AlignedPageAllocator.SetRW(m_OrigCallThunk);
    1.64 +
    1.65 +				unsigned char* thunkBase = reinterpret_cast<unsigned char*>(m_OrigCallThunk);
    1.66 +				*(thunkBase + 0) = 0xE9;		// offset jump, immediate operand
    1.67 +				ptrdiff_t *offsetAddr = reinterpret_cast<ptrdiff_t*>(thunkBase + 1);
    1.68 +				
    1.69 +				// destination = src + offset + 5
    1.70 +				// <=>  offset = destination - src - 5
    1.71 +				*offsetAddr =
    1.72 +					(reinterpret_cast<unsigned char*>(m_OrigEntry) - thunkBase) - 5;
    1.73 +
    1.74 +				ms_AlignedPageAllocator.SetRE(m_OrigCallThunk);
    1.75 +			}
    1.76 +#endif
    1.77 +			return true;
    1.78 +		}
    1.79 +
    1.80 +		class CVfnPtrOrigThunkCleanup : public ICleanupTask
    1.81 +		{
    1.82 +			CPageAlloc *m_Allocator;
    1.83 +			void *m_AddrToFree;
    1.84 +
    1.85 +		public:
    1.86 +			CVfnPtrOrigThunkCleanup(CPageAlloc *allocator, void *addrToFree) :
    1.87 +				m_Allocator(allocator), m_AddrToFree(addrToFree)
    1.88 +			{
    1.89 +			}
    1.90 +
    1.91 +			virtual void CleanupAndDeleteThis()
    1.92 +			{
    1.93 +				m_Allocator->Free(m_AddrToFree);
    1.94 +				delete this;
    1.95 +			}
    1.96 +		};
    1.97 +
    1.98 +		ICleanupTask* CVfnPtr::GetCleanupTask()
    1.99 +		{
   1.100 +			if (m_OrigCallThunk != NULL)
   1.101 +			{
   1.102 +				return new CVfnPtrOrigThunkCleanup(&ms_AlignedPageAllocator, m_OrigCallThunk);
   1.103 +			}
   1.104 +			else
   1.105 +			{
   1.106 +				return NULL;
   1.107 +			}
   1.108 +		}
   1.109 +
   1.110 +		void* CVfnPtr::GetOrigCallAddr() const
   1.111 +		{
   1.112 +			if (m_OrigCallThunk)
   1.113 +			{
   1.114 +				return m_OrigCallThunk;
   1.115 +			}
   1.116 +			else
   1.117 +			{
   1.118 +				return m_OrigEntry;
   1.119 +			}
   1.120 +		}
   1.121 +
   1.122 +		bool CVfnPtr::Revert()
   1.123 +		{
   1.124 +			// Only patch the vfnptr back if the module is still in memory
   1.125 +			// If it's not, do not remove stuff like we did before
   1.126 +			// First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
   1.127 +			// confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
   1.128 +			if (ModuleInMemory(reinterpret_cast<char*>(m_Ptr), SH_PTRSIZE))
   1.129 +			{
   1.130 +				return Patch(m_OrigEntry);
   1.131 +			}
   1.132 +			else
   1.133 +			{
   1.134 +				return true;
   1.135 +			}
   1.136 +		}
   1.137 +
   1.138 +		CIface *CVfnPtr::FindIface(void *iface)
   1.139 +		{
   1.140 +			List<CIface>::iterator iter = m_IfaceList.find(iface);
   1.141 +			if (iter == m_IfaceList.end())
   1.142 +				return NULL;
   1.143 +			else
   1.144 +				return &(*iter);
   1.145 +		}
   1.146 +
   1.147 +		void CVfnPtr::AddHookMan(CHookManager *pHookMan)
   1.148 +		{
   1.149 +			List<CHookManager*>::iterator iter;
   1.150 +
   1.151 +			// Don't accept invalid hook managers
   1.152 +			if (!*pHookMan)
   1.153 +				return;
   1.154 +
   1.155 +			// Check whether this hook manager already exists; if yes, ignore.
   1.156 +			iter = m_HookMans.find(pHookMan);
   1.157 +			if (iter != m_HookMans.end())
   1.158 +				return;
   1.159 +
   1.160 +			// It doesn't -> add it. Add it to the end of its version group.
   1.161 +			for (iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
   1.162 +			{
   1.163 +				if ((*iter)->GetVersion() < pHookMan->GetVersion())
   1.164 +					break;
   1.165 +			}
   1.166 +
   1.167 +			bool isBeginning = iter == m_HookMans.begin();
   1.168 +
   1.169 +			m_HookMans.insert(iter, pHookMan);
   1.170 +
   1.171 +			if (isBeginning)
   1.172 +			{
   1.173 +				pHookMan->IncrRef(this);
   1.174 +				if (m_HookMans.size() > 1)
   1.175 +				{
   1.176 +					// If another hookman was used until now but this one is better
   1.177 +					// (which it is because it's the first -> it has a higher version)
   1.178 +					// -> switch!
   1.179 +
   1.180 +					List<CHookManager*>::iterator second = m_HookMans.begin();
   1.181 +					++second;
   1.182 +
   1.183 +					(*second)->DecrRef(this);
   1.184 +				}
   1.185 +
   1.186 +				// Make sure that this vfnptr points at it
   1.187 +				Patch(pHookMan->GetHookFunc());
   1.188 +			}
   1.189 +		}
   1.190 +
   1.191 +		bool CVfnPtr::HookManRemoved(CHookManager *pHookMan)
   1.192 +		{
   1.193 +			// Don't accept invalid hook managers
   1.194 +			if (!*pHookMan)
   1.195 +				return true;
   1.196 +
   1.197 +			List<CHookManager*>::iterator iter = m_HookMans.find(pHookMan);
   1.198 +			if (iter == m_HookMans.end())
   1.199 +				return true;							// Didn't exist here anyway
   1.200 +
   1.201 +			if (iter == m_HookMans.begin())
   1.202 +			{
   1.203 +				// It is the first one!
   1.204 +				pHookMan->DecrRef(this);
   1.205 +				m_HookMans.erase(iter);
   1.206 +
   1.207 +				if (m_HookMans.empty())
   1.208 +					return false;				// No more hookmans -> let SH delete us
   1.209 +
   1.210 +				// Activate second -> now first hookman
   1.211 +				m_HookMans.front()->IncrRef(this);
   1.212 +				Patch(m_HookMans.front()->GetHookFunc());
   1.213 +			}
   1.214 +			else
   1.215 +			{
   1.216 +				m_HookMans.erase(iter);
   1.217 +			}
   1.218 +			return true;
   1.219 +		}
   1.220 +
   1.221 +		bool CVfnPtr::Patch(void *newValue)
   1.222 +		{
   1.223 +			if (!SetMemAccess(m_Ptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE))
   1.224 +			{
   1.225 +				return false;
   1.226 +			}
   1.227 +
   1.228 +			*reinterpret_cast<void**>(m_Ptr) = newValue;
   1.229 +
   1.230 +			return true;
   1.231 +		}
   1.232 +
   1.233 +		CIface &CVfnPtr::GetIface(void *iface)
   1.234 +		{
   1.235 +			List<CIface>::iterator iter = m_IfaceList.find(iface);
   1.236 +			if (iter == m_IfaceList.end())
   1.237 +			{
   1.238 +				CIface newIface(iface);
   1.239 +				if (iface == NULL)
   1.240 +				{
   1.241 +					m_IfaceList.push_front(newIface);
   1.242 +					return m_IfaceList.front();
   1.243 +				}
   1.244 +				else
   1.245 +				{
   1.246 +					m_IfaceList.push_back(newIface);
   1.247 +					return m_IfaceList.back();
   1.248 +				}
   1.249 +			}
   1.250 +			else
   1.251 +			{
   1.252 +				return *iter;
   1.253 +			}
   1.254 +		}
   1.255 +	}
   1.256 +}