1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/sourcehook_hookmangen.cpp Sun Oct 21 21:47:53 2007 +0000
1.3 @@ -0,0 +1,1292 @@
1.4 +/* ======== SourceHook ========
1.5 +* Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 +* No warranties of any kind
1.7 +*
1.8 +* License: zlib/libpng
1.9 +*
1.10 +* Author(s): Pavol "PM OnoTo" Marko
1.11 +* Contributor(s): Borja "faluco" Ferav (many thanks for assitance!)
1.12 +* David "BAILOPAN" Anderson
1.13 +* ============================
1.14 +*/
1.15 +
1.16 +// recommended literature:
1.17 +// http://www.cs.umbc.edu/~chang/cs313.s02/stack.shtml
1.18 +// http://www.angelcode.com/dev/callconv/callconv.html
1.19 +// http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
1.20 +
1.21 +#include "sourcehook_hookmangen.h"
1.22 +#include "sourcehook_hookmangen_x86.h"
1.23 +#include "sh_memory.h"
1.24 +
1.25 +// :TODO: test BIG vtable indices
1.26 +
1.27 +namespace SourceHook
1.28 +{
1.29 + namespace Impl
1.30 + {
1.31 + template <class T>
1.32 + jit_int32_t DownCastPtr(T ptr)
1.33 + {
1.34 + return reinterpret_cast<jit_int32_t>(ptr);
1.35 + }
1.36 +
1.37 + jit_uint32_t DownCastSize(size_t size)
1.38 + {
1.39 + return static_cast<jit_uint32_t>(size);
1.40 + }
1.41 +
1.42 + GenContext::GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr)
1.43 + : m_Proto(proto), m_VtblOffs(vtbl_offs), m_VtblIdx(vtbl_idx), m_SHPtr(pSHPtr),
1.44 + m_pHI(NULL), m_HookfuncVfnptr(NULL), m_RegCounter(0)
1.45 + {
1.46 + m_pHI = new void*;
1.47 + m_HookfuncVfnptr = new void*;
1.48 + m_BuiltPI = new ProtoInfo;
1.49 + m_BuiltPI_Params = NULL;
1.50 + m_BuiltPI_Params2 = NULL;
1.51 + }
1.52 +
1.53 + GenContext::~GenContext()
1.54 + {
1.55 + Clear();
1.56 + delete m_pHI;
1.57 + delete m_HookfuncVfnptr;
1.58 + delete m_BuiltPI;
1.59 + }
1.60 +
1.61 + void GenContext::Clear()
1.62 + {
1.63 + m_HookFunc.clear();
1.64 + m_PubFunc.clear();
1.65 + if (m_BuiltPI_Params)
1.66 + {
1.67 + delete m_BuiltPI_Params;
1.68 + m_BuiltPI_Params = NULL;
1.69 + }
1.70 + if (m_BuiltPI_Params2)
1.71 + {
1.72 + delete m_BuiltPI_Params2;
1.73 + m_BuiltPI_Params2 = NULL;
1.74 + }
1.75 + }
1.76 +
1.77 + void GenContext::BuildProtoInfo()
1.78 + {
1.79 + m_BuiltPI->convention = m_Proto.GetConvention();
1.80 + m_BuiltPI->numOfParams = m_Proto.GetNumOfParams();
1.81 +
1.82 + m_BuiltPI->retPassInfo.size = m_Proto.GetRet().size;
1.83 + m_BuiltPI->retPassInfo.type = m_Proto.GetRet().type;
1.84 + m_BuiltPI->retPassInfo.flags = m_Proto.GetRet().flags;
1.85 + m_BuiltPI->retPassInfo2.pNormalCtor = m_Proto.GetRet().pNormalCtor;
1.86 + m_BuiltPI->retPassInfo2.pCopyCtor = m_Proto.GetRet().pCopyCtor;
1.87 + m_BuiltPI->retPassInfo2.pDtor = m_Proto.GetRet().pDtor;
1.88 + m_BuiltPI->retPassInfo2.pAssignOperator = m_Proto.GetRet().pAssignOperator;
1.89 +
1.90 + if (m_BuiltPI_Params)
1.91 + delete m_BuiltPI_Params;
1.92 + m_BuiltPI_Params = new PassInfo[m_BuiltPI->numOfParams + 1];
1.93 + if (m_BuiltPI_Params2)
1.94 + delete m_BuiltPI_Params2;
1.95 + m_BuiltPI_Params2 = new PassInfo::V2Info[m_BuiltPI->numOfParams + 1];
1.96 +
1.97 + m_BuiltPI_Params[0].size = 1; // Version 1
1.98 + m_BuiltPI_Params[0].type = 0;
1.99 + m_BuiltPI_Params[0].flags = 0;
1.100 +
1.101 + for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1.102 + {
1.103 + m_BuiltPI_Params[i+1].size = m_Proto.GetParam(i).size;
1.104 + m_BuiltPI_Params[i+1].type = m_Proto.GetParam(i).type;
1.105 + m_BuiltPI_Params[i+1].flags = m_Proto.GetParam(i).flags;
1.106 +
1.107 + m_BuiltPI_Params2[i+1].pNormalCtor = m_Proto.GetParam(i).pNormalCtor;
1.108 + m_BuiltPI_Params2[i+1].pCopyCtor = m_Proto.GetParam(i).pCopyCtor;
1.109 + m_BuiltPI_Params2[i+1].pDtor = m_Proto.GetParam(i).pDtor;
1.110 + m_BuiltPI_Params2[i+1].pAssignOperator = m_Proto.GetParam(i).pAssignOperator;
1.111 + }
1.112 +
1.113 + m_BuiltPI->paramsPassInfo = m_BuiltPI_Params;
1.114 + m_BuiltPI->paramsPassInfo2 = m_BuiltPI_Params2;
1.115 + }
1.116 +
1.117 + jit_int32_t GenContext::GetRealSize(const IntPassInfo &info)
1.118 + {
1.119 + if (info.flags & PassInfo::PassFlag_ByRef)
1.120 + {
1.121 + return SIZE_PTR;
1.122 + }
1.123 + return static_cast<jit_int32_t>(info.size);
1.124 + }
1.125 +
1.126 + // Computes size on the stack
1.127 + jit_int32_t GenContext::GetStackSize(const IntPassInfo &info)
1.128 + {
1.129 + // Align up to 4 byte boundaries
1.130 + jit_int32_t rs = GetRealSize(info);
1.131 + if (rs % 4 != 0)
1.132 + rs = (rs & ~(3)) + 4;
1.133 + return rs;
1.134 + }
1.135 +
1.136 + jit_int8_t GenContext::NextRegEBX_ECX_EDX()
1.137 + {
1.138 + switch ((m_RegCounter++) % 3)
1.139 + {
1.140 + case 0:
1.141 + return REG_EBX;
1.142 + case 1:
1.143 + return REG_ECX;
1.144 + case 2:
1.145 + default:
1.146 + m_RegCounter = 0;
1.147 + return REG_EDX;
1.148 + }
1.149 + }
1.150 +
1.151 + jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
1.152 + {
1.153 + // push [ebp+<offset>]
1.154 + IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_offset);
1.155 +
1.156 + return SIZE_PTR;
1.157 + }
1.158 +
1.159 + jit_int32_t GenContext::PushBasic(jit_int32_t param_offset, const IntPassInfo &pi)
1.160 + {
1.161 + int reg;
1.162 + int reg2;
1.163 +
1.164 + switch (pi.size)
1.165 + {
1.166 + default:
1.167 + SH_ASSERT(0, ("Unsupported!"));
1.168 + return 0;
1.169 + case 1:
1.170 + reg = NextRegEBX_ECX_EDX();
1.171 + //movzx reg, BYTE PTR [ebp+<offset>]
1.172 + //push reg
1.173 + IA32_Movzx_Reg32_Rm8_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
1.174 + IA32_Push_Reg(&m_HookFunc, reg);
1.175 +
1.176 + return 4;
1.177 + case 2:
1.178 + reg = NextRegEBX_ECX_EDX();
1.179 + //movzx reg, WORD PTR [ebp+<offset>]
1.180 + //push reg
1.181 + m_HookFunc.write_ubyte(IA32_16BIT_PREFIX);
1.182 + IA32_Movzx_Reg32_Rm16_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
1.183 + IA32_Push_Reg(&m_HookFunc, reg);
1.184 +
1.185 + return 4;
1.186 + case 4:
1.187 + reg = NextRegEBX_ECX_EDX();
1.188 + //mov reg, DWORD PTR [ebp+<offset>]
1.189 + //push reg
1.190 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
1.191 + IA32_Push_Reg(&m_HookFunc, reg);
1.192 +
1.193 + return 4;
1.194 + case 8:
1.195 + reg = NextRegEBX_ECX_EDX();
1.196 + reg2 = NextRegEBX_ECX_EDX();
1.197 + //mov reg, DWORD PTR [ebp+<offset>+4]
1.198 + //mov reg2, DWORD PTR [ebp+<offset>]
1.199 + //push reg
1.200 + //push reg2
1.201 +
1.202 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset+4);
1.203 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg2, REG_EBP, param_offset);
1.204 + IA32_Push_Reg(&m_HookFunc, reg);
1.205 + IA32_Push_Reg(&m_HookFunc, reg2);
1.206 +
1.207 + return 8;
1.208 + }
1.209 + }
1.210 +
1.211 + jit_int32_t GenContext::PushFloat(jit_int32_t param_offset, const IntPassInfo &pi)
1.212 + {
1.213 + switch (pi.size)
1.214 + {
1.215 + default:
1.216 + SH_ASSERT(0, ("Unsupported!"));
1.217 + return 0;
1.218 + case 4:
1.219 + //fld DWORD PTR [ebp+<offset>]
1.220 + //push reg
1.221 + //fstp DWORD PTR [esp]
1.222 + IA32_Fld_Mem32_DispAuto(&m_HookFunc, REG_EBP, param_offset);
1.223 + IA32_Push_Reg(&m_HookFunc, NextRegEBX_ECX_EDX());
1.224 + IA32_Fstp_Mem32_ESP(&m_HookFunc);
1.225 + return 4;
1.226 + case 8:
1.227 + //fld QWORD PTR [ebp+<offset>]
1.228 + //sub esp, 8
1.229 + //fstp QWORD PTR [esp]
1.230 + IA32_Fld_Mem64_DispAuto(&m_HookFunc, REG_EBP, param_offset);
1.231 + IA32_Sub_Rm_Imm8(&m_HookFunc, REG_ESP, 8, MOD_REG);
1.232 + IA32_Fstp_Mem64_ESP(&m_HookFunc);
1.233 + return 8;
1.234 + }
1.235 + }
1.236 +
1.237 + jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi)
1.238 + {
1.239 + // make room on the stack
1.240 + // sub esp, <size>
1.241 + IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetStackSize(pi), MOD_REG);
1.242 +
1.243 + // if there is a copy constructor..
1.244 + if (pi.pCopyCtor)
1.245 + {
1.246 + // save eax
1.247 + // push src addr
1.248 + // this= target addr = esp+12
1.249 + // call copy constructor
1.250 + // restore eax
1.251 +
1.252 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.253 + IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
1.254 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
1.255 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.256 +#if SH_COMP == SH_COMP_GCC
1.257 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.258 +#endif
1.259 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(pi.pCopyCtor));
1.260 + IA32_Call_Reg(&m_HookFunc, REG_EDX);
1.261 +
1.262 + IA32_Pop_Reg(&m_HookFunc, REG_EAX);
1.263 + }
1.264 + else
1.265 + {
1.266 + jit_uint32_t dwords = DownCastSize(pi.size) / 4;
1.267 + jit_uint32_t bytes = DownCastSize(pi.size) % 4;
1.268 +
1.269 + // bitwise copy
1.270 +
1.271 + //cld
1.272 + //push edi
1.273 + //push esi
1.274 + //lea edi, [esp+8]
1.275 + //lea esi, [ebp+<offs>]
1.276 + //if dwords
1.277 + // mov ecx, <dwords>
1.278 + // rep movsd
1.279 + //if bytes
1.280 + // mov ecx, <bytes>
1.281 + // rep movsb
1.282 + //pop esi
1.283 + //pop edi
1.284 +
1.285 + IA32_Cld(&m_HookFunc);
1.286 + IA32_Push_Reg(&m_HookFunc, REG_EDI);
1.287 + IA32_Push_Reg(&m_HookFunc, REG_ESI);
1.288 + IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
1.289 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
1.290 + if (dwords)
1.291 + {
1.292 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
1.293 + IA32_Rep(&m_HookFunc);
1.294 + IA32_Movsd(&m_HookFunc);
1.295 + }
1.296 + if (bytes)
1.297 + {
1.298 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
1.299 + IA32_Rep(&m_HookFunc);
1.300 + IA32_Movsb(&m_HookFunc);
1.301 + }
1.302 + IA32_Pop_Reg(&m_HookFunc, REG_ESI);
1.303 + IA32_Pop_Reg(&m_HookFunc, REG_EDI);
1.304 + }
1.305 +
1.306 + return DownCastSize(pi.size);
1.307 + }
1.308 +
1.309 + // May not touch eax!
1.310 + jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset)
1.311 + {
1.312 + jit_int32_t added_to_stack = 0;
1.313 + jit_int32_t ret = 0;
1.314 +
1.315 + // compute the offset _after_ the last parameter
1.316 + jit_int32_t cur_offset = param_base_offset;
1.317 + for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1.318 + {
1.319 + cur_offset += GetStackSize(m_Proto.GetParam(i));
1.320 + }
1.321 +
1.322 + // push parameters in reverse order
1.323 + for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
1.324 + {
1.325 + const IntPassInfo &pi = m_Proto.GetParam(i);
1.326 + cur_offset -= GetStackSize(pi);
1.327 + if (pi.flags & PassInfo::PassFlag_ByVal)
1.328 + {
1.329 + switch (pi.type)
1.330 + {
1.331 + case PassInfo::PassType_Basic:
1.332 + ret = PushBasic(cur_offset, pi);
1.333 + break;
1.334 + case PassInfo::PassType_Float:
1.335 + ret = PushFloat(cur_offset, pi);
1.336 + break;
1.337 + case PassInfo::PassType_Object:
1.338 + ret = PushObject(cur_offset, pi);
1.339 + break;
1.340 + }
1.341 + }
1.342 + else if (pi.flags & PassInfo::PassFlag_ByRef)
1.343 + {
1.344 + PushRef(cur_offset, pi);
1.345 + }
1.346 + else
1.347 + {
1.348 + SH_ASSERT(0, ("Unsupported!"));
1.349 + }
1.350 + added_to_stack += ret;
1.351 + }
1.352 + return added_to_stack;
1.353 + }
1.354 +
1.355 + void GenContext::SaveRetVal(int v_where)
1.356 + {
1.357 + // :TODO: assign op support
1.358 + // :TODO: memory return support
1.359 +
1.360 + size_t size = m_Proto.GetRet().size;
1.361 + if (size == 0)
1.362 + {
1.363 + // No return value -> nothing
1.364 + return;
1.365 + }
1.366 +
1.367 + if (m_Proto.GetRet().type == PassInfo::PassType_Float)
1.368 + {
1.369 + if (size == 4)
1.370 + IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
1.371 + else if (size == 8)
1.372 + IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
1.373 + }
1.374 + else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
1.375 + {
1.376 + if (size <= 4)
1.377 + {
1.378 + // size <= 4: return in EAX
1.379 + // We align <4 sizes up to 4
1.380 +
1.381 +
1.382 + // mov [ebp + v_plugin_ret], eax
1.383 + IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
1.384 + }
1.385 + else if (size <= 8)
1.386 + {
1.387 + // size <= 4: return in EAX:EDX
1.388 + // We align 4<x<8 sizes up to 8
1.389 +
1.390 + // mov [ebp + v_plugin_ret], eax
1.391 + // mov [ebp + v_plugin_ret + 4], edx
1.392 + IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
1.393 + IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
1.394 + }
1.395 + else
1.396 + {
1.397 + // size >8: return in memory
1.398 + // :TODO:
1.399 + // add flag: MSVC_RetInMemory?
1.400 + }
1.401 + }
1.402 + // :TODO: object
1.403 + }
1.404 +
1.405 + void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
1.406 + {
1.407 + // only for non-void functions!
1.408 + if (m_Proto.GetRet().size == 0)
1.409 + return;
1.410 +
1.411 + // if (cur_res >= MRES_OVERRIDE)
1.412 + // *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
1.413 +
1.414 + // eax = cur_res
1.415 + // cmp eax,MRES_OVERRIDE
1.416 + // jnge thelabel
1.417 + // pContext->GetOverrideRetPtr() --> overrideretptr in eax
1.418 + // *eax = plugin_ret
1.419 + // thelabel:
1.420 + //
1.421 +
1.422 + jitoffs_t tmppos, counter;
1.423 +
1.424 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1.425 +
1.426 + IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
1.427 + tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
1.428 + m_HookFunc.start_count(counter);
1.429 +
1.430 + // eax = pContext->GetOverrideRetPtr()
1.431 + // ECX = pContext
1.432 + // gcc: push ecx
1.433 + // eax = [ecx]
1.434 + // eax = [eax + 4]
1.435 + // call eax
1.436 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
1.437 +#if SH_COMP == SH_COMP_GCC
1.438 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.439 +#endif
1.440 +
1.441 + // vtbloffs=0, vtblidx=1
1.442 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1.443 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
1.444 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.445 +
1.446 +
1.447 + // *eax = plugin_ret
1.448 + if (m_Proto.GetRet().pAssignOperator)
1.449 + {
1.450 + // lea edx, [ebp + v_plugin_ret]
1.451 + // msvc: ecx = eax <-- dest addr
1.452 + // gcc: push eax <-- dest addr
1.453 + // push edx <-- src addr
1.454 + // call it
1.455 +
1.456 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
1.457 +#if SH_COMP == SH_COMP_MSVC
1.458 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
1.459 +#elif SH_COMP == SH_COMP_GCC
1.460 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.461 +#endif
1.462 + IA32_Push_Reg(&m_HookFunc, REG_EDX);
1.463 +
1.464 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
1.465 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.466 + }
1.467 + else
1.468 + {
1.469 + jit_uint32_t dwords = DownCastSize(m_Proto.GetRet().size) / 4;
1.470 + jit_uint32_t bytes = DownCastSize(m_Proto.GetRet().size) % 4;
1.471 +
1.472 + // bitwise copy
1.473 +
1.474 + //cld
1.475 + //push edi
1.476 + //push esi
1.477 + //mov edi, eax <-- destination
1.478 + //lea esi, [ebp+v_plugin_ret] <-- src
1.479 + //if dwords
1.480 + // mov ecx, <dwords>
1.481 + // rep movsd
1.482 + //if bytes
1.483 + // mov ecx, <bytes>
1.484 + // rep movsb
1.485 + //pop esi
1.486 + //pop edi
1.487 +
1.488 + IA32_Cld(&m_HookFunc);
1.489 + IA32_Push_Reg(&m_HookFunc, REG_EDI);
1.490 + IA32_Push_Reg(&m_HookFunc, REG_ESI);
1.491 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
1.492 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
1.493 + if (dwords)
1.494 + {
1.495 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
1.496 + IA32_Rep(&m_HookFunc);
1.497 + IA32_Movsd(&m_HookFunc);
1.498 + }
1.499 + if (bytes)
1.500 + {
1.501 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
1.502 + IA32_Rep(&m_HookFunc);
1.503 + IA32_Movsb(&m_HookFunc);
1.504 + }
1.505 + IA32_Pop_Reg(&m_HookFunc, REG_ESI);
1.506 + IA32_Pop_Reg(&m_HookFunc, REG_EDI);
1.507 + }
1.508 +
1.509 + m_HookFunc.end_count(counter);
1.510 + m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1.511 + }
1.512 +
1.513 + void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
1.514 + {
1.515 + // only for non-void functions!
1.516 + if (m_Proto.GetRet().size == 0)
1.517 + return;
1.518 +
1.519 + // retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
1.520 +
1.521 + // OverrideRetPtr: vtblidx = 1
1.522 + // OrigRetPtr: vtbldix = 2
1.523 + // vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
1.524 +
1.525 + //
1.526 + // eax = pContext->GetOverrideRetPtr()
1.527 + // ECX = pContext
1.528 + // gcc: push ecx
1.529 +
1.530 + // eax = (status < MRES_OVERRIDE) ? 1 : 0
1.531 + // xor eax, eax
1.532 + // cmp [ebp + v_status], MRES_OVERRIDE
1.533 + // setl al <-- setcc optimization for ternary operators,
1.534 +
1.535 +
1.536 + // lea eax, [4*eax + 0x4]
1.537 +
1.538 + // edx = [ecx]
1.539 + // add edx, eax
1.540 + // mov edx, [edx]
1.541 +
1.542 + // call edx
1.543 +
1.544 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
1.545 +#if SH_COMP == SH_COMP_GCC
1.546 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.547 +#endif
1.548 + IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
1.549 + IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
1.550 + IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
1.551 +
1.552 + IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
1.553 +
1.554 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1.555 + IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
1.556 +
1.557 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
1.558 + IA32_Call_Reg(&m_HookFunc, REG_EDX);
1.559 +
1.560 + IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
1.561 + }
1.562 +
1.563 + void GenContext::DoReturn(int v_retptr)
1.564 + {
1.565 + size_t size = m_Proto.GetRet().size;
1.566 + if (!size)
1.567 + return;
1.568 +
1.569 + // :TODO: assign op support
1.570 + // :TODO: memory return support
1.571 +
1.572 + // Get real ret pointer into ecx
1.573 + // mov ecx, [ebp + v_ret_ptr]
1.574 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
1.575 +
1.576 + if (m_Proto.GetRet().type == PassInfo::PassType_Float)
1.577 + {
1.578 + if (size == 4)
1.579 + IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
1.580 + else if (size == 8)
1.581 + IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
1.582 + }
1.583 + else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
1.584 + {
1.585 + if (size <= 4)
1.586 + {
1.587 + // size <= 4: return in EAX
1.588 + // We align <4 sizes up to 4
1.589 +
1.590 +
1.591 + // mov eax, [ecx]
1.592 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1.593 + }
1.594 + else if (size <= 8)
1.595 + {
1.596 + // size <= 4: return in EAX:EDX
1.597 + // We align 4<x<8 sizes up to 8
1.598 +
1.599 + // mov eax, [ecx]
1.600 + // mov edx, [ecx+4]
1.601 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1.602 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
1.603 + }
1.604 + else
1.605 + {
1.606 + // size >8: return in memory
1.607 + // :TODO:
1.608 + // add flag: MSVC_RetInMemory?
1.609 + }
1.610 + }
1.611 + // :TODO: object
1.612 + }
1.613 +
1.614 + void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
1.615 + int v_pContext, int base_param_offset, int v_plugin_ret)
1.616 + {
1.617 + jitoffs_t counter, tmppos;
1.618 + jitoffs_t counter2, tmppos2;
1.619 +
1.620 + jitoffs_t loop_begin_counter;
1.621 +
1.622 + // prev_res = MRES_IGNORED
1.623 + IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
1.624 +
1.625 + m_HookFunc.start_count(loop_begin_counter);
1.626 +
1.627 + // eax = pContext->GetNext()
1.628 + // ECX = pContext
1.629 + // gcc: push ecx
1.630 + // eax = [ecx]
1.631 + // eax = [eax]
1.632 + // call eax
1.633 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
1.634 +#if SH_COMP == SH_COMP_GCC
1.635 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.636 +#endif
1.637 +
1.638 + // vtbloffs=0, vtblidx=0
1.639 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1.640 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
1.641 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.642 +
1.643 + // quit on zero
1.644 + // test eax, eax
1.645 + // jz exit
1.646 + IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
1.647 + tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
1.648 + m_HookFunc.start_count(counter);
1.649 +
1.650 + // prev_res = MRES_IGNORED
1.651 + IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res);
1.652 +
1.653 + // iter->call()
1.654 + // push params
1.655 + // ecx = eax
1.656 + // gcc: push ecx
1.657 + // eax = [ecx]
1.658 + // eax = [eax+2*SIZE_PTR]
1.659 + // call eax
1.660 + jit_int32_t gcc_clean_bytes = PushParams(base_param_offset);
1.661 +
1.662 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
1.663 +#if SH_COMP == SH_COMP_GCC
1.664 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.665 +#endif
1.666 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1.667 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
1.668 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.669 +
1.670 + SaveRetVal(v_plugin_ret);
1.671 +
1.672 + // cleanup
1.673 +#if SH_COMP == SH_COMP_GCC
1.674 + // params + thisptr
1.675 + IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
1.676 +#endif
1.677 +
1.678 + // process meta return:
1.679 + // prev_res = cur_res
1.680 + // if (cur_res > status) status = cur_res;
1.681 + //
1.682 + // eax = cur_res
1.683 + // edx = status
1.684 + // prev_res = eax
1.685 + // cmp eax,edx
1.686 + // jng thelabel
1.687 + // status = eax
1.688 + // thelabel:
1.689 + //
1.690 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1.691 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
1.692 + IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
1.693 +
1.694 + IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
1.695 + tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
1.696 + m_HookFunc.start_count(counter2);
1.697 +
1.698 + IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
1.699 +
1.700 + m_HookFunc.end_count(counter2);
1.701 + m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
1.702 +
1.703 + // process retval for non-void functions
1.704 + ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
1.705 +
1.706 + // jump back to loop begin
1.707 + tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
1.708 + m_HookFunc.end_count(loop_begin_counter);
1.709 + m_HookFunc.rewrite(tmppos2, -static_cast<jit_int32_t>(loop_begin_counter));
1.710 +
1.711 + m_HookFunc.end_count(counter);
1.712 + m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
1.713 + }
1.714 +
1.715 + short GenContext::GetParamsStackSize()
1.716 + {
1.717 + short acc = 0;
1.718 + for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1.719 + {
1.720 + acc += GetStackSize(m_Proto.GetParam(i));
1.721 + }
1.722 + return acc;
1.723 + }
1.724 +
1.725 + void GenContext::GenerateCallOrig(int v_status, int v_pContext,
1.726 + int param_base_offs, int v_this, int v_vfnptr_origentry, int v_orig_ret, int v_override_ret)
1.727 + {
1.728 + jitoffs_t counter, tmppos;
1.729 + jitoffs_t counter2, tmppos2;
1.730 + jitoffs_t counter3, tmppos3;
1.731 +
1.732 + // if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
1.733 + // *v_orig_ret = orig_call()
1.734 + // else
1.735 + // *v_orig_ret = *v_override_ret
1.736 +
1.737 + // mov eax, status
1.738 + // cmp eax, MRES_SUPERCEDE
1.739 + // je dont_call
1.740 + // call pContext->ShouldCallOrig()
1.741 + // test al, al !! important: al, not eax! bool is only stored in the LSbyte
1.742 + // jz dont_call
1.743 + //
1.744 + // orig_call()
1.745 + // SaveRet(v_orig_ret)
1.746 + // jmp skip_dont_call:
1.747 + //
1.748 + // dont_call:
1.749 + // *v_orig_ret = *v_override_ret
1.750 + // skip_dont_call:
1.751 +
1.752 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1.753 + IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
1.754 +
1.755 + tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
1.756 + m_HookFunc.start_count(counter);
1.757 +
1.758 + // eax = pContext->ShouldCallOrig()
1.759 + // ECX = pContext
1.760 + // gcc: push ecx
1.761 + // eax = [ecx]
1.762 + // eax = [eax + 3*PTR_SIZE]
1.763 + // call eax
1.764 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
1.765 +#if SH_COMP == SH_COMP_GCC
1.766 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.767 +#endif
1.768 +
1.769 + // vtbloffs=0, vtblidx=3
1.770 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
1.771 + IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
1.772 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.773 +
1.774 + IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
1.775 + tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
1.776 + m_HookFunc.start_count(counter2);
1.777 +
1.778 + // push params
1.779 + jit_int32_t gcc_clean_bytes = PushParams(param_base_offs);
1.780 +
1.781 + // thisptr
1.782 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
1.783 +#if SH_COMP == SH_COMP_GCC
1.784 + // on gcc/mingw, this is the first parameter
1.785 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.786 + // on msvc, simply leave it in ecx
1.787 +#endif
1.788 +
1.789 + // call
1.790 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
1.791 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.792 +
1.793 + // cleanup
1.794 +#if SH_COMP == SH_COMP_GCC
1.795 + // params + thisptr
1.796 + IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
1.797 +#endif
1.798 +
1.799 + // save retval
1.800 + SaveRetVal(v_orig_ret);
1.801 +
1.802 + // Skip don't call variant
1.803 + tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
1.804 + m_HookFunc.start_count(counter3);
1.805 +
1.806 +
1.807 + // don't call:
1.808 + m_HookFunc.end_count(counter);
1.809 + m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
1.810 +
1.811 + m_HookFunc.end_count(counter2);
1.812 + m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
1.813 +
1.814 + // *v_orig_ret = *v_override_ret
1.815 + if (m_Proto.GetRet().pAssignOperator)
1.816 + {
1.817 + // lea edx, [ebp + v_override_ret] <-- src addr
1.818 + // lea ecx, [ebp + v_orig_ret] <-- dest addr
1.819 + // gcc: push ecx
1.820 + // push edx <-- src addr
1.821 + // call it
1.822 +
1.823 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
1.824 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1.825 +#if SH_COMP == SH_COMP_GCC
1.826 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.827 +#endif
1.828 + IA32_Push_Reg(&m_HookFunc, REG_EDX);
1.829 +
1.830 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
1.831 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.832 + }
1.833 + else
1.834 + {
1.835 + jit_uint32_t dwords = DownCastSize(m_Proto.GetRet().size) / 4;
1.836 + jit_uint32_t bytes = DownCastSize(m_Proto.GetRet().size) % 4;
1.837 +
1.838 + // bitwise copy
1.839 +
1.840 + //cld
1.841 + //push edi
1.842 + //push esi
1.843 + //lea edi, [ebp+v_orig_ret] <-- destination
1.844 + //lea esi, [ebp+v_override_ret] <-- src
1.845 + //if dwords
1.846 + // mov ecx, <dwords>
1.847 + // rep movsd
1.848 + //if bytes
1.849 + // mov ecx, <bytes>
1.850 + // rep movsb
1.851 + //pop esi
1.852 + //pop edi
1.853 +
1.854 + IA32_Cld(&m_HookFunc);
1.855 + IA32_Push_Reg(&m_HookFunc, REG_EDI);
1.856 + IA32_Push_Reg(&m_HookFunc, REG_ESI);
1.857 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
1.858 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
1.859 + if (dwords)
1.860 + {
1.861 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
1.862 + IA32_Rep(&m_HookFunc);
1.863 + IA32_Movsd(&m_HookFunc);
1.864 + }
1.865 + if (bytes)
1.866 + {
1.867 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
1.868 + IA32_Rep(&m_HookFunc);
1.869 + IA32_Movsb(&m_HookFunc);
1.870 + }
1.871 + IA32_Pop_Reg(&m_HookFunc, REG_ESI);
1.872 + IA32_Pop_Reg(&m_HookFunc, REG_EDI);
1.873 + }
1.874 +
1.875 + // skip don't call label target:
1.876 + m_HookFunc.end_count(counter3);
1.877 + m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
1.878 + }
1.879 +
1.880 + // Sets *v_pContext to return value
1.881 + void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret,
1.882 + int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
1.883 + int v_this, int v_pContext)
1.884 + {
1.885 + // call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
1.886 + // &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
1.887 + // The last two params are null for void funcs.
1.888 +
1.889 + if (m_Proto.GetRet().size == 0)
1.890 + {
1.891 + // void
1.892 + IA32_Push_Imm8(&m_HookFunc, 0); // orig_ret
1.893 + IA32_Push_Imm8(&m_HookFunc, 0); // override_ret
1.894 + }
1.895 + else
1.896 + {
1.897 + // orig_ret and override_ret
1.898 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
1.899 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
1.900 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.901 + IA32_Push_Reg(&m_HookFunc, REG_EDX);
1.902 + }
1.903 +
1.904 + // cur_res and prev_res
1.905 + IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
1.906 + IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
1.907 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.908 + IA32_Push_Reg(&m_HookFunc, REG_EDX);
1.909 +
1.910 + // status and vfnptr_origentry
1.911 + IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
1.912 + IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
1.913 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.914 + IA32_Push_Reg(&m_HookFunc, REG_EDX);
1.915 +
1.916 + // this
1.917 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
1.918 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.919 +
1.920 + // our vfn ptr
1.921 + // *(this + vtbloffs) + SIZE_PTR*vtblidx
1.922 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this); // get this into ecx (gcc!)
1.923 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
1.924 + IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
1.925 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.926 +
1.927 + // *m_pHI
1.928 + IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1.929 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1.930 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.931 +
1.932 + // set up thisptr
1.933 +#if SH_COMP == SH_COMP_GCC
1.934 + // on gcc/mingw, this is the first parameter
1.935 + IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
1.936 +#elif SH_COMP == SH_COMP_MSVC
1.937 + // on msvc, it's ecx
1.938 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1.939 +#endif
1.940 +
1.941 + // call the function. vtbloffs = 0, vtblidx = 19
1.942 + // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1.943 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1.944 + (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
1.945 +
1.946 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.947 +
1.948 + // on gcc/mingw, we have to clean up after the call
1.949 +#if SH_COMP == SH_COMP_GCC
1.950 + // 9 params + hidden thisptr param
1.951 + IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG);
1.952 +#endif
1.953 +
1.954 + // store return value
1.955 + IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
1.956 + }
1.957 +
1.958 + void GenContext::CallEndContext(int v_pContext)
1.959 + {
1.960 + // call endcontext:
1.961 + // shptr->EndContext(pContex)
1.962 + IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
1.963 + IA32_Push_Reg(&m_HookFunc, REG_EAX);
1.964 +
1.965 + // thisptr
1.966 +#if SH_COMP == SH_COMP_GCC
1.967 + // on gcc/mingw, this is the first parameter
1.968 + IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
1.969 +#elif SH_COMP == SH_COMP_MSVC
1.970 + // on msvc, it's ecx
1.971 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
1.972 +#endif
1.973 +
1.974 + // get vtptr into edx -- we know shptr on jit time -> dereference it here!
1.975 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX,
1.976 + (*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
1.977 +
1.978 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.979 +
1.980 + // on gcc/mingw, we have to clean up after the call
1.981 +#if SH_COMP == SH_COMP_GCC
1.982 + // 1 param + hidden thisptr param
1.983 + IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG);
1.984 +#endif
1.985 + }
1.986 +
1.987 + unsigned char * GenContext::GenerateHookFunc()
1.988 + {
1.989 + // prologue
1.990 + IA32_Push_Reg(&m_HookFunc, REG_EBP);
1.991 + IA32_Push_Reg(&m_HookFunc, REG_EBX);
1.992 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
1.993 +
1.994 + // on msvc, save thisptr
1.995 +#if SH_COMP == SH_COMP_MSVC
1.996 + const jit_int8_t v_this = -4;
1.997 + const int addstackoffset = -4;
1.998 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.999 +#elif SH_COMP == SH_COMP_GCC
1.1000 + const jit_int8_t v_this = 8; // first param
1.1001 + const int addstackoffset = 0;
1.1002 +#endif
1.1003 +
1.1004 +
1.1005 + // ********************** stack frame **********************
1.1006 + // MSVC
1.1007 + // second param (gcc: first real param) ebp + 16
1.1008 + // first param (gcc: thisptr) ebp + 12
1.1009 + // ret address: ebp + 8
1.1010 + // caller's ebp ebp + 4
1.1011 + // saved ebx ebp
1.1012 + // MSVC ONLY: current this ebp - 4
1.1013 + // void *vfnptr_origentry ebp - 4 -4
1.1014 + // META_RES status = MRES_IGNORED ebp - 8 -4
1.1015 + // META_RES prev_res ebp - 12 -4
1.1016 + // META_RES cur_res ebp - 16 -4
1.1017 + // IMyDelegate *iter ebp - 20 -4
1.1018 + // IHookContext *pContext ebp - 24 -4
1.1019 + // == 3 ptrs + 3 enums = 24 bytes
1.1020 + //
1.1021 + // non-void: add:
1.1022 + // my_rettype *ret_ptr ebp - 28 -4
1.1023 + // my_rettype orig_ret ebp - 28 - sizeof(my_rettype) -4
1.1024 + // my_rettype override_ret ebp - 28 - sizeof(my_rettype)*2 -4
1.1025 + // my_rettype plugin_ret ebp - 28 - sizeof(my_rettype)*3 -4
1.1026 + // == + 3 * sizeof(my_rettype) bytes
1.1027 +
1.1028 + const jit_int8_t v_vfnptr_origentry = -4 + addstackoffset;
1.1029 + const jit_int8_t v_status = -8 + addstackoffset;
1.1030 + const jit_int8_t v_prev_res = -12 + addstackoffset;
1.1031 + const jit_int8_t v_cur_res = -16 + addstackoffset;
1.1032 + const jit_int8_t v_iter = -20 + addstackoffset;
1.1033 + const jit_int8_t v_pContext = -24 + addstackoffset;
1.1034 +
1.1035 +#if SH_COMP == SH_COMP_GCC
1.1036 + const jit_int32_t param_base_offs = 16;
1.1037 +#elif SH_COMP == SH_COMP_MSVC
1.1038 + const jit_int32_t param_base_offs = 12;
1.1039 +#endif
1.1040 +
1.1041 + jit_int32_t v_ret_ptr = -28 + addstackoffset;
1.1042 + jit_int32_t v_orig_ret = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 1;
1.1043 + jit_int32_t v_override_ret = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 2;
1.1044 + jit_int32_t v_plugin_ret = -28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 3;
1.1045 +
1.1046 + // Hash for temporary storage for byval params with copy constructors
1.1047 + // (param, offset into stack)
1.1048 + short usedStackBytes = 3*SIZE_MWORD + 3*SIZE_PTR + // vfnptr_origentry, status, prev_res, cur_res, iter, pContext
1.1049 + 3 * GetStackSize(m_Proto.GetRet()) + (m_Proto.GetRet().size == 0 ? 0 : SIZE_PTR) // ret_ptr, orig_ret, override_ret, plugin_ret
1.1050 + - addstackoffset; // msvc: current thisptr
1.1051 +
1.1052 + IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, usedStackBytes, MOD_REG);
1.1053 +
1.1054 + // init status localvar
1.1055 + IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
1.1056 +
1.1057 + // Call constructors for ret vars if required
1.1058 + if(m_Proto.GetRet().pNormalCtor)
1.1059 + {
1.1060 + // :TODO: Gcc version
1.1061 +
1.1062 + // orig_reg
1.1063 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
1.1064 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1.1065 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.1066 +
1.1067 + // override_reg
1.1068 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
1.1069 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1.1070 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.1071 +
1.1072 + // plugin_ret
1.1073 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
1.1074 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
1.1075 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.1076 + }
1.1077 +
1.1078 + // ********************** SetupHookLoop **********************
1.1079 + CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
1.1080 + v_this, v_pContext);
1.1081 +
1.1082 + // ********************** call pre hooks **********************
1.1083 + GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1.1084 + v_plugin_ret);
1.1085 +
1.1086 + // ********************** call orig func **********************
1.1087 + GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
1.1088 + v_override_ret);
1.1089 +
1.1090 + // ********************** call post hooks **********************
1.1091 + GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
1.1092 + v_plugin_ret);
1.1093 +
1.1094 + // ********************** end context and return **********************
1.1095 +
1.1096 + PrepareReturn(v_status, v_pContext, v_ret_ptr);
1.1097 +
1.1098 + CallEndContext(v_pContext);
1.1099 +
1.1100 + // Call destructors of byval object params which have a destructor
1.1101 + jit_int32_t cur_param_pos = param_base_offs;
1.1102 + for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1.1103 + {
1.1104 + const IntPassInfo &pi = m_Proto.GetParam(i);
1.1105 + if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
1.1106 + (pi.flags & PassInfo::PassFlag_ByVal))
1.1107 + {
1.1108 + IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
1.1109 + IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
1.1110 + IA32_Call_Reg(&m_HookFunc, REG_EAX);
1.1111 + }
1.1112 + cur_param_pos += GetStackSize(pi);
1.1113 + }
1.1114 +
1.1115 + DoReturn(v_ret_ptr);
1.1116 +
1.1117 + // !! :TODO: Call destructors of orig_ret/ ...
1.1118 +
1.1119 + // epilogue
1.1120 + IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
1.1121 + IA32_Pop_Reg(&m_HookFunc, REG_EBX);
1.1122 + IA32_Pop_Reg(&m_HookFunc, REG_EBP);
1.1123 + IA32_Return_Popstack(&m_HookFunc, GetParamsStackSize());
1.1124 +
1.1125 + // Store pointer for later use
1.1126 + // m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
1.1127 + // into the hookman's vtable
1.1128 + *m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
1.1129 + return m_HookFunc.GetData();
1.1130 + }
1.1131 +
1.1132 + // Pre-condition: GenerateHookFunc() has been called!
1.1133 + unsigned char * GenContext::GeneratePubFunc()
1.1134 + {
1.1135 + jitoffs_t counter, tmppos;
1.1136 +
1.1137 + // The pubfunc is a static cdecl function.
1.1138 + // C Code:
1.1139 + // int HookManPubFunc(
1.1140 + // bool store, ebp + 8
1.1141 + // IHookManagerInfo *hi ebp + 12
1.1142 + // )
1.1143 + // {
1.1144 + // if (store)
1.1145 + // *m_pHI = hi;
1.1146 + // if (hi)
1.1147 + // hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
1.1148 + // }
1.1149 +
1.1150 + // prologue
1.1151 + IA32_Push_Reg(&m_PubFunc, REG_EBP);
1.1152 + IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
1.1153 +
1.1154 +
1.1155 + // save store in eax, hi in ecx
1.1156 + IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
1.1157 + IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
1.1158 +
1.1159 + // Check for store == 0
1.1160 + IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1.1161 + tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1.1162 + m_PubFunc.start_count(counter);
1.1163 +
1.1164 + // nonzero -> store hi
1.1165 + IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
1.1166 + IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1.1167 +
1.1168 + // zero
1.1169 + m_PubFunc.end_count(counter);
1.1170 + m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1.1171 +
1.1172 + // check for hi == 0
1.1173 + IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
1.1174 + tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
1.1175 + m_PubFunc.start_count(counter);
1.1176 +
1.1177 + // nonzero -> call vfunc
1.1178 + // push params in reverse order
1.1179 + IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
1.1180 + IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
1.1181 + IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
1.1182 + IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
1.1183 + IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
1.1184 +
1.1185 + // hi == this is in ecx
1.1186 + // on gcc/mingw, ecx is the first parameter
1.1187 +#if SH_COMP == SH_COMP_GCC
1.1188 + IA32_Push_Reg(&m_PubFunc, REG_ECX);
1.1189 +#endif
1.1190 +
1.1191 + // call the function. vtbloffs = 0, vtblidx = 0
1.1192 + // get vtptr into edx
1.1193 + IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
1.1194 + // get funcptr into eax
1.1195 + IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
1.1196 +
1.1197 + IA32_Call_Reg(&m_PubFunc, REG_EAX);
1.1198 +
1.1199 + // on gcc/mingw, we have to clean up after the call
1.1200 +#if SH_COMP == SH_COMP_GCC
1.1201 + // 5 params + hidden thisptr param
1.1202 + IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
1.1203 +#endif
1.1204 +
1.1205 + // zero
1.1206 + m_PubFunc.end_count(counter);
1.1207 + m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
1.1208 +
1.1209 + // return value
1.1210 + IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
1.1211 +
1.1212 + // epilogue
1.1213 + IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
1.1214 + IA32_Pop_Reg(&m_PubFunc, REG_EBP);
1.1215 + IA32_Return(&m_PubFunc);
1.1216 +
1.1217 + SetMemAccess(reinterpret_cast<void*>(m_PubFunc.GetData()), m_PubFunc.GetSize(),
1.1218 + SH_MEM_READ | SH_MEM_EXEC);
1.1219 +
1.1220 + return m_PubFunc;
1.1221 + }
1.1222 +
1.1223 + bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
1.1224 + {
1.1225 + if (pi.type != PassInfo::PassType_Basic &&
1.1226 + pi.type != PassInfo::PassType_Float &&
1.1227 + pi.type != PassInfo::PassType_Object)
1.1228 + {
1.1229 + return false;
1.1230 + }
1.1231 +
1.1232 + if (pi.type == PassInfo::PassType_Object &&
1.1233 + (pi.flags & PassInfo::PassFlag_ByVal))
1.1234 + {
1.1235 + if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pNormalCtor)
1.1236 + return false;
1.1237 +
1.1238 + if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
1.1239 + return false;
1.1240 +
1.1241 + // only care for assignop and normalctor for return types
1.1242 + if (is_ret && (pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
1.1243 + return false;
1.1244 +
1.1245 + if (is_ret && (pi.flags & PassInfo::PassFlag_CCtor) && !pi.pNormalCtor)
1.1246 + return false;
1.1247 + }
1.1248 +
1.1249 + if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
1.1250 + {
1.1251 + return false; // Neither byval nor byref!
1.1252 + }
1.1253 + return true;
1.1254 + }
1.1255 +
1.1256 + HookManagerPubFunc GenContext::Generate()
1.1257 + {
1.1258 + Clear();
1.1259 +
1.1260 + // Check conditions:
1.1261 + // -1) good proto version
1.1262 + // 0) we don't support unknown passtypes, convention, ...
1.1263 + // 1) we don't support functions which return objects by value or take parameters by value
1.1264 + // that have a constructor, a destructor or an overloaded assignment op
1.1265 + // (we wouldn't know how to call it!)
1.1266 +
1.1267 + if (m_Proto.GetVersion() < 1)
1.1268 + {
1.1269 + return NULL;
1.1270 + }
1.1271 +
1.1272 + if (m_Proto.GetConvention() != ProtoInfo::CallConv_Cdecl &&
1.1273 + m_Proto.GetConvention() != ProtoInfo::CallConv_ThisCall)
1.1274 + {
1.1275 + return NULL;
1.1276 + }
1.1277 +
1.1278 +
1.1279 + if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
1.1280 + {
1.1281 + return NULL;
1.1282 + }
1.1283 +
1.1284 + for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
1.1285 + {
1.1286 + if (!PassInfoSupported(m_Proto.GetParam(i), false))
1.1287 + return NULL;
1.1288 + }
1.1289 +
1.1290 + BuildProtoInfo();
1.1291 + GenerateHookFunc();
1.1292 + return reinterpret_cast<HookManagerPubFunc>(GeneratePubFunc());
1.1293 + }
1.1294 + }
1.1295 +}
1.1296 \ No newline at end of file