1*51c0b2f7Stbbdev /* 2*51c0b2f7Stbbdev Copyright (c) 2005-2020 Intel Corporation 3*51c0b2f7Stbbdev 4*51c0b2f7Stbbdev Licensed under the Apache License, Version 2.0 (the "License"); 5*51c0b2f7Stbbdev you may not use this file except in compliance with the License. 6*51c0b2f7Stbbdev You may obtain a copy of the License at 7*51c0b2f7Stbbdev 8*51c0b2f7Stbbdev http://www.apache.org/licenses/LICENSE-2.0 9*51c0b2f7Stbbdev 10*51c0b2f7Stbbdev Unless required by applicable law or agreed to in writing, software 11*51c0b2f7Stbbdev distributed under the License is distributed on an "AS IS" BASIS, 12*51c0b2f7Stbbdev WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*51c0b2f7Stbbdev See the License for the specific language governing permissions and 14*51c0b2f7Stbbdev limitations under the License. 15*51c0b2f7Stbbdev */ 16*51c0b2f7Stbbdev 17*51c0b2f7Stbbdev #ifndef __TBB_function_replacement_H 18*51c0b2f7Stbbdev #define __TBB_function_replacement_H 19*51c0b2f7Stbbdev 20*51c0b2f7Stbbdev #include <stddef.h> //for ptrdiff_t 21*51c0b2f7Stbbdev typedef enum { 22*51c0b2f7Stbbdev FRR_OK, /* Succeeded in replacing the function */ 23*51c0b2f7Stbbdev FRR_NODLL, /* The requested DLL was not found */ 24*51c0b2f7Stbbdev FRR_NOFUNC, /* The requested function was not found */ 25*51c0b2f7Stbbdev FRR_FAILED, /* The function replacement request failed */ 26*51c0b2f7Stbbdev } FRR_TYPE; 27*51c0b2f7Stbbdev 28*51c0b2f7Stbbdev typedef enum { 29*51c0b2f7Stbbdev FRR_FAIL, /* Required function */ 30*51c0b2f7Stbbdev FRR_IGNORE, /* optional function */ 31*51c0b2f7Stbbdev } FRR_ON_ERROR; 32*51c0b2f7Stbbdev 33*51c0b2f7Stbbdev typedef void (*FUNCPTR)(); 34*51c0b2f7Stbbdev 35*51c0b2f7Stbbdev #ifndef UNICODE 36*51c0b2f7Stbbdev #define ReplaceFunction ReplaceFunctionA 37*51c0b2f7Stbbdev #else 38*51c0b2f7Stbbdev #define ReplaceFunction ReplaceFunctionW 39*51c0b2f7Stbbdev #endif //UNICODE 40*51c0b2f7Stbbdev 41*51c0b2f7Stbbdev FRR_TYPE ReplaceFunctionA(const char *dllName, const char *funcName, FUNCPTR newFunc, const char ** opcodes, FUNCPTR* origFunc=NULL); 42*51c0b2f7Stbbdev FRR_TYPE ReplaceFunctionW(const wchar_t *dllName, const char *funcName, FUNCPTR newFunc, const char ** opcodes, FUNCPTR* origFunc=NULL); 43*51c0b2f7Stbbdev 44*51c0b2f7Stbbdev bool IsPrologueKnown(const char* dllName, const char *funcName, const char **opcodes, HMODULE module); 45*51c0b2f7Stbbdev 46*51c0b2f7Stbbdev // Utilities to convert between ADDRESS and LPVOID 47*51c0b2f7Stbbdev union Int2Ptr { 48*51c0b2f7Stbbdev UINT_PTR uip; 49*51c0b2f7Stbbdev LPVOID lpv; 50*51c0b2f7Stbbdev }; 51*51c0b2f7Stbbdev 52*51c0b2f7Stbbdev inline UINT_PTR Ptr2Addrint(LPVOID ptr); 53*51c0b2f7Stbbdev inline LPVOID Addrint2Ptr(UINT_PTR ptr); 54*51c0b2f7Stbbdev 55*51c0b2f7Stbbdev // The size of a trampoline region 56*51c0b2f7Stbbdev const unsigned MAX_PROBE_SIZE = 32; 57*51c0b2f7Stbbdev 58*51c0b2f7Stbbdev // The size of a jump relative instruction "e9 00 00 00 00" 59*51c0b2f7Stbbdev const unsigned SIZE_OF_RELJUMP = 5; 60*51c0b2f7Stbbdev 61*51c0b2f7Stbbdev // The size of jump RIP relative indirect "ff 25 00 00 00 00" 62*51c0b2f7Stbbdev const unsigned SIZE_OF_INDJUMP = 6; 63*51c0b2f7Stbbdev 64*51c0b2f7Stbbdev // The size of address we put in the location (in Intel64) 65*51c0b2f7Stbbdev const unsigned SIZE_OF_ADDRESS = 8; 66*51c0b2f7Stbbdev 67*51c0b2f7Stbbdev // The size limit (in bytes) for an opcode pattern to fit into a trampoline 68*51c0b2f7Stbbdev // There should be enough space left for a relative jump; +1 is for the extra pattern byte. 69*51c0b2f7Stbbdev const unsigned MAX_PATTERN_SIZE = MAX_PROBE_SIZE - SIZE_OF_RELJUMP + 1; 70*51c0b2f7Stbbdev 71*51c0b2f7Stbbdev // The max distance covered in 32 bits: 2^31 - 1 - C 72*51c0b2f7Stbbdev // where C should not be smaller than the size of a probe. 73*51c0b2f7Stbbdev // The latter is important to correctly handle "backward" jumps. 74*51c0b2f7Stbbdev const __int64 MAX_DISTANCE = (((__int64)1 << 31) - 1) - MAX_PROBE_SIZE; 75*51c0b2f7Stbbdev 76*51c0b2f7Stbbdev // The maximum number of distinct buffers in memory 77*51c0b2f7Stbbdev const ptrdiff_t MAX_NUM_BUFFERS = 256; 78*51c0b2f7Stbbdev 79*51c0b2f7Stbbdev #endif //__TBB_function_replacement_H 80