Patch execution method and apparatus

By inserting and modifying the exception instruction into a jump instruction in the original function header of the program, it is possible to seamlessly repair defects while the program is running, solve the problems that affect performance in the existing technology, improve execution efficiency and patch efficiency, and not Depends on the operating system kernel.

WO2024193314A1PCT designated stage expired Publication Date: 2024-09-26HUAWEI TECH CO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2024/079345
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-03-22
Filing Date
2024-02-29
Publication Date
2024-09-26

AI Technical Summary

Technical Problem

Existing technology is difficult to seamlessly repair defects while the program is running, and requires the program to be paused for repair, affecting performance and efficiency.

Method used

By inserting an exception instruction into the original function header of the program and modifying it into a jump instruction, the thread can jump to the patch area to execute the patch function when accessing the original function, avoiding program restarts, improving execution efficiency and patch efficiency, and Does not depend on the operating system kernel.

Benefits of technology

It realizes the repair of defects without restarting the program, improves program execution efficiency and patching efficiency, and can implement patching operations without operating system kernel support.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024079345_26092024_PF_FP
    Figure CN2024079345_26092024_PF_FP
Patent Text Reader

Abstract

The present application provides a patch execution method and apparatus. The method comprises: acquiring a first patch function, and storing the first patch function into a patch area, wherein the first patch function is used for modifying a first primitive function in a program, an address of the first primitive function is a first address, and an address of the first patch function is a second address; inserting an anomaly instruction in front of a first instruction of the first primitive function, wherein the anomaly instruction is used by a thread to suspend execution of the instructions in the first primitive function when the execution advances to the anomaly instruction; and modifying the anomaly instruction into a jump instruction on the basis of the first address and the second address, wherein the jump instruction is used by the thread to jump to the patch area to execute the instructions in the first patch function when the thread accesses the first primitive function. The defect of a primitive function to be patched in a program can be repaired by means of a patch function. In addition, the program may also be patched without restarting the program, thereby improving program execution efficiency and patch efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Patch execution method and device

[0001] This application claims priority to the Chinese patent application with application number 202310317574.0 filed with the State Intellectual Property Office of China on March 22, 2023, and priority to the Chinese patent application with the invention name “A Patch Execution Method and Device”, all contents of which are incorporated by reference into this application. Technical Field

[0002] The present application relates to the field of computer technology, and in particular to a patch execution method and device. Background Art

[0003] Programs may contain defects while running. To ensure normal operation, these defects must be corrected. Pausing the program to correct defects can affect program performance and efficiency. Further research is needed to determine how to correct defects without pausing the program.

[0004] Summary of the Invention

[0005] The present application provides a patch execution method and device, which enables patching of a program without restarting the program, thereby improving program execution efficiency and patch efficiency.

[0006] In a first aspect, the present application provides a patch execution method, the method comprising: obtaining a first patch function and storing the first patch function in a patch area, the first patch function being used to modify a first original function in a program, the address of the first original function being a first address, and the address of the first patch function being a second address; inserting an exception instruction in front of the first instruction of the first original function, the exception instruction being used for the thread to suspend execution of instructions in the first original function after executing the exception instruction; modifying the exception instruction into a jump instruction based on the first address and the second address, the jump instruction being used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

[0007] Inserting a jump instruction can ensure that the thread accessing the original function can jump to the patch function instead of accessing the original function after the patch is activated.

[0008] Through this application, defects in the original function to be patched in the program can be repaired through the patch function. Patching the program can be implemented without restarting the program, improving program execution efficiency and patch efficiency. This application inserts an exception instruction in the function header of the original function and then modifies the exception instruction into a jump instruction. The area where the jump instruction is located is then the critical section. In this way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, so there is no need to trap the operating system kernel to check whether there are threads accessing instructions in the critical section. In other words, the patch execution method provided by this application does not rely on the kernel. Even if the application software where the program is located does not have permission to access the current operating system, that is, the current operating system is a third-party operating system, it is still possible to patch the program.

[0009] In conjunction with the first aspect, in one possible implementation, the exception instruction is modified into a jump instruction based on the first address and the second address, specifically including: when the offset between the first address and the second address is less than a preset offset, the exception instruction is modified into a first jump instruction based on the offset between the first address and the second address, the first jump instruction being used to cause the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function. The jump instruction has a limited jump distance. When a one-step jump is possible, the thread can jump directly from the original function to the patch function.

[0010] In conjunction with the first aspect, in one possible implementation, the exception instruction includes a p-byte exception instruction opcode, and q empty bytes are included after the exception instruction; based on the offset between the first address and the second address, the exception instruction is modified to a first jump instruction, specifically comprising: filling the offset between the first address and the second address into the q empty bytes; after filling the offset between the first address and the second address into the q empty bytes, replacing the p-byte exception instruction opcode with a p-byte jump instruction opcode to obtain the first jump instruction. In this way, finally modifying the exception instruction opcode to a jump instruction opcode can ensure that a jump error will not occur. If the exception instruction opcode is first modified to a jump instruction opcode, and then the offset between the first address and the second address is filled into the q empty bytes, before the offset between the first address and the second address is filled into the q empty bytes, if other threads access the original function, they will directly jump based on the jump instruction and cause an access error.

[0011] In combination with the first aspect, in a possible implementation, the exception instruction is modified into a jump instruction based on the first address and the second address, specifically including: when the offset between the first address and the second address is greater than the preset offset, based on the second address, the instruction in the first function jump island is modified to obtain a third jump instruction, and the address of the first function jump island is the third address; based on the first address and the third address, the exception instruction is modified to obtain a second jump instruction; the second jump instruction is used for the thread to jump to the first function jump island to execute the third jump instruction in the first function jump island when accessing the first original function, and the third jump instruction is used to jump to the patch area to execute the instruction in the first patch function when being executed by the thread. In this way, the jump distance of the jump instruction is limited. When it is not possible to jump in one step, you can first jump from the original function to the function jump island, and then jump from the function jump island to the patch function.

[0012] In conjunction with the first aspect, in one possible implementation, after the modification obtains the third jump instruction, the second jump instruction is then modified. Thus, the instruction in the first function jump island is first modified to obtain the third jump instruction, and then the exception instruction is modified to the second jump instruction. If the exception instruction is first modified to the second jump instruction, and then the instruction in the first function jump island is modified to obtain the third jump instruction, before the instruction in the first function jump island is modified to obtain the third jump instruction, if other threads access the original function, they will directly jump to the jump island based on the second jump instruction, while the third jump instruction in the jump island has not yet been modified, resulting in an access error.

[0013] In conjunction with the first aspect, in one possible implementation, the exception instruction includes a p-byte exception instruction opcode, followed by q empty bytes. Based on the second address, the instruction in the first function jump island is modified to obtain a third jump instruction, specifically including: filling the second address into the data portion of the first function jump island to obtain the third jump instruction; based on the first address and the third address, the exception instruction is modified to obtain a second jump instruction, specifically including: filling the offset between the first address and the third address into the q empty bytes; after filling the offset between the first address and the third address into the q empty bytes, the p-byte exception instruction opcode is replaced with the p-byte jump instruction opcode to obtain the second jump instruction. In this way, the exception instruction opcode is finally modified to the jump instruction opcode, which can ensure that jump errors will not occur. If the exception instruction opcode is first modified to a jump instruction opcode, and then the offset between the first address and the third address is filled into q empty bytes, before the offset between the first address and the third address is filled into q empty bytes, if other threads access the original function, an access error will occur due to direct jump based on the jump instruction.

[0014] In combination with the first aspect, in a possible implementation, when the offset between the first address and the second address is greater than the preset offset, the instruction in the first function jump island is modified based on the second address to obtain a third jump instruction, specifically including: when the offset between the first address and the second address is greater than the preset offset, determining the first function jump island in an idle state from multiple function jump islands; when the first function jump island has not been used, modifying the instruction in the first function jump island based on the second address to obtain the third jump instruction; when the first function jump island has been used before, after waiting for the first function jump island to be unoccupied for a period of time exceeding the first period, modifying the instruction in the first function jump island based on the second address to obtain the third jump instruction. In this way, a function jump island can be allocated to the original function. The allocation method is different for different types of jump islands.

[0015] In conjunction with the first aspect, in one possible implementation, the method further includes: obtaining a second patch function and storing it in a patch area, the second patch function being used to modify the first original function in the program, the address of the second patch function being a fourth address; modifying the data portion of the first function's jump island to the fourth address to obtain a fourth jump instruction, the fourth jump instruction being used to jump to the patch area to execute instructions in the second patch function when executed by the thread. In this way, when repeatedly patching the same function, there is no need to reconstruct the jump instruction or repeatedly construct the instructions in the function jump island; only the data portion of the function jump island needs to be modified, saving steps.

[0016] In conjunction with the first aspect, in one possible implementation, when the storage address of the first patch function address cannot be read into a single cache line, a null byte is added before the storage address of the first patch function address to change the storage address of the first patch function address; wherein, after the change, the storage address of the first patch function address is within the same cache line when it is read into the cache. This ensures that the patch function address can be written into the same cache line at once, avoiding access errors.

[0017] In conjunction with the first aspect, in one possible implementation, the method further includes: when the first thread accesses the exception instruction inserted in the first original function, pausing the first thread from executing the instructions in the first original function; after modifying the exception instruction to a jump instruction based on the offset between the first address and the second address, waking up the first thread and jumping to the patch area via the jump instruction to execute the instructions in the first patch function. In this way, before the jump instruction is modified, the thread accessing the original function will be suspended. After the jump instruction is modified, waking up the suspended thread and jumping to the patch area via the jump instruction to execute the patch function.

[0018] In conjunction with the first aspect, in one possible implementation, the method further includes: after modifying the exception instruction into a jump instruction based on the offset between the first address and the second address, when a second thread accesses the jump instruction inserted into the first original function, controlling the second thread to jump to the patch area via the jump instruction to execute instructions in the first patch function. After the jump instruction is modified, threads accessing the original function will directly jump to the patch area via the jump instruction to execute the patch function.

[0019] In the second aspect, the present application provides a patch execution device, which includes an acquisition module, a patch module and an execution module, wherein the acquisition module is used to acquire a first patch function and store the first patch function in a patch area, the first patch function is used to modify the first original function in the program, the address of the first original function is a first address, and the address of the first patch function is a second address; the patch module is used to insert an exception instruction in front of the first instruction of the first original function, the exception instruction is used for the thread to suspend the execution of instructions in the first original function after executing the exception instruction; the execution module is used to modify the exception instruction into a jump instruction based on the first address and the second address, the jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

[0020] Through this application, the defects of the original function to be patched in the program can be repaired through the patch function. Patching the program can be achieved without restarting the program, which improves the program execution efficiency and patch efficiency. This application inserts an exception instruction in the function header of the original function, and the exception instruction is the critical section. In this way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, so there is no need to trap the operating system kernel to find out whether there are threads accessing instructions in the critical section. In other words, the patch execution method provided by this application does not rely on the kernel. Even if the application software where the program is located does not have permission to access the current operating system, that is, when the current operating system is a third-party operating system, patching the program can be achieved.

[0021] In conjunction with the second aspect, in one possible implementation, the execution module is specifically configured to modify the exception instruction into a first jump instruction based on the offset between the first address and the second address when the offset between the first address and the second address is less than a preset offset. The first jump instruction is used to cause the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function. The jump instruction has a limited jump distance. If a one-step jump is possible, the thread can jump directly from the original function to the patch function.

[0022] In conjunction with the second aspect, in a possible implementation, the exception instruction includes a p-byte exception instruction opcode, and q empty bytes are included after the exception instruction; the execution module is specifically used to: fill the offset between the first address and the second address into the q empty bytes; after filling the offset between the first address and the second address into the q empty bytes, replace the p-byte exception instruction opcode with a p-byte jump instruction opcode to obtain a first jump instruction. In this way, the exception instruction opcode is finally modified to a jump instruction opcode, which can ensure that no jump error occurs. If the exception instruction opcode is first modified to a jump instruction opcode, and then the offset between the first address and the second address is filled into the q empty bytes, before the offset between the first address and the second address is filled into the q empty bytes, if other threads access the original function, they will directly jump based on the jump instruction and cause an access error.

[0023] In combination with the second aspect, in a possible implementation, the execution module is specifically used to: when the offset between the first address and the second address is greater than the preset offset, based on the second address, modify the instruction in the first function jump island to obtain a third jump instruction, and the address of the first function jump island is the third address; based on the first address and the third address, modify the exception instruction to obtain a second jump instruction; the second jump instruction is used for the thread to jump to the first function jump island to execute the third jump instruction in the first function jump island when accessing the first original function, and the third jump instruction is used to jump to the patch area to execute the instructions in the first patch function when being executed by the thread. In this way, the jump distance of the jump instruction is limited. When it is not possible to jump in one step, you can first jump from the original function to the function jump island, and then jump from the function jump island to the patch function.

[0024] In conjunction with the second aspect, in one possible implementation, after the third jump instruction is obtained by modification, the second jump instruction is then modified. In this way, the instruction in the first function jump island is first modified to obtain the third jump instruction, and then the exception instruction is modified to the second jump instruction. If the exception instruction is first modified to the second jump instruction, and then the instruction in the first function jump island is modified to obtain the third jump instruction, before the instruction in the first function jump island is modified to obtain the third jump instruction, if other threads access the original function, they will directly jump to the jump island based on the second jump instruction, and the third jump instruction in the jump island has not yet been modified, resulting in an access error.

[0025] In conjunction with the second aspect, in one possible implementation, the exception instruction includes a p-byte exception instruction opcode, followed by q empty bytes; the execution module is specifically configured to fill the second address into the data portion of the first function jump island to obtain a third jump instruction; the execution module is specifically configured to fill the offset between the first and third addresses into the q empty bytes; after filling the offset between the first and third addresses into the q empty bytes, replace the p-byte exception instruction opcode with a p-byte jump instruction opcode to obtain a second jump instruction. In this way, the exception instruction opcode is finally modified to a jump instruction opcode, which ensures that jump errors will not occur. If the exception instruction opcode is first modified to a jump instruction opcode, and then the offset between the first and third addresses is filled into the q empty bytes, before the offset between the first and third addresses is filled into the q empty bytes, if other threads access the original function, they will directly jump based on the jump instruction and cause an access error.

[0026] In conjunction with the second aspect, in a possible implementation, the execution module is specifically used to: when the offset between the first address and the second address is greater than a preset offset, determine the first function jump island in an idle state from multiple function jump islands; when the first function jump island has not been used, based on the second address, modify the instructions in the first function jump island to obtain a third jump instruction; when the first function jump island has been used before, after waiting, after the first function jump island has not been occupied for a period of time exceeding the first period of time, modify the instructions in the first function jump island based on the second address to obtain a third jump instruction. In this way, a function jump island can be allocated to the original function. The allocation method is different for different types of jump islands.

[0027] In conjunction with the second aspect, in one possible implementation, the acquisition module is further configured to acquire a second patch function and store it in the patch area. The second patch function is used to modify the first original function in the program, and the address of the second patch function is the fourth address. The execution module is further configured to modify the data portion of the first function jump island to the fourth address, thereby obtaining a fourth jump instruction. The fourth jump instruction is used to jump to the patch area to execute the instructions in the second patch function when executed by the thread. In this way, when repeatedly patching the same function, there is no need to reconstruct the jump instruction or repeatedly construct the instructions in the function jump island. Only the data portion of the function jump island needs to be modified, thus saving operational steps.

[0028] In conjunction with the second aspect, in one possible implementation, the execution module is further configured to, when the storage address of the first patch function address cannot be read into a cache line, add a null byte before the storage address of the first patch function address, thereby changing the storage address of the first patch function address; wherein the changed storage address of the first patch function address is within the same cache line when read into the cache. This ensures that the patch function address can be written into the same cache line at one time, thus avoiding access errors.

[0029] In conjunction with the second aspect, in one possible implementation, the patch module is further configured to suspend the first thread from executing instructions in the first original function when the first thread accesses an exception instruction inserted in the first original function; the execution module is further configured to, after modifying the exception instruction to a jump instruction based on the offset between the first address and the second address, wake up the first thread and jump to the patch area via the jump instruction to execute the instructions in the first patch function. In this way, before the jump instruction is modified, the thread accessing the original function will be suspended. After the jump instruction is modified, the suspended thread is awakened and jumps to the patch area via the jump instruction to execute the patch function.

[0030] In conjunction with the second aspect, in one possible implementation, the execution module is further configured to, after modifying the exception instruction into a jump instruction based on the offset between the first address and the second address, control the second thread to jump to the patch area via the jump instruction to execute instructions in the first patch function when the second thread accesses the jump instruction inserted into the first original function. After the jump instruction is modified, threads accessing the original function will directly jump to the patch area via the jump instruction to execute the patch function.

[0031] In a third aspect, the present application provides a computer-readable storage medium for storing computer instructions. When the computer instructions are executed on a patch execution device, the patch execution device executes a patch execution method provided in any possible implementation of the first aspect.

[0032] In a fourth aspect, the present application provides a computer program product. When the computer program product runs on a patch execution device, the patch execution device executes a patch execution method provided in any possible implementation of the first aspect.

[0033] For the beneficial effects of the third to fourth aspects, reference may be made to the description of the beneficial effects in the first aspect, and this application will not go into details here. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] FIG1 is a schematic diagram of inserting a jump instruction provided by an embodiment of the present application;

[0035] FIG2 is a schematic diagram of a software architecture provided in an embodiment of the present application;

[0036] FIG3 is a schematic diagram of a system architecture of a patch execution method provided in an embodiment of the present application;

[0037] FIG4 is a schematic diagram of inserting a nopl instruction into the function header of an original function according to an embodiment of the present application;

[0038] FIG5 is a schematic diagram of modifying a nopl instruction into an abnormal instruction according to an embodiment of the present application;

[0039] FIG6 is a schematic diagram of a method for modifying an abnormal instruction into a jump instruction according to an embodiment of the present application;

[0040] FIG7 is a schematic diagram of modifying an abnormal instruction into a jump instruction according to an embodiment of the present application;

[0041] FIG8 is a schematic diagram of another method of modifying an abnormal instruction into a jump instruction according to an embodiment of the present application;

[0042] FIG9 is a schematic diagram of a function jump island segment provided in an embodiment of the present application;

[0043] 10-12 are schematic diagrams of a group of allocated jump islands provided in an embodiment of the present application;

[0044] FIG13 is a schematic diagram of inserting a function jump island into a bin file / so file according to an embodiment of the present application;

[0045] 14A-14B are schematic diagrams of obtaining a third jump instruction according to an embodiment of the present application;

[0046] FIG15 is a schematic diagram of an original function, a patch function, and the original function code after the patch function is deleted, provided in an embodiment of the present application;

[0047] FIG16 is a flowchart of a patch execution method provided by the present application;

[0048] FIG17 is a flowchart of another patch execution method provided by the present application;

[0049] FIG18 is a schematic diagram of a patch execution device provided by the present application;

[0050] FIG19 is a schematic diagram of an apparatus 1900 provided in an embodiment of the present application. DETAILED DESCRIPTION

[0051] The following is a clear and detailed description of the technical solutions in the embodiments of the present application in conjunction with the accompanying drawings. In the description of the embodiments of the present application, unless otherwise specified, " / " means or, for example, A / B can mean A or B; "and / or" in the text is only a description of the association relationship between related objects, indicating that there can be three relationships, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, in the description of the embodiments of the present application, "multiple" means two or more than two.

[0052] In the following, the terms "first" and "second" are used for descriptive purposes only and should not be understood to imply or suggest relative importance or implicitly indicate the number of the technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. In the description of the embodiments of this application, unless otherwise specified, "plurality" means two or more.

[0053] First, the technical terms involved in this application are explained.

[0054] 1. Procedure

[0055] A program is a single execution of a collection of data. Programs are the foundation of operating system architecture, and computer applications are run by processes. A program can consist of one or more functions, each of which performs different functions. Multiple functions can work together to achieve the program's intended functionality.

[0056] However, a program may encounter defects during execution. For example, if a function fails to perform as expected, it may need to be modified so that it can function as expected. For example, Function A in a program is supposed to perform multiplication, but during execution, it is discovered that it actually performs addition. Function A then needs to be modified so that it can perform multiplication.

[0057] 2. Original function and patch function

[0058] As can be seen from the above introduction to the concept of a program, a program can be composed of one or more functions. Some functions can achieve the expected functionality, while others cannot. Functions in a program that cannot achieve the expected functionality can be called primitive functions. For primitive functions that cannot achieve the expected functionality, these primitive functions need to be modified. Programmers can then rewrite functions based on the expected functionality to be achieved. These rewritten functions that can achieve the expected functionality can be called patch functions. For example, the function A that implements the addition operation mentioned above is the primitive function, and the rewritten multiplication function is the patch function.

[0059] The patch function can be used to replace the original function in the program that cannot achieve the expected function, thereby repairing the defects in the program, so that one or more functions that can achieve the expected function and the patch function can work together to achieve the preset function of the program.

[0060] In the embodiment of the present application, a file containing a patch function may be referred to as a patch file.

[0061] 3. Patch area and critical area

[0062] After obtaining a patch file based on a program defect, the patch file can be stored in a memory area. This memory area is called a patch area. After the patch file is loaded into the patch area, the system automatically assigns an address to the patch file. This address varies for different patch files. When the program is running, it retrieves the correct patch file from the patch area based on this address, loads it, and executes it, thereby fixing the defect in the program.

[0063] As can be seen from the above introduction to original functions and patch functions, patch functions are used to replace original functions in a program that fail to achieve the intended functionality. During program execution, when the original function is reached, it is necessary to skip the original function and jump to the patch area to execute the patch function in the patch file. After the patch file is executed, the program jumps back to continue executing the next function. This process can be called patching. To jump from the original function to the patch file, a jump instruction must be inserted into the function header of the original function. This jump instruction is used to enable the program to automatically skip the original function and jump to the patch area to execute the patch function in the patch file. The area in the program where this jump instruction is inserted can be called a critical section.

[0064] 4. Function jump island segment and function jump island

[0065] From the above introduction, we can see that it is necessary to insert a jump instruction into the function header of the original function so that the program can jump to the patch area to execute the patch function in the patch file.

[0066] Instruction jumps are subject to distance restrictions. When the offset between the address of the jump instruction and the address of the patch file in the patch area is less than or equal to the preset offset, the instruction can jump to the patch area normally and execute the patch function in the patch file.

[0067] When the offset between the address of the jump instruction and the address of the patch file in the patch area is greater than the preset offset, the instruction will jump incorrectly and cannot jump to the patch area.

[0068] To address the issue of jump errors caused by excessive jump distance, an intermediate jump region can be introduced. A jump instruction can first jump to the intermediate jump region, and then from the intermediate jump region to the patch region. This shortens the jump distance of a jump instruction. Here, the intermediate jump region can be called a function jump island.

[0069] The system can have multiple function jump islands to allow multiple jump instructions to jump simultaneously. Function jump islands can be divided from a function jump island segment. A function jump island segment can be divided into several function jump islands, with different function jump islands having different addresses. A function jump island segment can refer to a special segment in an ELF file.

[0070] FIG1 shows a schematic diagram of inserting a jump instruction.

[0071] As shown in Figure 1, the original function may include multiple instructions, and the number of bytes of each instruction may be the same or different. For example, the first instruction of the original function is 1 byte, the second instruction of the original function is 2 bytes, the third instruction of the original function is 2 bytes, the fourth instruction of the original function is 3 bytes, the fifth instruction of the original function is 1 byte, the sixth instruction of the original function is 2 bytes, and the seventh instruction of the original function is 1 byte.

[0072] Since the original function cannot achieve the expected functionality, it is necessary to skip the original function and execute the patch function in the patch area to achieve the expected functionality. To achieve the function jump, a jump instruction needs to be inserted. If the byte length of the jump instruction is 5 bytes.

[0073] As shown in Figure 1, to insert a 5-byte nopl instruction, you can count multiple instructions containing at least 5 bytes, starting with the first instruction of the original function, as the insertion area for the nopl instruction. If the first, second, and third instructions of the original function are exactly 5 bytes, then you can define these instructions as a critical section and modify them to nopl instructions. After determining the jump destination, you can modify the nopl instructions to jump instructions. The area containing the first, second, and third instructions of the original function is also called a critical section. During the patch activation period, if other threads access instructions within the critical section, there is a possibility that instructions within the critical section may be partially modified. Accessing partially modified instructions will result in an error. For example, if the second instruction of the original function is being modified, and another thread calls the second instruction of the original function, the second instruction of the original function is no longer a complete instruction, resulting in an access error. Therefore, during the patch activation period, other processes cannot access instructions within the critical section.

[0074] To check whether other threads are accessing instructions within the critical section, you need to trap into the operating system kernel and use kernel mechanisms to query. If another thread accesses instructions within the critical section after n consecutive queries, the patch activation fails and ends. If no other threads access instructions within the critical section after n consecutive queries, the patch activation can continue.

[0075] However, the aforementioned trap in the operating system kernel, which uses kernel mechanisms to query whether other threads are accessing instructions within the critical section, requires that the application software containing the currently patched program has permission to access the current operating system. For example, if the current operating system and the application software containing the currently patched program are not developed by the same vendor, the current operating system can be considered a third-party operating system relative to the application software containing the currently patched program.

[0076] If the application software where the currently patched program is located does not have permission to access the current operating system, the application software where the currently patched program is located cannot fall into the kernel to query whether other threads access instructions in the critical section, which will cause the patch to be unable to proceed and cause the patch to fail.

[0077] Based on this, in order to ensure that the patch can be executed normally and activated successfully even when the application software containing the patched program does not have permission to access the current operating system, this application provides a patch execution method, which includes the following steps:

[0078] Step 1: Determine the original function to be patched in the program, and insert a nopl instruction before the first instruction of the original function.

[0079] Step 2: Modify the nopl instruction to an exception instruction. In this way, all threads accessing the original function will be suspended and in a waiting state. For example, the first thread will be suspended and in a waiting state.

[0080] Step 3: Load the patch function into the patch area and assign an address to the patch function.

[0081] Step 4: Calculate the offset between the original function address and the patch function address. Based on the offset, modify the exception instruction to a jump instruction. The first thread can then be awakened and redirected to the patch function using the jump instruction to execute the patch function.

[0082] In some embodiments, the jump distance of a jump function has an upper limit. If the upper limit is exceeded, the jump will fail. Therefore, before modifying the abnormal instruction into a jump instruction, it is necessary to determine whether the offset between the original function address and the patch function address exceeds the preset offset.

[0083] If the offset between the original function address and the patch function address is less than or equal to a preset offset, the original function can be directly jumped to the patch function. The exception instruction can be modified based on the offset between the original function address and the function jump island address to obtain a first jump instruction. After that, the first thread can be awakened and the first thread can jump to the patch function through the first jump instruction and execute the patch function.

[0084] If the offset between the original function address and the patch function address is greater than a preset offset, and a direct jump from the original function to the patch function is not possible, then the exception instruction can be modified based on the offset between the original function address and the function jump island address using an intermediate jump area (also known as a function jump island) to obtain a second jump instruction. The instructions in the function jump island are then modified based on the patch function address to obtain a third jump instruction. The first thread can then be awakened and can jump to the function jump island using the second jump instruction. The first thread can then jump to the patch function based on the third jump instruction and execute the patch function.

[0085] In step 4, the suspended thread can be awakened and jump to the patch function through the above method to execute the patch function.

[0086] Through this application, firstly, the defects of the original function to be patched in the program can be repaired through the patch function. Secondly, the program can be patched without restarting the program, which improves the program execution efficiency and patch efficiency. Thirdly, this application inserts a nopl instruction in the function header of the original function, and the nopl instruction is the critical section. In step 2, the nopl instruction is modified to an exception instruction. In this way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, so there is no need to trap the operating system kernel to check whether there are threads accessing instructions in the critical section. In other words, the patch execution method provided by this application does not rely on the kernel. Even if the application software where the program is located does not have permission to access the current operating system, that is, the current operating system is a third-party operating system, the program can be patched.

[0087] FIG2 is a schematic diagram of a software architecture provided by this application.

[0088] The program code of the patch execution method mentioned in this application (which may be referred to as patch management Lib) can be deployed on the upper layer of the operating system.

[0089] In one possible implementation, as shown in Figure 2, the patch management library can be deployed in the application software. The application software can then use the method provided by this application to patch the program in the application software based on the patch management library pre-installed in the application software, thereby repairing defects in the program in the application software.

[0090] In other possible implementations, Patch Management Lib may not be deployed within the application software; instead, Patch Management Lib may exist as a separate functional module. When the application software needs to patch a program within the application software using the method provided in this application, the application software can invoke Patch Management Lib through an interface to patch the program within the application software using the method provided in this application, thereby repairing defects in the program within the application software.

[0091] Patch management Lib can include patch function loading, patch function activation, patch function deletion and other functions.

[0092] Since the patch execution method provided by this application does not need to rely on the operating system kernel, the operating system where the application software is located can be a third-party operating system (third-party OS), and the operating system where the application software is located can also be a third-party container as a service (CaaS), and third-party CaaS can also be called a third-party virtual operating system. The third-party virtual operating system is similar to the third-party operating system in that the application software does not have permission to access the kernel of the third-party virtual operating system. This application is also applicable to non-third-party OS and non-third-party CaaS.

[0093] The hardware layer may include a central processing unit (CPU), read-only memory (ROM), random access memory (RAM), mips, etc.

[0094] The CPU can be used to fix defects in the program based on the patch management library, and the ROM and RAM can be used to store data such as patch functions.

[0095] FIG3 is a schematic diagram of a system architecture of a patch execution method provided by this application.

[0096] As shown in FIG3 , the system may include a host end and a target end.

[0097] The host includes a patch tool that generates a patch file based on program defects. This patch file is then used to fix the program defects. Specifically, the code can be modified based on the program defects. The modified code functions, newly added code functions, data structures, and other information can be extracted and placed in a separate file. This is then compiled to generate the corresponding OBJ file. The patch tool generates the patch file and a new global symbol information file based on the original software code's global symbol information file and OBJ file.

[0098] The Target side includes the main control board, business board, and software warehouse. The main control board, business board, and software warehouse can be understood as software modules.

[0099] The software repository is used to store patch files. After the patch tool generates a patch file, the host can send the patch file to the software repository, where it can be stored. Alternatively, the user can use system operations to store the patch file generated by the host in the software repository. The software repository can store multiple different patch files.

[0100] The main control board can include a patch management module and a software loading module. The business board can include a patch running module and a patch management lib.

[0101] The terminal may send a patch operation command to the patch management module, wherein the patch operation command is used to request to repair defects in the program based on the patch function, for example, downloading a patch file and downloading the patch file to a service board.

[0102] After receiving the patch operation command sent by the terminal, the patch management module parses the patch operation command and sends a patch file download instruction to the software loading module.

[0103] After receiving the patch file download instruction, the software loading module can respond to the patch file download instruction by downloading the patch file from the software repository and sending the patch file to the patch management library. The patch management library can save the patch file in the patch area. Later, when patching, the patch file can be obtained from the patch management library and the interface can be called to perform the patching operation.

[0104] The patch management module is further configured to send a patch file execution instruction to the patch execution module, wherein the patch file execution instruction is configured to instruct the patch execution module to execute a patching operation.

[0105] After receiving the patch file execution instruction, the patch execution module can respond to the patch file execution instruction by calling the capabilities and patch files provided by the patch management lib through the interface to patch the program to repair defects in the program.

[0106] First, let's introduce how this application inserts a nopl instruction into the function header of the original function.

[0107] Before inserting the nopl instruction, it is necessary to determine the defects in the program. The program may be composed of multiple functions, and the original function to be patched can be determined based on the defects in the program.

[0108] It should be noted that there may be multiple primitive functions to be patched in a program. This application only uses one primitive function to be patched as an example for illustration and does not constitute a limitation.

[0109] FIG4 (a) shows a schematic diagram of the program code of the original function to be patched.

[0110] The original function contains multiple instructions, among which the instruction "bf 00 00 00 00 mov $0x0, %edi" is the first instruction of the original function.

[0111] After the original function is determined, a nopl instruction is inserted into the function header of the original function. That is, a nopl instruction is inserted before the first instruction of the function.

[0112] FIG4( b ) shows a schematic diagram of a program code in which a nopl instruction is inserted into the function header of the original function.

[0113] For example, the nopl instruction may be “0x0f1f440000.” The nopl instruction is a no-operation instruction and does not affect the function.

[0114] As shown in (b) of FIG4 , the present application inserts a nopl instruction into the function header of the original function, and the nopl instruction is the critical section. There is only one nopl instruction in the critical section.

[0115] For example, under the x86 CPU, the spec file (x86_nop.spec) is designed as follows:

[0116] *invoke_as:

[0117] %{! fwpa*:%{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}%{! S:-o%|.s|

[0118] sed-i s / ^\\\t.cfi_startproc$ / &\\n\.byte\0xf,0x1f,0x44,0x0,0x0 / g%ms|

[0119] as %(asm_options) %ms %A}}

[0120] As shown in (c) of FIG4 , by using the -specs=x86_nop.spec compilation option, a 5-byte nopl instruction is inserted into the function header during the compilation process. The box in (c) of FIG4 shows the nopl instruction.

[0121] To prevent other threads from accessing the critical section during the patching process, the nopl instruction within the critical section must be modified to an exception instruction. When other threads attempt to access the original function, they are immediately suspended by the exception instruction and placed in a pending state. After the jump instruction is prepared, the suspended thread is awakened, allowing it to access the patched function.

[0122] Figure 5 (a) shows a schematic diagram of the instruction set in the original function. For example, the first instruction in the original function is "mov $0x0, %edi," and the second instruction is "sub $0x8, %rsp." The original function may also include more instructions, which are not shown in Figure 5 (a).

[0123] Figure 5(b) shows a schematic diagram of the instruction set after the nopl instruction is inserted into the function header of the original function. As shown in Figure 5(b), a nopl instruction can be inserted before the first instruction "mov $0x0, %edi".

[0124] When loading and activating the patch function, the nopl instruction can be modified to an exception instruction.

[0125] Figure 5(c) shows a schematic diagram of an instruction set after the nopl instruction is modified into an abnormal instruction. For example, the abnormal instruction may be "0xf4".

[0126] In some embodiments, a nopl instruction may include m bytes, and the critical section also includes m bytes. The m bytes in the nopl instruction are all empty. When modifying the nopl instruction to an exception instruction, the first p bytes in the critical section may be modified to the exception instruction, and the remaining q bytes in the critical section may be left empty to be subsequently filled with the operand of a jump instruction.

[0127] For example, m can be 5, p can be 1, and q can be 4. When inserting the nopl instruction in the original function header, a 5-byte empty instruction can be inserted. When modifying the nopl instruction to an exception instruction, the exception instruction can be placed in the first of the five empty bytes in the critical section. For example, the oxf4 instruction can be placed in the first of the five empty bytes. The last four bytes of the critical section are left empty to allow for the subsequent insertion of the jump instruction operand.

[0128] In this way, the present application inserts a nopl instruction into the function header of the original function, and the nopl instruction becomes the critical section. The nopl instruction is modified into an exception instruction, so there is only one exception instruction in the critical section. In this way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, so there is no need to trap into the operating system kernel to find out whether there are threads accessing instructions in the critical section. In other words, the patch execution method provided by the present application does not rely on the kernel, and can be patched even if the application software where the program is located does not have permission to access the current operating system, that is, the current operating system is a third-party operating system.

[0129] The application software can load and activate the patch function. After the jump instruction is prepared, the suspended thread is awakened, so that the thread accessing the original function can jump to the patch area to execute the patch function. In this way, the thread running the patch function can achieve the expected function that the original function cannot achieve.

[0130] Before waking up the suspended thread, in order to enable the thread to jump to the patch function, the exception instruction needs to be modified into a jump instruction, so that the suspended thread can jump to the patch function through the jump instruction.

[0131] FIG6 is a schematic diagram showing a method of modifying an abnormal instruction into a jump instruction.

[0132] S601: The critical section includes m bytes, and the first p bytes of the m bytes are modified into abnormal instructions.

[0133] When loading and activating the patch function, the nopl instruction can be modified to an exception instruction. This prevents the thread in the critical section from being suspended when other threads access the critical section during the patch function activation process. After the function is successfully activated, the suspended thread can be activated again, allowing the reactivated thread to execute the patch function.

[0134] The nopl instruction inserted into the original function header can contain m bytes, so the critical section also contains m bytes. The m bytes in the nopl instruction are all empty. When modifying the nopl instruction to an exception instruction, the first p bytes in the critical section can be modified to the exception instruction, for example, the exception instruction can be oxf4. The remaining q bytes in the critical section can be left empty to be filled with the operand of the jump instruction.

[0135] For example, m can be 5, p can be 1, and q can be 4. When inserting the nopl instruction in the original function header, a 5-byte empty instruction can be inserted. When modifying the nopl instruction to an exception instruction, the exception instruction can be placed in the first of the five empty bytes in the critical section. For example, the oxf4 instruction can be placed in the first of the five empty bytes. The last four bytes of the critical section are left empty to allow for the subsequent insertion of the jump instruction operand.

[0136] S602: After the patch function is activated, first fill the operand of the jump instruction into the last q bytes of the m bytes, and then modify the first p bytes of the m bytes into the operation code of the jump instruction to obtain the jump instruction.

[0137] The operand of a jump instruction is also called the jump distance of the jump instruction. The jump distance of the jump instruction can be obtained by subtracting the source address of the jump instruction from the destination address of the jump instruction.

[0138] For example, the source address of the jump instruction may be the original function address, and the destination address of the jump instruction may be the patch function address.

[0139] In some embodiments, a jump instruction has an upper limit on its jump distance. If the jump distance exceeds this limit, the jump will fail. To address the issue of jump errors caused by excessive jump distance, a function jump island can be introduced. The jump instruction can first jump to the function jump island, and then jump from the function jump island to the patch area. This shortens the jump distance of the jump instruction.

[0140] In some embodiments, there may be multiple primitive functions, and a function jump island may need to be allocated to each of the multiple primitive functions. Different primitive functions use different function jump islands.

[0141] Therefore, before modifying the abnormal instruction into a jump instruction, it is necessary to determine whether the offset between the original function address and the patch function address exceeds the preset offset.

[0142] If the offset between the original function address and the patch function address is less than or equal to a preset offset, the original function can be jumped directly to the patch function. The operand of the jump instruction in the last q bytes can be the offset between the original function address and the patch function address. Here, the first p bytes of opcode and the last q bytes of operand can be referred to as the first jump instruction.

[0143] FIG7 shows a schematic diagram of modifying an abnormal instruction into a jump instruction.

[0144] For the description of (a), (b) and (c) in FIG. 7 , reference may be made to the description of (a), (b) and (c) in FIG. 5 .

[0145] If the offset between the original function address and the patch function address is less than or equal to the preset offset, after determining the operand for the jump instruction to be filled in the last q bytes, as shown in (d) of Figure 7, this operand can be filled in the last q bytes of the critical section. The operand can be represented as "offset". Then, the opcode of the exception instruction in the first p bytes of the critical section is replaced with the opcode of the jump instruction, which can be represented as "0xe9".

[0146] As shown in (e) of Figure 7, the jump instruction opcode in the first p bytes of the critical section and the jump instruction operand in the last q bytes of the critical section can be collectively referred to as the first jump instruction. The first jump instruction is used to directly jump to the patch section to execute the patch function.

[0147] Optionally, when constructing the first jump instruction, it is also necessary to first modify the operand of the jump instruction of the last q bytes in the critical section. Finally, replace the abnormal instruction of the first p bytes in the modified critical section with the operation code of the jump instruction. If the operation code is modified before the operand, there is no abnormal instruction in the critical section at this time. If other threads access the critical section at this time, they will not be suspended, and the operand of the first jump instruction has not been filled in at this time, and other threads cannot jump, resulting in a jump failure. Based on this, the operand of the jump instruction of the last q bytes in the critical section can be modified first, and finally the abnormal instruction of the first p bytes in the modified critical section can be replaced with the operation code of the jump instruction.

[0148] If the offset between the original function address and the patch function address is greater than a preset offset, it is impossible to jump directly from the original function to the patch function. In this case, a function jump island can be introduced, and a function jump island can be allocated to the original function, and the address of the function jump island can be determined. The operand of the jump instruction in the last q bytes can be the offset between the original function address and the function jump island address. Here, the first p bytes of opcode and the last q bytes of operand can be referred to as the second jump instruction.

[0149] FIG8 shows another schematic diagram of modifying an abnormal instruction into a jump instruction.

[0150] Figure 8 is similar to Figure 7 . For the descriptions of (a), (b), and (c) in Figure 8 , refer to the descriptions of (a), (b), and (c) in Figure 7 . The difference is that the operands in the last q bytes of the critical section in Figure 8 (d) are different from the operands in the last q bytes of the critical section in Figure 7 (d). In Figure 8 (d), the operands in the last q bytes of the critical section are the offset between the original function address and the address of the function jump island. In Figure 7 (d), the operands in the last q bytes of the critical section are the offset between the original function address and the address of the patch function. In Figure 8 (e), the jump instruction opcode in the first p bytes of the critical section and the jump instruction operand in the last q bytes of the critical section can be collectively referred to as the second jump instruction. The second jump instruction is used to jump to the function jump island allocated for the original function.

[0151] Optionally, when constructing the second jump instruction, it is also necessary to first modify the operand of the jump instruction of the last q bytes in the critical section. Finally, replace the abnormal instruction of the first p bytes in the modified critical section with the opcode of the jump instruction. If the opcode is modified before the operand, there will be no abnormal instruction in the critical section at this time. At this time, if other threads access the critical section, they will not be suspended, and the operand of the second jump instruction has not been filled in at this time, and other threads will not be able to jump, resulting in a jump failure. Based on this, the operand of the jump instruction of the last q bytes in the critical section can be modified first, and finally the abnormal instruction of the first p bytes in the modified critical section can be replaced with the opcode of the jump instruction.

[0152] Next, we will introduce how to allocate function jump islands for the original functions.

[0153] First, the function jump island segment can be divided into n function jump islands of the same size, and each of the n function jump islands is numbered, with different function jump islands having different numbers.

[0154] Function jump islands can be divided into two types: full function jump islands and unfull function jump islands. A full function jump island means that the island has been used at least once by the original function. An unfull function jump island means that the island has not been used by any original function.

[0155] A full function jump island can be divided into two usage states: idle state and occupied state.

[0156] Function jump islands that are not full can also have two usage states: idle state and occupied state.

[0157] The idle state may mean that no original function currently occupies a full function jump island or an unfull function jump island in this state. The occupied state may mean that an original function currently occupies a full function jump island or an unfull function jump island in this state.

[0158] For example, n may be 6.

[0159] FIG9 shows a schematic diagram of a function jump island segment.

[0160] The function jump island segment includes function jump island 1, function jump island 2, function jump island 3, function jump island 4, function jump island 5 and function jump island 6.

[0161] There may also be multiple function jump island segments, which is not limited in this application.

[0162] This application provides the following two but not limited to the following methods for allocating function jump islands to original functions.

[0163] Method 1: When allocating a function jump island to the original function, start traversing from function jump island 1. Determine the usage status of function jump island 1. If the usage status of function jump island 1 is idle, then determine the type of function jump island 1. If the type of function jump island 1 is not full, then function jump island 1 can be directly allocated to the original function. If the type of function jump island 1 is full, you need to wait for a period of time. If function jump island 1 is not occupied during this period, then function jump island 1 can be allocated to the original function. The purpose of waiting for a while is that although the full function jump island is not occupied, it may continue to be accessed by other threads. Other threads may also jump to the patch area through function jump island 1 to execute the patch function that is about to be unloaded. Therefore, it is necessary to wait for a while to confirm that the full function jump island is not accessed by other threads, and then unload the patch function and allocate the full function jump island to the original function for use, so as to avoid other threads accessing the patch function that is about to be unloaded through the full function jump island and directly modifying the data part in the full function jump island, which will cause access errors of other threads.

[0164] If the allocation of function jump island 1 fails, then you can traverse function jump island 2 and allocate function jump island 2 to the original function in the same way as function jump island 1.

[0165] As shown in Figure 10, according to the above method, each function jump island is traversed in order from front to back until an available function jump island is assigned to the original function, and then the traversal stops. For example, the function jump island assigned to the original function may be function jump island 3. Function jump island 3 may be a function jump island in the full state, or it may be a function jump island in the unfull state.

[0166] In some embodiments, if all function jump islands are traversed and no function jump island is successfully allocated, the traversal can be repeated r times. If no function jump island is successfully allocated after r consecutive traversals, the allocation is considered a failure. The user can reactivate the patch function and then follow the above steps to allocate function jump islands.

[0167] Method 2: If no function jump islands are allocated in the function jump island segment, set the traversal index uiCurPos to 0. Define the traversal index uiCurPos as the starting position of the traversal function jump islands. When a function jump island in the function jump island segment is allocated, increment the traversal index uiCurPos by 1. When the traversal index uiCurPos exceeds n-1, it is reset to 0.

[0168] If the function jump island segment is not full, it means that there are incomplete function jump islands in the function jump island segment. If the traversal index uiCurPos is s, and s is less than n-1, it means that the function jump islands between function jump island s+1 and function jump island n-1 are all incomplete function jump islands. In this case, function jump island s+1 can be directly assigned to the original function. At the same time, the traversal index uiCurPos is increased by 1, and the traversal index uiCurPos is s+1.

[0169] As shown in FIG11 , according to the above method, if the traversal index uiCurPos is 2, it means that function jump island 3 to function jump island 6 are all function jump islands in an incomplete state, and function jump island 3 can be directly allocated to the original function for use.

[0170] If the function jump island segment is full, it means that there are no unfilled function jump islands in the segment, and the segment is filled with full function jump islands. If the traversal index uiCurPos is s, traverse the function jump islands starting from function jump island s+1 until traversing to function jump island n-1. If a function jump island is successfully allocated between function jump island 1 and function jump island n-1, the process ends. Otherwise, the traversal index uiCurPos is set to 0, and the function jump islands are traversed starting from function jump island 1 until traversing to function jump island n-1.

[0171] When the function jump island segment is full, when traversing each function jump island, if the function jump island is idle, it is necessary to wait for a while, and then allocate the function jump to the original function for use after ensuring that it is not accessed by other threads.

[0172] In some embodiments, if all function jump islands are traversed and no function jump island is successfully allocated, the traversal can be repeated r times. If no function jump island is successfully allocated after r consecutive traversals, the allocation is considered a failure. The user can reactivate the patch function and then follow the above steps to allocate function jump islands.

[0173] As shown in Figure 12, according to the above method, if the traversal index uiCurPos is 2, each function jump island is traversed in sequence starting from function jump island 3 until an available function jump island is assigned to the original function, and then the traversal stops. For example, the function jump island assigned to the original function may be function jump island 5. Function jump island 5 may be a function jump island in the full state.

[0174] The allocation of function jump islands to the original function is not limited to the above two methods, and the allocation of function jump islands to the original function can also be based on other methods, which is not limited in this application.

[0175] After determining the function jump island allocated to the original function, the address of the function jump island can be obtained.

[0176] For example, you can add the link option -Ttrampoline.lds to the source code build project to generate a jump island when linking each bin file / so file. You can design the following link script (trampoline.lds):

[0177] The page alignment and function jump island size in the linker script can be changed according to the actual situation. For example, if the linker script is 4K aligned, the above code segment can be filled with 4096; a function jump island unit is 16 bytes, so the function jump island size determines the number of function jump islands.

[0178] Figure 13 shows a schematic diagram of inserting a function jump island into a bin file / so file. The box in Figure 13 shows the code implementation of inserting the function jump island.

[0179] S603: Wake up the suspended first thread.

[0180] S604: The first thread jumps to the patch area through the jump instruction in the original function and executes the patch function.

[0181] If the offset between the original function address and the patch function address is less than or equal to a preset offset, the first thread can jump directly from the original function to the patch function. In the embodiment of Figure 7, after the exception instruction is modified to a first jump instruction, the first thread can use the first jump instruction in the original function to jump to the patch area and execute the patch function. The first thread does not need to execute the original function. By executing the patch function, the first thread can achieve the expected functionality that the original function could not achieve.

[0182] After the first jump instruction is modified, for example, by changing the exception instruction in the first p bytes of the critical section to a jump instruction opcode, the suspended first thread can be awakened. The first thread can then jump to the patched area and execute the patched function. This eliminates the need for the first thread to execute the original function. By executing the patched function, the first thread can achieve the desired functionality that the original function could not achieve.

[0183] When the offset between the original function address and the patch function address is greater than the preset offset, it is impossible to jump directly from the original function to the patch function. In this case, a function jump island can be introduced and allocated to the original function.

[0184] Based on the description of the embodiment in FIG8 , the exception instruction is modified into a second jump instruction, allowing the thread to jump from the original function to the function jump island. However, it is also necessary to jump from the function jump island to the patch area to execute the patch function. Therefore, before waking up the first thread, it is necessary to construct the jump instruction in the function jump island. The jump instruction in the function jump island can be called the third jump instruction.

[0185] Next, we will introduce how to construct a function jump island.

[0186] First, we need to construct the instruction format of the jump island.

[0187] The instruction format of the function jump island is:

[0188] 0x0:

[0189] 0x2:

[0190] 0x8:

[0191] The above shows an instruction format of a function jump island. The instruction format of the function jump island can be constructed before the patch or during the patch process, and this application does not limit this. The function jump island can include three instructions. The address of the first instruction can be "0x0". The address of the second instruction can be "0x2". The address of the third instruction can be "0x8". In some embodiments, the third instruction can be called the data part of the function jump island. The function jump island can be composed of 16 bytes. Among them, the first instruction can be composed of two bytes. The second instruction can also be composed of 6 bytes. The third instruction can be composed of 8 bytes.

[0192] The first instruction is a blank instruction, used to fill in the patch function address so that it is aligned with the cache line. The second instruction is the jump instruction part of the function jump island. The third instruction is the data part, for example, storing the patch function address.

[0193] The jump island segment can be divided into several 16-byte function jump islands. The data portion of the jump island segment is page-aligned, so the storage address of the data portion in each function jump island must be 16-byte aligned. The function jump island layout is designed with the first two bytes left blank, a 6-byte jump instruction, and an 8-byte data portion, so that the data portion address is 8-byte aligned (on a 64-bit boundary). When reading and writing the data portion at the 8-byte aligned storage address, the 8-byte data portion is in the same cache line, allowing for a one-time write.

[0194] This way, the first instruction has a two-byte blank portion, ensuring that the data portion of the function jump island is 8-byte aligned. For example, when an x86 CPU writes data to a cache line, it writes in 8-byte units. The 8-byte data portion of the function jump island is aligned with the 8-byte cache line, ensuring that the patch function address is written to the cache line all at once, eliminating the need for multiple writes, thus ensuring atomicity.

[0195] If the patch function address portion of the function jump island is not 8-byte aligned with the cache line, for example, if there is no 2-byte blank instruction in the function jump island, then the 6-byte second instruction and the first two bytes of the patch function address portion of the third instruction will be written together into one cache line. The remaining 6 bytes of the patch function address portion of the third instruction will be written into another cache line the next time. This means that the patch function address portion of the third instruction will not be written together into the cache line. For example, in the scenario of repeatedly patching the same original function, only the patch function address portion in the function jump island needs to be modified. After the patch function address portion in the function jump island is modified, the CPU needs to write the patch function address portion in the function jump island into the cache line. If another thread obtains the first two bytes of the patch function address from the third instruction before the remaining six bytes of the patch function address are written into the cache line, mistakenly identifying them as the address of the patch function, the other thread can access the patch function based on these two bytes. However, these two bytes are not the actual address of the patch function, resulting in an access error. Therefore, it is necessary to add a blank instruction to the function jump island to ensure that the CPU aligns the patch function address to 8 bytes when writing it into the cache line.

[0196] It should be noted that the above-mentioned cache line unit byte number, e.g., 8 bytes, is provided for illustrative purposes only and is not intended to be limiting. Based on actual circumstances, a certain number of blank bytes can be added to ensure that the starting address and the ending address of the data portion of a function jump island are within the same cache line. This ensures that the data portion of a function jump island can be written to the same cache line at once.

[0197] For example, the unit byte number of the cache line is the first byte number, the byte number of the instruction address in the function jump island may be the second byte number, and the byte number of the patch function address in the function jump island may be the third byte number.

[0198] When the first byte is an integer multiple of the sum of the second and third byte, or the sum of the second and third byte is an integer multiple of the first byte being an integer multiple of the second byte, there is no need to add a blank instruction in the function jump island. In this case, the address of the patch function with the third byte can be written to the cache at once. For example, if the first byte is 16, the second byte is 8, and the third byte is also 8.

[0199] When the first byte number is not an integer multiple of the sum of the second and third byte numbers, a blank instruction must be added to the function jump island. The byte number of the blank instruction is the fourth byte number, so that the first byte number is an integer multiple of the sum of the second, third, and fourth byte numbers, or the sum of the second, third, and fourth byte numbers is an integer multiple of the first byte number. For example, if the first byte number is 16, the second byte number is 6, and the third byte number is 8, then at least 2 bytes of blank instructions must be added to the function jump island. Alternatively, 18 bytes of blank instruction or 34 bytes of blank instruction may be added, and this application does not impose any further restrictions on this.

[0200] Exemplarily, as shown in FIG14A , the CPU accesses and writes into a cache line, where the unit byte number of the cache line is a first byte number, for example, the first byte number may be 16.

[0201] The CPU sequentially accesses and stores data in cache lines using a fixed first byte number. For example, the CPU can access data from the 0th byte to the 15th byte, a total of 16 bytes, and store them in cache line 1. The CPU can access data from the 16th byte to the 31st byte, a total of 16 bytes, and store them in cache line 2. The CPU can access data from the 32nd byte to the 47th byte, a total of 16 bytes, and store them in cache line 3. The CPU can access data from the 48th byte to the 63rd byte, a total of 16 bytes, and store them in cache line 4. And so on, the CPU can access and store data in cache lines sequentially.

[0202] For example, the storage address of the jump instruction in the function jump island includes 6 bytes in total, which are located between bytes 24 and 29. The storage address of the patch function address in the function jump island includes 8 bytes in total, which are located between bytes 30 and 37.

[0203] As shown in Figure 14A , during the second address fetch, the CPU writes the 6-byte jump instruction address and the first two bytes of the patch function address into cache line 2. During the third address fetch, the CPU writes the last 6 bytes of the patch function address into cache line 3. However, this causes the patch function address to be written twice, into different cache lines, resulting in an access error.

[0204] Based on this, in order to ensure that the storage address of the 8-byte patch function address is written into one cache line at a time, a certain number of null bytes can be added before the storage address of the patch function address, so that the address part of the 8-byte patch function address can be written into one cache line at a time.

[0205] As shown in Figure 14B, two empty bytes can be added after the 23rd byte, so that the 24th and 25th bytes are empty bytes. The storage address of the 6-byte jump instruction in the function jump island is stored between the 26th and 31st bytes. The storage address of the 8-byte patch function address in the function jump island is stored between the 32nd and 39th bytes. After adding the blank bytes, as shown in Figure 14B, the CPU will write the storage address of the 6-byte jump instruction into cache line 2 when it takes the address for the second time. When the CPU takes the address for the third time, it will write the storage address of the 8-byte patch function address into cache line 3. In this way, it is ensured that the storage address of the 8-byte patch function address is written into one cache line at a time.

[0206] You can modify the instructions of the function jump island. First, fill in the address of the patch function (addr) in the data part of the function jump island, and fill in the long jump instruction (0xff 25 00 00 00 00) in the jump instruction part. When uninstalling the patch function, you only need to roll back the last 8 bytes of the jump island unit.

[0207] The thread can jump to the patch area based on the function jump island to execute the patch function.

[0208] The modified instructions are as follows:

[0209] 0x0: 0x00 00;

[0210] 0x2: 0xff 25 00 00 00 00;

[0211] 0x8:addr

[0212] Here, "0xff 25 00 00 00 00" can be understood as a jump instruction. addr can be the address of the patch function. The jump instruction is used to read the patch function address and jump to the patch function based on the patch function address.

[0213] The modified instruction in the above function jump island can be called a third jump instruction.

[0214] Optionally, the third jump instruction may be modified before the second jump instruction.

[0215] Optionally, some steps of modifying the second jump instruction can also be completed before the step of modifying the third jump instruction. It is only necessary to replace the abnormal instructions of the first p bytes in the critical section with the operation code of the jump instruction when modifying the second jump instruction, and then execute the step after the third jump instruction is modified.

[0216] As shown in FIG15 , FIG15 shows a schematic diagram of the code implementation of the original function, the patch function, and the original function after the patch function is deleted.

[0217] In Figure 15, the code segment before patching corresponds to the original function code segment. The code segment after patching corresponds to the patch function code segment. The code segment after the patch function is deleted also corresponds to the original function code segment.

[0218] If the offset between the original function address and the patch function address is greater than a preset offset, after the second and third jump instructions are constructed—that is, when the second jump instruction is finally obtained—the first thread can be awakened by replacing the first p bytes of the exception instruction in the critical section with the jump instruction's opcode. The first thread can then use the second jump instruction to jump to the function jump island. Then, using the third jump instruction in the function jump island, the first thread can jump to the patch area and execute the patch function. This way, the first thread can execute the patch function without having to execute the original function.

[0219] Optionally, after the first jump instruction or the second jump instruction and the third jump instruction are constructed, a second thread may access the critical section, and the second thread is a thread that is not suspended.

[0220] In one possible implementation, the second thread can jump to the patch area and execute the patch function through the first jump instruction. In this way, the second thread does not need to execute the original function. By executing the patch function, the second thread can achieve the expected function that cannot be achieved by executing the original function.

[0221] In another possible implementation, the second thread can jump to the function jump island using the second jump instruction in the original function. Then, the second thread can jump to the patch area and execute the patch function using the third jump instruction in the function jump island. In this way, the second thread does not need to execute the original function. By executing the patch function, the second thread can achieve the desired functionality that the original function could not achieve.

[0222] FIG16 is a flow chart of a patch execution method provided by the present application.

[0223] S1601: Determine the original function to be patched in the program.

[0224] There may be multiple primitive functions to be patched in a program, and different primitive functions correspond to different patch files. This application only uses patching a primitive function as an example to illustrate.

[0225] S1602. Insert an m-byte nopl instruction into the function header of the original function.

[0226] Insert an m-byte nopl instruction into the function header of the original function, that is, insert an m-byte nopl instruction before the first instruction of the original function.

[0227] The m bytes may be referred to as a critical section.

[0228] For details, please refer to the description in the embodiments of FIG. 4 (a) and FIG. 4 (b).

[0229] S1603. Modify the nopl instruction to an abnormal instruction.

[0230] To prevent other threads from accessing the critical section during the patching process, the nopl instruction within the critical section must be modified to an exception instruction. When other threads attempt to access the original function, they are immediately suspended by the exception instruction and placed in a pending state. After the jump function is prepared, the suspended thread is awakened, allowing it to access the patched function.

[0231] By changing the nopl instruction to an exception instruction, there will only be one exception instruction in the critical section. This way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, and there is no need to trap the operating system kernel to check for threads accessing instructions in the critical section. In other words, the patch execution method provided by this application is kernel-independent and can be patched even when the application software containing the program does not have permission to access the current operating system, that is, when the current operating system is a third-party operating system.

[0232] To modify a nopl instruction into an exception instruction, the first p bytes of the m bytes in the critical section may be modified into the exception instruction, and the last q bytes of the m bytes in the critical section may be left blank. For information on how to modify a nopl instruction into an exception instruction, see the embodiments shown in FIG. 5(a), FIG. 5(b), and FIG. 5(c).

[0233] S1604: Obtain the original function address and the patch function address, and calculate the offset between the original function address and the patch function address.

[0234] After the patch function is loaded into the patch area, the patch function address can be obtained.

[0235] Based on the original function address and the patch function address, the offset between the original function address and the patch function address can be calculated. The offset between the original function address and the patch function address can be obtained by subtracting the original function address from the patch function address.

[0236] S1605: Is the offset between the original function address and the patch function address greater than a preset offset?

[0237] When the offset between the original function address and the patch function address is less than or equal to the preset offset, S1606 and S1607 are executed.

[0238] When the offset between the original function address and the patch function address is greater than the preset offset, steps S1608 to S1612 are executed.

[0239] S1606: Modify the abnormal instruction based on the offset between the original function address and the patch function address to obtain a first jump instruction.

[0240] In this way, the first jump instruction can directly jump to the patch area to execute the patch function.

[0241] Specifically, the first p bytes of the m bytes in the critical section can be modified into the opcode of the jump instruction, and the last q bytes of the m bytes in the critical section can be filled with the operand of the jump instruction. The operand can be the offset between the original function address and the patch function address to obtain the first jump instruction.

[0242] Optionally, when constructing the first jump instruction, it is also necessary to first modify the operand of the jump instruction of the last q bytes in the critical section. Finally, replace the abnormal instruction of the first p bytes in the modified critical section with the operation code of the jump instruction. If the operation code is modified before the operand, there is no abnormal instruction in the critical section at this time. If other threads access the critical section at this time, they will not be suspended, and the operand of the first jump instruction has not been filled in at this time, and other threads cannot jump, resulting in a jump failure. Based on this, the operand of the jump instruction of the last q bytes in the critical section can be modified first, and finally the abnormal instruction of the first p bytes in the modified critical section can be replaced with the operation code of the jump instruction.

[0243] For the specific implementation of how to obtain the first jump instruction, please refer to the description in the embodiment of FIG. 7 .

[0244] S1607: The thread executes the first jump instruction and runs the patch function.

[0245] Among them, the thread may be the first thread. For example, the first thread may have accessed the original function between S1603 and S1605. At this time, the first thread may be suspended by the abnormal instruction in the critical section. In S1606, after the abnormal instruction that modifies the first p bytes in the critical section is replaced with the opcode of the jump instruction, the suspended first thread can be awakened, so that the first thread can jump to the patch area through the first jump instruction in the critical section and execute the patch function. In this way, the first thread does not need to execute the original function. By executing the patch function, the first thread can achieve the expected function that cannot be achieved by executing the original function.

[0246] This thread may also be a second thread. For example, the second thread may access the original function after S1605. In this case, the second thread will not be suspended. The second thread can directly jump to the patch area and execute the patch function through the first jump instruction in the critical section. In this way, the second thread does not need to execute the original function. By executing the patch function, the second thread can achieve the expected function that the original function cannot achieve.

[0247] S1608. Allocate a first function jump island for the original function and obtain the address of the first function jump island.

[0248] In some embodiments, a jump instruction has an upper limit on its jump distance. If the jump distance exceeds this limit, the jump will fail. To address the issue of jump errors caused by excessive jump distance, a function jump island can be introduced. The jump instruction can first jump to the function jump island, and then jump from the function jump island to the patch area. This shortens the jump distance of the jump instruction.

[0249] Therefore, when the offset between the original function address and the patch function address is greater than the preset offset, a function jump island, such as the first function jump island, needs to be allocated to the original function, and the address of the first function jump island is obtained.

[0250] For the specific implementation of how to allocate a function jump island to the original function, please refer to the description in the embodiments of Figures 9 to 12.

[0251] S1609: Modify the instruction in the first function jump island based on the patch function address to obtain a third jump instruction.

[0252] Here, the instruction in the first function jump island is modified to obtain a third jump instruction, so that the thread can execute the patch function from the patch area of ​​the first function jump island strip device.

[0253] Modifying the instruction in the first function jump island is to fill the patch function address into the instruction in the first function jump island to obtain a third jump instruction. For details, please refer to the description in S604.

[0254] S1610: Modify the abnormal instruction based on the offset between the original function address and the first function jump island address to obtain a second jump instruction.

[0255] After obtaining the first function jump island address, the offset between the original function address and the first function jump island address can be calculated. Based on the offset between the original function address and the first function jump island address, the exception instruction is modified so that the instruction can jump from the original function to the first function jump island.

[0256] For how to modify the abnormal instruction to obtain the second jump instruction, please refer to the description in the embodiment of Figure 8, and this application will not go into details here.

[0257] The second jump instruction is received after the third jump instruction because if the second jump instruction is constructed first, there will be no exception instructions in the critical section. If other threads access the critical section at this time, they will not be suspended and will directly jump to the first function jump island based on the second jump instruction. However, the third jump island may not have been modified at this time. Other threads jumping to the third jump island will not be able to jump to the patched area, resulting in a jump failure. Based on this, it is possible to construct the third jump instruction first and then construct the second jump instruction.

[0258] Optionally, when constructing the second jump instruction, it is also necessary to first modify the operand of the jump instruction of the last q bytes in the critical section. Finally, replace the abnormal instruction of the first p bytes in the modified critical section with the opcode of the jump instruction. If the opcode is modified before the operand, there will be no abnormal instruction in the critical section at this time. At this time, if other threads access the critical section, they will not be suspended, and the operand of the second jump instruction has not been filled in at this time, and other threads will not be able to jump, resulting in a jump failure. Based on this, the operand of the jump instruction of the last q bytes in the critical section can be modified first, and finally the abnormal instruction of the first p bytes in the modified critical section can be replaced with the opcode of the jump instruction.

[0259] Optionally, S1609 may also be executed before S1610, as long as it is ensured that when modifying the second jump instruction, the step of replacing the first p bytes of abnormal instructions in the critical section with the operation code of the jump instruction is executed after the third jump instruction is constructed.

[0260] S1611. The thread executes the second jump instruction and jumps to the first function jump island.

[0261] S1612: The thread executes the third jump instruction again, jumping to the patch area to run the patch function.

[0262] Among them, the thread may be the first thread. For example, the first thread may have accessed the original function between S1603 and S1605. At this time, the first thread may be suspended by the exception instruction in the critical section. In S1610, when modifying the second jump instruction, after replacing the exception instruction of the first p bytes in the modified critical section with the opcode of the jump instruction, the suspended first thread can be awakened, so that the first thread can jump to the first function jump island through the second jump instruction in the critical section. Afterwards, the first thread executes the second and third jump instructions, so that the first thread can jump to the patch area to run the patch function.

[0263] This thread may also be the second thread. For example, the thread may access the original function after S1605. In this case, the second thread will not be suspended. The second thread can directly jump to the first function jump island using the second jump instruction in the critical section. Afterwards, the second thread executes the second and third jump instructions, allowing the second thread to jump to the patch area and execute the patch function.

[0264] In this way, the first thread or the second thread does not need to execute the original function, and the second thread can achieve the expected function that cannot be achieved by executing the original function by executing the patch function.

[0265] FIG17 is a flow chart of another patch execution method provided by the present application.

[0266] S1701. Obtain a first patch function and store the first patch function in a patch area. The first patch function is used to modify a first original function in a program. The address of the first original function is a first address, and the address of the first patch function is a second address.

[0267] The first primitive function may be the primitive function described in the embodiment of FIG16 . The first address may be the primitive function address described in the embodiment of FIG16 .

[0268] The first patch function may be the patch function described in the embodiment of Figure 16. The second address may be the patch function address described in the embodiment of Figure 16.

[0269] S1702 . Insert an exception instruction before the first instruction of the first primitive function. The exception instruction is used for the thread to suspend execution of instructions in the first primitive function after executing the exception instruction.

[0270] Inserting a jump instruction can ensure that the thread accessing the original function can jump to the patch function instead of accessing the original function after the patch is activated.

[0271] S1703: Modify the abnormal instruction into a jump instruction based on the first address and the second address. The jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

[0272] In one possible implementation, the exception instruction is modified into a jump instruction based on the first address and the second address, specifically including: when the offset between the first address and the second address is less than a preset offset, the exception instruction is modified into a first jump instruction based on the offset between the first address and the second address, wherein the first jump instruction is used to cause the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function. The jump instruction has a limited jump distance. When a one-step jump is possible, the thread can jump directly from the original function to the patch function.

[0273] In one possible implementation, the critical section where the exception instruction resides includes a p-byte exception instruction opcode and q empty bytes; the exception instruction is modified to a first jump instruction based on the offset between the first address and the second address, specifically comprising: filling the offset between the first address and the second address into the q empty bytes; after filling the offset between the first address and the second address into the q empty bytes, replacing the p-byte exception instruction opcode with a p-byte jump instruction opcode to obtain the first jump instruction. In this way, finally modifying the exception instruction opcode to a jump instruction opcode ensures that no jump error occurs. If the exception instruction opcode is first modified to a jump instruction opcode, and then the offset between the first address and the second address is filled into the q empty bytes, before the offset between the first address and the second address is filled into the q empty bytes, if other threads access the original function, they will directly jump based on the jump instruction and cause an access error.

[0274] In one possible implementation, the exception instruction is modified into a jump instruction based on the first address and the second address, specifically including: when the offset between the first address and the second address is greater than the preset offset, based on the second address, the instruction in the first function jump island is modified to obtain a third jump instruction, and the address of the first function jump island is the third address; based on the first address and the third address, the exception instruction is modified to obtain a second jump instruction; the second jump instruction is used for the thread to jump to the first function jump island to execute the third jump instruction in the first function jump island when accessing the first original function, and the third jump instruction is used to jump to the patch area to execute the instruction in the first patch function when being executed by the thread. In this way, the jump distance of the jump instruction is limited. When it is not possible to jump in one step, you can first jump from the original function to the function jump island, and then jump from the function jump island to the patch function.

[0275] In one possible implementation, after modifying to obtain the third jump instruction, the second jump instruction is modified. Thus, the instruction in the first function jump island is first modified to obtain the third jump instruction, and then the exception instruction is modified to the second jump instruction. If the exception instruction is first modified to the second jump instruction, and then the instruction in the first function jump island is modified to obtain the third jump instruction, before the instruction in the first function jump island is modified to obtain the third jump instruction, if other threads access the original function, they will directly jump to the jump island based on the second jump instruction, while the third jump instruction in the jump island has not yet been modified, resulting in an access error.

[0276] In one possible implementation, the exception instruction includes a p-byte exception instruction opcode, followed by q empty bytes. Based on the second address, the instruction in the first function jump island is modified to obtain a third jump instruction, specifically including: filling the second address into the data portion of the first function jump island to obtain the third jump instruction. Based on the first and third addresses, the exception instruction is modified to obtain a second jump instruction, specifically including: filling the offset between the first and third addresses into the q empty bytes. After filling the offset between the first and third addresses into the q empty bytes, the p-byte exception instruction opcode is replaced with the p-byte jump instruction opcode to obtain the second jump instruction. Finally, the exception instruction opcode is modified to the jump instruction opcode, ensuring that jump errors will not occur. If the exception instruction opcode is first modified to a jump instruction opcode, and then the offset between the first address and the third address is filled into q empty bytes, before the offset between the first address and the third address is filled into q empty bytes, if other threads access the original function, an access error will occur due to direct jump based on the jump instruction.

[0277] In one possible implementation, when the offset between the first address and the second address is greater than a preset offset, the instructions in the first function jump island are modified based on the second address to obtain a third jump instruction, specifically including: when the offset between the first address and the second address is greater than the preset offset, determining the first function jump island in an idle state from multiple function jump islands; when the first function jump island has not been used, modifying the instructions in the first function jump island based on the second address to obtain a third jump instruction; when the first function jump island has been used before, after waiting for the first function jump island to be unoccupied for a period of time exceeding the first period, modifying the instructions in the first function jump island based on the second address to obtain a third jump instruction. In this way, a function jump island can be allocated to the original function. The allocation method is different for different types of jump islands.

[0278] Illustratively, the first function jump island may be function jump island 3 shown in Figure 10. The first function jump island may also be function jump island 5 shown in Figure 11. The first function jump island may also be function jump island 5 shown in Figure 12.

[0279] In one possible implementation, the method further includes: obtaining a second patch function and storing it in a patch area, the second patch function being used to modify the first original function in the program, the address of the second patch function being a fourth address; modifying the data portion of the first function's jump island to the fourth address to obtain a fourth jump instruction, the fourth jump instruction being used to jump to the patch area to execute instructions in the second patch function when executed by the thread. In this way, when repeatedly patching the same function, there is no need to reconstruct the jump instruction or repeatedly construct the instructions in the function jump island; only the data portion of the function jump island needs to be modified, saving steps.

[0280] In one possible implementation, when the storage address of the first patch function address cannot be read into a single cache line, a null byte is added before the storage address of the first patch function address, and the storage address of the first patch function address is changed; wherein, after the change, the storage address of the first patch function address is within the same cache line when it is read into the cache. This ensures that the patch function address can be written into the same cache line at once, avoiding access errors.

[0281] In one possible implementation, the method further includes: when a first thread accesses an exception instruction inserted into the first original function, pausing the first thread from executing instructions in the first original function; after modifying the exception instruction to a jump instruction based on the offset between the first address and the second address, waking up the first thread and jumping to the patch area via the jump instruction to execute instructions in the first patch function. Thus, before the jump instruction is modified, thread access to the original function is suspended. After the jump instruction is modified, waking up the suspended thread and jumping to the patch area via the jump instruction to execute the patch function.

[0282] In one possible implementation, the method further includes: after modifying the exception instruction into a jump instruction based on the offset between the first address and the second address, when a second thread accesses the jump instruction inserted into the first original function, controlling the second thread to jump to the patch area via the jump instruction to execute instructions in the first patch function. After the jump instruction is modified, when a thread accesses the original function, the thread directly jumps to the patch area via the jump instruction to execute the patch function.

[0283] Through this application, firstly, the defects of the original function to be patched in the program can be repaired through the patch function. Secondly, the program can be patched without restarting the program, which improves the program execution efficiency and patch efficiency. Thirdly, this application inserts an exception instruction in the function header of the original function, and the exception instruction is the critical section. In this way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, so there is no need to trap the operating system kernel to find out whether there are threads accessing instructions in the critical section. In other words, the patch execution method provided by this application does not rely on the kernel. Even if the application software where the program is located does not have permission to access the current operating system, that is, when the current operating system is a third-party operating system, the program can be patched.

[0284] FIG18 is a schematic diagram of a patch execution device provided by this application.

[0285] As shown in FIG18 , the patch execution device 1800 includes an acquisition module 1801 , a patch module 1802 and an execution module 1803 .

[0286] Among them, the acquisition module 1801 is used to obtain the first patch function and store the first patch function in the patch area. The first patch function is used to modify the first original function in the program. The address of the first original function is the first address, and the address of the first patch function is the second address.

[0287] The patch module 1802 is configured to insert an exception instruction before the first instruction of the first primitive function, where the exception instruction is used to cause a thread to suspend execution of instructions in the first primitive function after executing the exception instruction.

[0288] The execution module 1803 is configured to modify the abnormal instruction into a jump instruction based on the first address and the second address. The jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

[0289] Inserting the exception instruction can ensure that the thread accessing the original function can jump to the patch function instead of accessing the original function after the patch is activated.

[0290] Through this application, firstly, the defects of the original function to be patched in the program can be repaired through the patch function. Secondly, the program can be patched without restarting the program, which improves the program execution efficiency and patch efficiency. Thirdly, this application inserts an exception instruction in the function header of the original function, and the exception instruction is the critical section. In this way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, so there is no need to trap the operating system kernel to find out whether there are threads accessing instructions in the critical section. In other words, the patch execution method provided by this application does not rely on the kernel. Even if the application software where the program is located does not have permission to access the current operating system, that is, when the current operating system is a third-party operating system, the program can be patched.

[0291] In one possible implementation, the execution module 1803 is specifically used to modify the exception instruction into a first jump instruction based on the offset between the first address and the second address when the offset between the first address and the second address is less than a preset offset. The first jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

[0292] The jump distance of the jump instruction is limited. If the jump can be done in one step, you can jump directly from the original function to the patched function.

[0293] In one possible implementation, the exception instruction includes a p-byte exception instruction opcode, and q empty bytes after the exception instruction; the execution module 1803 is specifically used to: fill the offset between the first address and the second address into the q empty bytes; after filling the offset between the first address and the second address into the q empty bytes, replace the p-byte exception instruction opcode with a p-byte jump instruction opcode to obtain a first jump instruction.

[0294] In this way, the exception instruction opcode is finally modified to a jump instruction opcode, which ensures that jump errors will not occur. If the exception instruction opcode is first modified to a jump instruction opcode, and the offset between the first address and the second address is filled with q null bytes, before the offset between the first address and the second address is filled with q null bytes, if other threads access the original function, they will directly jump based on the jump instruction and cause an access error.

[0295] In one possible implementation, the execution module 1803 is specifically used to: when the offset between the first address and the second address is greater than the preset offset, based on the second address, modify the instruction in the first function jump island to obtain a third jump instruction, and the address of the first function jump island is the third address; based on the first address and the third address, modify the exception instruction to obtain a second jump instruction; the second jump instruction is used for the thread to jump to the first function jump island to execute the third jump instruction in the first function jump island when accessing the first original function, and the third jump instruction is used to jump to the patch area to execute the instructions in the first patch function when being executed by the thread.

[0296] In this way, the jump distance of the jump instruction is limited. If it is not possible to jump in one step, you can first jump from the original function to the function jump island, and then jump from the function jump island to the patch function.

[0297] In a possible implementation, after the third jump instruction is modified, the second jump instruction is modified. In this way, the instruction in the first function jump island is modified to obtain the third jump instruction, and then the abnormal instruction is modified to the second jump instruction.

[0298] If the exception instruction is first modified to the second jump instruction, and then the instruction in the jump island of the first function is modified to obtain the third jump instruction, before the instruction in the jump island of the first function is modified to obtain the third jump instruction, if other threads access the original function, they will directly jump to the jump island based on the second jump instruction, and the third jump instruction in the jump island has not been modified yet, resulting in an access error.

[0299] In one possible implementation, the exception instruction includes a p-byte exception instruction opcode, and q empty bytes after the exception instruction; the execution module 1803 is specifically used to fill the second address into the data part in the first function jump island to obtain a third jump instruction; the execution module 1803 is specifically used to: fill the offset between the first address and the third address into the q empty bytes; after filling the offset between the first address and the third address into the q empty bytes, replace the p-byte exception instruction opcode with the p-byte jump instruction opcode to obtain the second jump instruction.

[0300] In this way, the exception instruction opcode is finally modified to a jump instruction opcode, which ensures that jump errors will not occur. If the exception instruction opcode is first modified to a jump instruction opcode, and the offset between the first and third addresses is then filled into q null bytes, before the offset between the first and third addresses is filled into q null bytes, if other threads access the original function, they will directly jump based on the jump instruction and cause an access error.

[0301] In one possible implementation, the execution module 1803 is specifically used to: determine the first function jump island in an idle state from multiple function jump islands when the offset between the first address and the second address is greater than a preset offset; modify the instructions in the first function jump island based on the second address to obtain a third jump instruction when the first function jump island has not been used; and modify the instructions in the first function jump island based on the second address to obtain a third jump instruction when the first function jump island has been used before, after waiting for the time period that the first function jump island has not been occupied to exceed the first time period.

[0302] In this way, a function jump island can be allocated to the original function. The allocation method is different for different types of jump islands.

[0303] In one possible implementation, the acquisition module 1801 is also used to obtain a second patch function and store the second patch function in the patch area. The second patch function is used to modify the first original function in the program, and the address of the second patch function is the fourth address; the execution module 1803 is also used to modify the data part in the first function jump island to the fourth address to obtain a fourth jump instruction. The fourth jump instruction is used to jump to the patch area to execute the instructions in the second patch function when executed by the thread.

[0304] In this way, when patching the same function repeatedly, there is no need to reconstruct the jump instructions or repeat the instructions in the function jump island. You only need to modify the data part in the function jump island, saving operation steps.

[0305] In one possible implementation, the execution module 1803 is further used to add a null byte before the storage address of the first patch function address and change the storage address of the first patch function address when the storage address of the first patch function address cannot be read into a cache line; wherein, the storage address of the first patch function address after the change is in the same cache line when it is read into the cache.

[0306] In this way, it can be ensured that the patch function address can be written into the same cache line at one time, avoiding access errors.

[0307] In one possible implementation, the patch module 1802 is also used to suspend the first thread from executing instructions in the first original function when the first thread accesses the exception instruction inserted in the first original function; the execution module 1803 is also used to wake up the first thread and jump to the patch area through the jump instruction to execute instructions in the first patch function after modifying the exception instruction to a jump instruction based on the offset between the first address and the second address.

[0308] In this way, before the jump instruction is modified, the thread accessing the original function will be suspended. After the jump instruction is modified, the suspended thread is awakened and jumped to the patch area through the jump instruction to execute the patch function.

[0309] In one possible implementation, the execution module 1803 is also used to control the second thread to jump to the patch area through the jump instruction to execute the instructions in the first patch function when the second thread accesses the jump instruction inserted in the first original function after modifying the exception instruction into a jump instruction based on the offset between the first address and the second address.

[0310] After the jump instruction is modified, the thread accessing the original function will directly jump to the patch area through the jump instruction to execute the patch function.

[0311] FIG19 is a schematic diagram of an apparatus 1900 provided in an embodiment of the present application.

[0312] As shown in Figure 19, the device 1900 includes a processor 1901 and a memory 1902. The processor 1901 and the memory 1902 are coupled, and the memory 1902 is used to store computer program code. The computer program code includes computer instructions. The processor 1901 calls the computer instructions to execute: obtaining a first patch function and storing the first patch function in a patch area. The first patch function is used to modify the first original function in the program. The address of the first original function is the first address, and the address of the first patch function is the second address; inserting an exception instruction before the first instruction of the first original function. The exception instruction is used to cause the thread to suspend the execution of instructions in the first original function after executing the exception instruction; modifying the exception instruction into a jump instruction based on the first address and the second address. The jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

[0313] Inserting the exception instruction can ensure that the thread accessing the original function can jump to the patch function instead of accessing the original function after the patch is activated.

[0314] Through this application, firstly, the defects of the original function to be patched in the program can be repaired through the patch function. Secondly, the program can be patched without restarting the program, which improves the program execution efficiency and patch efficiency. Thirdly, this application inserts an exception instruction in the function header of the original function, and the exception instruction is the critical section. In this way, all threads accessing the original function will first access the exception instruction, and these threads will be suspended. There will be no threads accessing instructions in the critical section, so there is no need to trap the operating system kernel to find out whether there are threads accessing instructions in the critical section. In other words, the patch execution method provided by this application does not rely on the kernel. Even if the application software where the program is located does not have permission to access the current operating system, that is, when the current operating system is a third-party operating system, the program can be patched.

[0315] In one possible implementation, processor 1901 invokes a computer instruction to execute: modifying an exception instruction into a jump instruction based on a first address and a second address, specifically including: modifying the exception instruction into a first jump instruction based on the offset between the first address and the second address when the offset between the first address and the second address is less than a preset offset, wherein the first jump instruction is used to cause a thread to jump to a patch area to execute instructions in the first patch function when accessing the first original function. The jump instruction has a limited jump distance. When a one-step jump is possible, the thread can jump directly from the original function to the patch function.

[0316] In one possible implementation, processor 1901 calls a computer instruction to execute: an exception instruction including a p-byte exception instruction opcode, followed by q null bytes; and modifying the exception instruction to a first jump instruction based on an offset between a first address and a second address, specifically comprising: filling the offset between the first address and the second address into the q null bytes; and after filling the offset between the first address and the second address into the q null bytes, replacing the p-byte exception instruction opcode with a p-byte jump instruction opcode to obtain the first jump instruction. Thus, finally modifying the exception instruction opcode into a jump instruction opcode ensures that a jump error will not occur. If the exception instruction opcode is first modified into a jump instruction opcode, and then the offset between the first address and the second address is filled into the q null bytes, before the offset between the first address and the second address is filled into the q null bytes, if another thread accesses the original function, it will directly jump based on the jump instruction and cause an access error.

[0317] In one possible implementation, the processor 1901 calls a computer instruction to execute: based on the first address and the second address, the exception instruction is modified into a jump instruction, specifically including: when the offset between the first address and the second address is greater than the preset offset, based on the second address, the instruction in the first function jump island is modified to obtain a third jump instruction, and the address of the first function jump island is the third address; based on the first address and the third address, the exception instruction is modified to obtain a second jump instruction; the second jump instruction is used for the thread to jump to the first function jump island to execute the third jump instruction in the first function jump island when accessing the first original function, and the third jump instruction is used to jump to the patch area to execute the instruction in the first patch function when being executed by the thread. In this way, the jump distance of the jump instruction is limited. When it is not possible to jump in one step, you can first jump from the original function to the function jump island, and then jump from the function jump island to the patch function.

[0318] In one possible implementation, processor 1901 calls a computer instruction to execute: after modifying to obtain the third jump instruction, it then modifies to obtain the second jump instruction. In this way, the instruction in the first function jump island is first modified to obtain the third jump instruction, and then the exception instruction is modified to the second jump instruction. If the exception instruction is first modified to the second jump instruction, and then the instruction in the first function jump island is modified to obtain the third jump instruction, before the instruction in the first function jump island is modified to obtain the third jump instruction, if other threads access the original function, they will directly jump to the jump island based on the second jump instruction, and the third jump instruction in the jump island has not yet been modified, resulting in an access error.

[0319] In one possible implementation, processor 1901 calls computer instructions to execute: an exception instruction including a p-byte exception instruction opcode, followed by q dummy bytes; based on a second address, modifying an instruction in a first function jump island to obtain a third jump instruction, specifically including: filling the second address into a data portion of the first function jump island to obtain the third jump instruction; based on the first address and the third address, modifying the exception instruction to obtain a second jump instruction, specifically including: filling an offset between the first address and the third address into the q dummy bytes; after filling the offset between the first address and the third address into the q dummy bytes, replacing the p-byte exception instruction opcode with a p-byte jump instruction opcode to obtain the second jump instruction. Finally, modifying the exception instruction opcode to the jump instruction opcode ensures that jump errors will not occur. If the exception instruction opcode is first modified to a jump instruction opcode, and then the offset between the first address and the third address is filled into q empty bytes, before the offset between the first address and the third address is filled into q empty bytes, if other threads access the original function, an access error will occur due to direct jump based on the jump instruction.

[0320] In one possible implementation, processor 1901 calls a computer instruction to execute: when the offset between the first address and the second address is greater than a preset offset, based on the second address, modify the instructions in the first function jump island to obtain a third jump instruction, specifically including: when the offset between the first address and the second address is greater than the preset offset, determine the first function jump island in an idle state from multiple function jump islands; when the first function jump island has not been used, based on the second address, modify the instructions in the first function jump island to obtain a third jump instruction; when the first function jump island has been used before, after waiting for the first function jump island to be unoccupied for a period of time exceeding the first time period, modify the instructions in the first function jump island based on the second address to obtain a third jump instruction. In this way, a function jump island can be allocated to the original function. The allocation method is different for different types of jump islands.

[0321] In one possible implementation, processor 1901 invokes computer instructions to execute: obtaining a second patch function and storing it in a patch area; the second patch function is used to modify the first original function in the program; the address of the second patch function is a fourth address; modifying the data portion of the first function jump island to the fourth address to obtain a fourth jump instruction; the fourth jump instruction is used to jump to the patch area to execute the instructions in the second patch function when executed by the thread. In this way, when repeatedly patching the same function, there is no need to reconstruct the jump instruction or repeatedly construct the instructions in the function jump island; only the data portion of the function jump island needs to be modified, saving operational steps.

[0322] In one possible implementation, processor 1901 invokes computer instructions to execute: when the storage address of the first patch function address cannot be read into a cache line, add a null byte before the storage address of the first patch function address, thereby changing the storage address of the first patch function address; wherein the changed storage address of the first patch function address is within the same cache line when it is read into the cache. This ensures that the patch function address can be written into the same cache line at one time, thus avoiding access errors.

[0323] In one possible implementation, processor 1901 invokes computer instructions to execute: when a first thread accesses an exception instruction inserted into a first original function, suspend the first thread's execution of instructions in the first original function; after modifying the exception instruction to a jump instruction based on the offset between the first address and the second address, awaken the first thread and jump to the patch area using the jump instruction to execute instructions in the first patch function. Thus, before the jump instruction is modified, the thread's access to the original function is suspended. After the jump instruction is modified, the suspended thread is awakened and jumps to the patch area using the jump instruction to execute the patch function.

[0324] In one possible implementation, processor 1901 invokes computer instructions to execute: after modifying the exception instruction into a jump instruction based on the offset between the first address and the second address, when a second thread accesses the jump instruction inserted into the first original function, control the second thread to jump to the patch area via the jump instruction to execute instructions in the first patch function. After the jump instruction is modified, threads accessing the original function directly jump to the patch area via the jump instruction to execute the patch function.

[0325] The various implementation modes of this application can be combined arbitrarily to achieve different technical effects.

[0326] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware, or any combination thereof. When implemented using software, all or part of the embodiments may be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described herein are generated. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions may be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated therein. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)).

[0327] Those skilled in the art will appreciate that all or part of the process steps in the above-described method embodiments can be implemented by a computer program instructing the relevant hardware. The program can be stored in a computer-readable storage medium, and when executed, the program can include the process steps in the above-described method embodiments. The aforementioned storage medium includes various media capable of storing program code, such as ROM or random access memory (RAM), magnetic disks, or optical disks.

[0328] In short, the above description is only an embodiment of the technical solution of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent replacements, improvements, etc. made based on the disclosure of the present invention should be included in the scope of protection of the present invention.

Claims

1. A patch execution method, It is characterized in that The method comprises: Obtaining a first patch function, and storing the first patch function in a patch area, wherein the first patch function is used to modify a first original function in a program, the address of the first original function is a first address, and the address of the first patch function is a second address; Inserting an exception instruction before the first instruction of the first primitive function, wherein the exception instruction is used for the thread to suspend execution of instructions in the first primitive function after executing the exception instruction; The exception instruction is modified into a jump instruction based on the first address and the second address, and the jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

2. The method according to claim 1, It is characterized in that The modifying the abnormal instruction into a jump instruction based on the first address and the second address specifically includes: When the offset between the first address and the second address is less than a preset offset, the exception instruction is modified into a first jump instruction based on the offset between the first address and the second address, and the first jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

3. The method according to claim 2, It is characterized in that The abnormal instruction includes p bytes of abnormal instruction operation code, and q empty bytes are included after the abnormal instruction; Modifying the abnormal instruction into a first jump instruction based on the offset between the first address and the second address specifically includes: Fill the offset between the first address and the second address into the q empty bytes; After filling the offset between the first address and the second address into the q empty bytes, the p-byte exception instruction operation code is replaced with the p-byte jump instruction operation code to obtain the first jump instruction.

4. The method according to claim 1, It is characterized in that The modifying the abnormal instruction into a jump instruction based on the first address and the second address specifically includes: When the offset between the first address and the second address is greater than a preset offset, based on the second address, modify the instruction in the first function jump island to obtain a third jump instruction, and the address of the first function jump island is the third address; Based on the first address and the third address, modify the abnormal instruction to obtain a second jump instruction; The second jump instruction is used for the thread to jump to the first function jump island to execute the third jump instruction in the first function jump island when accessing the first original function, and the third jump instruction is used to jump to the patch area to execute the instructions in the first patch function when executed by the thread.

5. The method according to claim 4, It is characterized in that After the third jump instruction is obtained by modification, the second jump instruction is further obtained by modification.

6. The method according to claim 4 or 5, It is characterized in that The abnormal instruction includes p bytes of abnormal instruction operation code, and q empty bytes are included after the abnormal instruction; The step of modifying the instruction in the first function jump island based on the second address to obtain a third jump instruction specifically includes: Fill the second address into the data part of the first function jump island to obtain the third jump instruction; Based on the first address and the third address, the abnormal instruction is modified to obtain a second jump instruction, specifically including: Fill the offset between the first address and the third address into the q empty bytes; After filling the offset between the first address and the third address into the q empty bytes, the p-byte abnormal instruction operation code is replaced with the p-byte jump instruction operation code to obtain the second jump instruction.

7. The method according to any one of claims 4 to 6, It is characterized in that The step of modifying the instruction in the first function jump island based on the second address to obtain a third jump instruction when the offset between the first address and the second address is greater than the preset offset specifically includes: In a case where the offset between the first address and the second address is greater than the preset offset, determining the first function jump island in an idle state from a plurality of function jump islands; In the case where the first function jump island has not been used, instructions in the first function jump island are executed based on the second address. The line is modified to obtain the third jump instruction; In the case that the first function jump island has been used before, after waiting, when the first function jump island has not been occupied for a period exceeding a first period, the instruction in the first function jump island is modified based on the second address to obtain the third jump instruction.

8. The method according to any one of claims 4 to 7, It is characterized in that The method further comprises: Obtaining a second patch function, and storing the second patch function in a patch area, wherein the second patch function is used to modify the first original function in the program, and an address of the second patch function is a fourth address; The data portion in the first function jump island is modified to the fourth address to obtain a fourth jump instruction, and the fourth jump instruction is used to jump to the patch area to execute instructions in the second patch function when executed by a thread.

9. The method according to any one of claims 1 to 8, It is characterized in that When the storage address of the first patch function address cannot be read into a cache line, adding a null byte before the storage address of the first patch function address to change the storage address of the first patch function address; The storage address of the first patch function address after the change is in the same cache line when it is read into the cache.

10. The method according to any one of claims 1 to 9, It is characterized in that The method further comprises: When the first thread accesses the abnormal instruction inserted in the first primitive function, pausing the first thread to execute the instruction in the first primitive function; After the abnormal instruction is modified into the jump instruction based on the offset between the first address and the second address, the first thread is awakened to jump to the patch area through the jump instruction to execute the instructions in the first patch function.

11. The method according to any one of claims 1 to 9, It is characterized in that The method further comprises: After the exception instruction is modified into the jump instruction based on the offset between the first address and the second address, when the second thread accesses the jump instruction inserted in the first original function, the second thread is controlled to jump to the patch area through the jump instruction to execute the instructions in the first patch function.

12. A patch execution device, It is characterized in that The patch execution device includes an acquisition module, a patch module and an execution module, wherein: The acquisition module is used to acquire a first patch function and store the first patch function in the patch area, the first patch function is used to modify a first original function in the program, the address of the first original function is a first address, and the address of the first patch function is a second address; The patch module is used to insert an exception instruction before the first instruction of the first primitive function, and the exception instruction is used for the thread to suspend the execution of instructions in the first primitive function after executing the exception instruction; The execution module is used to modify the abnormal instruction into a jump instruction based on the first address and the second address, and the jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

13. The device according to claim 12, It is characterized in that The execution module is specifically used to modify the exception instruction into a first jump instruction based on the offset between the first address and the second address when the offset between the first address and the second address is less than a preset offset. The first jump instruction is used for the thread to jump to the patch area to execute instructions in the first patch function when accessing the first original function.

14. The device according to claim 13, It is characterized in that The abnormal instruction includes p bytes of abnormal instruction operation code, and q empty bytes are included after the abnormal instruction; The execution module is specifically used for: Fill the offset between the first address and the second address into the q empty bytes; After filling the offset between the first address and the second address into the q empty bytes, the p-byte exception instruction operation code is replaced with the p-byte jump instruction operation code to obtain the first jump instruction.

15. The device according to claim 12, It is characterized in that The execution module is specifically used for: When the offset between the first address and the second address is greater than a preset offset, based on the second address, modify the instruction in the first function jump island to obtain a third jump instruction, and the address of the first function jump island is the third address; Based on the first address and the third address, modify the abnormal instruction to obtain a second jump instruction; The second jump instruction is used for the thread to jump to the first function jump island to execute the third jump instruction in the first function jump island when accessing the first original function, and the third jump instruction is used to jump to the patch area to execute the instructions in the first patch function when executed by the thread.

16. The device according to claim 15, It is characterized in that After the third jump instruction is obtained by modification, the second jump instruction is further obtained by modification.

17. The device according to claim 15 or 16, It is characterized in that The abnormal instruction includes p bytes of abnormal instruction operation code, and q empty bytes are included after the abnormal instruction; The execution module is specifically configured to fill the second address into the data portion of the first function jump island to obtain the third jump instruction; The execution module is specifically used for: Fill the offset between the first address and the third address into the q empty bytes; After filling the offset between the first address and the third address into the q empty bytes, the p-byte abnormal instruction operation code is replaced with the p-byte jump instruction operation code to obtain the second jump instruction.

18. The device according to any one of claims 15 to 17, It is characterized in that The execution module is specifically used for: In a case where the offset between the first address and the second address is greater than the preset offset, determining the first function jump island in an idle state from a plurality of function jump islands; In a case where the first function jump island has not been used, based on the second address, modifying the instruction in the first function jump island to obtain the third jump instruction; In the case that the first function jump island has been used before, after waiting, when the first function jump island has not been occupied for a period exceeding a first period, the instruction in the first function jump island is modified based on the second address to obtain the third jump instruction.

19. The device according to any one of claims 15 to 18, It is characterized in that The acquisition module is further used to acquire a second patch function and store the second patch function in the patch area, wherein the second patch function is used to modify the first original function in the program, and the address of the second patch function is a fourth address; The execution module is also used to modify the data part in the first function jump island to the fourth address to obtain a fourth jump instruction, and the fourth jump instruction is used to jump to the patch area to execute instructions in the second patch function when executed by the thread.

20. The device according to any one of claims 12 to 19, It is characterized in that The execution module is further configured to add a null byte before the storage address of the first patch function address to change the storage address of the first patch function address when the storage address of the first patch function address cannot be read into a cache line; The storage address of the first patch function address after the change is in the same cache line when it is read into the cache.

21. The device according to any one of claims 12 to 20, It is characterized in that The patch module is further configured to suspend the first thread from executing instructions in the first primitive function when the first thread accesses the abnormal instruction inserted in the first primitive function; The execution module is further used to wake up the first thread and jump to the patch area through the jump instruction to execute instructions in the first patch function after modifying the abnormal instruction into the jump instruction based on the offset between the first address and the second address.

22. The device according to any one of claims 12 to 20, It is characterized in that The execution module is also used to control the second thread to jump to the patch area through the jump instruction to execute the instructions in the first patch function when the second thread accesses the jump instruction inserted in the first original function after modifying the exception instruction into the jump instruction based on the offset between the first address and the second address.

23. A computer-readable storage medium, It is characterized in that Used to store computer instructions, when the computer instructions are run on a patch execution device, the patch execution device executes any one of the methods described in claims 1-11.

24. A computer program product, It is characterized in that When the computer program product runs on a patch execution device, the patch execution device is caused to execute the method described in any one of claims 1 to 11.

Citation Information

Patent Citations

  • Patch execution method and device

    CN118689519A

  • Patching method and patching device under multi-core environment

    CN101561764A

  • Method, device and system for patching kernel on line

    CN101799763A

  • Method, device and system for online activating patches

    CN102156661A

  • Hot patch implementation method and communication device

    CN107357622A