Method and device for obtaining function metadata

By inserting the <push pc> instruction into the assembly instruction of the target function, the problem of difficulty in obtaining function metadata during program running is solved, achieving fast acquisition of function metadata, reducing search time and accelerating program execution speed.

CN119336332APending Publication Date: 2025-01-21HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310898283.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-07-20
Publication Date
2025-01-21

AI Technical Summary

Technical Problem

During the program running, it is difficult to obtain function metadata, because the stack is a continuous memory space, and you can only know the instruction address pointer, bottom pointer, and stack on the current stack when running to a certain point. Top pointer, it is impossible to know which function or which function is currently running or which stack frame belongs to.

Method used

By inserting the instruction into the assembly instruction of the target function, the target pc value is pushed to the stack frame corresponding to the target function. The pc value is read from the stack frame during the running phase, and the function metadata is obtained through the pc value. The offset value of the instruction address relative to the base address, and then the function metadata is obtained based on the offset value, thereby reducing the time to search for function metadata during runtime and accelerating program execution.

Benefits of technology

It realizes the quick acquisition of function metadata during program running, reduces the time for searching function metadata, accelerates the execution speed of the program, and has a small impact on the running performance of the program.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119336332A_ABST
    Figure CN119336332A_ABST
Patent Text Reader

Abstract

The invention provides a method for acquiring function metadata, which is applied to acquiring the function metadata in the running process of a target program and comprises the following steps of: inserting a push pc instruction into an assembly instruction in a compiling stage, acquiring a stack address of the pc pushed to a stack in a running stage, and reading a pc value based on the stack address of the pc on the stack. Based on the pc value, an instruction address where an offset value of the function metadata is located is obtained, and the offset value of the function metadata indicates the offset distance between the instruction address and the base address of the function metadata of the target function; based on the instruction address where the offset value of the function metadata is located, reading the offset value of the function metadata; obtaining an instruction address of the function metadata based on the offset value and the base address of the function metadata; and obtaining the function metadata based on the instruction address of the function metadata. According to the method for obtaining the function metadata, the function metadata can be rapidly obtained by adding a small number of instructions in the assembly instruction, and the influence on the program running performance is small.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of software technology, and in particular to a method and device for obtaining function metadata. Background Art

[0002] In programming languages, it is common to obtain function metadata. For example, when an exception occurs, the runtime needs to trace back the stack frame to check the exception information of each function in order to correctly catch the exception. The exception information of the function belongs to the function metadata. Also, when garbage collection (GC) occurs, it is necessary to traverse the entire stack to find surviving objects. The usual practice is to access the stack map (StackMap) information of the function to accurately obtain the location of the surviving objects of each function in the stack frame. StackMap information also belongs to function metadata. Function metadata refers to function-related information.

[0003] However, it is difficult to obtain function metadata during program execution, because the stack is a continuous memory space. We can only know the instruction address pointer (program counter, PC), stack bottom pointer, and stack top pointer on the current stack when the program reaches a certain point. We cannot know which function is currently running or which function a stack frame belongs to. Summary of the invention

[0004] The embodiments of the present application provide a method for obtaining function metadata, which has little impact on program running performance and can achieve rapid acquisition of function metadata during program running.

[0005] In a first aspect, the present application provides a method for obtaining function metadata, which is applied to obtaining function metadata during the execution of a target program, including obtaining the stack address of a target address pointer during the execution of the target program, that is, the stack address at which a target pc is pushed to the stack, and the target pc is executed and written into the stack frame corresponding to the target function based on the first assembly instruction of the target function; obtaining the target pc based on the stack address of the target pc; obtaining the instruction address at which an offset value of the function metadata is located based on the target pc, the offset value of the function metadata indicating the offset distance between the instruction address and the base address of the function metadata of the target function; obtaining the offset value of the function metadata based on the instruction address at which the offset value of the function metadata is located; obtaining the instruction address of the function metadata based on the offset value and the base address of the function metadata; obtaining the function metadata based on the instruction address of the function metadata.

[0006] The method for obtaining function metadata provided by the present application adds a small number of instructions when the program is running, for example, inserting<push pc> Instruction, pushes the target pc value to the stack frame corresponding to the target function, which has little impact on the program running performance; reads the pc value from the stack frame during the running stage, and then uses the pc value to obtain the offset value of the instruction address of the function metadata relative to the base address, and then obtains the function metadata based on the offset value, thereby reducing the time of searching for function metadata at runtime and accelerating the program execution speed.

[0007] In one possible implementation, the assembly instructions of the target function also include a second assembly instruction, which is used to indicate that the function metadata offset value is inserted into the address space before the instruction code of the target function, for example, inserting the function metadata offset value 8 bytes before the function header of the target function.

[0008] In other words, in the assembly instructions generated during the compilation phase, the offset value of the function metadata is inserted at the beginning of the target function. In this way, the offset value of the function metadata is placed at the beginning of the target function code instruction. The instruction address of the offset value of the function metadata and the target PC are fixed offsets, and the offset distance is small, which speeds up the acquisition of the function metadata offset value, and thus speeds up the acquisition of the function metadata.

[0009] In another possible implementation, the base address is the instruction address where the offset value of the function metadata is located; in this case, the instruction address of the function metadata can be obtained through the offset value of the function metadata and the instruction address where the offset value of the function metadata is located.

[0010] By inserting the offset value of the function metadata at the function header position in the assembly instructions generated by compilation, the instruction address where the offset value of the function metadata is located is obtained when the function header position is obtained during the running stage, thereby saving the step of obtaining the instruction address where the offset value of the function metadata is located through the function header position again. The instruction address of the function metadata can be calculated directly through the offset value of the function metadata and the instruction address where the offset value of the function metadata is located, further reducing the time for searching for function metadata at run time and speeding up program execution.

[0011] In another example, the base address can also be the instruction address of a certain instruction code in the instruction code of the target function. After obtaining the instruction address where the offset value of the function metadata is located, the base address can be obtained by adding a fixed offset to the instruction address where the offset value of the function metadata is located. For example, the offset value of the function metadata is inserted into the function header position of the target function, and the base address is the instruction address D1, which is n bytes away from the instruction address where the offset value of the function metadata in the function header. Then, after obtaining the instruction address D1 where the offset value of the function metadata is located, the base address is the instruction address D1 + n, where n can be an integer greater than 0.

[0012] In another example, the offset value of the function metadata can also be placed in a unified offset table. Exemplarily, during the compilation stage, assembly instructions for updating the offset value of the function metadata to the offset table are generated. At runtime, these assembly instructions are executed to update the offset value of the function metadata to the offset table.

[0013] In this example, the base address is the instruction address of the offset value. The instruction address of the function metadata is obtained through the offset value of the function metadata and the instruction address of the offset table.

[0014] In another possible implementation, the assembly instructions of the target function further include a fourth assembly instruction, which is used to indicate writing the stack bottom pointer of the previous function into the stack frame; the fourth assembly instruction is executed before the first assembly instruction. For example, the fourth assembly instruction is <push the stack bottom pointer of the previous function>, then in the assembly instructions generated during the compilation stage of the target function, the <push the stack bottom pointer of the previous function> instruction is executed first, and then the <push pc> instruction is executed, so that the stack bottom pointer of the previous function is stored in the stack frame space before the pc.

[0015] This application has no special requirements for the storage positions of the stack bottom pointer of the previous function and the target pc in the stack frame space. For example, the stack bottom pointer of the previous function can be stored adjacent to the target pc, that is, the target pc and the stack bottom pointer of the previous function are stored next to each other in the stack frame space, or the stack bottom pointer of the previous function and the target pc are stored at a certain distance apart in the stack frame space, etc.

[0016] In another possible implementation, a specific implementation of obtaining the stack address of the target PC is: obtaining the stack bottom pointer corresponding to the stack frame; based on the stack bottom pointer, obtaining the stack address of the target pc. That is, first obtain the stack bottom pointer of the stack frame corresponding to the target function, and then obtain the stack address of the target pc in the stack frame space through the stack bottom pointer.

[0017] Optionally, the stack address of the target pc can be obtained by the method of stack bottom pointer + preset fixed offset.

[0018] In another possible implementation, a specific implementation of obtaining the instruction address of the offset value of the function metadata based on the target PC is: based on the target address pointer and the second preset offset value, the instruction address where the offset value of the function metadata is located is obtained, and the second preset offset value indicates the offset between the instruction address indicated by the target address pointer and the instruction address where the offset value of the function metadata is located. That is, the instruction address where the offset value of the function metadata is located is obtained by the method of target PC+fixed offset. This method of obtaining the instruction address of the offset value of the function metadata has a wider range of applications.

[0019] In another possible implementation, the target pc directly points to the instruction address where the offset value of the function metadata is located, and the offset value of the function metadata can be directly obtained through the target pc, that is, the target pc value is the instruction address where the offset value of the function metadata is located.

[0020] In the second aspect, the present application provides a function metadata acquisition device, which is used to acquire function metadata of a target function during the execution of a target program. The device includes an acquisition module, a pointer reading module, a first determination module, an offset value reading module, a second determination module and a function metadata reading module, wherein the acquisition module is used to acquire the stack address of a target address pointer, and the target address pointer is written into a stack frame corresponding to the target function based on the first assembly instruction of the target function; the pointer reading module is used to obtain the target address pointer based on the stack address of the target address pointer; the first determination module is used to obtain the instruction address where the offset value of the function metadata is located based on the target address pointer, and the offset value of the function metadata indicates the offset distance between the instruction address and the base address of the function metadata of the target function; the offset value reading module is used to obtain the offset value of the function metadata based on the instruction address where the offset value of the function metadata is located; the second determination module is used to obtain the instruction address of the function metadata based on the offset value and the base address of the function metadata; the function metadata reading module is used to obtain the function metadata based on the instruction address of the function metadata.

[0021] In a possible implementation, the assembly instruction of the target function further includes a second assembly instruction, where the second assembly instruction is used to instruct to insert the offset value of the function metadata into the address space before the instruction code of the target function.

[0022] In this possible implementation, the base address is the instruction address where the offset value of the function metadata is located; the second determination module is specifically used to obtain the instruction address of the function metadata based on the offset value of the function metadata and the instruction address where the offset value of the function metadata is located.

[0023] In another possible implementation, the assembly instruction of the target function further includes a third assembly instruction, where the third assembly instruction is used to instruct to update the offset value of the function metadata into the offset table.

[0024] In this possible implementation, the base address is the instruction address of the offset table; the second determination module is specifically used to obtain the instruction address of the function metadata based on the function metadata offset value and the instruction address of the offset table.

[0025] In another possible implementation, the assembly instructions of the target function further include a fourth assembly instruction, where the fourth assembly instruction is used to instruct to write the stack bottom pointer of the previous function into the stack frame; the fourth assembly instruction is executed before the first assembly instruction.

[0026] Optionally, the stack bottom pointer of the previous function pushed into the stack and the target pc are stored adjacently or separated by a preset distance in the stack frame.

[0027] In another possible implementation, the acquisition module is specifically used to: acquire a stack bottom pointer corresponding to the stack frame; and obtain a stack address of the target address pointer based on the stack bottom pointer.

[0028] In another possible implementation, obtaining the stack address of the target address pointer based on the stack bottom pointer includes: obtaining the stack address of the target address pointer based on the stack bottom pointer and a first offset value, wherein the first offset value indicates an offset distance between the stack address indicated by the stack bottom pointer and the stack address of the target address pointer.

[0029] In another possible implementation, the first determination module is specifically used to obtain the instruction address where the offset value of the function metadata is located based on the target address pointer and the second offset value, and the second offset value indicates the offset distance between the instruction address indicated by the target address pointer and the instruction address where the offset value of the function metadata is located.

[0030] In another possible implementation, the instruction address indicated by the target address pointer is the instruction address where the target instruction code is located in the instruction code of the target function.

[0031] In another possible implementation, the target address pointer indicates the instruction address where the offset value of the function metadata is located.

[0032] In a third aspect, an embodiment of the present application provides a computing device, including a memory and a processor, wherein the memory stores instructions, and when the instructions are executed by the processor, the method described in the first aspect is implemented.

[0033] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the method described in the first aspect is implemented.

[0034] In a fifth aspect, an embodiment of the present application further provides a computer program or a computer program product, wherein the computer program or the computer program product comprises instructions, and when the instructions are executed, the computer is caused to execute the method described in the first aspect.

[0035] In a sixth aspect, an embodiment of the present application further provides a chip, comprising at least one processor and a communication interface, wherein the processor is used to execute the method described in the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] Figure 1 A schematic diagram of a stack frame is shown;

[0037] Figure 2 A schematic diagram of the structure of a process space is shown;

[0038] Figure 3a and Figure 3b Schematic diagrams showing the GO language in the offline compilation stage and the runtime stage respectively;

[0039] Figure 4 A flow chart of a method for obtaining function metadata of the first solution is shown;

[0040] Figure 5a and Figure 5b The schematic diagram of ART in the compilation stage and the schematic diagram in the running stage are shown respectively;

[0041] Figure 6 This example illustrates the method flow chart of the Android ART solution at the runtime compilation stage;

[0042] Figure 7 A method flow chart of the Android ART solution in the running stage is shown;

[0043] Figure 8 A schematic diagram of an application scenario of the compiler provided in an embodiment of the present application is shown;

[0044] Fig. 9 A schematic diagram of a data structure of a binary executable file compiled and generated by a compiler provided in an embodiment of the present application is shown;

[0045] Fig.10 A schematic diagram showing function instruction codes in a text segment of a binary executable file generated by a programmer;

[0046] Fig.11 A schematic diagram showing the assembly instruction code of a function compiled and generated by a compiler;

[0047] Fig.12 A method flow chart of a method for compiling a program code provided in an embodiment of the present application is shown;

[0048] Fig.13 A schematic diagram of an application scenario of the runtime engine provided by an embodiment of the present application is shown;

[0049] Fig.14 A method flow chart of a method for obtaining function metadata during program execution provided by an embodiment of the present application is shown;

[0050] Fig.15a A schematic diagram showing a compiler provided by an embodiment of the present application when applied to an offline compilation scenario;

[0051] Fig.15b A schematic diagram showing a compiler provided by an embodiment of the present application applied to a runtime compilation scenario;

[0052] Fig.16a and Fig.16b The code segment and stack frame space of the embodiment of the present application are respectively shown;

[0053] Fig.17 The following illustrates the implementation steps of the method for obtaining function metadata provided by the embodiment of the present application in the compilation stage;

[0054] Fig.18 The following illustrates the implementation steps of the method for obtaining function metadata provided by the embodiment of the present application during the operation phase;

[0055] Fig.19 A schematic diagram of the structure of a device for acquiring function metadata provided in an embodiment of the present application;

[0056] Fig. 20 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0057] In order to better introduce the embodiments of the present application, the relevant concepts in the embodiments of the present application are first introduced below.

[0058] Heap: The heap mentioned in the embodiments of the present application is a heap in the operating system. It is a space that can be allocated and released by the programmer. The size of the heap is limited by the space of the virtual memory in the computer system and is reclaimed by the operating system when the program ends.

[0059] Stack: The stack mentioned in the embodiments of the present application is a stack in the operating system, which is a space for storing function parameter values, temporary variable values, etc., and its operation method is similar to the stack in the data structure (first in, last out).

[0060] Stack frame: When a function is running, a stack frame is created on the stack to record relevant information generated during the runtime.

[0061] Frame pointer (FP or base pointer, BP): A pointer pointing to the bottom of a stack frame is called the stack bottom pointer.

[0062] Stack pointer (SP): A pointer pointing to the top of a stack frame is called the stack pointer.

[0063] Figure 1 A schematic diagram of a stack frame is shown.

[0064] Push onto the stack: store data onto the stack, or write data onto the stack.

[0065] Function metadata: Function metadata records some information related to the function. It has different English names, such as FunctionInfo, MethodInfo, MethodData, and MethodDescription.

[0066] PC / PC pointer: is a pointer pointing to an address in the code (assembly instruction).

[0067] PC table: records the beginning and end of the assembly instructions of a function. If the value of a PC is within the range of the beginning and end, then the code (assembly instruction) pointed to by the PC belongs to this function. The industry usually uses the PC table to determine which function a PC belongs to.

[0068] Bytecode: Bytecode is an intermediate code, which is a representation between source code and machine code (assembly). Bytecode can be interpreted and executed, or compiled into machine code for execution at runtime. Dex bytecode specifically refers to the bytecode of the ART virtual machine.

[0069] Interpretation: Interpretation is the process of executing intermediate code. Specifically, it means compiling the source code into an intermediate form that is more abstract than machine code. The program is then simulated and interpreted based on this intermediate form, which is the process of turning the abstract into the concrete.

[0070] Compile and execute: compile the source code or intermediate form into machine code and run it directly on the machine.

[0071] Just-in-time compilation (JIT): Compilation is performed at runtime instead of offline compilation.

[0072] Garbage collection: Modern high-level programming languages ​​generally do not require programmers to handle memory application and release, but rather leave this work to the runtime. The runtime scans the global variable area or the surviving objects on the stack and cleans up the memory corresponding to the dead objects. This process is called garbage collection.

[0073] Stack Map: A data structure that records some data on the stack. It can be used to record the live objects on the stack. It can also be understood as a list that records the live objects on the stack. Different languages ​​have different names for it. For example, Java calls it OopMap, which records the location of objects (reference data types) in the stack.

[0074] Object: An object is a name for a class instance in a modern language. A class describes a data structure, and an instance created based on this data structure is called an object.

[0075] Android runtime (ART) virtual machine: refers to the program that Java relies on to run on Android. Usually Java is compiled into Dex bytecode, and then ART parses and executes according to the Dex bytecode.

[0076] Function: In the application examples, Method and Function have the same meaning, namely, function.

[0077] Class: It is the name of an abstract data structure unique to object-oriented programming languages.

[0078] Vtable: It can also be called a virtual function table. It is a lookup table used to find which virtual function to call at runtime. Specifically, it uses the vptr pointer to find the function in the vtable table.

[0079] Load: In the embodiment of the present application, load refers to taking data out of the stack.

[0080] Label: In assembly instructions, label is just a semantic concept without specific content, referring to the location of a certain instruction.

[0081] x86_64: is a 64-bit complex instruction set system chip architecture.

[0082] AArch64: A 64-bit reduced instruction set system chip architecture.

[0083] Process space: refers to the virtual memory layout of a process in the operating system. The process spaces of different operating systems are not the same. The process space described in this invention is only a general concept. Figure 2As shown in the figure, the process space includes the basic components of kernel space (Kernel Space), stack (address grows downward), mapping segment (Memory Mapping Segment), heap (address grows upward), uninitialized data segment (BSS Segment, Block Started by Symbol), initialized data segment (Data Segment) and code segment (Text Segment).

[0084] As can be seen from the background technology description, it is often necessary to obtain function metadata in programming languages, but it is difficult to obtain function metadata during program execution. In order to obtain function metadata during program execution, the following solutions are proposed.

[0085] It is understandable that the "it is difficult to obtain function metadata during program execution" mentioned in the embodiments of the present application is limited to compiling user code (source code / intermediate code) into machine code for execution, rather than interpreting and executing the simulated execution of user code. If it is interpreted, the currently executed function is usually added to the context of the interpreter, so it is easy to find the metadata of the currently executed function.

[0086] The first solution is a technical solution for GO semantics. In the GO language system, Module means module, which is at a higher level than Package. A Module can contain multiple different Packages, and each Package can contain multiple directories and many source code files. Correspondingly, the moduleData structure contains all the information under the module, and each module is connected through a linked list. The full name of pclntab is Program CounterLine Table, which literally means a mapping table between PC and line number. The content of pclntab contains the specific information of the function, which can be understood as a function metadata table.

[0087] The GO language has two stages: offline compilation and runtime. Figure 3a and Figure 3b Schematic diagrams of the GO language in the offline compilation stage and the runtime stage are shown respectively. Figure 3aAs shown in the figure, in the offline compilation phase, after compiling the Go language, a binary file corresponding to the platform will be generated, such as ELF for Linux, PE for Windows, and MachO for MacOS. The compiled moduleData (module data table) and pclntab (function information table) data are placed in this binary file. Currently, the module data table and the function information table are encapsulated in the .noptrdata segment and the .gopclntab segment respectively. At runtime, these two modules are also loaded into memory. The moduleData contains a pointer to the pclntab table.

[0088] like Figure 3b As shown in the figure, when the GO program is running, the contents of this binary will be mapped to the memory space (such as the initialization data segment space). When the GO program needs to obtain function metadata, it will first obtain moduleData, then obtain the pclntab inside, and then extract the specific function information from it. ModuleData contains minpc and maxpc, which is similar to the design of the PC table, and can determine which module the PC belongs to.

[0089] Figure 4 The flowchart of the method for obtaining function metadata of the first solution is shown. Figure 4 As shown, the method at least includes steps S401 to S404.

[0090] In step S401, the current PC is obtained by common means in the industry.

[0091] In step S402, the moduleData linked list is traversed according to the PC loop to find out which module the PC belongs to.

[0092] In step S403, various operations are performed on PC to calculate an offset, and then the ftab (FunctionTable) function table in moduleData is looped according to the offset to finally determine the function index.

[0093] In step S404, the function offset in pclntab is obtained according to the function index, and the function information structure, ie, function metadata, is obtained in the form of pclntab+offset.

[0094] From the perspective of the performance of obtaining function metadata, this solution has a time complexity of O(n) based on the PC loop traversing moduleData and ftab, and the performance of finding function metadata is poor. However, since no additional instructions are added to the function instructions, it has no impact on the performance of daily program operation. On the other hand, from the perspective of memory overhead, the GO language obtains function metadata by relying on the data in moduleData, which has a large memory overhead.

[0095] The second solution is the Android ART solution. Android ART has two compilation stages. The first is the static compilation stage to generate Dex bytecode, and the second is the runtime compilation stage to generate assembly instructions. Figure 5a and Figure 5b The following are schematic diagrams of ART in the compilation phase and the running phase respectively.

[0096] At runtime, ART can interpret and execute Dex bytecodes, as well as assembly instructions compiled at runtime. Runtime compilation is also called JIT (Just-in-Time Compilation), and generally the compiled assembly instructions are placed in the mapping segment.

[0097] What is discussed here is the process of obtaining function metadata when executing assembly instructions at runtime.

[0098] ART adds a series of instructions to the generated assembly instructions. The purpose is to dynamically obtain function metadata at runtime and push the function metadata pointer to a fixed position on the stack for storage, so that the function metadata can be obtained through the bottom or top pointer of the stack when running the compiled code.

[0099] The reason why Android ART does this is that dynamically obtaining function metadata at runtime can achieve the effect of delayed parsing. If all functions are parsed after the Dex bytecode is loaded into ART, it will be very time-consuming. Parsing when a function of a certain class is used can reduce the startup time of the application.

[0100] Since ART has two modes, interpreted execution and runtime compiled execution, each time before calling a function, it is necessary to load the metadata information of the called function to determine whether the function has been compiled. If it has not been compiled, it will be interpreted. Therefore, since loading the metadata information of the called function is a necessary operation, it is also a very easy solution to pass the metadata of the called function into the called function through parameters. However, not all languages ​​need to determine whether to interpret or compile. For example, the GO language only has a compiled mode, so the overhead of ART's runtime dynamic loading of function metadata is caused by its own scenario limitations.

[0101] Java functions are placed in classes, so to get a function, you need to get the class information first. In addition, since Java supports polymorphism, that is, the function to be called is not determined at compile time, but only during program execution, so you need to get the specific function to be called from the vtable.

[0102] The Android ART solution mainly includes the solution at the runtime compilation stage and the solution at the runtime stage. Figure 6 This example shows the method flow chart of the Android ART solution during the runtime compilation phase. Figure 6 As shown, the method in the runtime compilation stage at least includes steps S601 to S604.

[0103] In step S601, the runtime compilation stage adds a series of<load class> and<load vtable> Instructions to obtain the ArtMethod (function metadata) pointer.

[0104] In step S602, an instruction is added to pass the ArtMethod pointer to the called function via a parameter.

[0105] In step S603, an instruction is added for the called function to obtain the ArtMethod pointer of the function from the parameter.

[0106] In step S604, an instruction to push the ArtMethod pointer to the stack is added.

[0107] Figure 7 The flowchart of the method of the Android ART solution in the running stage is shown. Figure 7 As shown, the method in the running stage at least includes steps S701 to S704.

[0108] In step S701, the stack bottom pointer register of the current stack frame is obtained, such as the x29 register in AArch64.

[0109] In step S702, the stack address of the pushed ArtMethod pointer is obtained by means of stack bottom pointer + fixed offset.

[0110] In step S703, the ArtMethod pointer is loaded from the stack address of the pushed function metadata.

[0111] In step S704, the ArtMethod pointer points to the function metadata, and the function metadata can be read through the ArtMethod pointer.

[0112] The second solution places the function metadata pointer directly in the stack frame. The function metadata can be quickly obtained at runtime. However, multiple instructions at runtime continuously maintain the acquisition and insertion of function metadata, which affects the performance of daily business. In addition, the memory overhead is mainly in the size of instructions such as obtaining function metadata, parameter passing, and stacking at runtime, which will increase the memory overhead of the program.

[0113] In order to address the problems existing in the above-mentioned scheme, the present application proposes a method for obtaining function metadata, which increases the offset instructions pointing to the function metadata and the push pc instructions in the assembly instructions of the function, with small memory overhead, and only adds the push pc instructions during program running, which has little impact on the program running performance. The offset of the function metadata is obtained through the pc, and then the function metadata is obtained according to the offset, and the speed of obtaining the function metadata is fast.

[0114] It can be seen that this application realizes the rapid acquisition of function metadata by combining compilation and runtime, reduces the search time of function metadata, and accelerates the execution speed of the program.

[0115] The method for obtaining function metadata provided in the embodiment of the present application involves improvements to the program at the compilation stage and the runtime stage, including a method for compiling program code and a method for running the program. The method for compiling program code is applied to a compiler, and the compiler can compile source code / intermediate code to generate an offset value and<push pc> The binary executable file of the instruction. The program running method is applied to the runtime engine, which can achieve lightweight acquisition of function metadata through the PC value and the offset value of the function metadata during the program running phase.

[0116] Figure 8 A schematic diagram of an application scenario of the compiler provided in the embodiment of the present application is shown. Figure 8 As shown, the user (developer) inputs the source code into the compiler, and the compiler compiles the source code and outputs a binary executable file.

[0117] It is understandable that the formats of binary executable files may be different on different platforms. The embodiments of the present application do not limit the formats of binary executable files. The compiler provided in the embodiments of the present application generates binary executable files in the formats corresponding to the platforms on different platforms. For example, the binary executable files compiled in Linux are ELF files, the binary executable files compiled in Windows are PE files, and the binary executable files compiled in MacOS are MachO files.

[0118] Fig. 9A schematic diagram of a data structure of a binary executable file compiled and generated by a compiler provided in an embodiment of the present application is shown. Fig. 9 As shown, the binary executable file generated by the compiler includes multiple code segments, such as text segments, data segments, and other custom segments.

[0119] Function code and function metadata are encapsulated in multiple code segments of a binary executable file. For example, function code is encapsulated in a text segment, and function metadata is encapsulated in a data segment or other custom segments. After the compiler completes compilation, the relative position of the function metadata is determined, that is, the offset distance of the instruction address of the function metadata relative to a base address in the binary executable file is determined. For example, the base address can be the instruction address of the function code. When the program code is executed, the binary executable file will be loaded into the memory. The loading of the binary executable file into the memory does not change the relative position between the function code and the function metadata. In other words, during the running stage, the offset distance between the instruction address of the function code and the instruction address of the function metadata will not change. Therefore, the offset of the function metadata can be determined during the compilation stage and inserted into the function code, so that the offset of the function metadata can be obtained during the running stage, and then the function metadata can be obtained based on the offset.

[0120] Of course, the base address may also be other instruction addresses in the binary file, such as the instruction address of the function header and the instruction address of the offset table.

[0121] Optionally, the offset value of the function metadata can be inserted into the function header position of the function. For example, the first 8 bytes of each function header will contain a function metadata offset value (Methodinfo Offset), which indicates how many positions are offset from the address where the function metadata of the corresponding function is stored. For example, function metadata includes relevant information of the function such as StackMaps, Debuginfo, ExceptionHandler, etc.

[0122] Fig.10 The schematic diagram shows the function instruction code in the text segment of the binary executable file generated by the programmer. Fig.10 As shown, the offset of the function metadata of the function is inserted into the function header of each function code segment.

[0123] It is understandable that for each function's storage space, there is a storage space before the storage space of the function instruction code. This storage space can be called the function header of the function. For example, Fig.10In the text space shown, the space where Methodcode is located is the storage space of the function instruction code, and the space where Methodinfo Offset is located is the function header.

[0124] The function header may store only the offset value of the function metadata, or may store the offset value of the function metadata and other data related to the function.

[0125] Of course, in some other examples, the offset of the function metadata may also be stored in a unified offset table, and the offset value of the function metadata may be obtained by searching the offset table during the runtime.

[0126] In order to find the offset value of the function metadata at runtime, you can insert the following into the assembly instruction of the function:<push pc> Instruction, which is used to push the pc into the stack frame space corresponding to the function, through which the instruction address where the function metadata offset is located can be found.

[0127] Fig.11 FIG. 1 shows a schematic diagram of the assembly instruction code of the function compiled and generated by the compiler. Fig.11 As shown, in the assembly instruction code of each function,<push pc> instruction.

[0128] It should be explained that the compiler provided in the embodiment of the present application can be applied to offline compilation to compile source code into a binary executable file, and can also be applied to runtime compilation to compile intermediate code into a binary executable file.

[0129] Fig.12 A method flow chart of a method for compiling a program code provided by an embodiment of the present application is shown. The method can be applied to Figure 8 In the compiler shown in FIG. 1 , the assembly instructions generated by the compilation have the offset value information of the function metadata. Fig.12 As shown, a method for compiling a program code provided in an embodiment of the present application at least includes steps S1201 to S1204.

[0130] In step S1201, the offset value of the function metadata is obtained.

[0131] After the compiler completes compilation, the offset distance between the instruction address of the function metadata and a base address in the binary file is fixed. The compiler can obtain the instruction address and base address of the function metadata, and then subtract the two addresses to obtain the offset distance between the instruction address of the function metadata and the base address. This offset distance is also the offset value of the function metadata.

[0132] The base address can be the instruction address of the function code. For example, after the compiler compiles, assembles and links the source code, the instruction address of the function code and the instruction address of the function metadata of each function are fixed. At this time, the instruction address of the function code and the instruction address of the function metadata can be obtained, and then the offset value of the function metadata can be obtained by subtracting the instruction address of the function code from the instruction address of the function metadata.

[0133] The base address can also be the instruction address of the function header. For example, after the compiler compiles, assembles and links the source code, the instruction address of the function header of each function (the starting address of the function header can be used as the instruction address of the function header) and the instruction address of the function metadata are fixed. At this time, the instruction address of the function header and the instruction address of the function metadata can be obtained, and then the offset value of the function metadata can be obtained by subtracting the instruction address of the function header from the instruction address of the function metadata.

[0134] The base address can also be an offset table maintained by the binary file in a specific address space, and the offset table is used to store the offset table and the offset values ​​of each function. For example, after the compiler compiles, assembles and links the source code, the offset distance between the instruction address of the function metadata of each function and the instruction address of the offset table is fixed. At this time, the instruction address of the offset table and the instruction address of the function metadata can be obtained, and then the offset value of the function metadata can be obtained by subtracting the instruction address of the offset table from the instruction address of the function metadata.

[0135] In step S1202, the offset value of the function metadata is inserted into the function header.

[0136] Insert the offset value of the function metadata into the function header of the function. For example, the first 8 bytes of each function header will contain an offset value of the function metadata (Methodinfo Offset), indicating how many positions are offset from the address to store the function metadata of the corresponding function.

[0137] In step S1203, an instruction for pushing the target PC onto the stack is inserted into the assembly instruction code of the function.

[0138] For example, add the following to the assembly instructions of the function:<push pc> Instruction, through which the target pc is pushed into the stack frame of the function. There is a fixed offset between the target pc and the function metadata. The instruction address of the function metadata can be obtained by the target pc + fixed offset.

[0139] For example, an instruction address somewhere in a function is used as a target pc value and pushed into the function stack frame. Optionally, a function instruction code address at a preset distance from the function header can be pushed onto the stack as the target pc value.

[0140] In another example, the target PC can also directly indicate the instruction address where the function metadata offset is located. For example, the compiler obtains the instruction address of the function header and then passes<push pc> The instruction pushes the instruction address of the function header as the target pc into the function stack frame.

[0141] In step S1204, a first offset value is determined, where the first offset value indicates an offset distance between the stack address of the target PC and the stack bottom pointer of the function stack frame.

[0142] The first offset value may be a default value, for example, the compiler pushes the target pc to a fixed position on the stack at a distance from the stack bottom pointer by default.

[0143] The first offset value may also be set by the user. For example, the compiler receives a first offset value of N bytes input by the user, generates an assembly instruction according to the first offset value, and pushes the target PC onto the stack at a position N bytes away from the stack bottom pointer.

[0144] The compiler records the first offset value in the binary executable file generated by the compiler so that the first offset value can be obtained during runtime.

[0145] In another example, the first offset value is a default value and cannot be changed. In this case, the compiler does not need to record the first offset value. During the runtime phase, the stack address of the target PC can be directly obtained according to the stack bottom pointer + the default value.

[0146] In step S1205, a second offset value is determined, where the second offset value indicates an offset value distance between the target pc and the offset value of the function metadata.

[0147] The second offset value may be a default value, that is, the function instruction code address at a default distance from the instruction address of the function metadata is pushed onto the stack as the target pc value.

[0148] The second offset value can also be set by the user. For example, the compiler receives a second offset value of M bytes input by the user, and pushes the function instruction code address M bytes away from the instruction address of the function metadata as the target pc value onto the stack according to the second offset value.

[0149] In another example, the second offset value is a default value and cannot be changed. In this case, the compiler does not need to record the second offset value. During the runtime phase, the instruction address where the function metadata is located can be directly obtained according to the target pc + default value.

[0150] As an example, in an x86 system, the assembly instructions generated by the compiler provided in the embodiment of the present application are as follows:

[0151]

[0152] Through a call instruction, the value stored in the current rip register (the address of the next instruction to be executed) is pushed into the function stack frame, without changing the overall execution flow of the program. It is guaranteed that there is a fixed offset N between the stack address of the value stored in the rip register and the stack bottom pointer of the function stack frame, and there is a fixed offset M between the value stored in the rip register and the address of the function header instruction.

[0153] It can be seen from the above assembly instruction code that the assembly code of a general function body will have two assembly instructions "pushq%rbp" and "movq%rsp,%rbp" at the beginning, among which the "pushq%rbp" assembly instruction is used to push the value stored in the current stack bottom pointer register %rbp onto the stack. The value stored in the current stack bottom pointer register %rbp is the stack bottom pointer of the previous function, that is, the function of the "pushq%rbp" assembly instruction is to push the stack bottom pointer of the previous function onto the stack; the "movq%rsp,%rbp" assembly instruction is used to update the value in the stack bottom register %rbp, and update the value in the stack bottom register %rbp to the stack bottom pointer of the current function, for example, update the value in the stack top pointer register %rsp at the current time (i.e., the time after the "pushq%rbp" assembly instruction is executed) to the stack bottom pointer register %rbp.

[0154] It can be understood that the value stored in the rip register is the target pc mentioned above.

[0155] In another example, the target PC in step S1203 can also directly point to the instruction address of the function metadata. In this case, it is necessary to obtain the function header instruction address and push the function header instruction address into the function stack frame space. In this example, since the instruction address of the function metadata is obtained directly through the target PC, there is no offset between the target PC and the instruction address where the offset value of the function metadata is located, and there is no need to execute step S1204.

[0156] As an example, in an aarch64 system, the assembly instructions generated by the compiler provided in an embodiment of the present application are as follows:

[0157]

[0158]

[0159] The instruction address of the function header (i.e., the instruction address of the function metadata) is obtained through the adr instruction, and then the instruction address of the function header is saved at a fixed offset from the stack bottom pointer of the function stack frame through the stur instruction.

[0160] The assembly instructions compiled by the compiler provided in the embodiment of the present application can be implemented in the running stage to find the instruction address of the function header through the bottom pointer of the function stack frame, and then find the instruction address of the function metadata in combination with the offset value of the function metadata, thereby obtaining the function metadata.

[0161] In some other examples, the offset value of the function metadata may also be stored in a unified offset table, and the offset of the function metadata is the offset distance between the function metadata and the instruction address of the offset table. In this case, step S1202 is to update the offset value of the function metadata to the offset table.

[0162] For example, the compiler maintains an offset table storing the offset values ​​of each function metadata, and encapsulates the offset table in a code segment of the compiled binary executable file, such as a data segment or other custom segment. Accordingly, the target pc in step S1203 points to or the target pc+fixed offset points to the instruction address in the offset table of the offset value of the function metadata.

[0163] Fig.13 A schematic diagram of an application scenario of the runtime engine provided by the embodiment of the present application is shown. Fig.13 As shown, the binary executable file generated by the compiler is input into the runtime engine to run the program, and lightweight acquisition of function metadata is achieved at the same time.

[0164] Combine the following Fig.14 Explain in detail the specific implementation of lightweight acquisition of function metadata by the runtime engine during program execution.

[0165] Fig.14 The flowchart of a method for obtaining function metadata during program execution provided by an embodiment of the present application is shown. Fig.13 The runtime engine execution implementation shown in FIG. 1 is used to quickly obtain function metadata during program execution. Fig.14 As shown, the method at least includes steps S1401 to S1406.

[0166] In step S1401, the stack address of the target pc is obtained.

[0167] As can be seen from the above description, the assembly instructions generated by the compiler include assembly instructions for pushing the target PC into a position at a fixed distance (second offset value) from the function stack frame.

[0168] During the run phase, the runtime engine executes the assembly instruction and pushes the target pc into the function's stack frame at a fixed distance from the bottom of the stack.

[0169] For example, the assembly instruction could be<push pc> Instruction, which instructs to push the target PC into the stack frame corresponding to the target function. The target PC is related to the instruction address of the function metadata offset value, and the instruction address of the function metadata can be obtained through this PC.

[0170] Insert the assembly instruction at the appropriate location<push pc> Instruction, you can push the target pc into the stack frame of the function at a fixed distance from the bottom of the stack during the run phase. For example, insert after the second instruction of the assembly instruction <pushpc>The instruction inserts the <push pc> instruction into the stack frame space of the function during the running phase. The occupied space of one instruction pushed onto the stack is 8 bytes, and pc is the distance of 8 bytes from the bottom of the stack frame.

[0171] The insertion position of the <push pc> instruction in the assembly instructions of the function affects the layout in the stack frame space. For example, if the first instruction of the function assembly instructions is the <push the stack bottom pointer of the previous function> instruction, and the <push pc> instruction is inserted after the <push the stack bottom pointer of the previous function> instruction, then during the running phase when executing the assembly instructions, the "stack bottom pointer of the previous function" and "pc" are pushed onto the function stack frame in sequence, and "pc" is stored adjacent to the "stack bottom pointer of the previous function" in the stack frame space.

[0172] For another example, if the first push instruction of the function assembly instructions is the <push the stack bottom pointer of the previous function> instruction, and the <push pc> instruction is inserted after the second assembly instruction, then after executing the assembly instructions during the running phase, the "stack bottom pointer of the previous function" and "pc" are pushed onto the function stack frame, and "pc" and the "stack bottom pointer of the previous function" are stored at intervals in the stack frame space.

[0173] In step S1402, based on the stack address of the target pc, the target pc is obtained.

[0174] After obtaining the stack address of the target pc through step S1401, the value of the target pc can be loaded according to the stack address of the target pc to obtain the target pc.

[0175] In step S1403, based on the target pc, the instruction address where the offset value of the function metadata is located is obtained.

[0176] When the target pc directly indicates the instruction address where the offset value of the function metadata is located, the runtime engine obtains the target pc, which is the instruction address where the offset value of the function metadata is located.

[0177] When there is a second offset value between the target pc value and the instruction address where the offset value of the function metadata is located, the runtime engine obtains the instruction address where the offset value of the function metadata is located by the method of target pc + second offset value.

[0178] In step S1404, based on the instruction address of the function metadata offset value, the offset value of the function metadata is obtained.

[0179] After obtaining the instruction address of the function metadata offset value, the runtime engine can read the offset value of the function metadata from the memory space according to this instruction address.

[0180] In step S1405, the instruction address of the function metadata is obtained based on the offset value and the base address of the function metadata.

[0181] After obtaining the function metadata offset value, the runtime engine can obtain the instruction address of the function metadata according to the offset value and the base address.

[0182] In one example, the base address is the instruction address of the function code, and the instruction address of the function code is the instruction address of the function header. As can be seen from the above, one compilation scheme is to insert the offset value of the function metadata into the function header of the function. At this time, the instruction address of the function code is the instruction address where the offset value of the function metadata is located. The instruction address of the function metadata offset value obtained in step S1403 and the offset value of the function metadata obtained in step S1404 can be used to calculate the instruction address of the function metadata.

[0183] In the scheme of this example, the instruction address of the function header is the instruction address where the offset value of the function metadata is located. When the instruction address of the function header is obtained, it is equivalent to obtaining the instruction address where the offset value of the function metadata is located. There is no need to obtain the instruction address where the offset value of the function metadata is located through the function header position again, which saves instruction memory overhead. At the same time, the instruction address where the offset value of the function metadata is located is less offset from the target PC, which speeds up the speed of obtaining the offset value of the function metadata, thereby increasing the speed of obtaining the function metadata.

[0184] In another example, the base address may also be the instruction address of an instruction code in the instruction code of the target function. After obtaining the instruction address where the offset value of the function metadata is located, the base address can be obtained by adding a fixed offset to the instruction address where the offset value of the function metadata is located.

[0185] For example, the offset value of the function metadata is inserted into the function header position of the target function, and the base address is the instruction address of the instruction code of the function which is n bytes away from the instruction address D1 where the offset value of the function metadata is located in the function header. After obtaining the instruction address D1 where the offset value of the function metadata is located, the base address is the instruction address D1+n, where n can be an integer greater than 0.

[0186] In step S1406, the function metadata is obtained based on the instruction address of the function metadata.

[0187] After the instruction address of the function metadata is obtained through the above steps, the function metadata is read from the memory according to the obtained instruction address of the function metadata.

[0188] The runtime engine provided by the embodiment of the present application pushes an instruction address somewhere in the current function to a fixed position on the stack away from the stack bottom pointer during function execution; in the process of returning the function to the stack, obtains an instruction address somewhere that was previously pushed to the stack through the stack bottom pointer; obtains the instruction address of the function header through the somewhere instruction address; then obtains the instruction address of the function metadata through the offset value of the function metadata stored in the instruction address of the function header, and obtains the function metadata by reading it from the storage space corresponding to the instruction address of the function metadata.

[0189] In another example, the base address in step S1405 can also be the instruction address of the offset table. At this time, during the running stage, the runtime engine first obtains the instruction address of the offset table, and then calculates the instruction address of the function metadata based on the offset value of the function metadata and the instruction address of the offset table.

[0190] It can be understood that the instruction address mentioned in the embodiments of the present application means the storage address of a certain instruction or value in the memory space.

[0191] The method for obtaining function metadata provided in the embodiment of the present application adds a small number of instructions to the assembly instructions of the function, such as<push pc> Instructions, reducing instruction memory overhead, while only increasing the execution time during the run phase<push pc> The instruction has almost no effect on the program running performance. Secondly, the function metadata offset value is obtained through the target PC + offset, and then the function metadata is obtained through the offset value, so that the function metadata can be quickly obtained and the execution speed of the program can be accelerated.

[0192] It should be explained that function metadata can be understood as data used to describe a function, which can describe the function's properties, structure, and other related data. Function metadata includes information data related to the function, such as the line number information when the function executes to a certain line, the current stack depth information of the function, the function ID, and the exception information generated during the execution of the function. For different programming languages, due to the different syntax of each programming language, the content of the corresponding function metadata may also be different. For example, there is no concept of objects in the C programming language, and the function metadata does not include the object survival table. However, there is the concept of objects in the Java language, and the function metadata includes the object survival table.

[0193] The following introduces the detailed implementation of the method for obtaining function metadata provided in the embodiment of the present application in practical applications.

[0194] Programming languages ​​are often divided into two stages: compilation and running. Compilation is further divided into offline compilation and runtime compilation. Fig.15a A schematic diagram showing an embodiment of the present application providing a compiler for an offline compilation scenario. Fig.15a As shown, the assembly code instructions of the function compiled and generated by the compiler are encapsulated in the text segment of the binary executable file, and the content in the text segment will be mapped to the code segment at runtime.

[0195] Fig.15b A schematic diagram showing the compiler provided by the embodiment of the present application is applied to a runtime compilation scenario. The runtime compilation instruction is Fig.15b The content in the dotted box, the instructions generated by runtime compilation are generally stored in the mapping segment, also known as the mmap segment (Memory Mapping Segment). Some programming languages ​​only have offline compilation mode, and some programming languages ​​have both compilation modes.

[0196] The embodiment of the present application does not limit when the compilation phase is performed, but only cares about the generated instruction style. In order to obtain function metadata in a lightweight manner, the embodiment of the present application only adds<offset toMethodMeta> and<push pc> Instructions, here<push pc> It is not necessarily an instruction, but a semantic generalization, that is, storing the value of PC in the stack. The value of PC here is not limited. It can be the PC address where the function metadata offset is stored, or it can be a PC address that maintains a fixed distance from the PC address at the offset position. The purpose is to obtain the function metadata offset through the PC.

[0197] During the running stage, the runtime engine provided by the embodiment of the present application obtains the PC value saved on the stack, and then obtains the instruction address where the offset of the function metadata is located through the PC value (such as obtaining the instruction address where the offset value of the function metadata is located through the pc+offset method as described above, or the pc value directly points to the instruction address where the offset of the function metadata is located), reads the offset of the function metadata through the instruction address, and thus obtains the function metadata, thereby realizing lightweight acquisition of the function metadata.

[0198] Fig.16a and Fig.16b The code segment and stack frame space of the embodiment of the present application are shown respectively. The code segment contains the instructions that the program needs to execute, and the CPU will parse the assembly instructions to operate the stack frame space, such as Fig.16a For the instruction <push the stack bottom pointer of the previous function> in [the text], the stack bottom pointer of the previous function will be pushed onto the stack. This behavior is a common process when entering a new function. For example, on an x86_64 system, this instruction is <pushq %rbp>, which stores the value in the rbp register (usually storing the stack bottom pointer) onto the stack. On an AArch64 system, this instruction is <stp x29,x30,[sp,#0x10]>, which stores the values in the x29 register (usually storing the stack bottom pointer) and the x30 register onto the stack.

[0199] In the embodiment of the present application, during the compilation stage, an offset pointing to the function metadata will be inserted at the beginning of the code instructions of the function. After the instruction <push the stack bottom pointer of the previous function>, an instruction <push pc> will be inserted. <push pc> is a general writing method, and its semantics is to store a PC value onto the stack. This value is not limited. It can be either the instruction address where the offset of the function metadata is located, or there can be a fixed offset from the instruction address where the offset of the function metadata is located. As long as the instruction address where the offset of the function metadata can be obtained through this PC value, the offset of the function metadata can be loaded, and then the function metadata can be obtained through the offset. Here, the two instructions <push the stack bottom pointer of the previous function> and <push pc> are not necessarily adjacent, as long as they are in sequence after <push the stack bottom pointer>. From Fig.16b it can be seen that in the corresponding stack frame space, there is a fixed distance between the stored stack bottom pointer and the PC pointer, and this distance can also be 0.

[0200] On different chip architectures, the assembly instruction corresponding to <push pc> is different. For example, on an x86_64 system, this instruction can be <callq label>. The callq instruction will push the instruction address of the next instruction after the callq instruction onto the stack, and then the program will jump to the instruction at the label for execution. However, if the instruction at the label is itself the next instruction after the callq instruction, then the callq instruction is equivalent to just storing the next instruction address value onto the stack. See the following description for specific instructions.

[0201] During the function execution process, the layout of the pushed data in the function stack frame can be seen in Fig.16b the stack frame space shown. The stack bottom pointer of the previous function and the PC pointer can be stored adjacent to each other in the stack frame space (that is, Fig.16b the fixed distance in [the text] is 0), and of course, the stack bottom pointer of the previous function and the PC pointer can also be stored at intervals.

[0202] Fig.16a The function metadata content shown in the figure includes an exception table and an object survival table (StackMap), which is only an example. The function metadata can also include more or less data. For example, the function metadata can also include one or more data including the line number information when the function executes to a certain line, the current stack depth information of the function, and the ID of the function.

[0203] It should be explained that the content of the function metadata corresponding to each programming language is not exactly the same. The function metadata in the embodiment of the present application can be any data related to the function, and the embodiment of the present application does not limit the specific content of the function metadata.

[0204] Fig.17 The following shows the implementation steps of the method for obtaining function metadata provided by the embodiment of the present application in the compilation stage. Fig.17 As shown, the method for obtaining function metadata provided by the embodiment of the present application during the compilation stage includes at least step S1701 and step S1702.

[0205] In step S1701, the offset of the function metadata is inserted at the beginning of the function instruction.

[0206] In step S1702, insert the following into the instruction of the function:<push pc> instruction.

[0207] Fig.18 The following shows the implementation steps of the method for obtaining function metadata provided by the embodiment of the present application in the running stage. Fig.18 As shown, the method for obtaining function metadata provided by the embodiment of the present application during the running stage includes at least steps S1801 to S1806.

[0208] In step S1801, the stack bottom pointer register of the current stack frame is obtained by common means in the industry, such as the rbp register of x86_64 and the x29 register of AArch64.

[0209] In step S1802, the stack address of the stack bottom pointer position pushed in the current function stack frame is obtained through the value in the stack bottom pointer register.

[0210] In step S1803, the stack address of the pushed stack bottom pointer position is added with an offset to obtain the stack address of the pushed PC.

[0211] In step S1804, the PC value is loaded from the stack address where the PC is pushed.

[0212] In step S1805, the function metadata offset is obtained by adding the PC value to the offset.

[0213] In step S1806, the function metadata is obtained through the offset of the function metadata.

[0214] The following takes the x86_64 system and the AArch64 system as examples to introduce the code instructions generated by the function in the encoding stage and the running stage after adopting the method for obtaining function metadata provided by the embodiment of the present application.

[0215] In the x86_64 system, the function assembly instructions generated by the function during the compilation phase are as follows:

[0216]

[0217] Through the above function assembly instructions, we can know:

[0218] The offset of the function metadata is added at the beginning of the function. The label .Lmethod_desc.main$ points to the instruction address of the function metadata, and the label .Ltmp0 points to the current instruction address, so the subtraction is the offset between the current position and the function metadata.

[0219] exist<pushq%rbp> Added after the instruction<callq.LStorePC> Instruction, which will jump to the position of label <.LStorePC0> and store the next instruction address on the stack. However, since the instruction address corresponding to label .LStorePC0 is the next instruction after call instruction, it is equivalent to not jumping, but only pushing PC on the stack. The PC address at the offset position of the function metadata is a fixed instruction length distance.

[0220] <pushq%rbp> and<mov%rsp,%rbp> These two instructions are common to every function in x86_64. They mean to decrement the stack address in the rsp stack top register by 8, and then push the value in rbp to the position after the stack address -8. The mov instruction then assigns the new rsp value to rbp, which means that rbp now points to the new rsp position.

[0221] The binary corresponding to the assembly instruction is as follows:

[0222]

[0223] Assuming that the value in rbp is 0x55555557f2a0 before the execution of this instruction, the value in r13 is 1, and the value in r14 is 2, the stack frame layout after the execution of this instruction is as follows:

[0224]

[0225] After this instruction is executed, the value in the rbp register is 0x55555557f318, which points to the stack address of the stack bottom pointer rbp that was pushed. It is easy to obtain the current rbp register at runtime. After obtaining the value of rbp, the stack address where the pc is stored can be obtained according to the fixed offset, such as 0x55555557f310 above, and then the pc value is loaded from 0x55555557f310. Then the PC value is related to the offset of the function metadata. For example, the difference between the PC value and the offset of the function metadata in this embodiment is a fixed length 0xd (0x2b30d–0x2b300). The offset of the function metadata can be obtained by adding the PC value + 0xd.

[0226] In the AArch64 system, the function assembly instructions generated by the function during the compilation phase are as follows:

[0227]

[0228] Through the above function assembly instructions, we can know:

[0229] The offset of the function metadata is added at the beginning of the function, the same as the implementation in x86_64 system.

[0230] exist<stp x29,x30,[sp,#16]> Added after the instruction<adr x9,.Lfunc_begin13> and<sturx9,[x29,#-8]> Instruction, adr instruction will get the address of a label and put the address into x9 register. stur instruction will push x9 to x29 register -8. Since the .Lfunc_begin0 label is at the beginning of the function, the instruction address (PC) at the offset position of the function metadata is a fixed distance.

[0231] The binary corresponding to the assembly instruction is as follows:

[0232]

[0233] Assuming that before this instruction is executed, the value in x29 is 0x55555557f2a0, the value in x19 is 1, the value in x20 is 2, and the value in x30 is 0x3250c, then the stack frame layout after this instruction is executed is as follows:

[0234]

[0235] After this instruction is executed, the value in the x29 register is 0x55555557f300, pointing to the stack address of the pushed x29. At runtime, the value of x29 is obtained through common means, and the stack address of the pushed pc is at the position of the x29 value minus 8. After obtaining the pc value 0x007dc, the address of the function metadata offset 0x007d8 can be obtained based on the pc value and the fixed offset 4 of the function metadata, and then 0x7d8+0x2d67 can be used to obtain the function metadata.

[0236] It is understandable that there are some scenarios in which function metadata needs to be obtained during program execution. By executing the method for obtaining function metadata provided in the embodiment of the present application, the required function metadata can be obtained in a lightweight and fast manner.

[0237] For example, in a scenario where an exception occurs during program execution, it is necessary to obtain an exception table in the function metadata (e.g. Fig.16a The ExceptionTable in the function metadata in the program records the exception information of each function during the program execution. Through the exception information, it can be known to which function's exception handling block the exception jumps to, and then the exception is handled. At this time, the method for obtaining the function metadata provided by the embodiment of the present application is executed to quickly obtain the exception information of each function. For example, when an exception occurs during the function execution, the stack is returned, and each function stack frame is backtracked. In the process of the function stack return, the pc that was previously pushed to the stack is obtained through the stack bottom pointer; through the pc, the address where the offset value of the function metadata is located is obtained, and the offset value is obtained according to the address where the offset value is located, and the instruction address of the function metadata is obtained according to the offset value, and the function metadata is read from the storage space corresponding to the instruction address of the function metadata to obtain the exception information of the function.

[0238] In the scenario where GC is executed during program execution, it is necessary to obtain the stack diagram in the function metadata (for example Fig.16a The stack map records the positions of the surviving objects on the stack of each function. The GC can clean up and release the memory corresponding to the non-surviving objects according to the surviving objects on the stack. At this time, the method for obtaining the function metadata provided in the embodiment of the present application is executed to quickly obtain the stack map of each function. For example, when GC occurs during the function execution, the stack is returned and each function stack frame is backtraced. In the process of returning the function to the stack, the pc that was previously pushed to the stack is obtained through the bottom pointer of the stack; through the pc, the address where the offset value of the function metadata is located is obtained, and the offset value is obtained according to the address where the offset value is located, and the instruction address of the function metadata is obtained according to the offset value, and the function metadata is read from the storage space corresponding to the instruction address of the function metadata to obtain the stack map of the function.

[0239] Based on the same concept as the embodiment of the method for obtaining function metadata described above, the embodiment of the present application also provides a function metadata obtaining device 1900, which is deployed on a computing device, which can be any computing device with a processor, and is used to obtain the function metadata of the target function in a lightweight manner during program execution. The function metadata obtaining device 1900 includes a method for implementing Figure 12-18 The units or modules of each step in the method for obtaining function metadata shown.

[0240] Fig.19 A schematic diagram of a structure of a function metadata acquisition device provided in an embodiment of the present application. Fig.19 As shown, the function metadata acquisition device 1900 at least includes an acquisition module 1901, a pointer reading module 1902, a first determination module 1903, an offset value reading module 1904, a second determination module 1905 and a function metadata reading module 1906, wherein the acquisition module 1901 is used to acquire the stack address of the target address pointer, and the target address pointer is executed based on the first assembly instruction of the target function and written into the stack frame corresponding to the target function; the pointer reading module 1902 is used to obtain the target address pointer based on the stack address of the target address pointer; the first determination module 1903 is used to obtain the instruction address where the offset value of the function metadata is located based on the target address pointer, and the offset value of the function metadata indicates the offset distance between the instruction address and the base address of the function metadata of the target function; the offset value reading module 1904 is used to obtain the offset value of the function metadata based on the instruction address where the offset value of the function metadata is located; the second determination module 1905 is used to obtain the instruction address of the function metadata based on the offset value and the base address of the function metadata; the function metadata reading module 1906 is used to obtain the function metadata based on the instruction address of the function metadata.

[0241] In a possible implementation, the assembly instruction of the target function further includes a second assembly instruction, where the second assembly instruction is used to instruct to insert the offset value of the function metadata into the address space before the instruction code of the target function.

[0242] In this possible implementation, the base address is the instruction address where the offset value of the function metadata is located; the second determination module 1905 is specifically used to obtain the instruction address of the function metadata based on the offset value of the function metadata and the instruction address where the offset value of the function metadata is located.

[0243] In another possible implementation, the assembly instruction of the target function further includes a third assembly instruction, where the third assembly instruction is used to instruct to update the offset value of the function metadata into the offset table.

[0244] In this possible implementation, the base address is the instruction address of the offset table; the second determination module 1905 is specifically used to obtain the instruction address of the function metadata based on the function metadata offset value and the instruction address of the offset table.

[0245] In another possible implementation, the assembly instructions of the target function further include a fourth assembly instruction, where the fourth assembly instruction is used to instruct to write the stack bottom pointer of the previous function into the stack frame; the fourth assembly instruction is executed before the first assembly instruction.

[0246] Optionally, the stack bottom pointer of the previous function pushed into the stack and the target pc are stored adjacently or separated by a preset distance in the stack frame.

[0247] In another possible implementation, the acquisition module 1901 is specifically used to: acquire a stack bottom pointer corresponding to the stack frame; and obtain a stack address of a target address pointer based on the stack bottom pointer.

[0248] In another possible implementation, obtaining the stack address of the target address pointer based on the stack bottom pointer includes: obtaining the stack address of the target address pointer based on the stack bottom pointer and a first offset value, wherein the first offset value indicates an offset distance between the stack address indicated by the stack bottom pointer and the stack address of the target address pointer.

[0249] In another possible implementation, the first determination module 1903 is specifically used to obtain the instruction address where the offset value of the function metadata is located based on the target address pointer and the second offset value, and the second offset value indicates the offset distance between the instruction address indicated by the target address pointer and the instruction address where the offset value of the function metadata is located.

[0250] In another possible implementation, the instruction address indicated by the target address pointer is the instruction address where the target instruction code is located in the instruction code of the target function.

[0251] In another possible implementation, the target address pointer indicates the instruction address where the offset value of the function metadata is located.

[0252] The function metadata acquisition device 1900 according to the embodiment of the present application may correspond to the method described in the embodiment of the present application, and the above and other operations and / or functions of each module in the function metadata acquisition device 1900 are respectively to implement Figure 12-18 For the sake of brevity, the corresponding processes of each method in are not repeated here.

[0253] The present application also provides a computing device, including at least one processor, a memory and a communication interface, wherein the processor is used to execute Figure 12-18 The method described.

[0254] Fig. 20 A schematic diagram of the structure of a computing device provided in an embodiment of the present application.

[0255] like Fig. 20 As shown, the computing device 2000 includes at least one processor 2001, a memory 2002 and a communication interface 2003. The processor 2001, the memory 2002 and the communication interface 2003 are connected in communication, and the communication connection can be realized by wired means (such as a bus) or by wireless means. The communication interface 2003 is used to receive data sent by other devices; the memory 2002 stores computer instructions, and the processor 2001 executes the computer instructions to execute the method for obtaining function metadata in the aforementioned method embodiment, so as to achieve lightweight acquisition of function metadata of the target function during program execution.

[0256] It should be understood that in the embodiment of the present application, the processor 2001 may be a central processing unit CPU, and the processor 2001 may also be other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0257] The memory 2002 may include a read-only memory and a random access memory, and provides instructions and data to the processor 2001. The memory 2002 may also include a nonvolatile random access memory.

[0258] The memory 2002 may be a volatile memory or a nonvolatile memory, or may include both volatile and nonvolatile memories. Among them, the nonvolatile memory may be a read-only memory (ROM), a programmable ROM (PROM), an erasable PROM (EPROM), an electrically erasable programmable ROM (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static RAM (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link DRAM (SLDRAM), and direct rambus RAM (DR RAM).

[0259] It should be understood that the computing device 2000 according to the embodiment of the present application can execute the implementation of the embodiment of the present application. Figure 12-18 The method shown, the detailed description of the implementation of this method can be found above, for the sake of brevity, it will not be repeated here.

[0260] An embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon. When the computer instructions are executed by a processor, the above-mentioned method is implemented.

[0261] An embodiment of the present application provides a chip, which includes at least one processor and an interface, wherein the at least one processor determines program instructions or data through the interface; the at least one processor is used to execute the program instructions to implement the method mentioned above.

[0262] An embodiment of the present application provides a computer program or a computer program product, wherein the computer program or the computer program product comprises instructions, and when the instructions are executed, the computer is caused to execute the above-mentioned method.

[0263] Those of ordinary skill in the art should further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those of ordinary skill in the art may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0264] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented by hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0265] The specific implementation methods described above further illustrate the purpose, technical solutions and beneficial effects of the present application in detail. It should be understood that the above description is only the specific implementation method of the present application and is not intended to limit the scope of protection of the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the scope of protection of the present application.< / pushpc>

Claims

1. A method for obtaining function metadata, characterized in that: include: During program execution, a stack address of a target address pointer is obtained, wherein the target address pointer is written into a stack frame corresponding to the target function based on the first assembly instruction in the assembly instructions of the target function; Based on the stack address of the target address pointer, obtain the target address pointer; Based on the target address pointer, obtaining the instruction address where the offset value of the function metadata is located, wherein the offset value of the function metadata indicates the offset distance between the instruction address and the base address of the function metadata of the target function; Obtaining the offset value of the function metadata based on the instruction address where the offset value of the function metadata is located; Based on the offset value of the function metadata and the base address, obtaining the instruction address of the function metadata; The function metadata is obtained based on the instruction address of the function metadata.

2. The method according to claim 1, characterized in that The assembly instructions of the target function also include a second assembly instruction, where the second assembly instruction is used to instruct to insert the offset value of the function metadata into the address space before the instruction code of the target function.

3. The method according to claim 2, characterized in that The base address is the instruction address where the offset value of the function metadata is located; The obtaining the instruction address of the function metadata based on the offset value of the function metadata and the base address includes: Based on the offset value of the function metadata and the instruction address where the offset value of the function metadata is located, the instruction address of the function metadata is obtained.

4. The method according to claim 1, characterized in that: The assembly instructions of the target function also include a third assembly instruction, and the third assembly instruction is used to instruct to update the offset value of the function metadata into an offset table.

5. The method according to claim 4, characterized in that The base address is the instruction address of the offset table; The obtaining the instruction address of the function metadata based on the function metadata offset value and the base address includes: Based on the function metadata offset value and the instruction address of the offset table, the instruction address of the function metadata is obtained.

6. The method according to any one of claims 1 to 5, characterized in that: The step of obtaining the stack address of the target address pointer includes: Obtaining a stack bottom pointer corresponding to the stack frame; Based on the stack bottom pointer, the stack address of the target address pointer is obtained.

7. The method according to claim 6, characterized in that The step of obtaining the stack address of the target address pointer based on the stack bottom pointer includes: The stack address of the target address pointer is obtained based on the stack bottom pointer and a first offset value, wherein the first offset value indicates an offset distance between the stack address indicated by the stack bottom pointer and the stack address of the target address pointer.

8. The method according to any one of claims 1 to 7, characterized in that: The step of obtaining the instruction address of the function metadata offset value based on the target address pointer includes: Based on the target address pointer and the second offset value, the instruction address where the offset value of the function metadata is located is obtained, and the second offset value indicates the offset distance between the instruction address indicated by the target address pointer and the instruction address where the offset value of the function metadata is located.

9. The method according to claim 8, characterized in that The instruction address indicated by the target address pointer is the instruction address where the target instruction code in the instruction code of the target function is located.

10. The method according to any one of claims 1 to 7, characterized in that: The target address pointer indicates the instruction address where the offset value of the function metadata is located.

11. A device for acquiring function metadata, characterized in that: The device is used to obtain function metadata of a target function during the running of a target program, and the device includes: An acquisition module, used for acquiring a stack address of a target address pointer, wherein the target address pointer is executed and written into a stack frame corresponding to the target function based on a first assembly instruction in the assembly instructions of the target function; A pointer reading module, used for obtaining the target address pointer based on the stack address of the target address pointer; A first determination module is used to obtain, based on the target address pointer, an instruction address where an offset value of the function metadata is located, wherein the offset value of the function metadata indicates an offset distance between an instruction address and a base address of the function metadata of the target function; An offset value reading module, used for obtaining the offset value of the function metadata based on the instruction address where the offset value of the function metadata is located; A second determination module, configured to obtain an instruction address of the function metadata based on an offset value of the function metadata and the base address; The function metadata reading module is used to obtain the function metadata based on the instruction address of the function metadata.

12. The device according to claim 11, characterized in that The assembly instructions of the target function also include a second assembly instruction, where the second assembly instruction is used to instruct to insert the offset value of the function metadata into the address space before the instruction code of the target function.

13. The device according to claim 12, characterized in that The base address is the instruction address where the offset value of the function metadata is located; The second determining module is specifically used for: Based on the offset value of the function metadata and the instruction address where the offset value of the function metadata is located, the instruction address of the function metadata is obtained.

14. The device according to claim 11, characterized in that The assembly instructions of the target function also include a third assembly instruction, and the third assembly instruction is used to instruct to update the offset value of the function metadata into an offset table.

15. The device according to claim 14, characterized in that The base address is the instruction address of the offset table; The second determining module is specifically used for: Based on the function metadata offset value and the instruction address of the offset table, the instruction address of the function metadata is obtained.

16. The device according to any one of claims 11 to 15, characterized in that: The acquisition module is specifically used for: Obtaining a stack bottom pointer corresponding to the stack frame; Based on the stack bottom pointer, the stack address of the target address pointer is obtained.

17. The device according to claim 16, characterized in that The step of obtaining the stack address of the target address pointer based on the stack bottom pointer includes: The stack address of the target address pointer is obtained based on the stack bottom pointer and a first offset value, wherein the first offset value indicates an offset distance between the stack address indicated by the stack bottom pointer and the stack address of the target address pointer.

18. The device according to any one of claims 11 to 17, characterized in that: The first determining module is specifically used for: Based on the target address pointer and the second offset value, the instruction address where the offset value of the function metadata is located is obtained, and the second offset value indicates the offset distance between the instruction address indicated by the target address pointer and the instruction address where the offset value of the function metadata is located.

19. The device according to claim 18, characterized in that The instruction address indicated by the target address pointer is the instruction address where the target instruction code in the instruction code of the target function is located.

20. The device according to any one of claims 11 to 17, characterized in that The target address pointer indicates the instruction address where the offset value of the function metadata is located.

21. A computing device comprising a memory and a processor, characterized in that: Instructions are stored in the memory, and when the instructions are executed by the processor, the method according to any one of claims 1 to 10 is implemented.

22. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 10 is implemented.