Exceptions to the Java inverse base

Source: Internet
Author: User

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

This article references: "Reverse Engineering for Beginners" Dennis Yurichev

Abnormal

Examples of changes processed by the previous month

List 1incorrectmonthexception.java

public class Incorrectmonthexception extends Exception {private int index;public incorrectmonthexception (int index) { This.index = index;} public int GetIndex () {return index;}}

List 2month2.java

Class Month2 {public static string[] months = {"January", "February", "March", "April", "may", "June", "July", "August", " September "," October "," November "," December "};p ublic static String get_month (int i) throws incorrectmonthexception {if ( I < 0 | | i > One) throw new incorrectmonthexception (i); return months[i];}; public static void Main (string[] args) {try {System.out.println (get_month))} catch (Incorrectmonthexception e) { System.out.println ("Incorrect month Index:" + e.getindex ()); E.printstacktrace ();}};}

Compile

Javac Month2.java

Write only one Month2.java, here is Incorrectmonthexception.java will also automatically compile

Anti-compilation

Javap-c-verbose month2.classjavap-c-verbose incorrectmonthexception.class

Incorrectmonthexception method

 public incorrectmonthexception (int);    descriptor:  (I) V     flags: ACC_PUBLIC    Code:      stack=2,  locals=2, args_size=2         0: aload_0          1: invokespecial  #1                    // method java/lang/ Exception. " <init> ":() v         4: aload_0          5: iload_1         6:  putfield       #2                    // Field index:I          9:&nbSp;return 

Essentially, the Incorrectmonthexception class just constructs the object, and there are accessor methods. The Incorrectmonthexception class is inherited from the exception class, so before the Incorrectmonthexception class is constructed, the parent class exception is constructed, Then pass an integer to the Incorrectmonthexception class as a unique property value

GetIndex method

public int getindex ();      Descriptor: () I flags:acc_public code:stack=1, Locals=1, args_size=1 0:aload_0 1:getfield         #2//Field index:i 4:ireturn

GetIndex () is simply an accessor, referenced to the Incorrectmonthexception class, to the position of the index value 0 of the local variable (the this pointer), obtained with the aload_0 instruction, with the GetField instruction to obtain the object's integer value, Return it with the Ireturn directive.


Another look at the Get_month method of Month2.class

  public static java.lang.string get_month (int)  throws  incorrectmonthexception;    descriptor:  (I) ljava/lang/string;     flags: acc_public, acc_static    code:      stack= 3, locals=1, args_size=1         0: iload_0          1: iflt           10         4: iload_0          5: bipush        11          7: if_icmple     19         10: new            #2                    // class incorrectmonthexception         13: dup        14 : iload_0        15: invokespecial  #3                    // Method  Incorrectmonthexception. " <init> ":(I) v        18: athrow         19: getstatic      #4                    // field months:[ljava/lang/ string;        22: iload_0         23: aaload        24: areturn

Partial instruction interpretation

0:ILOAD_0//No. 0 variable (i.e. variable i) pressed into the operand stack

1:iflt 10//when the stack top int value is less than 0 o'clock jump to offset block 10 (here is a value from the top of the stack to compare)

4:ILOAD_0//No. 0 variable (i.e. variable i) pressed into the operand stack

5:bipush 11//Press 11 into the operand stack

7:if_icmple 19//will be popped from the stack two values to compare, if the second (variable i) is less than or equal to the first (number 11), then jump to the offset shift 19

10:new #2//class Incorrectmonthexception//Create a reference to Incorrectmonthexception object and press it into the top of the stack

13:dup//Copy stack top value pressed into the top of the stack

14:ILOAD_0//No. 0 variable (i.e. variable i) pressed into the operand stack

15:invokespecial #3//Method incorrectmonthexception. " <init> ":(I) V//Call Incorrectmonthexception's" <init> "method, where you need to eject two values from the top of the stack, 1 is a reference to the Incorrectmonthexception object, 2 is the variable i

18:athrow//Throws an exception at the top of the stack

19:getstatic #4//Field months:[ljava/lang/string;//Get a reference to a static member months array

22:ILOAD_0//No. 0 variable (i.e. variable i) pressed into the operand stack

23:aaload//stack top pops up two values, pushes the value of the reference array specified index to the top of the stack, where index i is the first pop-up value, and the value of the array reference months is the second pop-up value, that is months[i], into the top of the stack

24:areturn//return stack top value


The Main method of Month2.class

  public static void main (java.lang.string[]);     descriptor:   ([ljava/lang/string;) v    flags: acc_public, acc_static     Code:      stack=3, locals=2, args_size=1          0: getstatic      #5                    // field java/ Lang/system.out:ljava/io/printstream;         3: bipush         100         5:  invokestatic   #6                    // method get_month: (I) ljava/lang/string;          8: invokevirtual  #7                &NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;METHOD&NBSP;JAVA/IO/PRINTSTREAM.PRINTLN: (ljava/lang/string;) V         11: goto           47        14: astore_1         15: getstatic      #5                    // field java/lang/system.out:ljava/io/ printstream;        18: new             #8                    // class java/lang/StringBuilder         21: dup        22: invokespecial  #9                    // method java/lang/ StringBuilder. " <init> ":() v        25: ldc             #10                   // String incorrect month index:         27: invokevirtual  #11                   // method java/lang/stringbuilder.append: ( ljava/lang/string;) ljava/lang/stringbuilder;        30: aload_1         31: invokevirtual  #12           &nBsp;       // method incorrectmonthexception.getindex: () I         34: invokevirtual  #13                   // method java/lang/ Stringbuilder.append: (I) ljava/lang/stringbuilder;        37:  invokevirtual  #14                   // method java/lang/stringbuilder.tostring: () ljava/lang/string;         40: invokevirtual  #7          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;METHOD&NBSP;JAVA/IO/PRINTSTREAM.PRINTLN: ( ljava/lang/string;) v        43: aload_1         44: invokevirtual  #15                   // method incorrectmonthexception.printstacktrace: () v         47: return      Exception table:          from    to  target type              0    11     14   class incorrectmonthexception

Note Exception table: The text and the following tables data

The first row of data for the exception table represents rows from 0 to 11 If an exception occurs, jumps to the position of Offset block 14, and also indicates that the type of the exception is incorrectmonthexception class

The exception is executed starting at offset block 14, and if there is no data for the exception table, the instruction after the offset block 14 is not executed


Here's an example of how IDA shows the exception table:

The original author used the random.class of his own computer, here with just month2.class instead

. Catch incorrectmonthexception from Met003_begin to Met003_11 using met003_14


Exceptions to the Java inverse base

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.