Java Byte代码使用ASM进行操作

时间:2011-10-07 17:53:40

标签: java assembly bytecode-manipulation java-bytecode-asm

我正在使用ASM 3.3.1。我试图拦截一个类并改变其中一个方法。我正在使用org.objectweb.asm.util.ASMifierClassVisitor来获取ASM代码来创建新方法。一切都很好,但我不能asmify if()语句。当我尝试使用带有if语句(或循环)的结果asm代码时,我在运行时遇到“堆栈大小太大”错误。我正在使用ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);.这是它给我的ASMIFY代码。

你知道我为什么只在我发出if()语句时才会收到错误吗?我非常感谢任何帮助。

`mv = cw.visitMethod(ACC_PUBLIC, "doWrite", "(Lorg/apache/tomcat/util/buf/ByteChunk;)V", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/apache/coyote/Response", "req", "Lorg/apache/coyote/Request;");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/coyote/Request", "getParameters", "()Lorg/apache/tomcat/util/http/Parameters;");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/tomcat/util/http/Parameters", "paramsAsString", "()Ljava/lang/String;");
mv.visitVarInsn(ASTORE, 2);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
mv.visitLdcInsn("The Parameters are: ");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/apache/coyote/Response", "req", "Lorg/apache/coyote/Request;");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/coyote/Request", "getParameters", "()Lorg/apache/tomcat/util/http/Parameters;");
mv.visitLdcInsn("json");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/tomcat/util/http/Parameters", "getParameter", "(Ljava/lang/String;)Ljava/lang/String;");
mv.visitVarInsn(ASTORE, 3);
mv.visitVarInsn(ALOAD, 3);
Label l0 = new Label();
mv.visitJumpInsn(IFNULL, l0);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("###%#%#%$%#%#%#%#%#%#%#%##%#");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitLabel(l0);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
mv.visitLdcInsn("Headers: ");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/apache/coyote/Response", "headers", "Lorg/apache/tomcat/util/http/MimeHeaders;");
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/tomcat/util/http/MimeHeaders", "toString", "()Ljava/lang/String;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitLdcInsn("\n");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
mv.visitLdcInsn("Writeing: ");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/tomcat/util/buf/ByteChunk", "toString", "()Ljava/lang/String;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitLdcInsn("\n");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/apache/coyote/Response", "outputBuffer", "Lorg/apache/coyote/OutputBuffer;");
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/coyote/OutputBuffer", "doWrite", "(Lorg/apache/tomcat/util/buf/ByteChunk;Lorg/apache/coyote/Response;)I");
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(DUP);
mv.visitFieldInsn(GETFIELD, "org/apache/coyote/Response", "bytesWritten", "J");
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/tomcat/util/buf/ByteChunk", "getLength", "()I");
mv.visitInsn(I2L);
mv.visitInsn(LADD);
mv.visitFieldInsn(PUTFIELD, "org/apache/coyote/Response", "bytesWritten", "J");
mv.visitInsn(RETURN);
mv.visitMaxs(5, 4);
mv.visitEnd();
`

我正在制定的守则。 (注意:我已经取出了所有我正在使用的方法)

public void doWrite(ByteChunk chunk/*byte buffer[], int pos, int count*/)
        throws IOException
    {

        String params=req.getParameters().paramsAsString();

        System.out.println("The Parameters are: "+params);

        String reqParam = req.getParameters().getParameter("json");

        if(reqParam != null)
        {
            System.out.println("###%#%#%$%#%#%#%#%#%#%#%##%#");
        }

        System.out.println("Headers: " + headers.toString()+ "\n");
        System.out.println("Writeing: " +chunk.toString()+"\n");


        outputBuffer.doWrite(chunk, this);
        bytesWritten+=chunk.getLength();
    }

原始方法只包含最后两行

outputBuffer.doWrite(chunk, this);
bytesWritten+=chunk.getLength();

这看起来不太好。我使用了CheckClassAdapter.verify()方法并将其解释为我正在搞乱的类。

doWrite(Lorg/apache/tomcat/util/buf/ByteChunk;)V
00000 Response ByteChunk . .  :  :    FRAME FULL [] []
00001 Response ByteChunk . .  :  :     ALOAD 0
00002 Response ByteChunk . .  : Response  :     GETFIELD org/apache/coyote/Response.req : Lorg/apache/coyote/Request;
00003 Response ByteChunk . .  : Request  :     INVOKEVIRTUAL org/apache/coyote/Request.getParameters ()Lorg/apache/tomcat/util/http/Parameters;
00004 Response ByteChunk . .  : Parameters  :     INVOKEVIRTUAL org/apache/tomcat/util/http/Parameters.paramsAsString ()Ljava/lang/String;
00005 Response ByteChunk . .  : String  :     ASTORE 2
00006 Response ByteChunk String .  :  :     GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
00007 Response ByteChunk String .  : PrintStream  :     NEW java/lang/StringBuilder
00008 Response ByteChunk String .  : PrintStream StringBuilder  :     DUP
00009 Response ByteChunk String .  : PrintStream StringBuilder StringBuilder  :     INVOKESPECIAL java/lang/StringBuilder.<init> ()V
00010 Response ByteChunk String .  : PrintStream StringBuilder  :     LDC "The Parameters are: "
00011 Response ByteChunk String .  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00012 Response ByteChunk String .  : PrintStream StringBuilder  :     ALOAD 2
00013 Response ByteChunk String .  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00014 Response ByteChunk String .  : PrintStream StringBuilder  :     INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
00015 Response ByteChunk String .  : PrintStream String  :     INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
00016 Response ByteChunk String .  :  :     ALOAD 0
00017 Response ByteChunk String .  : Response  :     GETFIELD org/apache/coyote/Response.req : Lorg/apache/coyote/Request;
00018 Response ByteChunk String .  : Request  :     INVOKEVIRTUAL org/apache/coyote/Request.getParameters ()Lorg/apache/tomcat/util/http/Parameters;
00019 Response ByteChunk String .  : Parameters  :     LDC "json"
00020 Response ByteChunk String .  : Parameters String  :     INVOKEVIRTUAL org/apache/tomcat/util/http/Parameters.getParameter (Ljava/lang/String;)Ljava/lang/String;
00021 Response ByteChunk String .  : String  :     ASTORE 3
00022 Response ByteChunk String String  :  :     ALOAD 3
00023 Response ByteChunk String String  : String  :     IFNULL L0
00024 Response ByteChunk String String  :  :     GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
00025 Response ByteChunk String String  : PrintStream  :     LDC "###%#%#%$%#%#%#%#%#%#%#%##%#"
00026 Response ByteChunk String String  : PrintStream String  :     INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
00027 Response ByteChunk String String  :  :    L0
00028 Response ByteChunk String String  :  :    FRAME FULL [org/apache/coyote/Response org/apache/tomcat/
00029 Response ByteChunk String String  :  :     GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
00030 Response ByteChunk String String  : PrintStream  :     NEW java/lang/StringBuilder
00031 Response ByteChunk String String  : PrintStream StringBuilder  :     DUP
00032 Response ByteChunk String String  : PrintStream StringBuilder StringBuilder  :     INVOKESPECIAL java/lang/StringBuilder.<init> ()V
00033 Response ByteChunk String String  : PrintStream StringBuilder  :     LDC "Headers: "
00034 Response ByteChunk String String  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00035 Response ByteChunk String String  : PrintStream StringBuilder  :     ALOAD 0
00036 Response ByteChunk String String  : PrintStream StringBuilder Response  :     GETFIELD org/apache/coyote/Response.headers : Lorg/apache/tomcat/util/http/MimeHeaders;
00037 Response ByteChunk String String  : PrintStream StringBuilder MimeHeaders  :     INVOKEVIRTUAL org/apache/tomcat/util/http/MimeHeaders.toString ()Ljava/lang/String;
00038 Response ByteChunk String String  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00039 Response ByteChunk String String  : PrintStream StringBuilder  :     LDC "\n"
00040 Response ByteChunk String String  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00041 Response ByteChunk String String  : PrintStream StringBuilder  :     INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
00042 Response ByteChunk String String  : PrintStream String  :     INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
00043 Response ByteChunk String String  :  :     GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
00044 Response ByteChunk String String  : PrintStream  :     NEW java/lang/StringBuilder
00045 Response ByteChunk String String  : PrintStream StringBuilder  :     DUP
00046 Response ByteChunk String String  : PrintStream StringBuilder StringBuilder  :     INVOKESPECIAL java/lang/StringBuilder.<init> ()V
00047 Response ByteChunk String String  : PrintStream StringBuilder  :     LDC "Writeing: "
00048 Response ByteChunk String String  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00049 Response ByteChunk String String  : PrintStream StringBuilder  :     ALOAD 1
00050 Response ByteChunk String String  : PrintStream StringBuilder ByteChunk  :     INVOKEVIRTUAL org/apache/tomcat/util/buf/ByteChunk.toString ()Ljava/lang/String;
00051 Response ByteChunk String String  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00052 Response ByteChunk String String  : PrintStream StringBuilder  :     LDC "\n"
00053 Response ByteChunk String String  : PrintStream StringBuilder String  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00054 Response ByteChunk String String  : PrintStream StringBuilder  :     INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
00055 Response ByteChunk String String  : PrintStream String  :     INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
00056 Response ByteChunk String String  :  :     ALOAD 0
00057 Response ByteChunk String String  : Response  :     GETFIELD org/apache/coyote/Response.outputBuffer : Lorg/apache/coyote/OutputBuffer;
00058 Response ByteChunk String String  : OutputBuffer  :     ALOAD 1
00059 Response ByteChunk String String  : OutputBuffer ByteChunk  :     ALOAD 0
00060 Response ByteChunk String String  : OutputBuffer ByteChunk Response  :     INVOKEINTERFACE org/apache/coyote/OutputBuffer.doWrite (Lorg/apache/tomcat/util/buf/ByteChunk;Lorg/apache/coyote/Response;)I
00061 Response ByteChunk String String  : I  :     POP
00062 Response ByteChunk String String  :  :     ALOAD 0
00063 Response ByteChunk String String  : Response  :     DUP
00064 Response ByteChunk String String  : Response Response  :     GETFIELD org/apache/coyote/Response.bytesWritten : J
00065 Response ByteChunk String String  : Response J  :     ALOAD 1
00066 Response ByteChunk String String  : Response J ByteChunk  :     INVOKEVIRTUAL org/apache/tomcat/util/buf/ByteChunk.getLength ()I
00067 Response ByteChunk String String  : Response J I  :     I2L
00068 Response ByteChunk String String  : Response J J  :     LADD
00069 Response ByteChunk String String  : Response J  :     PUTFIELD org/apache/coyote/Response.bytesWritten : J
00070 Response ByteChunk String String  :  :     RETURN
00071 ?        :     NOP
00072 ?        :     NOP
00073 ?        :     NOP
00074 ?        :     NOP
00075 ?        :     NOP
00076 ?        :     NOP
00077 ?        :     NOP
00078 ?        :     NOP
00079 ?        :     NOP
00080 ?        :     NOP
00081 ?        :     NOP
00082 ?        :     NOP
00083 ?        :     NOP
00084 ?        :     NOP
00085 ?        :     NOP
00086 ?        :     NOP
00087 ?        :     NOP
00088 ?        :     NOP
00089 ?        :     NOP
00090 ?        :     NOP
00091 ?        :     NOP
00092 ?        :     NOP
00093 ?        :     NOP
00094 ?        :     NOP
00095 ?        :     NOP
00096 ?        :     NOP
00097 ?        :     ATHROW

1 个答案:

答案 0 :(得分:0)

我怀疑问题是你不能在包含if语句的类上使用ASMIfier。

当您添加到ASMifier生成的代码时,您更有可能以增加所需堆栈数量的方式使用堆栈,并且您没有增加方法结束时的最大设置。