An example of a pass-through Java byte-code checksum

Source: Internet
Author: User
Tags modifiers

In general, we write Java source code, with the Java compiler to compile the. class file, you will not encounter a failure to verify the situation, because the normal Java compiler will be careful to the generated code. So, one of the easiest ways to see the failure of validation is to generate an illegal byte code yourself.

Here I use the ObjectWeb asm to generate the byte code. You can download Asm-3.1.jar from the website and make sure it is on classpath when compiling and running the following program.

(I would have liked to have tried the Bitescript library written by Charles O ' Nutter, but laziness came up and bothered to download ...) Next time, next time.

Java code

Import Java.io.FileOutputStream;
Import Org.objectweb.asm.ClassWriter;
Import Org.objectweb.asm.MethodVisitor;

Import Org.objectweb.asm.Opcodes; public class Testasm implements opcodes {public static void main (string[] args) throws Exception {Classwrite
        R cw = new Classwriter (0);
            Cw.visit (v1_5,//class format version acc_public,//class modifiers
            ' Testverification ',//class name fully qualified name NULL,//generic signature
        "Java/lang/object",//Super class fully qualified name new string[] {}//implemented interfaces
        
        );
             Methodvisitor mv = Cw.visitmethod (acc_public,//access modifiers "foo",//Method name "() V",//Method description NULL,//generic signature null//Exceptio
        NS);
        Mv.visitcode (); MV.VISITINSN (FCONST_0);
        MV.VISITVARINSN (Fstore, 1);
        MV.VISITVARINSN (iload, 1);
        MV.VISITVARINSN (istore, 1);
        MV.VISITINSN (return);
        MV.VISITMAXS (1, 2); Mv.visitend (); End Method Cw.visitend ();
        End Class byte[] Clz = Cw.tobytearray ();
        FileOutputStream out = new FileOutputStream ("Testverification.class");
        Out.write (CLZ);
    Out.close (); }
}

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.