Function redirection method and device and electronic equipment

By automatically generating function redirection code, the problems of low coding efficiency and high maintenance difficulty when multiple functions are redirected are solved, achieving efficient function redirection and simplified code management.

CN121364902APending Publication Date: 2026-01-20HANGZHOU CHANGCHUAN TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511416255.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-29
Publication Date
2026-01-20

AI Technical Summary

Technical Problem

In existing technologies, redirecting multiple functions requires writing multiple redirection logic codes, resulting in low coding efficiency, high maintenance difficulty, poor scalability, and complex version control.

Method used

By reading and parsing configuration information, the function pointer of the target function and the parameter information of the redirection function are generated, the redirection code is automatically generated, and the Detours library is used to perform function redirection, thereby achieving automatic binding and unbinding of the target function.

Benefits of technology

It improves the coding efficiency of function redirection, simplifies code maintenance, and enhances scalability and version control convenience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121364902A_ABST
    Figure CN121364902A_ABST
Patent Text Reader

Abstract

The invention discloses a function redirection method and device and electronic equipment, configuration information is read and analyzed, and the configuration information comprises function information of a target function needing to be redirected; on the basis of the function information, obtained through analysis, of the target function, a function pointer of the target function is generated, parameter information corresponding to calling and executing a redirection function is called and executed, the function pointer of the target function points to a function address of the target function, and the redirection function is a function actually executed after redirection; generating a redirection code based on the function pointer of the target function and the parameter information corresponding to the redirection function; and executing the redirection codes to redirect the target functions, so that the redirection codes corresponding to the target functions can be respectively generated under the condition that the functions needing to be redirected comprise a plurality of target functions, repeated codes do not need to be manually written for multiple times, the coding efficiency can be improved, and the function redirection efficiency can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the technical field of computer, and particularly, to a function redirection method and device and electronic equipment. BACKGROUND

[0002] The function redirection can redirect an original function to a self-defined function, so as to replace the execution of the original function with the execution of the self-defined function.

[0003] In the related art, when a function is redirected, a redirection logic code can be written for the function. However, if multiple functions need to be redirected, multiple corresponding redirection logic codes need to be written, and repeated codes need to be written multiple times, which is low in efficiency, and in the case that the number of functions that need to be redirected increases, the difficulty of code maintenance also increases, and the code maintainability is poor. SUMMARY

[0004] Embodiments of the present disclosure provide a function redirection method, device and electronic equipment to solve the above technical problems to some extent.

[0005] In one aspect of the embodiments of the present disclosure, a function redirection method is provided, comprising:

[0006] reading and parsing configuration information, wherein the configuration information includes function information of a target function that needs to be redirected;

[0007] generating a function pointer of the target function and parameter information corresponding to a redirection function based on the function information of the target function obtained by parsing, wherein the function pointer of the target function points to a function address of the target function, and the redirection function is a function actually executed after redirection;

[0008] generating a redirection code based on the function pointer of the target function and the parameter information corresponding to the redirection function;

[0009] executing the redirection code to redirect the target function.

[0010] In another aspect of the embodiments of the present disclosure, a function redirection device is provided, comprising:

[0011] a process starting module configured to start a redirection process, wherein the redirection process is configured to control process resources of a target process, and the target process is a process configured to execute a target function that needs to be redirected;

[0012] a dynamic library linking module configured to control the target process to link a redirection dynamic library based on the redirection process, wherein the redirection dynamic library includes a redirection function, and the redirection function is a function executed by the target process after redirection;

[0013] The function redirection module is configured to determine a function address of the target function through the linked redirection dynamic library, and bind the function address with the redirection function to redirect the target function to the redirection function.

[0014] In another aspect of the embodiments of the present disclosure, an electronic device is provided, comprising:

[0015] a memory configured to store a computer program;

[0016] a processor configured to execute the computer program stored in the memory, and when the computer program is executed, implement the function redirection method in any of the above embodiments.

[0017] In another aspect of the embodiments of the present disclosure, a computer readable storage medium is provided, which stores a computer program, and when the computer program is executed by a processor, implement the function redirection method in any of the above embodiments.

[0018] In another aspect of the embodiments of the present disclosure, a computer program product is provided, which comprises a computer program, and when the computer program is executed by a processor, implement the function redirection method in any of the above embodiments.

[0019] The technical solutions of the present disclosure are described in further detail below with reference to the accompanying drawings and embodiments.

[0020] In the embodiments of the present disclosure, the configuration information read is parsed to obtain function information of a target function that needs to be redirected, so that a function pointer of the target function and parameter information corresponding to the actual execution function after redirection are generated according to the function information of the target function that needs to be redirected, so that the redirection code can be automatically generated based on the function pointer of the target function and the parameter information corresponding to the redirection function, and the redirection of the target function can be performed by executing the redirection code. Therefore, for the target function that needs to be redirected, the redirection code for redirecting the target function can be automatically generated, and in the case where the function that needs to be redirected includes multiple target functions, the redirection code corresponding to each target function can be generated respectively, without manually writing repeated codes multiple times, which can improve the coding efficiency and further help to improve the efficiency of function redirection. Moreover, the automatically generated code can be easily managed and maintained, and the maintainability of the code is improved.

[0021] The technical solutions of the present disclosure are described in further detail below with reference to the accompanying drawings and embodiments. BRIEF DESCRIPTION OF DRAWINGS

[0022] The accompanying drawings, which form a part of the specification, illustrate embodiments of the present disclosure and, together with the description, serve to explain the principles of the present disclosure.

[0023] The present disclosure can be more clearly understood and appreciated from the following detailed description taken in conjunction with the accompanying drawings, in which:

[0024] Figure 1 is a flowchart of a function redirection method provided by an exemplary embodiment of the present disclosure;

[0025] Figure 2 is a flowchart of a function pointer and parameter list generation process provided by an exemplary embodiment of the present disclosure;

[0026] Figure 3 is a flowchart of a redirection code generation process provided by an exemplary embodiment of the present disclosure;

[0027] Figure 4 is a flowchart of a de-redirection code generation process provided by an exemplary embodiment of the present disclosure;

[0028] Figure 5 is a flowchart of a structure body traversal process provided by an exemplary embodiment of the present disclosure;

[0029] Figure 6 is a flowchart of a redirection code generation process provided by an exemplary embodiment of the present disclosure;

[0030] Figure 7 is a structural diagram of a function redirection apparatus provided by an exemplary embodiment of the present disclosure;

[0031] Figure 8 is a structural diagram of an electronic device provided by an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION

[0032] Various exemplary embodiments of the present disclosure will now be described in detail with reference to the accompanying drawings. It should be noted that the relative arrangement of the components and steps set forth in these embodiments, numerical expressions, and numerical values do not limit the scope of the present disclosure unless otherwise specifically stated.

[0033] Those skilled in the art can understand that the terms "first", "second", and the like in the embodiments of the present disclosure are only used to distinguish different steps, devices, or modules, and do not represent any specific technical meaning, nor do they represent a necessary logical sequence between them.

[0034] It should also be understood that in the embodiments of the present disclosure, "a plurality of" can mean two or more, and "at least one" can mean one, two, or more.

[0035] It should also be understood that for any component, data, or structure mentioned in the embodiments of the present disclosure, unless specifically limited or unless the context clearly indicates otherwise, it can generally be understood as one or more.

[0036] In addition, the term "and / or" in the present disclosure is merely a description of the association relationship of the associated objects, which means that there can be three relationships, for example, A and / or B can represent the following three cases: A exists alone, A and B exist together, and B exists alone. In addition, the character " / " in the present disclosure generally represents an "or" relationship between the front and rear associated objects.

[0037] It should also be understood that the description of the various embodiments of the present disclosure focuses on the differences between the various embodiments, and the same or similar parts can be referred to each other, and for the sake of brevity, will not be repeated.

[0038] At the same time, it should be understood that, for the convenience of description, the size of each part shown in the drawings is not drawn according to the actual proportional relationship.

[0039] The following description of at least one example embodiment is merely illustrative in nature and is in no way intended to limit the present disclosure, its application, or uses.

[0040] Techniques, methods, and devices known to those of ordinary skill in the relevant art can not be discussed in detail, but should be considered part of the specification where appropriate.

[0041] It should be noted that similar reference numbers and letters refer to similar items in the following drawings, and therefore, once an item is defined in one drawing, it does not need to be further discussed in subsequent drawings.

[0042] The embodiments of the present disclosure can be applied to terminal devices, computer systems, servers and other electronic devices, which can operate with many other general or special computing system environments or configurations. Examples of well-known terminal devices, computing systems, environments, and / or configurations suitable for use with terminal devices, computer systems, servers and other electronic devices include, but are not limited to, personal computer systems, server computer systems, thin clients, thick clients, handheld or laptop devices, microprocessor-based systems, set-top boxes, programmable consumer electronics, network personal computers, small computer systems, mainframe computer systems, and distributed cloud computing technology environments including any of the above systems.

[0043] Electronic devices such as terminal devices, computer systems, servers, and the like can be described in the general context of computer system-executable instructions, such as program modules, being executed by a computer system. Generally, program modules can include routines, programs, objects, components, logic, data structures, and the like, that perform particular tasks or implement particular abstract data types. Computer systems / server can be practiced in distributed cloud computing environments with other computer systems coupled via communication networks. The program modules can be stored in the local or remote computer system memory devices.

[0044] Function redirection refers to a process of redirecting a target function to be executed to another function, so as to actually execute another function when the target function is executed. Through function redirection, the target function can be redirected to a custom function, and the custom function is executed.

[0045] In the related art, redirection logic is written separately for each function that needs to be redirected. This approach has the following disadvantages: (1) Code redundancy: similar redirection code and logic need to be written for each target function (function that needs to be redirected), resulting in a large amount of repeated code, which is not conducive to improving coding efficiency. (2) Poor maintainability: as the number of target functions increases, it becomes more difficult to maintain repeated code, and modifying or adding new redirection logic requires attention to updating the implementation of the function. (3) Limited scalability: it is difficult to dynamically add or remove redirection functions under this approach, and each time a function needs to be added or removed, the code needs to be redeployed. (4) Complex version control: as the number of redirection functions increases, version control becomes more complex. Each time a redirection function is modified or added, multiple version files need to be updated.

[0046] In the embodiments of the present disclosure, a function redirection method is provided, which can automatically generate redirection code for redirecting a target function, improve coding efficiency, and thus help improve function redirection efficiency.

[0047] As shown in Figure 1 The method can be used in the electronic device described above, and the method includes steps 110-140:

[0048] In step 110, configuration information is read and parsed, and the configuration information includes function information of a target function that needs to be redirected.

[0049] In the embodiments of the present disclosure, the target function refers to a function to be redirected (also referred to as an original function), and function information of the target function to be redirected is maintained in configuration information, and the function information is updated and managed based on the configuration information. Optionally, the configuration information can be stored in a configuration file preset. For the target function to be redirected, a user can pre-configure the target function, and store the function information of the target function into the configuration file.

[0050] Optionally, the function information of the target function includes information required for implementing redirection of the target function, which can include at least one of a function identifier of the target function, a function signature, and a name of a dynamic link library (DLL) where the target function is located.

[0051] In a possible implementation, the configuration information can be read from the configuration file, and the configuration information is parsed to obtain the function information of the target function. Optionally, the configuration information included in the configuration file can be in an extensible markup language (XML) format. Optionally, the TinyXML2 library can be used to parse the configuration information in the XML format to obtain the function information of the target function, where the TinyXML2 library is a lightweight and efficient C++ library, and the function information can be quickly and efficiently parsed.

[0052] In step 120, a function pointer of the target function and parameter information corresponding to calling an execution redirection function are generated based on the parsed function information of the target function.

[0053] The redirection function is a function actually executed after redirection, and can also be referred to as a Hook function.

[0054] In a possible implementation, the Detours library can be used for redirection, and the Detours library is used for intercepting and redirecting a function call, and can implement redirection of the target function. When the Detours library is used for redirection, a function pointer needs to be generated, where the function pointer points to a function address of the target function. In addition, parameter information corresponding to calling an execution redirection function also needs to be generated, where the parameter information corresponding to calling the execution redirection function needs to correspond to parameter information of the target function, for example, a parameter list.

[0055] In a possible implementation, based on the function information of the target function, a function address of the target function can be determined to generate a function pointer for pointing to the function address of the target function. Based on the function information of the target function, parameter information for calling an execution of the target function is determined to determine parameter information corresponding to calling an execution of the redirection function.

[0056] In step 130, the redirection code is generated based on the function pointer of the target function and the parameter information corresponding to the redirection function.

[0057] After the function pointer of the target function and the parameter information corresponding to the redirection function are generated, the redirection code can be automatically generated.

[0058] In a possible implementation, the Detours library is used for redirection. When the Detours library is used for redirection, the function pointer of the target function can be bound to the redirection function to realize the redirection of the target function.

[0059] In a possible implementation, the binding code for binding the function pointer of the target function and the redirection function can be generated based on the function pointer of the target function and the parameter information corresponding to the redirection function, and the redirection code can be generated based on the binding code.

[0060] In a possible implementation, when there are multiple target functions (i.e., there are multiple functions to be redirected), the function information of the multiple target functions can be respectively parsed from the configuration information, the function pointer corresponding to each target function and the parameter information corresponding to the redirection function can be respectively generated based on the function information of the multiple target functions, the binding code corresponding to each target function can be automatically generated based on the function pointer corresponding to each target function and the parameter information corresponding to the redirection function, and the redirection code for redirecting each target function can be determined based on the binding code corresponding to each target function. Thus, the repetitive similar codes can be automatically written without manual operation, the coding efficiency can be improved, and the function redirection efficiency can be improved.

[0061] In step 140, the redirection code is executed to redirect the target function.

[0062] After the redirection code is generated, the target function can be redirected by executing the redirection code.

[0063] In this embodiment, the read configuration information is parsed to obtain the function information of the target function to be redirected. Based on this information, a function pointer to the target function and the parameter information corresponding to the actual function being called after redirection are generated. Thus, based on the function pointer of the target function and the parameter information of the redirected function, redirection code can be automatically generated. Executing this redirection code allows the redirection of the target function. Therefore, for a target function to be redirected, redirection code can be automatically generated. When the function to be redirected includes multiple target functions, redirection code for each target function can be generated separately, eliminating the need for manual rewriting of repetitive code, improving coding efficiency, and consequently, improving the efficiency of function redirection. Furthermore, the automatically generated code is easier to manage and maintain, improving code maintainability.

[0064] In one possible implementation, the library information of the redirected dynamic library corresponding to the target function and the function signature of the target function can be obtained by reading and parsing the configuration information.

[0065] Among them, the redirected dynamic library is the DLL library. The library information of the DLL library can include the DLL name and the base address of the DLL. The function address of the target function can be determined based on the library information of the DLL library corresponding to the target function.

[0066] The function signature of the target function refers to the information contained in the definition of the target function, used to uniquely identify the function. Optionally, the function signature of the target function includes the function name, parameter types, return type, and other relevant information. The function name is the name of the target function, used to call the function; the parameter types indicate the parameters that the target function can accept and their types; and the return type is the type corresponding to the function's return value.

[0067] In one possible implementation, a preset command can be used to export the symbol table of the DLL containing the target function, and then the function signature of the target function can be looked up from the symbol table. In one example, the preset command could be the nm-C command.

[0068] In one possible implementation, such as Figure 2 As shown, in Figure 1 Based on the example, the process of generating the function pointer of the target function and the parameter information corresponding to the call execution redirection function based on the function information of the target function obtained by parsing includes the following steps:

[0069] Step 210: Based on the library information and function signature, determine the function address and parameter list of the target function.

[0070] In a possible implementation, the function address of the target function can be determined based on the library information. Optionally, if the target function is an exported function, the function address of the target function can be obtained from the export table of the DLL library corresponding to the target function. Optionally, if the target function is a non-exported function, the function address of the target function can be calculated based on the base address of the DLL library and the function offset of the target function. The function offset of the target function can be determined based on the preferred load address of the DLL where the target function is located and the virtual address of the target function, and the preferred load address of the target DLL and the virtual address of the target function can be obtained from the export file corresponding to the DLL where the target function is located, where the export file can be exported by the nm-C command. In an example, the function offset can be the difference between the virtual address and the preferred load address. The sum of the function offset and the base address is the function address of the target function.

[0071] In a possible implementation, the parameter list of the target function can be determined based on the function signature of the target function. Specifically, the parameter list of the target function can be determined based on the parameter types included in the function signature, and the parameter list includes the parameters and their types that can be received by the target function.

[0072] In step 220, the function pointer of the target function is generated based on the function address and the parameter list.

[0073] In a possible implementation, the function pointer of the target function can be generated based on the function address and the parameter list. Specifically, the declaration of the function pointer can be constructed based on the parameter list, and the function address of the target function is assigned to the function pointer to generate the function pointer corresponding to the target function.

[0074] In a possible implementation, when the declaration of the function pointer is constructed based on the parameter list, if the target function is a class member function, one more pointer type variable is needed in the parameter list to store the this pointer of the class member function, that is, one more pointer type variable is needed in the parameter list in addition to the parameters and their types indicated by the function signature.

[0075] In step 230, the parameter list of the redirection function is generated based on the parameter list of the target function, and the parameter list of the redirection function matches the parameter list of the target function.

[0076] Optionally, the parameter list of the redirection function can be defined based on the parameter list of the target parameter.

[0077] In a possible implementation, the parameter list corresponding to the redirection function can be determined based on the parameter type and the parameter variable number indicated in the parameter list of the target function, that is, the parameter type and the parameter variable number included in the parameter list of the redirection function correspond to the parameter type and the parameter variable number indicated in the parameter list of the target function.

[0078] In another possible implementation, the parameter list corresponding to the redirection function can be generated based on the parameter variable number indicated in the parameter list of the target function, where the parameter variable number included in the parameter list corresponding to the redirection function is not less than the parameter variable number indicated in the parameter list of the target function. That is, the parameter type of the redirection function does not need to refer to the parameter type of the target function, but only needs to align the parameter variable number. Optionally, in the case of only aligning the parameter variable number, the parameter type of the redirection function can be set as a void pointer (void*).

[0079] In a possible implementation, the target function can be a class member function, and in the case that the target function is a class member function, the parameter variable number of the redirection function needs to be greater than the parameter variable number corresponding to the target function, for storing the this pointer of the class member function. In an example, if the target function is a class member function, the parameter variable number of the redirection function is one more than the parameter variable number corresponding to the target function. For example, the target function is a class member function, and the parameter list of the target function only includes one variable of a string type, and the parameter list corresponding to the redirection function can be (void*, void*), where one void pointer is used to store the variable of the string type, and the other can be used to store the this pointer of the target function.

[0080] Optionally, the function signature of the target function includes function information used to indicate whether the target function is a class member function. In a possible implementation, whether the target function is a class member function can be determined based on the function signature of the target function in the export file.

[0081] In this embodiment, based on the function information of the target function, the function pointer used to point to the function address of the target function and the parameter list of the redirection function are automatically generated, so as to automatically generate the redirection code of the redirection target function, and ensure the accuracy and effectiveness of the code.

[0082] In a possible implementation, as shown in FIG. 10, based on the embodiment of FIG. 9, the process of generating the redirection code includes the following steps. Figure 3 Figure 1 The process of generating the redirection code includes the following steps.

[0083] In step 310, a binding function used to bind the function pointer and the redirection function is generated based on the function pointer of the target function and the parameter information of the redirection function.​

[0084] In one possible implementation, redirection is performed using the Detours library, and the process of redirection using the Detours library includes the following steps:

[0085] Step (1), initializing an internal data structure using a first initialization function, and preparing for Hook.

[0086] The first initialization function is used to initialize a transaction environment, i.e., a redirection binding environment. For example, the first initialization function can be the DetourTransactionBegin function, which can be used to create an atomic transaction environment and initialize an internal linked list structure to record subsequent binding operations.

[0087] Step (2), specifying a thread participating in Hook using a first flush thread function.

[0088] Optionally, the thread participating in Hook can be the current thread. The first flush thread function can be used to suspend the thread participating in Hook, and for example, the DetourUpdateThread function can be used to suspend the thread participating in Hook and pause the task corresponding to the thread to ensure smooth execution of the binding.

[0089] Step (3), binding the function pointer of the target function to the redirection function using a binding function.

[0090] The binding function is a function used for function binding, and for example, the binding function can be the DetourAttach function, which is used to bind the function pointer of the target function to the redirection function.

[0091] Step (4), submitting the Hook transaction using a first transaction commit function.

[0092] The first transaction commit function is a function used to submit all modified contents included in the transaction queue, and for example, the first transaction commit function can be the DetourTranscationCommit function, which is used to submit all modified contents included in the transaction queue, write them to the memory instruction, and restore the suspended thread.

[0093] The redirection processes of different functions are the same, in which, the internal data structure is initialized by the first initialization function, the threads participating in the Hook are specified by the first refresh thread function, and the Hook transaction is committed by the first transaction commit function; and the binding functions corresponding to different functions are different, in a possible implementation, the binding function for binding the function pointer of the target function and the binding function corresponding to the redirection function can be generated based on the function pointer of the target function and the parameter information of the redirection function.

[0094] Optionally, the function pointer of the target function and the function logic code corresponding to the redirection function and the parameter information of the redirection function can be filled into the required information of the DetourAttach function to generate the binding function for binding the function pointer of the target function and the redirection function, in which, the function logic code corresponding to the redirection function can be set in advance, which corresponds to the custom logic implemented by the user.

[0095] In step 320, the redirection code is generated based on the binding function.

[0096] In a possible implementation, the redirection processes of different functions are the same, in which, the internal data structure is initialized by the first initialization function, the threads participating in the Hook are specified by the first refresh thread function, and the Hook transaction is committed by the first transaction commit function, and the corresponding codes are the same, the codes corresponding to the initialization process by the first initialization function, the process of specifying the threads participating in the Hook by the first refresh thread function, and the Hook transaction committed by the first transaction commit function can be generated in advance.

[0097] After the binding function corresponding to each target function is generated, the binding function corresponding to each target function can be combined with the codes corresponding to the initialization process, the process of specifying the threads participating in the Hook, and the Hook transaction committed by the first transaction commit function to obtain the redirection code for redirecting each target function.

[0098] In the embodiment, in the process of generating the redirection code, the corresponding binding function can be generated based on the automatically generated function pointer of the target function and the parameter information of the redirection function, so that the redirection code is generated based on the automatically generated binding function, ensuring the effectiveness of the redirection code and helping to improve the coding efficiency.

[0099] In a possible implementation, the binding function for binding the function pointer and the redirection function can be generated based on the function pointer of the target function and the parameter information of the redirection function, and the process can include the following steps:

[0100] In the process of detecting whether the function pointer is a null function pointer, the function pointer is detected to determine whether the function pointer has pointed to the function address of the target function, and if the function pointer is not null, it is determined that the function pointer has pointed to the function address of the target function, so that the binding function can be generated based on the function pointer of the target function and the parameter information of the redirection function. If the detection result indicates that the function pointer is null, the redirection process of the target function is directly ended, and the target function is no longer redirected.

[0101] In the embodiment, the function pointer is detected before the redirection code is automatically generated based on the function pointer of the target function, so that the redirection code is generated only when the function pointer is not null, and the validity of the code is ensured.

[0102] In a possible implementation, the code for canceling the redirection can also be automatically generated to cancel the redirection of the target function. As shown in Figure 4 The process of generating the code for canceling the redirection includes the following steps:

[0103] Step 410, in response to the redirection cancel instruction, a cancel function for unbinding the function pointer and the redirection function is generated based on the function pointer of the target function and the parameter information of the redirection function.

[0104] In a possible implementation, the cancel code for canceling the redirection of the target function can be generated when the redirection cancel instruction is received. The redirection cancel instruction can be triggered by the user through the cancel option or triggered by the edited instruction.

[0105] In a possible implementation, Detours can be used to cancel the redirection. The unbinding process includes the following steps:

[0106] Step (1), the internal data structure is initialized by using the second initialization function to prepare for Hook.

[0107] The second initialization function is used to initialize the transaction environment, i.e., the unbinding environment. Optionally, the second initialization function in the unbinding process can be the same as or different from the first initialization function used in the above binding process. For example, the second initialization function can be the DetourTransactionBegin function.

[0108] Step (2), using a second refresh thread function to specify the thread participating in the Hook.

[0109] Optionally, the thread participating in the Hook can be the current thread. The second refresh thread function can be used to suspend the thread participating in the Hook, and the second refresh thread function can be the same as or different from the first refresh thread function described above. Illustratively, the second refresh thread function can be the DetourUpdateThread function.

[0110] Step (3), using an unbinding function to unbind the function pointer of the target function from the redirection function.

[0111] The unbinding function is a function used for function unbinding. Illustratively, the unbinding function can be the DetourDettach function, which is used to unbind the function pointer of the target function from the redirection function.

[0112] Step (4), using a second transaction commit function to commit the Hook transaction.

[0113] The second transaction commit function is a function used to commit all modified contents included in the transaction queue. Optionally, the second transaction commit function can be the same as or different from the first transaction commit function. Illustratively, the second transaction commit function can be the DetourTranscationCommit function.

[0114] For different redirection target functions, the corresponding unbinding functions are different. In one possible implementation, based on the function pointer of the target function and the parameter information of the redirection function, an unbinding function corresponding to the unbinding of the function pointer of the target function from the redirection function can be generated. Optionally, based on the function pointer of the target function and the parameter information of the redirection function, a DetourDettach function for unbinding can be generated.

[0115] Step 420, generating the unredirection code based on the unbinding function.

[0116] In one possible implementation, for the unbinding process of different functions, the process of initializing the internal data structure using the second initialization function, preparing to perform the Hook, using the second refresh thread function to specify the thread participating in the Hook, and using the second transaction commit function to commit the Hook transaction are all the same, and the corresponding codes are the same. The codes corresponding to the process of initializing using the second initialization function, the process of specifying the thread participating in the Hook using the second refresh thread function, and the process of committing the Hook transaction using the second transaction commit function can be generated in advance.

[0117] After the disbinding function corresponding to the target function is generated, the disbinding function corresponding to the target function can be combined with the code corresponding to the process of initializing by using the second initialization function, the process of specifying the thread participating in the Hook by using the second refresh thread function, and the process of submitting the Hook transaction by using the second transaction submission function, to obtain the unbinding code for unbinding the target function redirection.

[0118] In this embodiment, the disbinding function corresponding to the target function can be generated based on the function pointer of the automatically generated target function and the parameter information of the redirection function, so that the unbinding code is generated, and the redirection unbinding efficiency can be improved.

[0119] In a possible implementation, as shown in Figure 5 After the configuration information is read and parsed, the function redirection method further includes the following steps:

[0120] In step 510, the function information of each function parsed is respectively stored in the corresponding structure.

[0121] In a possible implementation, the configuration information includes the function information of at least one function, and the function information of each function includes the function name, the identification bit, the library information of the DLL where the function is located, and the function signature of the function. The identification bit is used to indicate whether the function is a target function that needs to be redirected. The configuration information is parsed to obtain the function information corresponding to each function.

[0122] Optionally, for the function information corresponding to each function, the elements contained therein can be stored in the corresponding data structure, that is, the structure. For each function, a corresponding structure can be generated, and the structure corresponding to one function includes the function name, the identification bit, the library information of the DLL where the function is located, and the function signature.

[0123] In step 520, the target structure in which the identification bit corresponding element in each structure is a preset value is determined, and the target structure is stored in a preset container.

[0124] In a possible implementation, the identification bit is a preset value used to indicate that the corresponding function is a target function that needs to be redirected. For example, the preset value is "true", or the preset value is "1". For the target structure in which the identification bit corresponding element in each structure corresponding to each function is a preset value, the structure corresponding to the target function can be determined, and the target structure corresponding to the target function can be stored in the preset container (vector) for management.

[0125] In step 530, the element information included in the target structure in the preset container is read to obtain the function information of the target function.

[0126] Optionally, the preset container stores target structures of each target function to be redirected. Based on the preset container, the target function to be redirected can be quickly queried. By traversing the target structures, the function information of each target function can be obtained.

[0127] In a possible implementation, the function information of each target function can be obtained by traversing the target structures in the preset container. Based on each target structure, the redirection code for redirecting each target function can be generated.

[0128] In this embodiment, the function information of the target function to be redirected is managed based on the structure, and the target structures of the target functions are stored in the preset container. Then, each target structure can be read from the container in sequence to obtain the function information of each target function, so as to generate the redirection code for redirecting each target function. The redirection code corresponding to multiple target functions can be quickly generated, which helps to improve the coding efficiency, and thus improves the redirection efficiency and helps to improve the development speed based on redirection.

[0129] In a possible implementation, the target function to be redirected can be adjusted by modifying the configuration information. Optionally, in response to a modification instruction of the configuration information, the modification information is determined. In response to the modification information indicating the function information of the new target function, the function information of the new target function is stored in the new structure, and the new structure is stored in the preset container.

[0130] In a possible implementation, if a user needs to add redirection to a function, the redirection can be realized by modifying the configuration information. If the redirection to a function included in the configuration information needs to be added, the identification bit corresponding to the function in the configuration information can be modified to a preset value. When a modification instruction of the identification bit is received, it can be determined whether the identification bit is modified to the preset value. If the identification bit of a function is modified to the preset value, the function can be determined as a target function. The identification bit in the structure corresponding to the function can be modified to the preset value, and the structure corresponding to the function is stored in the preset container. Then, when the preset container is traversed, the function information corresponding to the function can be automatically read from the preset container to generate the redirection code corresponding to the function.

[0131] Optionally, if a new function needs to be redirected, the function information of the new function can be added in the configuration information, and the identification bit can be set to a preset value. In response to the modification information indicating the function information of the new target function, the new structure corresponding to the new target function can be generated based on the function information of the new target function, and the new structure is stored in the preset container. Then, when the preset container is traversed, the function information corresponding to the new target function can be automatically read from the preset container to generate the redirection code corresponding to the new target function.

[0132] In a possible implementation, the deletion of the redirection target function can also be achieved by modifying the configuration information. If redirection to a target function is to be stopped, the identification bit of the target function in the configuration information can be set from a preset value to another value, for example, 0. In this case, the target structure corresponding to the target function is deleted from the preset container, and the function information of the target function will not be obtained through iteration, and the redirection to the target function is stopped.

[0133] In this embodiment, the dynamic addition and deletion of the target function can be achieved by modifying the configuration information, without the need for a large number of queries and modifications, which helps to improve the efficiency of adding and deleting the target function and improve the maintainability. Different configuration information can be maintained for different versions of the target function, the redirection of the functions corresponding to different versions is achieved, the multiple different versions of the target function do not need to be updated, and the version control efficiency is improved.

[0134] In a possible implementation, as shown in Figure 6 the process of generating the redirection code includes the following steps:

[0135] In step 610, the configuration information is read and parsed to obtain the function information of at least one function.

[0136] In step 620, the function information of each function is stored in the corresponding structure, and the target structure is stored in the preset container.

[0137] In step 630, the preset container is iterated, and based on each target structure in the preset container, the function pointer corresponding to each target function and the parameter information of the redirection function are generated.

[0138] In step 640, based on the function pointer corresponding to each target function and the function information of the redirection function, the binding function corresponding to each target function is generated respectively.

[0139] In step 650, the redirection code of each target function is generated based on each binding function.

[0140] The embodiments of steps 610-650 can refer to the above embodiments, and will not be described here.

[0141] As shown in Figure 7 it shows a structure diagram of a function redirection apparatus provided by an example embodiment of the present disclosure, which includes:

[0142] The information parsing module 710 is configured to read and parse the configuration information, and the configuration information includes the function information of the target function to be redirected.

[0143] The information generation module 720 is configured to generate a function pointer of the target function and parameter information corresponding to a redirection function based on the function information of the target function obtained by the analysis, the function pointer of the target function pointing to a function address of the target function, and the redirection function being a function actually executed after redirection.

[0144] The code generation module 730 is configured to generate a redirection code based on the function pointer of the target function and the parameter information corresponding to the redirection function.

[0145] The code execution module 740 is configured to execute the redirection code to redirect the target function.

[0146] In an exemplary embodiment, the information analysis module 710 is further configured to:

[0147] read and analyze the configuration information to obtain library information of a corresponding redirection dynamic library of the target function and a function signature of the target function;

[0148] The information generation module 720 is further configured to:

[0149] determine a function address of the target function and a parameter list of the target function based on the library information and the function signature;

[0150] generate the function pointer of the target function based on the function address and the parameter list;

[0151] generate a parameter list corresponding to the redirection function based on the parameter list of the target function, the parameter list corresponding to the redirection function matching the parameter list of the target function.

[0152] In an exemplary embodiment, the information generation module 720 is further configured to:

[0153] generate the parameter list corresponding to the redirection function based on a number of parameter variables indicated by the parameter list of the target function, the parameter list corresponding to the redirection function including a number of parameter variables not less than the number of parameter variables indicated by the parameter list of the target function.

[0154] In an exemplary embodiment, the code generation module 730 is further configured to:

[0155] generate a binding function for binding the function pointer and the redirection function based on the function pointer of the target function and the parameter information of the redirection function;

[0156] generate the redirection code based on the binding function.

[0157] In an exemplary embodiment, the code generation module 730 is further configured to:

[0158] detect whether the function pointer is a null function pointer;

[0159] in response to a detection result indicating that the function pointer is not the null function pointer, generate, based on the function pointer of the target function and the parameter information of the redirection function, a binding function for binding the function pointer and the redirection function.

[0160] In an exemplary embodiment, the code generation module 730 is further configured to:

[0161] in response to a redirection release instruction, generate, based on the function pointer of the target function and the parameter information of the redirection function, a release function for unbinding the function pointer and the redirection function;

[0162] generate, based on the release function, a release redirection code.

[0163] In an exemplary embodiment, the function redirection apparatus further comprises:

[0164] an information storage module configured to store the function information of each of the at least one function obtained by the parsing in a corresponding structure body respectively;

[0165] a structure body storage module configured to determine a target structure body in which an element corresponding to an identification bit is a preset value in each of the structure bodies, and store the target structure body in a preset container, the identification bit being used to indicate whether a function is a target function that needs to be redirected;

[0166] a structure body reading module configured to read element information included in the target structure body in the preset container to obtain the function information of the target function.

[0167] In an exemplary embodiment, the function redirection apparatus further comprises:

[0168] an information determination module configured to determine modification information in response to a modification instruction of the configuration information;

[0169] a function adding module configured to, in response to the modification information indicating that a function information of a new target function is added, store the function information of the new target function in a new structure body, and store the new structure body in the preset container.

[0170] The function redirection apparatus in the embodiments of the present disclosure and the embodiments of the function redirection method of the present disclosure correspond to each other, and related contents can be referred to each other, which will not be repeated here. The beneficial technical effects of the function redirection apparatus in the embodiments of the present disclosure can be referred to the corresponding beneficial technical effects of the example method part, which will not be repeated here.

[0171] In addition, the embodiments of the present disclosure also provide an electronic device, comprising:

[0172] a memory for storing a computer program;

[0173] a processor for executing the computer program stored in the memory, and when the computer program is executed, the function redirection method of any one of the embodiments of the present disclosure is implemented.

[0174] Figure 8 is a structural schematic diagram of an electronic device provided by an example embodiment of the present disclosure, as Figure 8 shown, the electronic device includes one or more processors and a memory.

[0175] The processor can be a central processing unit (CPU) or other forms of processing units with data processing capability and / or instruction execution capability, and can control other components in the electronic device to perform desired functions.

[0176] The memory can store one or more computer program products, and the memory can include various forms of computer readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may, for example, include random access memory (RAM), cache memory, and the like. The non-volatile memory may, for example, include read-only memory (ROM), hard disk, flash memory, and the like. One or more computer program products can be stored on the computer readable storage medium, and the processor can run the computer program products to implement the function redirection method of the embodiments of the present disclosure described above and / or other desired functions.

[0177] In one example, the electronic device can further include an input device and an output device, which are interconnected through a bus system and / or other forms of connection mechanism (not shown).

[0178] In addition, the input device can further include, for example, a keyboard, a mouse, and the like.

[0179] The output device can output various information to the outside, including the determined distance information, direction information, and the like. The output device can include, for example, a display, a speaker, a printer, a communication network and a remote output device connected thereto, and the like.

[0180] Of course, in order to simplify, Figure 8 Only some of the components of the electronic device related to the present disclosure are shown in the middle, and components such as buses, input / output interfaces, and the like are omitted. In addition, the electronic device can further include any other appropriate components according to a specific application.

[0181] In addition to the above method and device, an embodiment of the present disclosure can be a computer program product including computer program instructions that, when executed by a processor, cause the processor to perform the steps of the function redirection method according to various embodiments of the present disclosure described in the above part of the specification.

[0182] The computer program product can be written in any combination of one or more programming languages, including an object-oriented programming language such as Java, C++, and the like, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code can be executed entirely on the user computing device, partially on the user device, as a standalone software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server.

[0183] In addition, an embodiment of the present disclosure can also be a computer readable storage medium having stored thereon computer program instructions that, when executed by a processor, cause the processor to perform the steps of the function redirection method according to various embodiments of the present disclosure described in the above part of the specification.

[0184] The computer readable storage medium can employ any combination of one or more computer readable media. The computer readable medium can be a computer readable signal medium or a computer readable storage medium. The computer readable storage medium can include, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or apparatus, or any combination of the above. More specific examples (a non-exhaustive list) of the computer readable storage medium include an electrical connection having one or more wires, a portable disc, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any appropriate combination of the above.

[0185] The above describes the basic principles of the present disclosure in conjunction with specific embodiments, but it should be noted that the advantages, benefits, effects and the like mentioned in the present disclosure are merely examples and are not limiting, and these advantages, benefits, effects and the like cannot be considered as necessary for each embodiment of the present disclosure. In addition, the above specific details are merely for the purpose of example and understanding, and are not limiting, and the above details do not limit the present disclosure to be necessarily implemented with the above specific details.

[0186] Each embodiment in the specification is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between each embodiment can be mutually referred to. For system embodiments, since they basically correspond to method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the method embodiment.

[0187] The block diagrams of the devices, apparatuses, equipment, systems involved in the present disclosure are only exemplary examples and are not intended to require or imply the connection, arrangement, configuration shown in the block diagram. As those skilled in the art will recognize, these devices, apparatuses, equipment, systems can be connected, arranged, configured in any manner. Words such as "include", "contain", "have" and the like are open-ended words, which mean "including but not limited to", and can be used interchangeably. The words "or" and "and" used herein mean the word "and / or", and can be used interchangeably unless the context clearly indicates otherwise. The word "such as" used herein means the phrase "such as but not limited to", and can be used interchangeably.

[0188] The methods and apparatuses of the present disclosure can be implemented in many ways. For example, the methods and apparatuses of the present disclosure can be implemented by software, hardware, firmware, or any combination of software, hardware, firmware. The above order of steps for the method is only for illustration, and the steps of the method of the present disclosure are not limited to the above specific description, unless otherwise specifically described. In addition, in some embodiments, the present disclosure can also be implemented as programs recorded in recording media, which include machine-readable instructions for implementing the method according to the present disclosure. Therefore, the present disclosure also covers the recording media storing the programs for executing the method according to the present disclosure.

[0189] It should also be noted that in the apparatuses, equipment and methods of the present disclosure, each component or step can be decomposed and / or recombined. These decompositions and / or recombinations should be considered as equivalent solutions of the present disclosure.

[0190] The above description of the disclosed aspects is provided to enable any person skilled in the art to make or use the disclosure. Various modifications to these aspects will be readily apparent to those skilled in the art, and the generic principles defined herein can be applied to other aspects without departing from the scope of the disclosure. Thus, the present disclosure is not intended to be limited to the aspects shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

[0191] The above description has been presented to enable any person skilled in the art to make or use the disclosure. Furthermore, the purpose of the above description is not intended to limit the embodiments of the present disclosure to the form disclosed herein. Although various example aspects and embodiments have been discussed above, those of ordinary skill in the art will appreciate a variety of modifications, alternatives, permutations, additions, and sub-combinations of the described aspects and embodiments.

Claims

1. A function redirection method, characterized by, The method comprises: reading and parsing configuration information, wherein the configuration information comprises function information of a target function to be redirected; based on the function information of the target function obtained by parsing, generating a function pointer of the target function and parameter information corresponding to a redirection function to be executed, wherein the function pointer of the target function points to a function address of the target function, and the redirection function is a function to be actually executed after redirection; based on the function pointer of the target function and the parameter information corresponding to the redirection function, generating redirection code; executing the redirection code to redirect the target function.

2. The method of claim 1, wherein, The reading and parsing of the configuration information comprises: reading and parsing the configuration information to obtain library information of a corresponding redirection dynamic library of the target function and a function signature of the target function; the generating of the function pointer of the target function and the parameter information corresponding to the redirection function to be executed based on the function information of the target function obtained by parsing comprises: based on the library information and the function signature, determining a function address of the target function and a parameter list of the target function; based on the function address and the parameter list, generating the function pointer of the target function; based on the parameter list of the target function, generating a parameter list corresponding to the redirection function to be executed, wherein the parameter list corresponding to the redirection function to be executed matches the parameter list of the target function.

3. The method of claim 2, wherein, The generating of the parameter list corresponding to the redirection function to be executed based on the parameter list of the target function comprises: based on the number of parameter variables indicated by the parameter list of the target function, generating the parameter list corresponding to the redirection function to be executed, wherein the number of parameter variables included in the parameter list corresponding to the redirection function to be executed is not less than the number of parameter variables indicated by the parameter list of the target function.

4. The method according to any one of claims 1 to 3, characterized in that, The generating of the redirection code based on the function pointer of the target function and the parameter information of the redirection function comprises: based on the function pointer of the target function and the parameter information of the redirection function, generating a binding function for binding the function pointer and the redirection function; based on the binding function, generating the redirection code.

5. The method of claim 4, wherein, The generating of the binding function for binding the function pointer and the redirection function based on the function pointer of the target function and the parameter information of the redirection function comprises: detecting whether the function pointer is a null function pointer; in response to a detection result indicating that the function pointer is not the null function pointer, generating the binding function for binding the function pointer and the redirection function based on the function pointer of the target function and the parameter information of the redirection function.

6. The method according to any one of claims 1 to 3, characterized in that, The method further comprises: in response to a redirection release instruction, generating a release function for unbinding the function pointer and the redirection function based on the function pointer of the target function and the parameter information of the redirection function; based on the release function, generating release redirection code.

7. The method according to any one of claims 1 to 3, characterized in that, The configuration information comprises function information corresponding to each of at least one function; after the reading and parsing of the configuration information, the method further comprises: store the function information of the at least one function respectively obtained by the parsing into corresponding structure bodies respectively; determine a target structure body in which an element corresponding to an identification bit is a preset value, and store the target structure body into a preset container, the identification bit being used to indicate whether the function is a target function that needs to be redirected; read element information included in the target structure body in the preset container to obtain function information of the target function.

8. The method of claim 7, wherein, The method further includes: in response to a modification instruction for the configuration information, determine modification information; in response to the modification information indicating function information of a new target function, store the function information of the new target function into a new structure body, and store the new structure body into the preset container.

9. A function redirection apparatus, characterized by comprising: The apparatus includes: an information parsing module, configured to read and parse configuration information, the configuration information including function information of a target function that needs to be redirected; an information generating module, configured to generate a function pointer of the target function and parameter information corresponding to a redirection function based on the function information of the target function obtained by the parsing, the function pointer of the target function pointing to a function address of the target function, the redirection function being a function actually executed after the redirection; a code generating module, configured to generate a redirection code based on the function pointer of the target function and the parameter information corresponding to the redirection function; a code executing module, configured to execute the redirection code to redirect the target function.

10. An electronic device, comprising: include: a memory, configured to store a computer program; a processor, configured to execute the computer program stored in the memory, and when the computer program is executed, implement the function redirection method in any one of claims 1 to 8.