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 jit_int32_t GenContext::AlignStackBeforeCall(int paramsize, int flags)
209 GCC_ONLY( ((flags & AlignStack_GCC_ThisOnStack)!=0 ? SIZE_PTR : 0) + )
210 MSVC_ONLY( ((flags & AlignStack_MSVC_ThisOnStack)!=0 ? SIZE_PTR : 0) + )
211 ((flags & AlignStack_MemRet)!=0 ? SIZE_PTR : 0);
213 // At the beginning of the hookfunc, the stack is aligned to a 16 bytes boundary.
214 // Then, m_BytesPushedAfterInitialAlignment were pushed (can also be 0).
216 // After this function is called, paramsize bytes will be pushed onto the stack
217 // After that, the alignment has to be a 16 bytes boundary again.
220 // How many bytes we would subtract if the alignment was alright now:
221 int subtractFromEsp = 16 - (paramsize % 16);
222 if (subtractFromEsp == 16)
225 // But: there might be bytes pushed alreay!
226 subtractFromEsp -= m_BytesPushedAfterInitialAlignment;
228 // For example: paramsize was 0 and m_BytesPushedAfterInitialAlignment was 4.
229 // we then have to push another 12 bytes to reach 16 bytes alignment again.
231 if (subtractFromEsp < 0)
232 subtractFromEsp = 16 - ((-subtractFromEsp) % 16);
234 IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, subtractFromEsp, MOD_REG);
236 return subtractFromEsp;
239 void GenContext::AlignStackAfterCall(jit_int32_t numofbytes)
241 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, numofbytes, MOD_REG);
244 void GenContext::CheckAlignmentBeforeCall()
247 IA32_Test_Rm_Imm32(&m_HookFunc, REG_ESP, 15, MOD_REG);
248 IA32_Jump_Cond_Imm8(&m_HookFunc, CC_Z, 1);
249 IA32_Int3(&m_HookFunc);
253 short GenContext::GetParamsTotalStackSize()
256 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
258 acc += GetParamStackSize(m_Proto.GetParam(i));
264 short GenContext::GetForcedByRefParamOffset(int p)
267 for (int i = 0; i < p; ++i)
269 if (m_Proto.GetParam(i).flags & PassFlag_ForcedByRef)
270 off += AlignSize(static_cast<jit_int32_t>(m_Proto.GetParam(i).size), 4);
275 short GenContext::GetForcedByRefParamsSize()
277 return GetForcedByRefParamOffset(m_Proto.GetNumOfParams());
280 jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
282 // push [ebp+<offset>]
283 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_offset);
288 jit_int32_t GenContext::PushBasic(jit_int32_t param_offset, const IntPassInfo &pi)
296 SH_ASSERT(0, ("Unsupported!"));
299 reg = NextRegEBX_ECX_EDX();
300 //movzx reg, BYTE PTR [ebp+<offset>]
302 IA32_Movzx_Reg32_Rm8_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
303 IA32_Push_Reg(&m_HookFunc, reg);
307 reg = NextRegEBX_ECX_EDX();
308 //movzx reg, WORD PTR [ebp+<offset>]
310 m_HookFunc.write_ubyte(IA32_16BIT_PREFIX);
311 IA32_Movzx_Reg32_Rm16_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
312 IA32_Push_Reg(&m_HookFunc, reg);
316 reg = NextRegEBX_ECX_EDX();
317 //mov reg, DWORD PTR [ebp+<offset>]
319 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
320 IA32_Push_Reg(&m_HookFunc, reg);
324 reg = NextRegEBX_ECX_EDX();
325 reg2 = NextRegEBX_ECX_EDX();
326 //mov reg, DWORD PTR [ebp+<offset>+4]
327 //mov reg2, DWORD PTR [ebp+<offset>]
331 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset+4);
332 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg2, REG_EBP, param_offset);
333 IA32_Push_Reg(&m_HookFunc, reg);
334 IA32_Push_Reg(&m_HookFunc, reg2);
340 jit_int32_t GenContext::PushFloat(jit_int32_t param_offset, const IntPassInfo &pi)
345 SH_ASSERT(0, ("Unsupported!"));
348 //fld DWORD PTR [ebp+<offset>]
350 //fstp DWORD PTR [esp]
351 IA32_Fld_Mem32_DispAuto(&m_HookFunc, REG_EBP, param_offset);
352 IA32_Push_Reg(&m_HookFunc, NextRegEBX_ECX_EDX());
353 IA32_Fstp_Mem32_ESP(&m_HookFunc);
356 //fld QWORD PTR [ebp+<offset>]
358 //fstp QWORD PTR [esp]
359 IA32_Fld_Mem64_DispAuto(&m_HookFunc, REG_EBP, param_offset);
360 IA32_Sub_Rm_Imm8(&m_HookFunc, REG_ESP, 8, MOD_REG);
361 IA32_Fstp_Mem64_ESP(&m_HookFunc);
366 jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t place_fbrr)
368 if ((pi.flags & PassFlag_ForcedByRef) == 0)
370 // make room on the stack
372 IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetParamStackSize(pi), MOD_REG);
375 // if there is a copy constructor..
378 // :TODO: alignment here?
379 // can't use normal alignment methods
380 // because an unknown number of bytes has been pushed already (the other params)
381 IA32_Push_Reg(&m_HookFunc, REG_EAX);
383 // compute dest addr to ECX
384 // = forcedbyref ? ebp+place_fbrr : esp+12
385 if (pi.flags & PassFlag_ForcedByRef)
386 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
388 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
390 // compute src addr to EAX
391 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
393 // push params (gcc: also this)
394 IA32_Push_Reg(&m_HookFunc, REG_EAX);
395 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
398 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(pi.pCopyCtor));
399 IA32_Call_Reg(&m_HookFunc, REG_EDX);
402 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
411 // lea edi, [ebp_place_fbrr]
413 // lea edi, [esp+8] - bc_setup pushed two regs onto the stack!
414 //lea esi, [ebp+<offs>]
415 if (pi.flags & PassFlag_ForcedByRef)
416 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, place_fbrr);
418 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
420 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
422 BitwiseCopy_Do(pi.size);
425 // forcedref: push reference to ebp+place_fbrr
426 if (pi.flags & PassFlag_ForcedByRef)
428 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
429 IA32_Push_Reg(&m_HookFunc, REG_ECX);
433 return GetParamStackSize(pi);
436 void GenContext::DestroyParams(jit_int32_t fbrr_base)
438 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
440 const IntPassInfo &pi = m_Proto.GetParam(i);
441 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
442 (pi.flags & PassInfo::PassFlag_ByVal) && (pi.flags & PassFlag_ForcedByRef))
444 // Actually, this is only for GCC (see line above: ForcedByRef)
445 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
446 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, fbrr_base + GetForcedByRefParamOffset(i));
447 IA32_Push_Reg(&m_HookFunc, REG_ECX);
448 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
449 CheckAlignmentBeforeCall();
450 IA32_Call_Reg(&m_HookFunc, REG_EAX);
451 IA32_Pop_Reg(&m_HookFunc, REG_ECX);
453 AlignStackAfterCall(tmpAlign);
458 // May not touch eax!
459 jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret,
460 jit_int32_t v_place_fbrr_base)
462 jit_int32_t added_to_stack = 0;
465 // compute the offset _after_ the last parameter
466 jit_int32_t cur_offset = param_base_offset;
467 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
469 cur_offset += GetParamStackSize(m_Proto.GetParam(i));
472 // push parameters in reverse order
473 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
475 const IntPassInfo &pi = m_Proto.GetParam(i);
476 cur_offset -= GetParamStackSize(pi);
477 if (pi.flags & PassInfo::PassFlag_ByVal)
481 case PassInfo::PassType_Basic:
482 ret = PushBasic(cur_offset, pi);
484 case PassInfo::PassType_Float:
485 ret = PushFloat(cur_offset, pi);
487 case PassInfo::PassType_Object:
488 ret = PushObject(cur_offset, pi, v_place_fbrr_base + GetForcedByRefParamOffset(i));
492 else if (pi.flags & PassInfo::PassFlag_ByRef)
494 ret = PushRef(cur_offset, pi);
498 SH_ASSERT(0, ("Unsupported!"));
500 added_to_stack += ret;
503 return added_to_stack;
506 // It is IMPORTANT that PushMemRetPtr doesn't touch ecx and eax
507 jit_int32_t GenContext::PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret)
509 // Memory return support
510 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
512 // push address where to save it!
514 IA32_Lea_DispRegImmAuto(&m_HookFunc, reg, REG_EBP,
515 MemRetWithTempObj() ? v_place_for_memret : save_ret_to);
516 IA32_Push_Reg(&m_HookFunc, reg);
518 return (SH_COMP==SH_COMP_MSVC) ? 4 : 0; // varargs funcs on msvc might need this.
519 // gcc doesn't: callee cleans the memret ptr, caller the other params :s
524 void GenContext::SaveRetVal(int v_where, int v_place_for_memret)
526 size_t size = GetRealSize(m_Proto.GetRet());
529 // No return value -> nothing
533 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
535 // mov [ebp + v_plugin_ret], eax
536 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
543 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
545 if (MemRetWithTempObj())
547 // *v_where = *v_place_for_memret
549 // if we have an assign operator, use that
550 if (m_Proto.GetRet().pAssignOperator)
552 // lea edx, [ebp + v_place_for_memret] <-- src addr
553 // lea ecx, [ebp + v_where] <-- dest addr
559 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
561 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_place_for_memret);
562 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_where);
563 IA32_Push_Reg(&m_HookFunc, REG_EDX);
564 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
566 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
567 CheckAlignmentBeforeCall();
568 IA32_Call_Reg(&m_HookFunc, REG_EAX);
569 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
571 AlignStackAfterCall(tmpAlign);
578 //lea edi, [evp+v_where] <-- destination
579 //lea esi, [ebp+v_place_for_memret] <-- src
580 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_where);
581 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_place_for_memret);
583 BitwiseCopy_Do(m_Proto.GetRet().size);
586 // Then: destruct *v_place_for_memret if required
587 if (m_Proto.GetRet().pDtor)
589 //lea ecx, [ebp+v_place_for_memret]
594 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
596 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_place_for_memret);
597 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
598 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
599 CheckAlignmentBeforeCall();
600 IA32_Call_Reg(&m_HookFunc, REG_EAX);
601 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
603 AlignStackAfterCall(tmpAlign);
608 // Already copied to correct address -> we're done
613 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
616 IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
618 IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
620 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
624 // size <= 4: return in EAX
625 // We align <4 sizes up to 4
628 // mov [ebp + v_plugin_ret], eax
629 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
633 // size <= 4: return in EAX:EDX
634 // We align 4<x<8 sizes up to 8
636 // mov [ebp + v_plugin_ret], eax
637 // mov [ebp + v_plugin_ret + 4], edx
638 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
639 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
642 else if (m_Proto.GetRet().type == PassInfo::PassType_Object)
644 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)
648 // size <= 4: return in EAX
649 // We align <4 sizes up to 4
651 // mov [ebp + v_plugin_ret], eax
652 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
656 // size <= 4: return in EAX:EDX
657 // We align 4<x<8 sizes up to 8
659 // mov [ebp + v_plugin_ret], eax
660 // mov [ebp + v_plugin_ret + 4], edx
661 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
662 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
666 SH_ASSERT(0, ("RetReg and size > 8 !"));
672 bool GenContext::MemRetWithTempObj()
674 // Memory return AND (has destructor OR has assign operator)
675 return ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
676 && (m_Proto.GetRet().flags & (PassInfo::PassFlag_ODtor | PassInfo::PassFlag_AssignOp)));
679 void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
681 // only for non-void functions!
682 if (m_Proto.GetRet().size == 0)
685 // if (cur_res >= MRES_OVERRIDE)
686 // *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
689 // cmp eax,MRES_OVERRIDE
691 // pContext->GetOverrideRetPtr() --> overrideretptr in eax
696 jitoffs_t tmppos, counter;
698 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
700 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
701 tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
702 m_HookFunc.start_count(counter);
704 // eax = pContext->GetOverrideRetPtr() no alignment needs
711 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
712 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
714 // vtbloffs=0, vtblidx=1
715 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
716 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
717 IA32_Call_Reg(&m_HookFunc, REG_EAX);
718 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
722 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
724 // mov ecx, [ebp+v_plugin_ret]
726 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
727 IA32_Mov_Rm_Reg(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
731 if (m_Proto.GetRet().pAssignOperator)
733 // lea edx, [ebp + v_plugin_ret]
734 // push edx <-- src addr
735 // msvc: ecx = eax <-- dest addr
736 // gcc: push eax <-- dest addr
740 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
742 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
743 IA32_Push_Reg(&m_HookFunc, REG_EDX);
744 #if SH_COMP == SH_COMP_MSVC
745 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
746 #elif SH_COMP == SH_COMP_GCC
747 IA32_Push_Reg(&m_HookFunc, REG_EAX);
750 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
751 CheckAlignmentBeforeCall();
752 IA32_Call_Reg(&m_HookFunc, REG_EAX);
753 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
755 AlignStackAfterCall(tmpAlign);
762 //mov edi, eax <-- destination
763 //lea esi, [ebp+v_plugin_ret] <-- src
764 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
765 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
767 BitwiseCopy_Do(m_Proto.GetRet().size);
771 m_HookFunc.end_count(counter);
772 m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
775 void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
777 // only for non-void functions!
778 if (m_Proto.GetRet().size == 0)
781 // retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
783 // OverrideRetPtr: vtblidx = 1
784 // OrigRetPtr: vtbldix = 2
785 // vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
788 // eax = pContext->GetOverrideRetPtr()
792 // eax = (status < MRES_OVERRIDE) ? 1 : 0
794 // cmp [ebp + v_status], MRES_OVERRIDE
795 // setl al <-- setcc optimization for ternary operators,
798 // lea eax, [4*eax + 0x4]
807 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
808 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
809 IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
810 IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
811 IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
813 IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
815 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
816 IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
818 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
819 IA32_Call_Reg(&m_HookFunc, REG_EDX);
820 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
822 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
825 void GenContext::DoReturn(int v_retptr, int v_memret_outaddr)
827 size_t size = m_Proto.GetRet().size;
831 // Get real ret pointer into ecx
832 // mov ecx, [ebp + v_ret_ptr]
833 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
835 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
838 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
843 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
846 IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
848 IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
850 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic ||
851 ((m_Proto.GetRet().type == PassInfo::PassType_Object) && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)) )
855 // size <= 4: return in EAX
856 // We align <4 sizes up to 4
859 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
863 // size <= 4: return in EAX:EDX
864 // We align 4<x<8 sizes up to 8
868 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
869 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
873 // size >8: return in memory
878 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
880 // *memret_outaddr = plugin_ret
881 if (m_Proto.GetRet().pCopyCtor)
883 // mov edx, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
884 // push edx <-- src addr
885 // msvc: ecx = [ebp + v_memret_outaddr] <-- dest addr
886 // gcc: push [ebp + v_memret_outaddr] <-- dest addr
890 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
892 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_REG);
893 IA32_Push_Reg(&m_HookFunc, REG_EDX);
895 #if SH_COMP == SH_COMP_MSVC
896 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_memret_outaddr);
897 #elif SH_COMP == SH_COMP_GCC
898 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, v_memret_outaddr);
901 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pCopyCtor));
902 CheckAlignmentBeforeCall();
903 IA32_Call_Reg(&m_HookFunc, REG_EAX);
904 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
906 AlignStackAfterCall(tmpAlign);
913 //mov edi, [ebp+v_memret_outaddr] <-- destination
914 //mov esi, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
915 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDI, REG_EBP, v_memret_outaddr);
916 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESI, REG_ECX, MOD_REG);
918 BitwiseCopy_Do(m_Proto.GetRet().size);
921 // In both cases: return the pointer in EAX
922 // mov eax, [ebp + v_memret_outaddr]
923 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_memret_outaddr);
927 void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
928 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)
930 jitoffs_t counter, tmppos;
931 jitoffs_t counter2, tmppos2;
933 jitoffs_t loop_begin_counter;
935 // prev_res = MRES_IGNORED
936 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
938 m_HookFunc.start_count(loop_begin_counter);
940 // eax = pContext->GetNext()
947 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
948 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
950 // vtbloffs=0, vtblidx=0
951 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
952 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
953 IA32_Call_Reg(&m_HookFunc, REG_EAX);
954 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
959 IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
960 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
961 m_HookFunc.start_count(counter);
963 // prev_res = MRES_IGNORED
964 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res);
971 // eax = [eax+2*SIZE_PTR]
975 jit_int32_t caller_clean_bytes = 0; // gcc always, msvc never (hooks never have varargs!)
977 jit_int32_t alignBytes = AlignStackBeforeCall(
978 GetParamsTotalStackSize() + ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)!=0 ? SIZE_PTR : 0),
979 AlignStack_GCC_ThisOnStack | ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem) == 0 ? 0 : AlignStack_MemRet)
982 // vafmt: push va_buf
983 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
985 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
986 IA32_Push_Reg(&m_HookFunc, REG_ECX);
987 caller_clean_bytes += SIZE_PTR;
990 caller_clean_bytes += PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
992 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
993 if (SH_COMP == SH_COMP_GCC)
994 IA32_Push_Reg(&m_HookFunc, REG_ECX);
995 caller_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
996 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
997 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
998 CheckAlignmentBeforeCall();
999 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1001 AlignStackAfterCall(alignBytes);
1003 DestroyParams(v_place_fbrr_base);
1005 SaveRetVal(v_plugin_ret, v_place_for_memret);
1007 // cleanup (gcc only)
1009 if (SH_COMP == SH_COMP_GCC)
1010 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
1012 // process meta return:
1013 // prev_res = cur_res
1014 // if (cur_res > status) status = cur_res;
1024 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1025 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
1026 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
1028 IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
1029 tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
1030 m_HookFunc.start_count(counter2);
1032 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
1034 m_HookFunc.end_count(counter2);
1035 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
1037 // process retval for non-void functions
1038 ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
1040 // jump back to loop begin
1041 tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
1042 m_HookFunc.end_count(loop_begin_counter);
1043 m_HookFunc.rewrite(tmppos2, static_cast<jit_int32_t>(-loop_begin_counter));
1045 m_HookFunc.end_count(counter);
1046 m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
1049 void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
1050 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)
1052 jitoffs_t counter, tmppos;
1053 jitoffs_t counter2, tmppos2;
1054 jitoffs_t counter3, tmppos3;
1056 // if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
1057 // *v_orig_ret = orig_call()
1059 // *v_orig_ret = *v_override_ret
1062 // cmp eax, MRES_SUPERCEDE
1064 // call pContext->ShouldCallOrig()
1065 // test al, al !! important: al, not eax! bool is only stored in the LSbyte
1069 // SaveRet(v_orig_ret)
1070 // jmp skip_dont_call:
1073 // *v_orig_ret = *v_override_ret
1076 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1077 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
1079 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
1080 m_HookFunc.start_count(counter);
1082 // eax = pContext->ShouldCallOrig()
1086 // eax = [eax + 3*SIZE_PTR]
1089 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
1090 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1092 // vtbloffs=0, vtblidx=3
1093 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1094 IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
1095 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1096 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1098 IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
1099 tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
1100 m_HookFunc.start_count(counter2);
1102 jit_int32_t caller_clean_bytes = 0; // gcc always, msvc when cdecl-like (varargs)
1104 jit_int32_t alignBytes = AlignStackBeforeCall(
1105 GetParamsTotalStackSize() + ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)!=0 ? 2*SIZE_PTR : 0),
1106 AlignStack_GCC_ThisOnStack |
1107 ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem) == 0 ? 0 : AlignStack_MemRet) |
1108 ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs) == 0 ? 0 : AlignStack_MSVC_ThisOnStack)
1111 // vafmt: push va_buf, then "%s"
1112 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1114 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
1115 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1116 IA32_Push_Imm32(&m_HookFunc, DownCastPtr("%s"));
1117 caller_clean_bytes += 2*SIZE_PTR;
1121 caller_clean_bytes += PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
1124 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
1125 if (SH_COMP == SH_COMP_GCC)
1127 // on gcc/mingw, this is the first parameter
1128 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1129 // on msvc without varargs, simply leave it in ecx
1131 // actually, if we're returning in memory, this pointer is the second param
1132 // and the memret pointer is the real first parameter
1133 caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1137 // On msvc, if we're returning in memory, the memret pointer is the first parameter
1138 caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1140 // actually, with varargs, the this pointer is the first param and the memret ptr
1141 // is the second one
1142 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1144 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1149 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
1150 CheckAlignmentBeforeCall();
1151 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1153 AlignStackAfterCall(alignBytes);
1156 if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1157 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
1159 DestroyParams(v_place_fbrr_base);
1162 SaveRetVal(v_orig_ret, v_place_for_memret);
1164 // Skip don't call variant
1165 tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
1166 m_HookFunc.start_count(counter3);
1170 m_HookFunc.end_count(counter);
1171 m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
1173 m_HookFunc.end_count(counter2);
1174 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
1176 // *v_orig_ret = *v_override_ret
1177 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
1179 // mov ecx, [ebp + v_override_ret]
1180 // mov [ebp + v_orig_ret], ecx
1182 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1183 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_ECX, v_orig_ret);
1187 if (m_Proto.GetRet().pAssignOperator)
1189 // lea edx, [ebp + v_override_ret] <-- src addr
1190 // lea ecx, [ebp + v_orig_ret] <-- dest addr
1191 // push edx <-- src addr
1196 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
1198 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
1199 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1200 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1201 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1203 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
1204 CheckAlignmentBeforeCall();
1205 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1206 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1208 AlignStackAfterCall(tmpAlign);
1213 BitwiseCopy_Setup();
1215 //lea edi, [ebp+v_orig_ret] <-- destination
1216 //lea esi, [ebp+v_override_ret] <-- src
1217 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
1218 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
1220 BitwiseCopy_Do(m_Proto.GetRet().size);
1224 // skip don't call label target:
1225 m_HookFunc.end_count(counter3);
1226 m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
1229 // Sets *v_pContext to return value
1230 void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret,
1231 int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
1232 int v_this, int v_pContext)
1234 // call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
1235 // &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
1236 // The last two params are null for void funcs.
1238 if (m_Proto.GetRet().size == 0)
1241 IA32_Push_Imm8(&m_HookFunc, 0); // orig_ret
1242 IA32_Push_Imm8(&m_HookFunc, 0); // override_ret
1246 // orig_ret and override_ret
1247 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
1248 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
1249 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1250 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1253 // cur_res and prev_res
1254 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1255 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
1256 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1257 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1259 // status and vfnptr_origentry
1260 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1261 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
1262 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1263 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1266 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
1267 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1270 // *(this + vtbloffs) + SIZE_PTR*vtblidx
1271 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this); // get this into ecx (gcc!)
1272 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
1273 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
1274 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1277 IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1278 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1279 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1282 #if SH_COMP == SH_COMP_GCC
1283 // on gcc/mingw, this is the first parameter
1284 GCC_ONLY(IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr)));
1285 #elif SH_COMP == SH_COMP_MSVC
1286 // on msvc, it's ecx
1287 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1290 // call the function. vtbloffs = 0, vtblidx = 19
1291 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1292 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1293 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
1295 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1297 // on gcc/mingw, we have to clean up after the call
1298 // 9 params + hidden thisptr param
1299 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG));
1301 // store return value
1302 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
1305 void GenContext::CallEndContext(int v_pContext)
1308 // shptr->EndContext(pContex)
1309 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
1310 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1313 #if SH_COMP == SH_COMP_GCC
1314 // on gcc/mingw, this is the first parameter
1315 IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
1316 #elif SH_COMP == SH_COMP_MSVC
1317 // on msvc, it's ecx
1318 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1321 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1322 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1323 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
1325 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1327 // on gcc/mingw, we have to clean up after the call
1328 // 1 param + hidden thisptr param
1329 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1332 void GenContext::ResetFrame(jit_int32_t startOffset)
1334 m_HookFunc_FrameOffset = startOffset;
1335 m_HookFunc_FrameVarsSize = 0;
1338 jit_int32_t GenContext::AddVarToFrame(jit_int32_t size)
1340 m_HookFunc_FrameOffset -= size;
1341 m_HookFunc_FrameVarsSize += size;
1342 return m_HookFunc_FrameOffset;
1345 jit_int32_t GenContext::ComputeVarsSize()
1347 return m_HookFunc_FrameVarsSize;
1350 void * GenContext::GenerateHookFunc()
1353 IA32_Push_Reg(&m_HookFunc, REG_EBP);
1354 IA32_Push_Reg(&m_HookFunc, REG_EBX);
1355 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
1357 jit_int32_t v_this = 0;
1358 jit_int32_t param_base_offs = 0;
1359 if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1361 // gcc or msvc with varargs:
1362 v_this = 12; // first param
1363 param_base_offs = 16;
1368 // on msvc without varargs, save thisptr
1370 param_base_offs = 12;
1371 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1372 ResetFrame(-4); // start placing local vars on offset -4
1373 // because there already is the thisptr variable
1376 // ********************** stack frame **********************
1377 // MSVC without varargs
1378 // second param (gcc: first real param) ebp + 16
1379 // first param (gcc: thisptr) ebp + 12
1380 // ret address: ebp + 8
1381 // caller's ebp ebp + 4
1383 // MSVC ONLY: current this ebp - 4
1384 // void *vfnptr_origentry ebp - 4 -4
1385 // META_RES status = MRES_IGNORED ebp - 8 -4
1386 // META_RES prev_res ebp - 12 -4
1387 // META_RES cur_res ebp - 16 -4
1388 // IMyDelegate *iter ebp - 20 -4
1389 // IHookContext *pContext ebp - 24 -4
1390 // == 3 ptrs + 3 enums = 24 bytes
1393 // my_rettype *ret_ptr ebp - 28 -4
1394 // my_rettype orig_ret ebp - 28 - sizeof(my_rettype) -4
1395 // my_rettype override_ret ebp - 28 - sizeof(my_rettype)*2 -4
1396 // my_rettype plugin_ret ebp - 28 - sizeof(my_rettype)*3 -4
1397 // == + 3 * sizeof(my_rettype) bytes
1400 // my_rettype place_for_memret ebp - 28 - sizeof(my_rettype)*4 -4
1402 // gcc only: if required:
1403 // place forced byref params ebp - 28 - sizeof(my_rettype)*{4 or 5}
1407 // char va_buf[something];
1410 const jit_int8_t v_vfnptr_origentry = AddVarToFrame(SIZE_PTR);
1411 const jit_int8_t v_status = AddVarToFrame(sizeof(META_RES));
1412 const jit_int8_t v_prev_res = AddVarToFrame(sizeof(META_RES));
1413 const jit_int8_t v_cur_res = AddVarToFrame(sizeof(META_RES));
1414 const jit_int8_t v_iter = AddVarToFrame(SIZE_PTR);
1415 const jit_int8_t v_pContext = AddVarToFrame(SIZE_PTR);
1417 // Memory return: first param is the address
1418 jit_int32_t v_memret_addr = 0;
1419 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1421 if (SH_COMP == SH_COMP_GCC)
1423 // gcc: now: first param = mem ret addr
1424 // second param = this pointer
1425 // third param = actual first param
1428 param_base_offs += SIZE_PTR;
1432 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1436 // first param = this pointer
1437 // second param = mem ret addr
1438 // third param = actual first param
1440 // params_base_offs is already updated to point to after the this pointer
1441 v_memret_addr = param_base_offs;
1442 param_base_offs += SIZE_PTR;
1446 v_memret_addr = param_base_offs;
1447 param_base_offs += SIZE_PTR;
1452 jit_int32_t v_ret_ptr = 0;
1453 jit_int32_t v_orig_ret = 0;
1454 jit_int32_t v_override_ret = 0;
1455 jit_int32_t v_plugin_ret = 0;
1457 if (m_Proto.GetRet().size != 0)
1459 v_ret_ptr = AddVarToFrame(SIZE_PTR);
1460 v_orig_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1461 v_override_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1462 v_plugin_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1465 jit_int32_t v_place_for_memret = 0;
1466 if (MemRetWithTempObj())
1468 v_place_for_memret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1471 jit_int32_t v_place_fbrr_base = 0;
1472 if (SH_COMP == SH_COMP_GCC && GetForcedByRefParamsSize())
1474 v_place_fbrr_base = AddVarToFrame(GetForcedByRefParamsSize());
1477 // Only exists for varargs functions
1478 jit_int32_t v_va_argptr = 0;
1479 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1481 v_va_argptr = AddVarToFrame(SIZE_PTR);
1484 jit_int32_t v_va_buf = 0;
1485 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1487 v_va_buf = AddVarToFrame(SourceHook::STRBUF_LEN);
1490 IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, ComputeVarsSize(), MOD_REG);
1492 // Initial stack alignment
1493 IA32_And_Rm_Imm32(&m_HookFunc, REG_ESP, MOD_REG, -16);
1494 m_BytesPushedAfterInitialAlignment = 0;
1496 // init status localvar
1497 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
1499 // VarArgs: init argptr & format
1500 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1502 // argptr = first vararg param
1503 // lea eax, [ebp + param_base_offs + paramssize]
1506 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_base_offs + GetParamsTotalStackSize() + SIZE_PTR); // +SIZE_PTR: last const char * is not in protoinfo
1507 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_va_argptr);
1509 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1513 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR*3 + sizeof(size_t), AlignStack_GCC_ThisOnStack);
1515 // push valist, fmt param, maxsize, buffer
1516 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1517 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_base_offs + GetParamsTotalStackSize()); // last given param (+4-4, see above)
1518 IA32_Push_Imm32(&m_HookFunc, SourceHook::STRBUF_LEN - 1);
1519 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
1520 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1523 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(&vsnprintf));
1524 CheckAlignmentBeforeCall();
1525 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1528 IA32_Add_Rm_Imm32(&m_HookFunc, REG_ESP, 0x10, MOD_REG);
1530 AlignStackAfterCall(tmpAlign);
1532 // Set trailing zero
1533 IA32_Xor_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_REG);
1534 IA32_Mov_Rm8_Reg8_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_va_buf + SourceHook::STRBUF_LEN - 1);
1537 // Call constructors for ret vars if required
1538 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1539 m_Proto.GetRet().pNormalCtor)
1541 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
1544 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1545 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1546 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1547 CheckAlignmentBeforeCall();
1548 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1549 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1552 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1553 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1554 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1555 CheckAlignmentBeforeCall();
1556 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1557 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1560 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1561 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1562 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1563 CheckAlignmentBeforeCall();
1564 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1565 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1567 AlignStackAfterCall(tmpAlign);
1569 // _don't_ call a constructor for v_place_for_memret !
1572 // ********************** SetupHookLoop **********************
1573 CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
1574 v_this, v_pContext);
1576 // ********************** call pre hooks **********************
1577 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1578 v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1580 // ********************** call orig func **********************
1581 GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
1582 v_override_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1584 // ********************** call post hooks **********************
1585 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1586 v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1588 // ********************** end context and return **********************
1590 PrepareReturn(v_status, v_pContext, v_ret_ptr);
1592 CallEndContext(v_pContext);
1594 // Call destructors of byval object params which have a destructor
1596 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
1598 jit_int32_t cur_param_pos = param_base_offs;
1599 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1601 const IntPassInfo &pi = m_Proto.GetParam(i);
1602 // GCC: NOT of forced byref params. the caller destructs those.
1603 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
1604 (pi.flags & PassInfo::PassFlag_ByVal) && !(pi.flags & PassFlag_ForcedByRef))
1606 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
1607 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
1608 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1609 CheckAlignmentBeforeCall();
1610 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1611 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1613 cur_param_pos += GetParamStackSize(pi);
1616 AlignStackAfterCall(tmpAlign);
1618 DoReturn(v_ret_ptr, v_memret_addr);
1620 // Call destructors of orig_ret/ ...
1621 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1622 m_Proto.GetRet().pDtor)
1624 // Preserve return value in EAX(:EDX)
1625 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1626 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1628 m_BytesPushedAfterInitialAlignment += 8;
1630 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
1632 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1633 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1634 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1635 CheckAlignmentBeforeCall();
1636 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1637 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1639 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1640 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1641 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1642 CheckAlignmentBeforeCall();
1643 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1644 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1646 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1647 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1648 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1649 CheckAlignmentBeforeCall();
1650 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1651 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1653 AlignStackAfterCall(tmpAlign);
1655 IA32_Pop_Reg(&m_HookFunc, REG_EDX);
1656 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
1658 m_BytesPushedAfterInitialAlignment -= 8;
1662 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
1663 IA32_Pop_Reg(&m_HookFunc, REG_EBX);
1664 IA32_Pop_Reg(&m_HookFunc, REG_EBP);
1666 if (SH_COMP == SH_COMP_MSVC && !(m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1668 // msvc without varargs:
1669 // callee cleans the stack
1671 short cleansize = GetParamsTotalStackSize();
1672 // Memory return: address is first param
1673 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1674 cleansize += SIZE_PTR;
1676 IA32_Return_Popstack(&m_HookFunc, cleansize);
1680 // gcc or msvc with varargs: caller cleans the stack
1681 // exception: gcc removes the memret addr on memret:
1682 if (SH_COMP == SH_COMP_GCC && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem))
1683 IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
1685 IA32_Return(&m_HookFunc);
1689 // Store pointer for later use
1690 // m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
1691 // into the hookman's vtable
1692 *m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
1696 return m_HookFunc.GetData();
1699 // Pre-condition: GenerateHookFunc() has been called!
1700 void * GenContext::GeneratePubFunc()
1702 jitoffs_t counter, tmppos;
1704 // The pubfunc is a static cdecl function.
1706 // int HookManPubFunc(
1707 // bool store, ebp + 8
1708 // IHookManagerInfo *hi ebp + 12
1714 // hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
1718 IA32_Push_Reg(&m_PubFunc, REG_EBP);
1719 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
1722 // save store in eax, hi in ecx
1723 IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
1724 IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
1726 // Check for store == 0
1727 IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1728 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1729 m_PubFunc.start_count(counter);
1731 // nonzero -> store hi
1732 IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1733 IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1736 m_PubFunc.end_count(counter);
1737 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1739 // check for hi == 0
1740 IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
1741 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1742 m_PubFunc.start_count(counter);
1744 // nonzero -> call vfunc
1745 // push params in reverse order
1746 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
1747 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
1748 IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
1749 IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
1750 IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
1752 // hi == this is in ecx
1753 // on gcc/mingw, ecx is the first parameter
1754 #if SH_COMP == SH_COMP_GCC
1755 IA32_Push_Reg(&m_PubFunc, REG_ECX);
1758 // call the function. vtbloffs = 0, vtblidx = 0
1759 // get vtptr into edx
1760 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1761 // get funcptr into eax
1762 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1764 IA32_Call_Reg(&m_PubFunc, REG_EAX);
1766 // on gcc/mingw, we have to clean up after the call
1767 #if SH_COMP == SH_COMP_GCC
1768 // 5 params + hidden thisptr param
1769 IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
1773 m_PubFunc.end_count(counter);
1774 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1777 IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1780 IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
1781 IA32_Pop_Reg(&m_PubFunc, REG_EBP);
1782 IA32_Return(&m_PubFunc);
1789 bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
1791 // :TODO: Error returns
1792 if (pi.type != PassInfo::PassType_Basic &&
1793 pi.type != PassInfo::PassType_Float &&
1794 pi.type != PassInfo::PassType_Object)
1799 if (pi.type == PassInfo::PassType_Object &&
1800 (pi.flags & PassInfo::PassFlag_ByVal))
1802 if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pCopyCtor)
1807 if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
1812 if ((pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
1817 if ((pi.flags & PassInfo::PassFlag_OCtor) && !pi.pNormalCtor)
1823 if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
1825 return false; // Neither byval nor byref!
1830 void GenContext::AutoDetectRetType()
1832 IntPassInfo &pi = m_Proto.GetRet();
1834 // Only relevant for byval types
1835 if (pi.flags & PassInfo::PassFlag_ByVal)
1838 if (pi.type == PassInfo::PassType_Basic ||
1839 pi.type == PassInfo::PassType_Float)
1842 // _always_ in registers, no matter what the user says
1845 pi.flags &= ~PassInfo::PassFlag_RetMem;
1846 pi.flags |= PassInfo::PassFlag_RetReg;
1850 // Does this even exist? No idea, if it does: in memory!
1851 pi.flags &= ~PassInfo::PassFlag_RetReg;
1852 pi.flags |= PassInfo::PassFlag_RetMem;
1856 else if (pi.type == PassInfo::PassType_Object)
1858 // If the user says nothing, auto-detect
1859 if ((pi.flags & (PassInfo::PassFlag_RetMem | PassInfo::PassFlag_RetReg)) == 0)
1861 #if SH_COMP == SH_COMP_MSVC
1862 // MSVC seems to return _all_ structs, classes, unions in memory
1863 pi.flags |= PassInfo::PassFlag_RetMem;
1864 #elif SH_COMP == SH_COMP_GCC
1865 // Same goes for GCC :)
1866 pi.flags |= PassInfo::PassFlag_RetMem;
1873 // byref: make sure that the flag is _not_ set
1874 pi.flags &= ~PassInfo::PassFlag_RetMem;
1875 pi.flags |= PassInfo::PassFlag_RetReg;
1879 void GenContext::AutoDetectParamFlags()
1881 #if SH_COMP == SH_COMP_GCC
1882 // On GCC, all objects are passed by reference if they have a destructor
1883 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1885 IntPassInfo &pi = m_Proto.GetParam(i);
1886 if (pi.type == PassInfo::PassType_Object &&
1887 (pi.flags & PassInfo::PassFlag_ODtor))
1889 pi.flags |= PassFlag_ForcedByRef;
1895 HookManagerPubFunc GenContext::Generate()
1899 // Check conditions:
1900 // -1) good proto version
1901 // 0) we don't support unknown passtypes, convention, ...
1902 // 1) we don't support functions which return objects by value or take parameters by value
1903 // that have a constructor, a destructor or an overloaded assignment op
1904 // (we wouldn't know how to call it!)
1906 if (m_Proto.GetVersion() < 1)
1911 AutoDetectRetType();
1912 AutoDetectParamFlags();
1914 // Basically, we only support ThisCall/thiscall with varargs
1915 if ((m_Proto.GetConvention() & (~ProtoInfo::CallConv_HasVafmt)) != ProtoInfo::CallConv_ThisCall)
1921 if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
1926 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1928 if (!PassInfoSupported(m_Proto.GetParam(i), false))
1934 return fastdelegate::detail::horrible_cast<HookManagerPubFunc>(GeneratePubFunc());
1937 HookManagerPubFunc GenContext::GetPubFunc()
1939 if (m_GeneratedPubFunc == 0)
1940 m_GeneratedPubFunc = Generate();
1942 return m_GeneratedPubFunc;
1945 bool GenContext::Equal(const CProto &proto, int vtbl_offs, int vtbl_idx)
1947 return (m_OrigProto.ExactlyEqual(proto) && m_VtblOffs == vtbl_offs && m_VtblIdx == vtbl_idx);
1950 bool GenContext::Equal(HookManagerPubFunc other)
1952 return m_GeneratedPubFunc == other;
1955 // *********************************** class GenContextContainer
1956 CHookManagerAutoGen::CHookManagerAutoGen(ISourceHook *pSHPtr) : m_pSHPtr(pSHPtr)
1960 CHookManagerAutoGen::~CHookManagerAutoGen()
1962 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
1964 delete iter->m_GenContext;
1968 int CHookManagerAutoGen::GetIfaceVersion()
1970 return SH_HOOKMANAUTOGEN_IFACE_VERSION;
1973 int CHookManagerAutoGen::GetImplVersion()
1975 return SH_HOOKMANAUTOGEN_IMPL_VERSION;
1978 HookManagerPubFunc CHookManagerAutoGen::MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx)
1980 CProto mproto(proto);
1981 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
1983 if (iter->m_GenContext->Equal(mproto, vtbl_offs, vtbl_idx))
1986 return iter->m_GenContext->GetPubFunc();
1990 // Not found yet -> new one
1993 sctx.m_GenContext = new GenContext(proto, vtbl_offs, vtbl_idx, m_pSHPtr);
1995 if (sctx.m_GenContext->GetPubFunc() == NULL)
2001 m_Contexts.push_back(sctx);
2002 return sctx.m_GenContext->GetPubFunc();
2006 void CHookManagerAutoGen::ReleaseHookMan(HookManagerPubFunc pubFunc)
2008 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
2010 if (iter->m_GenContext->Equal(pubFunc))
2013 if (iter->m_RefCnt == 0)
2015 delete iter->m_GenContext;
2016 m_Contexts.erase(iter);