CN-117667738-B - Method and device for WebAssembly module exception handling and electronic equipment
Abstract
The application relates to the technical field of computers and discloses a method for processing an exception of a WebAssembly module, which comprises the steps of determining an exception operation label at a position where an exception operation needs to be executed, obtaining a program to be compiled comprising the exception operation label, compiling the program to be compiled into a corresponding WebAssembly module, converting the exception operation label in the WebAssembly module into an exception processing mechanism instruction, and obtaining an optimized WebAssembly module to process a runtime exception in the optimized WebAssembly module. According to the method, a runtime exception handling mechanism can be provided for a lower-level program language facing the structure through the obtained optimization WebAssembly module, and exception capturing and exception handling of the program running facing the structure can be realized. Meanwhile, by adding an exception handling mechanism, the robustness and the safety of the program in running can be improved. The application also discloses another method for WebAssembly module exception handling, a device for WebAssembly module exception handling and electronic equipment.
Inventors
- LU QIAN
- LIU JINGYA
- ZHANG ZHANG
- ZOU ZHONGREN
Assignees
- 紫光同芯微电子有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20231219
Claims (9)
- 1. A method for WebAssembly module exception handling, characterized by being applied to a structure-oriented programming language, the method comprising: determining an abnormal operation label at a position where abnormal operation needs to be performed; obtaining a program to be compiled comprising an abnormal operation label, and compiling the program to be compiled into a corresponding WebAssembly module; Converting the abnormal operation label in the WebAssembly module into an abnormal processing mechanism instruction to obtain an optimized WebAssembly module to process runtime anomalies in the optimized WebAssembly module; The method comprises the steps of adding an abnormal operation tag function call to a position, needing to execute abnormal operation, of a program to be compiled, carrying out condition judgment on a result value of the abnormal operation tag to obtain a condition judgment statement block, and embedding a program statement, needing to execute abnormal operation, into the condition judgment statement block to obtain the program to be compiled.
- 2. The method of claim 1, wherein converting the exception operation tag in the WebAssembly module into an exception handling mechanism instruction, obtaining an optimized WebAssembly module, comprises: Traversing the code segment of the WebAssembly module, identifying an abnormal operation tag instruction, and determining the instruction type of the abnormal operation tag instruction; determining an abnormal processing mechanism instruction corresponding to the abnormal operation tag instruction according to the instruction type of the abnormal operation tag instruction; An optimization WebAssembly module is generated that includes exception handling mechanism instructions.
- 3. The method of claim 2, wherein determining the exception handling mechanism instruction corresponding to the exception operation tag instruction according to the instruction type of the exception operation tag instruction comprises: determining an operand of the abnormal operation label instruction, and a function name and a function index of the abnormal operation label instruction under the condition that the instruction type of the abnormal operation label instruction is a function call instruction; Determining a corresponding exception handling mechanism instruction according to the operand of the exception operation label instruction, and the function name and the function index of the exception operation label instruction; And/or the number of the groups of groups, Determining an operand of the abnormal operation label instruction and an instruction type and an operand of a previous instruction of the abnormal operation label instruction under the condition that the instruction type of the abnormal operation label instruction is a global variable load instruction; And determining a corresponding exception handling mechanism instruction according to the operand of the exception operation label instruction, the instruction type and the operand of the previous instruction.
- 4. A method for WebAssembly module exception handling, wherein the WebAssembly module is an optimized WebAssembly module as claimed in any one of claims 1 to 3, the method comprising: Determining an exception flag corresponding to the exception handling mechanism instruction according to the execution condition of the monitored instruction in the optimizing WebAssembly module; determining an operand pushed by the exception handling mechanism instruction onto an operand stack according to an exception flag corresponding to the exception handling mechanism instruction; and determining the execution path of the conditional judgment statement block corresponding to the exception handling mechanism instruction according to the operand pushed onto the operand stack by the exception handling mechanism instruction so as to process the exception when the optimizing WebAssembly module runs.
- 5. The method of claim 4, wherein the exception handling mechanism instruction comprises an exception monitor instruction and an exception capture instruction, wherein the execution comprises normal execution or abnormal execution, and wherein before determining the exception flag corresponding to the exception handling mechanism instruction based on the execution of the monitored instruction in the optimization WebAssembly module, determining the exception flag corresponding to the exception monitor instruction as a no-jump flag if the exception monitor instruction is executed for the first time in the optimization WebAssembly module; determining an exception flag corresponding to the exception handling mechanism instruction according to the execution condition of the monitored instruction in the optimization WebAssembly module, including: Under the condition that the execution condition of the monitored instruction in the optimizing WebAssembly module is normal, determining the abnormality mark corresponding to the abnormality capturing instruction as a jump mark, and/or, Under the condition that the execution condition of the monitored instruction in the optimizing WebAssembly module is the execution abnormality, determining that an abnormality mark corresponding to the abnormality monitoring instruction is a jump mark and an abnormality mark corresponding to the abnormality capturing instruction is a non-jump mark.
- 6. The method of claim 4, wherein the exception handling mechanism instruction comprises an exception monitor instruction and an exception capture instruction, the exception flag comprising a jump flag or a no jump flag, wherein determining an operand pushed by the exception handling mechanism instruction onto the operand stack based on the exception flag corresponding to the exception handling mechanism instruction comprises one or more of: Under the condition that an abnormality mark corresponding to the abnormality monitoring instruction is a non-jump mark, pushing the current PC to the top of an abnormality monitoring stack as an abnormality monitoring point at the top of the abnormality monitoring stack, and determining that an operand pushed to an operand stack is the non-jump mark; under the condition that an abnormality mark corresponding to the abnormality monitoring instruction is a jump mark, popping up and executing the data pushed by the monitored instruction onto an operand stack according to the operand of the abnormality monitoring instruction, and determining the operand pushed onto the operand stack as the jump mark; Under the condition that an abnormality mark corresponding to the abnormality capturing instruction is a jump mark, an abnormality monitoring point at the top of an abnormality monitoring stack is popped up, and an operand pushed onto an operand stack is determined to be the jump mark; And determining that the operand pushed onto the operand stack is the non-jump mark under the condition that the exception mark corresponding to the exception capturing instruction is the non-jump mark.
- 7. The method of claim 4, wherein the exception handling mechanism instruction comprises an exception monitor instruction and an exception trap instruction, the operand comprises a skip flag or a no skip flag in the exception flag, and wherein determining the execution path of the conditional predicate statement block corresponding to the exception handling mechanism instruction based on the operand pushed onto the operand stack by the exception handling mechanism instruction to handle the exception when the optimization WebAssembly module is running comprises one or more of: When the operation number pushed by the abnormal monitoring instruction to the operation number stack is a non-jump mark, the jump is not carried out when the condition judgment statement block corresponding to the abnormal monitoring instruction is executed, and the monitored instruction in the condition judgment statement block corresponding to the abnormal monitoring instruction is executed; When the operation number pushed by the abnormality monitoring instruction to the operation number stack is a jump mark, executing a condition judgment statement block corresponding to the abnormality monitoring instruction, and executing the abnormality capturing instruction after jumping to the monitored instruction; When the operand pushed by the exception capturing instruction onto the operand stack is a jump mark, jumping is carried out when a condition judgment statement block corresponding to the exception capturing instruction is executed, and the instruction after jumping to the exception processing instruction in the WebAssembly module is executed; When the number of the pushed operands of the exception capturing instruction to the operand stack is a non-jump mark, the jump is not carried out when the condition judgment statement block corresponding to the exception capturing instruction is executed, and the exception processing instruction in the condition judgment statement block corresponding to the exception capturing instruction is executed.
- 8. An apparatus for WebAssembly module exception handling comprising a processor and a memory storing program instructions, wherein the processor is configured, when executing the program instructions, to perform the method for WebAssembly module exception handling of any one of claims 1 to 7.
- 9. An electronic device, comprising: and (c) an electronic device body, and, The apparatus for WebAssembly module exception handling as defined in claim 8 mounted to an electronic device body.
Description
Method and device for WebAssembly module exception handling and electronic equipment Technical Field The present application relates to the field of computer technologies, and for example, to a method, an apparatus, and an electronic device for WebAssembly module exception handling. Background Currently, resource-constrained devices (Resource Constrained Device) generally refer to electronic devices that have limited power supply, limited computing power, and limited storage capacity, such as smart cards (SMART CARD) and security units (S ecure Element). On these devices, there is usually an on-chip operating system (Chip Operating System, CO S) running, and the COs may communicate with the external interface device through an input or output interface, receive a command from the external interface device, complete processing of the command in its interior, send a command response to the external interface device, and provide the interface device with secure access and control functions such as secure storage of private information, secure execution of important programs, user identity authentication, and the like. The native instructions of WebAssembly module, although supporting exception monitoring instructions and exception capturing instructions, can only encode exception handling mechanism related bytecodes in object oriented high level languages such as c++, go, rust, etc., and cannot encode such bytecodes in lower level languages oriented to structures (e.g., C language), resulting in difficulty in implementing runtime exception handling in structured program languages. It should be noted that the information disclosed in the above background section is only for enhancing understanding of the background of the application and thus may include information that does not form the prior art that is already known to those of ordinary skill in the art. Disclosure of Invention The following presents a simplified summary in order to provide a basic understanding of some aspects of the disclosed embodiments. This summary is not an extensive overview, and is intended to neither identify key/critical elements nor delineate the scope of such embodiments, but is intended as a prelude to the more detailed description that follows. The embodiment of the disclosure provides a method, a device and electronic equipment for WebAssembly module exception handling, which can provide a runtime exception handling mechanism for a lower-level program language facing a structure, and realize exception capturing and exception handling of the program running facing the structure. In some embodiments, the method for WebAssembly module exception handling includes determining an exception operation tag located at a location where an exception operation is to be performed, obtaining a program to be compiled including the exception operation tag, and compiling the program to be compiled into a corresponding WebAssembly module, converting the exception operation tag in the WebAssembly module into an exception handling mechanism instruction, obtaining an optimization WebAssembly module to handle runtime exceptions in the optimization WebAssembly module. The method comprises the steps of obtaining a program to be compiled, wherein the program to be compiled comprises an abnormal operation label, adding the abnormal operation label to a position where the abnormal operation needs to be executed in the program, judging conditions of a result value of the abnormal operation label to obtain a condition judgment statement block, and embedding program statements needing to be executed with the abnormal operation into the condition judgment statement block to obtain the program to be compiled. Optionally, converting the abnormal operation tag in the WebAssembly module into an abnormal processing mechanism instruction to obtain an optimized WebAssembly module, which comprises traversing a code segment of the WebAssembly module, identifying the abnormal operation tag instruction, determining an instruction type of the abnormal operation tag instruction, determining an abnormal processing mechanism instruction corresponding to the abnormal operation tag instruction according to the instruction type of the abnormal operation tag instruction, and generating an optimized WebAssembly module comprising the abnormal processing mechanism instruction. Optionally, determining an exception handling mechanism instruction corresponding to the exception operation tag instruction according to the instruction type of the exception operation tag instruction, wherein the exception handling mechanism instruction comprises determining an operand of the exception operation tag instruction and a function name and a function index of the exception operation tag instruction when the instruction type of the exception operation tag instruction is a function call instruction, determining a corresponding exception handling mechanism instruction according to the operand of