Patch method and related devices

By generating a compilation information table during the compilation process of WebAssembly files and replacing function addresses during loading, the problem of needing to recompile the entire source file for function replacement in existing technologies is solved, achieving lightweight function-level updates and fast loading.

CN115167862BActive Publication Date: 2026-03-17HUAWEI TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-03-19
Publication Date
2026-03-17

AI Technical Summary

Technical Problem

In existing technologies, replacing specific functions in a WebAssembly file requires recompiling the entire source file, resulting in large amounts of file data, large file size, long download and loading times, and an inability to achieve lightweight and fast updates.

Method used

During the compilation process, a compilation information table is generated to record the correspondence between the original function and the patch function. The function address is replaced during loading using the function correspondence table, thereby achieving function-level replacement and update.

Benefits of technology

It enables lightweight function-level replacement and updates, shortening download and loading times and avoiding version redundancy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115167862B_ABST
    Figure CN115167862B_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a patching method and related equipment, the method comprising: generating a compilation information table in the compilation process of an original file, the original file being a WebAssembly binary format file, the compilation information table recording a function index of an original function, the original function being a function in the original file; generating a function correspondence table according to the compilation information table in the compilation process of a patch file, the patch file being a WebAssembly binary format file, the function correspondence table recording a correspondence between the original function and a patch function, the patch function being a function in the patch file; and replacing, in the loading process of the original file, a function address of the original function in a function index space with a function address of the patch function according to the function correspondence table, the function index space being generated in the loading process of the original file. With the embodiments of the present application, function-level replacement and update of a WebAssembly application using a patch can be achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a patching method and related equipment. Background Technology

[0002] Currently, there is no patching solution for WebAssembly applications. If a specific function in a WebAssembly file needs to be replaced, the entire source file needs to be recompiled to obtain a new binary .wasm file with the replaced function, which is then loaded and run in the WebAssembly virtual machine as a new version of the application. Summary of the Invention

[0003] This application provides a patching method and related equipment, which can realize the replacement and updating of WebAssembly applications at the function level using patches.

[0004] In a first aspect, embodiments of this application provide a patching method, comprising: during the compilation of the original file, generating a compilation information table, wherein the original file is a WebAssembly binary format file, the compilation information table records function indices of original functions, and the original functions are functions in the original file; during the compilation of the patch file, generating a function mapping table based on the compilation information table, wherein the patch file is a WebAssembly binary format file, the function mapping table records the correspondence between the original functions and patch functions, and the patch functions are functions in the patch file; during the loading of the original file, replacing the function addresses of the original functions in the function index space with the function addresses of the patch functions based on the function mapping table, wherein the function index space is generated during the loading of the original file. In this embodiment, during the compilation of the WebAssembly binary format source file, a compilation information table is generated, which records the function indices of the source functions in the source file. During the compilation of the WebAssembly binary format patch file, a function mapping table is generated based on the function indices of the source functions in the compilation information table and the function indices of the patch functions in the patch file. This function mapping table records the correspondence between source functions and patch functions. Specifically, the function indices of the source functions are obtained from the compilation information table, the function indices of the patch functions are obtained from the patch file, and the function indices of the source functions and patch functions are associated and recorded in the function mapping table. The process involves generating a function mapping table. During the loading of the original file, a function index space is generated, which records the function addresses of the original functions. The function mapping table records the correspondence between the original functions and the patch functions. Therefore, the function addresses of the patch functions can be obtained from the patch file. Then, based on the correspondence between the original functions and patch functions recorded in the function mapping table, the function addresses of the original functions in the function index space are replaced with the function addresses of the patch functions. Since the function addresses of the original functions in the function index space are replaced with the function addresses of the patch functions, the function executed during subsequent function applications is the patch function, thereby achieving function-level replacement and updating of WebAssembly applications using patches. It should be understood that the original function or patch function described in the embodiments of this application does not refer to a single function, but rather to a class of functions; for example, the original function refers to the general term for functions that need to be patched, and the patch function refers to the general term for functions used for patching.

[0005] In one possible implementation, the patch file contains only patch functions. In this embodiment, the patch file contains only the patch functions, meaning that every function in the patch file is a function beneficial to the patch, which helps to reduce the patch file size and shorten the download and loading time.

[0006] In one possible implementation, replacing the function address of the original function in the function index space with the function address of the patch function according to the function mapping table includes: determining whether the patch function exists according to the function mapping table; if the patch function exists, obtaining the function address of the patch function from the code segment of the patch file; and replacing the function address of the original function in the function index space with the function address of the patch function. In this embodiment, during the loading of the original file, the function address of the original function is obtained from the original file and added to the function index space. When patching is required, the function mapping table can be used to determine whether the original function needs to be replaced. When the function mapping table records the correspondence between the original function and the patch function, it indicates that the original function needs to be replaced with the patch function. Thus, the existence of the patch function can be determined based on the function mapping table. If the patch function exists, the function address of the patch function is obtained from the code segment of the patch file, and the function address of the original function in the function index space is replaced with the function address of the patch function. Alternatively, instead of adding the function address of the original function to the function index space, the function address of the patch function is added to the function index space, thereby realizing the replacement of the original function by the patch function.

[0007] In one possible implementation, the compilation information table also records the memory segment size of the original file; the method further includes: during the compilation of the patch file, obtaining the memory segment size of the original file from the compilation information table; and compiling the code segment of the patch file according to the memory segment size of the original file and the data offset of the patch function. In this embodiment, if the patch function involves memory operations, the memory operations of the code segment of the patch file need to be corrected; in this case, the compilation information table generated during the compilation of the original file also records the memory segment size of the original file; during the compilation of the patch file, the memory segment size of the original file is obtained from the compilation information table, and the memory segment size of the original file and the data offset of the patch function are used as new offsets and written into the code segment of the patch file, so that the patch function can perform memory operations normally.

[0008] In one possible implementation, the compiler information table also records the function index of the called function, which is the function called by the original function. The method further includes: during the compilation of the patch file, obtaining the function index of the called function from the compiler information table; and compiling the code segment of the patch file according to the function index of the called function. In this embodiment, if the original function involves calling other functions, then after the original function is replaced by the patch function, the patch function also involves calling the function originally called by the original function, requiring modification of the code segment of the patch file. In this case, the compiler information table also records the function index of the called function, which is the function called by the original function. During the compilation of the patch file, the function index of the called function is obtained from the compiler information table, and the function index of the called function is compiled and written into the code segment of the patch file. Thus, after replacing the original function with the patch function through patching, the patch function can also call the function originally called by the original function.

[0009] In one possible implementation, the called function is a function directly called by the original function, and the code segment of the patch file includes direct call instructions, which include the function index of the called function. In this embodiment, if the called function is a function directly called by the original function, then after the original function is replaced by the patch function, the called function is also a function directly called by the patch function. In this case, the compiler information table also records the function index of the called function. Therefore, by obtaining the function index of the called function from the compiler information table and writing the function index of the called function into the direct call instructions of the code segment of the patch file, the patch function can directly call the called function. Thus, after replacing the original function with the patch function through patching, the patch function can also directly call the function originally directly called by the original function.

[0010] In one possible implementation, the compilation information table further records the function signature index of the called function and the element segment size of the original file; the called function is a function indirectly called by the original function; compiling the code segment of the patch file based on the function index of the called function includes: obtaining the function signature index of the called function from the compilation information table; compiling the element segment of the patch file based on the function index of the called function to obtain the element index of the called function; compiling the code segment of the patch file based on the element segment size of the original file and the element index of the called function, wherein the code segment of the patch file includes indirect call instructions, and the indirect call instructions include the function signature index of the called function. In this embodiment, if the called function is a function indirectly called by the original function, then after the original function is replaced by the patch function, the called function will also be a function indirectly called by the patch function. In this case, the compiler information table also records the function index of the called function, the function signature index of the called function, and the element segment size of the original file. By obtaining the function index of the called function from the compiler information table and writing it into the element segment of the patch file, the element index of the called function can be obtained. By obtaining the element segment size of the original file from the compiler information table and writing the element segment size of the original file and the element index of the called function into the code segment of the patch file, and by obtaining the function signature index of the called function from the compiler information table and writing it into the indirect call instruction in the code segment of the patch file, the indirect call of the called function by the patch function can be realized. Thus, after replacing the original function with the patch function through patching, the patch function can also indirectly call the function that was originally indirectly called by the original function.

[0011] In one possible implementation, the method further includes: during the loading of the original file, obtaining the element index of the called function from the patch file; and adding the element index of the called function to a table index space, wherein the table index space is generated during the loading of the original file. In this embodiment, for indirect calls, since the specific function to be called is not known at compile time but only at runtime, and the called function is not directly located through its function index in the function index space, but indirectly located through the table index space (generated by the original file loading), it is necessary to obtain the element index of the called function from the patch file and add it to the table index space during the loading of the original file. Thus, at runtime, the patch function can obtain the element index of the called function from the table index space, thereby locating the called function and achieving indirect calling of the called function.

[0012] In one possible implementation, the called function is a virtual machine imported function; before obtaining the function index of the called function from the compiler information table, the method further includes: generating the function index of the called function; and adding the function index of the called function to the compiler information table. In this embodiment, since the called function is a virtual machine imported function, the compiler information table generated during the compilation of the original file does not record the function index of the called function. Therefore, it is necessary to generate the function index of the called function and add it to the compiler information table, which facilitates the patch function calling the called function.

[0013] In one possible implementation, the method further includes: during the loading of the original file, adding the called function to the function index space through the import section of the original file. In this embodiment, the called function is a virtual machine import function, so the function index space generated during the loading of the original file does not contain the called function. Therefore, it is necessary to add the called function to the function index space through the import section of the original file so that the called function exists in the function index space; thereby facilitating the patch function to call the called function.

[0014] Secondly, embodiments of this application provide a patching device, the beneficial effects of which are described in the first aspect and will not be repeated here. The device includes a processing unit, which is configured to: generate a compilation information table during the compilation of the original file, wherein the original file is a WebAssembly binary format file, the compilation information table records the function indexes of the original functions, and the original functions are functions in the original file; generate a function mapping table based on the compilation information table during the compilation of the patch file, wherein the patch file is a WebAssembly binary format file, the function mapping table records the correspondence between the original functions and the patch functions, and the patch functions are functions in the patch file; and replace the function addresses of the original functions in the function index space with the function addresses of the patch functions based on the function mapping table during the loading of the original file. The function index space is generated during the loading of the original file.

[0015] In one possible implementation, the patch file contains only patch functions.

[0016] In one possible implementation, the processing unit is specifically used to: determine whether the patch function exists according to the function correspondence table; if the patch function exists, obtain the function address of the patch function from the code segment of the patch file; and replace the function address of the original function in the function index space with the function address of the patch function.

[0017] In one possible implementation, the compilation information table also records the memory segment size of the original file; the processing unit is further configured to: obtain the memory segment size of the original file from the compilation information table during the compilation of the patch file; and compile the code segment of the patch file according to the memory segment size of the original file and the data offset of the patch function.

[0018] In one possible implementation, the compilation information table also records the function index of the called function, which is the function called by the original function; the processing unit is further configured to: obtain the function index of the called function from the compilation information table during the compilation of the patch file; and compile the code segment of the patch file according to the function index of the called function.

[0019] In one possible implementation, the called function is a function directly called by the original function, and the code segment of the patch file includes direct call instructions, which include the function index of the called function.

[0020] In one possible implementation, the compilation information table further records the function signature index of the called function and the element segment size of the original file; the called function is a function indirectly called by the original function; the processing unit is specifically used to: obtain the function signature index of the called function from the compilation information table; compile the element segment of the patch file according to the function index of the called function to obtain the element index of the called function; compile the code segment of the patch file according to the element segment size of the original file and the element index of the called function, wherein the code segment of the patch file includes indirect call instructions, and the indirect call instructions include the function signature index of the called function.

[0021] In one possible implementation, the processing unit is further configured to: obtain the element index of the called function from the patch file during the loading of the original file; and add the element index of the called function to the table index space, wherein the table index space is generated during the loading of the original file.

[0022] In one possible implementation, the called function is a virtual machine imported function; the processing unit is further configured to: generate the function index of the called function before obtaining the function index of the called function from the compiler information table; and add the function index of the called function to the compiler information table.

[0023] In one possible implementation, the processing unit is further configured to: add the called function to the function index space through the import section of the original file during the loading process of the original file.

[0024] Thirdly, embodiments of this application provide a patching device, including a processor, a memory, a communication interface, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the processor, and the programs include instructions for performing the steps of the method as described in any one of the first aspects above.

[0025] Fourthly, embodiments of this application provide a chip, including: a processor, configured to call and run a computer program from a memory, causing a device on which the chip is mounted to perform the method as described in any one of the first aspects above.

[0026] Fifthly, embodiments of this application provide a computer-readable storage medium storing a computer program for electronic data interchange, wherein the computer program causes a computer to perform the method as described in any one of the first aspects above.

[0027] In a sixth aspect, embodiments of this application provide a computer program that causes a computer to perform the method as described in any one of the first aspects above. Attached Figure Description

[0028] Figure 1 This is a schematic diagram of the main segment structure and the structure after loading and parsing of the WebAssembly file provided in the embodiments of this application.

[0029] Figure 2 This is a schematic diagram of the WebAssembly function call method provided in the embodiments of this application.

[0030] Figure 3 This is a flowchart illustrating a patching method provided in an embodiment of this application.

[0031] Figure 4 This is a schematic diagram of the loading process of each segment of WebAssembly provided in the embodiments of this application.

[0032] Figure 5 This is a schematic diagram of the original loading process of the function segment of WebAssembly provided in the embodiments of this application.

[0033] Figure 6 This is a schematic diagram of the modified loading process of WebAssembly function segments provided in the embodiments of this application.

[0034] Figure 7This is a schematic diagram of the original compilation process of the WebAssembly code segment when the existing function is directly called, as provided in the embodiments of this application.

[0035] Figure 8 This is a schematic diagram of the modified compilation process of the WebAssembly code segment when the existing function is directly called, as provided in the embodiments of this application.

[0036] Figure 9 This is a schematic diagram of the original compilation process of the WebAssembly code segment when there is an indirect call to an existing function, as provided in the embodiments of this application.

[0037] Figure 10 This is a schematic diagram of the modified compilation process of the WebAssembly code segment when indirectly calling an existing function, as provided in the embodiments of this application.

[0038] Figure 11 This is a schematic diagram of a WebAssembly compilation process provided in an embodiment of this application.

[0039] Figure 12 This is a schematic diagram of a WebAssembly patch creation process provided in an embodiment of this application.

[0040] Figure 13 This is a schematic diagram of a function index space correction process provided in an embodiment of this application.

[0041] Figure 14 This is a schematic diagram of another WebAssembly patch creation process provided in an embodiment of this application.

[0042] Figure 15 This is a schematic diagram of another WebAssembly compilation process provided in an embodiment of this application.

[0043] Figure 16 This is a schematic diagram of another WebAssembly patch creation process provided in the embodiments of this application.

[0044] Figure 17 This is a schematic diagram of a table index space correction process provided in an embodiment of this application.

[0045] Figure 18 This is a schematic diagram of a patching device provided in an embodiment of this application.

[0046] Figure 19 This is a schematic diagram of the structure of a patching device provided in an embodiment of this application. Detailed Implementation

[0047] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present application.

[0048] The terms "comprising" and "having," and any variations thereof, in the specification, claims, and accompanying drawings of this application are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the steps or units listed, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to such process, method, product, or apparatus.

[0049] In this specification, the term "embodiment" means that a specific feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.

[0050] First, the abbreviations and terms that may be involved in this application are defined to facilitate understanding by those skilled in the art, as shown in Table 1.

[0051] Table 1. Definitions of Abbreviations and Terms

[0052] English abbreviations Complete English expression / standard English terminology Chinese expressions / Chinese terminology wasm WebAssembly A new binary format i32 i32 WebAssembly data type: 32-bit integer call call WebAssembly directive: direct call call_indirect call_indirect WebAssembly directive: Indirect call i32.store i32.store WebAssembly command: Store in linear memory

[0053] Secondly, the relevant technical knowledge involved in the embodiments of this application will be introduced here. The description is as follows:

[0054] WebAssembly is a low-level code execution specification designed and promoted in recent years by major browser vendors such as Google, Apple, and Microsoft. It is designed as a portable, secure, low-size, and efficient binary format. WebAssembly allows you to write C / C++ code, compile it to generate binary .wasm files, and run them in a virtual machine that conforms to the WebAssembly specification. WebAssembly's sandboxing feature ensures that when a .wasm file is loaded into the runtime engine, it actually runs in its own private sandbox, where programs cannot access address spaces outside the sandbox. WebAssembly's opcode design is concise, achieving both fast compilation and high execution speeds. WebAssembly's secure execution of third-party code, ultra-lightweight nature, and high performance make it highly valuable for mobile devices, IoT devices, smart home appliances, and trusted runtime environments.

[0055] When embedded device products need to provide a browser-like interface for loading third-party modules, using WebAssembly as the medium is a very attractive solution. Developers can distribute their algorithms as WebAssembly applications. Embedded devices, by integrating a WebAssembly virtual machine, can load and use these applications across different architectures, platforms, and environments.

[0056] 1) WebAssembly binary format

[0057] The top-level structure of WebAssembly is modules. Each WebAssembly binary file corresponds to one module. The main body of a module consists of multiple segments, each identified by a unique segment number. The specific structure is as follows: Figure 1 As shown.

[0058] Since a function involves information such as its index, type, and code, it is represented by a function segment, a type segment, and a code segment.

[0059] Type section: Used to store the function signature type, including the function's parameters and return value.

[0060] Code segment: Stores information about local variables and executed code of a function.

[0061] Function segment: Stores the index of each function's signature in the type segment in sequence.

[0062] Import section: Supports importing external functions for module calls. Imported functions consist of a function name and a function signature.

[0063] Table segments and element segments: These can store function references, and function pointers can be implemented through table segments and element segments.

[0064] Memory segment: Used to define linear memory, which is used to store dynamic data during program execution.

[0065] Data segment: Stores static data used for initializing memory.

[0066] 2) WebAssembly virtual machine

[0067] WebAssembly code (i.e., WebAssembly files) runs on a virtual machine that supports the WebAssembly specification. The WebAssembly virtual machine executes WebAssembly files by parsing and loading the WebAssembly binary format.

[0068] The WebAssembly virtual machine generates function-related structures during the parsing and loading of WebAssembly files, such as... Figure 1 As shown, it includes:

[0069] Function index space: This space contains all function information, including function signatures and function addresses. It is resolved by function segments and code segments. The function's starting address identifies the function, and the function signature identifies the function type. The compiler records wasm compilation information in the .wasm file. The function address here is a WebAssembly virtual address, assigned to the function by the compiler at compile time.

[0070] Table index space: Stores function references for indirect function calls, resolved by table segments and element segments.

[0071] Linear memory index space: A contiguous, byte-addressable memory structure used to store dynamic data during program execution, resolved by memory segments and data segments.

[0072] 3) WebAssembly function calls

[0073] WebAssembly offers two ways to call functions, such as Figure 2 As shown, it includes:

[0074] Direct invocation: Using the call instruction, the call instruction first obtains the function index (func_idx), and then uses the function index to obtain the function address to invoke the function; where the function index is specified by the immediate parameter of the instruction.

[0075] Indirect invocation: This is accomplished using the `call_indirect` instruction and table index space. The `call_indirect` instruction first obtains the function signature index (type-idx), which is placed in the immediate value. Then, it obtains the (table) element index on the stack, and then obtains the function index through the element index. After dynamically checking the function signature, if the function signature matches, the function address is obtained through the function index to invoke the function. The wasm specification defines that a module can define at most one table, and the element type must be a function reference (encoded as 0x70). In addition to the element type, the table type also needs to specify a limit on the number of elements, with a lower limit required and an upper limit optional. The table index refers to obtaining the indirect function reference (function's starting address) through the index value in the table segment. The immediate value is the index; the pointer obtained at the table segment index is the indirect function reference (function's starting address).

[0076] Furthermore, to facilitate understanding of the embodiments of this application, the specific technical problems to be solved by this application are further analyzed and proposed. The following description follows:

[0077] Currently, there is no patching solution for WebAssembly applications. If a specific function in a WebAssembly file needs to be replaced, the entire source file must be recompiled to obtain a new binary .wasm file with the replaced function, which is then loaded and run as a new version of the application in the WebAssembly virtual machine. However, regenerating the WebAssembly binary file has the following drawbacks:

[0078] 1) This results in a large amount of application file data and a large file size, requiring a long time to download, replace, and load, making it impossible to achieve lightweight and fast updates.

[0079] 2) In WebAssembly applications that require frequent updates to local code without affecting the application version, if a new version of the .wasm file is generated each time, the application version number will continue to increase, resulting in version redundancy.

[0080] In view of the problems existing in the above analysis, this application proposes a patch compilation and loading method for WebAssembly. It creates patch files specifically for the WebAssembly binary format, enabling function-level replacement and updates of WebAssembly applications using these patches. The innovations of this application include: the creation and loading of patches specifically for the WebAssembly format, mainly in the following two aspects:

[0081] 1) Patch creation

[0082] Compilation phase: Applications developed using C / C++ are compiled into .wasm files in WebAssembly binary format. During the compilation process, compilation information is recorded, including the correspondence between function names and function indices, and a compilation information table is generated and exported, which includes a function index table, etc.

[0083] Patch creation phase: Based on the compilation information table exported by the WebAssembly application, compile the WebAssembly patch file, and simultaneously correct code segments during the patch file compilation process. The "compiling patch file" refers to the process of creating the patch file, while the "correcting code segments" refers to replacing references to original functions with references to the patch functions in the patch file.

[0084] 2) Patch loading

[0085] Loading Phase: During loading, the function addresses in the function index space are replaced, with the addresses of the patch functions being replaced by the original function addresses. The function address refers to the starting address of the function code block, also known as the function code address, the address of the function's code, or the starting address of the function code block. If the patch function involves other function calls, the table index space needs to be corrected.

[0086] This application is used in scenarios such as WebAssembly application upgrades and updates or application defect fixes. Because the patch files are small in size, the download and loading time is short, thus reducing the time required for WebAssembly application upgrades and updates or application defect fixes.

[0087] Please see Figure 3 , Figure 3 This is a flowchart illustrating a patching method provided in an embodiment of this application. The method includes, but is not limited to, the following steps:

[0088] 301. During the compilation of the original file, a compilation information table is generated, wherein the original file is a WebAssembly binary format file, and the compilation information table records the function index of the original function, wherein the original function is a function in the original file.

[0089] 302. During the compilation process of the patch file, a function mapping table is generated based on the compilation information table, wherein the patch file is a WebAssembly binary format file, the function mapping table records the correspondence between the original function and the patch function, and the patch function is the function in the patch file.

[0090] 303. During the loading process of the original file, the function address of the original function in the function index space is replaced with the function address of the patch function according to the function correspondence table, wherein the function index space is generated during the loading process of the original file.

[0091] The compilation information table includes a function index table, which records the function indices of all functions in the original file; and a function mapping table, which records the correspondence between all original functions that need to be replaced and their corresponding patch functions.

[0092] It should be understood that WebAssembly binary source and patch files can be obtained by first writing C / C++ source code and then compiling it into a .wasm file. The compilation process for source files refers to the process of compiling files from other formats into WebAssembly binary source files; the compilation process for patch files refers to the process of compiling files from other formats into WebAssembly binary patch files.

[0093] For example, generating the function mapping table based on the compilation information table includes: obtaining the function index of the original function from the compilation information table; and generating the function mapping table based on the function index of the original function and the function index of the patch function. The function mapping table can record the index of the original function and the function index of the patch function, and these two are recorded together in the function mapping table. For example, during the compilation of the patch file, the function index of the original function is obtained from the compilation information table, the function index of the patch function is obtained from the patch file, and the function index of the original function and the function index of the patch function are recorded together in the function mapping table, thereby generating the function mapping table.

[0094] It should be noted that the original function or patch function described in the embodiments of this application does not refer to a single function, but rather to a class of functions, which may include multiple functions. For example, the original function refers to the collective term for functions that need to be patched. There may be multiple functions in the original file that need to be replaced, and these multiple functions that need to be replaced are collectively referred to as the original functions. The patch function refers to the collective term for functions used for patching. There may be multiple functions in the patch file used to replace the original functions in the original file, and these multiple functions used to replace the original functions in the original file are collectively referred to as the patch functions.

[0095] In this embodiment, during the compilation of the WebAssembly binary format source file, a compilation information table is generated, which records the function indices of the source functions in the source file. During the compilation of the WebAssembly binary format patch file, a function mapping table is generated based on the function indices of the source functions in the compilation information table and the function indices of the patch functions in the patch file. This function mapping table records the correspondence between source functions and patch functions. Specifically, the function indices of the source functions are obtained from the compilation information table, the function indices of the patch functions are obtained from the patch file, and the function indices of the source functions and patch functions are associated and recorded in the function mapping table. The process involves generating a function mapping table. During the loading of the source file, a function index space is created, which records the function addresses of the source functions. The function mapping table records the correspondence between source functions and patch functions. Therefore, the function addresses of patch functions can be obtained from the patch file. Then, based on the correspondence between source and patch functions recorded in the function mapping table, the function addresses of source functions in the function index space are replaced with the function addresses of patch functions. Since the function addresses of source functions are replaced with the function addresses of patch functions in the function index space, the function executed during subsequent function applications is the patch function. This enables function-level replacement and updates of WebAssembly applications using patches.

[0096] In one possible implementation, the patch file contains only patch functions.

[0097] Specifically, the source code of the patch file contains only the functions and modules in the original file that need to be patched. This means that any function in the patch file is a function that is beneficial to the patch, that is, all functions in the patch file are patch functions.

[0098] In this embodiment of the application, the patch file contains only the patch function, that is, any function in the patch file is a function that is beneficial to the patch, which helps to make the patch file smaller and shorten the download and loading time.

[0099] In one possible implementation, replacing the function address of the original function in the function index space with the function address of the patch function according to the function mapping table includes: determining whether the patch function exists according to the function mapping table; if the patch function exists, obtaining the function address of the patch function from the code segment of the patch file; and replacing the function address of the original function in the function index space with the function address of the patch function.

[0100] Specifically, during the loading of the function segment of the original file, the existence of a patch function is determined according to the function mapping table. If a patch function exists, the function address of the patch function is obtained from the code segment of the patch file, and the function address of the original function in the function index space is replaced with the function address of the patch function.

[0101] The loading process of each segment of WebAssembly is as follows: Figure 4 As shown, the loading order of each segment is as follows: type segment, import segment, function segment, table segment, memory segment, global segment, export segment, start segment, element segment, code segment, and data segment.

[0102] The original loading process of WebAssembly function segments is as follows: Figure 5 As shown, it includes the following steps:

[0103] 501. Determine if the number of loaded functions is less than the total number of functions. If the number of loaded functions is not less than the total number of functions, it means that all functions have been loaded, and the process of loading the function segment ends.

[0104] 502. If the number of loaded functions is less than the total number of functions, then obtain the function signature index from the function segment. That is, obtain the function signature index of the functions that have not yet been loaded.

[0105] 503. Obtain the function address from the code segment. Specifically, the function address is obtained from the code segment using the function signature index.

[0106] 504. Add the function to the function index space. That is, add the function signature, function address, etc. to the function index space.

[0107] It should be understood that in the original loading process of WebAssembly's function segments, for each function, information such as the function signature index is obtained from the function segment, the starting address (i.e., the function address) of the corresponding function code block is obtained from the code segment, and after obtaining all the data of the function, the function is added to the function index space.

[0108] The preceding text described the original loading process of WebAssembly function segments. In scenarios requiring patch loading, to maintain function interface consistency, the original function and the patch function have identical function names and signatures. Therefore, during the loading phase, only the function address of the patch function in the function index space needs to be corrected. This application modifies the original loading process of WebAssembly function segments, correcting the function address of the patch function during the function segment loading process, thereby achieving the replacement of the original function by the patch function. The corrected loading process of WebAssembly function segments provided in this application's embodiments is as follows: Figure 6 As shown, it includes the following steps:

[0109] 601. Determine if the number of loaded functions is less than the total number of functions. If the number of loaded functions is not less than the total number of functions, it means that all functions have been loaded, and the process of loading the function segment ends.

[0110] 602. If the number of loaded functions is less than the total number of functions, then obtain the function signature index from the function segment; that is, obtain the function signature index of the functions that have not yet been loaded. It should be understood that a function signature is a collective term for a function's name, parameter list, and return type, and the function signature index includes the function index, etc.

[0111] 603. Look up the function mapping table. This table records the indexes of the original function and the patch function.

[0112] 604. Determine if a patch function exists. That is, determine if a patch function exists based on the correspondence between the original function and the patch function recorded in the function correspondence table. If the function correspondence table records a correspondence between the original function and the patch function, it indicates that a patch function exists; if the function correspondence table does not record a correspondence between the original function and the patch function, it indicates that a patch function does not exist.

[0113] 605. If no patch function exists, obtain the function address from the code segment of the original file.

[0114] 606. If a patch function exists, obtain the function address from the code segment of the patch file.

[0115] 607. Add the function to the function index space.

[0116] It should be noted that the WebAssembly function segment loading process provided in this application embodiment, for each function, obtains information such as the function signature index from the function segment of the original function (original file). Unlike the original loading process of WebAssembly function segments, this application adds the steps of identifying patch functions and correcting patch function addresses. If the function needs to be replaced with a patch function, the starting address of the corresponding function code block is obtained from the patch file code segment; otherwise, the starting address of the corresponding function code block is still obtained from the original file code segment. Finally, after obtaining all the data of the function, the function address replacement is completed, and the function is added to the function index space.

[0117] In this embodiment, during the loading of the original file, the function address of the original function is obtained from the original file and added to the function index space. When patching is required, the function mapping table can be used to determine whether the original function needs to be replaced. When the function mapping table records the correspondence between the original function and the patch function, it indicates that the original function needs to be replaced with the patch function. Thus, the existence of the patch function can be determined based on the function mapping table. If the patch function exists, the function address of the patch function is obtained from the code segment of the patch file, and the function address of the original function in the function index space is replaced with the function address of the patch function. Alternatively, instead of adding the function address of the original function to the function index space, the function address of the patch function is added to the function index space, thereby realizing the replacement of the original function by the patch function.

[0118] In one possible implementation, the compilation information table also records the memory segment size of the original file; the method further includes: during the compilation of the patch file, obtaining the memory segment size of the original file from the compilation information table; and compiling the code segment of the patch file according to the memory segment size of the original file and the data offset of the patch function.

[0119] When the patch function involves memory operations, the memory operations of the code segment in the patch file must be corrected; in this case, the compilation information table generated during the compilation of the original file also records the memory segment size of the original file.

[0120] For example, suppose the original file contains 6 functions: function A (occupying 0-100 memory in the memory segment), function B (occupying 100-300 memory in the memory segment), function C (occupying 300-400 memory in the memory segment), function D (occupying 400-600 memory in the memory segment), function E (occupying 600-800 memory in the memory segment), and function F (occupying 800-1200 memory in the memory segment). Therefore, the memory segment size of the original file is 0-1200. Suppose function D needs to be replaced by function D', and function D''s data offset is 300. The memory occupied by function D' will be arranged in the 1200-1500 range, so the new data offset is 1200 + 300 = 1500. The data offset of 1500 will be written into the code segment of the patch file.

[0121] In this embodiment, if the patch function involves memory operations, the memory operations of the code segment of the patch file need to be corrected. In this case, the compilation information table generated during the compilation of the original file also records the memory segment size of the original file. During the compilation of the patch file, the memory segment size of the original file is obtained from the compilation information table, and the memory segment size of the original file and the data offset of the patch function are used as the new offset and written into the code segment of the patch file, so that the patch function can perform memory operations normally.

[0122] In one possible implementation, the compilation information table also records the function index of the called function, which is the function called by the original function; the method further includes: during the compilation of the patch file, obtaining the function index of the called function from the compilation information table; and compiling the code segment of the patch file according to the function index of the called function.

[0123] It should be noted that when the original function contains function calls, the relevant instruction code segments of the function calls within the patch function need to be corrected during the compilation process to ensure the correctness of the calling relationship between functions. Therefore, the code segment of the patch file needs to be compiled based on the function index of the called function of the original function.

[0124] In this embodiment, if the original function involves calling other functions, then after the original function is replaced by the patch function, the patch function also involves calling functions originally called by the original function, requiring modification of the code segment of the patch file. In this case, the compiler information table also records the function index of the called function, which is the function called by the original function. During the compilation of the patch file, the function index of the called function is obtained from the compiler information table, and the function index of the called function is compiled and written into the code segment of the patch file. Thus, after the original function is replaced by the patch function through patching, the patch function can also call functions originally called by the original function.

[0125] In one possible implementation, the called function is a function directly called by the original function, and the code segment of the patch file includes direct call instructions, which include the function index of the called function.

[0126] When a function is called directly, the called function is located directly through the function index in the function index space, so the function index after the direct call instruction in the code segment needs to be updated.

[0127] The original compilation process of WebAssembly code segments when there are direct function calls is as follows: Figure 7 As shown, it includes the following steps:

[0128] 701. Obtain the function index (fun_id) of the called function at compile time.

[0129] 702. Write the direct call instruction (call fun_id) into the code segment.

[0130] For example, when compiling the code segment of the original file, it is possible to execute... Figure 7 The process is shown below.

[0131] It should be noted that this application embodiment introduces patches, therefore the compilation process of the code segment needs to be modified. For compiling the patch file, the index of the called function is obtained from the compilation information table, and the code segment of the patch file is modified. Figure 8 This is a schematic diagram of the modified compilation process of the WebAssembly code segment when a function is directly called, as provided in the embodiments of this application, including the following steps:

[0132] 801. Determine if the file is a patch file. That is, determine if the currently compiled file is a patch file.

[0133] 802. If the file is not a patch file, the function index (fun_id) of the called function is obtained during compilation. If the file being compiled is not a patch file, that is, if the file being compiled is the original file, the compilation process is the same as the original compilation process of the code segment.

[0134] 803. If the file is a patch file, then obtain the function index (fun_id) of the called function from the compiler information table.

[0135] 804. Write the direct call instruction (call fun_id) into the code segment.

[0136] In this embodiment, if the called function is a function directly called by the original function, then after the original function is replaced by the patch function, the called function will also be a function directly called by the patch function. In this case, the compiler information table also records the function index of the called function. Therefore, by obtaining the function index of the called function from the compiler information table and writing the function index of the called function into the direct call instruction of the code segment of the patch file, the patch function can directly call the called function. Thus, after replacing the original function with the patch function through patching, the patch function can also directly call the function that was originally directly called by the original function.

[0137] In one possible implementation, the compilation information table further records the function signature index of the called function and the element segment size of the original file; the called function is a function indirectly called by the original function; compiling the code segment of the patch file based on the function index of the called function includes: obtaining the function signature index of the called function from the compilation information table; compiling the element segment of the patch file based on the function index of the called function to obtain the element index of the called function; compiling the code segment of the patch file based on the element segment size of the original file and the element index of the called function, wherein the code segment of the patch file includes indirect call instructions, and the indirect call instructions include the function signature index of the called function.

[0138] The compiler information table also includes a function signature index table, which records the function signature index of the called function; the compiler information table also includes the element segment size of the original file.

[0139] In the case of indirect function calls, since the specific function to be called is not known at compile time but only at runtime, the called function is not located directly through the function index in the function index space, but indirectly from the table index space. Therefore, the element items in the element segment need to be updated at compile time.

[0140] The original compilation process of WebAssembly code segments when there are indirect function calls is as follows: Figure 9 As shown, it includes the following steps:

[0141] 901. Obtain the function index (fun_id) of the called function at compile time.

[0142] 902. Obtain the function signature index (type_id) of the called function at compile time.

[0143] 903. Write the function index (fun_id) of the called function into the element segment to obtain the element index (elem_id) of the called function.

[0144] 904. Write the element index (elem_id) of the called function into the code segment.

[0145] 905. Write the indirect call instruction (call_indirent type_id) into the code segment.

[0146] It should be noted that this application embodiment introduces a patch, therefore the compilation process of the code segment needs to be modified. If a patch file is compiled, the function index of the called function is obtained from the function index table, the function signature index of the called function is obtained from the function signature index table, the element segment size is obtained, and the element segment and code segment are modified simultaneously. Figure 10 This is a schematic diagram of the modified compilation process of WebAssembly code segments when indirectly calling existing functions, as provided in the embodiments of this application, including the following steps:

[0147] 1001. Is the file a patch file? That is, determine whether the currently compiled file is a patch file.

[0148] 1002. If the file is not a patch file, the function index (fun_id) of the called function is obtained during compilation. If the file being compiled is not a patch file, that is, if the file being compiled is the original file, the compilation process is the same as the original compilation process of the code segment.

[0149] 1003. If the file is not a patch file, the function signature index (type_id) of the called function is obtained during compilation.

[0150] 1004. If the file is a patch file, then obtain the function index (fun_id) of the called function from the compilation information table.

[0151] 1005. If the file is a patch file, then obtain the function signature index (type_id) of the called function from the compilation information table.

[0152] 1006. Write the function index (fun_id) of the called function into the element segment to obtain the element index (elem_id) of the called function.

[0153] 1007. If the file is not a patch file, the element index (elem_id) of the called function will be written into the code segment.

[0154] 1008. If the file is a patch file, the element index (elem_id) and element segment size (table_size) of the called function will be written into the code segment.

[0155] 1009. Write the indirect call instruction (call_indirent type_id) into the code segment.

[0156] In this embodiment, if the called function is a function indirectly called by the original function, then after the original function is replaced by the patch function, the called function will also be a function indirectly called by the patch function. In this case, the compiler information table also records the function index of the called function, the function signature index of the called function, and the element segment size of the original file. By obtaining the function index of the called function from the compiler information table and writing it into the element segment of the patch file, the element index of the called function can be obtained. By obtaining the element segment size of the original file from the compiler information table and writing the element segment size of the original file and the element index of the called function into the code segment of the patch file, and by obtaining the function signature index of the called function from the compiler information table and writing it into the indirect call instruction in the code segment of the patch file, the indirect call of the called function by the patch function can be realized. Thus, after replacing the original function with the patch function through patching, the patch function can also indirectly call the function that was originally indirectly called by the original function.

[0157] In one possible implementation, the method further includes: during the loading of the original file, obtaining the element index of the called function from the patch file; and adding the element index of the called function to a table index space, wherein the table index space is generated during the loading of the original file.

[0158] In this embodiment, for indirect calls, since the specific function to be called is not known at compile time but only at runtime, and the called function is not directly located through its function index in the function index space but indirectly located through the table index space (generated by the original file), the element index of the called function needs to be obtained from the patch file during the loading process of the original file and added to the table index space. Thus, at runtime, the patch function can obtain the element index of the called function from the table index space, thereby locating the called function and achieving indirect calling.

[0159] In one possible implementation, the called function is a virtual machine imported function; before obtaining the function index of the called function from the compiler information table, the method further includes: generating the function index of the called function; and adding the function index of the called function to the compiler information table.

[0160] If the called function is a virtual machine import, a new function index needs to be generated and added to the function index table in the compilation information table. If the called function is not a virtual machine import, its function index is already in the function index table.

[0161] In this embodiment, the called function is a virtual machine imported function. Therefore, the compilation information table generated during the compilation of the original file does not record the function index of the called function. Thus, it is necessary to generate the function index of the called function and add it to the compilation information table, which is beneficial for the patch function to call the called function.

[0162] In one possible implementation, the method further includes: during the loading process of the original file, adding the called function to the function index space through the import section of the original file.

[0163] In this embodiment, the called function is a virtual machine import function. Therefore, the function index space generated during the loading of the original file does not contain the called function. Thus, the called function needs to be added to the function index space through the import segment of the original file so that the called function exists in the function index space. This facilitates the patch function calling the called function.

[0164] The technical solution provided in this application will be illustrated below with specific examples.

[0165] Example 1

[0166] Example 1 illustrates an application scenario of this application. For upgrading or fixing defects in a WebAssembly application, it's necessary to replace functions in the existing WebAssembly application file compute.wasm. Taking the compute function as an example, the function implementation is replaced from ab to a+b. The specific technical solution for Example 1 is as follows:

[0167] Step 1: Compile

[0168] The compilation process is as follows Figure 11 As shown.

[0169] 1. Using the application source file main.c, compile it into a WebAssembly binary file main.wasm using the compiler toolchain; where main.wasm is the original file. Optionally, the wasm binary format can be transpiled into wat text format to make the WebAssembly binary file main.wasm readable, such as... Figure 11 As shown.

[0170] 1) The type segment contains two types of function signatures. In Figure 11 In the text, these correspond to (type(;0;)(func(param i32i32)(result i32))) and (type(;1;)(func(result i32))) respectively.

[0171] 2) The function segment contains two functions, indexed 0 and 1 respectively. The function names are discarded during the compilation phase. That is, in Figure 11 The functions func(;0;) and func(;1;) are used in the context of func(;0;).

[0172] 2. Generate and export compilation information tables during the compilation process; in Example 1, the correspondence between each function and its index in the source code is recorded, generating and exporting a function index table. Other structures of the compilation information table will be introduced in subsequent examples. The function index table is as follows: Figure 11 As shown, the structure is explained below:

[0173] 1) Function index: The index assigned to each function by the compiler during the compilation process, which is related to the compilation order.

[0174] 2) Function name: The actual function name corresponding to the function index in the WebAssembly file.

[0175] As you can see, the function index table corresponding to main.wasm contains two items: function 0 (compute) and function 1 (main). In the compilation order, index 0 corresponds to func(;0;), which is compute in Example 1; and index 1 corresponds to func(;1;), which is main in Example 1.

[0176] Step 2: Create a patch

[0177] The process of creating a patch is as follows: Figure 12 As shown.

[0178] 1. Based on the function index table exported in step one, find the function index corresponding to the original function in the function index table. For example, in this case, the function `compute` needs to be replaced. By querying the function index table, we can see that the function index corresponding to the `compute` function is 0.

[0179] 2. During the process of compiling the patch function file (patch.c) into the WebAssembly binary format file patch.wasm, there are two ways to record the correspondence between the functions in the original file and the functions in the patch file; where patch.wasm is the patch file.

[0180] 1) Keeping the function segments in the original file and the patch file consistent will increase the size of the patch file. That is, the functions in the original file refer to `compute` and `main`, and the functions in the patch file are also `compute` and `main`. `compute` is what the patch changes, while `main` remains unchanged. This means that the patch file simultaneously retains both `compute` and `main`, generating a new copy for each; in reality, `main` is redundant.

[0181] 2) A function mapping table is generated using the function indices of the original functions in the function index table and the function indices of the patch functions in the patch file, thus obtaining the correspondence between the old and new functions. In this application, the changed parts of WebAssembly are indexed, and the places that originally referenced the old functions are modified and replaced with the updated parts in the patch. Therefore, there is a changed function mapping table, which contains the correspondence between the function indices of the original functions and the function indices of the patch functions. This implementation method is used in the following implementations.

[0182] Step 3: Loading

[0183] Taking the compute function as an example, such as Figure 13 As shown, during the parsing and loading of the original file main.wasm, the compute function needs to be replaced with the compute function in the patch file. If the function address of the compute function is obtained from the code segment of the original file, the address is 0x08020000; however, if the patch file is loaded, the function address of the compute function is obtained from the code block of the patch file, resulting in address 0x08020200. The address 0x08020000 of the original function compute{ab} is replaced with the address 0x08020200 of the patch function compute{a+b}, thus completing the function address replacement. It should be understood that the starting address of the compute() function can be obtained from either main.wasm or patch.wasm. Here, what is being replaced is the reference to the compute function. The new compute function in patch.wasm and the original compute function in main.wasm are both there; only the call point is changed to achieve the replacement effect.

[0184] This example demonstrates how to update WebAssembly applications using small patch files without affecting already released versions, achieving lightweight and rapid upgrades and bug fixes. To implement these upgrades and bug fixes, patches are created and loaded specifically for the WebAssembly format. This is achieved by recording and exporting a function index table during the compilation phase, obtaining the function indices of the original functions, compiling the patch file with the patch functions, and then replacing the addresses of the original functions with the addresses of the patch functions when the application is loaded. This process enables patch function-level replacement and updates.

[0185] Example 2

[0186] Example 2 presents another application scenario of this application. The difference between Example 2 and Example 1 lies in the fact that the patch function involves memory operations. Taking the `compute` function as an example, the function implementation requires declaring a new array and assigning values ​​to it. The specific technical solution for Example 2 is as follows:

[0187] Step 1: Compile

[0188] The compilation process is based on the compilation process in Example 1 with the addition of steps.

[0189] 1. Use the application source file main.c to compile it into a WebAssembly binary file main.wasm using the compiler toolchain.

[0190] 2. During the compilation process, a compilation information table is generated and exported. The compilation information table includes the following structure:

[0191] 1) Function Index Table: During the compilation process, the correspondence between each function and its index in the application code is recorded, and the function index table is generated and exported.

[0192] 2) Memory segment size: Record the size of the memory segment used during the compilation process and export the memory segment size.

[0193] Step 2: Create a patch

[0194] The patch creation process adds steps to the patch creation process in Example 1, such as... Figure 14 As shown.

[0195] 1. Based on the function index table exported in step one, find the function index corresponding to the original function in the function index table.

[0196] 2. During the process of compiling the patch function file (patch.c) into the WebAssembly binary format file patch.wasm, it is necessary to modify the relevant instruction code segments for memory operations within the patch function to ensure the correctness of memory operations.

[0197] The specific process is as follows: The size of the used memory segment is known from the compilation information table. When compiling the patch file, the offset of the patch file (or patch function) plus the size of the used memory segment is used as the new offset to correct memory-related instructions, which are then written into the code segment of the patch file. It should be understood that because the patch modifies the size of the data memory, the size of the data segment needs to be expanded so that the new patch function can operate on the new data, while the old version can still operate on its own data. Here, the new data offset no longer starts indexing from 0, but from the end of the old data segment (the used memory data segment), so "new offset = offset + size of used memory segment".

[0198] 3. During the process of compiling the patch function file into the WebAssembly binary format file patch.wasm, a function mapping table is generated using the function indexes of the original functions in the function index table and the function indexes of the patch functions in the patch file, thereby obtaining the correspondence between the old and new functions.

[0199] Step 3: Loading

[0200] The loading process adds a step to the loading process in Example 1. It additionally loads data segments from the patch file into linear memory space, with an offset equal to the size of the already used memory segment.

[0201] This example demonstrates how to update WebAssembly applications using small patch files without affecting already released versions, achieving lightweight and rapid upgrades and bug fixes. For scenarios involving linear memory operations within patch functions, correct replacement of the patch function is achieved through processing during the compilation and loading phases. To upgrade or fix WebAssembly applications, patches are created and loaded specifically for the WebAssembly format. During compilation, compilation information, including the function index table and memory segment size, is recorded and exported. The function index and memory segment size of the original function are obtained. Because patch functions involve memory operations, the memory operations in the code segment are corrected, and the patch function is compiled into a WebAssembly patch file. During application loading, the function address of the original function is replaced with the function address of the patch function before loading.

[0202] Example 3

[0203] Example 3 presents a more complex application scenario of this application. The difference between Example 3 and Example 1 lies in the fact that the patch function involves calls to other functions. Taking the function `compute` as an example, the function implementation needs to replace direct calls to the function `add` with direct calls to the function `sub`, and indirect calls to the function `sub` with indirect calls to the function `add`. The specific technical solution for Example 3 is as follows:

[0204] Step 1: Compile

[0205] The compilation process adds steps to the compilation process in Example 1, such as... Figure 15 As shown.

[0206] 1. Using the application source file main.c, compile it into a WebAssembly binary file main.wasm using the compiler toolchain. Optionally, the wasm binary format can be transpiled into wat text format to make the WebAssembly binary file main.wasm readable, such as... Figure 15 As shown:

[0207] The table segment contains one element, a function reference, specifically the function reference corresponding to function index 1, used for function indirect calls. In fact, a reference space is reserved in the table segment, dynamically storing the value of `p`, but defaulting to `sub`. When the statement `p = sub` is executed, `{local.get 0; call_indirect(type0)}`, `wasm` writes the address of `sub` to table segment reference index 1. Immediately afterwards, `int ret = (*p)(a,b)` reads the address of the upcoming indirect call, i.e., `sub`, from reference index 1, and the function calls the `sub` function.

[0208] 2. During the compilation process, a compilation information table is generated and exported. The compilation information table includes the following structure:

[0209] 1) Function Index Table: During the compilation process, the correspondence between each function and its index is recorded, and the function index table is generated and exported.

[0210] 2) Function Signature Index Table: During compilation, the correspondence between each function signature and its index is recorded, generating and exporting the function signature index table. The function signature index table structure includes:

[0211] Function signature index: An index assigned by the compiler to each function signature during the compilation process.

[0212] Function signature: The function signature type corresponding to the function signature index in the type section of the WebAssembly file.

[0213] 3) Element segment size: Record the element segment size during compilation and export the element segment size.

[0214] 4) Memory segment size: Record the size of the memory segment used during the compilation process and export the memory segment size.

[0215] Step 2: Create a patch

[0216] The patch creation process is based on the patch creation process in Example 1, with the addition of steps, such as... Figure 16 As shown.

[0217] 1. If the called function is a virtual machine imported function, a new function index is generated and added to the function index table.

[0218] 2. Based on the function index table exported in step one, find the function index corresponding to the original function in the function index table. For example, in this case, the function `compute` needs to be replaced. By querying the function index table, we can find that the function index corresponding to the `compute` function is 2.

[0219] 3. During the compilation of the patch function file (patch.c) into the WebAssembly binary file patch.wasm, it is necessary to modify the relevant instruction code segments of function calls within the patch function to ensure the correctness of the call relationships between functions. The specific process is as follows, depending on the different function calling methods:

[0220] Direct invocation: The called function is located directly through the function index in the function index space, so the function index after the call instruction in the code segment needs to be updated, such as... Figure 7 and Figure 8 As shown. If the patch file is compiled, the function index of the called function is obtained from the function index table, and the code segment is corrected.

[0221] Indirect calling: Since the specific function to be called is not known at compile time, but only at runtime, the called function is not located directly through the function index space, but indirectly through the table index space. Therefore, it is necessary to update the element items in the element segment, such as... Figure 9 and Figure 10 As shown. If compiling a patch file, the index of the called function is obtained from the function index table, the function signature index of the called function is obtained from the function signature index table, the element segment size is obtained, and the element segment and code segment are corrected simultaneously.

[0222] 4. During the process of compiling the patch function file (patch.c) into the WebAssembly binary format file patch.wasm, a function mapping table is generated using the function indexes of the original functions in the function index table and the function indexes of the patch functions in the patch file, thereby obtaining the correspondence between the old and new functions.

[0223] Step 3: Loading

[0224] The loading process adds steps to the loading process in Example 1.

[0225] 1. For the patch function:

[0226] Correct the address of the patch function code in the function index space according to the loading steps in Example 1, that is, replace the address of the original function with the address of the patch function.

[0227] 2. For other functions called within the patch function:

[0228] If the called function is a virtual machine imported function, it is first added from the import segment of the patch function to the function index space. Then, different steps are performed for different function calling methods. The specific process is as follows:

[0229] Direct call: The called function is located directly through the function index in the function index space. The function index after the call instruction in the code segment has been corrected in the early compilation stage, and the function address of the called function has not changed, so the correctness of direct function call can be guaranteed.

[0230] Indirect calling: Since the specific function to be called is not known at compile time, but only at runtime, the called function is not located directly through the function index in the function index space, but indirectly through the table index space. Therefore, during the process of parsing the table index space from the original file, it is necessary to correct the function reference and add the corresponding element items in the table index space, such as... Figure 17 As shown, the index of the new item in the table index space is the sum of the element segment size of the original file and the element index of the patch file.

[0231] This example demonstrates how to update WebAssembly applications using small patch files without affecting already released versions, achieving lightweight and rapid upgrades and bug fixes. For scenarios where patch functions call other functions, correct replacement of the patch function is achieved through processing during the compilation and loading phases. To upgrade or fix WebAssembly applications, patches are created and loaded specifically for the WebAssembly format. During compilation, a compilation information table is recorded and exported, including function indexes, function signature indexes, and element segment sizes. The original function's function index, function signature index, and element segment size are obtained. Because the patch function involves calls to other functions, the code segment is modified based on the exported compilation information table, compiling the patch function into a WebAssembly patch file. During application loading, the original function address is replaced with the patch function address for the patch function. For other functions called within the patch function, different calling methods are handled, thus achieving patch function-level replacement and updates.

[0232] Please see Figure 18 , Figure 18 This is a schematic diagram of a patching device provided in an embodiment of this application. The patching device 1800 may include a processing unit 1801 and a communication unit 1802. The processing unit 1801 is used to perform actions such as... Figures 8 to 12 In any step of the method embodiment shown, and when performing data transmission such as acquisition, the communication unit 1802 may be selectively invoked to complete the corresponding operation. A detailed description follows.

[0233] The processing unit 1801 is configured to: generate a compilation information table during the compilation of the original file, wherein the original file is a WebAssembly binary format file, the compilation information table records the function indexes of the original functions, and the original functions are functions in the original file; generate a function mapping table based on the compilation information table during the compilation of the patch file, wherein the patch file is a WebAssembly binary format file, the function mapping table records the correspondence between the original functions and the patch functions, and the patch functions are functions in the patch file; and replace the function addresses of the original functions in the function index space with the function addresses of the patch functions based on the function mapping table during the loading of the original file.

[0234] In one possible implementation, the patch file contains only patch functions.

[0235] In one possible implementation, the processing unit 1801 is specifically used to: determine whether the patch function exists according to the function correspondence table; if the patch function exists, obtain the function address of the patch function from the code segment of the patch file; and replace the function address of the original function in the function index space with the function address of the patch function.

[0236] In one possible implementation, the compilation information table also records the memory segment size of the original file; the processing unit 1801 is further configured to: obtain the memory segment size of the original file from the compilation information table during the compilation process of the patch file; and compile the code segment of the patch file according to the memory segment size of the original file and the data offset of the patch function.

[0237] In one possible implementation, the compilation information table also records the function index of the called function, which is the function called by the original function; the processing unit 1801 is further configured to: obtain the function index of the called function from the compilation information table during the compilation of the patch file; and compile the code segment of the patch file according to the function index of the called function.

[0238] In one possible implementation, the called function is a function directly called by the original function, and the code segment of the patch file includes direct call instructions, which include the function index of the called function.

[0239] In one possible implementation, the compilation information table further records the function signature index of the called function and the element segment size of the original file; the called function is a function indirectly called by the original function; the processing unit 1801 is specifically used to: obtain the function signature index of the called function from the compilation information table; compile the element segment of the patch file according to the function index of the called function to obtain the element index of the called function; compile the code segment of the patch file according to the element segment size of the original file and the element index of the called function, wherein the code segment of the patch file includes indirect call instructions, and the indirect call instructions include the function signature index of the called function.

[0240] In one possible implementation, the processing unit 1801 is further configured to: obtain the element index of the called function from the patch file during the loading of the original file; and add the element index of the called function to the table index space, wherein the table index space is generated during the loading of the original file.

[0241] In one possible implementation, the called function is a virtual machine imported function; the processing unit 1801 is further configured to: generate the function index of the called function before obtaining the function index of the called function from the compiler information table; and add the function index of the called function to the compiler information table.

[0242] In one possible implementation, the processing unit 1801 is further configured to: add the called function to the function index space through the import section of the original file during the loading process of the original file.

[0243] The patching device 1800 may further include a storage unit 1803 for storing the terminal's program code and data. The processing unit 1801 may be a processor, the communication unit 1802 may be a transceiver, and the storage unit 1803 may be a memory.

[0244] It should be noted that the implementation of each unit can also be referenced accordingly. Figures 3 to 17 The corresponding description of the method embodiments shown.

[0245] Figure 18The described patching device 1800 generates a compilation information table during the compilation process to obtain the WebAssembly binary format source file. This compilation information table records the function indices of the source functions in the source file. During the compilation process to obtain the WebAssembly binary format patch file, a function mapping table is generated based on the function indices of the source functions in the compilation information table and the function indices of the patch functions in the patch file. This function mapping table records the correspondence between source functions and patch functions. That is, the function indexes of the source functions are obtained from the compilation information table, the function indexes of the patch functions are obtained from the patch file, and the function indices of the source functions and patch functions are associated and recorded in the function mapping table. This process generates a function mapping table. During the loading of the original file, a function index space is generated, which records the function addresses of the original functions. The function mapping table records the correspondence between the original functions and the patch functions. Therefore, the function addresses of the patch functions can be obtained from the patch file. Then, based on the correspondence between the original functions and the patch functions recorded in the function mapping table, the function addresses of the original functions in the function index space are replaced with the function addresses of the patch functions. Since the function addresses of the original functions in the function index space are replaced with the function addresses of the patch functions, the functions executed when the functions are applied later are the patch functions. This enables the use of patches to perform function-level replacement and updates to WebAssembly applications.

[0246] Please see Figure 19 , Figure 19 This is a schematic diagram of a patching device provided in an embodiment of this application. The patching device 1910 includes a transceiver 1911, a processor 1912, and a memory 1913. The transceiver 1911, the processor 1912, and the memory 1913 are interconnected via a bus 1914.

[0247] The memory 1913 includes, but is not limited to, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM), or compact disc read-only memory (CD-ROM), which is used for related instructions and data.

[0248] The transceiver 1911 is used to receive and send data.

[0249] Processor 1912 can be one or more central processing units (CPUs). When processor 1912 is a CPU, the CPU can be a single-core CPU or a multi-core CPU.

[0250] The processor 1912 in the patching device 1910 is used to read the program code stored in the memory 1913 and perform the following operations: During the compilation of the original file, a compilation information table is generated, wherein the original file is a WebAssembly binary format file, and the compilation information table records the function indexes of the original functions, wherein the original functions are functions in the original file; During the compilation of the patch file, a function mapping table is generated according to the compilation information table, wherein the patch file is a WebAssembly binary format file, and the function mapping table records the correspondence between the original functions and the patch functions, wherein the patch functions are functions in the patch file; During the loading of the original file, the function addresses of the original functions in the function index space are replaced with the function addresses of the patch functions according to the function mapping table, wherein the function index space is generated during the loading of the original file.

[0251] It should be noted that the implementation of each operation can also be referenced accordingly. Figures 3 to 17 The corresponding description of the method embodiments.

[0252] Figure 19The described patching device 1910 generates a compilation information table during the compilation process to obtain the WebAssembly binary format source file. This compilation information table records the function indices of the source functions in the source file. During the compilation process to obtain the WebAssembly binary format patch file, a function mapping table is generated based on the function indices of the source functions in the compilation information table and the function indices of the patch functions in the patch file. This function mapping table records the correspondence between source functions and patch functions. In other words, the function indexes of the source functions are obtained from the compilation information table, the function indexes of the patch functions are obtained from the patch file, and the function indices of the source functions and patch functions are associated and recorded in the function mapping table. This process generates a function mapping table. During the loading of the original file, a function index space is generated, which records the function addresses of the original functions. The function mapping table records the correspondence between the original functions and the patch functions. Therefore, the function addresses of the patch functions can be obtained from the patch file. Then, based on the correspondence between the original functions and the patch functions recorded in the function mapping table, the function addresses of the original functions in the function index space are replaced with the function addresses of the patch functions. Since the function addresses of the original functions in the function index space are replaced with the function addresses of the patch functions, the functions executed when the functions are applied later are the patch functions. This enables the use of patches to perform function-level replacement and updates to WebAssembly applications.

[0253] This application also provides a chip, which includes at least one processor, a memory, and an interface circuit. The memory, the transceiver, and the at least one processor are interconnected by a circuit. The at least one memory stores a computer program. When the computer program is executed by the processor, the method flow shown in the above method embodiment is implemented.

[0254] This application also provides a computer-readable storage medium storing a computer program, which, when run on an electronic device, implements the method flow shown in the above method embodiments.

[0255] This application also provides a computer program that, when run on an electronic device, enables the method flow shown in the above method embodiments to be implemented.

[0256] It should be understood that the processor mentioned in the embodiments of this application can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor.

[0257] It should also be understood that the memory mentioned in the embodiments of this application can be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDR SDRAM), Enhanced Synchronous DRAM (ESDRAM), Synchlink DRAM (SLDRAM), and Direct Rambus RAM (DR RAM).

[0258] It should be noted that when the processor is a general-purpose processor, DSP, ASIC, FPGA, or other programmable logic device, discrete gate or transistor logic device, or discrete hardware component, the memory (storage module) is integrated into the processor.

[0259] It should be noted that the memories described in this specification are intended to include, but are not limited to, these and any other suitable types of memories.

[0260] It should be understood that in the various embodiments of this application, the order of the above-mentioned processes does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.

[0261] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed in this specification can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0262] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.

[0263] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of the units described above is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0264] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0265] In addition, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.

[0266] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, a server, or a patching device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0267] The steps in the methods of this application can be adjusted, combined, or deleted according to actual needs. Furthermore, the terminology and explanations in the embodiments of this application can be referred to the corresponding descriptions in other embodiments.

[0268] The modules in the device of this application embodiment can be merged, divided, and deleted according to actual needs.

[0269] The above description and embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit it. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application.

Claims

1. A patching method characterized by, The method comprises: generating a compilation information table in a compilation process of an original file, wherein the original file is a WebAssembly binary format file, and the compilation information table records function indexes of original functions in the original file; generating a function correspondence table according to the compilation information table in a compilation process of a patch file, wherein the patch file is a WebAssembly binary format file, the function correspondence table records a correspondence between the original functions and patch functions in the patch file, and the patch functions are functions in the patch file; and the function correspondence table comprises a correspondence between the function indexes of the original functions and the function indexes of the patch functions; replacing function addresses of the original functions in a function index space with function addresses of the patch functions according to the function correspondence table in a loading process of the original file, wherein the function index space is generated in the loading process of the original file.

2. The method of claim 1, wherein, The functions in the patch file are patch functions.

3. The method according to claim 1 or 2, characterized in that, The replacing function addresses of the original functions in the function index space with function addresses of the patch functions according to the function correspondence table comprises: determining whether the patch functions exist according to the function correspondence table; if the patch functions exist, obtaining the function addresses of the patch functions from a code segment of the patch file; and replacing the function addresses of the original functions in the function index space with the function addresses of the patch functions.

4. The method of claim 1, wherein, The compilation information table further records memory segment sizes of the original file; and the method further comprises: obtaining the memory segment sizes of the original file from the compilation information table in the compilation process of the patch file; and compiling the code segment of the patch file according to the memory segment sizes of the original file and data offsets of the patch functions.

5. The method of claim 1, wherein, The compilation information table further records function indexes of called functions, and the called functions are functions called by the original functions; and the method further comprises: obtaining the function indexes of the called functions from the compilation information table in the compilation process of the patch file; and compiling the code segment of the patch file according to the function indexes of the called functions.

6. The method of claim 5, wherein, The called functions are functions directly called by the original functions, and the code segment of the patch file comprises direct call instructions, and the direct call instructions comprise the function indexes of the called functions.

7. The method of claim 5, wherein, The compilation information table further records function signature indexes of the called functions and element segment sizes of the original file; the called functions are functions indirectly called by the original functions; and the compiling the code segment of the patch file according to the function indexes of the called functions comprises: obtaining the function signature indexes of the called functions from the compilation information table; compiling an element segment of the patch file according to the function indexes of the called functions to obtain element indexes of the called functions; and compiling an element segment of the patch file according to the function indexes of the called functions to obtain element indexes of the called functions. compile a code segment of the patch file according to an element segment size of the original file and an element index of the called function, wherein the code segment of the patch file comprises an indirect call instruction, and the indirect call instruction comprises a function signature index of the called function.

8. The method of claim 7, wherein, The method further comprises: obtaining the element index of the called function from the patch file during loading of the original file; adding the element index of the called function into a table index space, wherein the table index space is generated during loading of the original file.

9. The method according to any one of claims 5-8, characterized in that, The called function is a virtual machine import function; before the function index of the called function is obtained from the compilation information table, the method further comprises: generating the function index of the called function; adding the function index of the called function into the compilation information table.

10. The method of claim 9, wherein, The method further comprises: adding the called function into the function index space through an import segment of the original file during loading of the original file.

11. A patch device characterized in that, The apparatus comprises a processing unit configured to: generate a compilation information table during compilation of an original file, wherein the original file is a WebAssembly binary format file, and the compilation information table records function indexes of original functions in the original file; generate a function correspondence table according to the compilation information table during compilation of a patch file, wherein the patch file is a WebAssembly binary format file, the function correspondence table records a correspondence between the original functions and patch functions in the patch file, and the patch functions are functions in the patch file; and the function correspondence table comprises a correspondence between the function indexes of the original functions and function indexes of the patch functions; replace function addresses of the original functions in a function index space with function addresses of the patch functions according to the function correspondence table during loading of the original file, wherein the function index space is generated during loading of the original file.

12. The apparatus of claim 11, wherein, The functions in the patch file are patch functions.

13. The apparatus of claim 11 or 12, wherein, The processing unit is specifically configured to: determine whether the patch functions exist according to the function correspondence table; if the patch functions exist, obtain the function addresses of the patch functions from code segments of the patch file; replace the function addresses of the original functions in the function index space with the function addresses of the patch functions.

14. The apparatus of claim 11, wherein, The compilation information table further records memory segment sizes of the original file; and the processing unit is further configured to: obtain the memory segment sizes of the original file from the compilation information table during compilation of the patch file; compile the code segments of the patch file according to the memory segment sizes of the original file and data offsets of the patch functions.

15. The apparatus of claim 11, wherein, The compilation information table further records function indexes of called functions, and the called functions are functions called by the original functions; and the processing unit is further configured to: obtain the function indexes of the called functions from the compilation information table during compilation of the patch file; compile a code segment of the patch file according to the function index of the called function.

16. The apparatus of claim 15, wherein, The called function is a function directly called by the original function, and the code segment of the patch file includes a direct call instruction including the function index of the called function.

17. The apparatus of claim 15, wherein, The compiling information table further records a function signature index of the called function and an element segment size of the original file; the called function is a function indirectly called by the original function; and the processing unit is specifically configured to: obtain the function signature index of the called function from the compiling information table; compile an element segment of the patch file according to the function index of the called function to obtain an element index of the called function; compile a code segment of the patch file according to the element segment size of the original file and the element index of the called function, wherein the code segment of the patch file includes an indirect call instruction including the function signature index of the called function.

18. The apparatus of claim 17, wherein, The processing unit is further configured to: obtain the element index of the called function from the patch file during loading of the original file; add the element index of the called function to a table index space, wherein the table index space is generated during loading of the original file.

19. The apparatus of any of claims 15-18, wherein, The called function is a virtual machine import function; and the processing unit is further configured to: before the obtaining of the function index of the called function from the compiling information table; generate the function index of the called function; add the function index of the called function to the compiling information table.

20. The apparatus of claim 19, wherein, The processing unit is further configured to: add the called function to the function index space through an import segment of the original file during loading of the original file.

21. A patch device characterized in that, A chip comprising a processor, a memory, a communication interface, and one or more programs stored in the memory and configured to be executed by the processor, the programs including instructions for performing steps in the method of any one of claims 1-10.

22. A chip, characterized by The chip comprises: a processor configured to call and run a computer program from a memory, so that a device installed with the chip performs the method of any one of claims 1-10.

23. A computer-readable storage medium, characterized in that, A computer program product for electronic data exchange, wherein the computer program product causes a computer to perform the method of any one of claims 1-10. A computer program product for electronic data exchange, wherein the computer program product causes a computer to perform the method of any one of claims 1-10.

Citation Information

Patent Citations

  • Method, device and system for patching kernel on line

    CN101799763A

  • Method and device for on-board programming of indirect addressing skip mode based on SRAM (Static Random Access Memory)

    CN105094784A