Example usage for org.objectweb.asm MethodVisitor visitVarInsn

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

Introduction

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

Prototype

public void visitVarInsn(final int opcode, final int var) 

Source Link

Document

Visits a local variable instruction.

Usage

From source file:jtaint.XssAdapter.java

License:Apache License

private void buildWriteIntWrapper(final MethodVisitor mv, final String methodName, final String desc) {
    new XssLockBuilder(mv, version, className, methodName, desc) {
        public void onUnlocked() {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, className, ByteCodeUtil.internalName("getHtmlValidator"),
                    "()Ljtaint/HtmlValidator;");

            if ("javax/servlet/ServletOutputStream".equals(className)) {
                mv.visitVarInsn(ILOAD, 1);
                mv.visitInsn(I2B);//  w w  w .  j av  a2  s  .  c  om
                mv.visitMethodInsn(INVOKEVIRTUAL, "jtaint/HtmlValidator", "write", "(B)V");
            } else if ("java/io/PrintWriter".equals(className)) {
                mv.visitVarInsn(ILOAD, 1);
                mv.visitInsn(I2C);
                mv.visitMethodInsn(INVOKEVIRTUAL, "jtaint/HtmlValidator", "print", "(C)V");
            } else {
                /* Runtime check required */
                Label l0 = new Label();
                Label l1 = new Label();

                mv.visitVarInsn(ALOAD, 0);
                mv.visitTypeInsn(INSTANCEOF, "java/io/PrintWriter");
                mv.visitJumpInsn(IFNE, l1);

                /* If-branch, we're a ServletOutputStream.  */
                mv.visitVarInsn(ILOAD, 1);
                mv.visitInsn(I2B);
                mv.visitMethodInsn(INVOKEVIRTUAL, "jtaint/HtmlValidator", "write", "(B)V");
                mv.visitJumpInsn(GOTO, l0);

                /* Else-branch, we're a PrintWriter  */
                mv.visitLabel(l1);
                if (version == V1_6)
                    mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "jtaint/HtmlValidator" });

                mv.visitVarInsn(ILOAD, 1);
                mv.visitInsn(I2C);
                mv.visitMethodInsn(INVOKEVIRTUAL, "jtaint/HtmlValidator", "print", "(C)V");
                mv.visitLabel(l0);
                if (version == V1_6) {
                    mv.visitFrame(F_SAME, 0, null, 0, null);
                    /* We can't end on a visitFrame because 
                     * InstrumentationLockAdapter performs a visitFrame 
                     * after this method completes. Two consecutive 
                     * visitFrames cause the Java 6 type checker to barf, 
                     * so just pad with a single NOP.
                     */
                    mv.visitInsn(NOP);
                }
            }
        }
    }.build();
}

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 w w w.j  a v  a  2 s.  c  o m*/
        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.CallWeaver.java

License:Open Source License

static void loadVar(MethodVisitor mv, int vmt, int var) {
    assert var >= 0 : "Got var = " + var;
    if (var < 4) {
        // short instructions like ALOAD_n exist for n = 0 .. 4
        mv.visitInsn(ldInsn[vmt] + var);
    } else {/*  w  w w  . j a  v  a 2  s .c om*/
        mv.visitVarInsn(loadInsn[vmt], var);
    }
}

From source file:kilim.analysis.CallWeaver.java

License:Open Source License

static void storeVar(MethodVisitor mv, int vmt, int var) {
    assert var >= 0;
    if (var < 4) {
        // short instructions like ALOAD_n exist for n = 0 .. 4
        mv.visitInsn(stInsn[vmt] + var);
    } else {//from   w w w  .j  ava  2s . co m
        mv.visitVarInsn(storeInsn[vmt], var);
    }
}

From source file:kilim.analysis.MethodWeaver.java

License:Open Source License

/**
 * //from   w w  w. j  a  v a 2 s .com
 * Say there are two invocations to two pausable methods obj.f(int)
 * (virtual) and fs(double) (a static call) ; load fiber from last arg, and
 * save it in a fresh register ; lest it gets stomped on. This is because we
 * only patch locally, and don't change the other instructions.
 * 
 * <pre>
 *     aload lastVar
 *     dup
 *     astore fiberVar 
 *     switch (fiber.pc) { 
 *       default: 0: START 
 *       1: F_PASS_DOWN 
 *       2: FS_PASS_DOWN 
 *     }
 * </pre>
 */
private void genPrelude(MethodVisitor mv) {
    assert isPausable : "MethodWeaver.genPrelude called for nonPausable method";
    MethodFlow mf = methodFlow;
    // load fiber from last var
    int lastVar = getFiberArgVar();

    mv.visitVarInsn(ALOAD, lastVar);
    if (lastVar < fiberVar) {
        if (callWeavers.size() > 0) {
            mv.visitInsn(DUP); // for storing into fiberVar
        }
        mv.visitVarInsn(ASTORE, getFiberVar());
    }

    if (callWeavers.size() == 0) {
        // Can happen if Task.getCurrentTask() is the only pausable method
        // call. We don't need the rest of the prelude.
        return;
    }

    mv.visitFieldInsn(GETFIELD, FIBER_CLASS, "pc", D_INT);
    // The prelude doesn't need more than two words in the stack.
    // The callweaver gen* methods may need more. 
    ensureMaxStack(2);

    // switch stmt
    Label startLabel = mf.getOrCreateLabelAtPos(0);
    Label errLabel = new Label();

    Label[] labels = new Label[callWeavers.size() + 1];
    labels[0] = startLabel;
    for (int i = 0; i < callWeavers.size(); i++) {
        labels[i + 1] = new Label();
    }
    // TODO       mv.visitTableSwitchInsn(0, callWeavers.size(), startLabel, labels);
    mv.visitTableSwitchInsn(0, callWeavers.size(), errLabel, labels);

    mv.visitLabel(errLabel);
    mv.visitMethodInsn(INVOKESTATIC, FIBER_CLASS, "wrongPC", "()V");
    // Generate pass through down code, one for each pausable method
    // invocation
    int last = callWeavers.size() - 1;
    for (int i = 0; i <= last; i++) {
        CallWeaver cw = callWeavers.get(i);
        mv.visitLabel(labels[i + 1]);
        cw.genRewind(mv);
    }
    mv.visitLabel(startLabel);
}

From source file:kilim.analysis.SAMweaver.java

License:Open Source License

/**
 * Generate a method like this://  www  .j  a v  a  2s  . com
 * 
 * <pre>
 * private static $shim$1 (I callee, ...args..., f fiber) {
 *    load each arg
 *    call interface.method
 *    f.setCallee(arg0)
 *    xret
 * }
 * </pre>
 * 
 * @param cv
 */
public void accept(ClassVisitor cv) {
    String shimDesc = getShimDesc();
    MethodVisitor mv = cv.visitMethod(ACC_STATIC | ACC_PRIVATE, getShimMethodName(), shimDesc, null,
            getExceptions());
    // load arguments
    int ivar = 0;
    for (String argType : TypeDesc.getArgumentTypes(shimDesc)) {
        int vmt = VMType.toVmType(argType);
        mv.visitVarInsn(VMType.loadInsn[vmt], ivar);
        ivar += VMType.category[vmt]; // register width = 2 if double or
                                      // long, 1 otherwise
    }
    int fiberVar = ivar - 1;

    // invoke interface
    String fiberDesc = desc.replace(")", Constants.D_FIBER + ")");
    /*
     * Fixed java.lang.IncompatibleClassChangeError
     * Show in testcase kilim.test.TestAbstractExtends
     */
    if (itf) {
        mv.visitMethodInsn(INVOKEINTERFACE, interfaceName, methodName, fiberDesc, true);
    } else {
        mv.visitMethodInsn(INVOKEVIRTUAL, interfaceName, methodName, fiberDesc, false);
    }

    // store callee object reference in fiber 
    mv.visitVarInsn(ALOAD, fiberVar);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, FIBER_CLASS, "setCallee", "(" + D_OBJECT + ")V", false);

    // return .. RETURN (if void) or ARETURN, IRETURN, etc.
    String retDesc = TypeDesc.getReturnTypeDesc(shimDesc);
    if (retDesc.charAt(0) == 'V') {
        mv.visitInsn(RETURN);
    } else {
        int vmt = VMType.toVmType(retDesc);
        mv.visitInsn(VMType.retInsn[vmt]);
    }
    mv.visitMaxs(0, 0); // maxLocals and maxStackDepth will be computed by asm's MethodWriter
    mv.visitEnd();
}

From source file:kr.debop4j.core.reflect.ConstructorAccess.java

License:Apache License

/** ?? . */
static private void insertConstructor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();//w  ww  .j  a  va2  s  .c  o m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, ReflectConsts.CONSTRUCTOR_ACCESS_PATH, "<init>", "()V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:kr.debop4j.core.reflect.ConstructorAccess.java

License:Apache License

/** inner ?  */
static void insertNewInstanceInner(ClassWriter cw, String classNameInternal,
        String enclosingClassNameInternal) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(Ljava/lang/Object;)Ljava/lang/Object;", null,
            null);/*from  w w  w  .  jav  a  2s  .c om*/
    mv.visitCode();
    if (enclosingClassNameInternal != null) {
        mv.visitTypeInsn(NEW, classNameInternal);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, enclosingClassNameInternal);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
        mv.visitInsn(POP);
        mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>",
                "(L" + enclosingClassNameInternal + ";)V");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(4, 2);
    } else {
        mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
        mv.visitInsn(DUP);
        mv.visitLdcInsn("Not an inner class.");
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>",
                "(Ljava/lang/String;)V");
        mv.visitInsn(ATHROW);
        mv.visitMaxs(3, 2);
    }
    mv.visitEnd();
}

From source file:kr.debop4j.core.reflect.FieldAccess.java

License:Apache License

static private void insertConstructor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/*from w  w  w .jav a  2s.  c o m*/
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, ReflectConsts.FIELD_ACCESS_PATH, "<init>", "()V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}