Methods, apparatus, and electronic devices for exception handling in WebAssembly modules
By adding exception operation tags to the WebAssembly module and converting them into exception handling mechanism instructions, the problem that structured languages cannot compile exception monitoring and capture instructions is solved, exception capture and handling are realized, and the robustness and security of the program are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING TONGFANG MICROELECTRONICS
- Filing Date
- 2023-12-19
- Publication Date
- 2026-05-05
AI Technical Summary
In existing technologies, low-level structure-oriented languages such as C cannot compile exception monitoring and capture instructions for WebAssembly modules, making it difficult to handle exceptions during program runtime and easily leading to program crashes or security attacks.
By adding exception operation tags to the program, it is converted into exception handling mechanism instructions, generating an optimized WebAssembly module to realize exception monitoring and capture functions and provide a runtime exception handling mechanism.
It provides runtime exception handling capabilities for structured programming languages, improving program robustness and security.
Smart Images

Figure CN117667738B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and for example to a method, apparatus, and electronic device for exception handling in a WebAssembly module. Background Technology
[0002] Currently, resource-constrained devices typically refer to electronic devices with limited power supply, computing power, and storage capacity, such as smart cards and secure elements. These devices usually run an on-chip operating system (COS). The COS can communicate with external interface devices through input or output interfaces, receive commands from the external interface devices, process the commands internally, and send the command responses back to the external interface devices. It provides secure access and control functions for the interface devices, including secure storage of private information, secure execution of important programs, and user authentication.
[0003] While the WebAssembly module's native instructions support exception monitoring and exception handling instructions, only high-level object-oriented languages such as C++, Go, and Rust can compile the bytecode related to exception handling mechanisms. Lower-level structured languages (such as C) cannot compile this type of bytecode, making it difficult for structured programming languages to implement runtime exception handling.
[0004] It should be noted that the information disclosed in the background section above is only used to enhance the understanding of the background of this application, and therefore may include information that does not constitute prior art known to those skilled in the art. Summary of the Invention
[0005] To provide a basic understanding of some aspects of the disclosed embodiments, a brief summary is given below. This summary is not intended as a general commentary, nor is it intended to identify key / important components or describe the scope of protection of these embodiments, but rather as a prelude to the detailed description that follows.
[0006] This disclosure provides a method, apparatus, and electronic device for exception handling in WebAssembly modules, which can provide a runtime exception handling mechanism for structured, low-level programming languages, enabling exception capture and handling during the runtime of the structured program.
[0007] In some embodiments, the method for exception handling in a WebAssembly module includes: determining an exception operation tag located at the position where an exception operation needs 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 exception handling mechanism instructions, and obtaining an optimized WebAssembly module to handle runtime exceptions in the optimized WebAssembly module.
[0008] Optionally, obtaining the program to be compiled, which includes exception operation tags, includes: adding exception operation tags to the locations in the program where exception operations need to be performed; performing conditional judgments on the result values of the exception operation tags to obtain a conditional judgment statement block; and embedding the program statements that need to perform exception operations into the conditional judgment statement block to obtain the program to be compiled.
[0009] Optionally, the exception operation tags in the WebAssembly module are converted into exception handling mechanism instructions to obtain an optimized WebAssembly module, including: traversing the code segments of the WebAssembly module, identifying exception operation tag instructions, and determining the instruction type of the exception operation tag instructions; determining the exception handling mechanism instructions corresponding to the exception operation tag instructions based on the instruction type of the exception operation tag instructions; and generating an optimized WebAssembly module including exception handling mechanism instructions.
[0010] Optionally, based on the instruction type of the exception operation label instruction, the exception handling mechanism instruction corresponding to the exception operation label instruction is determined, including: if the instruction type of the exception operation label instruction is a function call instruction, determining the operands of the exception operation label instruction, as well as the function name and function index of the exception operation label instruction; determining the corresponding exception handling mechanism instruction based on the operands of the exception operation label instruction, as well as the function name and function index of the exception operation label instruction; and / or, if the instruction type of the exception operation label instruction is a global variable loading instruction, determining the operands of the exception operation label instruction, as well as the instruction type and operands of the instruction preceding the exception operation label instruction; determining the corresponding exception handling mechanism instruction based on the operands of the exception operation label instruction, as well as the instruction type and operands of the instruction preceding the exception operation label instruction.
[0011] In some embodiments, the WebAssembly module is the optimized WebAssembly module as described above. The method for exception handling in the WebAssembly module includes: determining the exception flag corresponding to the exception handling mechanism instruction based on the execution status of the monitored instructions in the optimized WebAssembly module; determining the operands pushed onto the operand stack by the exception handling mechanism instruction based on the exception flag corresponding to the exception handling mechanism instruction; and determining the execution path of the conditional statement block corresponding to the exception handling mechanism instruction based on the operands pushed onto the operand stack by the exception handling mechanism instruction, so as to handle exceptions during the runtime of the optimized WebAssembly module.
[0012] Optionally, the exception handling mechanism instructions include exception monitoring instructions and exception capture instructions.
[0013] Optionally, the exception flags include a jump flag indicating that a jump will occur when executing the conditional statement block corresponding to the exception handling mechanism instruction, or a non-jump flag indicating that a jump will not occur when executing the conditional statement block corresponding to the exception handling mechanism instruction.
[0014] Optionally, the execution status includes normal execution or abnormal execution.
[0015] Optionally, before determining the exception flag corresponding to the exception handling mechanism instruction based on the execution status of the monitored instructions in the optimized WebAssembly module, the method further includes: determining that the exception flag corresponding to the exception monitoring instruction is a non-jump flag when the exception monitoring instruction is executed for the first time in the optimized WebAssembly module.
[0016] Optionally, the exception flag corresponding to the exception handling mechanism instruction is determined based on the execution status of the monitored instructions in the optimized WebAssembly module, including: if the execution status of the monitored instructions in the optimized WebAssembly module is normal, the exception flag corresponding to the exception capture instruction is determined to be a jump flag; and / or, if the execution status of the monitored instructions in the optimized WebAssembly module is abnormal, the exception flag corresponding to the exception monitoring instruction is determined to be a jump flag, and the exception flag corresponding to the exception capture instruction is a no-jump flag.
[0017] Optionally, the operands pushed onto the operand stack by the exception handling mechanism instruction are determined based on the exception flag corresponding to the exception handling mechanism instruction, including one or more of the following operations: if the exception flag corresponding to the exception monitoring instruction is a non-jump flag, the current PC is pushed onto the top of the exception monitoring stack as the exception monitoring point at the top of the exception monitoring stack, and the operands pushed onto the operand stack are determined to be non-jump flags; if the exception flag corresponding to the exception monitoring instruction is a jump flag, the data pushed onto the operand stack when the monitored instruction is executed is popped based on the operands of the exception monitoring instruction, and the operands pushed onto the operand stack are determined to be jump flags; if the exception flag corresponding to the exception catching instruction is a jump flag, the exception monitoring point at the top of the exception monitoring stack is popped, and the operands pushed onto the operand stack are determined to be jump flags; if the exception flag corresponding to the exception catching instruction is a non-jump flag, the operands pushed onto the operand stack are determined to be non-jump flags.
[0018] Optionally, the operands include a jump flag or a no-jump flag in the exception flags.
[0019] Optionally, the execution path of the conditional statement block corresponding to the exception handling mechanism instruction is determined based on the operands pushed onto the operand stack by the exception handling mechanism instruction, in order to handle and optimize exceptions during the runtime of the WebAssembly module, including one or more of the following operations: when the operand pushed onto the operand stack by the exception monitoring instruction is a non-jump flag, no jump is performed when executing the conditional statement block corresponding to the exception monitoring instruction, and the monitored instruction within the conditional statement block corresponding to the exception monitoring instruction is executed; when the operand pushed onto the operand stack by the exception monitoring instruction is a jump flag, the exception monitoring instruction is executed... When the corresponding conditional statement block is executed, a jump occurs, and execution proceeds to the exception handling instruction following the monitored instruction. If the operand pushed onto the operand stack by the exception handling instruction is a jump flag, a jump occurs when the conditional statement block corresponding to the exception handling instruction is executed, and execution proceeds to the instruction following the exception handling instruction in the WebAssembly module. If the operand pushed onto the operand stack by the exception handling instruction is a no-jump flag, no jump occurs when the conditional statement block corresponding to the exception handling instruction is executed, and the exception handling instruction within the conditional statement block corresponding to the exception handling instruction is executed.
[0020] In some embodiments, an apparatus for exception handling of a WebAssembly module includes a processor and a memory storing program instructions, the processor being configured to execute the method for exception handling of a WebAssembly module as described above when running the program instructions.
[0021] In some embodiments, the electronic device includes: an electronic device body; and, as described above, means for exception handling of the WebAssembly module, mounted on the electronic device body.
[0022] The method, apparatus, and electronic device for exception handling in WebAssembly modules provided in this disclosure can achieve the following technical effects:
[0023] By obtaining a program to be compiled that includes exception handling tags, the program is endowed with exception handling capabilities. Based on this, the program is compiled into a WebAssembly module. Since exception handling tags are in a high-level language, they must be converted into machine code—instructions for runtime exception handling mechanisms—to obtain an optimized WebAssembly module that can be executed by the processor. In this way, by executing the optimized WebAssembly module, a runtime exception handling mechanism can be provided for lower-level, structured programming languages, enabling exception capture and handling in structured programs. Furthermore, by adding an exception handling mechanism, the robustness and security of the program at runtime can be improved.
[0024] The above general description and the description below are exemplary and illustrative only and are not intended to limit this application. Attached Figure Description
[0025] One or more embodiments are illustrated by way of example with reference to the accompanying drawings. These illustrations and drawings do not constitute a limitation on the embodiments. Elements having the same reference numerals in the drawings are shown as similar elements. The drawings are not to be scaled. And wherein:
[0026] Figure 1 This is a schematic diagram of the structure of a stacked virtual machine provided in an embodiment of this disclosure;
[0027] Figure 2 A schematic diagram illustrating a method for exception handling in a WebAssembly module provided in an embodiment of this disclosure;
[0028] Figure 3 A schematic diagram illustrating another method for exception handling in a WebAssembly module provided in this embodiment of the disclosure;
[0029] Figure 4 A schematic diagram illustrating another method for exception handling in a WebAssembly module provided in this embodiment of the disclosure;
[0030] Figure 5 A schematic diagram illustrating another method for exception handling in a WebAssembly module provided in this embodiment of the disclosure;
[0031] Figure 6 A schematic diagram of an apparatus for exception handling in a WebAssembly module provided in an embodiment of this disclosure;
[0032] Figure 7 A schematic diagram of another apparatus for exception handling in a WebAssembly module provided in an embodiment of this disclosure. Detailed Implementation
[0033] To provide a more detailed understanding of the features and technical content of the embodiments of this disclosure, the implementation of the embodiments of this disclosure will be described in detail below with reference to the accompanying drawings. The accompanying drawings are for illustrative purposes only and are not intended to limit the embodiments of this disclosure. In the following technical description, for ease of explanation, several details are used to provide a full understanding of the disclosed embodiments. However, one or more embodiments may still be implemented without these details. In other cases, well-known structures and devices may be simplified in their depiction to simplify the drawings.
[0034] The terms "first," "second," etc., used in the specification, claims, and accompanying drawings of this disclosure 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 for the embodiments of this disclosure described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion.
[0035] In this embodiment of the disclosure, the character " / " indicates that the objects before and after it are in an "or" relationship. For example, A / B means: A or B.
[0036] The term "and / or" describes an association between objects, indicating that three relationships can exist. For example, A and / or B means: A or B, or A and B.
[0037] Unless otherwise stated, the term "multiple" means two or more.
[0038] The term "correspondence" can refer to an association or binding relationship. The correspondence between A and B means that there is an association or binding relationship between A and B.
[0039] It should be noted that, unless otherwise specified, the embodiments and features described in the present disclosure can be combined with each other.
[0040] The following explains the technical terms used in the embodiments of this disclosure:
[0041] Virtual machine: The source code is compiled into machine code by the local compiler of the resource-constrained device and embedded as firmware on the resource-constrained device. It interprets and runs the instruction stream embedded or downloaded to the resource-constrained device.
[0042] Instruction stream: The instruction stream is formed by the WebAssembly module, which is compiled from the application source code by the WebAssembly compiler, and then converted by the converter. It is pre-built on the resource-constrained device or downloaded to the resource-constrained device NVM (Non-Volatile Memory) area later.
[0043] WebAssembly Modules: WebAssembly is a new technology for running portable programs in a secure and efficient manner, primarily targeting the Web platform. Its goal is to provide an appropriate low-level abstraction of the intermediate representation of high-level programs; that is, WebAssembly code is intended to be generated by the compiler. It is called a "module" because with WebAssembly, there is no distinction between "programs" and "libraries," only "modules," which can be combined and communicate with each other, and each "module" contains functions.
[0044] Operand stack: Located in the RAM (Random Access Memory) area of resource-constrained devices, the virtual machine interprets the instructions in the instruction stream, pops data and instruction operands from the operand stack according to the function of the instructions, performs the operation, and pushes the operation result onto the operand stack.
[0045] Anomaly monitoring stack: Located in the RAM area of a resource-constrained device, it stores the addresses of try instructions for anomaly monitoring. When a runtime exception occurs, the address of the anomaly monitoring point is obtained from this stack, and execution is transferred to the anomaly monitoring point to continue.
[0046] An instruction is a single command that instructs electronic device (including physical or virtual machines) hardware to perform a certain operation or processing function. It consists of an instruction code and several optional operands.
[0047] Instruction code: also known as opcode, refers to the part of a computer program that specifies the operation to be performed (usually represented by a code). In fact, it is the instruction sequence number, used to tell the CPU which instruction to execute.
[0048] Operands (operand array): Indicates the source of data required for the operation executed by the instruction. Each element of the operand array is an operand of that instruction code.
[0049] Instruction Index: This is the sequence number of the instruction in the original instruction stream, which uniquely identifies the instruction. The instruction index of the first instruction in the original instruction stream is set to 0, and the instruction index of each subsequent instruction is the instruction index of the preceding instruction plus 1.
[0050] Instruction address: The logical address of the instruction in the original instruction stream. The instruction address of the first instruction in the original instruction stream is 0, and the instruction addresses of other instructions are the sum of the instruction address of the preceding instruction and the instruction length.
[0051] Jump target index array: This parameter is only valid for jump type instructions. Jump type instructions include original jump instructions and modified jump instructions. The jump target index array for the `br`, `br if`, `br u16`, `br if u16`, `br fw u8`, `br_if_fw_u8`, `br_bw_u8`, and `br_if_bwu8` instructions has only one element, the first element of which is the instruction index of the target instruction. The jump target index array for the `br_table`, `br_table_u16`, and `br_table_u8` instructions has (N+1) elements, each element corresponding to the instruction index of the target instruction in a jump branch of a jump table instruction.
[0052] The internal hardware of resource-constrained devices typically includes a Central Processing Unit (CPU), Random Access Memory (RAM), Read-Only Memory (ROM), encryption / decryption engines, input / output interfaces, and security sensors. WebAssembly technology initially consisted of bytecode executable in the browser, primarily addressing browser performance issues. The web client compiles languages like C, C++, Go, and Rust into WebAssembly modules, which are then downloaded to the local browser. These modules run at high speed within the browser's virtual machine, achieving native execution efficiency. By removing floating-point types from the native instruction set and optimizing it, this technology can be applied to resource-constrained devices, using a stack-based virtual machine to interpret and run WebAssembly modules. For example... Figure 1 As shown, the stacked virtual machine firmware and WebAssembly module are stored in the NVM area of the resource-constrained device, the instruction code is located in the user data area of the NVM space of the resource-constrained device, the operand stack is located in the RAM area of the resource-constrained device, and the exception monitoring stack is located in the RAM area of the resource-constrained device.
[0053] However, while WebAssembly native instructions support exception handling instructions (i.e., try instructions) and exception catching instructions (i.e., catch instructions), only object-oriented high-level languages (such as Java, C++, Go, Rust, etc.) can compile exception-related bytecode. Structured lower-level languages (such as C) cannot compile these two types of bytecode. In other words, structured programming languages lack exception handling instructions when compiled into WebAssembly modules. This means that structured programming languages do not have the robust exception handling methods found in object-oriented languages like Java and C++, making them prone to errors during runtime that cannot be properly handled, potentially leading to program crashes or security attacks.
[0054] In view of this, the present disclosure provides a method for exception handling in a WebAssembly module, enabling the WebAssembly module to provide runtime exception monitoring and capture functions for lower-level structured programming languages, thereby achieving exception capture and handling during the runtime of the structured program.
[0055] Combination Figure 2 As shown, this disclosure provides a method for exception handling in a WebAssembly module, where the execution entity of the method can be a converter. The method includes:
[0056] S201: The converter determines the abnormal operation label located at the position where an abnormal operation needs to be performed.
[0057] S202: The converter obtains the program to be compiled, which includes the exception operation tag, and compiles the program to be compiled into the corresponding WebAssembly module.
[0058] S203: The converter converts the exception operation tags in the WebAssembly module into exception handling mechanism instructions to obtain an optimized WebAssembly module to handle runtime exceptions in the optimized WebAssembly module.
[0059] In this disclosed embodiment, a program to be compiled, including exception handling tags, is obtained, thereby enabling the program to perform exception handling. Based on this, the program is compiled into a WebAssembly module. Since exception handling tags are in a high-level language, they need to be converted into machine code—specifically, exception handling mechanism instructions for handling runtime exceptions—to obtain an optimized WebAssembly module that can be executed by the processor. Thus, by executing the optimized WebAssembly module, a runtime exception handling mechanism can be provided for lower-level, structured programming languages, enabling exception capture and handling during the runtime of the structured program. Furthermore, by adding an exception handling mechanism, the robustness and security of the program at runtime can be improved.
[0060] For example, the abnormal operation location includes the abnormal monitoring location and the abnormal capture location. The abnormal monitoring location represents the position corresponding to the program statement where abnormal monitoring needs to begin (i.e., the monitored program statement). The abnormal capture location represents the position corresponding to the statement that handles the abnormal after it is captured (i.e., the abnormal handling program statement).
[0061] For example, based on the operation location, abnormal operation tags include abnormal monitoring tags and abnormal capture tags. The location corresponding to the abnormal monitoring tag is the abnormal monitoring location, and the location corresponding to the abnormal capture tag is the abnormal capture location.
[0062] Optionally, determining the exception operation tag located at the position where the exception operation needs to be performed includes: if the position where the exception operation needs to be performed is an exception monitoring position, determining the corresponding exception operation tag as an exception monitoring tag; if the position where the exception operation needs to be performed is an exception capture position, determining the corresponding exception operation tag as an exception capture tag.
[0063] Optionally, obtaining the program to be compiled, including exception operation tags, includes: adding exception operation tags to the locations in the program where exception operations need to be performed; performing conditional checks on the results of the exception operation tags to obtain a conditional statement block; and embedding the program statements that need to perform exception operations into the conditional statement block to obtain the program to be compiled.
[0064] The conditional statement block is represented by the br_if instruction.
[0065] For example, categorized by label type, exception operation labels include exception operation label functions or exception operation label variables.
[0066] For example, the exception operation label function includes exception monitoring label function and exception capture label function.
[0067] For example, exception operation label variables include exception monitoring label variables and exception capture label variables.
[0068] For example, the result value of the exception operation label includes the return value of the exception operation label function or the read value of the exception operation label variable. Specifically, the return value of the exception operation label function includes the return value of the exception monitoring label function and the return value of the exception catching label function. Specifically, the read value of the exception operation label variable includes the read value of the exception monitoring label variable and the read value of the exception catching label variable.
[0069] For example, the program statements that need to perform abnormal operations include monitored program statements and exception handling program statements. The location of the abnormal operation corresponding to the monitored program statement is the exception monitoring location; the location of the abnormal operation corresponding to the exception handling program statement is the exception capture location.
[0070] For example, adding an exception operation label to a location in the program where an exception operation needs to be performed includes: adding an exception operation label to a function call at a location in the program where an exception operation needs to be performed.
[0071] Specifically, adding exception operation tag function calls at locations where exception operations need to be performed in the program includes: adding exception monitoring tag function calls at exception monitoring locations and adding exception catching tag function calls at exception catching locations.
[0072] For example, performing conditional judgment on the result value of the exception operation label to obtain a conditional judgment statement block includes: performing conditional judgment on the return value of the exception operation label function to obtain a conditional judgment statement block.
[0073] Specifically, the conditional statement block for evaluating the return value of the exception operation label function includes: evaluating the return value of the exception monitoring label function to obtain the corresponding conditional statement block; and evaluating the return value of the exception catching label function to obtain the corresponding conditional statement block.
[0074] For example, embedding program statements that need to perform exception operations into conditional statement blocks to obtain the program to be compiled includes: embedding program statements that need to perform exception operations into conditional statement blocks corresponding to exception operation label functions to obtain the program to be compiled.
[0075] Specifically, the program statements that need to perform exception operations are embedded into the conditional statement block corresponding to the exception operation label function, resulting in a program to be compiled that includes: the program statements to be monitored are embedded into the conditional statement block corresponding to the exception monitoring label function, and the exception handling program statements are embedded into the conditional statement block corresponding to the exception catching label function.
[0076] For example, using the exception operation label function, a compiler containing exception operation labels is obtained, and a portion of the compiler is as follows:
[0077]
[0078] Here, StartMonit() represents the exception monitoring label function, and StartCatch() represents the exception catching label function. In this disclosed embodiment, the program to be compiled can add runtime exception monitoring and exception handling functionality to a structured programming language (such as C).
[0079] For example, using the exception handling tag function, the program to be compiled, including the exception handling tag, is obtained. The program to be compiled is then compiled into the corresponding WebAssembly module, part of which is as follows:
[0080]
[0081] It should be noted that the WebAssembly module includes exception operation tag instructions compiled from exception operation tags. That is, when compiling the program to be compiled into the corresponding WebAssembly module, exception operation tags are compiled into exception operation tag instructions. In this disclosed embodiment, the exception operation tag instructions compiled from exception operation tag functions are represented as call instructions.
[0082] It should be noted that the WebAssembly module includes monitored instructions that represent exceptions requiring monitoring and exception handling instructions that represent exception handling. Understandably, the "monitored program statements" in the program to be compiled are compiled into "monitored instructions," and the "exception handling program statements" in the program to be compiled are compiled into "exception handling instructions."
[0083] In this disclosed embodiment, the WebAssembly module is equipped with exception monitoring and exception handling functions by compiling the program to be compiled using the exception operation tag function.
[0084] For example, adding an exception operation label to a location in the program where an exception operation needs to be performed includes adding an exception operation label variable to that location in the program.
[0085] Specifically, adding exception operation label variables at the locations where exception operations need to be performed in the program includes: adding exception monitoring label variables at exception monitoring locations; and adding exception capture label variables at exception capture locations.
[0086] For example, performing conditional judgment on the result value of the exception operation label to obtain the conditional judgment statement block includes: performing conditional judgment on the read value of the exception operation label variable to obtain the conditional judgment statement block.
[0087] Specifically, the conditional judgment block for the read values of exception operation label variables includes: conditional judgment for the read values of exception monitoring label variables, and conditional judgment block for the read values of exception capture label variables.
[0088] For example, embedding program statements that need to perform exception operations into conditional statement blocks to obtain the program to be compiled includes: embedding program statements that need to perform exception operations into conditional statement blocks corresponding to exception operation label variables to obtain the program to be compiled.
[0089] Specifically, the program statements that need to perform exception operations are embedded into the conditional statement blocks corresponding to the exception operation label variables, resulting in a program to be compiled that includes: program statements to be monitored embedded into the conditional statement blocks corresponding to the exception monitoring label variables, and exception handling program statements embedded into the conditional statement blocks corresponding to the exception catching label variables.
[0090] For example, using the exception operation label variable, we obtain the program to be compiled, which includes the exception operation label. Part of the program to be compiled is as follows:
[0091] / / Declare exception monitoring label variable reads using macro definition
[0092] #define StartMonit()(*(volatile unsigned long*)0xAAAAAAAAA)
[0093] / / Declare exception handling tag variable read via macro definition
[0094] #define StartCatch()(*(volatile unsigned long*)0xCCCCCCCC)
[0095] / / Define an anomaly detection macro
[0096] #define TRY if(StartMonit()==0)
[0097] / / Define exception handling macros
[0098] #define CATCH if(StartCatch()==0)
[0099] Where *(volatile unsigned long*)0xAAAAAAAA represents the exception monitoring label variable, and *(volatile unsigned long*)0xCCCCCCCC represents the exception capture label variable.
[0100] In this disclosed embodiment, the program to be compiled can add runtime exception monitoring and exception handling functions to a structured programming language (such as C).
[0101] For example, using exception operation label variables, the program to be compiled, including exception operation labels, is obtained. The program to be compiled is then compiled into a corresponding WebAssembly module, part of which is as follows:
[0102]
[0103]
[0104] In this disclosed embodiment, the exception operation label instruction obtained by compiling the exception operation label variable is represented as the i32.load instruction.
[0105] In this disclosed embodiment, the WebAssembly module is equipped with exception monitoring and exception handling functions by compiling the program to be compiled using the exception operation label variable.
[0106] It should be noted that exception monitoring tag functions can be combined with exception catching tag variables to form exception operation tags. Similarly, exception monitoring tag variables can also be combined with exception catching tag functions to form exception operation tags. For example, an exception monitoring tag function can be added at the exception monitoring location, and an exception catching tag variable can be added at the exception catching location. Alternatively, an exception monitoring tag variable can be added at the exception monitoring location, and an exception catching tag function can be added at the exception catching location. In these cases, the program code will compile accordingly.
[0107] Optionally, converting exception operation tags in the WebAssembly module into exception handling mechanism instructions to obtain an optimized WebAssembly module includes: traversing the code segments of the WebAssembly module, identifying exception operation tag instructions, and determining the instruction type of the exception operation tag instructions. Based on the instruction type of the exception operation tag instructions, determining the exception handling mechanism instructions corresponding to the exception operation tag instructions. Generating an optimized WebAssembly module including the exception handling mechanism instructions.
[0108] It should be noted that the WebAssembly binary file, which is the presentation form of the WebAssembly module, has a total of 11 data segments: type segment, import segment, function segment, table segment, memory segment, global segment, export segment, start segment, element segment, code segment, data segment, and custom segment.
[0109] In this disclosed embodiment, the corresponding exception handling mechanism instruction can be determined by the instruction type of the exception operation label instruction. This allows the exception operation label instruction to be converted into machine code—specifically, exception handling mechanism instructions for handling runtime exceptions—so that the optimized WebAssembly module can be executed by the processor. Since the exception handling mechanism instructions are all located within the functions of the optimized WebAssembly module, when an exception occurs, there is no need to search for exception handling code in a dedicated exception handling handle and determine whether it is within the monitoring range, as is required in a Java card. This disclosed embodiment can directly obtain and execute the exception handling mechanism instructions within the function, resulting in faster execution speed.
[0110] Furthermore, regardless of whether the compiled program is obtained using exception operation label functions or exception operation label variables, the optimized WebAssembly module obtained after converter optimization has a smaller NVM space occupied by the exception handling mechanism framework structure. After being converted into an optimized WebAssembly module, the instructions corresponding to the monitoring, capture, and exception handling functions are all located inside the functions of the WebAssembly module. There is no need to concentrate the exception handling handles in the method components. Compared with Java cards, the binary structure of this embodiment is simple and occupies less space, and the exception handling code can be quickly located after a runtime exception occurs.
[0111] Optionally, determining the exception handling mechanism instruction corresponding to the exception operation label instruction based on its instruction type includes: if the exception operation label instruction is a function call instruction, determining the operands of the exception operation label instruction, as well as the function name and function index of the exception operation label instruction. The corresponding exception handling mechanism instruction is then determined based on the operands of the exception operation label instruction, as well as the function name and function index of the exception operation label instruction.
[0112] The function name and function index of the instruction are obtained through the import and export sections of the WebAssembly module. The function names of exception handling label instructions include exception monitoring label functions and exception catching label functions. The instruction type and operands of the instruction are obtained through the code segment of the WebAssembly module.
[0113] In this disclosed embodiment, when the instruction type of the exception operation label instruction is a function call instruction, it indicates that the exception operation label instruction is compiled from the exception operation label function. In this way, by obtaining the operands of the exception operation label instruction, as well as the function name and function index of the exception operation label instruction, the exception operation label instruction can be converted into the corresponding exception handling mechanism instruction for processing.
[0114] For example, the exception handling mechanism instructions include exception monitoring instructions for indicating exception monitoring and exception catching instructions for indicating exception catching. In this disclosed embodiment, the exception monitoring instructions are represented as try instructions, and the exception catching instructions are represented as catch instructions.
[0115] The exception handling mechanism instructions proposed in this disclosure are shown in Table 1:
[0116] Table 1
[0117]
[0118] For example, based on the operands of the exception handling label instruction and the function name and function index of the exception handling label instruction, the corresponding exception handling mechanism instruction is determined as follows: when the operands of the exception handling label instruction are equal to the function index of the exception monitoring label function, the exception handling label instruction is converted into an exception monitoring instruction. When the operands of the exception handling label instruction are equal to the function index of the exception catching label function, the exception handling label instruction is converted into an exception catching instruction. The operands of the exception monitoring instruction are the number of operands on the operand stack before the exception handling label instruction is executed. The exception catching instruction has no operands.
[0119] Understandably, for example, if the exception handling label instruction is a `call` instruction, and the operand of the `call` instruction equals the function index of the exception monitoring label function, then the `call` instruction is converted to a `try` instruction. The operand of the `try` instruction is the number of operands on the operand stack before the `call` instruction is executed. If the operand of the `call` instruction equals the function index of the exception catching label function, then the `call` instruction is converted to a `catch` instruction. The `catch` instruction has no operands.
[0120] Optionally, determining the exception handling mechanism instruction corresponding to the exception operation label instruction based on its instruction type includes: if the instruction type of the exception operation label instruction is a global variable loading instruction, determining the operands of the exception operation label instruction, as well as the instruction type and operands of the instruction preceding the exception operation label instruction. The corresponding exception handling mechanism instruction is then determined based on the operands of the exception operation label instruction and the instruction type and operands of the preceding instruction.
[0121] For example, determining the corresponding exception handling mechanism instruction based on the operands of the exception operation label instruction, and the instruction type and operands of the previous instruction, includes: if the instruction type of the previous instruction is a constant load instruction, and the sum of the second operand of the exception operation label instruction and the operands of the previous instruction equals the address read by the exception monitoring label variable, deleting the previous instruction and converting the exception operation label instruction into an exception monitoring instruction. If the instruction type of the previous instruction is a constant load instruction, and the sum of the second operand of the exception operation label instruction and the operands of the previous instruction equals the address read by the exception catching label variable, deleting the previous instruction and converting the exception operation label instruction into an exception catching instruction.
[0122] For example, the constant loading instruction is represented as the i32.const instruction.
[0123] Understandably, for example, if the exception handling label instruction is `i32.load`, and the instruction preceding `i32.load` is `i32.const`, and the sum of the second operand of `i32.load` and the operand of `i32.const` equals the address read from the exception handling label variable, then `i32.load` is converted to a `try` instruction. The operand of the `try` instruction is the number of operands on the operand stack before the execution of `i32.load`. If the instruction preceding `i32.load` is `i32.const`, and the sum of the second operand of `i32.load` and the operand of `i32.const` equals the address read from the exception handling label variable, then `i32.load` is converted to a `catch` instruction.
[0124] For example, the following section of the WebAssembly module has been optimized:
[0125] try xx
[0126] br_if(address of the catch instruction)
[0127] Monitored instructions
[0128] catch
[0129] br_if(address of the instruction following the exception handling instruction)
[0130] Exception handling instructions
[0131] ...
[0132] Optionally, determining the exception handling mechanism instruction corresponding to the exception operation label instruction based on its instruction type includes: If the exception operation label instruction is a function call instruction, determining the operands, function name, and function index of the exception operation label instruction. Then, determining the corresponding exception handling mechanism instruction based on the operands, function name, and function index of the exception operation label instruction. If the exception operation label instruction is a global variable loading instruction, determining the operands, instruction type, and operands of the instruction preceding the exception operation label instruction. Then, determining the corresponding exception handling mechanism instruction based on the operands, instruction type, and operands of the instruction preceding the exception operation label instruction.
[0133] Optionally, the WebAssembly module can be optimized for resource-constrained devices.
[0134] For example, when executing an optimized WebAssembly module, the optimized WebAssembly module needs to be downloaded to a resource-constrained device, and the optimized WebAssembly module needs to be executed using a virtual machine on the resource-constrained device.
[0135] Specifically, obtaining the optimized WebAssembly module is accomplished by a converter located outside the resource-constrained device. After obtaining the optimized WebAssembly module, the converter directly provides it to the resource-constrained device for execution, essentially downloading the optimized WebAssembly module to the resource-constrained device. Executing the optimized WebAssembly module via a virtual machine on the resource-constrained device reduces the workload on the electronic device when executing instructions, further accelerating the execution speed.
[0136] The execution of exception handling mechanism instructions is illustrated by example:
[0137] Exception monitoring instructions (try instructions): When the try instruction executes normally without an exception, the virtual machine pushes the current program counter (PC) onto the exception monitoring stack and pushes a non-jump flag onto the operand stack. Then, it executes the br_if instruction following the try instruction. When executing this br_if instruction, because the top of the operand stack is marked with a non-jump flag, the virtual machine does not jump; instead, it continues executing the monitored instruction until it reaches the catch instruction. If an exception occurs and the try instruction is executed, the virtual machine has already encountered an exception. Based on the operand of the try instruction, the virtual machine pops the data pushed onto the operand stack by the monitored instruction and pushes a jump flag onto the operand stack. Then, it executes the br_if instruction following the try instruction. When executing this br_if instruction, because the top of the operand stack is marked with a jump flag, the virtual machine jumps to the target address indicated by the br_if operand, i.e., skipping the monitored instruction and directly executing the catch instruction.
[0138] Exception handling instructions (catch instructions): When a catch instruction is executed normally, if no exception occurs in the virtual machine, a jump flag is pushed onto the operand stack, and then the br_if instruction following the catch block is executed. When this br_if instruction is executed, because the top of the operand stack is marked with a jump flag, the virtual machine jumps to the target address indicated by the br_if operand; that is, the virtual machine skips the exception handling instructions and executes the instructions following the catch block. If an exception occurs and the catch instruction is executed, the virtual machine has already encountered an exception. A non-jump flag is pushed onto the operand stack, and then the br_if instruction following the catch block is executed. When this br_if instruction is executed, because the top of the operand stack is marked with a non-jump flag, the virtual machine does not jump; instead, it executes the exception handling instructions immediately following the br_if instruction.
[0139] Combination Figure 3 As shown, another method for exception handling in a WebAssembly module is provided in this embodiment of the present disclosure, wherein the WebAssembly module is the optimized WebAssembly module as described above, and the execution subject of this method can be a virtual machine. The method includes:
[0140] S301: The virtual machine determines the exception flag corresponding to the exception handling mechanism instruction based on the execution status of the monitored instructions in the optimized WebAssembly module.
[0141] The execution status includes whether the execution is normal or abnormal.
[0142] S302: The virtual machine determines the operands to be pushed onto the operand stack by the exception handling mechanism instruction based on the exception flag corresponding to the exception handling mechanism instruction.
[0143] S303: The virtual machine determines the execution path of the conditional statement block corresponding to the exception handling mechanism instruction based on the operands pushed onto the operand stack by the exception handling mechanism instruction, so as to handle and optimize the exceptions during the runtime of the WebAssembly module.
[0144] The exception flag, located in the RAM area of the resource-constrained device, is used to record whether a runtime exception exists. This exception flag determines the operands pushed onto the operand stack by the try instruction (exception monitoring instruction), thus determining whether the "monitored instruction" is executed. It also determines the operands pushed onto the operand stack by the catch instruction (exception catching instruction), thus determining whether the "exception handling instruction" is executed.
[0145] For example, the exception flags include a jump flag indicating that a jump will occur when executing the conditional statement block corresponding to the exception handling mechanism instruction, or a non-jump flag indicating that a jump will not occur when executing the conditional statement block corresponding to the exception handling mechanism instruction.
[0146] In this disclosed embodiment, by setting the exception flag corresponding to the exception handling mechanism instruction, the operands pushed onto the operand stack by the exception handling mechanism instruction are determined, thereby determining whether to execute the exception handling instruction. This enables direct handling of runtime exceptions when executing the optimized WebAssembly module, resulting in fast execution speed.
[0147] Optionally, before determining the exception flag corresponding to the exception handling mechanism instruction based on the execution status of the monitored instructions in the optimized WebAssembly module, the method further includes: when the exception monitoring instruction is executed for the first time in the optimized WebAssembly module, determining that the exception flag corresponding to the exception monitoring instruction is a non-jump flag.
[0148] Optionally, determining the exception flag corresponding to the exception handling mechanism instruction based on the execution status of the monitored instructions in the optimized WebAssembly module includes: if the execution status of the monitored instructions in the optimized WebAssembly module is normal, determining the exception flag corresponding to the exception catching instruction as a jump flag. And / or, if the execution status of the monitored instructions in the optimized WebAssembly module is abnormal, determining the exception flag corresponding to the exception monitoring instruction as a jump flag, and the exception flag corresponding to the exception catching instruction as a no-jump flag.
[0149] In this disclosed embodiment, by determining the exception flag corresponding to the exception handling mechanism instruction, it is beneficial to subsequently determine the operands pushed onto the operand stack by the exception handling mechanism instruction, thereby determining the execution path of the condition judgment statement block corresponding to the exception handling mechanism instruction.
[0150] Optionally, the operands pushed onto the operand stack by the exception handling mechanism instruction are determined based on the exception flag corresponding to the exception handling mechanism instruction. This includes one or more of the following operations: If the exception flag corresponding to the exception monitoring instruction is a non-jump flag, the current PC is pushed onto the top of the exception monitoring stack as the exception monitoring point at the top of the stack, and the operands pushed onto the operand stack are determined to be non-jump flags. If the exception flag corresponding to the exception monitoring instruction is a jump flag, data pushed onto the operand stack by the monitored instruction is popped based on the operands of the exception monitoring instruction, and the operands pushed onto the operand stack are determined to be jump flags. If the exception flag corresponding to the exception catching instruction is a jump flag, the exception monitoring point at the top of the exception monitoring stack is popped, and the operands pushed onto the operand stack are determined to be jump flags. If the exception flag corresponding to the exception catching instruction is a non-jump flag, the operands pushed onto the operand stack are determined to be non-jump flags.
[0151] In this disclosed embodiment, during the anomaly monitoring phase, the operands pushed onto the operand stack are determined by the anomaly flag corresponding to the anomaly monitoring instruction. During the anomaly capture phase, the operands pushed onto the operand stack are determined by the anomaly flag corresponding to the anomaly capture instruction. This allows for further determination of the execution path of the conditional statement block corresponding to the anomaly handling mechanism instruction. Furthermore, by maintaining an anomaly monitoring stack within the RAM of the resource-constrained device, when an anomaly occurs, the location of the anomaly monitoring point can be found directly by sequentially traversing from the top of the stack, thereby quickly locating the anomaly capture instruction for anomaly handling. This improves the efficiency of runtime anomaly handling.
[0152] For example, operands include a jump flag or a no-jump flag in the exception flags.
[0153] Optionally, the execution path of the conditional statement block corresponding to the exception handling mechanism instruction is determined based on the operands pushed onto the operand stack by the exception handling mechanism instruction, in order to handle and optimize exceptions during the runtime of the WebAssembly module. This includes one or more of the following operations: If the operand pushed onto the operand stack by the exception monitoring instruction is a non-jump flag, no jump is performed when executing the conditional statement block corresponding to the exception monitoring instruction, and the monitored instruction within the conditional statement block is executed. If the operand pushed onto the operand stack by the exception monitoring instruction is a jump flag, a jump is performed when executing the conditional statement block corresponding to the exception monitoring instruction, and the execution jumps to the exception catching instruction following the monitored instruction. If the operand pushed onto the operand stack by the exception catching instruction is a jump flag, a jump is performed when executing the conditional statement block corresponding to the exception catching instruction, and the execution jumps to the instruction following the exception handling instruction in the WebAssembly module. If the operand pushed onto the operand stack by the exception capture instruction is a non-jump flag, no jump will occur when executing the conditional statement block corresponding to the exception capture instruction, and the exception handling instructions within the conditional statement block corresponding to the exception capture instruction will be executed.
[0154] In this disclosed embodiment, by using the exception flags corresponding to the exception monitoring instructions and exception catching instructions, it is determined whether a jump should occur when executing the corresponding conditional statement block, thereby determining whether to execute the monitored instructions and exception handling instructions, in order to handle and optimize exceptions during the program runtime of the WebAssembly module. This effectively addresses abnormal situations during operation, ensuring the stability and reliability of the program.
[0155] Combination Figure 4 As shown, another method for exception handling in a WebAssembly module provided in this disclosure includes:
[0156] S401: When the virtual machine executes the exception monitoring instruction for the first time in the optimized WebAssembly module, it determines that the exception flag corresponding to the exception monitoring instruction is the non-jump flag.
[0157] S402: The virtual machine obtains information on the execution of monitored instructions in the optimized WebAssembly module.
[0158] S403: If the execution of the monitored instructions in the WebAssembly module is normal, the virtual machine determines that the exception flag corresponding to the exception capture instruction is a jump flag.
[0159] S404: When the virtual machine executes an exception monitoring instruction, it pushes the current PC onto the top of the exception monitoring stack as the exception monitoring point at the top of the exception monitoring stack, and pushes a no-jump flag onto the operand stack.
[0160] S405: When the virtual machine executes the conditional statement block corresponding to the exception monitoring instruction, it does not jump, but executes the monitored instruction in the conditional statement block corresponding to the exception monitoring instruction.
[0161] S406: When the virtual machine executes an exception capture instruction, it pops the exception capture point at the top of the exception monitoring stack and pushes a jump flag onto the operand stack.
[0162] S407: When the virtual machine executes the conditional statement block corresponding to the exception handling instruction, it jumps to other instructions after the exception handling instruction for execution.
[0163] S408: When an exception occurs in the monitored instruction of the virtual machine during the execution of the optimized WebAssembly module, the exception flag corresponding to the exception monitoring instruction is determined to be a jump flag, and the exception flag corresponding to the exception capture instruction is determined to be a non-jump flag.
[0164] S409: When the virtual machine executes an exception monitoring instruction, it pops the data pushed onto the operand stack by the monitored instruction according to the operand of the exception monitoring instruction, and pushes a jump flag onto the operand stack.
[0165] S410: When the virtual machine executes the conditional statement block corresponding to the exception monitoring instruction, it jumps to the exception capture instruction after the monitored instruction for execution.
[0166] S411: When the virtual machine executes an exception capture instruction, it pushes a no-jump flag onto the operand stack.
[0167] S412: When the virtual machine executes the conditional statement block corresponding to the exception capture instruction, it does not jump, but executes the exception handling instructions in the conditional statement block corresponding to the exception capture instruction to handle and optimize the exceptions during the runtime of the WebAssembly module.
[0168] In this disclosed embodiment, the operations of executing the exception monitoring instruction, executing the condition judgment statement block corresponding to the exception monitoring instruction, executing the exception capture instruction, and executing the condition judgment statement block corresponding to the exception capture instruction are determined so that when an exception occurs in the monitored instruction, the exception is captured and processed, so that exceptions occurring during program execution can be handled correctly, avoiding program crashes or security attacks, and improving the robustness and security of the program during runtime.
[0169] Optionally, the method further includes: if the execution of the monitored instruction in the optimized WebAssembly module is abnormal, pop the abnormal monitoring point from the top of the abnormal monitoring stack, point the PC to the abnormal monitoring instruction corresponding to the abnormal monitoring point, and start running again from the abnormal monitoring instruction.
[0170] Combination Figure 5 As shown, another method for exception handling in a WebAssembly module provided in this disclosure includes:
[0171] S501: When the virtual machine executes an exception monitoring instruction, it pushes the current PC onto the top of the exception monitoring stack as the exception monitoring point at the top of the exception monitoring stack, and pushes a no-jump flag onto the operand stack.
[0172] S502: When the virtual machine executes the conditional statement block corresponding to the exception monitoring instruction, no jump is performed.
[0173] S503: The virtual machine executes the monitored instructions sequentially until the monitored instructions have been completed.
[0174] S504: The virtual machine determines whether an exception occurred during the execution of the monitored instruction. If no exception occurred, proceed to S505. If an exception occurred, proceed to S507.
[0175] Among them, such as Figure 5 As shown, "No abnormality" indicates "No"; "Abnormality" indicates "Yes".
[0176] S505: When the virtual machine executes an exception capture instruction, it pops the exception monitoring point at the top of the exception monitoring stack and pushes a jump flag onto the operand stack.
[0177] S506: When the virtual machine executes the conditional statement block corresponding to the exception handling instruction, it jumps to other instructions after the exception handling instruction for execution.
[0178] It is understandable that the execution jumps to the instructions following the exception handling instruction and ends the current try-catch block.
[0179] S507: The virtual machine sets the runtime exception flag, pops the exception monitoring point from the top of the exception monitoring stack, and jumps to the address of the exception monitoring instruction.
[0180] Setting the runtime exception flag means resetting the runtime exception flag.
[0181] S508: The virtual machine pops the data pushed onto the operand stack by the monitored instruction based on the operand of the exception monitoring instruction, and pushes a jump flag onto the operand stack.
[0182] S509: When the virtual machine executes the conditional statement block corresponding to the exception monitoring instruction, it jumps to the exception capture instruction after the monitored instruction for execution.
[0183] S510: When the virtual machine executes an exception capture instruction, a non-jump flag is pushed onto the operand stack.
[0184] S511: When the virtual machine executes the conditional statement block corresponding to the exception capture instruction, it does not jump, but executes the exception handling instructions in the conditional statement block corresponding to the exception capture instruction to handle and optimize the exceptions during the runtime of the WebAssembly module.
[0185] In this disclosed embodiment, by setting an exception flag in the RAM of a resource-constrained device, the operands pushed onto the operand stack by the try and catch instructions are set according to the value of the exception flag, thereby changing whether the subsequent br_if jump occurs. This ensures that the try-catch block only executes the "monitored instruction" when no exception occurs; when an exception occurs during the execution of the "monitored instruction", the operand stack can be restored to the state before the execution of the "monitored instruction" to execute the "exception handling instruction", so as to correctly handle runtime exceptions and improve the efficiency of runtime exception handling.
[0186] Optionally, obtaining the optimized WebAssembly module further includes: determining the target instruction block linked to the original jump instruction based on the initial operand array of the original jump instruction in the original instruction stream; obtaining the structure of the target instruction block linked to the original jump instruction; determining the target instruction linked to the original jump instruction based on the structure of the target instruction block linked to the original jump instruction; setting the elements of the jump target index array of the original jump instruction to the instruction index of the linked target instruction; deleting the target instruction in the original instruction stream; determining that the jump target instruction linked to the original jump instruction is the first undeleted instruction following the target instruction linked to the original jump instruction; setting the elements of the jump target index array of the original jump instruction to the instruction index of the linked jump target instruction; updating the instruction addresses of all instructions in the original instruction stream; and determining the instruction address of the jump target instruction linked to the original jump instruction based on the elements of the jump target index array of the original jump instruction. The improved jump instruction is obtained by replacing the instruction code of the original jump instruction with the instruction code of the improved conditional jump instruction, and replacing the elements of the operand array of the original jump instruction with the instruction address of the linked target instruction. The instruction code of the improved jump instruction indicates the jump direction and operand array type, while the operand array indicates the jump distance.
[0187] The original instruction stream is located in the WebAssembly module.
[0188] The primitive jump instructions include one or more of the following: the `br` instruction (representing an unconditional jump), the `br_if` instruction (representing a conditional jump), and the `br_table` instruction (representing a jump table). The instruction code for the `br` instruction is `br`, the instruction code for the `br_if` instruction is `br_if`, and the instruction code for the `br_table` instruction is `br_table`.
[0189] The improved jump instructions include one or more of the following: br_u16, br_fw_u8, and br_bw_u8 instructions representing unconditional jumps; br_if_u16, br_if_fw_u8, and br_if_bw_u8 instructions representing conditional jumps; and br_table_u16 and br_table_u8 instructions representing jump tables. The corresponding instruction codes are: br_u16, br_fw_u8, br_bw_u8, br_if_u16, br_if_fw_u8, br_if_bw_u8, br_table_u16, and br_table_u8. The br_u16, br_fw_u8, and br_bw_u8 instructions are obtained by conversion from the br instruction. The br_if_u16, br_if_fw_u8, and br_if_bw_u8 instructions are obtained by conversion from the br_if instruction. The br_table_u16 and br_table_u8 instructions are derived from the br_table instructions.
[0190] It should be noted that when the instruction code is a br instruction or a br_if instruction, the operand array of this instruction has only one element. When the instruction code is a br_table instruction, the operand array of this instruction has (N+2) elements, where N is the first operand of the br_table instruction.
[0191] Optionally, the target instruction includes a block start instruction and a block end instruction. The block start instruction includes a block instruction and a loop instruction. The block end instruction includes an end instruction. The target instruction block includes a block-end structure composed of pairs of block instructions and end instructions, or a loop-end structure composed of pairs of loop instructions and end instructions.
[0192] Specifically, when optimizing the WebAssembly module, only the instruction code and operand array are output; all other instruction information is not output.
[0193] The improved jump instruction and its execution description proposed in the embodiments of this disclosure are shown in Table 2.
[0194] Table 2
[0195]
[0196]
[0197] In this disclosed embodiment, by directly mapping the original jump instruction to the target instruction, it is no longer necessary to sequentially and step-by-step search outwards to determine the target instruction when interpreting and executing the original jump instruction. By deleting the target instruction from the original instruction stream, the NVM storage space occupied is reduced. Since deleting the target instruction changes the instruction addresses of all instructions in the original instruction stream, the original jump instruction is converted into an improved jump instruction after updating the instruction addresses of all instructions in the original instruction stream. The instruction code of the converted improved jump instruction can be used to indicate the jump direction and operand array type. The operand array can be used to indicate the jump distance; that is, the operand array is the target address. This makes the converted improved jump instruction bytecode independent of other bytecode, making it easier for electronic devices to interpret and execute. Therefore, this method can reduce the storage space occupied by instructions, improve the execution efficiency of WebAssembly module instructions, and is more suitable for running on resource-constrained devices.
[0198] It should be noted that, in this disclosed embodiment, the conditional statement block in the WebAssembly module is optimized to be one or more of the br_if_u16, br_if_fw_u8, and br_if_bw_u8 instructions.
[0199] Combination Figure 6 As shown, this disclosure provides an apparatus 200 for exception handling in WebAssembly modules, including a determining module 21, a compiling module 22, and a conversion module 23. The determining module 21 is configured to determine exception operation tags located at the position where an exception operation needs to be performed. The compiling module 22 is configured to obtain a program to be compiled, including the exception operation tags, and compile the program into a corresponding WebAssembly module. The conversion module 23 is configured to convert the exception operation tags in the WebAssembly module into exception handling mechanism instructions, thereby obtaining an optimized WebAssembly module to handle runtime exceptions in the optimized WebAssembly module. This enables runtime exception handling for programs in relatively low-level, structure-oriented programming languages and improves the robustness and security of the program at runtime.
[0200] Combination Figure 7As shown, this embodiment of the disclosure provides an apparatus 300 for exception handling of a WebAssembly module, including a processor 700 and a memory 701. Optionally, the apparatus 300 for exception handling of a WebAssembly module may further include a communication interface 702 and a bus 703. The processor 700, communication interface 702, and memory 701 can communicate with each other via the bus 703. The communication interface 702 can be used for information transmission. The processor 700 can call logical instructions in the memory 701 to execute the method for exception handling of a WebAssembly module as described above. Furthermore, the logical instructions in the memory 701 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium.
[0201] The memory 701, as a computer-readable storage medium, can be used to store software programs and computer-executable programs, such as program instructions / modules corresponding to the methods in the embodiments of this disclosure. The processor 700 executes functional applications and data processing by running the program instructions / modules stored in the memory 701, thereby implementing the method for exception handling of the WebAssembly module described in the above embodiments.
[0202] The memory 701 may include a program storage area and a data storage area. The program storage area may store the operating system and application programs required for at least one function; the data storage area may store data created based on the use of the terminal device. Furthermore, the memory 701 may include high-speed random access memory and may also include non-volatile memory.
[0203] This disclosure provides an electronic device, including: an electronic device body and a device for handling WebAssembly module anomalies, as described above, mounted on the electronic device body. The mounting relationship described herein is not limited to placement within the electronic device body, but also includes mounting connections with other components of the product, including but not limited to physical connections, electrical connections, or signal transmission connections. Those skilled in the art will understand that the device for handling WebAssembly module anomalies can be adapted to suitable electronic device bodies to achieve other feasible embodiments.
[0204] Optionally, the electronic device itself includes a physical machine or a virtual machine. If the electronic device is a physical machine, the device for exception handling of the WebAssembly module can be installed externally; if the electronic device is a virtual machine, the device for exception handling of the WebAssembly module can be installed internally.
[0205] This disclosure provides a computer-readable storage medium storing computer-executable instructions configured to perform the method for exception handling of the WebAssembly module described in the above embodiments.
[0206] This disclosure provides a computer program product comprising a computer program stored on a computer-readable storage medium. The computer program includes program instructions that, when executed by a computer, cause the computer to perform the method for exception handling in the WebAssembly module described above. The computer-readable storage medium may be a transient computer-readable storage medium or a non-transitory computer-readable storage medium.
[0207] The technical solutions of this disclosure can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes one or more instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in this disclosure. The aforementioned storage medium can be a non-transitory storage medium, including: a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, and other media capable of storing program code; it can also be a transient storage medium.
[0208] The foregoing description and accompanying drawings fully illustrate embodiments of this application to enable those skilled in the art to practice them. Other embodiments may include structural, logical, electrical, procedural, and other changes. The embodiments represent only possible variations. Individual components and functions are optional unless explicitly required, and the order of operation may vary. Parts and features of some embodiments may be included in or replace parts and features of other embodiments. Moreover, the terminology used in this application is for describing embodiments only and is not intended to limit the claims. As used in the description of embodiments and claims, the singular forms “a,” “an,” and “the” are intended to equally include the plural forms unless the context clearly indicates otherwise. Similarly, the term “and / or,” as used herein, means including one or more of the associated listed items and all possible combinations thereof. Additionally, when used in this application, the term "comprise" and its variations "comprises" and / or "comprising" refer to the presence of stated features, integrals, steps, operations, elements, and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components, and / or groups thereof. Without further limitations, an element defined by the phrase "comprises a..." does not exclude the presence of other identical elements in the process, method, or apparatus that includes said element. In this document, each embodiment may focus on the differences from other embodiments, and similar or identical parts between embodiments can be referred to mutually. For methods, products, etc., disclosed in the embodiments, if they correspond to the method section disclosed in the embodiments, the relevant parts can be referred to the description of the method section.
[0209] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. In some alternative implementations, the functions marked in the blocks may occur in a different order than that shown in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. In the descriptions corresponding to the flowcharts and block diagrams in the accompanying drawings, the operations or steps corresponding to different blocks may also occur in a different order than disclosed in the description, and sometimes there is no specific order between different operations or steps. For example, two consecutive operations or steps may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. Each block in a block diagram and / or flowchart, and combinations of blocks in a block diagram and / or flowchart, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
Claims
1. A method for exception handling in a WebAssembly module, characterized in that, Applied to structured programming languages, the methods include: Identify the abnormal operation label located at the position where the abnormal operation needs to be performed; Obtain the program to be compiled, including the exception operation tag, and compile the program to be compiled into the corresponding WebAssembly module; Convert exception operation tags in the WebAssembly module into exception handling mechanism instructions to obtain an optimized WebAssembly module to handle runtime exceptions in the optimized WebAssembly module; The process of obtaining the program to be compiled, which includes exception operation tags, involves: adding exception operation tag function calls to the locations in the program where exception operations need to be performed; performing conditional judgments on the result values of the exception operation tags to obtain a conditional judgment statement block; and embedding the program statements that need to perform exception operations into the conditional judgment statement block to obtain the program to be compiled.
2. The method according to claim 1, characterized in that, Converting exception handling tags in the WebAssembly module into exception handling mechanism instructions results in an optimized WebAssembly module, including: Traverse the code segments of the WebAssembly module, identify exception operation tag instructions, and determine the instruction type of the exception operation tag instructions; Based on the instruction type of the abnormal operation label instruction, determine the abnormal handling mechanism instruction corresponding to the abnormal operation label instruction; Generate an optimized WebAssembly module that includes instructions for exception handling mechanisms.
3. The method according to claim 2, characterized in that, Based on the instruction type of the exception operation label instruction, determine the exception handling mechanism instruction corresponding to the exception operation label instruction, including: When the instruction type of the exception operation label instruction is a function call instruction, determine the operands of the exception operation label instruction, as well as the function name and function index of the exception operation label instruction; Based on the operands of the exception operation label instruction, as well as the function name and function index of the exception operation label instruction, determine the corresponding exception handling mechanism instruction; And / or, When the instruction type of the exception operation label instruction is a global variable loading instruction, determine the operands of the exception operation label instruction, as well as the instruction type and operands of the instruction preceding the exception operation label instruction. Based on the operands of the exception operation label instruction, as well as the instruction type and operands of the previous instruction, determine the corresponding exception handling mechanism instruction.
4. A method for exception handling in a WebAssembly module, characterized in that, The WebAssembly module is an optimized WebAssembly module as described in any one of claims 1 to 3, the method comprising: The exception flag corresponding to the exception handling mechanism instruction is determined based on the execution status of the monitored instructions in the optimized WebAssembly module; The operands pushed onto the operand stack by the exception handling mechanism instruction are determined based on the exception flag corresponding to the exception handling mechanism instruction. The execution path of the conditional statement block corresponding to the exception handling mechanism instruction is determined based on the operands pushed onto the operand stack according to the exception handling mechanism instruction, so as to handle and optimize the exceptions during the runtime of the WebAssembly module.
5. The method according to claim 4, characterized in that, The exception handling mechanism instructions include exception monitoring instructions and exception capture instructions; the execution status includes normal execution or execution exception; before determining the exception flag corresponding to the exception handling mechanism instruction based on the execution status of the monitored instructions in the optimized WebAssembly module, it also includes: in the case of the exception monitoring instruction being executed for the first time in the optimized WebAssembly module, determining the exception flag corresponding to the exception monitoring instruction to be a non-jump flag; Based on the execution status of monitored instructions in the optimized WebAssembly module, the exception flags corresponding to the exception handling mechanism instructions are determined, including: If the execution of monitored instructions in the WebAssembly module is normal, determine that the exception flag corresponding to the exception capture instruction is a jump flag; and / or, In the case of execution exceptions of monitored instructions in the WebAssembly module, the exception flag corresponding to the exception monitoring instruction is determined to be a jump flag, and the exception flag corresponding to the exception capture instruction is determined to be a no-jump flag.
6. The method according to claim 4, characterized in that, The exception handling mechanism instructions include exception monitoring instructions and exception catching instructions. Exception flags include jump flags or non-jump flags. Based on the exception flags corresponding to the exception handling mechanism instructions, the operands pushed onto the operand stack by the exception handling mechanism instructions are determined, including one or more of the following operations: If the exception flag corresponding to the exception monitoring instruction is the non-jump flag, the current PC is pushed onto the top of the exception monitoring stack as the exception monitoring point at the top of the exception monitoring stack, and the operands pushed onto the operand stack are determined to be the non-jump flag. If the exception flag corresponding to the exception monitoring instruction is a jump flag, pop the data pushed onto the operand stack by the monitored instruction according to the operand of the exception monitoring instruction, and determine that the operand pushed onto the operand stack is a jump flag; If the exception flag corresponding to the exception capture instruction is a jump flag, pop the exception monitoring point at the top of the exception monitoring stack and determine that the operand pushed onto the operand stack is a jump flag. If the exception flag corresponding to the exception capture instruction is the non-jump flag, then the operands pushed onto the operand stack are determined to be non-jump flags.
7. The method according to claim 4, characterized in that, The exception handling mechanism instructions include exception monitoring instructions and exception catching instructions. Operands include jump flags or non-jump flags in the exception flags. The execution path of the conditional statement block corresponding to the exception handling mechanism instruction is determined based on the operands pushed onto the operand stack to handle and optimize exceptions during the runtime of the WebAssembly module. This includes one or more of the following operations: If the operand pushed onto the operand stack by the exception monitoring instruction is a non-jump flag, no jump will be performed when executing the conditional statement block corresponding to the exception monitoring instruction, and the monitored instruction in the conditional statement block corresponding to the exception monitoring instruction will be executed. When the operand pushed onto the operand stack by the exception monitoring instruction is a jump flag, a jump is performed when the conditional statement block corresponding to the exception monitoring instruction is executed, and the execution jumps to the exception capture instruction after the monitored instruction. When the operand pushed onto the operand stack by the exception handling instruction is a jump flag, a jump is performed when the conditional statement block corresponding to the exception handling instruction is executed, and the jump is performed to the instruction after the exception handling instruction in the WebAssembly module. If the operand pushed onto the operand stack by the exception capture instruction is a non-jump flag, no jump will occur when executing the conditional statement block corresponding to the exception capture instruction, and the exception handling instructions within the conditional statement block corresponding to the exception capture instruction will be executed.
8. An apparatus for exception handling in a WebAssembly module, comprising a processor and a memory storing program instructions, characterized in that, The processor is configured to, when executing the program instructions, perform the method for exception handling of the WebAssembly module as described in any one of claims 1 to 7.
9. An electronic device, characterized in that, include: The electronic device itself; and, The apparatus for handling exceptions in the WebAssembly module as described in claim 8, installed on the main body of the electronic device.
Citation Information
Patent Citations
C-language monitoring software fault tolerant method based on embedded Linux system
CN106649039A
Abnormality processing method and device, equipment and medium
CN117112073A