Example usage for org.objectweb.asm MethodVisitor visitCode

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

Introduction

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

Prototype

public void visitCode() 

Source Link

Document

Starts the visit of the method's code, if any (i.e.

Usage

From source file:jtaint.StringAdapter.java

License:Apache License

/** Export package-private java.lang methods for use by jtaint helper 
 * functions. /*from www .j a v  a  2 s. c om*/
 */
private void buildExportWrapper(ClassVisitor cv, String exportOwner, String name, String desc) {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC + ACC_STATIC, ByteCodeUtil.internalName(name), desc, null,
            null);
    mv.visitCode();
    Type[] t = Type.getArgumentTypes(desc);

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

    mv.visitInsn(Type.getReturnType(desc).getOpcode(IRETURN));
    mv.visitMaxs(l, l);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Create new method that compares its argument to the package-private
 * constant Character.ERROR. This must be exported for jtaint helper
 * methods. Equivalent to the following Java code:
 *
 * public static boolean isError(int c) {
 *     return c == Character.ERROR;//from  w  ww.  jav  a 2 s.  com
 * }
 */
private void addIsErrorMethod(ClassVisitor cv) {
    boolean isError = false;
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC + ACC_STATIC, ByteCodeUtil.internalName("isError"), "(I)Z",
            null, null);
    mv.visitCode();
    mv.visitVarInsn(ILOAD, 0);

    /* Test to see if java/lang/Character uses ERROR or CHAR_ERROR 
     * If ERROR cannot be found, an getDeclaredFields throws an exception
     */
    try {
        Character.class.getDeclaredField("ERROR");
        isError = true;
    } catch (Throwable th) {
        /* ignore */ }

    if (isError)
        mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "ERROR", "I");
    else
        mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "CHAR_ERROR", "C");

    Label l = new Label();
    mv.visitJumpInsn(IF_ICMPEQ, l);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);

    mv.visitLabel(l);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);

    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Create a new method that returns a Taint object representing the taint
 * for this String. Equivalent to the following Java code:
 * /*from   w w  w .  j  ava 2s .c  om*/
 * public Taint taint() {
 *     if (!tainted) {
 *         return null;
 *     } else {
 *         return jtaint.StringUtil.stringToTaint(value, count);
 *     }
 * }
 */

private void addTaintMethod(ClassVisitor cv) {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, ByteCodeUtil.internalName("taint"), "()Ljtaint/Taint;", null,
            null);
    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("tainted"), "Z");

    Label l = new Label();
    mv.visitJumpInsn(IFNE, l);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);

    mv.visitLabel(l);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "value", "[C");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "count", "I");
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/StringUtil", "stringToTaint", "([CI)Ljtaint/Taint;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 1);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Add a new constructor to a create a (partially or fully) tainted 
 * String. Equivalent to the following Java code:
 *
 * public String(String original, Taint t) {
 *     super();/*  w w  w . j a  v a  2s  .  c  o  m*/
 *     this.count = original.count;
 *
 *     if (!t.isTainted()) {
 *         this.offset = original.offset;
 *         this.value = original.value;
 *         this.tainted = original.tainted;
 *         return
 *     }
 *
 *     this.offset = 0;
 *     this.value = jtaint.StringUtil.taintToString(original, t)
 *     if (this.value.length == this.count)
 *         this.tainted = false;
 *     else
 *         this.tainted = true;
 *     return;
 * The final check (if value.length == count) is true only when an error
 * occurs during the execution of taintToString
 */

private void addConstructor(ClassVisitor cv) {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/String;Ljtaint/Taint;)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

    copyField(mv, "count", "I");

    Label l0 = new Label();
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "jtaint/Taint", "isTainted", "()Z");
    mv.visitJumpInsn(IFNE, l0);

    /* Taint object is actually untainted, copy all fields and return */
    copyField(mv, "offset", "I");
    copyField(mv, "value", "[C");
    copyField(mv, ByteCodeUtil.internalName("tainted"), "Z");
    mv.visitInsn(RETURN);

    mv.visitLabel(l0);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_0);
    mv.visitFieldInsn(PUTFIELD, className, "offset", "I");

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/StringUtil", "taintToString",
            "(Ljava/lang/String;Ljtaint/Taint;)[C");
    mv.visitInsn(DUP_X1);
    mv.visitFieldInsn(PUTFIELD, className, "value", "[C");
    mv.visitInsn(ARRAYLENGTH);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "count", "I");

    Label l1 = new Label();
    mv.visitJumpInsn(IF_ICMPEQ, l1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_1);
    mv.visitFieldInsn(PUTFIELD, className, ByteCodeUtil.internalName("tainted"), "Z");
    mv.visitInsn(RETURN);

    mv.visitLabel(l1);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_0);
    mv.visitFieldInsn(PUTFIELD, className, ByteCodeUtil.internalName("tainted"), "Z");

    mv.visitInsn(RETURN);

    mv.visitMaxs(3, 3);
    mv.visitEnd();
}

From source file:jtaint.StringMakerAdapter.java

License:Apache License

private void buildToStringWrapper(MethodVisitor mv) {
    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);// w w w  .  j av  a  2 s .c o m
    mv.visitFieldInsn(GETFIELD, className, "strings", "[Ljava/lang/String;");

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "size", "I");

    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, className, ByteCodeUtil.internalName("toString"), "()Ljava/lang/String;");
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/StringUtil", "concat",
            "([Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 1);
    mv.visitEnd();
}

From source file:jtaint.StubAdapter.java

License:Apache License

private void buildStub(MethodDecl m) {
    String name = m.name(), desc = m.type();
    int acc = m.access();
    Type[] args = Type.getArgumentTypes(desc);
    Type ret = Type.getReturnType(desc);

    /* We want any subclasses to visit the generated stub, so we invoke 
     * this.visitMethod() (which should be overridden by relevant
     * subclasses)./*from   www. j a v  a 2s .co  m*/
     */
    MethodVisitor mv = visitMethod(acc, name, desc, null, null);

    if (mv == null)
        return;

    mv.visitCode();

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

    for (int i = 0; i < args.length; l += args[i].getSize(), i++)
        mv.visitVarInsn(args[i].getOpcode(ILOAD), l);

    mv.visitMethodInsn(INVOKESPECIAL, superName, name, desc);
    mv.visitInsn(ret.getOpcode(IRETURN));

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

From source file:jvstm.atomic.ProcessParNestAnnotations.java

License:Open Source License

private static void generateCallable(File classFile, String className, String callableClass, MethodNode mn,
        boolean readOnly, boolean unsafe) {
    Type returnType = Type.getReturnType(mn.desc);

    List<Type> arguments = new ArrayList<Type>(Arrays.asList(Type.getArgumentTypes(mn.desc)));
    if (!isStatic(mn))
        arguments.add(0, Type.getObjectType(className));

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(V1_6, ACC_FINAL, callableClass, unsafe ? "Ljvstm/UnsafeParallelTask<"
            : "Ljvstm/ParallelTask<" + (isPrimitive(returnType) ? toObject(returnType)
                    : (returnType.equals(Type.VOID_TYPE) ? Type.getObjectType("java/lang/Void") : returnType))
                            .getDescriptor()
                    + ">;",
            unsafe ? "jvstm/UnsafeParallelTask" : "jvstm/ParallelTask", new String[] {});
    cw.visitSource("JVSTM Generated Wrapper Class", null);

    // Create fields to hold arguments
    {//from www. j  a  v a2 s. c  om
        int fieldPos = 0;
        for (Type t : arguments) {
            cw.visitField(ACC_PRIVATE | ACC_FINAL, "arg" + (fieldPos++), t.getDescriptor(), null, null);
        }
    }

    // Create constructor
    {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", getCallableCtorDesc(className, mn), null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, unsafe ? "jvstm/UnsafeParallelTask" : "jvstm/ParallelTask", "<init>",
                "()V");
        int localsPos = 0;
        int fieldPos = 0;
        for (Type t : arguments) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(t.getOpcode(ILOAD), localsPos + 1);
            mv.visitFieldInsn(PUTFIELD, callableClass, "arg" + fieldPos++, t.getDescriptor());
            localsPos += t.getSize();
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    // Create execute method
    {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "execute", "()Ljava/lang/Object;", null, null);
        mv.visitCode();
        int fieldPos = 0;
        for (Type t : arguments) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, callableClass, "arg" + fieldPos++, t.getDescriptor());
        }
        mv.visitMethodInsn(isStatic(mn) ? INVOKESTATIC : INVOKEVIRTUAL, className, mn.name, mn.desc);
        if (returnType.equals(Type.VOID_TYPE))
            mv.visitInsn(ACONST_NULL);
        else if (isPrimitive(returnType))
            boxWrap(returnType, mv);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    // Create the readOnly method
    {
        if (readOnly) {
            MethodVisitor mv = cw.visitMethod(ACC_PROTECTED, "isReadOnly", "()Z", null, null);
            mv.visitCode();
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
    }

    /*    protected boolean isReadOnly() {
    return false;
    }*/

    // Write the callable class file in the same directory as the original
    // class file
    String callableFileName = callableClass.substring(Math.max(callableClass.lastIndexOf('/'), 0)) + ".class";
    writeClassFile(new File((classFile.getParent() == null ? "" : classFile.getParent() + File.separatorChar)
            + callableFileName), cw.toByteArray());
}

From source file:kilim.analysis.MethodFlow.java

License:Open Source License

@Override
/**/*w w  w  .j a  v  a2  s .  com*/
 * Copied verbatim from MethodNode except for the instruction processing.
 * Unlike MethodNode, we don't keep LabelNodes inline, so we need to 
 * do visitLabel ourselves.
 * 
 * @param mv a method visitor.
 */
public void accept(final MethodVisitor mv) {
    // visits the method attributes
    int i, j, n;
    if (annotationDefault != null) {
        AnnotationVisitor av = mv.visitAnnotationDefault();
        acceptAnnotation(av, null, annotationDefault);
        av.visitEnd();
    }
    n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
    for (i = 0; i < n; ++i) {
        AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i);
        an.accept(mv.visitAnnotation(an.desc, true));
    }
    n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
    for (i = 0; i < n; ++i) {
        AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i);
        an.accept(mv.visitAnnotation(an.desc, false));
    }
    n = visibleParameterAnnotations == null ? 0 : visibleParameterAnnotations.length;
    for (i = 0; i < n; ++i) {
        List<?> l = visibleParameterAnnotations[i];
        if (l == null) {
            continue;
        }
        for (j = 0; j < l.size(); ++j) {
            AnnotationNode an = (AnnotationNode) l.get(j);
            an.accept(mv.visitParameterAnnotation(i, an.desc, true));
        }
    }
    n = invisibleParameterAnnotations == null ? 0 : invisibleParameterAnnotations.length;
    for (i = 0; i < n; ++i) {
        List<?> l = invisibleParameterAnnotations[i];
        if (l == null) {
            continue;
        }
        for (j = 0; j < l.size(); ++j) {
            AnnotationNode an = (AnnotationNode) l.get(j);
            an.accept(mv.visitParameterAnnotation(i, an.desc, false));
        }
    }
    n = attrs == null ? 0 : attrs.size();
    for (i = 0; i < n; ++i) {
        mv.visitAttribute((Attribute) attrs.get(i));
    }
    // visits the method's code
    if (instructions.size() > 0) {
        mv.visitCode();
        // visits try catch blocks
        for (i = 0; i < tryCatchBlocks.size(); ++i) {
            ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv);
        }
        // visits instructions
        for (i = 0; i < instructions.size(); ++i) {
            Label l = getLabelAt(i);
            if (l != null) {
                mv.visitLabel(l);
            }
            ((AbstractInsnNode) instructions.get(i)).accept(mv);
        }
        Label l = getLabelAt(instructions.size());
        if (l != null) {
            mv.visitLabel(l);
        }
        // visits local variables
        n = localVariables == null ? 0 : localVariables.size();
        for (i = 0; i < n; ++i) {
            ((LocalVariableNode) localVariables.get(i)).accept(mv);
        }
        // visits line numbers
        /* TODO this was in ASM 2.3.3 but not 3.x or 4.0, find a substitute or remove
                    n = lineNumbers == null ? 0 : lineNumbers.size();
                    for (i = 0; i < n; ++i) {
        ((LineNumberNode) lineNumbers.get(i)).accept(mv);
                    }
        */
        // visits maxs
        mv.visitMaxs(maxStack, maxLocals);
    }
    mv.visitEnd();
}

From source file:kilim.analysis.MethodWeaver.java

License:Open Source License

private void visitCode(MethodVisitor mv) {
    mv.visitCode();
    visitTryCatchBlocks(mv);/*from w ww .j  a v  a  2s  . c  o  m*/
    visitInstructions(mv);
    // TODO       visitLineNumbers(mv);
    visitLocals(mv);
    mv.visitMaxs(maxStack, maxVars);
}

From source file:kilim.analysis.MethodWeaver.java

License:Open Source License

void makeNotWovenMethod(ClassVisitor cv, MethodFlow mf) {
    if (classWeaver.isInterface())
        return;//  w  ww .  j a v  a  2 s  . c o  m
    // Turn of abstract modifier
    int access = mf.access;
    access &= ~Constants.ACC_ABSTRACT;
    MethodVisitor mv = cv.visitMethod(access, mf.name, mf.desc, mf.signature,
            ClassWeaver.toStringArray(mf.exceptions));
    mv.visitCode();
    visitAttrs(mv);
    mv.visitMethodInsn(INVOKESTATIC, TASK_CLASS, "errNotWoven", "()V");

    String rdesc = TypeDesc.getReturnTypeDesc(mf.desc);
    // stack size depends on return type, because we want to load
    // a constant of the appropriate size on the stack for 
    // the corresponding xreturn instruction.
    int stacksize = 0;
    if (rdesc != D_VOID) {
        // ICONST_0; IRETURN or ACONST_NULL; ARETURN etc.
        stacksize = TypeDesc.isDoubleWord(rdesc) ? 2 : 1;
        int vmt = VMType.toVmType(rdesc);
        mv.visitInsn(VMType.constInsn[vmt]);
        mv.visitInsn(VMType.retInsn[vmt]);
    } else {
        mv.visitInsn(RETURN);
    }

    int numlocals;
    if ((mf.access & Constants.ACC_ABSTRACT) != 0) {
        // The abstract method doesn't contain the number of locals required to hold the
        // args, so we need to calculate it.
        numlocals = getNumWordsInSig() + 1 /* fiber */;
        if (!mf.isStatic())
            numlocals++;
    } else {
        numlocals = mf.maxLocals + 1;
    }
    mv.visitMaxs(stacksize, numlocals);
    mv.visitEnd();
}