Methods to enhance WebAssembly memory safety based on the LLVM compilation framework
By extending the WebAssembly instruction set and LLVM compiler backend, and combining it with virtual machine memory safety checks, the problem of insufficient memory safety in WebAssembly was solved, achieving higher memory safety and language applicability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ZHEJIANG UNIV
- Filing Date
- 2023-04-26
- Publication Date
- 2026-04-24
AI Technical Summary
WebAssembly's memory safety is insufficient, which means that security vulnerabilities in non-memory-safe languages may be passed to wasm files, and there is a lack of effective memory safety checking mechanisms.
Extend the WebAssembly instruction set to add an abstract address type.
It improves the memory safety of WebAssembly, enhances the virtual machine's memory access inspection capabilities, reduces the cost of compiler modifications, and expands the language's applicability.
Smart Images

Figure CN116303143B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the WebAssembly instruction set, the LLVM compilation framework, and the WebAssembly virtual machine, and particularly to a method for enhancing WebAssembly memory safety based on the LLVM compilation framework. Background Technology
[0002] WebAssembly is a secure, portable low-level code format designed for efficient execution and compact representation. It was not designed for hand-written code but rather to provide an efficient compilation target for low-level source languages such as C, C++, and Rust. The emergence of WebAssembly solved the need for secure, fast, and portable low-level code on the Web. In recent years, the application scope of WebAssembly has expanded far beyond the web—it can run on the WebAssembly Virtual Machine and is also suitable for various application scenarios such as IoT and smart contracts.
[0003] However, WebAssembly's security does not include memory safety. This lack of memory safety allows security vulnerabilities in non-memory-safe languages such as C / C++ to be passed unrestricted to the wasm files that are the targets of compilation, which may cause many unexpected problems.
[0004] LLVM is a framework system for building compilers, written in C++. It optimizes the compilation time, linking time, runtime, and idle time of programs written in any programming language, remains open to developers, and is compatible with existing scripts. LLVM's modularity greatly reduces the complexity of modifying the compiler.
[0005] A virtual machine is a system simulated by software, possessing complete hardware system functionality and running in a completely isolated environment. The WebAssembly virtual machine can run .wasm files, enabling the interpretation or compilation of the WebAssembly instruction set.
[0006] WebAssembly virtual machine technology has injected vitality into the WebAssembly ecosystem, making WebAssembly not just a language that runs on the Web, but also enabling the WebAssembly ecosystem to have more possibilities and be applied to various scenarios such as IoT and smart contracts.
[0007] Therefore, providing WebAssembly with higher security is more conducive to the development of its ecosystem and can provide more assistance for projects in languages such as C / C++ to be ported to the Web. Summary of the Invention
[0008] The purpose of this invention is to address the shortcomings of the new language WebAssembly in terms of memory safety by providing a method based on the LLVM compilation framework to enhance the memory safety of WebAssembly. This method combines the advantages of the WebAssembly language and the LLVM compiler, thereby improving the memory safety of wasm files during runtime.
[0009] The objective of this invention is achieved through the following technical solution:
[0010] A method for enhancing WebAssembly memory safety based on the LLVM compilation framework, the method specifically includes the following steps:
[0011] (1) Extend the WebAssembly instruction set and add a new abstract address type.<base,size,attr,addr> , respectively represent the starting address, size, and attributes of a memory object or its sub-objects, and addr represents the address of the object to be accessed;
[0012] (2) Based on the extended WebAssembly instruction set, modify the LLVM compiler backend, add DAG node types for abstract address types, and add or modify the relevant conversion passes;
[0013] (3) By using the modified LLVM compiler backend, code that can be compiled into LLVM IR format can be compiled into wasm files with instruction set extensions;
[0014] (4) Modify the WebAssembly virtual machine to adapt it to the extended WebAssembly instruction set, so that it can compile or interpret the wasm file corresponding to the modified instruction set;
[0015] (5) Run the wasm file on the modified WebAssembly virtual machine. When encountering WebAssembly instructions related to memory access, in the abstract address type...<base,size,attr,addr> The system obtains the access address (addr) and related memory safety metadata, and performs a memory safety check. If a memory access error is detected, the execution of the wasm executable is terminated.
[0016] Furthermore, the modification of the LLVM compiler backend based on the extended WebAssembly instruction set includes the following sub-steps:
[0017] (2.1) Determine if the operand is an address in LLVM IR. If it is, convert the pointer type (not a function pointer) to an abstract address type.<base,size,attr,addr> If yes, proceed to the next step; otherwise, proceed directly to the next step.
[0018] (2.2) Convert the IR to the corresponding DAG node;
[0019] (2.3) Add or modify Pass to complete the detailed adjustments of the instruction format;
[0020] (2.4) Modify the format conventions of the global variable linking process to complete the modification of the LLVM compiler backend.
[0021] Furthermore, when running the wasm file on the modified WebAssembly virtual machine, the base field is stored separately when encountering instructions related to reading and writing abstract address types, while the size, attr, and addr fields are stored in a location arranged by the WebAssembly virtual machine.
[0022] Furthermore, in step (2), adding the relevant conversion Pass specifically means adding the global variable read / write method related Pass, and then storing all global variables in the global section, and reading and writing them through the global_get and global_set commands.
[0023] Furthermore, in step (1), in addition to adding a new abstract address type, a narrow instruction is added to protect the memory safety of sub-objects.
[0024] The beneficial effects of this invention are as follows:
[0025] This invention presents a method for enhancing WebAssembly memory safety based on the LLVM compiler framework. It fully combines the advantages of both the LLVM framework and the WebAssembly language. By extending the WebAssembly instruction set, the memory safety of the new instruction set is improved. Simultaneously, the LLVM framework reduces the cost of compiler modifications. In this invention, all memory-related WebAssembly instructions utilize the abstract address type.<base,size,attr,addr> It can be used for additional checks on memory read and write operations, and the added metadata improves the memory safety of WebAssembly. Secondly, modifications to the instruction set give virtual machine implementations more options, and the LLVM compilation framework also increases the breadth of source code languages. Combined with WebAssembly, this provides a highly maintainable WebAssembly ecosystem with enhanced memory safety. Attached Figure Description
[0026] Figure 1 This is a flowchart illustrating a method for enhancing WebAssembly memory safety based on the LLVM compilation framework. Detailed Implementation
[0027] The present invention will be described in detail below with reference to the accompanying drawings and preferred embodiments. The objectives and effects of the present invention will become clearer as a result. The present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
[0028] like Figure 1 As shown, a method for enhancing WebAssembly memory safety based on the LLVM compilation framework is presented. This method extends the WebAssembly instruction set by adding memory safety-related metadata fields (base, size, and attr) to the memory read / write instruction format. A modified LLVM compiler is used to compile a wasm file corresponding to the WebAssembly instruction set. The wasm file runs on an adapted WebAssembly virtual machine. When the virtual machine needs to interpret or compile memory read / write related instructions in WebAssembly, it can obtain runtime-safe metadata to check for memory access errors such as out-of-bounds access, thereby improving the memory safety of WebAssembly. The method specifically includes the following steps:
[0029] (1) Extend the WebAssembly instruction set and add a new abstract address type.<base,size,attr,addr> , respectively represent the starting address, size, and attributes of the memory object or its sub-objects, and addr represents the address of the object to be accessed.
[0030] When the existing WebAssembly instruction set is used as a compilation target for non-memory-safe languages such as C and C++, it lacks certain memory safety mechanisms. At the same time, it is difficult to obtain the data required for memory safety checks at the virtual machine level to adapt to the memory safety mechanisms that the hardware architecture may already have. Improving memory safety by extending the instruction set is essentially about maintaining the metadata of memory safety checks required by the virtual machine at the instruction set level, which also makes it easier for the virtual machine to make better use of the memory safety check mechanisms that the hardware may already have.
[0031] By incorporating the metadata of the WebAssembly address into the instruction format, but without directly inserting runtime checks, we can provide more options for the interpretation or compilation of WebAssembly files. For example, in scenarios that have been confirmed to be memory-safe, no additional checks are required.
[0032] Runtime checks are more likely to uncover problems that static checks struggle to find, and modifying the instruction set is essentially a way to increase runtime checks. Furthermore, the extension of the instruction set does not significantly conflict with the compiler's original static checks and compilation optimizations, while also preserving as much of the WebAssembly instruction set's inherent advantages as possible.
[0033] Extending the WebAssembly instruction set only requires the virtual machine to interpret or compile the new instruction set, without requiring the virtual machine to perform security checks or how to perform security checks, giving the virtual machine more options in implementing security checks.
[0034] In step (1), in addition to adding a new abstract address type, the `narrow` instruction is further added to protect the memory safety of sub-objects. Besides `narrow`, an important instruction related to memory safety, there are several other adaptable instructions such as `alloc`, `null`, and `field` to improve the completeness and compatibility of the modified instruction set.
[0035] (2) Based on the extended WebAssembly instruction set, modify the LLVM compiler backend to add DAG node types for operations related to abstract address types, and add or modify the relevant conversion passes. This step specifically includes:
[0036] (2.1) Determine if the operand is an address in LLVM IR. If it is, convert the pointer type (not a function pointer) to an abstract address type.<base,size,attr,addr> If yes, proceed to the next step; otherwise, proceed directly to the next step.
[0037] (2.2) Convert the IR to the corresponding DAG node;
[0038] (2.3) Add or modify Pass to complete the detailed adjustments of the instruction format;
[0039] (2.4) Modify the format conventions of the global variable linking process to complete the modification of the LLVM compiler backend.
[0040] In step (2), the Pass for adding relevant conversions is specifically to add a Pass related to the read and write methods of global variables, and then store all global variables in the global section, and write the address of the global variables through the global_get and global_set instructions.
[0041] In the original instruction set, only a few special global variables, such as stack_pointer, were read and written using the global_get and global_set instructions. The addresses of other global variables were directly represented as immediate values and appeared in various instructions, which is not compatible with the modified instruction set.
[0042] (3) By using the modified LLVM compiler backend, code that can be compiled into LLVM IR format can be compiled into wasm files with instruction set extensions.
[0043] The LLVM compilation framework offers a wider selection of source code languages. Modifications to the compilation process are primarily made to the LLVM backend. Any language (C, C++, Go, etc.) that can be compiled into LLVM IR can also be compiled into a WebAssembly file with an extended instruction set. The LLVM project itself is highly modular, has low coupling, and low maintenance costs, making it easy to continue modifying and improving the WebAssembly instruction set in the future.
[0044] (4) Modify the WebAssembly virtual machine to adapt it to the extended WebAssembly instruction set, so that it can compile or interpret the wasm file corresponding to the modified instruction set.
[0045] (5) Run the wasm file on the modified WebAssembly virtual machine. When encountering WebAssembly instructions related to memory access, in the abstract address type...<base,size,attr,addr> The system obtains the access address (addr) and metadata about memory safety, and performs a memory safety check. If a memory access error is detected, the execution of the wasm executable is terminated.
[0046] When running the wasm file on the modified WebAssembly virtual machine, when encountering instructions related to reading and writing abstract address types, the base field is stored separately, while the size, attr, and addr fields are stored together by the WebAssembly virtual machine. This method of storing addresses and metadata separately does not change the memory layout during data storage. Furthermore, the modified compiler obtains the same result as the addr field type size when calculating the size of the abstract address type, greatly improving compatibility.
[0047] It will be understood by those skilled in the art that the above descriptions are merely preferred examples of the invention and are not intended to limit the invention. Although the invention has been described in detail with reference to the foregoing examples, those skilled in the art can still modify the technical solutions described in the foregoing examples or make equivalent substitutions for some of the technical features. All modifications and equivalent substitutions made within the spirit and principles of the invention should be included within the scope of protection of the invention.
Claims
1. A method for enhancing WebAssembly memory safety based on the LLVM compilation framework, characterized in that, The method specifically includes the following steps: (1) Extend the WebAssembly instruction set and add a new abstract address type.<base,size,attr,addr> , respectively represent the starting address, size, and attributes of a memory object or its sub-objects, and addr represents the address of the object to be accessed; (2) Based on the extended WebAssembly instruction set, modify the LLVM compiler backend, add DAG node types for abstract address types, and add or modify the relevant conversion passes; (3) By using the modified LLVM compiler backend, code that can be compiled into LLVM IR format can be compiled into wasm files with instruction set extensions; (4) Modify the WebAssembly virtual machine to adapt it to the extended WebAssembly instruction set, so that it can compile or interpret the wasm file corresponding to the modified instruction set; (5) Run the wasm file on the modified WebAssembly virtual machine. When encountering WebAssembly instructions related to memory access, in the abstract address type...<base,size,attr,addr> The system obtains the access address (addr) and related memory safety metadata, and performs a memory safety check. If a memory access error is detected, the execution of the wasm executable is terminated.
2. The method for enhancing WebAssembly memory safety based on the LLVM compilation framework according to claim 1, characterized in that, in, Based on the expanded WebAssembly instruction set, modify the LLVM compiler backend, specifically including the following sub-steps: (2.1) Determine if the operand is an address in LLVM IR. If it is, convert the pointer type (not a function pointer) to an abstract address type.<base,size,attr,addr> If yes, proceed to the next step; otherwise, proceed directly to the next step. (2.2) Convert the IR to the corresponding DAG node; (2.3) Add or modify Pass to complete the detailed adjustments of the instruction format; (2.4) Modify the format conventions of the global variable linking process to complete the modification of the LLVM compiler backend.
3. The method for enhancing WebAssembly memory safety based on the LLVM compilation framework according to claim 1, characterized in that, When running the wasm file on the modified WebAssembly virtual machine, the base field is stored separately when encountering instructions related to reading and writing abstract address types, while the size, attr, and addr fields are stored in locations arranged by the WebAssembly virtual machine.
4. The method for enhancing WebAssembly memory safety based on the LLVM compilation framework according to claim 1, characterized in that, In step (2), adding the relevant conversion Pass specifically means adding the global variable read / write method related Pass, and then storing all global variables in the global section, and reading and writing them through the global_get and global_set commands.
5. The method for enhancing WebAssembly memory safety based on the LLVM compilation framework according to claim 1, characterized in that, In step (1), based on the addition of a new abstract address type, a narrow instruction is further added to protect the memory safety of sub-objects.
Citation Information
Patent Citations
WebAssembly virtual machine distributed system fine-grained migration method and system
CN113791870A
WebAssembly-oriented cross-platform GPU (Graphics Processing Unit) virtualization method
CN115658330A