A code redirection method, device, equipment and computer readable storage medium

By compiling the redirected method into a dynamic link library file before compilation by the JIT compiler, and redirecting it when the target method fails to be instrumented, the problem of application termination due to instrumentation failure in the .NET platform is solved, thus improving the application's running efficiency.

CN115373681BActive Publication Date: 2026-06-19SECZONE TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SECZONE TECH CO LTD
Filing Date
2022-08-11
Publication Date
2026-06-19

AI Technical Summary

Technical Problem

In the .NET platform, instrumentation failures can cause application errors and termination, especially when certain low-level methods cannot be effectively instrumented before or during the startup initialization of a managed application.

Method used

By compiling the redirection method into a dynamic link library file before the JIT compiler compiles it, matching and replacing the CIL code of the initial method, if the instrumentation of the target calling method fails, the redirection method in the dynamic link library is called to redirect it, ensuring that the application continues to run.

Benefits of technology

When instrumentation fails, redirection methods ensure the application continues to run, improving application efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115373681B_ABST
    Figure CN115373681B_ABST
Patent Text Reader

Abstract

The application provides a code redirection method and device, equipment and computer readable storage medium, the redirection method determined according to the redirection demand of initial method is compiled into dynamic link library file;Before the CIL code of initial method is compiled by JIT compiler, the CIL code of initial method is matched using the CIL code of the method to be called in the program set;If the target CIL code of the target calling method matched with the CIL code of initial method is detected, the CIL code is replaced with the target CIL code;If the target calling method fails to be inserted, the initial method is redirected by calling the redirection method. Through the implementation of the application scheme, when the CIL code replacement fails before the JIT compiler compiles, the target CIL code of the target calling method is redirected, the application continues to run after the insertion fails, and the application running efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of electronic technology, and in particular to a code redirection method, apparatus, device, and computer-readable storage medium. Background Technology

[0002] After compilation, .NET platform code becomes a platform-independent Common Intermediate Language (CIL). In our daily development, we need to analyze and monitor the performance of applications or implement automated coverage testing, which requires instrumentation of the application.

[0003] This method involves inserting probes (also known as "detectors," essentially code segments that collect information, such as assignment statements or function calls for collecting coverage information) into the program while ensuring the original logical integrity of the program under test. By executing these probes and outputting characteristic data of the program's operation, and analyzing this data, control flow and data flow information can be obtained, leading to dynamic information such as logical coverage, thus achieving the testing objective. Currently, the common instrumentation method in the .NET platform is to modify the CIL to achieve instrumentation functionality. However, this method has a drawback: some low-level methods, such as those provided in mscorlib.dll, cannot be effectively instrumented. In managed programs, before or during the initialization of an application's AppDomain, some functions of the managed program are not yet initialized. The application's initialization may be insufficient to handle the execution of some managed code (such as probe loading). This code may call from the rewritten CIL (directly or indirectly), causing infinite recursion or invalid CIL verification. In this case, instrumentation will fail, and the application will terminate with an error. Summary of the Invention

[0004] This application provides a code redirection method, apparatus, device, and computer-readable storage medium, which can at least solve the problem of application error termination caused by instrumentation failure in related technologies.

[0005] The first aspect of this application provides a code redirection method, including:

[0006] The redirection methods determined for the redirection requirements of the initial method are compiled into a dynamic link library file; wherein, the redirection methods are user-defined wrapper methods;

[0007] Before the JIT compiler compiles the CIL code of the initial method, the CIL code of the initial method is matched with the CIL code of the method to be called in the assembly; wherein, the CIL code includes: CIL opcode;

[0008] If a target CIL code matching the CIL code of the initial method is detected, then the target CIL code is used to replace the CIL code.

[0009] If instrumentation of the target method fails during the replacement process, the redirection method in the dynamic link library file is invoked to redirect the initial method.

[0010] A second aspect of this application provides a code redirection apparatus, comprising:

[0011] The compilation module is used to compile the redirection methods determined for the redirection requirements of the initial method into a dynamic link library file; wherein the redirection methods are user-defined wrapper methods;

[0012] A matching module is used to match the CIL code of the initial method with the CIL code of the method to be called in the assembly before the JIT compiler compiles the CIL code of the initial method; wherein the CIL code includes: CIL opcode;

[0013] The replacement module is configured to replace the CIL code with the target CIL code if a target CIL code matching the CIL code of the initial method is detected.

[0014] The redirection module is used to redirect the initial method if instrumentation of the target method fails during the replacement process.

[0015] A third aspect of this application provides an electronic device, characterized in that it includes a memory and a processor, wherein the processor is configured to execute a computer program stored in the memory, and the processor executes the computer program in accordance with the steps of the code redirection method provided in the first aspect of this application.

[0016] The fourth aspect of this application provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, it implements the steps of the code redirection method provided in the first aspect of this application.

[0017] As can be seen from the above, according to the code redirection method, apparatus, device, and computer-readable storage medium provided in this application, the redirection method determined for the redirection requirements of the initial method is compiled into a dynamic link library file; wherein, the redirection method is a user-defined wrapper method; before the JIT compiler compiles the CIL code of the initial method, the CIL code of the initial method is matched with the CIL code of the method to be called in the assembly; wherein, the CIL code includes: CIL opcode; if the target CIL code of the target calling method that matches the CIL code of the initial method is detected, the target CIL code is used to replace the CIL code; during the replacement process, if the instrumentation of the target calling method fails, the redirection method in the dynamic link library file is called to redirect the initial method. Through the implementation of this application, when the CIL code replacement fails before the JIT compiler compiles, the target CIL code of the target calling method is redirected, and compilation is performed again after the redirection is completed, ensuring that the application continues to run after instrumentation failure, thereby improving the application's running efficiency. Attached Figure Description

[0018] Figure 1 This is a basic flowchart illustrating the code redirection method provided in the first embodiment of this application;

[0019] Figure 2 A detailed flowchart illustrating the code redirection method provided in the second embodiment of this application;

[0020] Figure 3 This is a schematic diagram of the program modules of the code redirection device provided in the third embodiment of this application;

[0021] Figure 4 This is a schematic diagram of the structure of an electronic device provided in the fourth embodiment of this application. Detailed Implementation

[0022] To make the inventive objectives, features, and advantages of this application more apparent and understandable, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0023] To address the problem of application errors and termination caused by instrumentation failures in related technologies, the first embodiment of this application provides a code redirection method.

[0024] The following is the key code for performing CIL code replacement:

[0025] IL_0001:newobj instance void ConsoleAppCore.B::.ctor()

[0026] IL_0006:stloc.0

[0027] IL_0007:ldloc.0

[0028] IL_0008:ldarg.1

[0029] IL_0009:callvirt instance int32 ConsoleAppCore.B::MeyhodB(string)

[0030] IL_000e:stloc.1

[0031] The following is the key code for redirecting the method:

[0032] IL_0001:ldarg.1

[0033] IL_0002:call int32 ConsoleAppCore.C::MethodC(string)

[0034] IL_0007:stloc.0

[0035] like Figure 1 This is a basic flowchart of the code redirection method provided in this embodiment. The code redirection method includes the following steps:

[0036] Step 101: Compile the redirection methods determined for the redirection requirements of the initial method into a dynamic link library file.

[0037] Specifically, traditional instrumentation failures can cause the entire program to fail. Therefore, before instrumentation, we need to use technical means to find methods that cannot be instrumented using traditional methods; these methods are called methods that need to be redirected. In this embodiment, we first identify the initial method that needs to be redirected, and then determine the redirection method based on the redirection requirements of the initial method. The redirection method is a user-defined wrapper method, and it is compiled into a dynamic link library file (DLL). During redirection, the DLL file in the assembly can be directly called to redirect the initial method.

[0038] In an optional implementation of this embodiment, before compiling the redirection method determined for the redirection requirements of the initial method into a dynamic link library file, the method further includes: preprocessing the CIL code of the initial method and determining the redirection requirements of the initial method based on the preprocessing results; and determining the redirection method corresponding to the initial method based on the redirection requirements.

[0039] Specifically, in this embodiment, the CIL code of the initial method is preprocessed to determine whether there is a need for redirection. Then, the redirection method corresponding to the initial method is determined based on the redirection requirement. The preprocessing methods include, but are not limited to: obtaining the CIL code of the initial method, determining the parameters and variables in the CIL code, and analyzing the possibility of errors in the CIL code during the replacement process in conjunction with the installed .NET platform, thereby determining whether there is a need for redirection in the initial method; or, querying the historical compilation information of the initial method, determining whether there is redirection information in the initial method from the historical compilation information, determining the redirection requirement based on the redirection information, analyzing the requirement, and determining the corresponding redirection method based on the analysis results.

[0040] Step 102: Before the JIT compiler compiles the CIL code of the initial method, the CIL code of the initial method is matched with the CIL code of the method to be called in the assembly.

[0041] Specifically, in this embodiment, all CIL code to be compiled is filtered and matched before the JIT compiler compiles. The target CIL code of the method to be called that matches the CIL code of the initial method is determined from the assembly. Once the matching CIL code is detected, the CIL code replacement work can begin.

[0042] In one optional implementation of this embodiment, the step of matching the CIL code of the initial method with the CIL code of the method to be called in the assembly includes: obtaining the CIL code of the initial method based on the CIL runtime mechanism; analyzing the CIL code of the initial method; adding an assembly reference based on the analysis results; and matching the CIL code of the initial method with the CIL code of the method to be called in the referenced assembly.

[0043] Specifically, in this embodiment, based on the CLR's operating mechanism, the CIL code before compilation is dynamically obtained and matched against predefined rules. After a match is found, the original method's CIL is analyzed, and the points that need to be redirected are replaced. Matching the CIL is not simply finding the corresponding code, but requires context analysis, adding new assembly references, and matching them with the CIL code of the method to be called in the assembly. The matched method to be called is the target calling method.

[0044] Step 103: If a target CIL code for a target calling method that matches the CIL code of the initial method is detected, then the target CIL code is used to replace the CIL code.

[0045] Specifically, in this embodiment, the CIL code before compilation is dynamically obtained and matched against predefined rules. If the CIL code of the method to be called is found to contain a target CIL code that matches the CIL code of the initial method, the target CIL code is used to replace the CIL code.

[0046] In one optional implementation of this embodiment, the step of replacing CIL code with target CIL code includes: calling the constructor of the initial method using the newobj opcode and returning a reference instance of the target calling method; pushing the reference instance onto the local variable stack using the stloc.0 opcode; pushing the local variable at index 0 onto the evaluation stack using the ldloc.0 opcode, and pushing the initial method parameter at index 1 onto the evaluation stack using the ldloc.1 opcode; calling the target calling method using the callvirt opcode, replacing the initial method parameter with the target calling method parameter, and pushing the return value of the target calling method onto the local variable stack at index 1 using the stloc.1 opcode.

[0047] Specifically, in this embodiment, as shown in the key code for performing CIL code replacement above, the CIL code of the initial method from line IL_0001 to line IL_000e is calling the target calling method, that is, replacing the Method A method (initial method) of Class B (target calling method) with the Method A method (initial method) of Class B. Here, the IL_0001:newobj opcode is used to perform the instantiation of a class, calling the constructor and returning a Class. The reference instance of B; IL_0006: stloc.0 opcode is used to push the instantiated reference onto the local variable stack, with index 0. The data corresponding to the index includes: local variables and method parameters; IL_0007: ldloc.0 opcode is used to push the local variable with index 0 onto the evaluation stack, which is the instantiated reference above; IL_0008: ldarg.1 opcode is used to push the initial method parameter with index 1 onto the evaluation stack. The evaluation stack is a concept in CIL used to store temporary variables calculated during method execution; IL_0009: callvirt opcode is used to call the MethodB method of ClassB and replace the passed parameters with the initial method parameters passed in IL_0008; IL_000e: stloc.1 opcode is used to push the return value returned by the MethodB method onto the local variable stack with index 1.

[0048] Furthermore, in an optional implementation of this embodiment, before the step of pushing the return value of the target called method onto the local variable stack at index 1 using the stloc.1 opcode, the method further includes: instrumenting the target called method; and after successful instrumentation, controlling the initial method to obtain the return value of the target called method.

[0049] Specifically, in this embodiment, as shown in the IL_0009 code segment above, when the target calling method is invoked, the CIL code of the initial method and the target calling method will be replaced. Before the replacement, the target calling method needs to be instrumented. Only after successful instrumentation can all parameters, exceptions, and return values ​​of the target calling method be obtained, and the execution logic of the target calling method can also be affected. However, if there is a problem during traditional instrumentation, causing the target call to fail to be instrumented, then the target calling method needs to be redirected.

[0050] Step 104: During the replacement process, if instrumentation of the target method fails, the redirection method in the dynamic link library file is called to redirect the initial method.

[0051] Specifically, in this embodiment, if the instrumentation of the target calling method fails, it means that the initial method cannot perform normal calling work on the target calling method. In this case, the target calling method needs to be replaced, that is, the redirection method is used to call the redirection method in the dynamic link library file to redirect the initial method.

[0052] In one optional implementation of this embodiment, the step of redirecting the initial method includes: pushing the parameter of the initial method at index 1 onto the evaluation stack using the ldarg.1 opcode; calling the redirected method using the call function; and pushing the return value of the redirected method onto the local variable stack at index 0 using the stloc.0 opcode.

[0053] Specifically, in this embodiment, as shown in the key code for method redirection above, the IL_0001:ldarg.1 opcode is used to push the parameter at index 1 onto the evaluation stack; IL_0002:call is used to call the MethodC method of Class C, i.e., the redirected method. Since Class C is a static type, it is not necessary to instantiate it when calling the MethodC method of Class C, and Call is used instead of Callvirt; the IL_0007:stloc.0 opcode is used to push the return value returned by the MethodC method onto the local variable stack at index 0.

[0054] In one optional implementation of this embodiment, after the step of redirecting the initial method, the method is further further updated to the assembly; when the JIT compiler recompiles the CIL code of the initial method, the redirecting method is invoked by referencing the assembly to execute the compilation of the CIL code.

[0055] Specifically, in this embodiment, assemblies constitute the basic unit for deployment, version control, reuse, activation scope, and security permissions of .NET applications. An assembly is a collection of types and resources generated for collaborative work, forming a logical functional unit. Assemblies take the form of executable files (.exe) or dynamic link library files (.dll) and are the building blocks of .NET applications. After the CIL code replacement is complete, the assembly reference is updated and the new CIL code is saved in memory. After redirection, the assembly of the redirected method needs to be referenced; after referencing it, the redirected method can be called. At this point, the JIT compiler starts compiling again, and the compiled CIL code is the redirected CIL code, i.e., the initial method calls the redirected method for compilation.

[0056] Based on the embodiments of the above application, the redirection method determined for the redirection requirement of the initial method is compiled into a dynamic link library file. Before the JIT compiler compiles the CIL code of the initial method, the CIL code of the initial method is matched with the CIL code of the method to be called in the assembly. The CIL code includes CIL opcodes. If the target CIL code of the target calling method that matches the CIL code of the initial method is detected, the target CIL code is used to replace the CIL code. During the replacement process, if instrumentation of the target calling method fails, the redirection method in the dynamic link library file is called to redirect the initial method. Through the implementation of this application's solution, when CIL code replacement fails before compilation by the JIT compiler, the target CIL code of the target calling method is redirected. After redirection, compilation is performed again, ensuring that the application continues to run after instrumentation failure, thus improving application efficiency.

[0057] Figure 2 The method described in the second embodiment of this application is a refined code redirection method, which includes:

[0058] Step 201: Preprocess the CIL code of the initial method and determine the redirection requirements of the initial method based on the preprocessing results.

[0059] Step 202: Determine the redirection method corresponding to the initial method based on the redirection requirements, and compile the redirection method into a dynamic link library file.

[0060] Step 203: Before the JIT compiler compiles the CIL code of the initial method, obtain the CIL code of the initial method based on the CIL runtime mechanism and analyze the CIL code of the initial method.

[0061] Step 204: Match the CIL code of the initial method with the CIL code of the method to be called in the referenced assembly based on the analysis results.

[0062] Specifically, in this embodiment, assemblies constitute the basic unit for the deployment, versioning, reuse, activation scope, and security permissions of .NET applications. An assembly is a collection of types and resources generated for collaborative work, which together form a logical functional unit. Assemblies take the form of executable files (.exe) or dynamic link library files (.dll) and are the building blocks of .NET applications.

[0063] Step 205: If a target CIL code for a target calling method that matches the CIL code of the initial method is detected, then the target CIL code is used to replace the CIL code.

[0064] Step 206: During the replacement process, if instrumentation of the target method fails, the redirection method in the dynamic link library file is called to redirect the initial method.

[0065] Step 207: Update the CIL code of the redirection method to the assembly. When the JIT compiler recompiles the CIL code of the initial method, it calls the redirection method by referencing the assembly to compile the CIL code.

[0066] According to the code redirection method provided in this application, the CIL code of the initial method is preprocessed, and the redirection requirements of the initial method are determined based on the preprocessing results. A redirection method corresponding to the initial method is determined based on the redirection requirements, and the redirection method is compiled into a dynamic link library file. Before the JIT compiler compiles the CIL code of the initial method, the CIL code of the initial method is obtained based on the CIL runtime mechanism and analyzed. Based on the analysis results, the CIL code of the initial method is matched with the CIL code of the method to be called in the referenced assembly. If the target CIL code of the target calling method that matches the CIL code of the initial method is detected, the target CIL code is used to replace the CIL code. During the replacement process, if the instrumentation of the target calling method fails, the redirection method in the dynamic link library file is called to redirect the initial method. The CIL code of the redirection method is updated to the assembly. When the JIT compiler compiles the CIL code of the initial method again, the redirection method is called through the referenced assembly to execute the compilation of the CIL code. By implementing the solution in this application, when the replacement of CIL code fails before the JIT compiler compiles, the target CIL code of the target calling method will be redirected. After the redirection is completed, the compilation will be performed again, ensuring that the application continues to run after instrumentation failure, thereby improving the application's running efficiency.

[0067] Figure 3 A code redirection apparatus is provided in the third embodiment of this application, which can be used to implement the code redirection method in the foregoing embodiments. For example... Figure 3 As shown, the code redirection device mainly includes:

[0068] The compilation module 301 is used to compile the redirection method determined for the redirection requirements of the initial method into a dynamic link library file; wherein the redirection method is a user-defined wrapper method;

[0069] Matching module 302 is used to match the CIL code of the initial method with the CIL code of the method to be called in the assembly before the JIT compiler compiles the CIL code of the initial method; wherein, the CIL code includes: CIL opcode;

[0070] Replacement module 303 is used to replace the CIL code with the target CIL code if the target CIL code of the target calling method that matches the CIL code of the initial method is detected.

[0071] The redirection module 304 is used to redirect the initial method if instrumentation of the target method fails during the replacement process.

[0072] In an optional embodiment of this invention, the code redirection device further includes a preprocessing module and a determination module. The preprocessing module is used to preprocess the CIL code of the initial method and determine the redirection requirements of the initial method based on the preprocessing results. The determination module is used to determine the redirection method corresponding to the initial method based on the redirection requirements.

[0073] In one optional implementation of this embodiment, the matching module is specifically used for: obtaining the CIL code of the initial method based on the CIL runtime mechanism; analyzing the CIL code of the initial method; adding assembly references according to the analysis results; and matching the CIL code of the initial method with the CIL code of the method to be called in the referenced assembly.

[0074] In an optional implementation of this embodiment, when the replacement module performs the function of replacing CIL code with target CIL code, it specifically performs the following steps: It calls the constructor of the initial method using the `newobj` opcode and returns a reference instance of the target calling method; it pushes the reference instance onto the local variable stack using the `stloc.0` opcode; where `stloc.0` represents the index of the `stloc` opcode being 0, and the data corresponding to the index includes: local variables and method parameters; it pushes the local variable at index 0 onto the evaluation stack using the `ldloc.0` opcode, and pushes the initial method parameter at index 1 onto the evaluation stack using the `ldloc.1` opcode; it calls the target calling method using the `callvirt` opcode, replaces the initial method parameter with the target calling method parameter, and pushes the return value of the target calling method onto the local variable stack at index 1 using the `stloc.1` opcode.

[0075] Furthermore, in an optional embodiment of this invention, the code redirection device further includes: an instrumentation module and a calling module. The instrumentation module is used to: instrument the target calling method. The calling module is used to: control the initial method to obtain the return value of the target calling method after successful instrumentation.

[0076] Furthermore, in another optional implementation of this embodiment, the redirection module is specifically used to: push the parameter of the initial method at index 1 onto the evaluation stack using the ldarg.1 opcode; call the redirection method using the call function; and push the return value of the redirection method onto the local variable stack at index 0 using the stloc.0 opcode.

[0077] In an optional embodiment of this example, the code redirection device further includes an update module. The update module is configured to update the redirection method in the assembly. The compilation module is further configured to: when the JIT compiler recompiles the CIL code of the initial method, invoke the redirection method by referencing the assembly to perform the compilation of the CIL code.

[0078] According to the code redirection device provided in this application, the redirection method determined for the redirection requirements of the initial method is compiled into a dynamic link library file; wherein, the redirection method is a user-defined wrapper method; before the JIT compiler compiles the CIL code of the initial method, the CIL code of the initial method is matched with the CIL code of the method to be called in the assembly; wherein, the CIL code includes: CIL opcode; if the target CIL code of the target calling method that matches the CIL code of the initial method is detected, the CIL code is replaced with the target CIL code; during the replacement process, if the instrumentation of the target calling method fails, the redirection method in the dynamic link library file is called to redirect the initial method. Through the implementation of this application, when the CIL code replacement fails before the JIT compiler compiles, the target CIL code of the target calling method is redirected, and compilation is performed again after the redirection is completed, ensuring that the application continues to run after instrumentation failure, thereby improving the application's running efficiency.

[0079] Figure 4 An electronic device is provided in the fourth embodiment of this application. This electronic device can be used to implement the code redirection method in the foregoing embodiments, and mainly includes:

[0080] The system includes a memory 401, a processor 402, and a computer program 403 stored on the memory 401 and executable on the processor 402. The memory 401 and the processor 402 are communicatively connected. When the processor 402 executes the computer program 403, it implements the code redirection method described in the foregoing embodiments. The number of processors can be one or more.

[0081] The memory 401 can be a high-speed random access memory (RAM) or a non-volatile memory, such as a disk storage device. The memory 401 is used to store executable program code, and the processor 402 is coupled to the memory 401.

[0082] Furthermore, embodiments of this application also provide a computer-readable storage medium, which may be disposed in the electronic device described in the above embodiments, and the computer-readable storage medium may be as described above. Figure 4 The memory in the illustrated embodiment.

[0083] The computer-readable storage medium stores a computer program that, when executed by a processor, implements the code redirection method described in the foregoing embodiments. Furthermore, the computer-readable storage medium can also be a USB flash drive, external hard drive, read-only memory (ROM), RAM, magnetic disk, or optical disk, or any other medium capable of storing program code.

[0084] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or modules may be electrical, mechanical, or other forms.

[0085] The modules described as separate components may or may not be physically separate. Similarly, the components shown as modules may or may not be physical modules; they may be located in one place or distributed across multiple network modules. Some or all of the modules can be selected to achieve the purpose of this embodiment, depending on actual needs.

[0086] Furthermore, the functional modules in the various embodiments of this application can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The integrated modules described above can be implemented in hardware or as software functional modules.

[0087] If the integrated module is implemented as a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a readable storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned readable storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.

[0088] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and modules involved are not necessarily essential to this application.

[0089] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.

[0090] The above is a description of the code redirection method, apparatus, device, and computer-readable storage medium provided in this application. For those skilled in the art, based on the ideas of the embodiments of this application, there will be changes in the specific implementation methods and application scope. Therefore, the content of this specification should not be construed as a limitation of this application.

Claims

1. A code redirection method, characterized by, include: The redirection methods determined for the redirection requirements of the initial method are compiled into a dynamic link library file; wherein, the redirection methods are user-defined wrapper methods; Before the JIT compiler compiles the CIL code of the initial method, the CIL code of the initial method is matched with the CIL code of the method to be called in the assembly; wherein, the CIL code includes: CIL opcode; If a target CIL code matching the CIL code of the initial method is detected, then the target CIL code is used to replace the CIL code. If instrumentation of the target method fails during the replacement process, the redirection method in the dynamic link library file is invoked to redirect the initial method.

2. The code redirection method of claim 1, wherein, Before the step of compiling the redirection method determined for the redirection requirements of the initial method into a dynamic link library file, the method further includes: The CIL code of the initial method is preprocessed, and the redirection requirements of the initial method are determined based on the preprocessing results; The redirection method corresponding to the initial method is determined based on the redirection requirements.

3. The code redirection method of claim 1, wherein, The step of matching the CIL code of the initial method with the CIL code of the method to be called in the assembly includes: The CIL code of the initial method is obtained based on the CIL runtime mechanism; The CIL code of the initial method was analyzed; Add assembly references based on the analysis results; The CIL code of the initial method is matched with the CIL code of the method to be called in the referenced assembly.

4. The code redirection method of claim 1, wherein, The step of replacing the CIL code with the target CIL code includes: The constructor of the initialization method is invoked via the opcode newobj, and a reference instance of the target method is returned. The reference instance is pushed onto the local variable stack using the stloc.0 opcode; wherein, the stloc.0 opcode is represented by the index 0 of the stloc opcode, and the data corresponding to the index includes: local variables and method parameters; The local variable at index 0 is pushed onto the evaluation stack using the ldloc.0 opcode, and the initial method parameter at index 1 is pushed onto the evaluation stack using the ldloc.1 opcode. The target method is invoked using the callvirt opcode, the initial method parameters are replaced with the target method parameters, and the return value of the target method is pushed onto the local variable stack at index 1 using the stloc.1 opcode.

5. The code redirection method of claim 4, wherein, Before the step of pushing the return value of the target method call onto the local variable stack at index 1 using the stloc.1 opcode, the method further includes: Instrument the target calling method; After successful instrumentation, the initial method is controlled to obtain the return value of the target calling method.

6. The code redirection method of claim 4, wherein, The step of redirecting the initial method includes: The parameter of the initial method with index 1 is pushed onto the evaluation stack using the opcode ldarg.1; The redirection method is invoked via the call function; The return value of the redirection method is pushed onto the local variable stack at index 0 using the stloc.0 opcode.

7. The code redirection method of claim 1, wherein, After the step of redirecting the initial method, the method further includes: Update the redirection method to the assembly; When the JIT compiler recompiles the CIL code of the initial method, it calls the redirection method by referencing the assembly to execute the compilation of the CIL code.

8. A code redirection apparatus, characterized by, include: The compilation module is used to compile the redirection methods determined for the redirection requirements of the initial method into a dynamic link library file; wherein the redirection methods are user-defined wrapper methods; A matching module is used to match the CIL code of the initial method with the CIL code of the method to be called in the assembly before the JIT compiler compiles the CIL code of the initial method; wherein the CIL code includes: CIL opcode; The replacement module is configured to replace the CIL code with the target CIL code if a target CIL code matching the CIL code of the initial method is detected. The redirection module is used to redirect the initial method if instrumentation of the target method fails during the replacement process.

9. An electronic device, characterized in that, Includes memory and processor, of which: The processor is used to execute computer programs stored in the memory; When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 7.

10. A computer-readable storage medium having stored thereon a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.