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