Java hostswap implementation that can modify the class structure

Source: Internet
Author: User

Some time ago, I did the dynamic and non-intrusive interception work and had a deep understanding of the new classes of "instant loading". I have written two articlesArticleHere.

We have solved the "how to modify" problem, but another problem is "what can be changed ".

A class that uses instrumentation to dynamically redefine can only modify methods, that is, the original methodCodeInsert code to implement the logic we need.

But it cannot be added. Delete methods and fields, that is, modify the class definition structure.

 

Currently, javarebel is the new class tool that can truly "instantly" load the structure that has been modified. However, this tool is charged (free of charge)

Tools, other tools with the same functions have not yet been seen. In fact, this demand is very strong, especially in the development stage, the implementation of a new class, especially the class structure.

We hope to see the effect immediately without restarting. In fact, only javarebel can achieve this, indicating that this technology is still somewhat difficult.

 

After a lot of human suffering (Looking at obfuscation decompilation codes is better than simply looking at bytecode to better understand), I have basically understood the implementation principle of a tool (legend says it was for the purpose of research)

Some work can be forgiven. It is precisely because of this that it may cause unnecessary troubles. Therefore, do not write specific code and only record the principles here ).

Of course, for reading its bytecode from the class, crack is quite simple.

 

First, we can modify the source code of several basic classes in JDK.

Classloader, class, Enum, constructor, field, method, and proxy. There are several other classes, including objectstreamclass, urlclassloader, propertyresourcebundle, and resourcebundle $ control, which need to be modified. However

In principle, we first change the classloader, class, Enum, constructor, field, method, and proxy for the minimum model.

 

Assume that there is a class:

Class {

Int x = 0;

}

To load this class, it must not be loaded by JRE's default classloader, because once the class is exposed in the JVM, you can only modify its method body during redefin.

Modified its structure. So we need to modify the classloader. When loading class A, we use the bytecode generator to generate a $ ver1.class, where

All methods are implemented in a $ ver1, and the class reference of A $ ver1 is reversed.

 

Here we need to modify the basic classes of class, constructor, field, and method. For example, when a $ ver1.class. getname () is

Before return name

Int Index = Name. indexof ("$ Ver ");

If (index! =-1) name = Name. substring (0, index );

 

For Class. forname (string name), we need

Change to name = Name + "$ verx"; // This X can use a Global Map to save the corresponding table of the original class and the current version.

 

There are also constructor, method, field reflection methods that need to be modified, that is, when I load a, I actually dynamically generated a $ ver1.class, so the corresponding methods in these classes

You need to change it to the logic that corresponds to a by finding a $ ver1.

 

 

If we modify:

Class {

Int x = 0;

Int y = 0;

}

 

After the scan finds that the class structure is modified, the crack classloader dynamically generates a $ ver2.class. Of course, the modified methods and fields are generated in a $ ver2.class, in the ing table, the corresponding class of A is changed to a $ ver2. Therefore, the basic classes such as class, constructor, field, method, and proxy access. class, you know to directly access a $ ver2.class, the original a $ ver1.class will give up.

 

In this way, each time a is modified, the underlying layer actually generates a different version of the proxy, and then uses the crack base class classloader, class, Enum, constructor, field, method, proxy can achieve dynamic "loading.

 

The premise is that these basic classes that have been crack must work in boot-classpath prior to the original basic class of JRE.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.