Example usage for org.objectweb.asm MethodVisitor visitEnd

List of usage examples for org.objectweb.asm MethodVisitor visitEnd

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitEnd.

Prototype

public void visitEnd() 

Source Link

Document

Visits the end of the method.

Usage

From source file:jtaint.ClassLoaderAdapter.java

License:Apache License

private void addDefineClassWrapper(String methodName, String methodDesc) {
    MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, ByteCodeUtil.internalName("defineClass"), methodDesc, null,
            null);/*w  ww  .  j  av  a 2s. c o m*/
    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0); /* This */
    mv.visitVarInsn(ALOAD, 1); /* String name */
    mv.visitVarInsn(ALOAD, 2); /* Buffer (byte[] or ByteBuffer */
    mv.visitVarInsn(ILOAD, 3); /* offset */
    mv.visitVarInsn(ILOAD, 4); /* len */

    Type[] args = Type.getArgumentTypes(methodDesc);
    Type[] newArgs = new Type[3];
    System.arraycopy(args, 1, newArgs, 0, 3); /* Buffer, offset, len */
    String newDesc = Type.getMethodDescriptor(newArgs[0], newArgs);

    mv.visitMethodInsn(INVOKESTATIC, "jtaint/InstrumentationUtils", "instrument", newDesc);

    if ("Ljava/nio/ByteBuffer;".equals(newArgs[0].getDescriptor())) {
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/nio/ByteBuffer", "position", "()I");
        mv.visitInsn(SWAP);
        mv.visitInsn(DUP_X1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/nio/ByteBuffer", "remaining", "()I");
    } else {
        mv.visitInsn(DUP);
        mv.visitInsn(ARRAYLENGTH);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(SWAP);
    }

    /* Now push all additional arguments */
    int l = 5;
    for (int i = 4; i < args.length; l += args[i].getSize(), i++)
        mv.visitVarInsn(args[i].getOpcode(ILOAD), l);
    mv.visitMethodInsn(INVOKESPECIAL, className, methodName, methodDesc);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(l, l);
    mv.visitEnd();
}

From source file:jtaint.ClassLoaderAdapter.java

License:Apache License

private void addExportWrapper() {
    String desc = "(Ljava/lang/String;)Ljava/lang/Class;";
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, ByteCodeUtil.internalName("findLoadedClass"), desc, null,
            null);/*from w  w  w.jav a 2  s.  co m*/
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, className, "findLoadedClass", desc);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:jtaint.InstrumentationLockBuilder.java

License:Apache License

private static final void buildIncLock(ClassVisitor cv, String className) {
    MethodVisitor mv;

    mv = cv.visitMethod(ACC_PUBLIC, ByteCodeUtil.internalName("incLock"), "()V", null, null);
    mv.visitCode();// www. ja  va  2 s . c  o m

    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(DUP);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("lock"), "I");
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IADD);
    mv.visitFieldInsn(PUTFIELD, className, ByteCodeUtil.internalName("lock"), "I");
    mv.visitInsn(RETURN);
    mv.visitMaxs(3, 1);

    mv.visitEnd();
}

From source file:jtaint.InstrumentationLockBuilder.java

License:Apache License

private static final void buildDecAndTestLock(ClassVisitor cv, String className) {
    MethodVisitor mv;

    mv = cv.visitMethod(ACC_PUBLIC, ByteCodeUtil.internalName("decAndTestLock"), "()I", null, null);
    mv.visitCode();//w w  w .  j  a  va  2s .  c o  m

    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(DUP);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("lock"), "I");
    mv.visitInsn(ICONST_1);
    mv.visitInsn(ISUB);
    mv.visitInsn(DUP_X1);
    mv.visitFieldInsn(PUTFIELD, className, ByteCodeUtil.internalName("lock"), "I");
    mv.visitInsn(IRETURN);
    mv.visitMaxs(3, 1);

    mv.visitEnd();
}

From source file:jtaint.RemappingStringClassAdapter.java

License:Apache License

private void buildToString(int access, boolean hasOffset) {
    /* Do not call super.visitMethod, we don't want remapping done here */
    MethodVisitor mv = cv.visitMethod(access, "toString", "()Ljava/lang/String;", null, null);
    if (mv == null)
        return;/*w  ww  .  j ava2 s  .c o m*/

    if ((access & ACC_ABSTRACT) != 0) {
        mv.visitEnd();
        return;
    }

    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/OrigStringUtil", "toBaseString",
            "(L" + className + ";)Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:jtaint.ServletAdapter.java

License:Apache License

private void buildTaintedReturnWrapper(MethodVisitor mv, Klass k, int access, String name, String desc) {
    mv.visitCode();/*from  www .  j  a  v  a2  s.  c  o  m*/
    Type[] args = Type.getArgumentTypes(desc);
    Type ret = Type.getReturnType(desc);
    boolean isStatic = (access & ACC_STATIC) != 0;
    int l = 0;

    if (!isStatic) {
        mv.visitVarInsn(ALOAD, 0);
        l = 1;
    }

    for (int i = 0; i < args.length; l += args[i].getSize(), i++)
        mv.visitVarInsn(args[i].getOpcode(ILOAD), l);
    mv.visitMethodInsn(isStatic ? INVOKESTATIC : INVOKESPECIAL, className, ByteCodeUtil.internalName(name),
            desc);

    String taintDesc = Type.getMethodDescriptor(ret, new Type[] { ret });

    if (k.isExact()) {
        /* We already know that we need to wrap...no runtime check needed */
        mv.visitMethodInsn(INVOKESTATIC, "jtaint/StringUtil", "toTainted", taintDesc);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(l, l);
        mv.visitEnd();
        return;
    }

    /* Now taint the return type, if we need to */
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("is" + k.simpleName()), "Z");
    Label l0 = new Label();
    mv.visitJumpInsn(IFEQ, l0);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/StringUtil", "toTainted", taintDesc);

    mv.visitLabel(l0);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { ret.getInternalName() });
    mv.visitInsn(ARETURN);
    mv.visitMaxs(Math.max(l, 2), l);
    mv.visitEnd();
}

From source file:jtaint.ServletAdapter.java

License:Apache License

private void buildServletWrapper(MethodVisitor mv, Klass k, String name, String desc) {
    mv.visitCode();/*w w  w . j  a  v a  2  s .c  om*/

    Label start = new Label(), end = new Label(), handler = new Label();
    mv.visitTryCatchBlock(start, end, handler, null);
    mv.visitLabel(start);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("is" + k.simpleName()), "Z");
    Label l0 = new Label();
    mv.visitJumpInsn(IFEQ, l0);

    Type[] t = Type.getArgumentTypes(desc);

    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, t[0].getInternalName(), "getParameterMap", "()Ljava/util/Map;");

    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, t[0].getInternalName(), "getRemoteHost", "()Ljava/lang/String;");

    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, t[0].getInternalName(), "getRemoteAddr", "()Ljava/lang/String;");

    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "preService",
            "(Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;)V");
    mv.visitLabel(l0);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);

    mv.visitVarInsn(ALOAD, 0);
    int l = 1;

    for (int i = 0; i < t.length; l += t[i].getSize(), i++)
        mv.visitVarInsn(t[i].getOpcode(ILOAD), l);
    mv.visitMethodInsn(INVOKESPECIAL, className, ByteCodeUtil.internalName(name), desc);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("is" + k.simpleName()), "Z");
    Label l1 = new Label();
    mv.visitJumpInsn(IFEQ, l1);

    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "postService", "()V");
    mv.visitLabel(l1);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitInsn(RETURN);
    mv.visitLabel(end);

    mv.visitLabel(handler);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("is" + k.simpleName()), "Z");
    Label l2 = new Label();
    mv.visitJumpInsn(IFEQ, l2);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "postService", "()V");
    mv.visitLabel(l2);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });

    mv.visitInsn(ATHROW);
    mv.visitMaxs(Math.max(l, 3), l);
    mv.visitEnd();
}

From source file:jtaint.ServletAdapter.java

License:Apache License

private void buildGetPathTranslatedWrapper(MethodVisitor mv) {
    mv.visitCode();//from  w  ww  .  ja  va  2s .c o  m

    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, className, ByteCodeUtil.internalName("getPathTranslated"),
            "()Ljava/lang/String;");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("isHttpServletRequest"), "Z");
    Label l0 = new Label();
    mv.visitJumpInsn(IFEQ, l0);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "getPathTranslated",
            "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;");

    mv.visitLabel(l0);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/String" });
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:jtaint.ServletAdapter.java

License:Apache License

private void buildHtmlValidatorWrapper(MethodVisitor mv, String name, String desc) {
    mv.visitCode();/*from   w w w .j  a  va2  s. c o  m*/

    Type[] t = Type.getArgumentTypes(desc);
    Type r = Type.getReturnType(desc);

    mv.visitVarInsn(ALOAD, 0);
    int l = 1;

    for (int i = 0; i < t.length; l += t[i].getSize(), i++)
        mv.visitVarInsn(t[i].getOpcode(ILOAD), l);
    mv.visitMethodInsn(INVOKESPECIAL, className, ByteCodeUtil.internalName(name), desc);

    Label l0 = new Label();
    mv.visitInsn(DUP);
    mv.visitJumpInsn(IFNULL, l0);

    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKEVIRTUAL, r.getInternalName(), ByteCodeUtil.internalName("getHtmlValidator"),
            "()Ljtaint/HtmlValidator;");
    mv.visitJumpInsn(IFNONNULL, l0);

    /* Okay, we have a valid print object and null html validator, time 
     * to initialize...
     */

    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "getHtmlValidator",
            "(Ljava/lang/Object;)Ljtaint/HtmlValidator;");
    mv.visitMethodInsn(INVOKEVIRTUAL, r.getInternalName(), ByteCodeUtil.internalName("setHtmlValidator"),
            "(Ljtaint/HtmlValidator;)V");

    mv.visitLabel(l0);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { r.getInternalName() });
    mv.visitInsn(ARETURN);

    mv.visitMaxs(Math.max(l, 3), l);
    mv.visitEnd();
}

From source file:jtaint.SqlAdapter.java

License:Apache License

private void addSqlValidator() {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC
            //[ifJava4]
            + ACC_SYNCHRONIZED/*from   w  w w  .jav a  2 s .c  o m*/
    //[fiJava4] 
            , ByteCodeUtil.internalName("sqlValidator"), "()Ljtaint/SqlValidator;", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("validator"), "Ljtaint/SqlValidator;");
    mv.visitInsn(DUP);

    Label l0 = new Label();
    mv.visitJumpInsn(IFNULL, l0);
    mv.visitInsn(ARETURN);

    mv.visitLabel(l0);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "jtaint/SqlValidator" });

    mv.visitInsn(POP);

    /* XXX This is an industrial-sized barrel of fun. We have to avoid
     * infinite recursion here when initializing the validator field --
     * i.e. when sqlValidator is called for the first time. In this case,
     * what can happen is:
     * connection.sqlValidator -> jtaint.SqlUtil.getSqlValidator
     * -> Connection.getDatabaseMetadata 
     * -> Connection.sqlValidator ->
     * -> jtaint.SqlUtil.getSqlValidator
     * -> Connection.getDatabaseMetadata
     * ... (repeat last three steps forever), where -> denotes a method call
     * So if we ever find that we already own the lock that we are about
     * to acquire, then we return an EmptySqlValidator to break
     * the recursion(Note that once the recursion unwinds, the validator
     * field will be correctly set, so we will begin returning the correct
     * sql validator. This corner case applies only during initialization).
     */

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("lockObj"), "Ljava/lang/Object;");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "holdsLock", "(Ljava/lang/Object;)Z");
    Label l1 = new Label();
    mv.visitJumpInsn(IFEQ, l1);

    /* Break the recursion */
    mv.visitFieldInsn(GETSTATIC, "jtaint/EmptySqlValidator", "INSTANCE", "Ljtaint/EmptySqlValidator;");
    mv.visitInsn(ARETURN);

    /* No recursion -- acquire the lock and initialize our field */
    mv.visitLabel(l1);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/Object" });
    mv.visitInsn(DUP);
    mv.visitInsn(MONITORENTER);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("validator"), "Ljtaint/SqlValidator;");
    mv.visitInsn(DUP);
    Label l2 = new Label();
    mv.visitJumpInsn(IFNULL, l2);
    mv.visitInsn(SWAP);
    mv.visitInsn(MONITOREXIT);
    mv.visitInsn(ARETURN);

    mv.visitLabel(l2);
    if (version == V1_6)
        mv.visitFrame(F_FULL, 1, new Object[] { className }, 2,
                new Object[] { "java/lang/Object", "jtaint/SqlValidator" });
    mv.visitInsn(POP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/SqlUtil", "getSqlValidator",
            "(Ljava/lang/Object;)Ljtaint/SqlValidator;");
    mv.visitInsn(DUP_X1);
    mv.visitFieldInsn(PUTFIELD, className, ByteCodeUtil.internalName("validator"), "Ljtaint/SqlValidator;");
    mv.visitInsn(SWAP);
    mv.visitInsn(MONITOREXIT);
    mv.visitInsn(ARETURN);

    mv.visitMaxs(4, 1);
    mv.visitEnd();
}