Function processing method and device, equipment and storage medium
By modifying the function pointer at the bottom of the operating system, the intercepted function points to the intercepting function, the problems of complexity and limitations of function interception in the existing technology are solved, and efficient interception of arbitrary functions is achieved.
Patent Information
- Application Number
- CN202411564824.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-05
- Publication Date
- 2025-02-25
AI Technical Summary
The function interception technology in the prior art increases the complexity of the code in the application or function, and has limitations, making it difficult to effectively intercept any function.
The Java layer sends an interception request to the operating system's underlying layer. The operating system's underlying layer modifies the function pointer of the intercepted function to point to the intercept function, thereby achieving interception of any function.
Without increasing the complexity of the original code of the application or intercepted function, the interception of arbitrary functions is implemented, extending the scope and scenarios of function interception.
Smart Images

Figure CN119512909A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a function processing method, apparatus, device and storage medium. Background Art
[0002] Developers can use function interception technology to intercept applications' calls to certain functions.
[0003] In the related art, developers can insert additional logic code into the source code or bytecode of an application or function through the instrumentation method, so as to intercept the function call behavior when the application calls the function.
[0004] However, the instrumentation method increases the complexity of the code in the application or function, so the function interception technology in the related art has certain limitations. Summary of the invention
[0005] The embodiment of the present application provides a function processing method, device, equipment and storage medium. The technical solution provided by the embodiment of the present application is as follows:
[0006] According to one aspect of an embodiment of the present application, a function processing method is provided, the method comprising:
[0007] Sending an interception request through the Java layer, the interception request is used to indicate a first function and a second function; the first function is the intercepted party of the function interception, and the second function is the interceptor of the function interception;
[0008] Receiving the interception request through the bottom layer of the operating system;
[0009] Through the bottom layer of the operating system, the function pointer of the first function is modified to a first function pointer, and the first function pointer points to the second function, so that the second function intercepts a call request to the first function.
[0010] According to an aspect provided by an embodiment of the present application, a function processing device is provided, the device comprising:
[0011] A sending module, used for sending an interception request through a Java layer, wherein the interception request is used for indicating a first function and a second function; the first function is an intercepted party of the function interception, and the second function is an intercepting party of the function interception;
[0012] A receiving module, used for receiving the interception request through the bottom layer of the operating system;
[0013] The modification module is used to modify the function pointer of the first function into a first function pointer through the bottom layer of the operating system, and the first function pointer points to the second function so that the second function intercepts the call request to the first function.
[0014] In some embodiments, the apparatus further comprises:
[0015] A request forwarding module, configured to forward the call request to the second function according to the first function pointer when receiving the call request to the first function through the bottom layer of the operating system;
[0016] An execution module is used to execute the interception logic of the call request through the second function.
[0017] In some embodiments, the interception logic includes one or more of the following logics:
[0018] Monitor function calls, modify function behavior, record function call information, and report abnormal calls.
[0019] In some embodiments, the modification module is used to query the metadata of the first function through the bottom layer of the operating system, and modify the function pointer in the metadata of the first function to the first function pointer; or
[0020] The modification module is used to modify the function pointer of the first function into the first function pointer in a hook manner through the bottom layer of the operating system.
[0021] In some embodiments, the apparatus further comprises:
[0022] An establishing module, used to establish a mapping relationship between the first function and the second function according to the interception request through the bottom layer of the operating system;
[0023] A parameter forwarding module is used to forward the call parameters corresponding to the call request to the second function according to the mapping relationship between the first function and the second function through the bottom layer of the operating system when the call request to the first function is received.
[0024] In some embodiments, the intercept request includes function information of the first function and function information of the second function;
[0025] The establishing module is used to establish a mapping relationship between the function information of the first function and the function information of the second function through the bottom layer of the operating system.
[0026] In some embodiments, the establishment module is used to call a registration function through the bottom layer of the operating system, and write the mapping relationship between the function information of the first function and the function information of the second function into a mapping table in the bottom layer of the operating system.
[0027] In some embodiments, the sending module is used to send the interception request through the second function running in the Java layer;
[0028] The receiving module is used to receive the interception request sent by the second function running in the Java layer through the bottom layer of the operating system.
[0029] In some embodiments, the sending module is used to send the interception request to a specified application programming interface API at the bottom layer of the operating system through the second function running in the Java layer;
[0030] The receiving module is used to receive the interception request sent by the second function to the designated API through the bottom layer of the operating system.
[0031] According to one aspect of an embodiment of the present application, a computer device is provided, the computer device comprising a processor and a memory, the memory storing a computer program, the computer program being loaded and executed by the processor to implement the above-mentioned function processing method.
[0032] According to one aspect of an embodiment of the present application, a computer-readable storage medium is provided, in which a computer program is stored. The computer program is loaded and executed by a processor to implement the above-mentioned function processing method.
[0033] According to one aspect of an embodiment of the present application, a computer program product is provided. The computer program product includes a computer program. The computer program is loaded and executed by a processor to implement the above-mentioned function processing method.
[0034] The technical solution provided by the embodiments of the present application includes at least the following beneficial effects:
[0035] In the application scenario of function interception, the Java layer can send an interception request for a specified function to the underlying operating system; accordingly, the underlying operating system can point the pointer of the specified function to a new function, and the developer can pre-define the interception logic for the first function in the new function; when a call request for the specified function from the application is received, the interception logic corresponding to the new function can be executed; therefore, by modifying the function pointer at the underlying operating system, this solution can intercept any function without increasing the complexity of the original code of the application or the intercepted function, thereby expanding the scope and scenarios of function interception. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0037] Figure 1 It is a schematic diagram of a solution implementation environment provided by an exemplary embodiment of the present application;
[0038] Figure 2 is a flow chart of a function processing method provided by an exemplary embodiment of the present application;
[0039] Figure 3 is a flow chart of a function processing method provided by an exemplary embodiment of the present application;
[0040] Figure 4 is a flow chart of a function interception method provided by an exemplary embodiment of the present application;
[0041] Figure 5 is a flow chart of a function interception method provided by an exemplary embodiment of the present application;
[0042] Figure 6 is a schematic diagram of a function interception method provided by an exemplary embodiment of the present application;
[0043] Figure 7 is a block diagram of a function processing device provided by an exemplary embodiment of the present application;
[0044] Figure 8 It is a structural block diagram of a computer device provided by an exemplary embodiment of the present application.
[0045] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application. DETAILED DESCRIPTION
[0046] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0047] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.
[0048] The terms used in this disclosure are for the purpose of describing specific embodiments only and are not intended to limit the disclosure. The singular forms of "a", "said" and "the" used in this disclosure and the appended claims are also intended to include plural forms unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.
[0049] It should be understood that although the terms first, second, etc. may be used in the present disclosure to describe various information, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of the present disclosure, the first function may also be referred to as the second function, and similarly, the second function may also be referred to as the first function. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0050] First, several terms involved in this application are introduced and explained:
[0051] 1) Function: or method, is a block of code used to perform a specified task of an application. Different function methods can perform different tasks. Optionally, in Android development, functions or methods refer to functions or methods in Java or Kotlin, which are blocks of code that perform specific tasks.
[0052] 2) Function interception: refers to inserting additional logic before and after a function call through a certain mechanism during program execution to control and modify the function call. This technology can be used to add new functions or change the behavior of existing functions without modifying the original code.
[0053] 3) Function pointer: It is a variable that stores the address of a function. The corresponding function can be called through this variable. Function pointers are usually used in callback functions, event processing, polymorphism and other scenarios. The definition of a function pointer is: function return value type (*pointer variable name) (function parameter list).
[0054] Please refer to Figure 1, which shows a schematic diagram of an implementation environment provided by an exemplary embodiment of the present application. The implementation environment can be implemented as a function processing system of a terminal device. Figure 1 As shown, the implementation environment may include: a Java layer 10 and an operating system bottom layer 20.
[0055] The Java layer 10 may include a Java Virtual Machine (JVM), a framework, and an application. The Java Virtual Machine provides a running environment for the application, which is used to interpret or compile Java bytecode (.class files), memory management, class loading, and the like. The framework is a collection of a series of Java class libraries and application programming interfaces (APIs), which can provide common functions such as Web request processing, data access, security control, and the like, to help developers build applications faster. Applications can be written by developers in Java language according to actual needs to implement specific business logic, such as data processing, user interaction, network communication, and the like. Optionally, the application can be an application (APP) installed in a terminal device, or it can be an APP that comes with the system.
[0056] The bottom layer 20 of the operating system may include an operating system kernel, a hardware abstraction layer (HAL) and hardware drivers. Among them, the operating system kernel directly interacts with hardware devices, manages resources such as processes, memory, device drivers, and provides services and support for applications. The hardware abstraction layer is located between the operating system kernel and the hardware devices, providing a set of interfaces for the operating system kernel so that the operating system kernel can interact with different types of hardware in a unified way. The hardware driver is a special software that enables the operating system to communicate with the hardware devices, allowing the operating system to communicate effectively with the hardware devices.
[0057] Exemplarily, in the application scenario of function interception involved in the present application, the Java layer 10 can send an interception request to the operating system bottom layer 20; wherein the interception request is used to indicate a first function and a second function, the first function is the intercepted party of the function interception, and the second function is the interceptor of the function interception; accordingly, the operating system bottom layer 20 can receive the above-mentioned interception request; thereafter, the operating system bottom layer 20 can modify the function pointer of the first function to a first function pointer, and the first function pointer points to the second function, so that the second function intercepts the call request to the first function.
[0058] Please refer to Figure 2, which shows a flow chart of a function processing method provided by an exemplary embodiment of the present application. The execution subject of each step of the method may be a computer device, and the computer device may be any electronic device with data storage and processing capabilities. Optionally, the computer device may be the above-mentioned Figure 1 The terminal equipment in the solution implementation environment shown in the figure. Figure 2 As shown, the function processing method may include steps 210 to 230.
[0059] Step 210: Send an interception request through the Java layer, where the interception request is used to indicate a first function and a second function; the first function is the intercepted party of the function interception, and the second function is the intercepting party of the function interception.
[0060] The first function and the second function may be functions or methods running in the Java layer. The first function and the second function perform different tasks. The second function is used to execute the interception logic of the first function, such as logging, modifying parameters, throwing exceptions, etc.
[0061] Exemplarily, the developer may define the interception logic for the first function in the second function. Alternatively, the developer may define one or more interception logics for the first function in the second function.
[0062] Optionally, the above interception request may indicate one or more first functions and second functions corresponding to the first functions, that is, the Java layer may send an interception request for one function at a time, or may send an interception request for multiple functions at a time.
[0063] Step 220: Receive an interception request through the bottom layer of the operating system.
[0064] In an embodiment of the present application, the underlying operating system can parse the above-mentioned interception request to obtain relevant information of the first function and the second function, such as the method name, signature, and class name of the first function, and the method name, signature, and class name of the second function.
[0065] Optionally, the bottom layer of the operating system may perform verification based on the signature of the first function and the signature of the second function to ensure the legal consistency of the above interception request.
[0066] Step 230: through the bottom layer of the operating system, modify the function pointer of the first function to a first function pointer, the first function pointer points to the second function, so that the second function intercepts the call request to the first function.
[0067] In an embodiment of the present application, the underlying operating system may modify the address of the first function to the address of the second function, so that the second function may execute the interception logic of the first function.
[0068] In a possible implementation, the above-mentioned call request for the first function can be a call request for the first function by the first application running in the Java layer. Among them, the embodiment of the present application does not limit the specific category of the first application, such as the application is any application developed by the development tool, such as a game application, a social application, and a shopping application. Exemplarily, the first application of the client can send a call request for the first function through the API specified in the Java layer, and the specified API will call the first function of the Java layer to process the request and return a response.
[0069] To summarize, in the solution provided by the embodiment of the present application, in the application scenario of function interception, the Java layer can send an interception request for a specified function to the underlying operating system; accordingly, the underlying operating system can point the pointer of the specified function to a new function, and the developer can pre-define the interception logic for the first function in the new function; when the application's call request for the specified function is received, the interception logic corresponding to the new function can be executed; therefore, by modifying the function pointer by the underlying operating system, this solution can implement the interception of any function without increasing the complexity of the original code of the application or the intercepted function, thereby expanding the scope and scenarios of function interception.
[0070] Based on the above application Figure 2 In a possible implementation of the solution in the embodiment shown, please refer to Figure 3 , which shows a flow chart of a function processing method provided by an exemplary embodiment. Figure 3 As shown above Figure 2 The function processing method shown also includes step 240 and step 250 .
[0071] Step 240: When a call request to the first function is received through the bottom layer of the operating system, the call request is forwarded to the second function according to the first function pointer.
[0072] When receiving a call request for the first function, the bottom layer of the operating system may query the function pointer of the first function, thereby querying the modified first function pointer.
[0073] Step 250: Execute the interception logic of the call request through the second function.
[0074] The above interception logic may be logging, modifying parameters, throwing exceptions, etc. Optionally, the second function may select one from a variety of optional interception logics to execute as needed.
[0075] Since the underlying operating system has modified the function pointer of the first function to point to the first function pointer pointing to the second function in step 230, when a call request to the first function is subsequently received, the underlying operating system can forward the call request to the second function, and the second function will execute the corresponding interception logic. This solution shows how to modify the function pointer of the first function and how to handle the call request to the first function, which can ensure the accuracy of the interception logic executed by the second function.
[0076] Optionally, after the second function completes the execution of the interception logic of the call request, the original caller (such as the first application) can restore the call request to the first function, and the first function can execute the corresponding code logic.
[0077] Based on the solutions in the above embodiments of the present application, in a possible implementation, the interception logic includes one or more of the following logics:
[0078] Monitor function calls, modify function behavior, record function call information, and report abnormal calls.
[0079] Among them, the above-mentioned monitoring of function calls means that when a function is called, this event can be detected and related information such as the frequency and duration of the function call can be recorded. Specifically, by recording the frequency and duration of function calls, developers can find performance bottlenecks and optimize the code. Recording function calls can be used for security audits to detect whether there are unauthorized calls or abnormal behaviors. During the debugging process, recording function calls can help developers track problems and understand the execution flow of the program. Recording function call information can generate detailed logs for subsequent analysis and backtracking.
[0080] The above-mentioned modification of function behavior refers to changing the behavior of a function by inserting custom logic when the function is called. For example, without modifying the original code, new functions such as logging and permission checking can be added; exceptions can be captured and handled before and after the original function is called to improve the robustness of the program; time can be recorded before and after the call for performance analysis; and parameter verification can be performed before the call to prevent security issues caused by illegal input.
[0081] The above-mentioned recording of function call information refers to collecting and saving relevant information when a function is called, such as the call time, parameters, return value, etc. This information is usually written into a log file or sent to a log server. Specifically, through log information, problems can be quickly located and solved; the time consumed by function calls can be recorded to help developers optimize performance; function call information can be recorded for security audits and compliance checks; the collected call information can be used for data analysis to discover potential problems and trends.
[0082] The above-mentioned reporting of abnormal calls means that if an exception or error occurs during the function call process, these exceptions can be captured and reported in a timely manner. Capture exceptions and handle them appropriately to prevent program crashes; record exception information to facilitate subsequent analysis and debugging; detect and report abnormal calls to prevent malicious attacks. For example, developers can be notified by email, SMS, etc. to handle problems in a timely manner.
[0083] The embodiments of the present application expand the multiple implementable methods of interception logic and improve the diversity of interception logic, including software development requirements such as monitoring function calls, modifying function behaviors, recording function call information, and reporting abnormal calls. It may also include other methods, which are not limited by the embodiments of the present application.
[0084] Based on the solutions in the above embodiments of the present application, in a possible implementation manner, the above step 230 can be implemented as follows:
[0085] The metadata of the first function is queried through the bottom layer of the operating system, and the function pointer in the metadata of the first function is modified to be the first function pointer.
[0086] In the embodiment of the present application, the bottom layer of the operating system can query the metadata of the first function from the symbol table (Symbol Table) or the export table (Export Table) of the dynamic link library (DLL).
[0087] Among them, the metadata of the first function may include at least one of the following: address, parameter type, return type, etc., and may also include other information, which is not limited in this embodiment of the present application.
[0088] Among them, modifying the function pointer means replacing the original function address with a new function address, so that when the original function is called, the new function is actually called. Exemplarily, the process of modifying the function pointer in the metadata of the first function to the first function pointer can be achieved by modifying the import table of the function or directly modifying the function address.
[0089] In a possible implementation, the above step 230 may be implemented as follows:
[0090] Through the bottom layer of the operating system, the function pointer of the first function is modified to the first function pointer in a hook manner.
[0091] In other words, the underlying operating system can modify function pointers through Hook.
[0092] Exemplarily, the modification of the function pointer is implemented by Hook mode, which may specifically include the following steps:
[0093] Get the Java method information (JMethod) to be hooked through the Java Native Interface (JNI);
[0094] By reversing JMethod, modify JMethod properties (such as forcing it to Native) and quickcode jump address (art_quick_generic_jni_trampoline);
[0095] Register a special Native function for this JMethod through JNI Registernative;
[0096] In this Native function, the args and this parameters of JMethod are dynamically parsed;
[0097] Call the custom callback function through JNI;
[0098] Load the custom DEX file through the class loader, execute the specific custom implementation, and return;
[0099] Parse the Hook function return value and return it.
[0100] The embodiments of the present application provide two implementable methods for modifying a function pointer, which can improve the flexibility of the solution, and developers can choose a suitable pointer modification method according to different scenario requirements.
[0101] Based on the solutions in the above embodiments of the present application, in a possible implementation manner, the above Figure 2 The function processing method shown also includes step 260 and step 270 .
[0102] Step 260: Through the bottom layer of the operating system, a mapping relationship between the first function and the second function is established according to the interception request.
[0103] Optionally, the developer can define the interception logic for one or more first functions in the second function. In other words, multiple different first functions can correspond to the same second function.
[0104] Exemplarily, the bottom layer of the operating system may assign unique identification numbers (Identity Document, ID) to the first function and the second function respectively, and the mapping relationship between the first function and the second function is represented by the corresponding relationship between the IDs.
[0105] Optionally, before establishing the mapping relationship between the first function and the second function, the underlying operating system may perform verification based on the signature of the second function to ensure the legitimacy of the second function.
[0106] Step 270: When a call request to the first function is received through the bottom layer of the operating system, the call parameters corresponding to the call request are forwarded to the second function according to the mapping relationship between the first function and the second function.
[0107] The calling parameter of the first function can be one or more, and a specific operation is performed inside the first function. When the first function is called and there is a parameter request, the first function needs to provide the specific value of the calling parameter to the caller of the first function.
[0108] Among them, the calling parameters of the above-mentioned first function may include but are not limited to one or more of the following data types: integer, floating point number, string, object, and may also include other data types, which are not limited in this embodiment of the present application.
[0109] In an embodiment of the present application, when an interception request is received, the underlying operating system can establish a mapping relationship between the first function and the second function; when the first function is called, the underlying operating system can search for the second function that has a mapping relationship with the first function, and forward the calling parameters of the first function to the second function, so as to accurately implement the passing of the calling parameters.
[0110] Based on the solutions in the above-mentioned embodiments of the present application, in a possible implementation method, the interception request includes function information of the first function and function information of the second function; the above-mentioned step 260 can be implemented as step 260a.
[0111] Step 260a: Establish a mapping relationship between the function information of the first function and the function information of the second function through the bottom layer of the operating system.
[0112] The function information of the first function may include but is not limited to:
[0113] The method name of the first function: refers to the name of the first function, which is the identifier used when calling the first function;
[0114] The signature of the first function refers to the combination of the name of the first function and the parameter types of the first function, which is used to identify and distinguish different first functions;
[0115] The class name of the first function: refers to the name of the class to which the first function belongs, which is used to limit the scope of the first function;
[0116] Priority: refers to the execution order of each first function when multiple first functions are called;
[0117] The function information of the above-mentioned first function may also include other contents, which is not limited in the embodiment of the present application.
[0118] The function information of the second function may include but is not limited to:
[0119] The method name of the second function: refers to the name of the second function, which is the identifier used when calling the second function;
[0120] The signature of the second function refers to the combination of the name of the second function and the parameter types of the second function, which is used to identify and distinguish different second functions;
[0121] The class name of the second function: refers to the name of the class to which the second function belongs, which is used to limit the scope of the second function;
[0122] The interception logic of the first function refers to one or more operations that the second function should perform when the first function is intercepted by the second function;
[0123] The function information of the second function mentioned above may also include other contents, which is not limited in the embodiment of the present application.
[0124] In an embodiment of the present application, when the Java layer sends an interception request of the first function to the bottom layer of the operating system, the function information of the first function and the function information of the second function can be carried in the interception request; accordingly, after receiving the above interception request, the bottom layer of the operating system can parse the above interception request to obtain the function information of the above first function and the function information of the second function; thereafter, the bottom layer of the operating system can establish a mapping relationship between the function information of the first function and the function information of the second function. This solution can simplify the process of calling parameters, and thus improve the processing efficiency of function interception.
[0125] Based on the solutions in the above embodiments of the present application, in a possible implementation manner, the above step 260a can be implemented as follows:
[0126] By calling the registration function at the bottom layer of the operating system, the mapping relationship between the function information of the first function and the function information of the second function is written into a mapping table at the bottom layer of the operating system.
[0127] It should be noted that a registration mechanism is required in the bottom layer of the operating system to associate the first function with the specified second function and map the function information of the first function to the function information of the second function. In the embodiment of the present application, in order to improve the callback efficiency of the second function to the calling parameters of the first function, the bottom layer of the operating system can establish a mapping relationship between the first function and the second function by registering a mapping table.
[0128] The mapping table may be a data structure maintained by the bottom layer of the operating system in the bottom layer framework and used to establish a mapping relationship between the first function and the second function. Exemplarily, the mapping table may be an array; or, the mapping table may be a linked list. Exemplarily, the mapping table may be implemented as Table 1.
[0129] Table 1
[0130] First function ID #001 #002 #003 #004 #005 …… Second function ID #001 #002 #001 #003 #002 ……
[0131] Based on the solutions in the above-mentioned embodiments of the present application, in a possible implementation manner, the above-mentioned step 210 can be implemented as step 210a, and the above-mentioned step 220 can be implemented as step 220a.
[0132] Step 210a: Send an intercept request through the second function running in the Java layer.
[0133] Step 220a: Receive an interception request sent by the second function running in the Java layer through the bottom layer of the operating system.
[0134] That is to say, the above interception request can be sent by the second function running in the Java layer to the bottom layer of the operating system. Accordingly, after receiving the above interception request, the bottom layer of the operating system can parse the above interception request to obtain the function information of the above first function and the function information of the second function; after that, the bottom layer of the operating system can modify the function pointer of the first function and establish a mapping relationship between the first function and the second function. Therefore, this solution can simplify the process of function interception and thus improve the flexibility of function interception.
[0135] Based on the solutions in the above embodiments of the present application, in a possible implementation manner, the above step 210a can be implemented as follows:
[0136] Sending an interception request to a specified application programming interface API at the bottom layer of the operating system through a second function running in the Java layer;
[0137] The above step 220a may be implemented as follows:
[0138] An interception request sent by the second function to the specified API is received through the bottom layer of the operating system.
[0139] The Application Programming Interface (API) is a set of rules and protocols that allow the Java layer to communicate and interact with the underlying operating system. The API defines the calling methods, data formats, request and response types, etc. between the Java layer and the underlying operating system.
[0140] In an embodiment of the present application, in order to simplify the request process of the Java layer and the response efficiency of the underlying operating system, the underlying operating system may provide a unified interface specification to facilitate communication and interaction between the Java layer and the underlying operating system.
[0141] Optionally, the above API may be an HTTP interface. The HTTP interface is an application program interface based on the HTTP protocol, which is a stateless protocol based on a request-response model.
[0142] Optionally, the above API may be an RPC interface. The RPC interface is an application program interface based on the RPC protocol, which is a remote procedure call protocol.
[0143] Please refer to Figure 4 , which shows a flow chart of a function interception method provided by an exemplary embodiment of the present application. Figure 4 As shown, the above function interception method includes the following steps:
[0144] Step 401, the second function running in the Java layer sends an interception request for the first function to a specified API at the bottom layer of the operating system;
[0145] Step 402: the bottom layer of the operating system modifies the function pointer of the first function into a first function pointer, and the first function pointer points to the second function;
[0146] Step 403: the bottom layer of the operating system establishes a mapping relationship between the first function and the second function according to the interception request;
[0147] Step 404: upon receiving a call request for the first function, the bottom layer of the operating system forwards the call request to the second function according to the first function pointer;
[0148] Step 405, the second function executes the interception logic for the call request;
[0149] Step 406: When receiving a call request for the first function, the bottom layer of the operating system forwards the call parameters corresponding to the call request to the second function according to the mapping relationship between the first function and the second function.
[0150] Among them, the execution method of step 401 to step 406 is the same as the execution method of the above-mentioned embodiments of the present application, and will not be repeated here.
[0151] Please refer to Figure 5 , which shows a flowchart of a function interception method provided by an exemplary embodiment of the present application.
[0152] like Figure 5As shown in part (a) of the figure, the second function running in the Java layer 510 can send an interception request for the first function to the specified API of the operating system bottom layer 520, and the interception request includes function information of the first function and function information of the second function; accordingly, the operating system bottom layer 520 receives the interception request sent by the second function to the API. Afterwards, the operating system bottom layer 520 modifies the function pointer of the first function to the first function pointer pointing to the second function according to the interception request, so that the second function intercepts the call request for the first function; in addition, the operating system bottom layer 520 establishes a mapping relationship between the first function and the second function according to the interception request.
[0153] like Figure 5 As shown in part (b) of the figure, when receiving a call request from the first application to the first function, the operating system bottom layer 520 queries the function pointer of the first function, and forwards the call request to the second function according to the first function pointer; then, the second function executes the interception logic for the call request. In addition, when the call request includes a parameter request, the operating system bottom layer 520 queries the mapping relationship of the first function in the mapping table, and forwards the call parameters corresponding to the call request to the second function.
[0154] In related technologies, developers can intercept functions through stubs or dynamic proxies. Among them, stubs are used to scan the code during code compilation and insert a piece of logic into the first line of the function to achieve function interception. Dynamic proxy is a programming technology that allows the creation of object proxies dynamically at runtime without knowing the specific class at compile time. The proxy object can add additional processing logic before and after calling the actual object's method.
[0155] However, the interception solutions in related technologies not only have certain limitations in the interception range, but also rely on already inserted or implemented interfaces to intercept, and also have an impact on memory usage, requiring pre-embedded interface implementation or insertion.
[0156] Based on the solutions shown in the above embodiments, in order to solve the problems of the interception solutions in the related technologies, this application proposes a function interception method, which modifies the function method pointer through the bottom layer of the operating system to point to a second function of the Java layer. When the function is executed, it will become the second function of the Java layer, thereby realizing function interception. Not only does the developer not need to pre-embed the interface implementation or stub logic in advance, but also multiple Java layer functions can be intercepted at the same time, with a wider and deeper interception range.
[0157] Please refer to Figure 6 , which shows a schematic diagram of a function interception method provided by an exemplary embodiment of the present application. Figure 6 As shown, the specific technical solution includes:
[0158] S1, request the interface to obtain the interception information of the first function:
[0159] The bottom layer of the operating system may create an interface, which can be used to receive a request from the Java layer to obtain function information of the first function that needs to be intercepted.
[0160] Exemplarily, the interface may be a Hypertext Transfer Protocol (HTTP) interface; or, the interface may also be some form of Remote Procedure Call (RPC) interface.
[0161] Among them, the Java layer can send an array containing the method name, signature, and class name of the first function to be intercepted through this interface. In addition, this array may also include other information, such as the priority of interception.
[0162] Optionally, the specific implementation steps of the above step S1 include:
[0163] Design an API endpoint to receive requests from the Java layer;
[0164] Parse the request data and extract the function information of the first function that needs to be intercepted;
[0165] Verify the legitimacy of the Java layer request based on the signature to ensure that the data sent by the Java layer meets the expected format and security requirements.
[0166] S2, registration mapping relationship:
[0167] After the underlying operating system receives the function array of the first function to be intercepted, a registration mechanism is needed to associate the first function with the specified second function. This usually involves maintaining a mapping table in the underlying framework of the operating system to map the identifier of the first function (such as method name, signature, class name) to the second function.
[0168] Optionally, the specific implementation steps of the above step S2 include:
[0169] Create a registry for storing the identifier of the first function and the corresponding second function;
[0170] Provide a registration function that allows the Java layer to specify the first function and the corresponding second function to be intercepted;
[0171] During registration, the legitimacy of the second function is checked based on the signature to ensure that it can correctly handle the intercepted function call.
[0172] S3, modify the pointer of the first function:
[0173] This step involves modifying the pointer of the first function so that the first function pointer points to the registered second function. Exemplarily, the bottom layer of the operating system can be implemented by modifying the metadata of the first function or using some form of hook technology.
[0174] Optionally, the specific implementation steps of the above step S3 include:
[0175] Use assembly language or specified API to modify function pointers;
[0176] Ensure that modification of function pointers is safe based on the signature and will not affect other parts of the program;
[0177] Before the first function is executed, control is transferred to the second function.
[0178] S4, call the first function:
[0179] That is to say, the bottom layer of the operating system receives a call request from the caller for the first function.
[0180] S5: Does the pointer of the first function point to the second function?
[0181] If the pointer of the first function does not point to the second function, the logic of the first function is executed.
[0182] If the pointer of the first function points to the second function, execute step S6.
[0183] S6, execute the logic of the second function:
[0184] When the first function that needs to be intercepted is called, since the pointer of the first function has been modified, the control flow will first reach the registered second function.
[0185] Optionally, the specific implementation steps of the above step S6 include:
[0186] In the second function, capture the calling parameters of the first function;
[0187] Execute interception logic as needed, such as logging, modifying parameters, throwing exceptions, etc.
[0188] Among them, the interception logic is defined in the second function. The interception logic defines the operation that should be performed when the first function is intercepted. Optionally, the second function can customize specific interception logic, such as monitoring function calls, modifying function behaviors, recording function call information, etc. In addition, in order to ensure that the interception logic does not introduce new errors or performance issues, developers can protect it at the bottom of the operating system. If an exception occurs in the interception logic of the second function, it will roll back to the original logic execution of the first function, that is, point the pointer of the currently intercepted first function back to the original function logic.
[0189] S7, the second function returns the interception result:
[0190] After the interception logic of the second function is executed, the second function needs to process the return value of the second function and pass it back to the original caller.
[0191] Optionally, the specific implementation steps of the above step S7 include:
[0192] Process the return value of the second function and modify or record the return value as needed;
[0193] Ensure that the return value can be correctly passed to the original caller; specifically, the underlying operating system can perform a function pointer query at the underlying operating system based on the interception function information passed in by the Java layer, and match it based on the passed in function class name, method name, and signature to ensure correctness. Only after a successful match will it be returned to the second function and the return value of the second function is obtained, and then returned to the first function.
[0194] In summary, the function interception scheme in the related art is too complicated and requires pre-embedded interface implementation or plug-in logic, which leads to limitations and makes it impossible to intercept any function. The function interception method of the present application can perform function interception by modifying the pointer of the first function at the bottom layer of the operating system to point to the second function registered at the Java layer.
[0195] That is to say, the function interception method of the present application writes a function interception interface at the bottom layer of the operating system to pass an array of the first function to be intercepted to the Java layer, in which the method name, signature, and class name of the first function to be intercepted are passed in the array, and then the second function to be intercepted is registered. After the bottom layer of the operating system receives the array of the first function to be intercepted, it searches and finds the second function corresponding to the first function, and points the pointer of the first function to the second function registered for interception. In this way, when the first function to be intercepted is executed, it will be executed in the second function that has been registered for interception, and the parameters of the first function will also be passed in. In this way, function interception is realized, and the interception logic can be customized in the second function.
[0196] For example, the function interception method of the present application can be used to provide a logical backup for online applications (Application, APP). For example, if function A crashes when executing logic, then this application can be used to intercept function A to prevent it from crashing; for another example, if function B misses a conditional judgment, resulting in unreasonable logic, then this application can be used to intercept and rewrite function B.
[0197] The following are device embodiments of the present application, which can be used to execute the method embodiments of the present application. For details not disclosed in the device embodiments of the present application, please refer to the method embodiments of the present application.
[0198] Please refer to Figure 7 , which shows a block diagram of a function processing device provided by an exemplary embodiment of the present application. The device has the functions of implementing the above examples, and the functions can be implemented by hardware or by hardware executing corresponding software. Figure 7 As shown, the device may include a sending module 701 , a receiving module 702 and a modifying module 703 .
[0199] The sending module 701 is used to send an interception request through the Java layer, where the interception request is used to indicate a first function and a second function; the first function is the intercepted party of the function interception, and the second function is the intercepting party of the function interception;
[0200] A receiving module 702 is used to receive an interception request through the bottom layer of the operating system;
[0201] The modification module 703 is used to modify the function pointer of the first function into a first function pointer through the bottom layer of the operating system, and the first function pointer points to the second function, so that the second function intercepts the call request to the first function.
[0202] In some embodiments, the apparatus further comprises:
[0203] A request forwarding module, configured to forward the call request to the second function according to the first function pointer when receiving a call request to the first function through the bottom layer of the operating system;
[0204] The execution module is used to execute the interception logic of the call request through the second function.
[0205] In some embodiments, the interception logic includes one or more of the following logic:
[0206] Monitor function calls, modify function behavior, record function call information, and report abnormal calls.
[0207] In some embodiments, the modification module 703 is used to query the metadata of the first function through the bottom layer of the operating system, and modify the function pointer in the metadata of the first function to the first function pointer; or,
[0208] The modification module 703 is used to modify the function pointer of the first function into the first function pointer in a hook manner through the bottom layer of the operating system.
[0209] In some embodiments, the apparatus further comprises:
[0210] Establishing a module, used to establish a mapping relationship between the first function and the second function according to the interception request through the bottom layer of the operating system;
[0211] The parameter forwarding module is used to forward the call parameters corresponding to the call request to the second function according to the mapping relationship between the first function and the second function through the bottom layer of the operating system when a call request to the first function is received.
[0212] In some embodiments, the interception request includes function information of the first function and function information of the second function;
[0213] An establishing module is used to establish a mapping relationship between the function information of the first function and the function information of the second function through the bottom layer of the operating system.
[0214] In some embodiments, a module is established to call a registration function through the bottom layer of the operating system, and write the mapping relationship between the function information of the first function and the function information of the second function into a mapping table in the bottom layer of the operating system.
[0215] In some embodiments, the sending module 701 is used to send an interception request through a second function running in the Java layer;
[0216] The receiving module 702 is used to receive, through the bottom layer of the operating system, an interception request sent by the second function running in the Java layer.
[0217] In some embodiments, the sending module 701 is used to send an interception request to a specified application programming interface API at the bottom layer of the operating system through a second function running in the Java layer;
[0218] The receiving module 702 is used to receive the interception request sent by the second function to the specified API through the bottom layer of the operating system.
[0219] The solution provided by the embodiment of the present application is that, in the application scenario of function interception, the Java layer can send an interception request for a specified function to the underlying operating system; accordingly, the underlying operating system can point the pointer of the specified function to a new function, and the developer can pre-define the interception logic for the first function in the new function; when a call request for the specified function is received from the application, the interception logic corresponding to the new function can be executed; therefore, by modifying the function pointer by the underlying operating system, the present solution can implement the interception of any function without increasing the complexity of the original code of the application or the intercepted function, thereby expanding the scope and scenarios of function interception.
[0220] It should be noted that the device provided in the above embodiment, when implementing its functions, is only illustrated by the division of the above functional modules. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the device and method embodiments provided in the above embodiment belong to the same concept, and the specific implementation process is detailed in the method embodiment, which will not be repeated here.
[0221] Please refer to Figure 8 , which shows a structural block diagram of a computer device 800 provided by an exemplary embodiment of the present application. The computer device 800 may be Figure 1 The terminal device in the implementation environment shown is used to implement the function processing method provided in the above embodiment. Specifically:
[0222] Typically, the computer device 800 includes a processor 810 and a memory 820 .
[0223] The processor 810 may include one or more processing cores, such as a 4-core processor, an 8-core processor, and the like. The processor 810 may be implemented in at least one hardware form of digital signal processing (DSP), field programmable gate array (FPGA), and programmable logic array (PLA). The processor 810 may also include a main processor and a coprocessor. The main processor is a processor for processing data in an awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in a standby state. In some embodiments, the processor 810 may be integrated with a graphics processing unit (GPU), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 810 may also include an AI processor for processing computing operations related to machine learning.
[0224] The memory 820 may include one or more computer-readable storage media, which may be non-transitory. The memory 820 may also include high-speed random access memory, and non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In some embodiments, the non-transitory computer-readable storage medium in the memory 820 is used to store a computer program, which is configured to be executed by one or more processors to implement the above-mentioned function processing method.
[0225] Those skilled in the art will understand that Figure 8 The structure shown in the figure does not constitute a limitation on the computer device 800, and the computer device 800 may include more or less components than those shown in the figure, or combine some components, or adopt a different arrangement of components.
[0226] In an exemplary embodiment, a computer-readable storage medium is also provided, in which a computer program is stored, and the computer program is executed by a processor to implement the above-mentioned function processing method. Optionally, the computer-readable storage medium may include: a read-only memory (ROM), a random access memory (RAM), a solid state drive (SSD) or an optical disk, etc. Among them, the random access memory may include a resistance random access memory (ReRAM) and a dynamic random access memory (DRAM).
[0227] In an exemplary embodiment, a computer program product is also provided, the computer program product comprising a computer program, the computer program being stored in a computer-readable storage medium. A processor of a computer device reads the computer program from the computer-readable storage medium, and the processor executes the computer program, so that the computer device executes the above-mentioned function processing method.
[0228] It should be understood that the "multiple" mentioned in this article refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. In addition, the step numbers described in this article only illustrate a possible execution sequence between the steps. In some other embodiments, the above steps may not be executed in the order of the numbers, such as two steps with different numbers are executed at the same time, or two steps with different numbers are executed in the opposite order to the diagram. The embodiments of the present application are not limited to this.
[0229] The above description is only an optional embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
Claims
1. A function processing method, characterized in that: The method comprises: Sending an interception request through the Java layer, the interception request is used to indicate a first function and a second function; the first function is the intercepted party of the function interception, and the second function is the interceptor of the function interception; Receiving the interception request through the bottom layer of the operating system; Through the bottom layer of the operating system, the function pointer of the first function is modified to a first function pointer, and the first function pointer points to the second function, so that the second function intercepts a call request to the first function.
2. The method according to claim 1, characterized in that The method further comprises: When receiving the call request for the first function, forwarding the call request to the second function according to the first function pointer through the bottom layer of the operating system; The interception logic of the call request is executed through the second function.
3. The method according to claim 2, characterized in that The interception logic includes one or more of the following logics: Monitor function calls, modify function behavior, record function call information, and report abnormal calls.
4. The method according to any one of claims 1 to 3, characterized in that: The step of modifying the function pointer of the first function to a first function pointer through the bottom layer of the operating system includes: querying the metadata of the first function through the bottom layer of the operating system, and modifying the function pointer in the metadata of the first function to the first function pointer; or, Through the bottom layer of the operating system, the function pointer of the first function is modified to the first function pointer in a hook manner.
5. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: By means of the bottom layer of the operating system, a mapping relationship between the first function and the second function is established according to the interception request; Through the bottom layer of the operating system, when the call request for the first function is received, the call parameters corresponding to the call request are forwarded to the second function according to the mapping relationship between the first function and the second function.
6. The method according to claim 5, characterized in that The interception request includes function information of the first function and function information of the second function; The establishing, through the bottom layer of the operating system, a mapping relationship between the first function and the second function according to the interception request includes: A mapping relationship between the function information of the first function and the function information of the second function is established through the bottom layer of the operating system.
7. The method according to claim 6, characterized in that The establishing, through the bottom layer of the operating system, a mapping relationship between the function information of the first function and the function information of the second function comprises: By calling the registration function at the bottom layer of the operating system, the mapping relationship between the function information of the first function and the function information of the second function is written into a mapping table at the bottom layer of the operating system.
8. The method according to any one of claims 1 to 3, characterized in that: The interception request is sent through the Java layer, including: Sending the intercept request through the second function running in the Java layer; The receiving the interception request through the bottom layer of the operating system includes: The interception request sent by the second function running in the Java layer is received through the bottom layer of the operating system.
9. The method according to claim 8, characterized in that The sending of the interception request through the second function running in the Java layer includes: Sending the interception request to a specified application programming interface API at the bottom layer of the operating system through the second function running in the Java layer; The receiving, through the bottom layer of the operating system, the interception request sent by the second function running in the Java layer comprises: The interception request sent by the second function to the designated API is received through the bottom layer of the operating system.
10. A function processing device, characterized in that: The device comprises: A sending module, used for sending an interception request through a Java layer, wherein the interception request is used for indicating a first function and a second function; the first function is an intercepted party of the function interception, and the second function is an intercepting party of the function interception; A receiving module, used for receiving the interception request through the bottom layer of the operating system; The modification module is used to modify the function pointer of the first function into a first function pointer through the bottom layer of the operating system, and the first function pointer points to the second function so that the second function intercepts the call request to the first function.
11. The device according to claim 10, characterized in that The device also includes: A request forwarding module, configured to forward the call request to the second function according to the first function pointer when receiving the call request to the first function through the bottom layer of the operating system; An execution module is used to execute the interception logic of the call request through the second function.
12. The device according to claim 11, characterized in that The interception logic includes one or more of the following logics: Monitor function calls, modify function behavior, record function call information, and report abnormal calls.
13. The device according to any one of claims 10 to 12, characterized in that: The modification module is used to query the metadata of the first function through the bottom layer of the operating system, and modify the function pointer in the metadata of the first function to the first function pointer; or, The modification module is used to modify the function pointer of the first function into the first function pointer in a hook manner through the bottom layer of the operating system.
14. The device according to any one of claims 10 to 12, characterized in that: The device also includes: An establishing module, used to establish a mapping relationship between the first function and the second function according to the interception request through the bottom layer of the operating system; A parameter forwarding module is used to forward the call parameters corresponding to the call request to the second function according to the mapping relationship between the first function and the second function through the bottom layer of the operating system when the call request to the first function is received.
15. The device according to claim 14, characterized in that The interception request includes function information of the first function and function information of the second function; The establishing module is used to establish a mapping relationship between the function information of the first function and the function information of the second function through the bottom layer of the operating system.
16. The device according to claim 15, characterized in that The establishment module is used to call the registration function through the bottom layer of the operating system, and write the mapping relationship between the function information of the first function and the function information of the second function into a mapping table in the bottom layer of the operating system.
17. The device according to claim 16, characterized in that The sending module is used to send the interception request through the second function running in the Java layer; The receiving module is used to receive the interception request sent by the second function running in the Java layer through the bottom layer of the operating system.
18. The device according to claim 17, characterized in that The sending module is used to send the interception request to a specified application programming interface API at the bottom layer of the operating system through the second function running in the Java layer; The receiving module is used to receive the interception request sent by the second function to the designated API through the bottom layer of the operating system.
19. A computer device, characterized in that: The computer device comprises a processor and a memory, wherein a computer program is stored in the memory, and the computer program is loaded and executed by the processor to implement the method according to any one of claims 1 to 9.
20. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and the computer program is used to be executed by a processor to implement the method according to any one of claims 1 to 9.
21. A computer program product, characterized in that The computer program product comprises a computer program, which is loaded and executed by a processor to implement the method according to any one of claims 1 to 9.
Citation Information
Cited By
Task processing method and device, electronic equipment and storage medium
CN121277795A