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(16);
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 if (subtractFromEsp != 0)
235 IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, subtractFromEsp, MOD_REG);
237 return subtractFromEsp;
240 void GenContext::AlignStackAfterCall(jit_int32_t numofbytes)
243 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, numofbytes, MOD_REG);
246 void GenContext::CheckAlignmentBeforeCall()
249 IA32_Test_Rm_Imm32(&m_HookFunc, REG_ESP, 15, MOD_REG);
250 IA32_Jump_Cond_Imm8(&m_HookFunc, CC_Z, 1);
251 IA32_Int3(&m_HookFunc);
255 short GenContext::GetParamsTotalStackSize()
258 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
260 acc += GetParamStackSize(m_Proto.GetParam(i));
266 short GenContext::GetForcedByRefParamOffset(int p)
269 for (int i = 0; i < p; ++i)
271 if (m_Proto.GetParam(i).flags & PassFlag_ForcedByRef)
272 off += AlignSize(static_cast<jit_int32_t>(m_Proto.GetParam(i).size), 4);
277 short GenContext::GetForcedByRefParamsSize()
279 return GetForcedByRefParamOffset(m_Proto.GetNumOfParams());
282 jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
284 // push [ebp+<offset>]
285 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_offset);
290 jit_int32_t GenContext::PushBasic(jit_int32_t param_offset, const IntPassInfo &pi)
298 SH_ASSERT(0, ("Unsupported!"));
301 reg = NextRegEBX_ECX_EDX();
302 //movzx reg, BYTE PTR [ebp+<offset>]
304 IA32_Movzx_Reg32_Rm8_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
305 IA32_Push_Reg(&m_HookFunc, reg);
309 reg = NextRegEBX_ECX_EDX();
310 //movzx reg, WORD PTR [ebp+<offset>]
312 m_HookFunc.write_ubyte(IA32_16BIT_PREFIX);
313 IA32_Movzx_Reg32_Rm16_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
314 IA32_Push_Reg(&m_HookFunc, reg);
318 reg = NextRegEBX_ECX_EDX();
319 //mov reg, DWORD PTR [ebp+<offset>]
321 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
322 IA32_Push_Reg(&m_HookFunc, reg);
326 reg = NextRegEBX_ECX_EDX();
327 reg2 = NextRegEBX_ECX_EDX();
328 //mov reg, DWORD PTR [ebp+<offset>+4]
329 //mov reg2, DWORD PTR [ebp+<offset>]
333 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset+4);
334 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg2, REG_EBP, param_offset);
335 IA32_Push_Reg(&m_HookFunc, reg);
336 IA32_Push_Reg(&m_HookFunc, reg2);
342 jit_int32_t GenContext::PushFloat(jit_int32_t param_offset, const IntPassInfo &pi)
347 SH_ASSERT(0, ("Unsupported!"));
350 //fld DWORD PTR [ebp+<offset>]
352 //fstp DWORD PTR [esp]
353 IA32_Fld_Mem32_DispAuto(&m_HookFunc, REG_EBP, param_offset);
354 IA32_Push_Reg(&m_HookFunc, NextRegEBX_ECX_EDX());
355 IA32_Fstp_Mem32_ESP(&m_HookFunc);
358 //fld QWORD PTR [ebp+<offset>]
360 //fstp QWORD PTR [esp]
361 IA32_Fld_Mem64_DispAuto(&m_HookFunc, REG_EBP, param_offset);
362 IA32_Sub_Rm_Imm8(&m_HookFunc, REG_ESP, 8, MOD_REG);
363 IA32_Fstp_Mem64_ESP(&m_HookFunc);
368 jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t place_fbrr)
370 if ((pi.flags & PassFlag_ForcedByRef) == 0)
372 // make room on the stack
374 IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetParamStackSize(pi), MOD_REG);
377 // if there is a copy constructor..
380 // :TODO: alignment here?
381 // can't use normal alignment methods
382 // because an unknown number of bytes has been pushed already (the other params)
385 IA32_Push_Reg(&m_HookFunc, REG_EAX);
387 // compute dest addr to ECX
388 // = forcedbyref ? ebp+place_fbrr : esp+12
389 if (pi.flags & PassFlag_ForcedByRef)
390 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
392 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
394 // compute src addr to EAX
395 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
397 // push params (gcc: also this)
398 IA32_Push_Reg(&m_HookFunc, REG_EAX);
399 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
402 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(pi.pCopyCtor));
403 IA32_Call_Reg(&m_HookFunc, REG_EDX);
405 // gcc: clean up stack
406 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
409 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
418 // lea edi, [ebp_place_fbrr]
420 // lea edi, [esp+8] - bc_setup pushed two regs onto the stack!
421 //lea esi, [ebp+<offs>]
422 if (pi.flags & PassFlag_ForcedByRef)
423 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, place_fbrr);
425 IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
427 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
429 BitwiseCopy_Do(pi.size);
432 // forcedref: push reference to ebp+place_fbrr
433 if (pi.flags & PassFlag_ForcedByRef)
435 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
436 IA32_Push_Reg(&m_HookFunc, REG_ECX);
440 return GetParamStackSize(pi);
443 void GenContext::DestroyParams(jit_int32_t fbrr_base)
445 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
447 const IntPassInfo &pi = m_Proto.GetParam(i);
448 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
449 (pi.flags & PassInfo::PassFlag_ByVal) && (pi.flags & PassFlag_ForcedByRef))
451 // Actually, this is only for GCC (see line above: ForcedByRef)
452 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
453 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, fbrr_base + GetForcedByRefParamOffset(i));
454 IA32_Push_Reg(&m_HookFunc, REG_ECX);
455 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
456 CheckAlignmentBeforeCall();
457 IA32_Call_Reg(&m_HookFunc, REG_EAX);
458 IA32_Pop_Reg(&m_HookFunc, REG_ECX);
460 AlignStackAfterCall(tmpAlign);
465 // May not touch eax!
466 jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret,
467 jit_int32_t v_place_fbrr_base)
469 jit_int32_t added_to_stack = 0;
472 // compute the offset _after_ the last parameter
473 jit_int32_t cur_offset = param_base_offset;
474 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
476 cur_offset += GetParamStackSize(m_Proto.GetParam(i));
479 // push parameters in reverse order
480 for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
482 const IntPassInfo &pi = m_Proto.GetParam(i);
483 cur_offset -= GetParamStackSize(pi);
484 if (pi.flags & PassInfo::PassFlag_ByVal)
488 case PassInfo::PassType_Basic:
489 ret = PushBasic(cur_offset, pi);
491 case PassInfo::PassType_Float:
492 ret = PushFloat(cur_offset, pi);
494 case PassInfo::PassType_Object:
495 ret = PushObject(cur_offset, pi, v_place_fbrr_base + GetForcedByRefParamOffset(i));
499 else if (pi.flags & PassInfo::PassFlag_ByRef)
501 ret = PushRef(cur_offset, pi);
505 SH_ASSERT(0, ("Unsupported!"));
507 added_to_stack += ret;
510 return added_to_stack;
513 // It is IMPORTANT that PushMemRetPtr doesn't touch ecx and eax
514 jit_int32_t GenContext::PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret)
516 // Memory return support
517 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
519 // push address where to save it!
521 IA32_Lea_DispRegImmAuto(&m_HookFunc, reg, REG_EBP,
522 MemRetWithTempObj() ? v_place_for_memret : save_ret_to);
523 IA32_Push_Reg(&m_HookFunc, reg);
525 return (SH_COMP==SH_COMP_MSVC) ? 4 : 0; // varargs funcs on msvc might need this.
526 // gcc doesn't: callee cleans the memret ptr, caller the other params :s
531 void GenContext::SaveRetVal(int v_where, int v_place_for_memret)
533 size_t size = GetRealSize(m_Proto.GetRet());
536 // No return value -> nothing
540 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
542 // mov [ebp + v_plugin_ret], eax
543 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
550 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
552 if (MemRetWithTempObj())
554 // *v_where = *v_place_for_memret
556 // if we have an assign operator, use that
557 if (m_Proto.GetRet().pAssignOperator)
559 // lea edx, [ebp + v_place_for_memret] <-- src addr
560 // lea ecx, [ebp + v_where] <-- dest addr
566 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
568 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_place_for_memret);
569 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_where);
570 IA32_Push_Reg(&m_HookFunc, REG_EDX);
571 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
573 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
574 CheckAlignmentBeforeCall();
575 IA32_Call_Reg(&m_HookFunc, REG_EAX);
576 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
578 AlignStackAfterCall(tmpAlign);
585 //lea edi, [evp+v_where] <-- destination
586 //lea esi, [ebp+v_place_for_memret] <-- src
587 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_where);
588 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_place_for_memret);
590 BitwiseCopy_Do(m_Proto.GetRet().size);
593 // Then: destruct *v_place_for_memret if required
594 if (m_Proto.GetRet().pDtor)
596 //lea ecx, [ebp+v_place_for_memret]
601 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
603 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_place_for_memret);
604 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
605 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
606 CheckAlignmentBeforeCall();
607 IA32_Call_Reg(&m_HookFunc, REG_EAX);
608 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
610 AlignStackAfterCall(tmpAlign);
615 // Already copied to correct address -> we're done
620 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
623 IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
625 IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
627 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
631 // size <= 4: return in EAX
632 // We align <4 sizes up to 4
635 // mov [ebp + v_plugin_ret], eax
636 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
640 // size <= 4: return in EAX:EDX
641 // We align 4<x<8 sizes up to 8
643 // mov [ebp + v_plugin_ret], eax
644 // mov [ebp + v_plugin_ret + 4], edx
645 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
646 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
649 else if (m_Proto.GetRet().type == PassInfo::PassType_Object)
651 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)
655 // size <= 4: return in EAX
656 // We align <4 sizes up to 4
658 // mov [ebp + v_plugin_ret], eax
659 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
663 // size <= 4: return in EAX:EDX
664 // We align 4<x<8 sizes up to 8
666 // mov [ebp + v_plugin_ret], eax
667 // mov [ebp + v_plugin_ret + 4], edx
668 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
669 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
673 SH_ASSERT(0, ("RetReg and size > 8 !"));
679 bool GenContext::MemRetWithTempObj()
681 // Memory return AND (has destructor OR has assign operator)
682 return ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
683 && (m_Proto.GetRet().flags & (PassInfo::PassFlag_ODtor | PassInfo::PassFlag_AssignOp)));
686 void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
688 // only for non-void functions!
689 if (m_Proto.GetRet().size == 0)
692 // if (cur_res >= MRES_OVERRIDE)
693 // *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
696 // cmp eax,MRES_OVERRIDE
698 // pContext->GetOverrideRetPtr() --> overrideretptr in eax
703 jitoffs_t tmppos, counter;
705 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
707 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
708 tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
709 m_HookFunc.start_count(counter);
711 // eax = pContext->GetOverrideRetPtr() no alignment needs
718 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
719 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
721 // vtbloffs=0, vtblidx=1
722 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
723 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
724 IA32_Call_Reg(&m_HookFunc, REG_EAX);
725 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
729 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
731 // mov ecx, [ebp+v_plugin_ret]
733 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
734 IA32_Mov_Rm_Reg(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
738 if (m_Proto.GetRet().pAssignOperator)
740 // lea edx, [ebp + v_plugin_ret]
741 // push edx <-- src addr
742 // msvc: ecx = eax <-- dest addr
743 // gcc: push eax <-- dest addr
747 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
749 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
750 IA32_Push_Reg(&m_HookFunc, REG_EDX);
751 #if SH_COMP == SH_COMP_MSVC
752 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
753 #elif SH_COMP == SH_COMP_GCC
754 IA32_Push_Reg(&m_HookFunc, REG_EAX);
757 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
758 CheckAlignmentBeforeCall();
759 IA32_Call_Reg(&m_HookFunc, REG_EAX);
760 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
762 AlignStackAfterCall(tmpAlign);
769 //mov edi, eax <-- destination
770 //lea esi, [ebp+v_plugin_ret] <-- src
771 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
772 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
774 BitwiseCopy_Do(m_Proto.GetRet().size);
778 m_HookFunc.end_count(counter);
779 m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
782 void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
784 // only for non-void functions!
785 if (m_Proto.GetRet().size == 0)
788 // retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
790 // OverrideRetPtr: vtblidx = 1
791 // OrigRetPtr: vtbldix = 2
792 // vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
795 // eax = pContext->GetOverrideRetPtr()
799 // eax = (status < MRES_OVERRIDE) ? 1 : 0
801 // cmp [ebp + v_status], MRES_OVERRIDE
802 // setl al <-- setcc optimization for ternary operators,
805 // lea eax, [4*eax + 0x4]
814 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
815 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
816 IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
817 IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
818 IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
820 IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
822 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
823 IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
825 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
826 IA32_Call_Reg(&m_HookFunc, REG_EDX);
827 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
829 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
832 void GenContext::DoReturn(int v_retptr, int v_memret_outaddr)
834 size_t size = m_Proto.GetRet().size;
838 // Get real ret pointer into ecx
839 // mov ecx, [ebp + v_ret_ptr]
840 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
842 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
845 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
850 if (m_Proto.GetRet().type == PassInfo::PassType_Float)
853 IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
855 IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
857 else if (m_Proto.GetRet().type == PassInfo::PassType_Basic ||
858 ((m_Proto.GetRet().type == PassInfo::PassType_Object) && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)) )
862 // size <= 4: return in EAX
863 // We align <4 sizes up to 4
866 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
870 // size <= 4: return in EAX:EDX
871 // We align 4<x<8 sizes up to 8
875 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
876 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
880 // size >8: return in memory
885 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
887 // *memret_outaddr = plugin_ret
888 if (m_Proto.GetRet().pCopyCtor)
890 // mov edx, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
891 // push edx <-- src addr
892 // msvc: ecx = [ebp + v_memret_outaddr] <-- dest addr
893 // gcc: push [ebp + v_memret_outaddr] <-- dest addr
897 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
899 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_REG);
900 IA32_Push_Reg(&m_HookFunc, REG_EDX);
902 #if SH_COMP == SH_COMP_MSVC
903 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_memret_outaddr);
904 #elif SH_COMP == SH_COMP_GCC
905 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, v_memret_outaddr);
908 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pCopyCtor));
909 CheckAlignmentBeforeCall();
910 IA32_Call_Reg(&m_HookFunc, REG_EAX);
911 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
913 AlignStackAfterCall(tmpAlign);
920 //mov edi, [ebp+v_memret_outaddr] <-- destination
921 //mov esi, ecx <-- src ( we set ecx to [ebp+v_retptr] before )
922 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDI, REG_EBP, v_memret_outaddr);
923 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESI, REG_ECX, MOD_REG);
925 BitwiseCopy_Do(m_Proto.GetRet().size);
928 // In both cases: return the pointer in EAX
929 // mov eax, [ebp + v_memret_outaddr]
930 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_memret_outaddr);
934 void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
935 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)
937 jitoffs_t counter, tmppos;
938 jitoffs_t counter2, tmppos2;
940 jitoffs_t loop_begin_counter;
942 // prev_res = MRES_IGNORED
943 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
945 m_HookFunc.start_count(loop_begin_counter);
947 // eax = pContext->GetNext()
954 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
955 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
957 // vtbloffs=0, vtblidx=0
958 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
959 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
960 IA32_Call_Reg(&m_HookFunc, REG_EAX);
961 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
966 IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
967 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
968 m_HookFunc.start_count(counter);
970 // prev_res = MRES_IGNORED
971 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res);
978 // eax = [eax+2*SIZE_PTR]
982 jit_int32_t caller_clean_bytes = 0; // gcc always, msvc never (hooks never have varargs!)
984 jit_int32_t alignBytes = AlignStackBeforeCall(
985 GetParamsTotalStackSize() + ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)!=0 ? SIZE_PTR : 0),
986 AlignStack_GCC_ThisOnStack | ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem) == 0 ? 0 : AlignStack_MemRet)
989 // vafmt: push va_buf
990 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
992 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
993 IA32_Push_Reg(&m_HookFunc, REG_ECX);
994 caller_clean_bytes += SIZE_PTR;
997 caller_clean_bytes += PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
999 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
1000 if (SH_COMP == SH_COMP_GCC)
1001 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1002 caller_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
1003 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1004 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
1005 CheckAlignmentBeforeCall();
1006 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1008 AlignStackAfterCall(alignBytes);
1010 // cleanup (gcc only)
1012 if (SH_COMP == SH_COMP_GCC)
1013 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
1015 DestroyParams(v_place_fbrr_base);
1017 SaveRetVal(v_plugin_ret, v_place_for_memret);
1019 // process meta return:
1020 // prev_res = cur_res
1021 // if (cur_res > status) status = cur_res;
1031 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1032 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
1033 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
1035 IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
1036 tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
1037 m_HookFunc.start_count(counter2);
1039 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
1041 m_HookFunc.end_count(counter2);
1042 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
1044 // process retval for non-void functions
1045 ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
1047 // jump back to loop begin
1048 tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
1049 m_HookFunc.end_count(loop_begin_counter);
1050 m_HookFunc.rewrite(tmppos2, static_cast<jit_int32_t>(-loop_begin_counter));
1052 m_HookFunc.end_count(counter);
1053 m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
1056 void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
1057 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)
1059 jitoffs_t counter, tmppos;
1060 jitoffs_t counter2, tmppos2;
1061 jitoffs_t counter3, tmppos3;
1063 // if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
1064 // *v_orig_ret = orig_call()
1066 // *v_orig_ret = *v_override_ret
1069 // cmp eax, MRES_SUPERCEDE
1071 // call pContext->ShouldCallOrig()
1072 // test al, al !! important: al, not eax! bool is only stored in the LSbyte
1076 // SaveRet(v_orig_ret)
1077 // jmp skip_dont_call:
1080 // *v_orig_ret = *v_override_ret
1083 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1084 IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
1086 tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
1087 m_HookFunc.start_count(counter);
1089 // eax = pContext->ShouldCallOrig()
1093 // eax = [eax + 3*SIZE_PTR]
1096 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
1097 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1099 // vtbloffs=0, vtblidx=3
1100 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1101 IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
1102 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1103 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1105 IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
1106 tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
1107 m_HookFunc.start_count(counter2);
1109 jit_int32_t caller_clean_bytes = 0; // gcc always, msvc when cdecl-like (varargs)
1111 jit_int32_t alignBytes = AlignStackBeforeCall(
1112 GetParamsTotalStackSize() + ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)!=0 ? 2*SIZE_PTR : 0),
1113 AlignStack_GCC_ThisOnStack |
1114 ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem) == 0 ? 0 : AlignStack_MemRet) |
1115 ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs) == 0 ? 0 : AlignStack_MSVC_ThisOnStack)
1118 // vafmt: push va_buf, then "%s"
1119 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1121 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
1122 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1123 IA32_Push_Imm32(&m_HookFunc, DownCastPtr("%s"));
1124 caller_clean_bytes += 2*SIZE_PTR;
1128 caller_clean_bytes += PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
1131 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
1132 if (SH_COMP == SH_COMP_GCC)
1134 // on gcc/mingw, this is the first parameter
1135 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1136 // on msvc without varargs, simply leave it in ecx
1138 // actually, if we're returning in memory, this pointer is the second param
1139 // and the memret pointer is the real first parameter
1140 caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1144 // On msvc, if we're returning in memory, the memret pointer is the first parameter
1145 caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1147 // actually, with varargs, the this pointer is the first param and the memret ptr
1148 // is the second one
1149 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1151 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1156 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
1157 CheckAlignmentBeforeCall();
1158 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1160 AlignStackAfterCall(alignBytes);
1163 if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1164 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
1166 DestroyParams(v_place_fbrr_base);
1169 SaveRetVal(v_orig_ret, v_place_for_memret);
1171 // Skip don't call variant
1172 tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
1173 m_HookFunc.start_count(counter3);
1177 m_HookFunc.end_count(counter);
1178 m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
1180 m_HookFunc.end_count(counter2);
1181 m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
1183 // *v_orig_ret = *v_override_ret
1184 if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
1186 // mov ecx, [ebp + v_override_ret]
1187 // mov [ebp + v_orig_ret], ecx
1189 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1190 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_ECX, v_orig_ret);
1194 if (m_Proto.GetRet().pAssignOperator)
1196 // lea edx, [ebp + v_override_ret] <-- src addr
1197 // lea ecx, [ebp + v_orig_ret] <-- dest addr
1198 // push edx <-- src addr
1203 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
1205 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
1206 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1207 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1208 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1210 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
1211 CheckAlignmentBeforeCall();
1212 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1213 GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1215 AlignStackAfterCall(tmpAlign);
1220 BitwiseCopy_Setup();
1222 //lea edi, [ebp+v_orig_ret] <-- destination
1223 //lea esi, [ebp+v_override_ret] <-- src
1224 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
1225 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
1227 BitwiseCopy_Do(m_Proto.GetRet().size);
1231 // skip don't call label target:
1232 m_HookFunc.end_count(counter3);
1233 m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
1236 // Sets *v_pContext to return value
1237 void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret,
1238 int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
1239 int v_this, int v_pContext)
1241 // call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
1242 // &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
1243 // The last two params are null for void funcs.
1245 if (m_Proto.GetRet().size == 0)
1248 IA32_Push_Imm8(&m_HookFunc, 0); // orig_ret
1249 IA32_Push_Imm8(&m_HookFunc, 0); // override_ret
1253 // orig_ret and override_ret
1254 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
1255 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
1256 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1257 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1260 // cur_res and prev_res
1261 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1262 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
1263 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1264 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1266 // status and vfnptr_origentry
1267 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1268 IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
1269 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1270 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1273 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
1274 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1277 // *(this + vtbloffs) + SIZE_PTR*vtblidx
1278 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this); // get this into ecx (gcc!)
1279 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
1280 IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
1281 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1284 IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1285 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1286 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1289 #if SH_COMP == SH_COMP_GCC
1290 // on gcc/mingw, this is the first parameter
1291 GCC_ONLY(IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr)));
1292 #elif SH_COMP == SH_COMP_MSVC
1293 // on msvc, it's ecx
1294 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1297 // call the function. vtbloffs = 0, vtblidx = 19
1298 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1299 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1300 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
1302 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1304 // on gcc/mingw, we have to clean up after the call
1305 // 9 params + hidden thisptr param
1306 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG));
1308 // store return value
1309 IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
1312 void GenContext::CallEndContext(int v_pContext)
1315 // shptr->EndContext(pContex)
1316 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
1317 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1320 #if SH_COMP == SH_COMP_GCC
1321 // on gcc/mingw, this is the first parameter
1322 IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
1323 #elif SH_COMP == SH_COMP_MSVC
1324 // on msvc, it's ecx
1325 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1328 // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1329 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1330 (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
1332 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1334 // on gcc/mingw, we have to clean up after the call
1335 // 1 param + hidden thisptr param
1336 GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
1339 void GenContext::ResetFrame(jit_int32_t startOffset)
1341 m_HookFunc_FrameOffset = startOffset;
1342 m_HookFunc_FrameVarsSize = 0;
1345 jit_int32_t GenContext::AddVarToFrame(jit_int32_t size)
1347 m_HookFunc_FrameOffset -= size;
1348 m_HookFunc_FrameVarsSize += size;
1349 return m_HookFunc_FrameOffset;
1352 jit_int32_t GenContext::ComputeVarsSize()
1354 return m_HookFunc_FrameVarsSize;
1357 void * GenContext::GenerateHookFunc()
1360 IA32_Push_Reg(&m_HookFunc, REG_EBP);
1361 IA32_Push_Reg(&m_HookFunc, REG_EBX);
1362 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
1364 jit_int32_t v_this = 0;
1365 jit_int32_t param_base_offs = 0;
1366 if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1368 // gcc or msvc with varargs:
1369 v_this = 12; // first param
1370 param_base_offs = 16;
1375 // on msvc without varargs, save thisptr
1377 param_base_offs = 12;
1378 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1379 ResetFrame(-4); // start placing local vars on offset -4
1380 // because there already is the thisptr variable
1383 // ********************** stack frame **********************
1384 // MSVC without varargs
1385 // second param (gcc: first real param) ebp + 16
1386 // first param (gcc: thisptr) ebp + 12
1387 // ret address: ebp + 8
1388 // caller's ebp ebp + 4
1390 // MSVC ONLY: current this ebp - 4
1391 // void *vfnptr_origentry ebp - 4 -4
1392 // META_RES status = MRES_IGNORED ebp - 8 -4
1393 // META_RES prev_res ebp - 12 -4
1394 // META_RES cur_res ebp - 16 -4
1395 // IMyDelegate *iter ebp - 20 -4
1396 // IHookContext *pContext ebp - 24 -4
1397 // == 3 ptrs + 3 enums = 24 bytes
1400 // my_rettype *ret_ptr ebp - 28 -4
1401 // my_rettype orig_ret ebp - 28 - sizeof(my_rettype) -4
1402 // my_rettype override_ret ebp - 28 - sizeof(my_rettype)*2 -4
1403 // my_rettype plugin_ret ebp - 28 - sizeof(my_rettype)*3 -4
1404 // == + 3 * sizeof(my_rettype) bytes
1407 // my_rettype place_for_memret ebp - 28 - sizeof(my_rettype)*4 -4
1409 // gcc only: if required:
1410 // place forced byref params ebp - 28 - sizeof(my_rettype)*{4 or 5}
1414 // char va_buf[something];
1417 const jit_int8_t v_vfnptr_origentry = AddVarToFrame(SIZE_PTR);
1418 const jit_int8_t v_status = AddVarToFrame(sizeof(META_RES));
1419 const jit_int8_t v_prev_res = AddVarToFrame(sizeof(META_RES));
1420 const jit_int8_t v_cur_res = AddVarToFrame(sizeof(META_RES));
1421 const jit_int8_t v_iter = AddVarToFrame(SIZE_PTR);
1422 const jit_int8_t v_pContext = AddVarToFrame(SIZE_PTR);
1424 // Memory return: first param is the address
1425 jit_int32_t v_memret_addr = 0;
1426 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1428 if (SH_COMP == SH_COMP_GCC)
1430 // gcc: now: first param = mem ret addr
1431 // second param = this pointer
1432 // third param = actual first param
1435 param_base_offs += SIZE_PTR;
1439 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1443 // first param = this pointer
1444 // second param = mem ret addr
1445 // third param = actual first param
1447 // params_base_offs is already updated to point to after the this pointer
1448 v_memret_addr = param_base_offs;
1449 param_base_offs += SIZE_PTR;
1453 v_memret_addr = param_base_offs;
1454 param_base_offs += SIZE_PTR;
1459 jit_int32_t v_ret_ptr = 0;
1460 jit_int32_t v_orig_ret = 0;
1461 jit_int32_t v_override_ret = 0;
1462 jit_int32_t v_plugin_ret = 0;
1464 if (m_Proto.GetRet().size != 0)
1466 v_ret_ptr = AddVarToFrame(SIZE_PTR);
1467 v_orig_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1468 v_override_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1469 v_plugin_ret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1472 jit_int32_t v_place_for_memret = 0;
1473 if (MemRetWithTempObj())
1475 v_place_for_memret = AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
1478 jit_int32_t v_place_fbrr_base = 0;
1479 if (SH_COMP == SH_COMP_GCC && GetForcedByRefParamsSize())
1481 v_place_fbrr_base = AddVarToFrame(GetForcedByRefParamsSize());
1484 // Only exists for varargs functions
1485 jit_int32_t v_va_argptr = 0;
1486 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1488 v_va_argptr = AddVarToFrame(SIZE_PTR);
1491 jit_int32_t v_va_buf = 0;
1492 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1494 v_va_buf = AddVarToFrame(SourceHook::STRBUF_LEN);
1497 IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, ComputeVarsSize(), MOD_REG);
1499 // Initial stack alignment
1500 IA32_And_Rm_Imm32(&m_HookFunc, REG_ESP, MOD_REG, -16);
1501 m_BytesPushedAfterInitialAlignment = 0;
1503 // init status localvar
1504 IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
1506 // VarArgs: init argptr & format
1507 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1509 // argptr = first vararg param
1510 // lea eax, [ebp + param_base_offs + paramssize]
1513 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_base_offs + GetParamsTotalStackSize() + SIZE_PTR); // +SIZE_PTR: last const char * is not in protoinfo
1514 IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_va_argptr);
1516 if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
1520 jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR*3 + sizeof(size_t), 0);
1522 // push valist, fmt param, maxsize, buffer
1523 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1524 IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_base_offs + GetParamsTotalStackSize()); // last given param (+4-4, see above)
1525 IA32_Push_Imm32(&m_HookFunc, SourceHook::STRBUF_LEN - 1);
1526 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
1527 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1530 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(&vsnprintf));
1531 CheckAlignmentBeforeCall();
1532 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1535 IA32_Add_Rm_Imm32(&m_HookFunc, REG_ESP, 0x10, MOD_REG);
1537 AlignStackAfterCall(tmpAlign);
1539 // Set trailing zero
1540 IA32_Xor_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_REG);
1541 IA32_Mov_Rm8_Reg8_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_va_buf + SourceHook::STRBUF_LEN - 1);
1544 // Call constructors for ret vars if required
1545 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1546 m_Proto.GetRet().pNormalCtor)
1548 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
1551 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1552 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1553 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1554 CheckAlignmentBeforeCall();
1555 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1556 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1559 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1560 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1561 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1562 CheckAlignmentBeforeCall();
1563 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1564 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1567 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1568 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1569 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1570 CheckAlignmentBeforeCall();
1571 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1572 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1574 AlignStackAfterCall(tmpAlign);
1576 // _don't_ call a constructor for v_place_for_memret !
1579 // ********************** SetupHookLoop **********************
1580 CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
1581 v_this, v_pContext);
1583 // ********************** call pre hooks **********************
1584 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1585 v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1587 // ********************** call orig func **********************
1588 GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
1589 v_override_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1591 // ********************** call post hooks **********************
1592 GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1593 v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
1595 // ********************** end context and return **********************
1597 PrepareReturn(v_status, v_pContext, v_ret_ptr);
1599 CallEndContext(v_pContext);
1601 // Call destructors of byval object params which have a destructor
1603 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
1605 jit_int32_t cur_param_pos = param_base_offs;
1606 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1608 const IntPassInfo &pi = m_Proto.GetParam(i);
1609 // GCC: NOT of forced byref params. the caller destructs those.
1610 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
1611 (pi.flags & PassInfo::PassFlag_ByVal) && !(pi.flags & PassFlag_ForcedByRef))
1613 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
1614 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
1615 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1616 CheckAlignmentBeforeCall();
1617 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1618 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1620 cur_param_pos += GetParamStackSize(pi);
1623 AlignStackAfterCall(tmpAlign);
1625 DoReturn(v_ret_ptr, v_memret_addr);
1627 // Call destructors of orig_ret/ ...
1628 if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
1629 m_Proto.GetRet().pDtor)
1631 // Preserve return value in EAX(:EDX)
1632 IA32_Push_Reg(&m_HookFunc, REG_EAX);
1633 IA32_Push_Reg(&m_HookFunc, REG_EDX);
1635 m_BytesPushedAfterInitialAlignment += 8;
1637 jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
1639 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_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_override_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 IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1654 GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
1655 IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
1656 CheckAlignmentBeforeCall();
1657 IA32_Call_Reg(&m_HookFunc, REG_EAX);
1658 GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
1660 AlignStackAfterCall(tmpAlign);
1662 IA32_Pop_Reg(&m_HookFunc, REG_EDX);
1663 IA32_Pop_Reg(&m_HookFunc, REG_EAX);
1665 m_BytesPushedAfterInitialAlignment -= 8;
1669 IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
1670 IA32_Pop_Reg(&m_HookFunc, REG_EBX);
1671 IA32_Pop_Reg(&m_HookFunc, REG_EBP);
1673 if (SH_COMP == SH_COMP_MSVC && !(m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1675 // msvc without varargs:
1676 // callee cleans the stack
1678 short cleansize = GetParamsTotalStackSize();
1679 // Memory return: address is first param
1680 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1681 cleansize += SIZE_PTR;
1683 IA32_Return_Popstack(&m_HookFunc, cleansize);
1687 // gcc or msvc with varargs: caller cleans the stack
1688 // exception: gcc removes the memret addr on memret:
1689 if (SH_COMP == SH_COMP_GCC && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem))
1690 IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
1692 IA32_Return(&m_HookFunc);
1696 // Store pointer for later use
1697 // m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
1698 // into the hookman's vtable
1699 *m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
1703 return m_HookFunc.GetData();
1706 // Pre-condition: GenerateHookFunc() has been called!
1707 void * GenContext::GeneratePubFunc()
1709 jitoffs_t counter, tmppos;
1711 // The pubfunc is a static cdecl function.
1713 // int HookManPubFunc(
1714 // bool store, ebp + 8
1715 // IHookManagerInfo *hi ebp + 12
1721 // hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
1725 IA32_Push_Reg(&m_PubFunc, REG_EBP);
1726 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
1729 // save store in eax, hi in ecx
1730 IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
1731 IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
1733 // Check for store == 0
1734 IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1735 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1736 m_PubFunc.start_count(counter);
1738 // nonzero -> store hi
1739 IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1740 IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1743 m_PubFunc.end_count(counter);
1744 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1746 // check for hi == 0
1747 IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
1748 tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1749 m_PubFunc.start_count(counter);
1751 // nonzero -> call vfunc
1752 // push params in reverse order
1753 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
1754 IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
1755 IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
1756 IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
1757 IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
1759 // hi == this is in ecx
1760 // on gcc/mingw, ecx is the first parameter
1761 #if SH_COMP == SH_COMP_GCC
1762 IA32_Push_Reg(&m_PubFunc, REG_ECX);
1765 // call the function. vtbloffs = 0, vtblidx = 0
1766 // get vtptr into edx
1767 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1768 // get funcptr into eax
1769 IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1771 IA32_Call_Reg(&m_PubFunc, REG_EAX);
1773 // on gcc/mingw, we have to clean up after the call
1774 #if SH_COMP == SH_COMP_GCC
1775 // 5 params + hidden thisptr param
1776 IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
1780 m_PubFunc.end_count(counter);
1781 m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1784 IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1787 IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
1788 IA32_Pop_Reg(&m_PubFunc, REG_EBP);
1789 IA32_Return(&m_PubFunc);
1796 bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
1798 // :TODO: Error returns
1799 if (pi.type != PassInfo::PassType_Basic &&
1800 pi.type != PassInfo::PassType_Float &&
1801 pi.type != PassInfo::PassType_Object)
1806 if (pi.type == PassInfo::PassType_Object &&
1807 (pi.flags & PassInfo::PassFlag_ByVal))
1809 if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pCopyCtor)
1814 if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
1819 if ((pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
1824 if ((pi.flags & PassInfo::PassFlag_OCtor) && !pi.pNormalCtor)
1830 if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
1832 return false; // Neither byval nor byref!
1837 void GenContext::AutoDetectRetType()
1839 IntPassInfo &pi = m_Proto.GetRet();
1841 // Only relevant for byval types
1842 if (pi.flags & PassInfo::PassFlag_ByVal)
1845 if (pi.type == PassInfo::PassType_Basic ||
1846 pi.type == PassInfo::PassType_Float)
1849 // _always_ in registers, no matter what the user says
1852 pi.flags &= ~PassInfo::PassFlag_RetMem;
1853 pi.flags |= PassInfo::PassFlag_RetReg;
1857 // Does this even exist? No idea, if it does: in memory!
1858 pi.flags &= ~PassInfo::PassFlag_RetReg;
1859 pi.flags |= PassInfo::PassFlag_RetMem;
1863 else if (pi.type == PassInfo::PassType_Object)
1865 // If the user says nothing, auto-detect
1866 if ((pi.flags & (PassInfo::PassFlag_RetMem | PassInfo::PassFlag_RetReg)) == 0)
1868 #if SH_COMP == SH_COMP_MSVC
1869 // MSVC seems to return _all_ structs, classes, unions in memory
1870 pi.flags |= PassInfo::PassFlag_RetMem;
1871 #elif SH_COMP == SH_COMP_GCC
1872 // Same goes for GCC :)
1873 pi.flags |= PassInfo::PassFlag_RetMem;
1880 // byref: make sure that the flag is _not_ set
1881 pi.flags &= ~PassInfo::PassFlag_RetMem;
1882 pi.flags |= PassInfo::PassFlag_RetReg;
1886 void GenContext::AutoDetectParamFlags()
1888 #if SH_COMP == SH_COMP_GCC
1889 // On GCC, all objects are passed by reference if they have a destructor
1890 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1892 IntPassInfo &pi = m_Proto.GetParam(i);
1893 if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ByVal) &&
1894 (pi.flags & PassInfo::PassFlag_ODtor))
1896 pi.flags |= PassFlag_ForcedByRef;
1902 HookManagerPubFunc GenContext::Generate()
1906 // Check conditions:
1907 // -1) good proto version
1908 // 0) we don't support unknown passtypes, convention, ...
1909 // 1) we don't support functions which return objects by value or take parameters by value
1910 // that have a constructor, a destructor or an overloaded assignment op
1911 // (we wouldn't know how to call it!)
1913 if (m_Proto.GetVersion() < 1)
1918 AutoDetectRetType();
1919 AutoDetectParamFlags();
1921 // Basically, we only support ThisCall/thiscall with varargs
1922 if ((m_Proto.GetConvention() & (~ProtoInfo::CallConv_HasVafmt)) != ProtoInfo::CallConv_ThisCall)
1928 if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
1933 for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1935 if (!PassInfoSupported(m_Proto.GetParam(i), false))
1941 return fastdelegate::detail::horrible_cast<HookManagerPubFunc>(GeneratePubFunc());
1944 HookManagerPubFunc GenContext::GetPubFunc()
1946 if (m_GeneratedPubFunc == 0)
1947 m_GeneratedPubFunc = Generate();
1949 return m_GeneratedPubFunc;
1952 bool GenContext::Equal(const CProto &proto, int vtbl_offs, int vtbl_idx)
1954 return (m_OrigProto.ExactlyEqual(proto) && m_VtblOffs == vtbl_offs && m_VtblIdx == vtbl_idx);
1957 bool GenContext::Equal(HookManagerPubFunc other)
1959 return m_GeneratedPubFunc == other;
1962 // *********************************** class GenContextContainer
1963 CHookManagerAutoGen::CHookManagerAutoGen(ISourceHook *pSHPtr) : m_pSHPtr(pSHPtr)
1967 CHookManagerAutoGen::~CHookManagerAutoGen()
1969 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
1971 delete iter->m_GenContext;
1975 int CHookManagerAutoGen::GetIfaceVersion()
1977 return SH_HOOKMANAUTOGEN_IFACE_VERSION;
1980 int CHookManagerAutoGen::GetImplVersion()
1982 return SH_HOOKMANAUTOGEN_IMPL_VERSION;
1985 HookManagerPubFunc CHookManagerAutoGen::MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx)
1987 CProto mproto(proto);
1988 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
1990 if (iter->m_GenContext->Equal(mproto, vtbl_offs, vtbl_idx))
1993 return iter->m_GenContext->GetPubFunc();
1997 // Not found yet -> new one
2000 sctx.m_GenContext = new GenContext(proto, vtbl_offs, vtbl_idx, m_pSHPtr);
2002 if (sctx.m_GenContext->GetPubFunc() == NULL)
2008 m_Contexts.push_back(sctx);
2009 return sctx.m_GenContext->GetPubFunc();
2013 void CHookManagerAutoGen::ReleaseHookMan(HookManagerPubFunc pubFunc)
2015 for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
2017 if (iter->m_GenContext->Equal(pubFunc))
2020 if (iter->m_RefCnt == 0)
2022 delete iter->m_GenContext;
2023 m_Contexts.erase(iter);