CN110597515A - Byte code pile inserting method - Google Patents

Byte code pile inserting method Download PDF

Info

Publication number
CN110597515A
CN110597515A CN201910794489.7A CN201910794489A CN110597515A CN 110597515 A CN110597515 A CN 110597515A CN 201910794489 A CN201910794489 A CN 201910794489A CN 110597515 A CN110597515 A CN 110597515A
Authority
CN
China
Prior art keywords
class
file
interface
bytecode
java
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201910794489.7A
Other languages
Chinese (zh)
Inventor
张福军
陆昕
鲍碧波
蒋如超
李祥兵
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Green Man Technology Co Ltd
Original Assignee
Green Man Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Green Man Technology Co Ltd filed Critical Green Man Technology Co Ltd
Priority to CN201910794489.7A priority Critical patent/CN110597515A/en
Publication of CN110597515A publication Critical patent/CN110597515A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/3051Monitoring arrangements for monitoring the configuration of the computing system or of the computing system component, e.g. monitoring the presence of processing resources, peripherals, I/O links, software programs
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Quality & Reliability (AREA)
  • Software Systems (AREA)
  • Stored Programmes (AREA)
  • Devices For Executing Special Programs (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The invention relates to the technical field of byte code pile insertion, in particular to a byte code pile insertion method, which comprises the following steps: (1) generating a ClassVisitor interface through an ASM; (2) generating a toByteArray () function within the bytecode; (3) returning the byte stream of the generated bytecode; (4) the byte stream is written back to the file to generate an adjusted class file, and the byte code modification is carried out according to the specific rule, so that the purpose of monitoring the time consumption of each method is achieved.

Description

Byte code pile inserting method
Technical Field
The invention relates to the technical field of byte code instrumentation, in particular to a byte code instrumentation method.
Background
The common framework of ButterKnife, Dagger, familiar to software developers, also generates code during compilation, simplifying programmer operations. However, when a large amount of repetitive code development is completed, a large amount of working time and labor cost are wasted.
Disclosure of Invention
The invention aims to provide a byte code instrumentation method to solve the problem that effective processing cannot be performed through simple coding when the monitoring method in the prior art reaches hundreds of thousands of levels.
In order to achieve the purpose, the invention provides the following technical scheme: a byte code pile inserting method comprises the following steps:
(1) generating a ClassVisitor interface through an ASM;
(2) generating a toByteArray () function within the bytecode;
(3) returning the byte stream of the generated bytecode;
(4) and writing the byte stream back to the file to generate an adjusted class file.
The class file is a Java class framework file.
The Java class file is a binary stream of 8-bit bytes, with data items stored in order in a class file, with no space between adjacent items.
4. A bytecode instrumentation method according to claim 3, characterized in that: the items of the Java Class files include Magic, Version, Constant Pool, Access _ flag, This Class, Super Class, Interfaces, Fields, Methods, Class attributes.
Magic is used for storing Magic number and version information of a Java class file,
version is used for storing Version information of the Java class file.
Constant Pool is used to store various character strings, class names, method names and interface names, final variables and reference information to external classes,
access flag is used to indicate that the file defines a class-latter interface, and also to designate an Access flag for the class or interface.
This Class is used to point to pointers to string constants representing This Class of fully qualified names,
super Class is used to point to pointers to string constants representing the fully qualified name of the parent Class,
the Interfaces is a pointer array, and pointers of character string constants of all interface names realized by the class or the parent class are stored.
Fields are used to describe in detail the Fields declared in a class or interface,
methods are used to describe in detail the Methods declared in a class or interface,
class attributes are used to store basic information for the attributes defined by the classes or interfaces in the file.
Compared with the prior art, the invention has the beneficial effects that: the existing classes can be modified through the ASM, and needed codes can be directly generated. The enhanced code is hard-coded inside the newly generated class file, with no effort on performance due to reflections, and by instrumentation, each class file is scanned and bytecode modifications are made to specific rules to achieve the time-consuming purpose of monitoring each method.
Drawings
FIG. 1 is a process for rewriting class files according to the present invention;
fig. 2 is an internal structural diagram of a Java class file according to the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Referring to fig. 1, the present invention provides a technical solution: a byte code instrumentation method, characterized by: the method comprises the following steps:
(1) generating a ClassVisitor interface through an ASM;
(2) generating a toByteArray () function within the bytecode;
(3) returning the byte stream of the generated bytecode;
(4) and writing the byte stream back to the file to generate an adjusted class file.
The class file is a Java class framework file.
The Java class file is a binary stream of 8-bit bytes, with data items stored in order in a class file, with no space between adjacent items.
As shown in FIG. 2, the items of the Java Class file include Magic, Version, Constant Pool, Access _ flag, This Class, Super Class, Interfaces, Fields, Methods, and Class attributes.
Magic is used for storing Magic number and version information of a Java class file,
version is used for storing Version information of the Java class file.
Constant Pool is used to store various character strings, class names, method names and interface names, final variables and reference information to external classes,
access flag is used to indicate that the file defines a class-latter interface, and also to designate an Access flag for the class or interface.
This Class is used to point to pointers to string constants representing This Class of fully qualified names,
super Class is used to point to pointers to string constants representing the fully qualified name of the parent Class,
the Interfaces is a pointer array, and pointers of character string constants of all interface names realized by the class or the parent class are stored.
Fields are used to describe in detail the Fields declared in a class or interface,
methods are used to describe in detail the Methods declared in a class or interface,
class attributes are used to store basic information for the attributes defined by the classes or interfaces in the file.
By the technical scheme, the ASM can directly generate the required code by transforming the existing class. The enhanced code is hard-coded inside the newly generated class file, with no effort in performance due to reflections. Meanwhile, the ASM is different from Proxy programming, an interface does not need to be newly defined for enhancing codes, and generated codes can cover the original class or be the subclass of the original class. It is a common Java class rather than proxy class, and can even have its own position in the class framework of the application program to derive its subclasses.
ASM is smaller and faster than other popular Java bytecode manipulation tools. ASM has a function similar to BCEL or SERP and only 33k in size, while the latter are 350k and 150k, respectively. Meanwhile, the load of the same class switching, if ASM is 60%, BCEL requires 700%, and SERP requires 1100% or more.
The Java class file is a binary stream of 8-bit bytes. The data items are stored in the class file in sequence without a space between adjacent items, which makes the class file compact and reduces storage space. Many items with different sizes are contained in the Java class file, and the structure of each item is strictly specified, so that the class file can be successfully analyzed from beginning to end.
As shown in fig. 2, a Java class file can be roughly classified into 10 items:
magic: this item stores the magic number (magic number) and version information of a Java class file. The first 4 bytes of a Java class file are called its magic number. Each correct Java class file is started with 0xCAFEBABE, which ensures that the Java virtual machine can easily distinguish Java files from non-Java files.
Version: the item stores the version information of the Java class file and has important significance for one Java file. As Java technology has been developed, the format of class files is also in constant change. The version information of the class file lets the virtual machine know how to read and process the class file.
Constant Pool: the item stores constants such as various character strings, class names, method names and interface names, final variables, and reference information to external classes in the class. The virtual machine must maintain a constant pool for each class loaded, in which symbolic references of all types, fields and methods used by the corresponding type are stored, so that it plays a core role in the dynamic linking of Java. The size of the constant pool accounts for on average about 60% of the size of the whole class.
Access _ flag: the entry indicates whether a class or an interface is defined in the file (only one class or interface exists in a class file), and also indicates an access mark of the class or the interface, such as public, private, abstract and other information.
This Class: a pointer to a string constant representing the class of fully qualified names.
Super Class: a pointer to a string constant representing the parent fully qualified name.
Interfaces: and the pointer array stores pointers of the character string constants of all the interface names realized by the class or the parent class. The constants pointed to by the above three terms, particularly the first two terms, generally need to be modified when we derive a new class from an existing class using ASM: changing the class name to a subclass name; changing the parent class into the class name before derivation; if necessary, a new implementation interface is added.
Fields: this entry describes in detail the fields declared in a class or interface. It should be noted that the fields list only fields in the class or interface, and does not include fields inherited from the super class and parent interface.
Methods: this item describes in detail the methods declared in a class or interface. Such as the name of the method, the parameter and the return value type, etc. It should be noted that the methods list only stores the methods in the class or the interface, and does not include the methods inherited from the super class and the parent interface. AOP programming using ASM is typically accomplished by adjusting instructions in a Method.
Class attributes: this item holds the basic information of the properties defined by the class or interface in the file.
The ultimate purpose of ASM is to generate a class file that can be loaded normally, so its framework structure provides the customer with a tool class that generates bytecodes, ClassWriter. The method realizes a ClassVisitor interface, and comprises a tobyteArray () function, returns a byte stream of generated byte codes, and writes the byte stream back to a file to produce an adjusted class file. Generally, it is used as the end point of the responsibility chain, and the sequential calling (temporal sequential) of all visit events is finally converted into the position adjustment (spatial sequential) of the byte code.
Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that changes, modifications, substitutions and alterations can be made in these embodiments without departing from the principles and spirit of the invention, the scope of which is defined in the appended claims and their equivalents.

Claims (8)

1. A byte code instrumentation method, characterized by: the method comprises the following steps:
(1) generating a ClassVisitor interface through an ASM;
(2) generating a toByteArray () function within the bytecode;
(3) returning the byte stream of the generated bytecode;
(4) and writing the byte stream back to the file to generate an adjusted class file.
2. The bytecode instrumentation method according to claim 1, characterized in that: the class file is a Java class framework file.
3. The bytecode instrumentation method according to claim 2, characterized in that: the Java class file is a binary stream of 8-bit bytes, with data items stored in order in a class file, with no space between adjacent items.
4. A bytecode instrumentation method according to claim 3, characterized in that: the items of the Java Class files include Magic, Version, Constant Pool, Access _ flag, This Class, Super Class, Interfaces, Fields, Methods, Class attributes.
5. The bytecode instrumentation method according to claim 4, characterized in that: magic is used for storing Magic number and version information of a Java class file,
version is used for storing Version information of the Java class file.
6. The bytecode instrumentation method according to claim 4, characterized in that: constant Pool is used to store various character strings, class names, method names and interface names, final variables and reference information to external classes,
access flag is used to indicate that the file defines a class-latter interface, and also to designate an Access flag for the class or interface.
7. The bytecode instrumentation method according to claim 4, characterized in that: this Class is used to point to pointers to string constants representing This Class of fully qualified names,
super Class is used to point to pointers to string constants representing the fully qualified name of the parent Class,
the Interfaces is a pointer array, and pointers of character string constants of all interface names realized by the class or the parent class are stored.
8. The bytecode instrumentation method according to claim 6, characterized in that: fields are used to describe in detail the Fields declared in a class or interface,
methods are used to describe in detail the Methods declared in a class or interface,
class attributes are used to store basic information for the attributes defined by the classes or interfaces in the file.
CN201910794489.7A 2019-08-27 2019-08-27 Byte code pile inserting method Pending CN110597515A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910794489.7A CN110597515A (en) 2019-08-27 2019-08-27 Byte code pile inserting method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910794489.7A CN110597515A (en) 2019-08-27 2019-08-27 Byte code pile inserting method

Publications (1)

Publication Number Publication Date
CN110597515A true CN110597515A (en) 2019-12-20

Family

ID=68855799

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910794489.7A Pending CN110597515A (en) 2019-08-27 2019-08-27 Byte code pile inserting method

Country Status (1)

Country Link
CN (1) CN110597515A (en)

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111352849A (en) * 2020-03-13 2020-06-30 杭州趣维科技有限公司 Method for solving code-free embedded point of mobile terminal
CN111782526A (en) * 2020-06-30 2020-10-16 北京同邦卓益科技有限公司 Interface testing method and device, electronic equipment and storage medium
CN113051122A (en) * 2019-12-26 2021-06-29 百度在线网络技术(北京)有限公司 Performance data acquisition method, performance data acquisition device, electronic equipment and medium
CN114398102A (en) * 2022-01-18 2022-04-26 杭州米络星科技(集团)有限公司 Application package generation method and device, compiling server and computer readable storage medium

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108536589A (en) * 2018-03-26 2018-09-14 广州小鹏汽车科技有限公司 A kind of application program buries point methods and system
CN109684027A (en) * 2017-10-18 2019-04-26 北京京东尚科信息技术有限公司 The method and apparatus of dynamically track Java Virtual Machine operation

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109684027A (en) * 2017-10-18 2019-04-26 北京京东尚科信息技术有限公司 The method and apparatus of dynamically track Java Virtual Machine operation
CN108536589A (en) * 2018-03-26 2018-09-14 广州小鹏汽车科技有限公司 A kind of application program buries point methods and system

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
布偶O: "ASM字节码增强技术", 《HTTPS://WWW.CNBLOGS.COM/BUOU/P/11272187.HTML》 *

Cited By (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113051122A (en) * 2019-12-26 2021-06-29 百度在线网络技术(北京)有限公司 Performance data acquisition method, performance data acquisition device, electronic equipment and medium
CN113051122B (en) * 2019-12-26 2023-09-15 百度在线网络技术(北京)有限公司 Performance data acquisition method, device, electronic equipment and medium
CN111352849A (en) * 2020-03-13 2020-06-30 杭州趣维科技有限公司 Method for solving code-free embedded point of mobile terminal
CN111352849B (en) * 2020-03-13 2023-05-16 杭州趣维科技有限公司 Mobile terminal code-free embedded point solving method
CN111782526A (en) * 2020-06-30 2020-10-16 北京同邦卓益科技有限公司 Interface testing method and device, electronic equipment and storage medium
CN114398102A (en) * 2022-01-18 2022-04-26 杭州米络星科技(集团)有限公司 Application package generation method and device, compiling server and computer readable storage medium
CN114398102B (en) * 2022-01-18 2023-08-08 杭州米络星科技(集团)有限公司 Application package generation method and device, compiling server and computer readable storage medium

Similar Documents

Publication Publication Date Title
CN110597515A (en) Byte code pile inserting method
US7047524B1 (en) Object oriented ADN and method of converting a non-object oriented computer language to an object oriented computer language
US11733985B2 (en) Accessing a migrated member in an updated type
US10853096B2 (en) Container-based language runtime loading an isolated method
US10922081B2 (en) Conditional branch frame barrier
US10466975B2 (en) Execution of parameterized classes on legacy virtual machines to generate instantiation metadata
US20050071809A1 (en) System and method for serializing objects in a compiled programming language
US8407678B2 (en) Method of array interception using data-flow analysis
Tilevich et al. NRMI: Natural and efficient middleware
US6996813B1 (en) Frameworks for loading and execution of object-based programs
US11507503B1 (en) Write barrier for remembered set maintenance in generational Z garbage collector
CN112114810A (en) Compiling processing method and device
US8793671B2 (en) Interface optimization in a closed system
US11030097B2 (en) Verifying the validity of a transition from a current tail template to a new tail template for a fused object
US6978456B1 (en) Methods and apparatus for numeric constant value inlining in virtual machines
US7024664B2 (en) Symbolic assembly language
US7565646B2 (en) Method for compression of object code interpreted by tree-structured expression factorization
CN117149155A (en) Code verification method, device and equipment of service interface and storage medium
Van Vliet C++ for Financial Applications: Syntax and Algorithms
CN115309405A (en) Code link optimization method
CN112667245A (en) Code processing method and device, electronic equipment and storage medium
CN117369861A (en) Thread management policy configuration method and related device for application program
Plasmeijer et al. Term graph rewriting and mobile expressions in functional languages
JP2006011712A (en) Perpetuation adaptor generating device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20191220