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.StringAdapter.java

License:Apache License

/** Force the StringBuilder/StringBuffer constructors to convert to
 * a String and invoke the String constructor
 *//*from w w w . ja v  a  2  s .  co m*/
private void replaceConstructorStringBuilder(MethodVisitor mv, String name) {
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, name, "toString", "()Ljava/lang/String;");
    mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", "(Ljava/lang/String;)V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

private void copyField(MethodVisitor mv, String name, String desc) {
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);// w  w  w  .ja  v  a 2s  .c  o  m
    mv.visitFieldInsn(GETFIELD, className, name, desc);
    mv.visitFieldInsn(PUTFIELD, className, name, desc);
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Force the String constructor to just copy all fields */
private void replaceConstructorString(MethodVisitor mv) {
    mv.visitCode();//from w w  w  . j  ava  2  s .  c o  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

    copyField(mv, "offset", "I");
    copyField(mv, "count", "I");
    copyField(mv, "value", "[C");
    copyField(mv, ByteCodeUtil.internalName("tainted"), "Z");

    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Export package-private java.lang methods for use by jtaint helper 
 * functions. /*from w w  w.  j ava  2s  . 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  w  w.ja  va2s .  c  o m
 * }
 */
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 ww  .  ja va  2  s  . co m
 * 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();/*from ww w . j  av a2s . com*/
 *     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();/*from   w  w w  .j  av  a 2 s .co  m*/

    mv.visitVarInsn(ALOAD, 0);
    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 ww w  . j av a2 s.c  o 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:jtaint.XssAdapter.java

License:Apache License

private void buildGenericWrapper(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;");

            Type[] t = Type.getArgumentTypes(methodDesc);
            for (int i = 0, l = 1; i < t.length; l += t[i].getSize(), i++)
                mv.visitVarInsn(t[i].getOpcode(ILOAD), l);

            mv.visitMethodInsn(INVOKEVIRTUAL, "jtaint/HtmlValidator", methodName,
                    Type.getMethodDescriptor(Type.VOID_TYPE, t));
        }/*from w w w.  j  a  v a  2 s  .  c om*/
    }.build();
}