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

License:Apache License

/** Wrap String methods that create new Strings so that taint is propagated.
 * Wrappers call the original method, and then call a helper routine in
 * jtaint with the original String object, method arguments, and the
 * return value from the original method. The jtaint helper then 
 * returns a String with the appropriate taint value.
 *///from  w w  w .j a va  2 s  . c om

private void buildTaintWrapper(MethodVisitor mv, String name, String desc) {
    mv.visitCode();

    Type[] t = Type.getArgumentTypes(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(INVOKEVIRTUAL, className, ByteCodeUtil.internalName(name), desc);
    mv.visitVarInsn(ASTORE, l);

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

    /* We call the jtaint helper method by passing the arguments
     * this_object, arg1, arg2, ..., argN, result_object, so append
     * and prepend an extra java/lang/String object to the arg list.
     */
    Type[] u = new Type[t.length + 2];
    Type stringType = Type.getObjectType("java/lang/String");
    u[0] = u[u.length - 1] = stringType;
    System.arraycopy(t, 0, u, 1, t.length);

    String helperDesc = Type.getMethodDescriptor(stringType, u);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/StringUtil", name, helperDesc);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(l + 1, l + 1);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Force to{Upper/Lower}Case() to return 
 * to{Upper/LowerCase}(java.util.Locale.getDefault())
 *//*from  w w w. j  a v  a2  s  .  co m*/
private void replaceChangeCase(MethodVisitor mv, String name) {
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "java/util/Locale", "getDefault", "()Ljava/util/Locale;");
    mv.visitMethodInsn(INVOKEVIRTUAL, className, name, "(Ljava/util/Locale;)Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Force substring(begin) to return substring(begin, this.count) */
private void replaceSubstring(MethodVisitor mv) {
    mv.visitCode();/*from   w  ww.  j a  va  2 s .c  om*/
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "count", "I");
    mv.visitMethodInsn(INVOKEVIRTUAL, className, "substring", "(II)Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}

From source file:jtaint.StringAdapter.java

License:Apache License

/** Force subSequence(begin, end) to return substring(begin, end) */
private void replaceSubSequence(MethodVisitor mv) {
    mv.visitCode();//from   w w w .jav  a 2  s.  c  o  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, className, "substring", "(II)Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 3);
    mv.visitEnd();
}

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  ww w.  ja v a  2 s .c  om*/
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

/** Force the String constructor to just copy all fields */
private void replaceConstructorString(MethodVisitor mv) {
    mv.visitCode();/*from w w w.j a v a  2s.  com*/
    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  a  va  2  s  .  c o  m*/
 */
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;//  w  w w .  ja  va 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:
 * /* w  ww  .ja  v  a2  s. 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();//from  w  ww.  j  a v a  2  s.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();
}