Merge.
1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2010 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
11 #include "sourcehook_impl.h"
17 CPageAlloc CVfnPtr::ms_AlignedPageAllocator(8);
19 CVfnPtr::CVfnPtr(void *ptr)
20 : m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(m_Ptr)),
27 if (!m_HookMans.empty())
28 m_HookMans.front()->DecrRef(this);
33 // Initalize the vfn ptr info object.
35 // If we're running on GCC and the original vtable entry is odd
37 // The hook functions use non-virtual member function pointers to
38 // call the original function (see sourcehook.hxx, SH_SETUP_MFP and SH_CALL_ORIG)
39 // GCC has the same format for virtual and non-virtual MFPs:
40 // virtual ones have an odd value as "funcptr" where the offset into the vtable is encoded
41 // non-virtual ones have the direct function pointer as "funcptr", assuming that it is even
43 // When m_OrigEntry is odd, GCC's runtime MFP calling code interpretes it as a virtual
44 // function call though we want to call the function in a non-virtual way
45 // (the original function call mechanism between the pre and post hook loop has to bypass
46 // the virtual calling mechanism in order to call the original function).
48 #if SH_COMP==SH_COMP_GCC
49 if ((((ptrdiff_t)m_OrigEntry) & 1) != 0)
54 // We only have code for IA32 atm!
58 // Generate a new thunk
59 m_OrigCallThunk = ms_AlignedPageAllocator.Alloc(5);
60 ms_AlignedPageAllocator.SetRW(m_OrigCallThunk);
62 unsigned char* thunkBase = reinterpret_cast<unsigned char*>(m_OrigCallThunk);
63 *(thunkBase + 0) = 0xE9; // offset jump, immediate operand
64 ptrdiff_t *offsetAddr = reinterpret_cast<ptrdiff_t*>(thunkBase + 1);
66 // destination = src + offset + 5
67 // <=> offset = destination - src - 5
69 (reinterpret_cast<unsigned char*>(m_OrigEntry) - thunkBase) - 5;
71 ms_AlignedPageAllocator.SetRE(m_OrigCallThunk);
77 class CVfnPtrOrigThunkCleanup : public ICleanupTask
79 CPageAlloc *m_Allocator;
83 CVfnPtrOrigThunkCleanup(CPageAlloc *allocator, void *addrToFree) :
84 m_Allocator(allocator), m_AddrToFree(addrToFree)
88 virtual void CleanupAndDeleteThis()
90 m_Allocator->Free(m_AddrToFree);
95 ICleanupTask* CVfnPtr::GetCleanupTask()
97 if (m_OrigCallThunk != NULL)
99 return new CVfnPtrOrigThunkCleanup(&ms_AlignedPageAllocator, m_OrigCallThunk);
107 void* CVfnPtr::GetOrigCallAddr() const
111 return m_OrigCallThunk;
119 bool CVfnPtr::Revert()
121 // Only patch the vfnptr back if the module is still in memory
122 // If it's not, do not remove stuff like we did before
123 // First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
124 // confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
125 if (ModuleInMemory(reinterpret_cast<char*>(m_Ptr), SH_PTRSIZE))
127 return Patch(m_OrigEntry);
135 CIface *CVfnPtr::FindIface(void *iface)
137 List<CIface>::iterator iter = m_IfaceList.find(iface);
138 if (iter == m_IfaceList.end())
144 void CVfnPtr::AddHookMan(CHookManager *pHookMan)
146 List<CHookManager*>::iterator iter;
148 // Don't accept invalid hook managers
152 // Check whether this hook manager already exists; if yes, ignore.
153 iter = m_HookMans.find(pHookMan);
154 if (iter != m_HookMans.end())
157 // It doesn't -> add it. Add it to the end of its version group.
158 for (iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
160 if ((*iter)->GetVersion() < pHookMan->GetVersion())
164 bool isBeginning = iter == m_HookMans.begin();
166 m_HookMans.insert(iter, pHookMan);
170 pHookMan->IncrRef(this);
171 if (m_HookMans.size() > 1)
173 // If another hookman was used until now but this one is better
174 // (which it is because it's the first -> it has a higher version)
177 List<CHookManager*>::iterator second = m_HookMans.begin();
180 (*second)->DecrRef(this);
183 // Make sure that this vfnptr points at it
184 Patch(pHookMan->GetHookFunc());
188 bool CVfnPtr::HookManRemoved(CHookManager *pHookMan)
190 // Don't accept invalid hook managers
194 List<CHookManager*>::iterator iter = m_HookMans.find(pHookMan);
195 if (iter == m_HookMans.end())
196 return true; // Didn't exist here anyway
198 if (iter == m_HookMans.begin())
200 // It is the first one!
201 pHookMan->DecrRef(this);
202 m_HookMans.erase(iter);
204 if (m_HookMans.empty())
205 return false; // No more hookmans -> let SH delete us
207 // Activate second -> now first hookman
208 m_HookMans.front()->IncrRef(this);
209 Patch(m_HookMans.front()->GetHookFunc());
213 m_HookMans.erase(iter);
218 bool CVfnPtr::Patch(void *newValue)
220 if (!MakePageWritable(m_Ptr))
225 *reinterpret_cast<void**>(m_Ptr) = newValue;
230 CIface &CVfnPtr::GetIface(void *iface)
232 List<CIface>::iterator iter = m_IfaceList.find(iface);
233 if (iter == m_IfaceList.end())
235 CIface newIface(iface);
238 m_IfaceList.push_front(newIface);
239 return m_IfaceList.front();
243 m_IfaceList.push_back(newIface);
244 return m_IfaceList.back();