Reverse Engineering for Beginners

(avery) #1

CHAPTER 54. JAVA CHAPTER 54. JAVA


public int my_rand();
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: getstatic #2 // Field rand_state:I
3: getstatic #3 // Field RNG_a:I
6: imul
7: putstatic #2 // Field rand_state:I
10: getstatic #2 // Field rand_state:I
13: getstatic #4 // Field RNG_c:I
16: iadd
17: putstatic #2 // Field rand_state:I
20: getstatic #2 // Field rand_state:I
23: sipush 32767
26: iand
27: ireturn

It just loads all the values from the object’ fields, does the operations and updatesrand_state’s value using theputstatic
instruction. At offset 20,rand_stateis reloaded again (because it was dropped from the stack before, byputstatic).
This looks like non-efficient code, but be sure, theJVMis usually good enough to optimize such things really well.


54.8 Conditional jumps.


Now let’s proceed to conditional jumps.


public class abs
{
public static int abs(int a)
{
if (a<0)
return -a;
return a;
}
}


public static int abs(int);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=1, args_size=1
0: iload_0
1: ifge 7
4: iload_0
5: ineg
6: ireturn
7: iload_0
8: ireturn

ifgejumps to offset 7 if the value atTOSis greater or equal to 0. Don’t forget, anyifXXinstruction pops the value (to be
compared) from the stack.


inegjust negates value atTOS.


Another example:


public static int min (int a, int b)
{
if (a>b)
return b;
return a;
}

We get:


public static int min(int, int);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=2
Free download pdf