Binary file execution method and device and electronic equipment
By executing the binary file of the WebAssembly application in the bytecode interpreter and using the native method for parameter checking and passing, the problem of poor running performance on embedded devices is solved and efficient application execution is achieved.
Patent Information
- Application Number
- CN202411644878.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-18
- Publication Date
- 2025-02-28
- Estimated Expiration
- 2044-11-18
Smart Images

Figure CN119536740A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software technology, and in particular to a method, device and electronic device for executing a binary file. Background Art
[0002] WebAssembly (abbreviated as Wasm) is a binary instruction format for a stack-based virtual machine. It is a low-level assembly-like language with a compact binary format that can run at near-native performance and provide a compilation target for languages such as C / C++.
[0003] The original wasm application was run on a computer web page. The existing execution process requires two traversal processes for the passed parameters. Since the computer runs at a relatively high speed, the impact on the running performance when running on a web page can be basically ignored. However, if the wasm application is run on an embedded device with limited resources, there will be obvious differences in running performance, resulting in the wasm application being unable to run on the embedded device according to the existing execution process. Summary of the invention
[0004] The purpose of the present application is to provide a binary file execution method, device and electronic device, which can be applied to embedded devices with limited resources to improve the running performance of applications.
[0005] In a first aspect, the present application provides a method for executing a binary file, which is applied to a bytecode interpreter; the method comprises: reading the bytecode instruction to be executed currently from the binary file; when determining that the bytecode instruction is a method call instruction, reading the first index of the method to be called corresponding to the bytecode instruction, and determining the method instance corresponding to the method to be called based on the first index; if the method to be called is an imported method, obtaining the signature parsing parameters of the method to be called and pushing them into an operand stack; setting a first stack frame structure corresponding to the method to be called, calling a native method and writing the return value of the native method into the stack frame, and restoring the calling function instance; wherein, in the native method, parameter type checking and parameter passing are performed synchronously; if the method to be called is not an imported method, pushing the method parameters of the method to be called into the operand stack; allocating stack space, setting a second stack frame structure corresponding to the method to be called, obtaining the calling method, and executing the method instance corresponding to the method to be called.
[0006] Furthermore, the above-mentioned step of determining the method instance corresponding to the method to be called based on the first index includes: obtaining the second index corresponding to the currently called method; determining whether the second index is consistent with the first index; if so, using the method instance corresponding to the currently called method as the method instance corresponding to the method to be called; if not, obtaining the corresponding instance data in the downloaded file according to the second index, and reconstructing the method instance required by the method to be called based on the instance data.
[0007] Furthermore, the above method also includes: determining whether the method to be called is an imported method, specifically including: obtaining the number of imported methods, and determining whether the first index is less than the number of imported methods; if so, determining that the method to be called is an imported method; if not, determining that the method to be called is not an imported method.
[0008] Furthermore, the above-mentioned step of obtaining the signature parsing parameters of the method to be called and pushing them into the operand stack includes: obtaining the function signature corresponding to the method to be called; performing address conversion calculation on the parameters corresponding to the specified characters according to the character string in the function signature, and placing the calculated values as the parameters to be passed into the specified position in the operand stack.
[0009] Furthermore, the above-mentioned steps of setting the first stack frame structure corresponding to the method to be called, calling the native method and writing the return value of the native method into the stack frame, and restoring the calling function instance include: setting the current stack frame pointer to point to the first stack frame structure corresponding to the method to be called; obtaining the native method corresponding to the method index in the first stack frame structure from the native method table according to the method index in the first stack frame structure, and executing the native method; after the execution of the native method is completed, storing the return value of the native method in a specified position on the operand stack according to the number and position information of the return values in the first stack frame structure; and pointing the current stack frame pointer to the stack frame structure before calling the native method.
[0010] Furthermore, the above-mentioned steps of obtaining the native method corresponding to the method index in the first stack frame structure from the native method table and executing the native method include: obtaining a new method to be called by the method to be called, calculating the stack size required for the new method and allocating stack space, and setting a third stack frame structure for the new method; calling the new method, and when the new method is executed, updating the third stack frame structure based on the return value; copying the return value to the first stack frame structure of the method to be called according to the offset of the return value obtained from the third stack frame structure; and releasing the third stack frame structure.
[0011] Furthermore, the first stack frame structure includes: the frame address of the previous calling method, the index of the current method, the offset of the current method return value in the operand stack, the current method bytecode address, the local variable stack address, and the address of the operand stack.
[0012] In a second aspect, the present application also provides an execution device for a binary file, which is applied to a bytecode interpreter; the device includes: an instruction acquisition module, which is used to read the bytecode instruction to be executed from the binary file; an instance determination module, which is used to determine that the bytecode instruction is a method call instruction, read the first index of the method to be called corresponding to the bytecode instruction, and determine the method instance corresponding to the method to be called based on the first index; a first execution module, which is used to obtain the signature parsing parameters of the method to be called if the method to be called is an imported method, and push them into the operand stack; set the first stack frame structure corresponding to the method to be called, call the native method and write the return value of the native method into the stack frame, and restore the calling function instance; wherein, in the native method, parameter type checking and parameter passing are performed synchronously; a second execution module, which is used to push the method parameters of the method to be called into the operand stack if the method to be called is not an imported method; allocate stack space, set the second stack frame structure corresponding to the method to be called, obtain the calling method, and execute the method instance corresponding to the method to be called.
[0013] In a third aspect, the present application further provides an electronic device, comprising a processor and a memory, wherein the memory stores computer executable instructions that can be executed by the processor, and the processor executes the computer executable instructions to implement the method described in the first aspect above.
[0014] In a fourth aspect, the present application further provides a computer-readable storage medium, which stores computer-executable instructions. When the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to implement the method described in the first aspect above.
[0015] The present application provides a binary file execution method, device and electronic device, the method is applied to a bytecode interpreter; the method includes: reading the current bytecode instruction to be executed from the binary file; when determining that the bytecode instruction is a method call instruction, reading the first index of the method to be called corresponding to the bytecode instruction, and determining the method instance corresponding to the method to be called based on the first index; if the method to be called is an import method, obtaining the signature parsing parameters of the method to be called and pressing them into the operand stack; setting the first stack frame structure corresponding to the method to be called, calling the native method and writing the return value of the native method into the stack frame, and restoring the calling function instance; wherein, in the native method, parameter type checking and parameter passing are performed synchronously; if the method to be called is not an import method, the method parameters of the method to be called are pressed into the operand stack; allocating stack space, setting the second stack frame structure corresponding to the method to be called, obtaining the calling method, and executing the method instance corresponding to the method to be called. In the execution method, in the native method, parameter type checking and parameter passing are performed synchronously, which can reduce the processing process and space consumption, and can be applied to embedded devices with limited resources to improve the running performance and efficiency of the application. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the specific implementation methods of the present application or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0017] Figure 1 A flowchart of a binary file execution method provided in an embodiment of the present application;
[0018] Figure 2 A flowchart of another binary file execution method provided in an embodiment of the present application;
[0019] Figure 3 A structural block diagram of an instruction execution device for a binary file provided in an embodiment of the present application;
[0020] Figure 4 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0021] The technical solution of the present application will be described clearly and completely in conjunction with the embodiments below. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present application.
[0022] In the existing execution process, the processing of parameter transfer needs to be traversed twice. For example, the called method has three parameters and one return value. The positions of the parameters specified in the method call instruction are 0, 1, and 3, and the return value position is 2; where 0, 1, 2, and 3 are all indexes in the operand stack. In the first step, according to the number of parameters, the parameters are traversed, and the specified parameters (values with indexes 0, 1, and 3 in the operand stack) are copied to the local variable stack (lp) of the calling method with indexes 0, 1, and 2; in the second step, according to the method signature, the parameters copied to the local variable stack are traversed, the address type in the parameters is parsed and converted into the actual address, and finally the parameters of the local variable stack are copied to the temporary buf for method calling. This two-time traversal process will directly affect the application running performance for embedded devices.
[0023] Based on this, the embodiments of the present application provide a binary file execution method, device and electronic device, which can be applied to embedded devices with limited resources to improve the running performance of applications. To facilitate understanding of the embodiments, a binary file execution method disclosed in the embodiments of the present application is first introduced in detail.
[0024] Figure 1 A flowchart of a binary file execution method provided in an embodiment of the present application, the method is applied to a bytecode interpreter; the method specifically comprises the following steps:
[0025] Step S102, reading the bytecode instruction to be executed currently from the binary file;
[0026] The bytecode interpreter obtains the instructions to be executed from the card application binary file; the instructions to be executed are bytecode instructions defined by the wasm instruction set, which are used to represent the next instruction to be executed.
[0027] Step S104, when it is determined that the bytecode instruction is a method call instruction, the first index of the method to be called corresponding to the bytecode instruction is read, and based on the first index, the method instance corresponding to the method to be called is determined;
[0028] When it is determined that the bytecode instruction is not a method call instruction, the instruction can be directly executed according to the instruction function corresponding to the bytecode instruction. When it is determined that the bytecode instruction is a method call instruction, the method instance corresponding to the method to be called is determined based on the first index. Specifically, the judgment can be made through the first index and the second index of the currently called method. If the two are consistent, the method instance of the currently called method is used as the method instance corresponding to the method to be called. If they are inconsistent, the corresponding instance data is obtained in the downloaded file according to the second index, and a method instance is recreated for the method to be called based on the instance data.
[0029] The process of recreating a method instance is: obtaining specified information from the downloaded file in flash, filling the specified information into the preset method instance data structure, and obtaining the recreated method instance; the specified information includes: method index, number of parameters, size of stack space used, and return value position.
[0030] It can be seen that the method provided in the embodiment of the present application can effectively reduce the complexity of the instantiation process and the number of times flash reads data by introducing a process for determining whether a method instance needs to be rebuilt based on an index, thereby improving the execution efficiency of the application.
[0031] Step S106, if the method to be called is an imported method, obtain the signature parsing parameters of the method to be called and push them into the operand stack; set the first stack frame structure corresponding to the method to be called, call the native method and write the return value of the native method into the stack frame, and restore the calling function instance; wherein, in the native method, parameter type checking and parameter passing are performed synchronously;
[0032] Before this step, the following may also be included: a stack space determination process and an import method determination process, such as determining whether the stack space satisfies the space required by the method to be called to execute the method instance; if so, further determining whether the method to be called is an import method;
[0033] Whether the stack space is sufficient can be determined in the following way: compare the stack space size used by the running instance with the remaining stack space size. If the stack space size used by the running instance is less than the remaining stack space size, it can be determined that the stack space is sufficient; otherwise, it is not sufficient.
[0034] When it is determined that the stack space does not meet the space required by the method to be called to execute the method instance, a stack overflow exception will be thrown and the current process will be exited; when the stack space meets the space required by the method to be called to execute the method instance, it can be further determined whether the method to be called is an imported method.
[0035] Imported methods refer to methods that are not defined in the current application module. They can be understood as methods other than those defined by the application itself. The number of imported methods is included in the download file. If the method index is greater than or equal to the value of the number of imported methods, it is an application-defined method, otherwise it is an imported method.
[0036] After determining that the method to be called is an imported method, obtain the signature parsing parameters of the method to be called and push them into the operand stack; set the first stack frame structure corresponding to the method to be called, call the native method and write the return value of the native method into the stack frame, and restore the calling function instance.
[0037] The first stack frame structure includes: the frame address of the previous calling method, the index of the current method, the offset of the return value of the current method in the operand stack, the bytecode address of the current method, the address of the local variable stack, and the address of the operand stack. The setting of the stack frame structure can reduce the amount of stack space occupied and reduce the consumption of stack space at runtime.
[0038] In the native method, parameter type checking and parameter passing are performed synchronously, which can avoid the two traversal processes in the prior art and convert the two traversal processes into one traversal process, thereby improving the application operation efficiency.
[0039] Optionally, in this embodiment, step 102 and step 104 are designed for the import method. For the export method, the card operating system needs to call the corresponding application method to obtain the index of the export method and create a method instance for the export method, and then the bytecode interpreter executes the export method.
[0040] Specifically, step 102 and step 104 are replaced accordingly as follows: the card operating system calls a preset application method to obtain a first index of the method to be called from a preset application method registration list, and creates a method instance corresponding to the method to be called based on the first index.
[0041] Step S108, if the method to be called is not an imported method, the method parameters of the method to be called are pushed into the operand stack; stack space is allocated, a second stack frame structure corresponding to the method to be called is set, and the calling method is obtained to execute the method instance corresponding to the method to be called.
[0042] In the instruction execution method of the binary file provided in the embodiment of the present application, since in the native method, the parameter type check and parameter passing are performed simultaneously, the processing process and space consumption can be reduced, and it can be applied to embedded devices with limited resources to improve the operating performance and efficiency of the application.
[0043] The embodiment of the present application also provides another method for executing a binary file, which is implemented on the basis of the above embodiment; this embodiment focuses on describing the instance determination process, the import method judgment process, and the execution process of the import method and the non-import method.
[0044] See also Figure 2 As shown, the instruction execution method of the binary file of this embodiment is applied to the bytecode interpreter, and specifically includes the following steps:
[0045] S1: Get the instructions to be executed from the card application binary file; the instructions to be executed refer to the next instruction to be executed, which is the instruction defined by the wasm instruction set.
[0046] S2: Determine whether the instruction to be executed is a method call instruction. If so, execute S3; if not, execute S4;
[0047] S3: Read the index of the instruction to be executed, and determine whether the method to be called is the same as the currently called method according to the index of the executed instruction. If so, execute S5; if not, execute S6;
[0048] S4: parse the instructions to be executed according to the corresponding instruction functions and execute them, and then execute S1 after completion;
[0049] S5: Use the instance of the currently called method as the instance of the method to be called, and execute S7;
[0050] S6: Reconstruct the instance of the method to be called and execute S7;
[0051] A method instance refers to a data structure that stores the information that a method depends on when it is running. When a method is called, this data structure needs to be updated based on the method information in the downloaded file. This data structure is placed in RAM to improve the access speed during execution, so this data structure needs to be re-instantiated when a method is called. For example, the data structure of a method instance consists of the method index, number of parameters, size of the stack space used, and the position of the return value. Constructing a method instance is to find this information from the downloaded file based on the information carried in the method call instruction and fill it into the method instance data structure. This information will be used in subsequent parameter passing, stack space application, and return value setting.
[0052] S7: Check whether the stack space meets the space required by the method to be called; the information carried by the method call instruction can be used to obtain the stack space required for the called method to run, and compare it with the remaining available space on the stack. If it meets the requirements, execute S8; if not, execute S9;
[0053] S8: Determine whether the method to be called is an imported method; if so, execute S10; if not, execute S11;
[0054] Imported methods refer to methods that are not defined in the current application module. They can be understood as methods other than those defined by the application itself. The download file contains the number of imported methods. If the method index is greater than or equal to the value of the number of imported methods, it is an application-defined method, otherwise it is an imported method.
[0055] Determining whether the method to be called is an imported method includes: obtaining the number of imported methods and determining whether the first index is less than the number of imported methods; if so, determining that the method to be called is an imported method; if not, determining that the method to be called is not an imported method.
[0056] For example, the downloaded file in the flash (the executable file generated after the application is compiled by the compiler) contains 3 imported functions. When the method is called, the method index obtained from the method call instruction is 2, which is less than the number of imported functions 3. Therefore, it can be determined that the method with the method index 2 to be called is an imported method, and the calling process of the imported method is subsequently executed.
[0057] S9: throw a stack overflow exception, exit the current process, and execute S1;
[0058] S10: Obtain the signature parsing parameters of the method to be called, push them into the stack, and execute S12;
[0059] Get the function signature corresponding to the method to be called; according to the string in the function signature, perform address conversion calculation on the parameter corresponding to the specified character, and put the calculated value as the parameter to be passed into the specified position in the operand stack.
[0060] The function signature in the Wasm application indicates the type of the transmitted parameter, which is represented by a string of characters. For example, i indicates that the parameter is an integer value type, and * indicates that the parameter is an address type. According to the string in the function signature, the parameter corresponding to the address type is converted and calculated, and the calculated value is placed in the specified position in the operand stack as the parameter to be passed.
[0061] S11: transfer the method parameters of the method to be called to the stack and execute S13;
[0062] S12: Record the number and position information of the return values of the method to be called; set the first stack frame structure, call the native method of the cos layer, and write the return value of the native method into the stack frame according to the number and position information of the return values, restore the instance of the method to be called, complete the call of the method to be called, and execute S1;
[0063] The number and location information of the method return value are stored in the stack frame Ret_offset field. After the method execution ends and before returning to the calling function, the return value needs to be placed in the specified position of the operand stack.
[0064] The process of writing the return value of the native method into the stack frame, restoring the instance of the method to be called, and completing the call of the method to be called is as follows:
[0065] (1) Setting the current stack frame pointer to point to the first stack frame structure corresponding to the method to be called; the first stack frame structure includes: the frame address of the previous calling method, the index of the current method, the offset of the current method return value in the operand stack, the current method bytecode address, the local variable stack address, and the address of the operand stack.
[0066] (2) According to the method index in the first stack frame structure, the native method in the native method table is called to execute the content of the native method;
[0067] Taking the example in the above-mentioned prior art as an example, according to the number of parameters (3), the parameters are traversed, and the parameters are resolved according to the method signature. The parameters with operand stack indexes 0 and 1 are copied to positions 0 and 1 of the local variable stack (lp) of the calling method, and the parameter with operand stack index 3 is copied to the position with local variable stack index 2 after address resolution.
[0068] In specific implementation, the new method to be called by the method to be called can be obtained, stack space can be allocated according to the calculated stack size of the new method, and a third stack frame structure can be set for the new method; the new method can be called, and the third stack frame structure can be updated based on the return value after the new method is executed; according to the offset of the return value obtained from the third stack frame structure, the return value can be copied to the first stack frame structure of the method to be called; and the third stack frame structure can be released.
[0069] (3) After the execution of the native method is completed, the method return value is stored in the specified position of the operand stack according to the number and position information of the return values in the first stack frame structure;
[0070] (4) Set the current stack frame pointer to the stack frame structure before calling the native method.
[0071] S13: Record the number and position information of the return values of the method to be called; allocate stack space, set the second stack frame structure, complete the call of the method to be called, and execute S1.
[0072] The second stack frame structure here may be the same as the aforementioned first stack frame structure.
[0073] The binary file execution method provided in the embodiment of the present application has the following advantages:
[0074] (1) By optimizing the method instance data structure, the data structure is made more compact and the method execution process is simplified.
[0075] (2) Increase the consistency judgment between the calling method and the current method. If it is the same method, there is no need to re-instantiate the method, thereby improving the execution efficiency during recursive calls; by optimizing the instantiation process during method calls, reducing the number of judgments during instantiation and the number of flash data reads, thereby improving execution efficiency.
[0076] (3) Optimize the parameter preparation process when calling native methods, combine parameter type checking with parameter passing, reduce the number of loop operations to traverse parameters, and thus improve operation efficiency. Before the improvement, when calling native methods to pass parameters, the parameters were processed through two loop operations. The first time, the parameters were transferred to a new buffer and environment variables were added before the parameters. The second time, the address type parameters were further processed according to the function signature. After the improvement, these two processes were merged into one loop, and the environment variables were passed to the underlying function as separate parameters, thereby improving execution efficiency. At the same time, the use of local variables in the processing process was reduced, reducing native stack consumption.
[0077] (4) Optimization of the loaded file data structure. In this application, constant values are placed in flash, which does not need to occupy stack space and reduces the use of runtime stack space.
[0078] (5) Analyze the processing logic and optimize the processing flow to improve execution efficiency.
[0079] (6) Optimize the data composition of the frame structure and reduce the size of the space occupied by the frame. For example, the stack frame data structure occupies 16 bytes before optimization and 8 bytes after optimization. The optimization is performed by deleting data that is not used during operation and compressing the bytes occupied by useful data.
[0080] Based on the above method embodiment, the present application embodiment also provides a binary file execution device, which is applied to a bytecode interpreter; see Figure 3As shown, the device includes: an instruction acquisition module 302, which is used to read the bytecode instruction to be executed from the binary file; an instance determination module 304, which is used to determine that the bytecode instruction is a method call instruction, read the first index of the method to be called corresponding to the bytecode instruction, and determine the method instance corresponding to the method to be called based on the first index; a first execution module 306, which is used to obtain the signature parsing parameters of the method to be called if the method to be called is an imported method, and push them into the operand stack; set the first stack frame structure corresponding to the method to be called, call the native method and write the return value of the native method into the stack frame, and restore the calling function instance; wherein, in the native method, parameter type checking and parameter passing are performed synchronously; a second execution module 308, which is used to push the method parameters of the method to be called into the operand stack if the method to be called is not an imported method; allocate stack space, set the second stack frame structure corresponding to the method to be called, obtain the calling method, and execute the method instance corresponding to the method to be called.
[0081] Furthermore, the above-mentioned instance determination module 304 is used to: obtain a second index corresponding to the currently called method; determine whether the second index is consistent with the first index; if so, use the method instance corresponding to the currently called method as the method instance corresponding to the method to be called; if not, obtain the corresponding instance data in the downloaded file according to the second index, and reconstruct the method instance required by the method to be called based on the instance data.
[0082] Furthermore, the above-mentioned device also includes: a judgment module, used to judge whether the method to be called is an imported method, including: obtaining the number of imported methods, and judging whether the first index is less than the number of imported methods; if so, determining that the method to be called is an imported method; if not, determining that the method to be called is not an imported method.
[0083] Furthermore, the first execution module 306 is used to obtain a function signature corresponding to the method to be called; perform address conversion calculation on the parameter corresponding to the specified character according to the character string in the function signature, and put the calculated value as the parameter to be passed into the specified position in the operand stack.
[0084] Furthermore, the first execution module 306 is used to set the current stack frame pointer to point to the first stack frame structure corresponding to the method to be called; obtain the native method corresponding to the method index in the first stack frame structure from the native method table according to the method index in the first stack frame structure, and execute the native method; after the execution of the native method is completed, store the return value of the native method to the specified position of the operand stack according to the number and position information of the return values in the first stack frame structure; and point the current stack frame pointer to the stack frame structure before calling the native method.
[0085] Furthermore, the first execution module 306 is used to obtain a new method to be called by the method to be called, calculate the stack size used by the new method and allocate stack space, and set a third stack frame structure for the new method; call the new method, and when the new method is executed, update the third stack frame structure based on the return value; according to the offset of the return value obtained from the third stack frame structure, copy the return value to the first stack frame structure of the method to be called; and release the third stack frame structure.
[0086] Furthermore, the first stack frame structure includes: the frame address of the previous calling method, the index of the current method, the offset of the current method return value in the operand stack, the current method bytecode address, the local variable stack address, and the address of the operand stack.
[0087] The device provided in the embodiment of the present application has the same implementation principle and technical effects as those of the aforementioned method embodiment. For the sake of brief description, for matters not mentioned in the embodiment of the device, reference may be made to the corresponding contents in the aforementioned method embodiment.
[0088] The present application also provides an electronic device, such as Figure 4 As shown, it is a schematic diagram of the structure of the electronic device, wherein the electronic device includes a processor 41 and a memory 40, the memory 40 stores computer executable instructions that can be executed by the processor 41, and the processor 41 executes the computer executable instructions to implement the above method.
[0089] exist Figure 4 In the illustrated embodiment, the electronic device further includes a bus 42 and a communication interface 43 , wherein the processor 41 , the communication interface 43 and the memory 40 are connected via the bus 42 .
[0090] Among them, the memory 40 may include a high-speed random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 43 (which can be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. can be used. The bus 42 can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, etc. The bus 42 can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 4 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0091] The processor 41 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 41. The above processor 41 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor. The steps of the method disclosed in the embodiment of the present application can be directly embodied as a hardware decoding processor to execute, or the hardware and software modules in the decoding processor can be executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory, and the processor 41 reads the information in the memory and completes the steps of the method of the above embodiment in combination with its hardware.
[0092] An embodiment of the present application also provides a computer-readable storage medium, which stores computer-executable instructions. When the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to implement the above method. The specific implementation can be found in the aforementioned method embodiment, which will not be repeated here.
[0093] The computer program products of the methods, devices, and electronic devices provided in the embodiments of the present application include a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the methods described in the previous method embodiments. The specific implementation can be found in the method embodiments, which will not be repeated here.
[0094] Unless otherwise specifically stated, the relative steps, numerical expressions and values of the components and steps set forth in these embodiments do not limit the scope of the present application.
[0095] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0096] In the description of the present application, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present application and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present application. In addition, the terms "first", "second", and "third" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.
[0097] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present application, which are used to illustrate the technical solutions of the present application, rather than to limit them. The protection scope of the present application is not limited thereto. Although the present application is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed in the present application, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application, and should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.
Claims
1. A method for executing a binary file, characterized in that: The method is applied to a bytecode interpreter; the method comprises: Read the bytecode instructions currently to be executed from the binary file; When it is determined that the bytecode instruction is a method call instruction, a first index of the method to be called corresponding to the bytecode instruction is read, and based on the first index, a method instance corresponding to the method to be called is determined; If the method to be called is an imported method, the signature parsing parameters of the method to be called are obtained and pushed into the operand stack; a first stack frame structure corresponding to the method to be called is set, a native method is called and the return value of the native method is written into the stack frame, and the calling function instance is restored; wherein, in the native method, parameter type checking and parameter passing are performed synchronously; If the method to be called is not an imported method, the method parameters of the method to be called are pushed into the operand stack; stack space is allocated, a second stack frame structure corresponding to the method to be called is set, and a calling method is obtained to execute the method instance corresponding to the method to be called.
2. The method according to claim 1, characterized in that The step of determining, based on the first index, a method instance corresponding to the method to be called comprises: Get the second index corresponding to the currently called method; Determine whether the second index is consistent with the first index; If yes, use the method instance corresponding to the currently called method as the method instance corresponding to the method to be called; If not, the corresponding instance data is obtained from the downloaded file according to the second index, and the method instance required by the method to be called is reconstructed based on the instance data.
3. The method according to claim 1, characterized in that: The method further comprises: determining whether the method to be called is an import method; The determining whether the method to be called is an import method specifically includes: Obtain the number of import methods, and determine whether the first index is less than the number of import methods; If yes, determining that the method to be called is an import method; If not, it is determined that the method to be called is not an imported method.
4. The method according to claim 1, characterized in that: The step of obtaining the signature parsing parameters of the method to be called and pushing them into the operand stack includes: Obtaining the function signature corresponding to the method to be called; According to the character string in the function signature, the parameter corresponding to the specified character is calculated by address conversion, and the calculated value is placed in the specified position in the operand stack as the parameter to be passed.
5. The method according to claim 1, characterized in that The steps of setting a first stack frame structure corresponding to the method to be called, calling the native method and writing the return value of the native method into the stack frame, and resuming the calling function instance include: Set the current stack frame pointer to point to the first stack frame structure corresponding to the method to be called; According to the method index in the first stack frame structure, obtaining a native method corresponding to the method index in the first stack frame structure from a native method table, and executing the native method; After the native method is executed, according to the number and position information of the return values in the first stack frame structure, the return value of the native method is stored in a specified position of the operand stack; The current stack frame pointer points to the stack frame structure before calling the native method.
6. The method according to claim 5, characterized in that The step of obtaining a native method corresponding to the method index in the first stack frame structure from a native method table and executing the native method includes: Obtaining a new method to be called by the method to be called, calculating a stack size required by the new method and allocating stack space, and setting a third stack frame structure for the new method; Calling the new method, and when the new method is executed, updating the third stack frame structure based on the return value; According to the offset of the return value obtained from the third stack frame structure, copying the return value to the first stack frame structure of the method to be called; The third stack frame structure is released.
7. The method according to claim 1, characterized in that The first stack frame structure includes: the frame address of the previous calling method, the index of the current method, the offset of the current method return value in the operand stack, the current method bytecode address, the local variable stack address, and the address of the operand stack.
8. A binary file execution device, characterized in that: The device is applied to a bytecode interpreter; the device comprises: The instruction acquisition module is used to read the bytecode instructions to be executed from the binary file; An instance determination module, configured to, when determining that the bytecode instruction is a method call instruction, read a first index of a method to be called corresponding to the bytecode instruction, and determine a method instance corresponding to the method to be called based on the first index; The first execution module is used for obtaining the signature parsing parameters of the method to be called and pushing them into the operand stack if the method to be called is an imported method; setting the first stack frame structure corresponding to the method to be called, calling the native method and writing the return value of the native method into the stack frame, and restoring the calling function instance; wherein, in the native method, parameter type checking and parameter passing are performed synchronously; The second execution module is used to push the method parameters of the method to be called into the operand stack if the method to be called is not an imported method; allocate stack space, set the second stack frame structure corresponding to the method to be called, obtain the calling method, and execute the method instance corresponding to the method to be called.
9. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores computer executable instructions that can be executed by the processor, and the processor executes the computer executable instructions to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to implement the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
A method of inlining Java program methods based on bytecode
CN109032572A
Byte code verification method and device and storage medium
CN112988157A
WebAssembly code execution method and device
CN117130721A