1 /* 2 * This file is part of the openHiTLS project. 3 * 4 * openHiTLS is licensed under the Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * See the Mulan PSL v2 for more details. 14 */ 15 16 #ifndef STUB_REPALCE_H 17 #define STUB_REPALCE_H 18 19 #include <stdint.h> 20 21 22 #if defined(__x86_64__) 23 /* 24 * The first 14 bytes of the function entry are used to construct the jump instruction. 25 * Short jump instruction is 5 bytes, and Long jump instruction is 14 bytes. 26 */ 27 #define CODESIZE 14U 28 #elif defined(__aarch64__) || defined(_M_ARM64) 29 /* ARM64 needs 16 bytes to construct the jump instruction. */ 30 #define CODESIZE 16U 31 #elif defined(__arm__) 32 /* ARM32 needs 12 bytes to construct the jump instruction. */ 33 #define CODESIZE 12U 34 #endif 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 typedef struct { 41 void *fn; 42 unsigned char codeBuf[CODESIZE]; 43 } FuncStubInfo; 44 45 /* 46 * Initialize the dynamic stub change function. Invoke the function once. 47 * return - 0:Success, non-zero:Error code 48 */ 49 int STUB_Init(void); 50 51 /* 52 * Replaces the specified function with the specified stub function. 53 * stubInfo - Record information about stub replacement, which is used for STUB_Reset restoration. 54 * srcFn - Functions in the source code 55 * stubFn - Need to replace the stub function that is inserted into the run. 56 * return - 0:Success, non-zero:Error code 57 */ 58 int STUB_Replace(FuncStubInfo *stubInfo, void *srcFn, const void *stubFn); 59 60 /* 61 * Restore the source function and remove the instrumentation. 62 * stubInfo - Information logged when instrumentation 63 * return - 0:Success, non-zero:Error code 64 */ 65 int STUB_Reset(FuncStubInfo *stubInfo); 66 67 #ifdef __cplusplus 68 } 69 #endif 70 71 #endif // STUB_REPALCE_H