Virtual machine exception processing instruction execution method, device, apparatus, and medium
By establishing an exception handling stack frame and writing the address of the exception monitoring instruction when the virtual machine executes the program, the problem of low efficiency in virtual machine exception handling is solved, the location of the exception instruction is quickly located, and the exception handling efficiency is improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING TONGFANG MICROELECTRONICS
- Filing Date
- 2024-08-29
- Publication Date
- 2026-07-03
AI Technical Summary
In existing technologies, virtual machines are inefficient in handling exceptions, especially when WebAssembly virtual machine technology is applied to the smart card field, where there is a need to improve the efficiency of exception handling.
When the virtual machine detects that the current function is executing the exception monitoring instruction normally during program execution, an exception handling stack frame is created, and the instruction address of the exception monitoring instruction is written into the stack frame to quickly locate the location of the exception.
By pre-constructing exception handling stack frames, the instruction location where the virtual machine exception occurred can be quickly located, thereby improving the efficiency of virtual machine exception handling.
Smart Images

Figure CN119088506B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method, apparatus, device, and medium for executing virtual machine exception handling instructions. Background Technology
[0002] WebAssembly (Wasm for short) is a virtual instruction set architecture. Programs written in languages such as C / C++ can be compiled and run securely and at high speed on the Web (World Wide Web) platform. When applying WebAssembly virtual machine technology based on the C language to computer devices, such as smart cards, the virtual machine needs to support an exception handling mechanism and improve the efficiency of exception handling as much as possible. Summary of the Invention
[0003] This invention provides a method, apparatus, device, and medium for executing virtual machine exception handling instructions, which can effectively improve the efficiency of virtual machine exception handling.
[0004] According to one aspect of the present invention, a method for executing virtual machine exception handling instructions is provided, the method comprising:
[0005] During the execution of a program in a virtual machine, when it is detected that the current function is executing an exception monitoring instruction normally, an exception handling stack frame corresponding to the exception monitoring instruction is established.
[0006] Write the instruction address of the exception monitoring instruction into the established exception handling stack frame.
[0007] According to another aspect of the present invention, an execution apparatus for virtual machine exception handling instructions is provided, the apparatus comprising:
[0008] The exception stack frame creation module is used to create an exception handling stack frame corresponding to the exception monitoring instruction when the current function is detected to be executing the exception monitoring instruction normally during the execution of the program by the virtual machine.
[0009] The instruction address writing module is used to write the instruction address of the exception monitoring instruction into the established exception handling stack frame.
[0010] According to another aspect of the present invention, a computer device is provided, characterized in that the computer device includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores a computer program executable by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to execute the virtual machine exception handling instruction execution method according to any embodiment of the present invention.
[0011] According to another aspect of the present invention, a computer-readable storage medium is provided, characterized in that the computer-readable storage medium stores computer instructions, the computer instructions being used to cause a processor to execute the virtual machine exception handling instruction execution method described in any embodiment of the present invention.
[0012] The technical solution provided by this invention provides a WebAssembly virtual machine exception handling method based on the C language. By establishing an exception handling stack frame corresponding to the exception monitoring instruction when the current function is detected to be executing the exception monitoring instruction normally during the execution of the virtual machine program, and writing the instruction address of the exception monitoring instruction to the established exception handling stack frame, this method can quickly locate the instruction position where the exception occurred by using a pre-built exception handling stack frame when the virtual machine runs abnormally, thereby improving the efficiency of virtual machine exception handling.
[0013] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of the present invention, nor is it intended to limit the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description
[0014] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0015] Figure 1 This is a flowchart of a virtual machine exception handling instruction execution method according to an embodiment of the present invention;
[0016] Figure 2a This is a flowchart of another method for executing virtual machine exception handling instructions according to an embodiment of the present invention;
[0017] Figure 2b This is a flowchart of a method for handling virtual machine exceptions when a virtual machine malfunctions, according to an embodiment of the present invention.
[0018] Figure 2c This is a flowchart of a method for handling virtual machine exceptions when a virtual machine malfunctions, according to an embodiment of the present invention.
[0019] Figure 3 This is a schematic diagram of the structure of a virtual machine exception handling instruction execution device according to an embodiment of the present invention;
[0020] Figure 4This is a schematic diagram of the structure of a computer device according to an embodiment of the present invention. Detailed Implementation
[0021] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0022] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0023] Figure 1 This is a flowchart illustrating a method for executing virtual machine exception handling instructions according to an embodiment of the present invention. This embodiment is applicable to situations where a virtual machine is controlled to execute exception handling instructions. This method can be executed by a virtual machine exception handling instruction execution device, which can be configured within the virtual machine. Figure 1 As shown, the method includes:
[0024] Step 110: During the execution of the program in the virtual machine, when it is detected that the current function is executing the exception monitoring instruction normally, an exception handling stack frame corresponding to the exception monitoring instruction is established.
[0025] In this embodiment, during the execution of the program by the virtual machine, the currently executed function may include exception handling instructions, which may include exception monitoring instructions (try) and paired exception catching instructions (catch).
[0026] During the normal execution of the current function, when an exception monitoring instruction is executed, an exception handling stack frame corresponding to that exception monitoring instruction can be established.
[0027] Step 120: Write the instruction address of the exception monitoring instruction into the established exception handling stack frame.
[0028] In this embodiment, specifically, after establishing the exception handling stack frame corresponding to the exception monitoring instruction, the address (Program Counter, PC) of the exception monitoring instruction in memory can be written into the exception handling stack frame.
[0029] In a specific embodiment, suppose the pseudocode of the program is as follows:
[0030]
[0031] When the current function funcA executes normally to the try1 instruction, an exception handling stack frame corresponding to the try1 instruction can be created, and the instruction address of the try1 instruction can be written into the created exception handling stack frame.
[0032] The advantage of this setup is that when a virtual machine encounters an error, the location of the instruction that caused the error can be quickly identified through a pre-built exception handling stack frame, thereby improving the efficiency of virtual machine exception handling.
[0033] The technical solution provided by this invention provides a WebAssembly virtual machine exception handling method based on the C language. By establishing an exception handling stack frame corresponding to the exception monitoring instruction when the current function is detected to be executing the exception monitoring instruction normally during the execution of the virtual machine program, and writing the instruction address of the exception monitoring instruction to the established exception handling stack frame, this method can quickly locate the instruction position where the exception occurred by using a pre-built exception handling stack frame when the virtual machine runs abnormally, thereby improving the efficiency of virtual machine exception handling.
[0034] Figure 2a A flowchart of another method for executing virtual machine exception handling instructions provided in an embodiment of the present invention is shown below. Figure 2a As shown, the method includes:
[0035] Step 210: During the execution of the program in the virtual machine, when it is detected that the current function is executing the exception monitoring instruction normally, an exception handling stack frame corresponding to the exception monitoring instruction is established.
[0036] Step 220: Write the instruction address of the exception monitoring instruction into the established exception handling stack frame.
[0037] Step 230: Push the normal operating state corresponding to the anomaly monitoring instruction onto the virtual machine operand stack, so that the virtual machine continues to execute the code in the anomaly monitoring instruction block corresponding to the anomaly monitoring instruction.
[0038] Specifically, when the current function is executing the exception monitoring instruction normally, a zero value can be pushed onto the virtual machine operand stack as a normal operation status flag corresponding to the exception monitoring instruction.
[0039] In a specific embodiment, suppose the pseudocode of the program is as follows:
[0040]
[0041]
[0042] When the current function funcA executes normally to the try1 instruction, an exception handling stack frame corresponding to the try1 instruction can be created. The instruction address of the try1 instruction is written into the created exception handling stack frame, and the zero value is pushed onto the virtual machine operand stack as the normal operation status flag corresponding to the try1 instruction, so that the virtual machine continues to execute the code in the exception monitoring instruction block corresponding to try1.
[0043] Step 240: When the current function is executing the exception handling instruction normally, the normal operation state corresponding to the exception handling instruction is pushed onto the virtual machine operand stack, so that the virtual machine skips the exception handling instruction block corresponding to the exception handling instruction and continues to execute the code after the exception handling instruction block.
[0044] In this step, specifically, when the current function executes the exception capture instruction normally, a non-zero value can be pushed onto the virtual machine operand stack as a normal operation status flag corresponding to the exception capture instruction.
[0045] In a specific embodiment, taking the above program code as an example, when the current function funcA executes normally to the catch1 instruction, a non-zero value (such as 1) can be pushed onto the virtual machine operand stack as a normal operation status identifier corresponding to the catch1 instruction. Then, the virtual machine can skip the exception capture instruction block corresponding to catch1 and continue to execute the code after the exception capture instruction block, such as callB()...
[0046] In one embodiment of this example, within the current function, the exception monitoring instruction or exception catching instruction contains at least one level of other exception monitoring instructions and exception catching instructions nested within it; alternatively, within the current function, the exception monitoring instruction or exception catching instruction does not contain any other exception monitoring instructions and exception catching instructions nested within it. Specifically, assume the program pseudocode is as follows:
[0047]
[0048]
[0049] In a function, the try-catch level of exception monitoring blocks and exception handling blocks that are within other exception monitoring blocks is not the first level; the try-catch level of exception monitoring blocks and exception handling blocks that are not within other exception monitoring blocks is the first level. In function `funcA`, the `try1` instruction is not within any other exception monitoring block in this function, therefore its try-catch level is the first level. Similarly, the `try21` instruction in function `funcA` is not within any other exception monitoring block in this function, therefore its try-catch level is the first level; however, the `try22` instruction in function `funcA` is within the `try21` exception monitoring block, therefore its try-catch level is not the first level.
[0050] In this embodiment, after establishing the exception handling stack frame corresponding to the exception monitoring instruction, the method further includes: writing the level information of the exception monitoring instruction in the current function into the exception handling stack frame; wherein, the level information includes the first level or a non-first level.
[0051] The level information of the anomaly monitoring instruction in the current function can be determined by the running status flag in the anomaly monitoring instruction block in the virtual machine stack frame corresponding to the function.
[0052] Specifically, before the current function begins execution, a virtual machine stack frame corresponding to the current function can be established, and the runtime status flag of the exception monitoring instruction block in the current function can be maintained in real time within the virtual machine stack frame. Before writing the level information of the exception monitoring instruction in the current function to the exception handling stack frame, the process further includes: querying the virtual machine stack frame; if the runtime status flag of the exception monitoring instruction block in the current function is negative, then the level information of the exception monitoring instruction in the current function is determined to be the first level; if the runtime status flag of the exception monitoring instruction block in the current function is positive, then the level information of the exception monitoring instruction in the current function is determined to be non-first level.
[0053] When the running status flag in the exception monitoring instruction block of the current function is negative, it indicates that the currently executing code is not being monitored for exceptions in the current function; when the running status flag in the exception monitoring instruction block of the current function is positive, it indicates that the currently executing code is being monitored for exceptions in the current function.
[0054] In a specific embodiment, taking the above program pseudocode as an example, before the execution of the funcA function begins, a virtual machine stack frame corresponding to the funcA function can be established, and in the virtual machine stack frame, the running status flag in the exception monitoring instruction block of the funcA function can be maintained in real time.
[0055] When the function funcA executes normally to the try1 instruction, an exception handling stack frame corresponding to the try1 instruction can be established. Then, the running status flag in the exception monitoring instruction block of the virtual machine stack frame corresponding to the funcA function is queried. Since the try1 instruction is not in any other exception monitoring instruction block in this function, the running status flag in the exception monitoring instruction block of the virtual machine stack frame is negative. Therefore, it can be determined that the try-catch level information to which the try1 instruction belongs in the funcA function is the first level, and this try-catch level information is written into the exception handling stack frame corresponding to the try1 instruction.
[0056] When the funcA() function executes to the catch1 instruction, the exception handling stack frame of the try1 instruction can be popped. After determining that the try-catch level of the try1 instruction is the first level through the exception handling stack frame, the running status flag in the exception monitoring instruction block recorded in the virtual machine stack frame of the funcA function can be set to no.
[0057] When the `funcA` function executes normally to the `try21` instruction, an exception handling stack frame corresponding to the `try21` instruction can be established. Then, the running status flag in the exception monitoring instruction block of the virtual machine stack frame corresponding to the `funcA` function is queried. Since the `try21` instruction is not in any other exception monitoring instruction block in this function, the running status flag in the exception monitoring instruction block of the virtual machine stack frame is negative. Therefore, it can be determined that the try-catch level information of the `try21` instruction in the `funcA` function is the first level, and this try-catch level information is written into the exception handling stack frame corresponding to the `try21` instruction.
[0058] When the function funcA executes normally to the try22 instruction, an exception handling stack frame corresponding to the try22 instruction can be established. Then, the running status flag in the exception monitoring instruction block of the virtual machine stack frame corresponding to the funcA function is queried. Since the try22 instruction is in the try21 exception monitoring instruction block of this function, and the running status flag in the exception monitoring instruction block of the virtual machine stack frame is "yes", it can be determined that the try-catch level information of the try22 instruction in the funcA function is not the first level, and this try-catch level information is written into the exception handling stack frame corresponding to the try22 instruction.
[0059] In this embodiment, the runtime status flag of the exception monitoring instruction block in the current function is maintained in real time in the virtual machine stack frame, including: when the current function is executing the exception monitoring instruction normally, the runtime status flag of the current exception monitoring instruction block is set to "yes" in the virtual machine stack frame; when the current function is executing the exception catching instruction, an exception handling stack frame is popped; if the level information recorded in the popped exception handling stack frame is the first level, the runtime status flag of the current exception monitoring instruction block in the virtual machine stack frame is set to "no".
[0060] In a specific embodiment, taking the above program pseudocode as an example, when the funcA function executes normally to the try1 instruction, the running status flag in the exception monitoring instruction block recorded in the virtual machine stack frame corresponding to the funcA function is no. When executing the bytecode of the try1 instruction, the running status flag in the exception monitoring instruction block of this virtual machine stack frame can be set to yes.
[0061] When the funcA() function executes to the catch1 instruction, the exception handling stack frame of the try1 instruction can be popped. After determining that the try-catch level of the try1 instruction is the first level through the exception handling stack frame, the running status flag in the exception monitoring instruction block recorded in the virtual machine stack frame of the funcA function can be set to no.
[0062] When the function funcA executes normally to the try21 instruction, the runtime status flag in the exception monitoring instruction block recorded in the virtual machine stack frame corresponding to the function funcA is no. When executing the bytecode of the try21 instruction, the information of the try-catch level to which the try21 instruction belongs in the function funcA as the first level can be written into the exception handling stack frame corresponding to the try21 instruction, and the runtime status flag in the exception monitoring instruction block in this virtual machine stack frame can be set to yes.
[0063] When the funcA function executes normally to the try22 instruction, the runtime status flag in the exception monitoring instruction block recorded in the virtual machine stack frame corresponding to the funcA function is set to "yes". When executing the bytecode of the try22 instruction, the information that the try-catch level to which the try22 instruction belongs in the funcA function is not the first level can be written into the exception handling stack frame corresponding to the try22 instruction. This eliminates the need to repeatedly set the runtime status flag in the exception monitoring instruction block of this virtual machine stack frame to "yes".
[0064] When the funcA() function executes to the catch22 instruction, the exception handling stack frame information of the try22 instruction can be popped. This exception handling stack frame information records that the try-catch level information of the try22 instruction is not the first level. Therefore, it is not necessary to clear the running status flag in the exception monitoring instruction block recorded in the virtual machine stack frame of the funcA function. That is, the running status flag in the exception monitoring instruction block recorded in the virtual machine stack frame of the funcA function should be kept as "yes".
[0065] When the funcA() function executes to the catch21 instruction, the exception handling stack frame information of the try21 instruction can be popped. This exception handling stack frame information records that the try-catch level information of the try21 instruction is the first level. Therefore, the running status flag in the exception monitoring instruction block recorded in the virtual machine stack frame of the funcA function needs to be set to no.
[0066] The technical solution provided by this invention improves the efficiency of virtual machine exception handling by establishing an exception handling stack frame corresponding to the exception monitoring instruction when the current function is executing an exception monitoring instruction normally during the execution of a program by a virtual machine, writing the instruction address of the exception monitoring instruction into the established exception handling stack frame, pushing the normal operation state corresponding to the exception monitoring instruction onto the virtual machine operand stack, so that the virtual machine continues to execute the code in the exception monitoring instruction block corresponding to the exception monitoring instruction, and pushing the normal operation state corresponding to the exception catching instruction onto the virtual machine operand stack when the current function is executing an exception catching instruction normally, so that the virtual machine skips the exception catching instruction block corresponding to the exception catching instruction and continues to execute the code after the exception catching instruction block.
[0067] Based on the above embodiments, the method further includes: when a virtual machine anomaly is detected, locating the target virtual machine stack frame and taking the top frame of the exception handling stack frame as the target exception handling stack frame; obtaining the target instruction address of the target exception monitoring instruction from the target exception handling stack frame information; and, through the target instruction address, jumping to execute the target exception capture instruction paired with the target exception monitoring instruction, as well as the code in the exception capture instruction block corresponding to the target exception capture instruction, so as to realize the exception handling of the virtual machine.
[0068] Specifically, Figure 2b This embodiment provides a flowchart of a method for handling virtual machine exceptions when the virtual machine malfunctions. The method includes:
[0069] Step 211: When a virtual machine anomaly is detected, backtrack the virtual machine stack frame information from the top of the virtual machine stack to the bottom of the stack, obtain the virtual machine stack frame with the running status flag set to "yes" in the first anomaly monitoring instruction block in the virtual machine stack frame information, and use the virtual machine stack frame as the target virtual machine stack frame.
[0070] In this embodiment, when an exception occurs during virtual machine operation, the virtual machine stack frame information can be traced back from the top of the virtual machine stack to the bottom of the stack to find the virtual machine stack frame with the running status flag set to "yes" in the first exception monitoring instruction block. If such a virtual machine stack frame exists, it is used as the target virtual machine stack frame.
[0071] Step 212: Use the top frame of the exception handling stack frame as the target exception handling stack frame.
[0072] Step 213: Obtain the target instruction address of the target anomaly monitoring instruction from the target anomaly handling stack frame information.
[0073] Step 214: Set the target instruction address as the current execution address to restore the virtual machine runtime environment to the target instruction address.
[0074] Step 215: Push the non-zero value as the abnormal running status identifier corresponding to the target abnormal monitoring instruction onto the virtual machine operand stack.
[0075] In this step, specifically, when the target exception monitoring instruction is executed after an exception occurs in the program, a non-zero value (such as 1) can be pushed onto the virtual machine operand stack as the exception running status identifier corresponding to the target exception monitoring instruction.
[0076] Step 216: After determining that the abnormal running status identifier of the target abnormal monitoring instruction pushed onto the virtual machine operand stack is non-zero by using a conditional jump instruction, skip the abnormal monitoring instruction block corresponding to the target abnormal monitoring instruction and execute the target abnormal capture instruction paired with the target abnormal monitoring instruction.
[0077] Step 217: When executing the target exception capture instruction, push the zero value as the exception running status identifier corresponding to the target exception capture instruction onto the virtual machine operand stack. After determining that the exception running status identifier pushed onto the virtual machine operand stack by the target exception capture instruction is zero through a conditional jump instruction, continue to execute the code in the exception capture instruction block corresponding to the target exception capture instruction.
[0078] In this embodiment, when executing the target anomaly capture instruction, the method further includes: obtaining the level information of the target anomaly monitoring instruction in the target function from the target anomaly handling stack frame information; determining whether the level information of the target anomaly monitoring instruction in the target function is the first level; if so, setting the running status flag in the target anomaly monitoring instruction block in the target virtual machine stack frame to no.
[0079] In this embodiment, the conditional jump virtual machine instructions include two virtual machine instructions: br_if_u16 and br_if_fw_u8.
[0080] In a specific embodiment, suppose the pseudocode of the program is as follows:
[0081]
[0082] If an exception occurs when the program executes the `callA()` function in the exception monitoring instruction block corresponding to `try1`, and the virtual machine stack frame information of the `callA()` function does not set the running state of the exception monitoring instruction block, the program searches the stack frame information of the previous virtual machine stack frame, the `funcA()` function, and finds that the running state flag of the exception monitoring instruction block is "yes". The program then reads the top frame information of the exception handling stack frame, uses the instruction address recorded in the read exception handling stack frame information as the current execution address, restores the virtual machine runtime environment to the location of the `try1` instruction, pushes the exception running state flag 1 corresponding to the `try1` instruction onto the operand stack, and skips the exception monitoring instruction block corresponding to the `try1` instruction using the conditional jump instruction `br_if_u16` or `br_if_fw_u8`, executing the `catch1` instruction. When the catch1 instruction is executed, the top frame of the exception handling stack frame is popped as the target exception handling stack frame. After determining that the try-catch level information in the target exception handling stack frame is the first level, the running status flag in the exception monitoring instruction block recorded in the virtual machine stack frame of the funcA() function can be set to no. At the same time, the exception running status flag 0 corresponding to the catch1 instruction is pushed onto the operand stack. The exception capture instruction block corresponding to catch1 is jumped into through the conditional jump instruction br_if_u16 or br_if_fw_u8, and the code in the exception capture instruction block is continued to be executed.
[0083] By recording the execution flag of the exception monitoring instruction block in the virtual machine stack frame information, and combining it with the exception monitoring instruction address and try-catch level state recorded in the exception handling stack frame, the function where the exception monitoring instruction is located can be quickly retrieved when an exception occurs during the execution of the virtual machine program, thereby improving the exception handling efficiency.
[0084] Based on the above embodiments, when a virtual machine anomaly is detected, if there is no virtual machine stack frame with the running status flag set to "yes" in the anomaly monitoring instruction block in the virtual machine stack frame information, then the virtual machine operation is terminated.
[0085] Figure 2c This is a flowchart of another method for handling virtual machine exceptions when they occur in this embodiment, as shown below. Figure 2c As shown, the method may include:
[0086] After detecting the virtual machine anomaly, we begin searching for the anomaly handling point;
[0087] Determine whether the runtime status flag in the exception monitoring instruction block of the current virtual machine stack frame is "yes";
[0088] If so, use the top frame of the exception handling stack frame as the target exception handling stack frame; read the instruction address recorded in the target exception handling stack frame information as the target exception monitoring instruction address, and restore the virtual machine runtime environment to the target exception monitoring instruction address to start execution;
[0089] If not, determine whether the current virtual machine stack frame is at the bottom of the stack. If yes, terminate the virtual machine operation. If not, backtrack the virtual machine stack frame information from the top of the stack to the bottom of the stack, obtain the previous virtual machine stack frame, and return to execute the operation of determining whether the running status flag in the exception monitoring instruction block of the current virtual machine stack frame is yes.
[0090] Figure 3 This is a schematic diagram of the structure of a virtual machine exception handling instruction execution device provided in an embodiment of the present invention. The device includes: an exception stack frame establishment module 310 and an instruction address writing module 320.
[0091] The exception stack frame establishment module 310 is used to establish an exception handling stack frame corresponding to the exception monitoring instruction when the current function is detected to be executing the exception monitoring instruction normally during the execution of the program by the virtual machine.
[0092] The instruction address writing module 320 is used to write the instruction address of the exception monitoring instruction into the established exception handling stack frame.
[0093] The technical solution provided by this invention provides a WebAssembly virtual machine exception handling method based on the C language. By establishing an exception handling stack frame corresponding to the exception monitoring instruction when the current function is detected to be executing the exception monitoring instruction normally during the execution of the virtual machine program, and writing the instruction address of the exception monitoring instruction to the established exception handling stack frame, this method can quickly locate the instruction position where the exception occurred by using a pre-built exception handling stack frame when the virtual machine runs abnormally, thereby improving the efficiency of virtual machine exception handling.
[0094] Based on the above embodiments, in the current function, the anomaly monitoring instruction or anomaly capture instruction contains at least one level of other anomaly monitoring instructions and anomaly capture instructions; or, in the current function, the anomaly monitoring instruction or anomaly capture instruction does not contain other anomaly monitoring instructions and anomaly capture instructions.
[0095] The exception stack frame establishment module 310 includes:
[0096] The hierarchical information writing unit is used to write the hierarchical information of the exception monitoring instruction in the current function to the exception handling stack frame; wherein, the hierarchical information includes the first level or non-first level.
[0097] The device further includes:
[0098] The normal state recording module is used to push the normal operation state corresponding to the exception monitoring instruction onto the virtual machine operand stack when the current function is executing the exception monitoring instruction normally; so that the virtual machine continues to execute the code in the exception monitoring instruction block corresponding to the exception monitoring instruction; and to push the normal operation state corresponding to the exception catching instruction onto the virtual machine operand stack when the current function is executing the exception catching instruction normally; so that the virtual machine skips the exception catching instruction block corresponding to the exception catching instruction and continues to execute the code after the exception catching instruction block.
[0099] The virtual machine stack frame establishment module is used to establish a virtual machine stack frame corresponding to the current function before the current function begins execution; and to maintain the running status flag in the exception monitoring instruction block of the current function in real time within the virtual machine stack frame.
[0100] The layer determination module is used to query the virtual machine stack frame when the anomaly monitoring instruction is executed normally; if the running status flag in the anomaly monitoring instruction block in the current function is negative, the layer information to which the anomaly monitoring instruction belongs in the current function is determined to be the first layer; if the running status flag in the anomaly monitoring instruction block in the current function is positive, the layer information to which the anomaly monitoring instruction belongs in the current function is determined to be a non-first layer.
[0101] Wherein, when the running status flag in the exception monitoring instruction block of the current function is no, it is used to indicate that the currently executing code is not being monitored for exceptions in the current function; when the running status flag in the exception monitoring instruction block of the current function is yes, it is used to indicate that the currently executing code is being monitored for exceptions in the current function.
[0102] The state maintenance module is used to set the running status flag in the current exception monitoring instruction block to "yes" in the virtual machine stack frame when the current function is executing the exception monitoring instruction normally; and to pop an exception handling stack frame when the current function is executing the exception catching instruction. If the level information recorded in the popped exception handling stack frame is the first level, the running status flag in the current exception monitoring instruction block in the virtual machine stack frame is set to "no".
[0103] The exception handling module is used to locate the target virtual machine stack frame when a virtual machine exception is detected, and take the top frame of the exception handling stack frame as the target exception handling stack frame; obtain the target instruction address of the target exception monitoring instruction from the target exception handling stack frame information; and jump to execute the target exception capture instruction paired with the target exception monitoring instruction and the code in the exception capture instruction block corresponding to the target exception capture instruction through the target instruction address, so as to realize the exception handling of the virtual machine.
[0104] The normal status recording module includes:
[0105] The zero-value recording unit is used to push the zero value onto the virtual machine operand stack as a normal operation status identifier corresponding to the exception monitoring instruction when the current function is executing the exception monitoring instruction normally.
[0106] The non-zero value recording unit is used to push a non-zero value onto the virtual machine operand stack as a normal operation status identifier corresponding to the exception capture instruction when the current function executes the exception capture instruction normally.
[0107] The exception handling module includes:
[0108] The target virtual machine stack frame determination unit is used to backtrack virtual machine stack frame information from the top of the virtual machine stack to the bottom of the stack, obtain the virtual machine stack frame whose running status flag is "yes" in the first exception monitoring instruction block from the virtual machine stack frame information, and use the virtual machine stack frame as the target virtual machine stack frame.
[0109] The exception capture instruction block execution unit is used to take the target instruction address as the current execution address, so that the virtual machine runtime environment is restored to the target instruction address; when executing the target exception monitoring instruction, a non-zero value is pushed onto the virtual machine operand stack as the exception running status identifier corresponding to the target exception monitoring instruction; after determining that the exception running status identifier pushed onto the virtual machine operand stack by the target exception monitoring instruction is a non-zero value through a conditional jump instruction, the exception monitoring instruction block corresponding to the target exception monitoring instruction is skipped, and the target exception capture instruction paired with the target exception monitoring instruction is executed; when executing the target exception capture instruction, a zero value is pushed onto the virtual machine operand stack as the exception running status identifier corresponding to the target exception capture instruction; after determining that the exception running status identifier pushed onto the virtual machine operand stack by the target exception capture instruction is a zero value through a conditional jump instruction, the code in the exception capture instruction block corresponding to the target exception capture instruction continues to be executed;
[0110] The running status flag setting unit is used to obtain the level information of the target exception monitoring instruction in the target function from the target exception handling stack frame information; determine whether the level information of the target exception monitoring instruction in the target function is the first level; if so, set the running status flag in the target exception monitoring instruction block in the target virtual machine stack frame to no.
[0111] The virtual machine termination unit is used to terminate the virtual machine operation if there is no virtual machine stack frame with the running status flag set to "yes" in the abnormal monitoring instruction block in the virtual machine stack frame information.
[0112] The above-described apparatus can execute the methods provided in all the foregoing embodiments of the present invention, and has the corresponding functional modules and beneficial effects for executing the above methods. Technical details not described in detail in the embodiments of the present invention can be found in the methods provided in all the foregoing embodiments of the present invention.
[0113] Figure 4 A schematic diagram of a computer device structure is shown that can be used to implement embodiments of the present invention. The virtual machine can be loaded as a computer program onto various forms of digital computers, such as laptops, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframes, and other suitable computers. The virtual machine program can also be loaded onto mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices (such as helmets, glasses, watches, etc.), and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.
[0114] like Figure 4 As shown, the computer device 10 includes at least one processor 11 and memory, such as non-volatile memory (NVM) 12, random access memory (RAM) 13, etc., communicatively connected to the at least one processor 11. The memory stores computer programs executable by the at least one processor. The processor 11 can perform various appropriate actions and processes based on the computer programs stored in the NVM 12 or loaded from storage unit 18 into the RAM 13. The RAM 13 may also store various programs and data required for the operation of the computer device 10. The processor 11, NVM 12, and RAM 13 are interconnected via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0115] Multiple components in computer device 10 are connected to I / O interface 15, including: input unit 16, such as keyboard, mouse, etc.; output unit 17, such as various types of monitors, speakers, etc.; storage unit 18, such as disk, optical disk, etc.; and communication unit 19, such as network card, modem, wireless transceiver, etc. Communication unit 19 allows computer device 10 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.
[0116] Processor 11 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 11 performs the various methods and processes described above, such as the execution method of virtual machine exception handling instructions.
[0117] In some embodiments, the method for executing virtual machine exception handling instructions may be implemented as a computer program tangibly contained in a computer-readable storage medium, such as storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the computer device 10 via NVM 12 and / or communication unit 19. When the computer program is loaded into RAM 13 and executed by processor 11, one or more steps of the method for executing virtual machine exception handling instructions described above may be performed. Alternatively, in other embodiments, processor 11 may be configured to execute the method for executing virtual machine exception handling instructions by any other suitable means (e.g., by means of firmware).
[0118] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.
[0119] Computer programs used to implement the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs may be executed entirely on a machine, partially on a machine, or as a standalone software package, partially on a machine and partially on a remote machine, or entirely on a remote machine or server.
[0120] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0121] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer device having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer device. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).
[0122] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or computing systems that include middleware components (e.g., application servers), or computing systems that include frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.
[0123] A computing system can include clients and servers. Clients and servers are generally located far apart and typically interact through communication networks. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. The server can be a cloud server, also known as a cloud computing server or cloud host, which is a hosting product within the cloud computing service system to address the shortcomings of traditional physical hosts and VPS services, such as high management difficulty and weak business scalability.
[0124] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.
[0125] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A method for executing virtual machine exception handling instructions, characterized in that, include: During the execution of a program in a virtual machine, when it is detected that the current function is executing an exception monitoring instruction normally, an exception handling stack frame corresponding to the exception monitoring instruction is established. Write the instruction address of the exception monitoring instruction into the established exception handling stack frame; The current function includes exception handling instructions, which include exception monitoring instructions and paired exception capture instructions. The method further includes: When a virtual machine exception is detected, the target virtual machine stack frame is located, and the top frame of the exception handling stack frame is taken as the target exception handling stack frame. Obtain the target instruction address of the target anomaly monitoring instruction from the target anomaly handling stack frame information; By using the target instruction address, the execution jumps to the target exception capture instruction paired with the target exception monitoring instruction, as well as the code in the exception capture instruction block corresponding to the target exception capture instruction, in order to realize exception handling of the virtual machine.
2. The method of claim 1, wherein, Also includes: In the current function, there are at least one level of other exception monitoring instructions and exception catching instructions nested within the exception monitoring instructions or exception catching instructions. Alternatively, within the current function, there are no other exception monitoring or exception catching instructions nested within the exception monitoring or exception catching instructions.
3. The method of claim 2, wherein, After establishing the exception handling stack frame corresponding to the exception monitoring instruction, it also includes: The level information of the exception monitoring instruction in the current function is written into the exception handling stack frame; wherein, the level information includes the first level or a non-first level.
4. The method of claim 3, wherein, Also includes: Before the current function begins execution, a virtual machine stack frame corresponding to the current function is created; as well as In the virtual machine stack frame, the running status flags in the exception monitoring instruction block of the current function are maintained in real time.
5. The method of claim 1, wherein, When a virtual machine anomaly is detected, the target virtual machine stack frame is located, including: Backtrack the virtual machine stack frame information from top to bottom of the virtual machine stack, obtain the virtual machine stack frame whose running status flag is "yes" in the first exception monitoring instruction block in the virtual machine stack frame information, and use the virtual machine stack frame as the target virtual machine stack frame.
6. The method of claim 1, wherein, The execution jumps to the target instruction address and executes the target anomaly capture instruction paired with the target anomaly monitoring instruction, as well as the code in the corresponding anomaly capture instruction block, including: The target instruction address is used as the current execution address, restoring the virtual machine runtime environment to the target instruction address; Push the non-zero value as the abnormal running status identifier corresponding to the target abnormal monitoring instruction onto the virtual machine operand stack; After determining that the abnormal running status flag of the target anomaly monitoring instruction pushed onto the virtual machine operand stack is non-zero by using a conditional jump instruction, the anomaly monitoring instruction block corresponding to the target anomaly monitoring instruction is skipped, and the target anomaly capture instruction paired with the target anomaly monitoring instruction is executed. When executing the target exception capture instruction, a zero value is pushed onto the virtual machine operand stack as the exception running status identifier corresponding to the target exception capture instruction; By using a conditional jump instruction, if the exception status flag pushed onto the virtual machine operand stack by the target exception capture instruction is zero, the code in the exception capture instruction block corresponding to the target exception capture instruction will continue to be executed.
7. An execution device for virtual machine exception handling instructions, characterized in that, The device includes: The exception stack frame creation module is used to create an exception handling stack frame corresponding to the exception monitoring instruction when the current function is detected to be executing the exception monitoring instruction normally during the execution of the program by the virtual machine. The instruction address writing module is used to write the instruction address of the exception monitoring instruction into the established exception handling stack frame. The current function includes exception handling instructions, which include exception monitoring instructions and paired exception capture instructions. The device further includes: The exception handling module is used to locate the target virtual machine stack frame when a virtual machine exception is detected, and take the top frame of the exception handling stack frame as the target exception handling stack frame; obtain the target instruction address of the target exception monitoring instruction from the target exception handling stack frame information; and jump to execute the target exception capture instruction paired with the target exception monitoring instruction, as well as the code in the exception capture instruction block corresponding to the target exception capture instruction, through the target instruction address, so as to realize the exception handling of the virtual machine.
8. A computer device, comprising: The computer device includes: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to execute the virtual machine exception handling instruction execution method according to any one of claims 1-6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that are used to cause a processor to execute the method of executing virtual machine exception handling instructions as described in any one of claims 1-6.