Java Inverse base operand stack

Source: Internet
Author: User

This article references: http://www.vuln.cn/7115

Local variables and operand stacks

Local variable array (Variable array)

An array of local variables includes all the variables required by the method to execute, including the reference to this, all method parameters, and other locally defined variables. For those methods (static method) The parameters are zero-based, and for instance methods, 0 is reserved for this.

All types occupy one slot (entry) in the local variable array, and long and double occupy two consecutive slots because they have double widths (64-bit instead of 32-bit).

operand stack (Operand stack)

The operand stack is used when executing bytecode directives, which are similar to the common registers used in the native CPU. Most JVM bytecode uses the operand stack through pushing,popping,duplicating,swapping, or operations that produce consumption values.


Look at an example of an operation

public class Calc{public static int half (int a) {return A/2;}}

Compile

Javac Calc.java

Anti-compilation

Javap-c-verbose Calc.class

Anti-compilation results

... major version:52...public static int half (int);d escriptor: (I) Iflags:acc_public, acc_staticcode:stack=2, Locals=1, Args_size=10:iload_01:iconst_22:idiv3:ireturnlinenumbertable:line 5:0

ILOAD_0 No. 0 variable (that is, variable a) presses into the operand stack

+-------+|   Stack |+-------+| A |+-------+

Iconst_2 pushes 2 into the operand stack

+-------+|   Stack |+-------+|   2 | | A |+-------+

Idiv the first two int in the operand stack and pushes the result to the top of the operand stack

+-------+| Stack |+-------+| result|+-------+

Ireturn returns the top element of the stack


Example 2, a more complex example, dealing with double-precision values

public class Calc{public static double Half_double (double a) {return a/2.0;}}

Anti-compilation

... major version:52 ... #2 = double 2.0d...public static double half_double (double);d escriptor: (D) dflags:acc_ Public, acc_staticcode:stack=4, locals=2, Args_size=10:dload_01:ldc2_w #2//Double 2.0D4:DDIV5 : Dreturnlinenumbertable:line 5:0

The ldc2_w instruction is loaded from the constant zone 2.0d, in addition, the other three instructions have a D prefix, meaning they use a double data type.


Example 3, two parameters

public class Calc{public static int sum (int a, int b) {return a+b;}}

Anti-compilation

... major version:52...public static int sum (int, int);d escriptor: (II) iflags:acc_public, acc_staticcode:stack=2, locals=2, Args_size=20:iload_01:iload_12:iadd3:ireturnlinenumbertable:line 5:0

ILOAD_0 No. 0 variable (that is, variable a) presses into the operand stack

+-------+|   Stack |+-------+| A |+-------+

Iload_1 1th variable (that is, variable B) presses into the operand stack

+-------+|   Stack |+-------+|   B | | A |+-------+

Iadd the first two int in the operand stack and pushes the result to the top of the operand stack

+-------+| Stack |+-------+| result|+-------+

Ireturn returns the top element of the stack


Example 4, type changed to Long integer

public class Calc{public static long Lsum (long A, long B) {return a+b;}}

Anti-compilation

... major version:52...public static long lsum (long, long);d Escriptor: (JJ) jflags:acc_public, acc_staticcode:stack=4, locals=4, Args_size=20:lload_01:lload_22:ladd3:lreturnlinenumbertable:line 5:0

You can see that the second parameter is pressed into the lload_2, visible lload_0 accounted for two slots (entry)


Example 5, mixed operation

public class Calc{public static int mult_add (int a, int b, int c) {return a*b+c;}}

Anti-compilation

... major version:52...public static int mult_add (int, int, int);d escriptor: (III) iflags:acc_public, Acc_staticcode: stack=2, locals=3, Args_size=30:iload_01:iload_12:imul3:iload_24:iadd5:ireturnlinenumbertable:line 5:0

ILOAD_0 No. 0 variable (that is, variable a) presses into the operand stack

+-------+|   Stack |+-------+| A |+-------+

Iload_1 1th variable (that is, variable B) presses into the operand stack

+-------+|   Stack |+-------+|   B | | A |+-------+

Imul the first two int in the operand stack and presses the result into the top of the operand stack

+-------+| Stack |+-------+|result1|+-------+

Iload_2 2nd variable (that is, variable C) presses into the operand stack

+-------+|   Stack |+-------+| C | | result1|+-------+

Iadd the first two int in the operand stack and pushes the result to the top of the operand stack

+-------+| Stack |+-------+|result2|+-------+

Ireturn returns the top element of the stack


Java Inverse base operand stack

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.