Fixed problems with object params on gcc when object has a destructor (forcedbyref)
1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * Contributor(s): Borja "faluco" Ferav (many thanks for assitance!)
9 * David "BAILOPAN" Anderson
10 * ============================
13 // recommended literature:
14 // http://www.cs.umbc.edu/~chang/cs313.s02/stack.shtml
15 // http://www.angelcode.com/dev/callconv/callconv.html
16 // http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
18 #include "sourcehook_impl.h"
19 #include "sourcehook_hookmangen.h"
20 #include "sourcehook_hookmangen_x86.h"
21 #include "sh_memory.h"
22 #include <stdarg.h> // we might need the address of vsnprintf
25 #if SH_COMP == SH_COMP_MSVC
27 # define MSVC_ONLY(x) x
28 #elif SH_COMP == SH_COMP_GCC
29 # define GCC_ONLY(x) x
33 // :TODO: test BIG vtable indices
39 CPageAlloc GenBuffer::ms_Allocator;
42 jit_int32_t DownCastPtr(T ptr)
44 return reinterpret_cast<jit_int32_t>(ptr);
47 jit_uint32_t DownCastSize(size_t size)
49 return static_cast<jit_uint32_t>(size);
52 GenContext::GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr)
53 : m_GeneratedPubFunc(NULL), m_OrigProto(proto), m_Proto(proto), m_VtblOffs(vtbl_offs),
54 m_VtblIdx(vtbl_idx), m_SHPtr(pSHPtr), m_pHI(NULL), m_HookfuncVfnptr(NULL), m_RegCounter(0)
57 m_HookfuncVfnptr = new void*;
58 m_BuiltPI = new ProtoInfo;
59 m_BuiltPI_Params = NULL;
60 m_BuiltPI_Params2 = NULL;
63 GenContext::~GenContext()
67 delete m_HookfuncVfnptr;
71 void GenContext::Clear()
77 delete m_BuiltPI_Params;
78 m_BuiltPI_Params = NULL;
80 if (m_BuiltPI_Params2)
82 delete m_BuiltPI_Params2;
83 m_BuiltPI_Params2 = NULL;
87 void GenContext::BuildProtoInfo()
89 m_BuiltPI->convention = m_Proto.GetConvention();
90 m_BuiltPI->numOfParams = m_Proto.GetNumOfParams();
92 m_BuiltPI->retPassInfo.size = m_Proto.GetRet().size;
93 m_BuiltPI->retPassInfo.type = m_Proto.GetRet().type;
94 m_BuiltPI->retPassInfo.flags = m_Proto.GetRet().flags;
95 m_BuiltPI->retPassInfo2.pNormalCtor = m_Proto.GetRet().pNormalCtor;
96 m_BuiltPI->retPassInfo2.pCopyCtor = m_Proto.GetRet().pCopyCtor;
97 m_BuiltPI->retPassInfo2.pDtor = m_Proto.GetRet().pDtor;
98 m_BuiltPI->retPassInfo2.pAssignOperator = m_Proto.GetRet().pAssignOperator;
100 if (m_BuiltPI_Params)
101 delete m_BuiltPI_Params;
102 m_BuiltPI_Params = new PassInfo[m_BuiltPI->numOfParams + 1];
103 if (m_BuiltPI_Params2)
104 delete m_BuiltPI_Params2;
105 m_BuiltPI_Params2 = new PassInfo::V2Info[m_BuiltPI->numOfParams + 1];
107 m_BuiltPI_Params[0].size = 1; // Version 1
108 m_BuiltPI_Params[0].type = 0;
109 m_BuiltPI_Params[0].flags = 0;
111 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
113 m_BuiltPI_Params[i+1].size = m_Proto.GetParam(i).size;
114 m_BuiltPI_Params[i+1].type = m_Proto.GetParam(i).type;
115 m_BuiltPI_Params[i+1].flags = m_Proto.GetParam(i).flags;
117 m_BuiltPI_Params2[i+1].pNormalCtor = m_Proto.GetParam(i).pNormalCtor;
118 m_BuiltPI_Params2[i+1].pCopyCtor = m_Proto.GetParam(i).pCopyCtor;
119 m_BuiltPI_Params2[i+1].pDtor = m_Proto.GetParam(i).pDtor;
120 m_BuiltPI_Params2[i+1].pAssignOperator = m_Proto.GetParam(i).pAssignOperator;
123 m_BuiltPI->paramsPassInfo = m_BuiltPI_Params;
124 m_BuiltPI->paramsPassInfo2 = m_BuiltPI_Params2;
127 jit_int32_t GenContext::GetRealSize(const IntPassInfo &info)
129 if (info.flags & (PassInfo::PassFlag_ByRef | PassFlag_ForcedByRef))
133 return static_cast<jit_int32_t>(info.size);
136 jit_int32_t GenContext::AlignSize(jit_int32_t x, jit_int32_t boundary)
138 if (x % boundary != 0)
139 x = (x & ~(boundary-1)) + boundary;
143 // Computes size on the stack
144 jit_int32_t GenContext::GetParamStackSize(const IntPassInfo &info)
146 // Align up to 4 byte boundaries
147 return AlignSize(GetRealSize(info), 4);
150 jit_int8_t GenContext::NextRegEBX_ECX_EDX()
152 switch ((m_RegCounter++) % 3)
165 void GenContext::BitwiseCopy_Setup()
171 IA32_Cld(&m_HookFunc);
172 IA32_Push_Reg(&m_HookFunc, REG_EDI);
173 IA32_Push_Reg(&m_HookFunc, REG_ESI);
176 void GenContext::BitwiseCopy_Do(size_t size)
178 jit_uint32_t dwords = DownCastSize(size) / 4;
179 jit_uint32_t bytes = DownCastSize(size) % 4;
192 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
193 IA32_Rep(&m_HookFunc);
194 IA32_Movsd(&m_HookFunc);
198 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
199 IA32_Rep(&m_HookFunc);
200 IA32_Movsb(&m_HookFunc);
202 IA32_Pop_Reg(&m_HookFunc, REG_ESI);
203 IA32_Pop_Reg(&m_HookFunc, REG_EDI);
206 short GenContext::GetParamsTotalStackSize()
209 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
211 acc += GetParamStackSize(m_Proto.GetParam(i));
217 short GenContext::GetForcedByRefParamOffset(int p)
220 for (int i = 0; i < p; ++i)
222 if (m_Proto.GetParam(i).flags & PassFlag_ForcedByRef)
223 off += AlignSize(m_Proto.GetParam(i).size, 4);
228 short GenContext::GetForcedByRefParamsSize()
230 return GetForcedByRefParamOffset(m_Proto.GetNumOfParams());
233 jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
235 // push [ebp+<offset>]
236 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_offset);
241 jit_int32_t GenContext::PushBasic(jit_int32_t param_offset, const IntPassInfo &pi)
249 SH_ASSERT(0, ("Unsupported!"));
252 reg = NextRegEBX_ECX_EDX();
253 //movzx reg, BYTE PTR [ebp+<offset>]
255 IA32_Movzx_Reg32_Rm8_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
256 IA32_Push_Reg(&m_HookFunc, reg);
260 reg = NextRegEBX_ECX_EDX();
261 //movzx reg, WORD PTR [ebp+<offset>]
263 m_HookFunc.write_ubyte(IA32_16BIT_PREFIX);
264 IA32_Movzx_Reg32_Rm16_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
265 IA32_Push_Reg(&m_HookFunc, reg);
269 reg = NextRegEBX_ECX_EDX();
270 //mov reg, DWORD PTR [ebp+<offset>]
272 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
273 IA32_Push_Reg(&m_HookFunc, reg);
277 reg = NextRegEBX_ECX_EDX();
278 reg2 = NextRegEBX_ECX_EDX();
279 //mov reg, DWORD PTR [ebp+<offset>+4]
280 //mov reg2, DWORD PTR [ebp+<offset>]
284 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset+4);
285 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg2, REG_EBP, param_offset);
286 IA32_Push_Reg(&m_HookFunc, reg);
287 IA32_Push_Reg(&m_HookFunc, reg2);
293 jit_int32_t GenContext::PushFloat(jit_int32_t param_offset, const IntPassInfo &pi)
298 SH_ASSERT(0, ("Unsupported!"));
301 //fld DWORD PTR [ebp+<offset>]
303 //fstp DWORD PTR [esp]
304 IA32_Fld_Mem32_DispAuto(&m_HookFunc, REG_EBP, param_offset);
305 IA32_Push_Reg(&m_HookFunc, NextRegEBX_ECX_EDX());
306 IA32_Fstp_Mem32_ESP(&m_HookFunc);
309 //fld QWORD PTR [ebp+<offset>]
311 //fstp QWORD PTR [esp]
312 IA32_Fld_Mem64_DispAuto(&m_HookFunc, REG_EBP, param_offset);
313 IA32_Sub_Rm_Imm8(&m_HookFunc, REG_ESP, 8, MOD_REG);
314 IA32_Fstp_Mem64_ESP(&m_HookFunc);
319 jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t place_fbrr)
321 if ((pi.flags & PassFlag_ForcedByRef) == 0)
323 // make room on the stack
325 IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetParamStackSize(pi), MOD_REG);
328 // if there is a copy constructor..
333 // this= target addr = forcedbyref ? ebp+place_fbrr : esp+12
334 // call copy constructor
337 IA32_Push_Reg(&m_HookFunc, REG_EAX);
339 if (pi.flags & PassFlag_ForcedByRef)
340 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
342 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
344 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
345 IA32_Push_Reg(&m_HookFunc, REG_EAX);
346 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
347 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(pi.pCopyCtor));
348 IA32_Call_Reg(&m_HookFunc, REG_EDX);
349 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
350 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
360 // lea edi, [ebp_place_fbrr]
362 // lea edi, [esp+8] - bc_setup pushed two regs onto the stack!
363 //lea esi, [ebp+<offs>]
364 if (pi.flags & PassFlag_ForcedByRef)
365 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, place_fbrr);
367 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
369 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
371 BitwiseCopy_Do(pi.size);
374 // forcedref: push reference to ebp+place_fbrr
375 if (pi.flags & PassFlag_ForcedByRef)
377 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
378 IA32_Push_Reg(&m_HookFunc, REG_ECX);
382 return DownCastSize(pi.size);
385 void GenContext::DestroyParams(jit_int32_t fbrr_base)
387 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
389 const IntPassInfo &pi = m_Proto.GetParam(i);
390 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
391 (pi.flags & PassInfo::PassFlag_ByVal) && (pi.flags & PassFlag_ForcedByRef))
393 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, fbrr_base + GetForcedByRefParamOffset(i));
394 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
395 IA32_Call_Reg(&m_HookFunc, REG_EAX);
400 // May not touch eax!
401 jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret,
402 jit_int32_t v_place_fbrr_base)
404 jit_int32_t added_to_stack = 0;
407 // compute the offset _after_ the last parameter
408 jit_int32_t cur_offset = param_base_offset;
409 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
411 cur_offset += GetParamStackSize(m_Proto.GetParam(i));
414 // push parameters in reverse order
415 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
417 const IntPassInfo &pi = m_Proto.GetParam(i);
418 cur_offset -= GetParamStackSize(pi);
419 if (pi.flags & PassInfo::PassFlag_ByVal)
423 case PassInfo::PassType_Basic:
424 ret = PushBasic(cur_offset, pi);
426 case PassInfo::PassType_Float:
427 ret = PushFloat(cur_offset, pi);
429 case PassInfo::PassType_Object:
430 ret = PushObject(cur_offset, pi, v_place_fbrr_base + GetForcedByRefParamOffset(i));
434 else if (pi.flags & PassInfo::PassFlag_ByRef)
436 PushRef(cur_offset, pi);
440 SH_ASSERT(0, ("Unsupported!"));
442 added_to_stack += ret;
445 return added_to_stack;
448 // It is IMPORTANT that PushMemRetPtr doesn't touch ecx and eax
449 jit_int32_t GenContext::PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret)
451 // Memory return support
452 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
454 // push address where to save it!
456 IA32_Lea_DispRegImmAuto(&m_HookFunc, reg, REG_EBP,
457 MemRetWithTempObj() ? v_place_for_memret : save_ret_to);
458 IA32_Push_Reg(&m_HookFunc, reg);
460 return (SH_COMP==SH_COMP_MSVC) ? 4 : 0; // varargs funcs on msvc might need this.
461 // gcc doesn't: callee cleans the memret ptr, caller the other params :s
466 void GenContext::SaveRetVal(int v_where, int v_place_for_memret)
468 size_t size = GetRealSize(m_Proto.GetRet());
471 // No return value -> nothing
475 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
477 // mov [ebp + v_plugin_ret], eax
478 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
485 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
487 if (MemRetWithTempObj())
489 // *v_where = *v_place_for_memret
491 // if we have an assign operator, use that
492 if (m_Proto.GetRet().pAssignOperator)
494 // lea edx, [ebp + v_place_for_memret] <-- src addr
495 // lea ecx, [ebp + v_where] <-- dest addr
501 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_place_for_memret);
502 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_where);
503 IA32_Push_Reg(&m_HookFunc, REG_EDX);
504 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
506 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
507 IA32_Call_Reg(&m_HookFunc, REG_EAX);
508 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
515 //lea edi, [evp+v_where] <-- destination
516 //lea esi, [ebp+v_place_for_memret] <-- src
517 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_where);
518 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_place_for_memret);
520 BitwiseCopy_Do(m_Proto.GetRet().size);
523 // Then: destruct *v_place_for_memret if required
524 if (m_Proto.GetRet().pDtor)
526 //lea ecx, [ebp+v_place_for_memret]
531 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_place_for_memret);
532 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
533 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
534 IA32_Call_Reg(&m_HookFunc, REG_EAX);
535 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
540 // Already copied to correct address -> we're done
545 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
548 IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
550 IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
552 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
556 // size <= 4: return in EAX
557 // We align <4 sizes up to 4
560 // mov [ebp + v_plugin_ret], eax
561 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
565 // size <= 4: return in EAX:EDX
566 // We align 4<x<8 sizes up to 8
568 // mov [ebp + v_plugin_ret], eax
569 // mov [ebp + v_plugin_ret + 4], edx
570 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
571 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
574 else if (m_Proto.GetRet().type == PassInfo::PassType_Object)
576 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)
580 // size <= 4: return in EAX
581 // We align <4 sizes up to 4
583 // mov [ebp + v_plugin_ret], eax
584 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
588 // size <= 4: return in EAX:EDX
589 // We align 4<x<8 sizes up to 8
591 // mov [ebp + v_plugin_ret], eax
592 // mov [ebp + v_plugin_ret + 4], edx
593 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
594 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
598 SH_ASSERT(0, ("RetReg and size > 8 !"));
604 bool GenContext::MemRetWithTempObj()
606 // Memory return AND (has destructor OR has assign operator)
607 return ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
608 && (m_Proto.GetRet().flags & (PassInfo::PassFlag_ODtor | PassInfo::PassFlag_AssignOp)));
611 void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
613 // only for non-void functions!
614 if (m_Proto.GetRet().size == 0)
617 // if (cur_res >= MRES_OVERRIDE)
618 // *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
621 // cmp eax,MRES_OVERRIDE
623 // pContext->GetOverrideRetPtr() --> overrideretptr in eax
628 jitoffs_t tmppos, counter;
630 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
632 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
633 tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
634 m_HookFunc.start_count(counter);
636 // eax = pContext->GetOverrideRetPtr()
643 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
644 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
646 // vtbloffs=0, vtblidx=1
647 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
648 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
649 IA32_Call_Reg(&m_HookFunc, REG_EAX);
650 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
654 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
656 // mov ecx, [ebp+v_plugin_ret]
658 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
659 IA32_Mov_Rm_Reg(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
663 if (m_Proto.GetRet().pAssignOperator)
665 // lea edx, [ebp + v_plugin_ret]
666 // push edx <-- src addr
667 // msvc: ecx = eax <-- dest addr
668 // gcc: push eax <-- dest addr
672 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
673 IA32_Push_Reg(&m_HookFunc, REG_EDX);
674 #if SH_COMP == SH_COMP_MSVC
675 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
676 #elif SH_COMP == SH_COMP_GCC
677 IA32_Push_Reg(&m_HookFunc, REG_EAX);
680 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
681 IA32_Call_Reg(&m_HookFunc, REG_EAX);
682 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
689 //mov edi, eax <-- destination
690 //lea esi, [ebp+v_plugin_ret] <-- src
691 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
692 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
694 BitwiseCopy_Do(m_Proto.GetRet().size);
698 m_HookFunc.end_count(counter);
699 m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
702 void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
704 // only for non-void functions!
705 if (m_Proto.GetRet().size == 0)
708 // retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
710 // OverrideRetPtr: vtblidx = 1
711 // OrigRetPtr: vtbldix = 2
712 // vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
715 // eax = pContext->GetOverrideRetPtr()
719 // eax = (status < MRES_OVERRIDE) ? 1 : 0
721 // cmp [ebp + v_status], MRES_OVERRIDE
722 // setl al <-- setcc optimization for ternary operators,
725 // lea eax, [4*eax + 0x4]
734 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
735 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
736 IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
737 IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
738 IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
740 IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
742 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
743 IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
745 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
746 IA32_Call_Reg(&m_HookFunc, REG_EDX);
747 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
749 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
752 void GenContext::DoReturn(int v_retptr, int v_memret_outaddr)
754 size_t size = m_Proto.GetRet().size;
758 // Get real ret pointer into ecx
759 // mov ecx, [ebp + v_ret_ptr]
760 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
762 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
765 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
770 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
773 IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
775 IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
777 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic ||
778 ((m_Proto.GetRet().type == PassInfo::PassType_Object) && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)) )
782 // size <= 4: return in EAX
783 // We align <4 sizes up to 4
786 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
790 // size <= 4: return in EAX:EDX
791 // We align 4<x<8 sizes up to 8
795 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
796 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
800 // size >8: return in memory
805 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
807 // *memret_outaddr = plugin_ret
808 if (m_Proto.GetRet().pCopyCtor)
810 // mov edx, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
811 // push edx <-- src addr
812 // msvc: ecx = [ebp + v_memret_outaddr] <-- dest addr
813 // gcc: push [ebp + v_memret_outaddr] <-- dest addr
817 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_REG);
818 IA32_Push_Reg(&m_HookFunc, REG_EDX);
820 #if SH_COMP == SH_COMP_MSVC
821 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_memret_outaddr);
822 #elif SH_COMP == SH_COMP_GCC
823 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, v_memret_outaddr);
826 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pCopyCtor));
827 IA32_Call_Reg(&m_HookFunc, REG_EAX);
828 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
835 //mov edi, [ebp+v_memret_outaddr] <-- destination
836 //mov esi, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
837 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDI, REG_EBP, v_memret_outaddr);
838 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESI, REG_ECX, MOD_REG);
840 BitwiseCopy_Do(m_Proto.GetRet().size);
843 // In both cases: return the pointer in EAX
844 // mov eax, [ebp + v_memret_outaddr]
845 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_memret_outaddr);
849 void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
850 int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf)
852 jitoffs_t counter, tmppos;
853 jitoffs_t counter2, tmppos2;
855 jitoffs_t loop_begin_counter;
857 // prev_res = MRES_IGNORED
858 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
860 m_HookFunc.start_count(loop_begin_counter);
862 // eax = pContext->GetNext()
869 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
870 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
872 // vtbloffs=0, vtblidx=0
873 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
874 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
875 IA32_Call_Reg(&m_HookFunc, REG_EAX);
876 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
881 IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
882 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
883 m_HookFunc.start_count(counter);
885 // prev_res = MRES_IGNORED
886 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res);
893 // eax = [eax+2*SIZE_PTR]
897 jit_int32_t caller_clean_bytes = 0; // gcc always, msvc never (hooks never have varargs!)
898 // vafmt: push va_buf
899 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
901 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
902 IA32_Push_Reg(&m_HookFunc, REG_ECX);
903 caller_clean_bytes += SIZE_PTR;
906 caller_clean_bytes += PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
908 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
909 if (SH_COMP == SH_COMP_GCC)
910 IA32_Push_Reg(&m_HookFunc, REG_ECX);
911 caller_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
912 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
913 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
914 IA32_Call_Reg(&m_HookFunc, REG_EAX);
916 DestroyParams(v_place_fbrr_base);
918 SaveRetVal(v_plugin_ret, v_place_for_memret);
920 // cleanup (gcc only)
922 if (SH_COMP == SH_COMP_GCC)
923 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
925 // process meta return:
926 // prev_res = cur_res
927 // if (cur_res > status) status = cur_res;
937 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
938 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
939 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
941 IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
942 tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
943 m_HookFunc.start_count(counter2);
945 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
947 m_HookFunc.end_count(counter2);
948 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
950 // process retval for non-void functions
951 ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
953 // jump back to loop begin
954 tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
955 m_HookFunc.end_count(loop_begin_counter);
956 m_HookFunc.rewrite(tmppos2, -static_cast<jit_int32_t>(loop_begin_counter));
958 m_HookFunc.end_count(counter);
959 m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
962 void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
963 int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf)
965 jitoffs_t counter, tmppos;
966 jitoffs_t counter2, tmppos2;
967 jitoffs_t counter3, tmppos3;
969 // if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
970 // *v_orig_ret = orig_call()
972 // *v_orig_ret = *v_override_ret
975 // cmp eax, MRES_SUPERCEDE
977 // call pContext->ShouldCallOrig()
978 // test al, al !! important: al, not eax! bool is only stored in the LSbyte
982 // SaveRet(v_orig_ret)
983 // jmp skip_dont_call:
986 // *v_orig_ret = *v_override_ret
989 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
990 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
992 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
993 m_HookFunc.start_count(counter);
995 // eax = pContext->ShouldCallOrig()
999 // eax = [eax + 3*SIZE_PTR]
1002 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
1003 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1005 // vtbloffs=0, vtblidx=3
1006 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1007 IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
1008 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1009 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1011 IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
1012 tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
1013 m_HookFunc.start_count(counter2);
1015 jit_int32_t caller_clean_bytes = 0; // gcc always, msvc when cdecl-like (varargs)
1017 // vafmt: push va_buf, then "%s"
1018 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1020 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
1021 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1022 IA32_Push_Imm32(&m_HookFunc, DownCastPtr("%s"));
1023 caller_clean_bytes += 2*SIZE_PTR;
1027 caller_clean_bytes += PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
1030 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
1031 if (SH_COMP == SH_COMP_GCC)
1033 // on gcc/mingw, this is the first parameter
1034 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1035 // on msvc without varargs, simply leave it in ecx
1037 // actually, if we're returning in memory, this pointer is the second param
1038 // and the memret pointer is the real first parameter
1039 caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1043 // On msvc, if we're returning in memory, the memret pointer is the first parameter
1044 caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1046 // actually, with varargs, the this pointer is the first param and the memret ptr
1047 // is the second one
1048 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1050 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1055 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
1056 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1059 if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1060 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
1062 DestroyParams(v_place_fbrr_base);
1065 SaveRetVal(v_orig_ret, v_place_for_memret);
1067 // Skip don't call variant
1068 tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
1069 m_HookFunc.start_count(counter3);
1073 m_HookFunc.end_count(counter);
1074 m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
1076 m_HookFunc.end_count(counter2);
1077 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
1079 // *v_orig_ret = *v_override_ret
1080 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
1082 // mov ecx, [ebp + v_override_ret]
1083 // mov [ebp + v_orig_ret], ecx
1085 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1086 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_ECX, v_orig_ret);
1090 if (m_Proto.GetRet().pAssignOperator)
1092 // lea edx, [ebp + v_override_ret] <-- src addr
1093 // lea ecx, [ebp + v_orig_ret] <-- dest addr
1094 // push edx <-- src addr
1099 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
1100 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1101 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1102 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1104 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
1105 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1106 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1111 BitwiseCopy_Setup();
1113 //lea edi, [ebp+v_orig_ret] <-- destination
1114 //lea esi, [ebp+v_override_ret] <-- src
1115 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
1116 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
1118 BitwiseCopy_Do(m_Proto.GetRet().size);
1122 // skip don't call label target:
1123 m_HookFunc.end_count(counter3);
1124 m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
1127 // Sets *v_pContext to return value
1128 void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret,
1129 int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
1130 int v_this, int v_pContext)
1132 // call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
1133 // &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
1134 // The last two params are null for void funcs.
1136 if (m_Proto.GetRet().size == 0)
1139 IA32_Push_Imm8(&m_HookFunc, 0); // orig_ret
1140 IA32_Push_Imm8(&m_HookFunc, 0); // override_ret
1144 // orig_ret and override_ret
1145 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
1146 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
1147 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1148 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1151 // cur_res and prev_res
1152 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1153 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
1154 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1155 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1157 // status and vfnptr_origentry
1158 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1159 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
1160 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1161 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1164 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
1165 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1168 // *(this + vtbloffs) + SIZE_PTR*vtblidx
1169 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this); // get this into ecx (gcc!)
1170 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
1171 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
1172 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1175 IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1176 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1177 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1180 #if SH_COMP == SH_COMP_GCC
1181 // on gcc/mingw, this is the first parameter
1182 GCC_ONLY(IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr)));
1183 #elif SH_COMP == SH_COMP_MSVC
1184 // on msvc, it's ecx
1185 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1188 // call the function. vtbloffs = 0, vtblidx = 19
1189 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1190 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1191 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
1193 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1195 // on gcc/mingw, we have to clean up after the call
1196 // 9 params + hidden thisptr param
1197 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG));
1199 // store return value
1200 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
1203 void GenContext::CallEndContext(int v_pContext)
1206 // shptr->EndContext(pContex)
1207 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
1208 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1211 #if SH_COMP == SH_COMP_GCC
1212 // on gcc/mingw, this is the first parameter
1213 IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
1214 #elif SH_COMP == SH_COMP_MSVC
1215 // on msvc, it's ecx
1216 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1219 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1220 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1221 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
1223 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1225 // on gcc/mingw, we have to clean up after the call
1226 // 1 param + hidden thisptr param
1227 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1230 void GenContext::ResetFrame(jit_int32_t startOffset)
1232 m_HookFunc_FrameOffset = startOffset;
1233 m_HookFunc_FrameVarsSize = 0;
1236 jit_int32_t GenContext::AddVarToFrame(jit_int32_t size)
1238 m_HookFunc_FrameOffset -= size;
1239 m_HookFunc_FrameVarsSize += size;
1240 return m_HookFunc_FrameOffset;
1243 jit_int32_t GenContext::ComputeVarsSize()
1245 return m_HookFunc_FrameVarsSize;
1248 void * GenContext::GenerateHookFunc()
1251 IA32_Push_Reg(&m_HookFunc, REG_EBP);
1252 IA32_Push_Reg(&m_HookFunc, REG_EBX);
1253 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
1255 jit_int32_t v_this = 0;
1256 jit_int32_t param_base_offs = 0;
1257 if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1259 // gcc or msvc with varargs:
1260 v_this = 12; // first param
1261 param_base_offs = 16;
1266 // on msvc without varargs, save thisptr
1268 param_base_offs = 12;
1269 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1270 ResetFrame(-4); // start placing local vars on offset -4
1271 // because there already is the thisptr variable
1274 // ********************** stack frame **********************
1275 // MSVC without varargs
1276 // second param (gcc: first real param) ebp + 16
1277 // first param (gcc: thisptr) ebp + 12
1278 // ret address: ebp + 8
1279 // caller's ebp ebp + 4
1281 // MSVC ONLY: current this ebp - 4
1282 // void *vfnptr_origentry ebp - 4 -4
1283 // META_RES status = MRES_IGNORED ebp - 8 -4
1284 // META_RES prev_res ebp - 12 -4
1285 // META_RES cur_res ebp - 16 -4
1286 // IMyDelegate *iter ebp - 20 -4
1287 // IHookContext *pContext ebp - 24 -4
1288 // == 3 ptrs + 3 enums = 24 bytes
1291 // my_rettype *ret_ptr ebp - 28 -4
1292 // my_rettype orig_ret ebp - 28 - sizeof(my_rettype) -4
1293 // my_rettype override_ret ebp - 28 - sizeof(my_rettype)*2 -4
1294 // my_rettype plugin_ret ebp - 28 - sizeof(my_rettype)*3 -4
1295 // == + 3 * sizeof(my_rettype) bytes
1298 // my_rettype place_for_memret ebp - 28 - sizeof(my_rettype)*4 -4
1300 // gcc only: if required:
1301 // place forced byref params ebp - 28 - sizeof(my_rettype)*{4 or 5}
1305 // char va_buf[something];
1308 const jit_int8_t v_vfnptr_origentry = AddVarToFrame(SIZE_PTR);
1309 const jit_int8_t v_status = AddVarToFrame(sizeof(META_RES));
1310 const jit_int8_t v_prev_res = AddVarToFrame(sizeof(META_RES));
1311 const jit_int8_t v_cur_res = AddVarToFrame(sizeof(META_RES));
1312 const jit_int8_t v_iter = AddVarToFrame(SIZE_PTR);
1313 const jit_int8_t v_pContext = AddVarToFrame(SIZE_PTR);
1315 // Memory return: first param is the address
1316 jit_int32_t v_memret_addr = 0;
1317 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1319 if (SH_COMP == SH_COMP_GCC)
1321 // gcc: now: first param = mem ret addr
1322 // second param = this pointer
1323 // third param = actual first param
1326 param_base_offs += SIZE_PTR;
1330 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1334 // first param = this pointer
1335 // second param = mem ret addr
1336 // third param = actual first param
1338 // params_base_offs is already updated to point to after the this pointer
1339 v_memret_addr = param_base_offs;
1340 param_base_offs += SIZE_PTR;
1344 v_memret_addr = param_base_offs;
1345 param_base_offs += SIZE_PTR;
1350 jit_int32_t v_ret_ptr = 0;
1351 jit_int32_t v_orig_ret = 0;
1352 jit_int32_t v_override_ret = 0;
1353 jit_int32_t v_plugin_ret = 0;
1355 if (m_Proto.GetRet().size != 0)
1357 v_ret_ptr = AddVarToFrame(SIZE_PTR);
1358 v_orig_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1359 v_override_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1360 v_plugin_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1363 jit_int32_t v_place_for_memret = 0;
1364 if (MemRetWithTempObj())
1366 v_place_for_memret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1369 jit_int32_t v_place_fbrr_base = 0;
1370 if (SH_COMP == SH_COMP_GCC && GetForcedByRefParamsSize())
1372 v_place_fbrr_base = AddVarToFrame(GetForcedByRefParamsSize());
1375 // Only exists for varargs functions
1376 jit_int32_t v_va_argptr = 0;
1377 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1379 v_va_argptr = AddVarToFrame(SIZE_PTR);
1382 jit_int32_t v_va_buf = 0;
1383 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1385 v_va_buf = AddVarToFrame(SourceHook::STRBUF_LEN);
1388 IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, ComputeVarsSize(), MOD_REG);
1390 // init status localvar
1391 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
1393 // VarArgs: init argptr & format
1394 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1396 // argptr = first vararg param
1397 // lea eax, [ebp + param_base_offs + paramssize]
1400 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_base_offs + GetParamsTotalStackSize() + SIZE_PTR); // +SIZE_PTR: last const char * is not in protoinfo
1401 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_va_argptr);
1403 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1407 // push valist, fmt param, maxsize, buffer
1408 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1409 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_base_offs + GetParamsTotalStackSize()); // last given param (+4-4, see above)
1410 IA32_Push_Imm32(&m_HookFunc, SourceHook::STRBUF_LEN - 1);
1411 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
1412 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1415 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(&vsnprintf));
1416 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1419 IA32_Add_Rm_Imm32(&m_HookFunc, REG_ESP, 0x10, MOD_REG);
1421 // Set trailing zero
1422 IA32_Xor_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_REG);
1423 IA32_Mov_Rm8_Reg8_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_va_buf + SourceHook::STRBUF_LEN - 1);
1426 // Call constructors for ret vars if required
1427 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1428 m_Proto.GetRet().pNormalCtor)
1431 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1432 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1433 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1434 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1435 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1438 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1439 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1440 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1441 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1442 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1445 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1446 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1447 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1448 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1449 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1451 // _don't_ call a constructor for v_place_for_memret !
1454 // ********************** SetupHookLoop **********************
1455 CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
1456 v_this, v_pContext);
1458 // ********************** call pre hooks **********************
1459 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1460 v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1462 // ********************** call orig func **********************
1463 GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
1464 v_override_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1466 // ********************** call post hooks **********************
1467 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1468 v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1470 // ********************** end context and return **********************
1472 PrepareReturn(v_status, v_pContext, v_ret_ptr);
1474 CallEndContext(v_pContext);
1476 // Call destructors of byval object params which have a destructor
1477 jit_int32_t cur_param_pos = param_base_offs;
1478 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1480 const IntPassInfo &pi = m_Proto.GetParam(i);
1481 // GCC: NOT of forced byref params. the caller destructs those.
1482 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
1483 (pi.flags & PassInfo::PassFlag_ByVal) && !(pi.flags & PassFlag_ForcedByRef))
1485 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
1486 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
1487 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1489 cur_param_pos += GetParamStackSize(pi);
1492 DoReturn(v_ret_ptr, v_memret_addr);
1494 // Call destructors of orig_ret/ ...
1495 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1496 m_Proto.GetRet().pDtor)
1498 // Preserve return value in EAX(:EDX)
1499 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1500 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1502 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1503 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1504 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1505 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1506 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1508 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1509 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1510 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1511 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1512 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1514 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1515 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1516 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1517 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1518 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1520 IA32_Pop_Reg(&m_HookFunc, REG_EDX);
1521 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
1525 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
1526 IA32_Pop_Reg(&m_HookFunc, REG_EBX);
1527 IA32_Pop_Reg(&m_HookFunc, REG_EBP);
1529 if (SH_COMP == SH_COMP_MSVC && !(m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1531 // msvc without varargs:
1532 // callee cleans the stack
1534 short cleansize = GetParamsTotalStackSize();
1535 // Memory return: address is first param
1536 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1537 cleansize += SIZE_PTR;
1539 IA32_Return_Popstack(&m_HookFunc, cleansize);
1543 // gcc or msvc with varargs: caller cleans the stack
1544 // exception: gcc removes the memret addr on memret:
1545 if (SH_COMP == SH_COMP_GCC && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem))
1546 IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
1548 IA32_Return(&m_HookFunc);
1552 // Store pointer for later use
1553 // m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
1554 // into the hookman's vtable
1555 *m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
1559 return m_HookFunc.GetData();
1562 // Pre-condition: GenerateHookFunc() has been called!
1563 void * GenContext::GeneratePubFunc()
1565 jitoffs_t counter, tmppos;
1567 // The pubfunc is a static cdecl function.
1569 // int HookManPubFunc(
1570 // bool store, ebp + 8
1571 // IHookManagerInfo *hi ebp + 12
1577 // hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
1581 IA32_Push_Reg(&m_PubFunc, REG_EBP);
1582 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
1585 // save store in eax, hi in ecx
1586 IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
1587 IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
1589 // Check for store == 0
1590 IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1591 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1592 m_PubFunc.start_count(counter);
1594 // nonzero -> store hi
1595 IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1596 IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1599 m_PubFunc.end_count(counter);
1600 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1602 // check for hi == 0
1603 IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
1604 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1605 m_PubFunc.start_count(counter);
1607 // nonzero -> call vfunc
1608 // push params in reverse order
1609 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
1610 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
1611 IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
1612 IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
1613 IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
1615 // hi == this is in ecx
1616 // on gcc/mingw, ecx is the first parameter
1617 #if SH_COMP == SH_COMP_GCC
1618 IA32_Push_Reg(&m_PubFunc, REG_ECX);
1621 // call the function. vtbloffs = 0, vtblidx = 0
1622 // get vtptr into edx
1623 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1624 // get funcptr into eax
1625 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1627 IA32_Call_Reg(&m_PubFunc, REG_EAX);
1629 // on gcc/mingw, we have to clean up after the call
1630 #if SH_COMP == SH_COMP_GCC
1631 // 5 params + hidden thisptr param
1632 IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
1636 m_PubFunc.end_count(counter);
1637 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1640 IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1643 IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
1644 IA32_Pop_Reg(&m_PubFunc, REG_EBP);
1645 IA32_Return(&m_PubFunc);
1652 bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
1654 // :TODO: Error returns
1655 if (pi.type != PassInfo::PassType_Basic &&
1656 pi.type != PassInfo::PassType_Float &&
1657 pi.type != PassInfo::PassType_Object)
1662 if (pi.type == PassInfo::PassType_Object &&
1663 (pi.flags & PassInfo::PassFlag_ByVal))
1665 if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pCopyCtor)
1670 if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
1675 if ((pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
1680 if ((pi.flags & PassInfo::PassFlag_OCtor) && !pi.pNormalCtor)
1686 if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
1688 return false; // Neither byval nor byref!
1693 void GenContext::AutoDetectRetType()
1695 IntPassInfo &pi = m_Proto.GetRet();
1697 // Only relevant for byval types
1698 if (pi.flags & PassInfo::PassFlag_ByVal)
1701 if (pi.type == PassInfo::PassType_Basic ||
1702 pi.type == PassInfo::PassType_Float)
1705 // _always_ in registers, no matter what the user says
1708 pi.flags &= ~PassInfo::PassFlag_RetMem;
1709 pi.flags |= PassInfo::PassFlag_RetReg;
1713 // Does this even exist? No idea, if it does: in memory!
1714 pi.flags &= ~PassInfo::PassFlag_RetReg;
1715 pi.flags |= PassInfo::PassFlag_RetMem;
1719 else if (pi.type == PassInfo::PassType_Object)
1721 // If the user says nothing, auto-detect
1722 if ((pi.flags & (PassInfo::PassFlag_RetMem | PassInfo::PassFlag_RetReg)) == 0)
1724 #if SH_COMP == SH_COMP_MSVC
1725 // MSVC seems to return _all_ structs, classes, unions in memory
1726 pi.flags |= PassInfo::PassFlag_RetMem;
1727 #elif SH_COMP == SH_COMP_GCC
1728 // Same goes for GCC :)
1729 pi.flags |= PassInfo::PassFlag_RetMem;
1736 // byref: make sure that the flag is _not_ set
1737 pi.flags &= ~PassInfo::PassFlag_RetMem;
1738 pi.flags |= PassInfo::PassFlag_RetReg;
1742 void GenContext::AutoDetectParamFlags()
1744 #if SH_COMP == SH_COMP_GCC
1745 // On GCC, all objects are passed by reference if they have a destructor
1746 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1748 IntPassInfo &pi = m_Proto.GetParam(i);
1749 if (pi.type == PassInfo::PassType_Object &&
1750 (pi.flags & PassInfo::PassFlag_ODtor))
1752 pi.flags |= PassFlag_ForcedByRef;
1758 HookManagerPubFunc GenContext::Generate()
1762 // Check conditions:
1763 // -1) good proto version
1764 // 0) we don't support unknown passtypes, convention, ...
1765 // 1) we don't support functions which return objects by value or take parameters by value
1766 // that have a constructor, a destructor or an overloaded assignment op
1767 // (we wouldn't know how to call it!)
1769 if (m_Proto.GetVersion() < 1)
1774 AutoDetectRetType();
1775 AutoDetectParamFlags();
1777 // Basically, we only support ThisCall/thiscall with varargs
1778 if ((m_Proto.GetConvention() & (~ProtoInfo::CallConv_HasVafmt)) != ProtoInfo::CallConv_ThisCall)
1784 if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
1789 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1791 if (!PassInfoSupported(m_Proto.GetParam(i), false))
1797 return fastdelegate::detail::horrible_cast<HookManagerPubFunc>(GeneratePubFunc());
1800 HookManagerPubFunc GenContext::GetPubFunc()
1802 if (m_GeneratedPubFunc == 0)
1803 m_GeneratedPubFunc = Generate();
1805 return m_GeneratedPubFunc;
1808 bool GenContext::Equal(const CProto &proto, int vtbl_offs, int vtbl_idx)
1810 return (m_OrigProto.ExactlyEqual(proto) && m_VtblOffs == vtbl_offs && m_VtblIdx == vtbl_idx);
1813 bool GenContext::Equal(HookManagerPubFunc other)
1815 return m_GeneratedPubFunc == other;
1818 // *********************************** class GenContextContainer
1819 CHookManagerAutoGen::CHookManagerAutoGen(ISourceHook *pSHPtr) : m_pSHPtr(pSHPtr)
1823 CHookManagerAutoGen::~CHookManagerAutoGen()
1825 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
1827 delete iter->m_GenContext;
1831 int CHookManagerAutoGen::GetIfaceVersion()
1833 return SH_HOOKMANAUTOGEN_IFACE_VERSION;
1836 int CHookManagerAutoGen::GetImplVersion()
1838 return SH_HOOKMANAUTOGEN_IMPL_VERSION;
1841 HookManagerPubFunc CHookManagerAutoGen::MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx)
1843 CProto mproto(proto);
1844 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
1846 if (iter->m_GenContext->Equal(mproto, vtbl_offs, vtbl_idx))
1849 return iter->m_GenContext->GetPubFunc();
1853 // Not found yet -> new one
1856 sctx.m_GenContext = new GenContext(proto, vtbl_offs, vtbl_idx, m_pSHPtr);
1858 if (sctx.m_GenContext->GetPubFunc() == NULL)
1864 m_Contexts.push_back(sctx);
1865 return sctx.m_GenContext->GetPubFunc();
1869 void CHookManagerAutoGen::ReleaseHookMan(HookManagerPubFunc pubFunc)
1871 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
1873 if (iter->m_GenContext->Equal(pubFunc))
1876 if (iter->m_RefCnt == 0)
1878 delete iter->m_GenContext;
1879 m_Contexts.erase(iter);