151c0b2f7Stbbdev /*
2*c21e688aSSergey Zheltov     Copyright (c) 2005-2022 Intel Corporation
351c0b2f7Stbbdev 
451c0b2f7Stbbdev     Licensed under the Apache License, Version 2.0 (the "License");
551c0b2f7Stbbdev     you may not use this file except in compliance with the License.
651c0b2f7Stbbdev     You may obtain a copy of the License at
751c0b2f7Stbbdev 
851c0b2f7Stbbdev         http://www.apache.org/licenses/LICENSE-2.0
951c0b2f7Stbbdev 
1051c0b2f7Stbbdev     Unless required by applicable law or agreed to in writing, software
1151c0b2f7Stbbdev     distributed under the License is distributed on an "AS IS" BASIS,
1251c0b2f7Stbbdev     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1351c0b2f7Stbbdev     See the License for the specific language governing permissions and
1451c0b2f7Stbbdev     limitations under the License.
1551c0b2f7Stbbdev */
1651c0b2f7Stbbdev 
1751c0b2f7Stbbdev #ifndef __TBB_function_replacement_H
1851c0b2f7Stbbdev #define __TBB_function_replacement_H
1951c0b2f7Stbbdev 
2051c0b2f7Stbbdev #include <stddef.h> //for ptrdiff_t
2151c0b2f7Stbbdev typedef enum {
2251c0b2f7Stbbdev     FRR_OK,     /* Succeeded in replacing the function */
2351c0b2f7Stbbdev     FRR_NODLL,  /* The requested DLL was not found */
2451c0b2f7Stbbdev     FRR_NOFUNC, /* The requested function was not found */
2551c0b2f7Stbbdev     FRR_FAILED, /* The function replacement request failed */
2651c0b2f7Stbbdev } FRR_TYPE;
2751c0b2f7Stbbdev 
2851c0b2f7Stbbdev typedef enum {
2951c0b2f7Stbbdev     FRR_FAIL,     /* Required function */
3051c0b2f7Stbbdev     FRR_IGNORE,   /* optional function */
3151c0b2f7Stbbdev } FRR_ON_ERROR;
3251c0b2f7Stbbdev 
3351c0b2f7Stbbdev typedef void (*FUNCPTR)();
3451c0b2f7Stbbdev 
3551c0b2f7Stbbdev #ifndef UNICODE
3651c0b2f7Stbbdev #define ReplaceFunction ReplaceFunctionA
3751c0b2f7Stbbdev #else
3851c0b2f7Stbbdev #define ReplaceFunction ReplaceFunctionW
3951c0b2f7Stbbdev #endif //UNICODE
4051c0b2f7Stbbdev 
4157f524caSIlya Isaev FRR_TYPE ReplaceFunctionA(const char *dllName, const char *funcName, FUNCPTR newFunc, const char ** opcodes, FUNCPTR* origFunc=nullptr);
4257f524caSIlya Isaev FRR_TYPE ReplaceFunctionW(const wchar_t *dllName, const char *funcName, FUNCPTR newFunc, const char ** opcodes, FUNCPTR* origFunc=nullptr);
4351c0b2f7Stbbdev 
4451c0b2f7Stbbdev bool IsPrologueKnown(const char* dllName, const char *funcName, const char **opcodes, HMODULE module);
4551c0b2f7Stbbdev 
4651c0b2f7Stbbdev // Utilities to convert between ADDRESS and LPVOID
4751c0b2f7Stbbdev union Int2Ptr {
4851c0b2f7Stbbdev     UINT_PTR uip;
4951c0b2f7Stbbdev     LPVOID lpv;
5051c0b2f7Stbbdev };
5151c0b2f7Stbbdev 
5251c0b2f7Stbbdev inline UINT_PTR Ptr2Addrint(LPVOID ptr);
5351c0b2f7Stbbdev inline LPVOID Addrint2Ptr(UINT_PTR ptr);
5451c0b2f7Stbbdev 
5551c0b2f7Stbbdev // The size of a trampoline region
5651c0b2f7Stbbdev const unsigned MAX_PROBE_SIZE = 32;
5751c0b2f7Stbbdev 
5851c0b2f7Stbbdev // The size of a jump relative instruction "e9 00 00 00 00"
5951c0b2f7Stbbdev const unsigned SIZE_OF_RELJUMP = 5;
6051c0b2f7Stbbdev 
6151c0b2f7Stbbdev // The size of jump RIP relative indirect "ff 25 00 00 00 00"
6251c0b2f7Stbbdev const unsigned SIZE_OF_INDJUMP = 6;
6351c0b2f7Stbbdev 
6451c0b2f7Stbbdev // The size of address we put in the location (in Intel64)
6551c0b2f7Stbbdev const unsigned SIZE_OF_ADDRESS = 8;
6651c0b2f7Stbbdev 
6751c0b2f7Stbbdev // The size limit (in bytes) for an opcode pattern to fit into a trampoline
6851c0b2f7Stbbdev // There should be enough space left for a relative jump; +1 is for the extra pattern byte.
6951c0b2f7Stbbdev const unsigned MAX_PATTERN_SIZE = MAX_PROBE_SIZE - SIZE_OF_RELJUMP + 1;
7051c0b2f7Stbbdev 
7151c0b2f7Stbbdev // The max distance covered in 32 bits: 2^31 - 1 - C
7251c0b2f7Stbbdev // where C should not be smaller than the size of a probe.
7351c0b2f7Stbbdev // The latter is important to correctly handle "backward" jumps.
7451c0b2f7Stbbdev const __int64 MAX_DISTANCE = (((__int64)1 << 31) - 1) - MAX_PROBE_SIZE;
7551c0b2f7Stbbdev 
7651c0b2f7Stbbdev // The maximum number of distinct buffers in memory
7751c0b2f7Stbbdev const ptrdiff_t MAX_NUM_BUFFERS = 256;
7851c0b2f7Stbbdev 
7951c0b2f7Stbbdev #endif //__TBB_function_replacement_H
80