Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing instructions from Java bytecode

I have been using Javassist to dynamically manipulate classes as they are loaded. While adding code to a method is relatively easy using Javassist, I have not been able to find a way to remove code.

At this time I am simulating the removal of the code by using nop instructions to replace the targeted opcodes and any parameters. However, I consider this to be mostly a hack:

  • Each opcode has to be treated separately, since the byte length of the parameters differs. In some cases I also need to choose between nop and pop, depending on whether the removed opcode affects the stack or not. This kind of manipulation is starting to get tedious - and the code that does it is becoming accordingly convoluted. So, naturally, I am hoping for an existing solution.

  • The final result is filled with nop instructions. While the JVM should optimize those out with no performance impact, the resulting bytecode is still quite inelegant and bigger than it should be. This is more of an issue of aesthetics, but it is still something to consider.

Unfortunately, merely shifting parts of the bytecode array to close the gap is not enough - any references to the moved code (e.g. branch instruction indexes) should be updated as well.

Is it possible to remove instructions using Javassist? Alternatively, is there a bytecode manipulation library that would allow me to do that easily, without having to essentially parse the bytecode myself?

like image 607
thkala Avatar asked Feb 10 '12 12:02

thkala


People also ask

What is bytecode in Java?

Bytecode is the intermediate representation of a Java program, allowing a JVM to translate a program into machine-level assembly instructions. When a Java program is compiled, bytecode is generated in the form of a .class file. This .class file contains non-runnable instructions and relies on a JVM to be interpreted. 3. Using javap

What happens to the bytecode after the first compilation?

After the first compilation, the bytecode generated is now run by the Java Virtual Machine and not the processor in consideration. This essentially means that we only need to have basic java installation on any platforms that we want to run our code on.

How to create/manipulate Java class files using the byte code engineering library?

The Byte Code Engineering Library, popularly known as Apache Commons BCEL, provides a convenient way to create/manipulate Java class files. 5.1. Maven Dependency As usual, let's add the latest bcel Maven dependency to our pom.xml: 5.2. Disassemble Class and View Bytecode Then, we can use the Repository class to generate the JavaClass object:

What is a byte code instruction?

Similar to assembly code, the eventual goal of byte code instructions is loading values from memory locations, perform operations on them, and write the result back to memory locations. In higher languages, we always work with symbols.


1 Answers

Apache BCEL allows you to delete instructions:

Deletion of instructions is also very straightforward; all instruction handles and the contained instructions within a given range are removed from the instruction list and disposed. The delete() method may however throw a TargetLostException when there are instruction targeters still referencing one of the deleted instructions. The user is forced to handle such exceptions in a try-catch clause and redirect these references elsewhere.

You can find an example in the manual as well.

like image 184
Dan Cruz Avatar answered Oct 08 '22 18:10

Dan Cruz