Function calling method and device, electronic equipment and readable medium

By obtaining the target function name and querying heterogeneous function library, the problem of function call exceptions on the host architecture platform is solved, and function call compatibility and smooth execution between different architectures is achieved.

CN120469740APending Publication Date: 2025-08-12LOONGSON TECH CORP
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510533858.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-25
Publication Date
2025-08-12

AI Technical Summary

Technical Problem

In the prior art, client programs run by host architecture platform may be called exceptions due to rewriting when calling functions, making it difficult to accurately perform function calls.

Method used

By obtaining the object function name requested by the function call during the operation of the target program, and querying heterogeneous function library based on the object function name and the function library address of the client architecture, determine whether there is a rewritten objective function, and then calling the object function in the heterogeneous or homogeneous function library.

Benefits of technology

It realizes direct compatibility of function calls between different architectures, ensuring that the rewritten functions are called smoothly and avoiding call exceptions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120469740A_ABST
    Figure CN120469740A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a function calling method and device, electronic equipment and a readable medium, and relates to the technical field of computers.The method comprises the steps that in the process that a target program runs on a host platform, a function name of a target function called by a function calling request serves as a target function name; the client architecture adapted to the target program is different from the architecture of the host platform; performing heterogeneous function library query based on the target function name and the function library address of the client architecture to obtain a query result; and based on the query result, calling a target function in the heterogeneous function library or the same-architecture function library of the host platform, so that the target function is executed. In this way, the target function can be accurately called from the heterogeneous function library or the same-architecture function library of the host platform based on the query result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a function calling method, device, electronic device and readable medium. Background Art

[0002] With the continuous development of computer technology, more and more instruction set architectures are available. Currently, client programs for client architectures are often run on host architecture platforms. This allows programs compiled for one architecture to run on hardware platforms of another architecture, solving cross-platform compatibility issues between different architectures and improving cross-architecture portability.

[0003] In the prior art, client programs running on a host architecture platform sometimes call functions, and the called functions may be rewritten for some reason, resulting in call exceptions. Therefore, how to accurately call functions becomes a technical problem that needs to be solved urgently. Summary of the Invention

[0004] The embodiments of the present invention provide a function calling method, device, electronic device and readable medium, which can solve the problem of how to accurately call a function.

[0005] In order to solve the above problem, an embodiment of the present invention discloses a function calling method, which includes:

[0006] During the running of the target program on the host platform, the function name of the target function called by the function call request is used as the target function name; the client architecture adapted by the target program is different from the architecture of the host platform;

[0007] Performing a heterogeneous function library query based on the target function name and the function library address of the client architecture to obtain a query result; the query result is used to indicate whether the rewritten target function exists in the heterogeneous function library;

[0008] Based on the query result, the target function in the heterogeneous function library or the same-architecture function library of the host platform is called to execute the target function.

[0009] On the other hand, an embodiment of the present invention discloses a function calling device, comprising:

[0010] an acquisition module, configured to use, during the execution of a target program on a host platform, a function name of a target function called by a function call request as a target function name; wherein the client architecture adapted by the target program is different from the architecture of the host platform;

[0011] A query module, configured to query a heterogeneous function library based on the target function name and the function library address of the client architecture, and obtain a query result; the query result is used to indicate whether the rewritten target function exists in the heterogeneous function library;

[0012] A calling module is used to call the target function in the heterogeneous function library or the same-architecture function library of the host platform based on the query result, so that the target function is executed.

[0013] On the other hand, an embodiment of the present invention discloses an electronic device, comprising: a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the aforementioned method.

[0014] An embodiment of the present invention further discloses a machine-readable medium having instructions stored thereon. When executed by one or more processors, the processors are enabled to execute the method described above.

[0015] The function calling method provided by the embodiment of the present invention uses the function name of the target function called by the function call request as the target function name during the operation of the target program on the host platform; the client architecture adapted by the target program is different from the architecture of the host platform; a heterogeneous function library query is performed based on the target function name and the function library address of the client architecture to obtain a query result; based on the query result, the target function in the heterogeneous function library or the same-architecture function library of the host platform is called to execute the target function. In this way, by performing a heterogeneous function library query through a binary translator, it is possible to accurately determine whether the target function called this time has a rewritten version, that is, whether the rewritten target function exists in the heterogeneous function library, so that the target function can be accurately called from the heterogeneous function library or the same-architecture function library of the host platform based on the query result, so that function calls between different architectures are directly compatible to ensure that the rewritten function is called smoothly. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments of the present invention. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0017] Figure 1 This is a flowchart of a function calling method provided by an embodiment of the present invention;

[0018] Figure 2This is a schematic diagram of a processing process provided by an embodiment of the present invention;

[0019] Figure 3 is another processing process schematic diagram provided by an embodiment of the present invention;

[0020] Figure 4 is a block diagram of a function calling device provided by an embodiment of the present invention;

[0021] Figure 5 It is a structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0022] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention without making any creative efforts shall fall within the scope of protection of the present invention.

[0023] Figure 1 This is a flow chart of the steps of a function calling method provided by an embodiment of the present invention. Figure 1 As shown, the function calling method may include the following steps:

[0024] Step 101: When a target program is running on a host platform, the function name of the target function called by the function call request is used as the target function name; the client architecture adapted by the target program is different from the architecture of the host platform.

[0025] Step 102: perform a heterogeneous function library query based on the target function name and the function library address of the client architecture to obtain a query result; the query result is used to indicate whether the rewritten target function exists in the heterogeneous function library.

[0026] Step 103: Based on the query result, call the target function in the heterogeneous function library or the same-architecture function library of the host platform to execute the target function.

[0027] In an embodiment of the present invention, the target program includes a call instruction for calling a function. During the operation of the target program, if the call instruction is executed, the target program will trigger a function call request. Accordingly, the function called by the function call request is the target function. Furthermore, the heterogeneous function library refers to the function library of the client architecture. The function library of the client architecture can also be called a cross-architecture function library or a cross-architecture shared library. The function library address of the client architecture is the library address of the heterogeneous function library. The functions in the function library of the client architecture, that is, the functions in the heterogeneous function library, are binary files of the client architecture.

[0028] Functions in a common architecture library are binary files for the host architecture. These libraries include functions for implementing common operations, such as module loading and unloading. These functions can be called by different Java programs. Developers sometimes override functions in common architecture libraries to achieve polymorphism, improve program performance, implement customized features, or optimize specific hardware resources. For example, overriding a module loading function can enable authentication during module loading. In object-oriented programming, overriding refers to a subclass redefining and implementing an existing function in a parent class to meet the subclass's needs without modifying the parent class's code, thereby changing the behavior of the parent class's methods. Functions that can be overridden can include virtual functions. Virtual functions are special functions in object-oriented programming that allow overridden functions in subclasses to be dynamically called at runtime. Subclasses can call existing functions in their parent class to meet their own needs.

[0029] Accordingly, for overridden functions, the functions included in the same architecture function library are the functions in the parent class, while the function library of the client architecture includes the overridden functions, that is, the overridden functions in the child class. Of course, functions that can be overridden also include functions used to implement abstract methods, interface methods, and default methods in Java, operator overloading functions in C++ or Python, template methods in Python, and functions in other dynamic languages.

[0030] Furthermore, the embodiment of the present invention is directed to a calling scenario of a function that supports being rewritten. In the case where the function being called this time is a function that supports being rewritten, since the target function being called this time may be rewritten, the target function name can be obtained and subsequent processes can be executed to query whether the heterogeneous function library includes the rewritten target function, and then decide whether to call the target function in the heterogeneous function library or the same architecture function based on the query result. Specifically, when obtaining the target function name, the content of the bit field used to store the function name in the function call request can be extracted to obtain the target function name. Exemplarily, the bit field used to store the function name in the function call request can be determined based on the instruction format of the function call request. Assuming that the bit field used to store the function name is determined to be the 10th to 15th bits according to the established instruction format of the function request, the content of the 10th to 15th bits can be read to obtain the target function name.

[0031] In an embodiment of the present invention, the above-mentioned function calling method can be executed by the host platform. The architecture adapted by the host platform is called the host architecture. The host platform can run a virtual machine, in which user programs of other architectures are run. The other architecture is the client architecture, and the user program of the other architecture run this time is the target program. For example, assuming that a Java program of the x86 architecture is run through a virtual machine on a platform of the LoongArch architecture, then the LoongArch architecture is the host architecture, the x86 architecture is the client architecture, and the user program of the x86 architecture is the target program. The same-architecture function library can be a function library of the host architecture provided by the virtual machine. The virtual machine can be a program adapted to the host architecture. The virtual machine is a program that can run on the host architecture and can simulate the instruction set and execution environment of the client architecture. In this way, the virtual machine can translate the instructions in the user program of the client architecture into instructions of the host architecture through software simulation, thereby executing the instructions of the user program of the client architecture on the hardware of the host architecture. When the target program needs to be run, the host platform can start the virtual machine and then create a virtual machine process. The virtual machine process can use the user program as the target program and create a main thread for the target program. The main thread can be responsible for executing the main function (main) of the target program. The main thread can be called the target program thread. Among them, the virtual machine process can be used to create the target program thread through a virtual machine thread. The virtual machine process also includes some other threads, for example, threads for tasks such as garbage collection and compilation. These threads together constitute the operating environment of the virtual machine. Of course, other cross-architecture operating modes can also be used to run the target program, and the implementation of the present invention is not limited to this.

[0032] In summary, the function calling method provided by the embodiment of the present invention uses the function name of the target function called by the function call request as the target function name during the operation of the target program on the host platform; the client architecture adapted by the target program is different from the architecture of the host platform; a heterogeneous function library query is performed based on the target function name and the function library address of the client architecture to obtain a query result; based on the query result, the target function in the heterogeneous function library or the same-architecture function library of the host platform is called to execute the target function. In this way, by performing a heterogeneous function library query through a binary translator, it is possible to accurately know whether the target function called this time has a rewritten version, that is, whether there is a rewritten target function in the heterogeneous function library, so that the target function can be accurately called from the heterogeneous function library or the same-architecture function library of the host platform based on the query result, so that the function calls between different architectures are directly compatible to ensure that the rewritten function is called smoothly.

[0033] Optionally, the embodiment of the present invention may further include the following steps:

[0034] Step S21 : loading a preset rewriting function list; the rewriting function list records the function names of the functions that are supported to be rewritten.

[0035] Step S22: If a function name consistent with the target function name is recorded in the rewritten function list, the step of performing a heterogeneous function library query based on the target function name and the function library address of the client architecture is executed.

[0036] In an embodiment of the present invention, the rewrite function list can be pre-set by a developer, and the rewrite function list enumerates the function names of all functions that can be rewritten. Exemplarily, the data structure of the rewrite function list can be an array. The developer can pre-create a one-dimensional array, in which each element represents the function name of a function that can be rewritten. The rewrite function list can be stored in a specified location, and accordingly, the data in the specified location can be read to obtain the preset rewrite function list. The specified location can be pre-set by the developer, and this embodiment of the present invention does not limit this. It should be noted that, in response to receiving a function name addition instruction, the host platform adds the function name carried by the function name addition instruction to the rewrite function list. In response to receiving a function name deletion instruction, the host platform deletes the function name carried by the function name deletion instruction from the rewrite function list. In response to receiving a function name modification instruction, the host platform updates the original function name carried by the function name modification instruction recorded in the rewrite function list to the updated function name carried by the function name modification instruction. The function name addition, deletion, and modification instructions are entered by the developer to update the rewrite function list, ensuring that the rewrite function list fully and accurately records the function names of all supported rewritten functions, thereby avoiding misjudgments. Alternatively, the developer can manually update the rewrite function list, which is not limited in this embodiment of the present invention.

[0037] For any function name recorded in the rewrite function list, the target function name can be compared with the function name. If the function name is the same as the target function name, it can be determined that the rewrite function list contains a function name that is consistent with the target function name. Specifically, each character in the target function name can be compared bit by bit with each character in the function name. If the number of characters included in the function name is the same as the number of characters included in the target function name, and each character is consistent, then the two are determined to be the same, thereby reducing the probability of misjudgment. Assuming that the target function name includes m characters, the i-th character in the target function name can be compared with the i-th character in the function name. If the i-th character is consistent, then the i+1-th character is compared. If the m-th character is consistent, that is, m characters are consistent, then the two are determined to be the same. Wherein, i∈[1,m].

[0038] On the contrary, if each function name recorded in the rewrite function list is different from the target function name, it can be determined that there is no function name recorded in the rewrite function list that is consistent with the target function name. Correspondingly, if there is no function name recorded in the rewrite function list that is consistent with the target function name, it means that the target function called this time is not a virtual function, and the target function called this time cannot be rewritten in the heterogeneous function library, that is, the heterogeneous function library will not include the rewritten version of the target function. Therefore, the function call can be made according to the original processing flow, for example, determining the function library to which the target function belongs, and then executing the target function in the function library. Among them, the function library to which the target function belongs can be the aforementioned same-architecture function library.

[0039] Furthermore, if a function name that is consistent with the target function name is recorded in the rewrite function list, it means that the target function called this time is a virtual function, and the target function called this time may be rewritten in the heterogeneous function library, that is, the heterogeneous function library may include a rewritten version of the target function. Therefore, there is no need to determine whether the target function called this time is a heterogeneous function, and the above-mentioned step of querying the heterogeneous function library based on the target function name and the function library address of the client architecture can be directly executed. For example, assuming that the target function name is: JNI_OnLoad, the function names recorded in the rewrite function list include: "JNI_OnLoad" and "JNI_OnUnLoad". Then a heterogeneous function library query can be performed directly to determine whether there is a rewritten JNI_OnLoad function in the heterogeneous function library.

[0040] In an embodiment of the present invention, a heterogeneous function library query is performed only when a function name consistent with the target function name is recorded in the rewrite function list, that is, when the target function is a virtual function. In this way, unnecessary query operations can be avoided, thereby saving processing resources.

[0041] Optionally, in an embodiment of the present invention, the step of performing a heterogeneous function library query based on the target function name and the function library address of the client architecture may specifically include:

[0042] Step 1021: Pass the target function name and the function library address to the binary translator.

[0043] Step 1022: Perform heterogeneous function library query based on the target function name and the function library address through a preset query program running in the binary translator; the preset query program is adapted to the client architecture.

[0044] Step 1023: If the target function is found from the heterogeneous function library, a first query result is generated; otherwise, a second query result is generated.

[0045] Among them, the binary translator can also be a dynamic translator, a JIT compiler, and illustratively, the binary translator can be QEMU. In an embodiment of the present invention, the dynamic translator is used to identify whether there is a rewritten version of the called function in the heterogeneous function library. The virtual machine process can use a thread to call a system call to create a new thread, for example, a new thread is created through the Clone system call, and the new thread can be responsible for running the binary translator, and the new thread can be called a translator thread. The thread that needs to call the target function (i.e., the aforementioned target program thread) can send the target function name and the function library address of the client architecture to the translator thread through the inter-thread communication mechanism. For example, a query request carrying information that needs to be passed to the translator thread is sent, for example, a query request carrying the target function name and the function library address of the client architecture, thereby realizing the transfer of the target function name and the function library address of the client architecture to the binary translator. Among them, the function library address of the client architecture can be recorded in advance by the virtual machine.

[0046] Exemplarily, the virtual machine can record the library address of the function library to which each function belongs in the method object of the function. Accordingly, the function library address of the client architecture can be extracted from the method object of the target function. The translator thread can be in a running state and continuously wait for a query request from the target program thread. After the target program thread sends the query request, it can wait for the translator thread to return the query result. Exemplarily, assuming that the target function is named "JNI_OnLoad", the target program thread can extract the function library address of the client architecture from the JNI_OnLoad method object, and the extracted function library address is ". / examplel / library so". Then the target program thread sends the target function name "JNI_OnLoad" and the function library address ". / examplel / library so" to the translator thread through inter-thread communication. The translator thread returns a "true" string or a "false" string representing the query result to the target program thread through inter-thread communication.

[0047] It should be noted that a target program process can also be obtained by creating a process to run the target program, or by creating a process to run the binary translator to obtain the translator process. Accordingly, the target program process can send a query request to the translator process via an inter-process communication mechanism. In the embodiments of the present invention, the query request and query result transmission can be achieved based on the thread / inter-process communication mechanism, with low implementation cost.

[0048] Furthermore, the first query result and the second query result can be default values, and the default values can be pre-set by the developer. Exemplarily, the default value of the first query result can be a first string: "true", and the default value of the second query result can be a second string: "false". Accordingly, if the target function exists in the heterogeneous function library, the first string "true" can be used as the query result, and the translator thread / process can return "false" to the target program thread / process through thread / process communication. If the target function does not exist in the heterogeneous function library, the second string "true" can be used as the query result, and the translator thread / process can return "false" to the target program thread / process through thread / process communication. The target program thread / process only needs to receive the string returned through thread / process communication to obtain the query result, so the target program thread / process can easily obtain the query result.

[0049] Furthermore, the binary translator can execute a preset query program that is set in advance. By executing the preset query program, it is possible to implement the operation of querying the heterogeneous function library according to the target function name and the function library address of the client architecture in response to the query request. Among them, the preset query program can also be called a heterogeneous function query program, and the preset query program is a query function used to implement the query function. The preset query program is adapted to the client architecture, which means that the preset query program is compiled into a binary file of the client architecture. Furthermore, since the preset query program is designed for the client architecture, the preset query program is compiled into a binary file of the client architecture, and the virtual machine processes bytecodes and cannot directly process the binary file of the client architecture. Therefore, the preset query program can be executed by the binary translator to implement the query of the heterogeneous function library.

[0050] Accordingly, when the binary translator is called to execute the preset query program to query the heterogeneous database, the translator thread is equivalent to running in the client architecture environment. In the embodiment of the present invention, the preset query program adapted to the client architecture is executed by calling the binary translator, and the heterogeneous function library is queried and the query results are generated through the preset query program. Since the preset query program is compiled into a binary file of the client architecture, that is, the architecture adapted by the preset query program is the same as the architecture adapted by the function library of the client architecture, the preset query program can be correctly loaded into the heterogeneous function library to obtain the virtual function table included in the heterogeneous database, thereby ensuring the correctness of the query based on the target function name and the function library address of the client architecture.

[0051] It should be noted that the binary translator can adapt to the host architecture. The binary translator itself runs on the hardware of the host architecture and can directly interact with the hardware and operating system of the host architecture. The platform where the binary translator is located is called the host platform, and the host platform is responsible for actually executing the converted instructions. The architecture to which the instructions translated by the binary translator are adapted is the guest architecture. The binary translator is a device that converts instructions of one architecture into instructions of another architecture at runtime, thereby supporting code compatibility between different hardware architectures and optimizing code execution on different platforms. Specifically, in an embodiment of the present invention, the binary translator can be used to translate a preset query program. Accordingly, when executing the preset query program, the binary translator can first dynamically translate the instructions of the guest architecture in the preset query program into instructions of the host architecture. In this way, the preset query program of the guest architecture is run on the platform of the host architecture, and the instructions in the instruction form of the guest architecture in the preset query program are translated into instructions in the instruction form of the host architecture. The translated instructions in the instruction form of the host architecture are executed to execute the preset query program, thereby realizing the query of the heterogeneous function library on the platform of the host architecture.

[0052] Optionally, in the embodiment of the present invention, the target function is a virtual function, and the step of performing a heterogeneous function library query based on the target function name and the function library address may specifically include:

[0053] Step 1022a: Load the heterogeneous function library through the function library address; the heterogeneous function library includes a virtual function table.

[0054] Step 1022b: Compare the target function name with each function name recorded in the virtual function table.

[0055] Step 1022c: If a function name that is consistent with the target function name exists in the virtual function table, it is determined that the target function is found by querying from the heterogeneous function library.

[0056] In an embodiment of the present invention, a preset query program can be dynamically linked to search for a target function by using a library loading function (e.g., dlopen function) and a library search function (e.g., dlsym function) provided by a dynamic linker. Specifically, a translator thread inputs a function library address and a target function name into a preset query program. During execution of the preset query program, the function library address can be used as an input parameter of a dlopen function to load the heterogeneous function library using the dlopen function. The heterogeneous function library includes a virtual function table (VTable), wherein the virtual function table included in the heterogeneous function library is a virtual function table of a subclass, and the virtual function table of a subclass records the function name of the subclass and the method address of the subclass. Wherein, the function name of the subclass refers to the function name of the rewritten virtual function, and the method address of the subclass refers to the function address of the rewritten virtual function. The virtual function table is automatically generated by a compiler. When the compiler compiles a heterogeneous function library, it automatically generates a virtual function table for the virtual functions therein, and the virtual function table is stored in the library file of the compiled heterogeneous function library. Specifically, during the compilation process, if the compiler encounters any virtual function in a heterogeneous library, it triggers the compiler to extract the function name and write it into the virtual function table, thereby automatically generating the virtual function table. The library file of a heterogeneous library is a package of compiled functions and virtual function tables. The virtual function table can be stored in the read-only data segment of the library file, which can be a .so file.

[0057] Correspondingly, the virtual function table included in the same architecture function library is the virtual function table of the parent class, and the virtual function table of the parent class records the function name of the parent class virtual function (i.e. the virtual function before overwriting) and the method address of the parent class virtual function.

[0058] When the function library is loaded through the dlopen function, it will be loaded into the existing virtual function table. The virtual function table stores the offset relative to the function library loading base address. When the function library is loaded into memory, the dynamic linker will relocate the function address of the virtual function recorded in the virtual function table according to the actual loading location of the function library (that is, the function library loading base address). Specifically, the original address recorded in the virtual function table can be updated to the sum of the function library loading base address and the offset of the virtual function, so that the function address in the virtual function table can represent the actual address of the virtual function. Assume that the offset of the virtual function: myFunction is 0x1000, and the function library is loaded to 0x7f000000, then after redirection, the function address of myFunction recorded in the virtual function table is 0x7f001000.

[0059] Then, the target function named dlsym is used as an input parameter, and a function name that matches the target function name is searched from the virtual function table. For any function name recorded in the virtual function table, the dlsym function will compare the target function name with the function name. If the two are consistent, the function address corresponding to the consistent function name is used as the output value. If each function name recorded in the virtual function table is different from the target function name, a preset character is used as the output value. Among them, the preset character can be pre-set by the developer. For example, the preset character can be "NULL". Accordingly, the preset query program can know whether there is a function name consistent with the target function name in the virtual function table based on the output value of the dlsym function, that is, whether there is a rewritten target function in the heterogeneous function library. For example, if the output value is "NULL", it can be determined that there is no function name consistent with the target function name in the virtual function table, and the target function is not queried from the heterogeneous function library, indicating that the target function has not been rewritten. On the contrary, if the output value is not "NULL", it can be determined that there is a function name consistent with the target function name in the virtual function table, and the target function is queried from the heterogeneous function library, indicating that the target function is overwritten.

[0060] In an embodiment of the present invention, a heterogeneous function library is loaded using a function library address; the heterogeneous function library includes a virtual function table. A target function name is compared with each function name recorded in the virtual function table. If a function name matching the target function name exists in the virtual function table, it is determined that the target function has been retrieved from the heterogeneous function library. Thus, by comparing the target function name with the function name recorded in the virtual function table, it is determined whether the heterogeneous function library includes the rewritten target function, thereby ensuring the accuracy of the query results to a certain extent.

[0061] It should be noted that each architecture's function library generates its own virtual function table, and the layout of the virtual function tables of different architectures is often different, that is, the virtual function tables of different architectures are incompatible. Based on the virtual function table in the function library of the same architecture, it is impossible to call the rewritten virtual function, which will lead to the problem of virtual function call error. In the embodiment of the present invention, by directly calling the dynamic translator, using the dynamic translator to translate the preset query program, and then executing the preset query program on the host platform, by executing the preset query program, it is realized to use the library load function and the library search function to query whether the rewritten target function exists, and obtain the function address of the target function in the heterogeneous function library, which can ensure that the virtual function call is performed correctly.

[0062] Furthermore, in an embodiment of the present invention, the binary translator's source code can be pre-compiled into a library file. The binary translator can then be run by calling the main function in the library file, thereby implementing the call to the binary translator. This reduces the coupling between the binary translator and the host environment, making it easier to replace the binary translator. Compared to modifying the binary translator's source code, the code used to access the function library and find function addresses within the function library is extracted from the binary translator, packaged into a public interface, and queried by calling the public interface. This solution does not require modifying the dynamic translator's source code or extracting code, thus reducing the implementation difficulty for developers. For example, assuming the binary translator's source code includes n lines of code, if a query is performed by calling the public interface, the developer must interpret these n lines of code line by line to identify and extract the corresponding code, and then manually package and generate the public interface. In an embodiment of the present invention, these n lines of code can be directly compiled into a library file by executing the library compilation command: flag-shared-fPIC-olibtranslator.so translator.cpp. Among them, flag indicates the identifier of the compiler used, shared indicates that the type of library file is a dynamic library, -fPIC indicates the generation of position-independent code, -olibtranslator.so indicates the name of the final generated library file, and translator.cpp indicates the source code file.

[0063] Optionally, in the embodiment of the present invention, the step of calling the target function in the heterogeneous function library or the homogeneous architecture function library of the host platform based on the query result may specifically include:

[0064] Step 1031: When the query result is the first query result, call the target function in the heterogeneous function library through the binary translator.

[0065] Step 1032: When the query result is the second query result, directly call the target function in the same-architecture function library.

[0066] Specifically, if the query result received by the target program thread is the first query result, it can be determined that the heterogeneous function library includes the rewritten target function, and the architecture adapted by the target function is different from the host architecture. Therefore, the target function in the heterogeneous function library can be called through the binary translator. If the query result received by the target program thread is the second query result, it can be determined that the heterogeneous function library does not include the rewritten target function, and there is a target function in the same-architecture function library whose adapted architecture is the same as the host architecture. Specifically, the function address corresponding to the function name consistent with the target function name can be queried from the virtual function table of the same-architecture function library. Then, the target function at the function address is directly executed under the local architecture.

[0067] For example, assuming the target function is the JNI_OnLoad function and the query result is the first query result, that is, the JNI_OnLoad function is overwritten in the heterogeneous function library, then the JNI_OnLoad function in the heterogeneous function library can be executed, that is, the JNI_OnLoad function of the client architecture is executed instead of the JNI_OnLoad function in the same-architecture function library. Correspondingly, if the query result is the second query result, that is, the JNI_OnLoad function is not overwritten in the heterogeneous function library, then the JNI_OnLoad function in the same-architecture function library can be directly executed, that is, the JNI_OnLoad function of the host architecture is executed.

[0068] In an embodiment of the present invention, if the query result is the first query result, a target function in a heterogeneous function library is called through a binary translator. The target function in the heterogeneous function library is adapted to an architecture different from that of the host. If the query result is the second query result, the target function in the same-architecture function library is directly called. The target function in the same-architecture function library is adapted to the same architecture as that of the host. This ensures that even if the target function is rewritten, that is, the heterogeneous function library includes the rewritten target function, the rewritten target function can be accurately called and executed.

[0069] Optionally, in the embodiment of the present invention, the step of calling the target function in the heterogeneous function library through the binary translator may specifically include:

[0070] Step 1031a: Use the binary translator to search the virtual function table for the function address corresponding to the target function name to obtain the function address of the target function.

[0071] Step 1031b: Execute the target function based on the function address of the target function through the binary translator.

[0072] In an embodiment of the present invention, the target program thread can send an execution request to the translator thread, and the execution request can carry the input parameters of the target function and the target function name. Accordingly, the translator thread can use the target function name as the input parameter of the dlsym function, use the dlsym function to find the function address corresponding to the function name that matches the target function name from the virtual function table, and use the output value of the dlsym function as the function address of the target function. Alternatively, it is also possible to directly obtain the output value of the dlsym function when calling the dlsym function after using the target function name as the input parameter of the dlsym function as the function address of the target function.

[0073] The translator thread can execute a preset execution program to sequentially store input parameters in a designated parameter storage location, then jump to the target function's function address for execution. During execution, the executed target function obtains the input parameters from the parameter storage location and processes the input parameters according to the target function's processing logic. Furthermore, when calling the target function, the preset execution program stores the return address in a return address register. The return address can be the address of the instruction following the call instruction in the preset execution program. The return address register is a pre-defined register, and the function body of the target function includes a return instruction. When the final return instruction is executed, the return address is read from the return address register and then returned to that return address. Therefore, when execution reaches the end of the function, the target function completes execution and returns to the preset execution program. This enables cross-architecture function calls within heterogeneous architectures, namely, jumping back to a pre-recorded return address. Furthermore, the preset execution program can read the value of a return value register to obtain the execution result of the target function. The return value register can be a register used to store the execution result of the function executed by the preset execution program, and the return value register can be pre-defined by the developer. For example, assume that the input parameters include parameter a and parameter b. The processing logic of the target function is to calculate the difference between parameter a and parameter b. The specified parameter storage location is 0x1000, the return value register is R1, and the return address register is R2. Then, during the execution of the target function, parameter a and parameter b can be read from 0x1000. Then, the difference between parameter a and parameter b is calculated, and the difference is written to register R1. When executing the return instruction at the end of the target function, it returns according to the address recorded in register R2. After returning, the preset execution program reads the value of register R1 to obtain the execution result of the target function.

[0074] Furthermore, the translator thread may send the read execution result of the target function to the target program thread, thereby returning the execution result of the target function to the target program, so that the target program thread continues to execute subsequent operations.

[0075] During the execution of the target function, a binary translator can first dynamically translate the client-based instructions in the target function into host-based instructions. The translated instructions are then executed by the host platform's central processing unit (CPU). This ensures that the target function being called is correctly executed, allowing the client-based user program to be run on the host platform while the target function in the client-based function library is executed. This eliminates the need to recompile the client-based function library into host-based code, thereby saving operating costs.

[0076] Furthermore, because the host CPU cannot directly run binary files compiled for the client architecture, in an embodiment of the present invention, a binary translator searches the virtual function table for the function address corresponding to the target function name to obtain the target function's function address. Based on the target function's function address, the binary translator obtains the target function, performs translation, and then executes the target function. This ensures that the target function in the heterogeneous function library is correctly executed.

[0077] Figure 2 This is a schematic diagram of a processing process provided by an embodiment of the present invention. Figure 2 As shown, thread 1 represents a virtual machine thread, and thread 1 can create thread 2 and thread 3. Among them, thread 2 represents the aforementioned translator thread, and thread 3 represents the aforementioned target program thread. During the operation of thread 3, if the rewritten function list records a function name that is consistent with the target function name of the function called this time, that is, the called function may be rewritten by a cross-architecture library, a query request carrying the target function name and the function library address of the client architecture will be sent to thread 2. Accordingly, the translator thread can execute a pre-set preset query program, and query according to the target function name and the function library address of the client architecture through the dlopen function that represents library loading and the dlsym function that represents library search, and generate a query result. Finally, the query result is returned to the target program thread. In this way, the target program thread can easily know whether there is a rewritten version of the target function.

[0078] Figure 3 This is another processing diagram provided by an embodiment of the present invention. Figure 3As shown, if it is detected that the target function name of the target function called this time is recorded in the rewrite function list, the dynamic translator is started to query the heterogeneous function library. For example, in the process of matching the target function name with the function name in the rewrite function list one by one, a function name consistent with the target function name is detected, then it is determined that the target function name is recorded in the rewrite function list, and it is determined that there is a function with the same name in the heterogeneous function library. If there is a function with the same name in the heterogeneous function library, that is, the target function is queried from the heterogeneous function library, it means that the target function has been rewritten, then the dynamic translator is started to execute the rewritten target function in the heterogeneous function library. If there is no function with the same name in the heterogeneous function library, that is, the target function is not queried from the heterogeneous function library, it means that the target function has not been rewritten, then the target function in the same architecture function library is executed under the local architecture, so that the original target function is executed. After that, the subsequent program can be continued.

[0079] Reference Figure 4 , shows a block diagram of a function calling device provided by an embodiment of the present invention, such as Figure 4 As shown, the function calling device may specifically include:

[0080] The acquisition module 201 is configured to use the function name of the target function called by the function call request as the target function name during the running of the target program on the host platform; the client architecture adapted by the target program is different from the architecture of the host platform;

[0081] A query module 202 is configured to query a heterogeneous function library based on the target function name and the function library address of the client architecture to obtain a query result; the query result is used to indicate whether the rewritten target function exists in the heterogeneous function library;

[0082] The calling module 203 is used to call the target function in the heterogeneous function library or the same-architecture function library of the host platform based on the query result, so that the target function is executed.

[0083] In summary, in the function calling device provided by the embodiment of the present invention, during the process of the target program running on the host platform, the function name of the target function called by the function call request is used as the target function name; the client architecture adapted by the target program is different from the architecture of the host platform; a heterogeneous function library query is performed based on the target function name and the function library address of the client architecture to obtain a query result; based on the query result, the target function in the heterogeneous function library or the same-architecture function library of the host platform is called to execute the target function. In this way, by performing a heterogeneous function library query through a binary translator, it is possible to accurately know whether the target function called this time has a rewritten version, that is, whether there is a rewritten target function in the heterogeneous function library, so that based on the query result, the target function can be accurately called from the heterogeneous function library or the same-architecture function library of the host platform, so that function calls between different architectures are directly compatible to ensure that the rewritten function is called smoothly.

[0084] Optionally, the device further comprises:

[0085] A loading module, used for loading a preset rewriting function list; the rewriting function list records the function names of the functions that are supported to be rewritten;

[0086] An execution module is used to execute the step of performing heterogeneous function library query based on the target function name and the function library address of the customer architecture through the query module 202 if a function name consistent with the target function name is recorded in the rewritten function list.

[0087] Optionally, the query module 202 is specifically configured to:

[0088] Passing the target function name and the function library address to a binary translator;

[0089] Performing a heterogeneous function library query based on the target function name and the function library address through a preset query program running in the binary translator; the preset query program is adapted to the client architecture;

[0090] If the target function is found from the heterogeneous function library, a first query result is generated; otherwise, a second query result is generated.

[0091] Optionally, the target function is a virtual function; the query module 202 is specifically configured to:

[0092] Loading the heterogeneous function library through the function library address; the heterogeneous function library includes a virtual function table;

[0093] Compare the target function name with each function name recorded in the virtual function table respectively;

[0094] If there is a function name consistent with the target function name in the virtual function table, it is determined that the target function is queried from the heterogeneous function library.

[0095] Optionally, the calling module 203 is specifically configured to:

[0096] In a case where the query result is the first query result, calling the target function in the heterogeneous function library through the binary translator;

[0097] When the query result is the second query result, the target function in the function library with the same architecture is directly called.

[0098] Optionally, the calling module 203 is further configured to:

[0099] Searching the virtual function table by the binary translator to obtain the target function address corresponding to the target function name;

[0100] The target function is executed by the binary translator based on the target function address.

[0101] Reference Figure 5 Schematic diagram of the structure of the electronic device provided by the embodiment of the present invention. Figure 5 As shown, the electronic device includes: a processor, a memory, a communication interface and a communication bus.

[0102] The processor, the memory, and the communication interface communicate with each other via the communication bus; the memory is used to store at least one executable instruction, which enables the processor to execute the function calling method of the above embodiment. The executable instructions can form a program.

[0103] An embodiment of the present invention provides a machine-readable medium having instructions stored thereon, which, when executed by one or more processors, enables the processors to execute the function calling method of the aforementioned embodiment.

[0104] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0105] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, apparatus, or computer program products. Thus, embodiments of the present invention may take the form of a fully hardware embodiment, a fully software embodiment, or an embodiment combining software and hardware. Furthermore, embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0106] It should be noted that all actions of acquiring signals, information or data in this application are carried out in compliance with the relevant data protection laws and policies of the country where they are located and with the authorization given by the owner of the corresponding device.

[0107] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the process in the flowchart and / or block diagram. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0108] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing terminal device to operate in a predictable manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.

[0109] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0110] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they become aware of the basic creative concepts. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.

[0111] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are merely used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations.

[0112] Moreover, the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or terminal device that includes a list of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or terminal device. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not preclude the presence of additional identical elements in the process, method, article, or terminal device that includes the element.

[0113] The above is a detailed introduction to a function calling method, a function calling device, an electronic device and one or more readable media provided by the present invention. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the ideas of the present invention, there will be changes in the specific implementation methods and application scopes. In summary, the content of this specification should not be understood as limiting the present invention.

Claims

1. A function calling method, characterized in that: The method comprises: During the running of the target program on the host platform, the function name of the target function called by the function call request is used as the target function name; the client architecture adapted by the target program is different from the architecture of the host platform; Performing a heterogeneous function library query based on the target function name and the function library address of the client architecture to obtain a query result; the query result is used to indicate whether the rewritten target function exists in the heterogeneous function library; Based on the query result, the target function in the heterogeneous function library or the same-architecture function library of the host platform is called to execute the target function.

2. The method according to claim 1, characterized in that The method further comprises: Loading a preset rewriting function list; the rewriting function list records the function names of the functions that support the rewriting; If a function name consistent with the target function name is recorded in the rewritten function list, the step of performing heterogeneous function library query based on the target function name and the function library address of the client architecture is executed.

3. The method according to claim 1 or 2, characterized in that The performing heterogeneous function library query based on the target function name and the function library address of the client architecture includes: Passing the target function name and the function library address to a binary translator; Performing heterogeneous function library query based on the target function name and the function library address through a preset query program running in the binary translator; the preset query program is adapted to the client architecture; If the target function is found from the heterogeneous function library, a first query result is generated; otherwise, a second query result is generated.

4. The method according to claim 3, characterized in that The target function is a virtual function; and performing a heterogeneous function library query according to the target function name and the function library address includes: Loading the heterogeneous function library through the function library address; the heterogeneous function library includes a virtual function table; Compare the target function name with each function name recorded in the virtual function table respectively; If there is a function name consistent with the target function name in the virtual function table, it is determined that the target function is queried from the heterogeneous function library.

5. The method according to claim 3, characterized in that The calling of the target function in the heterogeneous function library or the same-architecture function library of the host platform includes: In a case where the query result is the first query result, calling the target function in the heterogeneous function library through the binary translator; When the query result is the second query result, the target function in the function library with the same architecture is directly called.

6. The method according to claim 5, characterized in that The calling of the target function in the heterogeneous function library by the binary translator comprises: Searching the virtual function table by the binary translator to obtain the target function address corresponding to the target function name; The target function is executed by the binary translator based on the target function address.

7. A function calling device, characterized in that: The device comprises: an acquisition module, configured to use, during the execution of a target program on a host platform, a function name of a target function called by a function call request as a target function name; wherein the client architecture adapted by the target program is different from the architecture of the host platform; A query module, configured to query a heterogeneous function library based on the target function name and the function library address of the client architecture, and obtain a query result; the query result is used to indicate whether the rewritten target function exists in the heterogeneous function library; A calling module is used to call the target function in the heterogeneous function library or the same-architecture function library of the host platform based on the query result, so that the target function is executed.

8. The device according to claim 7, characterized in that The device further comprises: A loading module, used for loading a preset rewriting function list; the rewriting function list records the function names of the functions that are supported to be rewritten; An execution module is used to execute the step of querying a heterogeneous function library based on the target function name and the function library address of the client architecture if a function name consistent with the target function name is recorded in the rewritten function list.

9. An electronic device, characterized in that: include: A processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other via the communication bus; The memory is used to store executable instructions, and the executable instructions enable the processor to execute the method according to any one of claims 1 to 6.

10. A machine-readable medium, characterized in that Instructions are stored thereon, which, when executed by one or more processors, cause the processors to perform the method according to any one of claims 1 to 6.