0

recently I am doing an experiment on JVM and bytecode.

I use these code snippets to test.

import java.util.*;

public class Simple {

    private String a = "abcdefghijklmnopaqrstuvwaxyazaaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyaz";

    public int test()
    {
        String bb = "abcdefghijklmnopaqrstuvwaxyazaaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyaz";

        int a = 0;
        int b = a;
        int c = a + b;
        return c;
    }

    public static void main(String[] args) 
    {
        String cc = "abcdefghijklmnopaqrstuvwaxyazaaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyaz";

        Simple simple = new Simple();
        simple.test();

        Scanner input=new Scanner(System.in);
        System.out.println("how much money do you need?");
        double  number=input.nextDouble();
    }
} 

FIrstly I use HotSpot to conduct the experiment. On Windows, I trun off the

-Djava.compiler=NONE

and use HeapMemView to view the HotSpot's heap memory. I can find a sequence of "6162 6364.."(whichs match my private String variant) and find my code snippet's bytecode sequence.

But I cannot find the bytecode sequence of Java Standard library.. like

Java.Lang.Obeject
Java.Lang.Math

What's wrong..? In my understanding, I think I should find their bytecode sequence in the JVM's heap..

Then I use JRocket to do it again.. use

   -Djava.compiler=NONE

to turn of the complier mode... but this time I cannot even find my String variant on the heap....

I am trapped here for two days.. Could anybody can me some help...? I really really appreciate it...

Thank you!

Belizzle
  • 1,295
  • 3
  • 13
  • 28
lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
  • What do you exactly mean with “bytecode sequence”? I don’t understand why you expect to find `java.lang.Math` inside the heap when you run a program that doesn’t use that class. The sequence of your constant string is just a temporary artifact existing temporarily while loading the class. After the `String` instance has been created (which uses a different representation, typically UTF-16 char arrays) that memory might be used for other purposes. – Holger Oct 18 '13 at 15:36
  • Hi Holger, thank you for your reply and I think I might be able to do a ROP to ByteCode by finding the exact bddress of loaded Java Standard Library... – lllllllllllll Oct 18 '13 at 17:20
  • The standard library neither has a fixed form nor is it loaded completely into the JVM, typically. And the bytecode sequences inside the JVM runtime will not be the same as in the class files in the jar. The JVM performs transformations on the byte code to reduce memory consumption (e.g. by sharing constants) and improve performance (e.g. by using additional non-standard JVM-specific opcodes). It’s rather unlikely that you find what you are looking for. – Holger Oct 18 '13 at 17:34
  • @Holger Yes it do have lots of complication......:( Could you please provide me some articles about the "additional non-standard JVM-specific opcodes"...? I greatly appreciate you help... – lllllllllllll Oct 18 '13 at 19:39

1 Answers1

1

I am trapped here for two days.. Could anybody can me some help...? I really really appreciate it...

I would focus on the problem you are trying to solve first. Perhaps you could make it clearer as to why you are doing this in the question.

On Windows, I trun off the -Djava.compiler=NONE

This only changes how the code is compiled to native code. This will not change the heap in any way.

But I cannot find the bytecode sequence of Java Standard library.. like

The byte code and class definitions are not in the heap, they are in the perm gen.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hi Peter, thank you a lot! I know they are in the JVM's "perm gen", but as Hotspot and JRockit are all implemented in C++, so they should in C++'s heap. and I think HeapMemView can view the heap of C++.. – lllllllllllll Oct 18 '13 at 15:18
  • @computereasy In that case, you should be looking for UTF-16 encoded strings. It would be helpful to know why you are doing this because it is unlikely to be the simplest way for doing anything I can think of. – Peter Lawrey Oct 18 '13 at 15:46
  • Thank you for your reply. I am thinking if it is feasible to Return Oriented Programming on Java ByteCode.(For research purpose!!) I try to find certain ByteCode sequence's memory address. – lllllllllllll Oct 18 '13 at 15:56
  • I just try to search the corresponding binary sequence of certain bytecodes on JVM's Heap and that's the only possible way I can come up with.. Could you tell me what do you mean by "UTF-16", does it matter with the binary representation of ByteCode...? Thankyou – lllllllllllll Oct 18 '13 at 15:59
  • @computereasy Byte code is usually stored in JAR files in compressed format. This makes search for it rather difficult. But UTF-16 I mean http://en.wikipedia.org/wiki/UTF-16 This means that you cannot search for plain text without the right encoding. If you want to get into the internals of the JVM, I suggest you obtain the source for OpenJDK and modify it to expose what interests you. IT is much easier to find in the code than the native heap. – Peter Lawrey Oct 18 '13 at 17:38
  • I am sure ByteCode is compressed in JAR but there is no article pointed out the Bytecode in Memory is also compressed.. When I conducting my searching, I just searching for the [opcode](http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings) of certain ByteCode in the hex file HeapMemView exported... Thank you for your advise! – lllllllllllll Oct 18 '13 at 19:35