Java bytecode injection method, device, electronic device and storage medium
By registering a globally unique class transformer in a Java virtual machine and maintaining the type mapping relationship of injection point, the uncertainty and time-consuming problems caused by multiple bytecode injections in a Java virtual machine are solved, and class transformation is improved Efficiency and application stability.
Patent Information
- Application Number
- CN202211289274.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-20
- Publication Date
- 2024-12-31
- Estimated Expiration
- 2042-10-20
AI Technical Summary
In Java virtual machines, multiple bytecode injections lead to uncertainty in program behavior and the class transformation takes too long, affecting application startup and fault emergency efficiency.
By registering a globally unique class transformer in the Java virtual machine and maintaining the mapping relationship between the injection point type and injection method corresponding to the implementation class of the business module, determine the target injection method to perform bytecode injection, avoiding repeated injection and multiple times Match knitting.
It shortens the time consumed by class transformation, improves matching efficiency, reduces the dependence of business scenarios on bytecode weaving, and enhances the stability and emergency response capabilities of application startup.
Smart Images

Figure CN115904611B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a Java bytecode injection method, device, electronic device and storage medium. Background Art
[0002] In Java, the code that can be understood by the JVM (Java Virtual Machine) is called bytecode. Bytecode is not targeted at any specific processor, but only at the virtual machine. The Java language solves the problem of low execution efficiency of traditional interpreted languages to a certain extent through bytecode, while retaining the portable characteristics of interpreted languages. Since bytecode is not targeted at a specific machine, Java programs can run on computers with different operating systems without recompiling.
[0003] The Javaagent loaded by the JVM at startup is a new feature introduced by the Java language software development kit, also known as bytecode enhancement. The Javaagent mechanism allows users to intercept the JVM before loading the bytecode, first load the agent file written by themselves, and inject custom code by modifying the bytecode. By using agent technology to build an agent program independent of the application, it can be used to monitor and assist programs running on the JVM, and even dynamically replace and modify the definition of certain classes. For example, Agent technology can be used to monitor the JVM status in real time and locate faults.
[0004] However, when multiple injection agents perform multiple injections on the same class, multiple bytecode modifications are required. Since the modifications are cumulative, multiple modifications will increase unpredictability and easily cause uncertainty in program behavior. Summary of the invention
[0005] In view of this, one or more embodiments of the present specification provide a Java bytecode injection method, device, electronic device and storage medium to solve the problems existing in the related art.
[0006] To achieve the above objectives, one or more embodiments of this specification provide the following technical solutions:
[0007] According to a first aspect of an embodiment of the present specification, a Java bytecode injection method is provided, which is applied to a Javaagent framework deployed in a Java virtual machine, wherein the Javaagent framework is connected to at least one business module with code injection requirements; a globally unique class transformer is registered in the Java virtual machine, and a mapping relationship between an injection point type and an injection mode corresponding to an implementation class included in the at least one business module is also maintained in the Java virtual machine; the method comprises:
[0008] Obtaining a Java bytecode to be injected submitted by any target business module in the at least one business module, and determining a target implementation class corresponding to the Java bytecode to be injected;
[0009] Determine the target injection point type corresponding to the target implementation class, and match the target injection point type with the maintained mapping relationship respectively to determine the target injection mode corresponding to the target injection point type;
[0010] The globally unique class transformer is called, and the class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection mode.
[0011] According to a second aspect of an embodiment of the present specification, a Java bytecode injection device is provided, which is applied to a Javaagent framework deployed in a Java virtual machine, wherein the Javaagent framework is connected to at least one business module with code injection requirements; a globally unique class transformer is registered in the Java virtual machine, and a mapping relationship between an injection point type and an injection method corresponding to an implementation class included in the at least one business module is also maintained in the Java virtual machine; the device comprises:
[0012] An acquisition module is used to acquire the Java bytecode to be injected submitted by any target business module in the at least one business module, and determine a target implementation class corresponding to the Java bytecode to be injected;
[0013] A matching module determines a target injection point type corresponding to the target implementation class, and matches the target injection point type with the maintained mapping relationship to determine a target injection mode corresponding to the target injection point type;
[0014] The injection module calls the globally unique class transformer, and the class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection mode.
[0015] According to a third aspect of the embodiments of this specification, there is provided an electronic device, including a communication interface, a processor, a memory and a bus, wherein the communication interface, the processor and the memory are interconnected via the bus;
[0016] The memory stores machine-readable instructions, and the processor executes the above method by calling the machine-readable instructions.
[0017] According to a fourth aspect of the embodiments of this specification, a machine-readable storage medium is provided, wherein the machine-readable storage medium stores machine-readable instructions, and the machine-readable instructions implement the above method when called and executed by a processor.
[0018] The technical solutions provided by the embodiments of this specification may have the following beneficial effects:
[0019] Through the above technical scheme, by constructing a globally unique class transformer and at the same time constructing a mapping relationship between the injection point type and the injection method corresponding to the implementation class contained in the business module, when injecting the bytecode submitted by the business module, the injection method can be determined based on the mapping relationship, thereby avoiding the creation of a new class transformer each time the bytecode is injected, and there is no need to match and weave multiple class transformers, which shortens the time consumed for class transformation and improves the matching efficiency during class transformation. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 A schematic diagram of a Javaagent framework provided as an exemplary embodiment of this specification;
[0021] Figure 2 A flowchart of a Java bytecode injection method provided as an exemplary embodiment of this specification;
[0022] Figure 3 A schematic diagram of a Java bytecode injection method provided as an exemplary embodiment of this specification;
[0023] Figure 4 A schematic diagram of stability assurance provided for an exemplary embodiment of this specification;
[0024] Figure 5 A schematic diagram of the structure of an electronic device where a Java bytecode injection device is provided in an exemplary embodiment of this specification;
[0025] Figure 6 A block diagram of a Java bytecode injection device provided by an exemplary embodiment of this specification. DETAILED DESCRIPTION
[0026] 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 one or more embodiments of this specification. Instead, they are merely examples of devices and methods consistent with some aspects of one or more embodiments of this specification as detailed in the appended claims.
[0027] It should be noted that: in other embodiments, the steps of the corresponding method are not necessarily performed in the order shown and described in this specification. In some other embodiments, the steps included in the method may be more or less than those described in this specification. In addition, a single step described in this specification may be decomposed into multiple steps for description in other embodiments; and multiple steps described in this specification may be combined into a single step for description in other embodiments.
[0028] At present, since Javaagent can monitor and analyze all aspects of JVM and even intervene in JVM operation, there are many bytecode injection scenarios for Java applications both in online and offline environments.
[0029] For example, the bytecode injection agent in the offline environment includes coverage collection agent, precision testing agent, basic security agent, etc.; while the bytecode injection agent in the online environment includes technical blue army attack components, several dynamic program behavior analysis components, test regression agent, etc.
[0030] However, the above-mentioned bytecode injection scenarios are not built on a unified standard, and the underlying injection frameworks are not unified. Some are self-developed frameworks directly targeting Java bytecode operation and analysis, and some are open source bytecode frameworks, such as sandbox and byteman. Although these frameworks can provide complete programming software development toolkits, module lifecycle management, injection template capabilities, etc., which can solve business demands that rely on bytecode scenarios to a certain extent, there are still some shortcomings when facing large-scale use in production environments.
[0031] Java supports setting multiple class transformers (i.e. ClassFileTransformer) during the class loading phase. Class transformers can implement bytecode injection before the class is loaded into the JVM. For each injection point, a new class transformer is created to implement class transformation. Since these multiple class transformers use a chain structure to achieve the purpose of reweaving the class, chain matching weaving is required when transforming the class. By executing the class transformers one by one to determine whether to transform, even the same injection point will be woven multiple times.
[0032] When the injection point is for a superclass or interface, structural analysis is usually required for class transformation. Therefore, when it is used on a large scale in a production environment, the analysis of multiple injection points will cause the bytecode modification to take too long, which will prolong the startup phase of the application process, affect the startup of the application, and affect the emergency response time when a failure occurs.
[0033] In view of this, the present specification discloses a technical solution that, based on the target implementation class corresponding to the bytecode submitted by the target business module, first determines the target injection point type and then determines the corresponding target injection method by matching the mapping relationship, so that a globally unique class transformer performs injection according to the target injection method, thereby breaking the above-mentioned repeated injection situation, reducing the business scenario's dependence on the bytecode weaving capability, and reducing the time consumption during class transformation.
[0034] During implementation, the Java bytecode to be injected submitted by any target business module in at least one business module with code injection requirement connected to the Javaagent framework can be obtained, and the target implementation class corresponding to the Java bytecode to be injected can be determined.
[0035] For example, by calling the SPI interface, at least one business module can be obtained, and the obtained business module can be loaded into the Javaagent framework, thereby completing the access of at least one business module; then, the Java bytecode to be injected submitted by any target business module in the accessed business modules can be obtained, thereby determining the target implementation class corresponding to the Java bytecode to be injected.
[0036] Then, the target injection point type corresponding to the target implementation class can be determined, and the target injection point type can be matched with the mapping relationship between the injection point type and the injection method corresponding to the implementation class contained in at least one business module maintained in the Java virtual machine to determine the target injection method corresponding to the target injection point type.
[0037] For example, the target implementation class can be structurally parsed, and the target injection point type corresponding to the target implementation class can be determined based on the parsing result, and the target injection point type can be matched with the mapping relationship between the injection point type and the injection method corresponding to the implementation class contained in at least one business module maintained in the Java virtual machine, thereby determining the target injection method corresponding to the target injection point type.
[0038] Afterwards, a globally unique class transformer registered in the Java virtual machine may be called, and the class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection method.
[0039] For example, a globally unique class transformer registered in the Java virtual machine can be called, and the class transformer follows the target injection method. Assuming that the target injection method is a precise injection of code for the implementation class based on the class name information corresponding to the implementation class, it can be determined based on the class name information of the target implementation class whether the class file corresponding to the target business module to be loaded by the Java virtual machine is a class file adapted to the target implementation class; if so, the Java bytecode is injected into the class file.
[0040] Through the above technical scheme, by constructing a globally unique class transformer and at the same time constructing a mapping relationship between the injection point type and the injection method corresponding to the implementation class contained in the business module, when injecting the bytecode submitted by the business module, the injection method can be determined based on the mapping relationship, thereby avoiding the creation of a new class transformer each time the bytecode is injected, and there is no need to match and weave multiple class transformers, which shortens the time consumed for class transformation and improves the matching efficiency during class transformation.
[0041] The Javaagent framework of this specification is described in detail below with reference to the accompanying drawings.
[0042] See also Figure 1 , Figure 1 The following is a schematic diagram of a Javaagent framework provided as an exemplary embodiment of this specification. Figure 1 As shown, the Javaagent framework can include four parts: programming API, business module life cycle management, injection core and stability assurance.
[0043] The above-mentioned programming API (Application Programming Interface) is a set of predefined functions, which aims to provide applications and developers with the ability to access a set of routines based on certain software or hardware. Based on the programming API, the Javaagent framework can provide a set of standard basic capabilities for different business modules that need to be accessed, such as injection capability, configuration change capability, monitoring capability, and task management capability.
[0044] The above-mentioned business module lifecycle management can discover the business modules that need to be connected through SPI (Service Provider Interface), and also defines a set of business module lifecycle management methods including loading, activating and unloading.
[0045] The injection core mentioned above consists of injection templates, process control, class transformation implementation, and injection point storage. The injection template can be used to determine the timing of injection, such as injection before method execution, injection after method execution returns results, or injection when an exception occurs during method execution. Process control can be used to determine the process control statements during the injection process, such as null processing, immediate return, and throwing exceptions. Class transformation implementation can be used to manage the globally unique class transformer and the constructed matcher group. The injection point storage can be used to store injection points, record injection events, and perform event management.
[0046] The above stability assurance includes two parts: risk monitoring and service degradation. Through risk monitoring, data of various monitoring indicators can be collected and reported in the form of logs, and then the logs can be used to determine whether service degradation is required. When service degradation is required, the service degradation part can perform the corresponding degradation process and notify the operation and maintenance personnel to conduct abnormality investigation.
[0047] It is worth noting that the above Javaagent is essentially a tool for implementing the code injection function. This tool can be in the form of a jar package, and the tool can be deployed in the JVM by loading the jar package into a specified process in the JVM.
[0048] Combine the following Figure 1 and Figure 2 The application of this specification to the above Figure 1 The Java bytecode injection method of the Javaagent framework is described in detail.
[0049] See also Figure 2 , Figure 2The flowchart of a Java bytecode injection method provided by an exemplary embodiment of this specification is applied to the Javaagent framework deployed in the Java virtual machine. Figure 2 As shown, the method includes the following execution steps:
[0050] Step 201, obtaining a Java bytecode to be injected submitted by any target business module in the at least one business module, and determining a target implementation class corresponding to the Java bytecode to be injected;
[0051] Step 202, determining the target injection point type corresponding to the target implementation class, and matching the target injection point type with the maintained mapping relationship respectively to determine the target injection mode corresponding to the target injection point type;
[0052] Step 203: calling the globally unique class transformer, and having the class transformer inject the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection method.
[0053] See also Figure 3 , Figure 3 The schematic diagram of a Java bytecode injection method provided by an exemplary embodiment of this specification is applied to a Javaagent framework deployed in a Java virtual machine. Figure 3 As shown, the Javaagent framework is connected to at least one business module with code injection requirements; a globally unique class transformer is registered in the Java virtual machine, and the Java virtual machine also maintains a mapping relationship between the injection point type and the injection method corresponding to the implementation class included in the at least one business module.
[0054] In one embodiment shown, the Javaagent framework defines an SPI interface for the at least one business module;
[0055] Furthermore, the at least one business module may be acquired by calling the SPI interface, and the acquired at least one business module may be loaded into the Javaagent framework to complete the access of the at least one business module.
[0056] For example, Figure 1 For example, by calling the SPI interface, the business module that needs to be accessed can be found, thereby obtaining at least one business module, and the obtained at least one business module is loaded into the Javaagent framework to complete the access of at least one business module.
[0057] In this embodiment, the Java bytecode to be injected submitted by any target business module in the at least one business module may be obtained, and a target implementation class corresponding to the Java bytecode to be injected may be determined.
[0058] For example, Figure 3 For example, after the loading of S301 is completed, for at least one business module connected to the Javaagent framework, the bytecode to be injected submitted by any target business module can be obtained, and the target implementation class corresponding to the Java bytecode to be injected can be determined.
[0059] In addition, the bytecode to be injected may itself be a target implementation class.
[0060] In this embodiment, the target injection point type corresponding to the target implementation class may be determined, and the target injection point type may be matched with the maintained mapping relationship to determine the target injection mode corresponding to the target injection point type.
[0061] It should be noted that the class transformer needs to replace the corresponding class in the class file corresponding to the target business module based on the target implementation class. Therefore, in order to realize the injection of the target implementation class, the target injection method needs to be determined. As can be seen from the above, the method of injection based on multiple class transformers takes too long, so in this manual, the target injection method can be determined through the mapping relationship maintained by the Java virtual machine summary to shorten the time consumed for class transformation.
[0062] For example, Figure 3 For example, after determining the target implementation class corresponding to the Java bytecode to be injected, the target injection point type corresponding to the target implementation class can be determined, and the target injection point type is matched with the maintained mapping relationship to determine the target injection method corresponding to the target injection point type.
[0063] It is worth noting that the above mapping relationship can exist in the form of a set.
[0064] In one embodiment shown, a mapping relationship set corresponding to the at least one business module is maintained in the Java virtual machine; wherein the mapping relationship set includes the mapping relationships between the injection point type and the injection method corresponding to each implementation class contained in the at least one business module.
[0065] Furthermore, in response to any business module accessing the Javaagent framework, structural analysis can be performed on the implementation classes contained in the business module respectively, and the injection point type corresponding to the implementation class can be determined based on the result of the structural analysis, and the injection method corresponding to the implementation class can be determined based on the injection point type; a mapping relationship between the injection point type corresponding to the implementation class and the injection method can be established, and the established mapping relationship can be saved in a mapping relationship set corresponding to the business module.
[0066] For example, in Figure 3 In the Java virtual machine, not only the mapping relationship between the injection point type and the injection method corresponding to the implementation class included in at least one business module can be maintained, but also a mapping relationship set corresponding to at least one business module can be maintained. The mapping relationship set can include the mapping relationship between the injection point type and the injection method corresponding to each implementation class included in at least one business module.
[0067] In one example, each business module may include multiple implementation classes, each implementation class has a corresponding mapping relationship between the injection point type and the injection method, so that the multiple mapping relationships corresponding to each business module can constitute the above mapping relationship set. In other words, the mapping relationship set can include not only multiple different mapping relationships corresponding to the same business module, but also multiple different mapping relationships corresponding to multiple different business modules.
[0068] Next, Figure 3 Take the following as an example to illustrate the construction process of the above mapping relationship set: Figure 3 As shown, the following steps are included:
[0069] S301, loading a service module.
[0070] S302, activating the service module.
[0071] By loading and activating the business module, the business module can be connected to the Javaagent framework.
[0072] S303, in response to any business module accessing the Javaagent framework, respectively performing structural analysis on the implementation classes included in the business module, and determining the injection point type corresponding to the implementation class based on the result of the structural analysis.
[0073] In one example, after the business module is connected to the Javaagent framework, each implementation class included in the business module may be structurally parsed, thereby determining the injection point type corresponding to each implementation class according to the result of the structural parsing.
[0074] S304: Determine an injection method corresponding to the implementation class based on the injection point type.
[0075] It should be noted that, since the mapping relationship between the injection point type and the injection method has not yet been established, it is necessary to make a judgment based on the injection point type to determine the injection method corresponding to the implementation class.
[0076] In one embodiment shown, the injection point type includes an interface injection type and a class injection type; the injection mode includes precise injection and fuzzy injection;
[0077] Wherein, the fuzzy injection refers to a method of injecting code into all implementation classes included in the interface that inherits the implementation class; the precise injection refers to a method of injecting code into the implementation class based on the class name information corresponding to the implementation class;
[0078] Furthermore, when structural analysis is performed on the implementation classes included in the business module in the above S303, and the injection point type corresponding to the implementation class is determined based on the result of the structural analysis, it can be determined whether the implementation class is a superclass inherited by other interfaces based on the result of the structural analysis; if so, the injection point type corresponding to the implementation class is determined as an interface injection type; if not, the injection point type corresponding to the implementation class is determined as a class injection type.
[0079] For example, based on the structural analysis of the implementation class, the family members of the class, such as the parent class, interface, etc., can be obtained according to the implementation class to determine whether the implementation class is a superclass inherited by other interfaces.
[0080] In one example, when the implementation class is a superclass inherited by other classes, if the other classes are inherited by another interface, then the implementation class can be determined to be a superclass inherited by other interfaces, and the injection point type corresponding to the implementation class can be determined to be the interface injection type.
[0081] In another example, when the implementation class has no parent class, it can be determined that the implementation class is not a superclass inherited by other interfaces, and the injection point type corresponding to the implementation class can be determined as the class injection type.
[0082] Furthermore, when determining the injection method corresponding to the implementation class based on the injection point type in the above S304, if the injection point type is an interface injection type, the injection method corresponding to the implementation class is determined as fuzzy injection; and if the injection point type is a class injection type, the injection method corresponding to the implementation class is determined as precise injection.
[0083] For example, after parsing, the corresponding injection method can be determined according to the injection point type. When the injection point type is interface injection type, since it is necessary to inject methods in all implementation classes under the interface, the corresponding injection method is fuzzy injection, and code injection is performed on all implementation classes contained in the interface that inherits the implementation class. When the injection point type is class injection type, since the injected implementation class is clear, the corresponding injection method is precise injection, and code injection is performed based on the class name information corresponding to the implementation class.
[0084] S305: Establish a mapping relationship between the injection point type corresponding to the implementation class and the injection method.
[0085] After determining the injection point type and injection method corresponding to each implementation class in the business module, a mapping relationship between the injection point type and the injection method can be established for each implementation class in each business module.
[0086] For example, after determining that the injection point type corresponding to the implementation class in the business module is the interface injection type and the injection method is fuzzy injection, a mapping relationship corresponding to the implementation class can be established, which is the interface injection type and fuzzy injection.
[0087] S306: Save the established mapping relationship to a mapping relationship set corresponding to the business module.
[0088] Through the above-mentioned preprocessing of the business module, injection point analysis to determine the injection point type, and injection point decision to determine the injection method, a mapping relationship set corresponding to each business module can be constructed. Therefore, when performing class transformation, the injection method can be determined directly according to the mapping relationship, and the class transformer no longer needs to decide how to inject, which shortens the time consumed for class transformation and improves the matching efficiency during class transformation.
[0089] It should be noted that the mapping relationship maintained in the above Java virtual machine can be matched by a Javaagent, or maintained by building a matcher (ie, Matcher) that performs corresponding matching actions.
[0090] In one embodiment shown, a matcher group corresponding to the class transformer is registered in the Java virtual machine; wherein the matcher group includes matchers corresponding to the at least one business module respectively; the matcher corresponding to any business module is used to maintain a mapping relationship set corresponding to the business module, and perform matching actions related to maintaining the mapping relationship set;
[0091] For example, when executing the above S305, a matcher corresponding to the implementation class of the business module can be generated, and the matcher maintains the mapping relationship between the injection point type corresponding to the implementation class and the injection method. When executing the above S306, the generated matcher can be added to the matcher group (i.e., GroupMatcher) corresponding to the class transformer registered in the Java virtual machine, and the matcher group includes multiple matchers.
[0092] Furthermore, when executing the aforementioned step 202, it can be implemented through a matcher by calling a matcher in the matcher group corresponding to the target business module, and the matcher performs a matching action of matching the target injection point type with a mapping relationship in a mapping relationship set maintained by the matcher, so as to determine a target injection method corresponding to the target injection point type.
[0093] For example, the matching action can be performed by calling the matcher corresponding to the target business module in the matcher group registered in the Java virtual machine, and the matcher determines the target injection method that matches the target injection point type based on the mapping relationship set between the injection point type and the injection method maintained by itself.
[0094] In this embodiment, after matching the target injection method corresponding to the target injection point type through the above-mentioned mapping relationship, the globally unique class transformer can be called, and the class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection method.
[0095] For example, by calling a globally unique class transformer, the class transformer can perform injection according to the target injection method, thereby injecting the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine, thereby achieving the purpose of bytecode modification.
[0096] Next, Figure 3 Take the example to illustrate the matching of the above mapping relationship and the bytecode injection process, as follows Figure 3 As shown, the following steps are included:
[0097] S307: Determine the target injection point type corresponding to the target implementation class.
[0098] In one embodiment shown, the target implementation class may be structurally parsed, and the injection point type corresponding to the target implementation class may be determined based on the structural parsing result.
[0099] For example, the target implementation class may be structurally parsed, and then the injection point type corresponding to the target implementation class may be determined based on the result of the structural parsing.
[0100] In one embodiment shown, when determining the injection point type corresponding to the target implementation class based on the structural analysis result in the above S307, it can be determined based on the result of the structural analysis whether the target implementation class is a superclass inherited by other interfaces; if so, the injection point type corresponding to the target implementation class is determined to be an interface injection type; if not, the injection point type corresponding to the target implementation class is determined to be a class injection type.
[0101] For example, based on structural analysis of the target implementation class, family members of the class, such as parent classes, interfaces, etc., can be obtained based on the target implementation class to determine whether the target implementation class is a superclass inherited by other interfaces.
[0102] In one example, when the target implementation class is a superclass inherited by other classes, if the other classes are inherited by another interface, then the target implementation class can be determined to be a superclass inherited by other interfaces, and the injection point type corresponding to the target implementation class can be determined as an interface injection type.
[0103] In another example, when the target implementation class has no parent class, it can be determined that the target implementation class is not a superclass inherited by other interfaces, and the injection point type corresponding to the target implementation class can be determined as a class injection type.
[0104] S308: Match the target injection point type with the maintained mapping relationship respectively to determine a target injection mode corresponding to the target injection point type.
[0105] For example, after determining the target injection point type, each mapping relationship in the mapping relationship set maintained in the matcher corresponding to the target business module in the matcher group can be called to perform matching respectively to determine the target injection mode corresponding to the target injection point type.
[0106] S309: The class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection method.
[0107] In an embodiment shown, if the target injection mode is precise injection, based on the class name information of the target implementation class, it is determined whether the class file corresponding to the target business module to be loaded by the Java virtual machine is a class file adapted to the target implementation class; if so, the Java bytecode is injected into the class file; wherein, precise injection refers to a method of injecting code into the implementation class based on the class name information corresponding to the implementation class;
[0108] And, if the target injection method is fuzzy injection, the class file corresponding to the target business module to be loaded by the Java virtual machine is structurally parsed to obtain at least one implementation class to be loaded contained in the class file; the implementation class contained in the target interface is determined from the at least one implementation class to be loaded, and the Java bytecode is injected into the implementation class contained in the target interface; wherein the target interface is an interface that inherits the target implementation class; the fuzzy injection refers to a method of injecting code into all implementation classes contained in the interface that inherits the implementation class.
[0109] For example, the above-mentioned target injection method may include precise injection and fuzzy injection, wherein the precise injection refers to a method of injecting code into the implementation class based on the class name information corresponding to the implementation class, and the fuzzy injection refers to a method of injecting code into all implementation classes contained in the interface that inherits the implementation class.
[0110] In one example, when the target injection method is precise injection, it can be determined based on the class name information of the target implementation class whether the class file corresponding to the target business module to be loaded by the Java virtual machine is a class file adapted to the target implementation class; if so, the Java bytecode is injected into the class file.
[0111] For example, when the business module defines the method in the target implementation class to be modified, when the injection point type is the class injection type, the corresponding injection method is precise injection. At this time, according to the class name information of the target implementation class, the class file corresponding to the target business module to be loaded by the Java virtual machine can be matched by the class name contained in the class file to determine the class file that is adapted to the target implementation class, and the class with the same class name information in the class file can be modified.
[0112] In another example, when the target injection method is fuzzy injection, the class file corresponding to the target business module to be loaded by the Java virtual machine can be structurally parsed to obtain at least one implementation class to be loaded contained in the class file; the implementation class contained in the target interface is determined from the at least one implementation class to be loaded, and the Java bytecode is injected into the implementation class contained in the target interface; wherein, the target interface is an interface that inherits the target implementation class.
[0113] For example, when the injection point defined by the business module is for the method in the interface of the inherited target implementation class, the injection point type is the interface injection type, and the corresponding injection method is fuzzy injection. At this time, the class file corresponding to the target business module to be loaded by the Java virtual machine can be structurally parsed, the family information of the class in the class file can be obtained, and at least one implementation class to be loaded contained in the class file can be obtained; then, the interface that inherits the target implementation class is determined as the target interface, and the method in the implementation class contained in the target interface is modified.
[0114] It should be noted that the underlying injection framework in the relevant technology lacks stability guarantee and risk management capabilities when used on a large scale in production environments. It is limited to one architecture domain to solve problems in a specific field.
[0115] Therefore, in order to improve monitoring capabilities and reduce stability risks when used on a large scale in production environments, stability assurance can be achieved by building indicator monitoring capabilities based on injection runtime and business degradation processing capabilities in the above-mentioned Javaagent framework.
[0116] In one embodiment shown, monitoring indicator data related to code injection performed by the class transformer on the class file to be loaded may be collected; wherein the monitoring indicator data indicates the service performance of the code injection service provided by the class transformer to the at least one business module;
[0117] And, based on the collected monitoring indicator data, determine whether to perform service downgrade processing on the code injection service; if so, perform service downgrade processing on the code injection service; wherein the service downgrade processing includes performing overall service downgrade processing on the code injection service provided for all business modules; or performing partial service downgrade processing on the code injection service provided for at least one business module.
[0118] Combine the following Figure 4 The application of this specification to the above Figure 1 The stability guarantee of the Javaagent framework is described in detail.
[0119] See also Figure 4 , Figure 4 A schematic diagram of stability assurance provided by an exemplary embodiment of this specification, such as Figure 4 As shown, the following execution steps may be included:
[0120] S401, collecting monitoring index data related to code injection performed by the class transformer on the class file to be loaded, wherein the monitoring index data indicates the service performance of the code injection service provided by the class transformer to the at least one business module.
[0121] In one embodiment shown, the monitoring indicator data includes one or more combinations of the following:
[0122] The time consumption data of the code injection, the system information of the Java virtual machine, and the load condition of the Java virtual machine.
[0123] For example, the monitoring indicator data may be time consumption data during code injection, such as average injection time, time consumption for different bytecode injections, etc.; the system information of the Java virtual machine may be processor and memory usage; the Java virtual machine load condition may be the current load pressure of the virtual machine.
[0124] S402: Determine whether to perform service degradation processing on the code injection service based on the collected monitoring indicator data.
[0125] like Figure 4 As shown, the collected monitoring indicator data can be reported to the monitoring system in the form of logs, and the monitoring system makes a downgrade decision based on the collected monitoring indicator data to determine whether to perform service downgrade processing on the code injection service.
[0126] The above service degradation process may be to stop executing the logic corresponding to the code injection service, or to skip part of the logic, which is not limited in this specification.
[0127] S403: If yes, perform service downgrade processing on the code injection service.
[0128] When performing service degradation processing on the code injection service, the overall service degradation processing may be performed on the code injection service provided for all business modules, or the partial service degradation processing may be performed on the code injection service provided for the at least one business module.
[0129] For example, when performing service downgrade processing on the code injection service, the overall service downgrade processing can be performed on the code injection service provided to all business modules, that is, downgrade within the entire framework; it is also possible to downgrade the service of the code injection service provided by the low-priority business modules first according to the priority of different business modules. If downgrade processing is still required, the code injection service provided by the higher-priority business module will be downgraded.
[0130] Through the above technical scheme, by constructing a globally unique class transformer and at the same time constructing a mapping relationship between the injection point type and the injection method corresponding to the implementation class contained in the business module, when injecting the bytecode submitted by the business module, the injection method can be determined based on the mapping relationship, thereby avoiding the creation of a new class transformer each time the bytecode is injected, and there is no need to match and weave multiple class transformers, which shortens the time consumed for class transformation and improves the matching efficiency during class transformation.
[0131] In an exemplary embodiment of the present specification, a device capable of implementing the above method is also provided.
[0132] Figure 5 is a schematic structural diagram of a device provided by an exemplary embodiment. Figure 5 At the hardware level, the device includes a processor 502, an internal bus 504, a network interface 506, a memory 508, and a non-volatile memory 510, and may also include hardware required for other services. One or more embodiments of this specification may be implemented based on software, such as the processor 502 reading the corresponding computer program from the non-volatile memory 510 into the memory 509 and then running it. Of course, in addition to the software implementation, one or more embodiments of this specification do not exclude other implementations, such as logic devices or a combination of software and hardware, etc., that is, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0133] Please refer to Figure 6 In one software implementation, a Java bytecode injection device 600 is provided, which is applied to a Javaagent framework deployed in a Java virtual machine, such as Figure 6 As shown, the device 600 includes:
[0134] The acquisition module 601 acquires the Java bytecode to be injected submitted by any target business module in the at least one business module, and determines the target implementation class corresponding to the Java bytecode to be injected;
[0135] A matching module 602 determines a target injection point type corresponding to the target implementation class, and matches the target injection point type with the maintained mapping relationship to determine a target injection mode corresponding to the target injection point type;
[0136] The injection module 603 calls the globally unique class transformer, and the class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection mode.
[0137] Optionally, the Javaagent framework defines an SPI interface for the at least one business module;
[0138] Furthermore, the device 600 further includes:
[0139] The loading module 604 (not shown in the figure) acquires the at least one business module by calling the SPI interface, and loads the acquired at least one business module into the Javaagent framework to complete the access of the at least one business module.
[0140] Optionally, a mapping relationship set corresponding to the at least one business module is maintained in the Java virtual machine; wherein the mapping relationship set includes a mapping relationship between an injection point type and an injection method corresponding to each implementation class included in the at least one business module;
[0141] Furthermore, the device 600 further includes:
[0142] The parsing module 605 (not shown in the figure) performs structural parsing on the implementation classes included in the business module in response to any business module accessing the Javaagent framework, and determines the injection point type corresponding to the implementation class based on the result of the structural parsing, and determines the injection method corresponding to the implementation class based on the injection point type;
[0143] The mapping module 606 (not shown in the figure) establishes a mapping relationship between the injection point type corresponding to the implementation class and the injection method, and saves the established mapping relationship to a mapping relationship set corresponding to the business module.
[0144] Optionally, a matcher group corresponding to the class transformer is registered in the Java virtual machine; wherein the matcher group includes matchers corresponding to the at least one business module respectively; the matcher corresponding to any business module is used to maintain a mapping relationship set corresponding to the business module, and perform matching actions related to maintaining the mapping relationship set;
[0145] The analysis module 605 further:
[0146] The matcher corresponding to the target business module in the matcher group is called, and the matcher performs a matching action of matching the target injection point type with a mapping relationship in a mapping relationship set maintained by the matcher to determine a target injection method corresponding to the target injection point type.
[0147] Optionally, the injection point type includes an interface injection type and a class injection type; the injection method includes precise injection and fuzzy injection;
[0148] The analysis module 605 further:
[0149] Based on the result of the structure analysis, determine whether the implementation class is a superclass inherited by other interfaces;
[0150] If yes, determine the injection point type corresponding to the implementation class as the interface injection type;
[0151] If not, determining the injection point type corresponding to the implementation class as the class injection type;
[0152] And, the parsing module 605 further:
[0153] If the injection point type is an interface injection type, the injection method corresponding to the implementation class is determined to be fuzzy injection; wherein the fuzzy injection refers to a method of injecting code into all implementation classes included in the interface that inherits the implementation class;
[0154] If the injection point type is a class injection type, the injection method corresponding to the implementation class is determined as precise injection; wherein the precise injection refers to a method of performing code injection on the implementation class based on the class name information corresponding to the implementation class.
[0155] Optionally, the acquisition module 601 further:
[0156] The target implementation class is structurally parsed, and an injection point type corresponding to the target implementation class is determined based on the structural parsing result.
[0157] Optionally, the acquisition module 601 further:
[0158] Based on the result of the structure analysis, determine whether the target implementation class is a superclass inherited by other interfaces;
[0159] If yes, determining the injection point type corresponding to the target implementation class as an interface injection type;
[0160] If not, the injection point type corresponding to the target implementation class is determined as the class injection type.
[0161] Optionally, the injection module 603 further:
[0162] If the target injection mode is precise injection, based on the class name information of the target implementation class, determine whether the class file corresponding to the target business module to be loaded by the Java virtual machine is a class file adapted to the target implementation class; if so, inject the Java bytecode into the class file; wherein, precise injection refers to a method of injecting code into the implementation class based on the class name information corresponding to the implementation class;
[0163] If the target injection method is fuzzy injection, the class file corresponding to the target business module to be loaded by the Java virtual machine is structurally parsed to obtain at least one implementation class to be loaded contained in the class file; the implementation class included in the target interface is determined from the at least one implementation class to be loaded, and the Java bytecode is injected into the implementation class included in the target interface; wherein the target interface is an interface that inherits the target implementation class; the fuzzy injection refers to a method of injecting code into all implementation classes contained in the interface that inherits the implementation class.
[0164] Optionally, the device 600 further includes:
[0165] A collection module 607 (not shown in the figure) collects monitoring indicator data related to code injection performed by the class transformer on the class file to be loaded; wherein the monitoring indicator data indicates the service performance of the code injection service provided by the class transformer to the at least one business module;
[0166] A downgrade module 608 (not shown in the figure) determines whether to perform service downgrade processing on the code injection service based on the collected monitoring indicator data; if so, performs service downgrade processing on the code injection service;
[0167] The service degradation processing includes performing overall service degradation processing on the code injection service provided for all business modules; or performing partial service degradation processing on the code injection service provided for at least one business module.
[0168] Optionally, the monitoring indicator data includes one or more combinations of the following:
[0169] The time consumption data of the code injection, the system information of the Java virtual machine, and the load condition of the Java virtual machine.
[0170] The implementation process of the functions and effects of each module in the above-mentioned device 600 is specifically detailed in the implementation process of the corresponding steps in the above-mentioned Java bytecode injection method. For relevant parts, please refer to the partial description of the method implementation method, which will not be repeated here.
[0171] The device implementation described above is merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network modules. Some or all of the units or modules may be selected according to actual needs to achieve the purpose of the scheme of this specification. Ordinary technicians in this field can understand and implement it without paying creative work.
[0172] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer, which may be in the form of a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver, a game console, a tablet computer, a wearable device or a combination of any of these devices.
[0173] In a typical configuration, a computer includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0174] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0175] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be used to store information by any method or technology. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include temporary computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0176] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0177] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0178] The terms used in one or more embodiments of this specification are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of this specification. The singular forms of "a", "said" and "the" used in one or more embodiments of this specification and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. 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.
[0179] It should be understood that although the terms first, second, third, etc. may be used to describe various information in one or more embodiments of this specification, these 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 one or more embodiments of this specification, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. 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".
[0180] The above description is merely a preferred embodiment of one or more embodiments of the present specification and is not intended to limit one or more embodiments of the present specification. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of the present specification shall be included in the scope of protection of one or more embodiments of the present specification.
Claims
1. A Java bytecode injection method, applied to a Javaagent framework deployed in a Java virtual machine, wherein the Javaagent framework is connected to at least one business module with code injection requirements; a globally unique class transformer is registered in the Java virtual machine, and a mapping relationship between an injection point type and an injection mode corresponding to an implementation class included in the at least one business module is also maintained in the Java virtual machine; the method comprises: Obtaining a Java bytecode to be injected submitted by any target business module in the at least one business module, and determining a target implementation class corresponding to the Java bytecode to be injected; Determine the target injection point type corresponding to the target implementation class, and match the target injection point type with the maintained mapping relationship respectively to determine the target injection mode corresponding to the target injection point type; The globally unique class transformer is called, and the class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection mode.
2. According to the method of claim 1, the Javaagent framework defines an SPI interface for the at least one business module; The method further comprises: The at least one business module is acquired by calling the SPI interface, and the at least one business module acquired is loaded into the Javaagent framework to complete the access of the at least one business module.
3. The method according to claim 1, wherein the Java virtual machine maintains a set of mapping relationships corresponding to the at least one business module; wherein: The mapping relationship set includes mapping relationships between injection point types and injection methods corresponding to respective implementation classes included in the at least one business module; The method further comprises: In response to any business module accessing the Javaagent framework, respectively performing structural analysis on the implementation classes included in the business module, and determining the injection point type corresponding to the implementation class based on the result of the structural analysis, and determining the injection method corresponding to the implementation class based on the injection point type; A mapping relationship between the injection point type corresponding to the implementation class and the injection method is established, and the established mapping relationship is saved in a mapping relationship set corresponding to the business module.
4. The method according to claim 3, wherein a matcher group corresponding to the class transformer is registered in the Java virtual machine; wherein: The matcher group includes matchers corresponding to the at least one business module respectively; the matcher corresponding to any business module is used to maintain a mapping relationship set corresponding to the business module and perform matching actions related to maintaining the mapping relationship set; The matching of the target injection point type with the maintained mapping relationship to determine the target injection mode corresponding to the target injection point type includes: The matcher corresponding to the target business module in the matcher group is called, and the matcher performs a matching action of matching the target injection point type with a mapping relationship in a mapping relationship set maintained by the matcher to determine a target injection method corresponding to the target injection point type.
5. According to the method of claim 3, the injection point type includes interface injection type and class injection type; the injection mode includes precise injection and fuzzy injection; The step of determining the injection point type corresponding to the implementation class based on the result of the structure analysis includes: Based on the result of the structure analysis, determine whether the implementation class is a superclass inherited by other interfaces; If yes, determine the injection point type corresponding to the implementation class as the interface injection type; If not, determining the injection point type corresponding to the implementation class as the class injection type; The determining, based on the injection point type, an injection method corresponding to the implementation class includes: If the injection point type is an interface injection type, the injection method corresponding to the implementation class is determined to be fuzzy injection; wherein the fuzzy injection refers to a method of injecting code into all implementation classes included in the interface that inherits the implementation class; If the injection point type is a class injection type, the injection method corresponding to the implementation class is determined as precise injection; wherein the precise injection refers to a method of performing code injection on the implementation class based on the class name information corresponding to the implementation class.
6. According to the method of claim 1, the step of determining the target injection point type corresponding to the target implementation class comprises: The target implementation class is structurally parsed, and an injection point type corresponding to the target implementation class is determined based on the structural parsing result.
7. According to the method of claim 6, the step of determining the injection point type corresponding to the target implementation class based on the structure analysis result comprises: Based on the result of the structure analysis, determine whether the target implementation class is a superclass inherited by other interfaces; If yes, determining the injection point type corresponding to the target implementation class as an interface injection type; If not, the injection point type corresponding to the target implementation class is determined as the class injection type.
8. The method according to claim 6, wherein the step of injecting the Java bytecode into a class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection mode comprises: If the target injection mode is precise injection, based on the class name information of the target implementation class, determine whether the class file corresponding to the target business module to be loaded by the Java virtual machine is a class file adapted to the target implementation class; if so, inject the Java bytecode into the class file; wherein, precise injection refers to a method of injecting code into the implementation class based on the class name information corresponding to the implementation class; If the target injection method is fuzzy injection, the class file corresponding to the target business module to be loaded by the Java virtual machine is structurally parsed to obtain at least one implementation class to be loaded contained in the class file; the implementation class included in the target interface is determined from the at least one implementation class to be loaded, and the Java bytecode is injected into the implementation class included in the target interface; wherein the target interface is an interface that inherits the target implementation class; the fuzzy injection refers to a method of injecting code into all implementation classes contained in the interface that inherits the implementation class.
9. The method according to claim 1, further comprising: Collecting monitoring indicator data related to code injection performed by the class transformer on the class file to be loaded; wherein the monitoring indicator data indicates the service performance of the code injection service provided by the class transformer to the at least one business module; Determine whether to perform service downgrade processing on the code injection service based on the collected monitoring indicator data; if so, perform service downgrade processing on the code injection service; The service degradation processing includes performing overall service degradation processing on the code injection service provided for all business modules; or performing partial service degradation processing on the code injection service provided for at least one business module.
10. According to the method of claim 9, the monitoring indicator data includes one or more combinations of the following: The time consumption data of the code injection, the system information of the Java virtual machine, and the load condition of the Java virtual machine.
11. A Java bytecode injection device, applied to a Javaagent framework deployed in a Java virtual machine, wherein the Javaagent framework is connected to at least one business module with code injection requirements; a globally unique class transformer is registered in the Java virtual machine, and a mapping relationship between an injection point type and an injection method corresponding to an implementation class included in the at least one business module is also maintained in the Java virtual machine; the device comprises: An acquisition module is used to acquire the Java bytecode to be injected submitted by any target business module in the at least one business module, and determine a target implementation class corresponding to the Java bytecode to be injected; A matching module determines a target injection point type corresponding to the target implementation class, and matches the target injection point type with the maintained mapping relationship to determine a target injection mode corresponding to the target injection point type; The injection module calls the globally unique class transformer, and the class transformer injects the Java bytecode into the class file corresponding to the target business module to be loaded by the Java virtual machine according to the target injection mode.
12. An electronic device comprising: processor; a memory for storing processor-executable instructions; The processor implements the method according to any one of claims 1 to 10 by running the executable instructions.
13. A machine-readable storage medium having machine-readable instructions stored thereon, which implement the steps of the method according to any one of claims 1 to 10 when executed by a processor.
