Reverse Engineering for Beginners

(avery) #1

CHAPTER 54. JAVA CHAPTER 54. JAVA


We can see that the Java compiler allocates variables inLVAslots in the same order they were declared in the source code.


There are separateistoreinstructions for accessing slots 0, 1, 2 and 3, but not for 4 and larger, so there isistorewith
an additional operand at offset 8 which takes the slot number as an operand. It’s the same withiloadat offset 10.


But isn’t it dubious to allocate another slot for thelimitvariable, which always contains 20 (so it’s a constant in essence),
and reload its value so often? JVMJITcompiler is usually good enough to optimize such things. Manual intervention in
the code is probably not worth it.


54.12switch().


The switch() statement is implemented with thetableswitchinstruction:


public static void f(int a)
{
switch (a)
{
case 0: System.out.println("zero"); break;
case 1: System.out.println("one\n"); break;
case 2: System.out.println("two\n"); break;
case 3: System.out.println("three\n"); break;
case 4: System.out.println("four\n"); break;
default: System.out.println("something unknown\n"); break;
};
}

As simple, as possible:


public static void f(int);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=1, args_size=1
0: iload_0
1: tableswitch { // 0 to 4
0: 36
1: 47
2: 58
3: 69
4: 80
default: 91
}
36: getstatic #2 // Field java/lang/System.out:Ljava/io/⤦
ÇPrintStream;
39: ldc #3 // String zero
41: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang⤦
Ç/String;)V
44: goto 99
47: getstatic #2 // Field java/lang/System.out:Ljava/io/⤦
ÇPrintStream;
50: ldc #5 // String one\n
52: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang⤦
Ç/String;)V
55: goto 99
58: getstatic #2 // Field java/lang/System.out:Ljava/io/⤦
ÇPrintStream;
61: ldc #6 // String two\n
63: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang⤦
Ç/String;)V
66: goto 99
69: getstatic #2 // Field java/lang/System.out:Ljava/io/⤦
ÇPrintStream;
72: ldc #7 // String three\n
74: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang⤦
Ç/String;)V
77: goto 99
80: getstatic #2 // Field java/lang/System.out:Ljava/io/⤦
ÇPrintStream;
83: ldc #8 // String four\n
Free download pdf