Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Exception from method body with ASM

Intro:

I have a (obfuscated) METHOD that I printed using ASM and the output was as follows:

METHOD: m(ZB)Lcc;
--------------------------------------

L0:
{
    ALOAD_0
    GETFIELD   d/x I
    LDC  2036719157
    IMUL
    ISTORE
    GOTO  L1
}

L2:
{
    ALOAD_6
    ICONST_0
    LDC  -373364649
    ALOAD_0
    GETFIELD   d/at I
    IMUL
    ICONST_0
    INVOKEVIRTUAL   cc/y(III)V
    GOTO  L3
}

L4:
{
    ACONST_NULL
    ARETURN
}

L5:
{
    ILOAD_1
    IFEQ  L6
    LDC  -723220973
    ALOAD_0
    GETFIELD   d/an I
    IMUL
    ISTORE
    LDC  1671960653
    ALOAD_0
    GETFIELD   d/ad I
    IMUL
    ISTORE
    GOTO  L7
}

L8:
{
    LDC  1955946639
    ALOAD_0
    GETFIELD   d/au I
    IMUL
    IFEQ  L9
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L10
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L11:
{
    IINC
    GOTO  L12
}

L7:
{
    LDC  894092883
    ALOAD_0
    GETFIELD   d/ac I
    IMUL
    ISTORE
    GOTO  L6
}

L13:
{
    GETSTATIC   d/b Lem;
    ILOAD_3
    ICONST_0
    INVOKESTATIC   cc/l(Lem;II)Lcc;
    ASTORE
    ILOAD_4
    ICONST_M1
    IF_ICMPEQ  L14
    GETSTATIC   d/b Lem;
    ILOAD_4
    ICONST_0
    INVOKESTATIC   cc/l(Lem;II)Lcc;
    ASTORE
    GOTO  L15
}

L16:
{
    ICONST_2
    ANEWARRAY
    DUP
    ICONST_0
    ALOAD_6
    AASTORE
    DUP
    ICONST_1
    ALOAD_7
    AASTORE
    ASTORE
    GOTO  L17
}

L18:
{
    GETSTATIC   d/b Lem;
    ILOAD_5
    ICONST_0
    INVOKESTATIC   cc/l(Lem;II)Lcc;
    ASTORE
    ICONST_3
    ANEWARRAY
    DUP
    ICONST_0
    ALOAD_6
    AASTORE
    DUP
    ICONST_1
    ALOAD_7
    AASTORE
    DUP
    ICONST_2
    ALOAD_8
    AASTORE
    ASTORE
    NEW
    DUP
    ALOAD_9
    ICONST_3
    INVOKESPECIAL   cc/<init>([Lcc;I)V
    ASTORE
    GOTO  L14
}

L19:
{
    ALOAD_6
    ARETURN
}

L17:
{
    NEW
    DUP
    ALOAD_8
    ICONST_2
    INVOKESPECIAL   cc/<init>([Lcc;I)V
    ASTORE
}

L14:
{
    ILOAD_1
    IFNE  L9
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L8
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L15:
{
    ILOAD_5
    ICONST_M1
    IF_ICMPEQ  L16
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L18
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L20:
{
    LDC  1642271889
    ALOAD_0
    GETFIELD   d/ay I
    IMUL
    ISTORE
    GOTO  L5
}

L9:
{
    ILOAD_1
    IFEQ  L3
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L21
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L21:
{
    ICONST_0
    LDC  -373364649
    ALOAD_0
    GETFIELD   d/at I
    IMUL
    IF_ICMPEQ  L3
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L2
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L22:
{
    ICONST_0
    ISTORE
    GOTO  L23
}

L3:
{
    ALOAD_0
    GETFIELD   d/c [S
    IFNULL  L24
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L25
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L25:
{
    ICONST_0
    ISTORE
    GOTO  L12
}

L24:
{
    ALOAD_0
    GETFIELD   d/a [S
    IFNULL  L19
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L22
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L26:
{
    ALOAD_6
    ALOAD_0
    GETFIELD   d/c [S
    ILOAD_7
    SALOAD
    ALOAD_0
    GETFIELD   d/m [S
    ILOAD_7
    SALOAD
    INVOKEVIRTUAL   cc/v(SS)V
    GOTO  L11
}

L12:
{
    ILOAD_7
    ALOAD_0
    GETFIELD   d/c [S
    ARRAYLENGTH
    IF_ICMPGE  L24
    GOTO  L26
}

L1:
{
    ALOAD_0
    GETFIELD   d/ar I
    LDC  608958183
    IMUL
    ISTORE
    GOTO  L20
}

L10:
{
    ALOAD_6
    ICONST_0
    LDC  1955946639
    ALOAD_0
    GETFIELD   d/au I
    IMUL
    ICONST_0
    INVOKEVIRTUAL   cc/y(III)V
    GOTO  L9
}

L23:
{
    ILOAD_7
    ALOAD_0
    GETFIELD   d/a [S
    ARRAYLENGTH
    IF_ICMPGE  L19
    ILOAD_2
    ICONST_1
    IF_ICMPEQ  L27
    NEW
    DUP
    INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
    ATHROW
}

L27:
{
    ALOAD_6
    ALOAD_0
    GETFIELD   d/a [S
    ILOAD_7
    SALOAD
    ALOAD_0
    GETFIELD   d/e [S
    ILOAD_7
    SALOAD
    INVOKEVIRTUAL   cc/s(SS)V
    IINC
    GOTO  L23
}

L6:
{
    ILOAD_3
    ICONST_M1
    IF_ICMPNE  L13
    GOTO  L4
}

L28:
{
    NEW
    DUP
    INVOKESPECIAL   java/lang/StringBuilder/<init>()V
    LDC  "af.m("
    INVOKEVIRTUAL   java/lang/StringBuilder/append(Ljava/lang/String;)Ljava/lang/StringBuilder;
    LDC  41
    INVOKEVIRTUAL   java/lang/StringBuilder/append(C)Ljava/lang/StringBuilder;
    INVOKEVIRTUAL   java/lang/StringBuilder/toString()Ljava/lang/String;
    INVOKESTATIC   b/b(Ljava/lang/Throwable;Ljava/lang/String;)Lcd;
    ATHROW
}

What I want:

I want to REMOVE all the exceptions. For example (I want to remove):

ILOAD_2
ICONST_1
IF_ICMPEQ  L27
NEW
DUP
INVOKESPECIAL   java/lang/IllegalStateException/<init>()V
ATHROW

which is translated as: if (var2 != 1) {throw new IllegalStateException();}

I don't plan on running the class but I do plan on it being properly assembled. Meaning that if I remove the exception, there won't be any bytecode errors when using the CheckClassAdapter. I don't need the code to actually work.

What I tried:

I tried the following which I used to remove the exceptions:

private void findException(MethodNode method) {
    int i = findExceptionIndex(method); //returns the first occurrence of the above pattern.

    while (i != -1) {
        for (int j = i; j < i + 7; ++j) { //length of if statement + exception instructions.
            if (method.instructions.get(j) instanceof MethodInsnNode) {

                //if it really is an exception
                if (((MethodInsnNode) method.instructions.get(j)).owner.contains("Exception")) {

                    for (int k = i + 6; k != i - 1; --k) {
                        System.out.println("Removing: " + Printer.OPCODES[method.instructions.get(k).getOpcode()]);
                        method.instructions.remove(method.instructions.get(k));
                    }
                    return; //I only want to try to remove one first (before trying to remove all)
                }
            }
        }
        i = findNextExceptionIndex(method, i);
    }
}

and it prints (successfully removed the first exception that has the following Opcodes):

Removing: ATHROW
Removing: INVOKESPECIAL
Removing: DUP
Removing: NEW
Removing: IF_ICMPEQ
Removing: ICONST_1
Removing: ILOAD

ERRORS:

However, I get a HUGE exception when I try to save the class:

jdk.internal.org.objectweb.asm.tree.analysis.AnalyzerException: Error at instruction 41: Expected I, but found .
    at jdk.internal.org.objectweb.asm.tree.analysis.Analyzer.analyze(Analyzer.java:323)
    at jdk.internal.org.objectweb.asm.util.CheckMethodAdapter$1.visitEnd(CheckMethodAdapter.java:479)
    at jdk.internal.org.objectweb.asm.MethodVisitor.visitEnd(MethodVisitor.java:906)
    at jdk.internal.org.objectweb.asm.util.CheckMethodAdapter.visitEnd(CheckMethodAdapter.java:1057)
    at jdk.internal.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:866)
    at jdk.internal.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:755)
    at jdk.internal.org.objectweb.asm.tree.ClassNode.accept(ClassNode.java:441)
    at com.other.JarParser.saveClass(JarParser.java:81)
Caused by: jdk.internal.org.objectweb.asm.tree.analysis.AnalyzerException: Expected I, but found .
    at jdk.internal.org.objectweb.asm.tree.analysis.BasicVerifier.unaryOperation(BasicVerifier.java:211)
    at jdk.internal.org.objectweb.asm.tree.analysis.BasicVerifier.unaryOperation(BasicVerifier.java:76)
    at jdk.internal.org.objectweb.asm.tree.analysis.Frame.execute(Frame.java:546)
    at jdk.internal.org.objectweb.asm.tree.analysis.Analyzer.analyze(Analyzer.java:228)
    ... 14 more
Exception in thread "main" java.lang.RuntimeException: Error at instruction 41: Expected I, but found . m(ZB)Lcc;
00000 R I I . . . . . . .  :  :    L0
00001 R I I . . . . . . .  :  :     ALOAD 0
00002 R I I . . . . . . .  : R  :     GETFIELD d.x : I
00003 R I I . . . . . . .  : I  :     LDC 2036719157
00004 R I I . . . . . . .  : I I  :     IMUL
00005 R I I . . . . . . .  : I  :     ISTORE 3
00006 R I I I . . . . . .  :  :     GOTO L1
00007 R I I I I I R . . .  :  :    L2
00008 R I I I I I R . . .  :  :     ALOAD 6
00009 R I I I I I R . . .  : R  :     ICONST_0
00010 R I I I I I R . . .  : R I  :     LDC -373364649
00011 R I I I I I R . . .  : R I I  :     ALOAD 0
00012 R I I I I I R . . .  : R I I R  :     GETFIELD d.at : I
00013 R I I I I I R . . .  : R I I I  :     IMUL
00014 R I I I I I R . . .  : R I I  :     ICONST_0
00015 R I I I I I R . . .  : R I I I  :     INVOKEVIRTUAL cc.y (III)V
00016 R I I I I I R . . .  :  :     GOTO L3
00017 ?               :    L4
00018 ?               :     ACONST_NULL
00019 ?               :     ARETURN
00020 R I I I I I . . . .  :  :    L5
00021 R I I I I I . . . .  :  :     ILOAD 1
00022 R I I I I I . . . .  : I  :     IFEQ L6
00023 R I I I I I . . . .  :  :     LDC -723220973
00024 ?               :     ALOAD 0
00025 ?               :     GETFIELD d.an : I
00026 ?               :     IMUL
00027 ?               :     ISTORE 3
00028 ?               :     LDC 1671960653
00029 ?               :     ALOAD 0
00030 ?               :     GETFIELD d.ad : I
00031 ?               :     IMUL
00032 ?               :     ISTORE 4
00033 ?               :     GOTO L7
00034 R I I I I I R . . .  :  :    L8
00035 R I I I I I R . . .  :  :     LDC 1955946639
00036 R I I I I I R . . .  : I  :     ALOAD 0
00037 R I I I I I R . . .  : I R  :     GETFIELD d.au : I
00038 R I I I I I R . . .  : I I  :     IMUL
00039 R I I I I I R . . .  : I  :     IFEQ L9
00040 R I I I I I R . . .  :  :    L10
00041 R I I I I I R . . .  :  :     IINC 7 1
00042 R I I I I I R I . .  :  :     GOTO L11
00043 ?               :    L7
00044 ?               :     LDC 894092883
00045 ?               :     ALOAD 0
00046 ?               :     GETFIELD d.ac : I
00047 ?               :     IMUL
00048 ?               :     ISTORE 5
00049 ?               :     GOTO L6
00050 R I I I I I . . . .  :  :    L12
00051 R I I I I I . . . .  :  :     GETSTATIC d.b : Lem;
00052 R I I I I I . . . .  : R  :     ILOAD 3
00053 R I I I I I . . . .  : R I  :     ICONST_0
00054 R I I I I I . . . .  : R I I  :     INVOKESTATIC cc.l (Lem;II)Lcc;
00055 R I I I I I . . . .  : R  :     ASTORE 6
00056 R I I I I I R . . .  :  :     ILOAD 4
00057 R I I I I I R . . .  : I  :     ICONST_M1
00058 R I I I I I R . . .  : I I  :     IF_ICMPEQ L13
00059 R I I I I I R . . .  :  :     GETSTATIC d.b : Lem;
00060 ?               :     ILOAD 4
00061 ?               :     ICONST_0
00062 ?               :     INVOKESTATIC cc.l (Lem;II)Lcc;
00063 ?               :     ASTORE 7
00064 ?               :     GOTO L14
00065 ?               :    L15
00066 ?               :     ICONST_2
00067 ?               :     ANEWARRAY cc
00068 ?               :     DUP
00069 ?               :     ICONST_0
00070 ?               :     ALOAD 6
00071 ?               :     AASTORE
00072 ?               :     DUP
00073 ?               :     ICONST_1
00074 ?               :     ALOAD 7
00075 ?               :     AASTORE
00076 ?               :     ASTORE 8
00077 ?               :     GOTO L16
00078 ?               :    L17
00079 ?               :     GETSTATIC d.b : Lem;
00080 ?               :     ILOAD 5
00081 ?               :     ICONST_0
00082 ?               :     INVOKESTATIC cc.l (Lem;II)Lcc;
00083 ?               :     ASTORE 8
00084 ?               :     ICONST_3
00085 ?               :     ANEWARRAY cc
00086 ?               :     DUP
00087 ?               :     ICONST_0
00088 ?               :     ALOAD 6
00089 ?               :     AASTORE
00090 ?               :     DUP
00091 ?               :     ICONST_1
00092 ?               :     ALOAD 7
00093 ?               :     AASTORE
00094 ?               :     DUP
00095 ?               :     ICONST_2
00096 ?               :     ALOAD 8
00097 ?               :     AASTORE
00098 ?               :     ASTORE 9
00099 ?               :     NEW cc
00100 ?               :     DUP
00101 ?               :     ALOAD 9
00102 ?               :     ICONST_3
00103 ?               :     INVOKESPECIAL cc.<init> ([Lcc;I)V
00104 ?               :     ASTORE 6
00105 ?               :     GOTO L13
00106 R I I I I I R . . .  :  :    L18
00107 R I I I I I R . . .  :  :     ALOAD 6
00108 R I I I I I R . . .  : R  :     ARETURN
00109 ?               :    L16
00110 ?               :     NEW cc
00111 ?               :     DUP
00112 ?               :     ALOAD 8
00113 ?               :     ICONST_2
00114 ?               :     INVOKESPECIAL cc.<init> ([Lcc;I)V
00115 ?               :     ASTORE 6
00116 R I I I I I R . . .  :  :    L13
00117 R I I I I I R . . .  :  :     ILOAD 1
00118 R I I I I I R . . .  : I  :     IFNE L9
00119 R I I I I I R . . .  :  :     ILOAD 2
00120 R I I I I I R . . .  : I  :     ICONST_1
00121 R I I I I I R . . .  : I I  :     IF_ICMPEQ L8
00122 R I I I I I R . . .  :  :     NEW java/lang/IllegalStateException
00123 ?               :     DUP
00124 ?               :     INVOKESPECIAL java/lang/IllegalStateException.<init> ()V
00125 ?               :     ATHROW
00126 ?               :    L14
00127 ?               :     ILOAD 5
00128 ?               :     ICONST_M1
00129 ?               :     IF_ICMPEQ L15
00130 ?               :     ILOAD 2
00131 ?               :     ICONST_1
00132 ?               :     IF_ICMPEQ L17
00133 ?               :     NEW java/lang/IllegalStateException
00134 ?               :     DUP
00135 ?               :     INVOKESPECIAL java/lang/IllegalStateException.<init> ()V
00136 ?               :     ATHROW
00137 R I I I I . . . . .  :  :    L19
00138 R I I I I . . . . .  :  :     LDC 1642271889
00139 R I I I I . . . . .  : I  :     ALOAD 0
00140 R I I I I . . . . .  : I R  :     GETFIELD d.ay : I
00141 R I I I I . . . . .  : I I  :     IMUL
00142 R I I I I . . . . .  : I  :     ISTORE 5
00143 R I I I I I . . . .  :  :     GOTO L5
00144 R I I I I I R . . .  :  :    L9
00145 R I I I I I R . . .  :  :     ILOAD 1
00146 R I I I I I R . . .  : I  :     IFEQ L3
00147 R I I I I I R . . .  :  :     ILOAD 2
00148 R I I I I I R . . .  : I  :     ICONST_1
00149 R I I I I I R . . .  : I I  :     IF_ICMPEQ L20
00150 R I I I I I R . . .  :  :     NEW java/lang/IllegalStateException
00151 R I I I I I R . . .  : R  :     DUP
00152 R I I I I I R . . .  : R R  :     INVOKESPECIAL java/lang/IllegalStateException.<init> ()V
00153 R I I I I I R . . .  : R  :     ATHROW
00154 R I I I I I R . . .  :  :    L20
00155 R I I I I I R . . .  :  :     ICONST_0
00156 R I I I I I R . . .  : I  :     LDC -373364649
00157 R I I I I I R . . .  : I I  :     ALOAD 0
00158 R I I I I I R . . .  : I I R  :     GETFIELD d.at : I
00159 R I I I I I R . . .  : I I I  :     IMUL
00160 R I I I I I R . . .  : I I  :     IF_ICMPEQ L3
00161 R I I I I I R . . .  :  :     ILOAD 2
00162 R I I I I I R . . .  : I  :     ICONST_1
00163 R I I I I I R . . .  : I I  :     IF_ICMPEQ L2
00164 R I I I I I R . . .  :  :     NEW java/lang/IllegalStateException
00165 R I I I I I R . . .  : R  :     DUP
00166 R I I I I I R . . .  : R R  :     INVOKESPECIAL java/lang/IllegalStateException.<init> ()V
00167 R I I I I I R . . .  : R  :     ATHROW
00168 R I I I I I R . . .  :  :    L21
00169 R I I I I I R . . .  :  :     ICONST_0
00170 R I I I I I R . . .  : I  :     ISTORE 7
00171 R I I I I I R I . .  :  :     GOTO L22
00172 R I I I I I R . . .  :  :    L3
00173 R I I I I I R . . .  :  :     ALOAD 0
00174 R I I I I I R . . .  : R  :     GETFIELD d.c : [S
00175 R I I I I I R . . .  : R  :     IFNULL L23
00176 R I I I I I R . . .  :  :     ILOAD 2
00177 R I I I I I R . . .  : I  :     ICONST_1
00178 R I I I I I R . . .  : I I  :     IF_ICMPEQ L24
00179 R I I I I I R . . .  :  :     NEW java/lang/IllegalStateException
00180 R I I I I I R . . .  : R  :     DUP
00181 R I I I I I R . . .  : R R  :     INVOKESPECIAL java/lang/IllegalStateException.<init> ()V
00182 R I I I I I R . . .  : R  :     ATHROW
00183 R I I I I I R . . .  :  :    L24
00184 R I I I I I R . . .  :  :     ICONST_0
00185 R I I I I I R . . .  : I  :     ISTORE 7
00186 R I I I I I R I . .  :  :     GOTO L11
00187 R I I I I I R . . .  :  :    L23
00188 R I I I I I R . . .  :  :     ALOAD 0
00189 R I I I I I R . . .  : R  :     GETFIELD d.a : [S
00190 R I I I I I R . . .  : R  :     IFNULL L18
00191 R I I I I I R . . .  :  :     ILOAD 2
00192 R I I I I I R . . .  : I  :     ICONST_1
00193 R I I I I I R . . .  : I I  :     IF_ICMPEQ L21
00194 R I I I I I R . . .  :  :     NEW java/lang/IllegalStateException
00195 R I I I I I R . . .  : R  :     DUP
00196 R I I I I I R . . .  : R R  :     INVOKESPECIAL java/lang/IllegalStateException.<init> ()V
00197 R I I I I I R . . .  : R  :     ATHROW
00198 R I I I I I R I . .  :  :    L25
00199 R I I I I I R I . .  :  :     ALOAD 6
00200 R I I I I I R I . .  : R  :     ALOAD 0
00201 R I I I I I R I . .  : R R  :     GETFIELD d.c : [S
00202 R I I I I I R I . .  : R R  :     ILOAD 7
00203 R I I I I I R I . .  : R R I  :     SALOAD
00204 R I I I I I R I . .  : R I  :     ALOAD 0
00205 R I I I I I R I . .  : R I R  :     GETFIELD d.m : [S
00206 R I I I I I R I . .  : R I R  :     ILOAD 7
00207 R I I I I I R I . .  : R I R I  :     SALOAD
00208 R I I I I I R I . .  : R I I  :     INVOKEVIRTUAL cc.v (SS)V
00209 R I I I I I R I . .  :  :     GOTO L10
00210 R I I I I I R I . .  :  :    L11
00211 R I I I I I R I . .  :  :     ILOAD 7
00212 R I I I I I R I . .  : I  :     ALOAD 0
00213 R I I I I I R I . .  : I R  :     GETFIELD d.c : [S
00214 R I I I I I R I . .  : I R  :     ARRAYLENGTH
00215 R I I I I I R I . .  : I I  :     IF_ICMPGE L23
00216 R I I I I I R I . .  :  :     GOTO L25
00217 R I I I . . . . . .  :  :    L1
00218 R I I I . . . . . .  :  :     ALOAD 0
00219 R I I I . . . . . .  : R  :     GETFIELD d.ar : I
00220 R I I I . . . . . .  : I  :     LDC 608958183
00221 R I I I . . . . . .  : I I  :     IMUL
00222 R I I I . . . . . .  : I  :     ISTORE 4
00223 R I I I I . . . . .  :  :     GOTO L19
00224 ?               :    L26
00225 ?               :     ALOAD 6
00226 ?               :     ICONST_0
00227 ?               :     LDC 1955946639
00228 ?               :     ALOAD 0
00229 ?               :     GETFIELD d.au : I
00230 ?               :     IMUL
00231 ?               :     ICONST_0
00232 ?               :     INVOKEVIRTUAL cc.y (III)V
00233 ?               :     GOTO L9
00234 R I I I I I R I . .  :  :    L22
00235 R I I I I I R I . .  :  :     ILOAD 7
00236 R I I I I I R I . .  : I  :     ALOAD 0
00237 R I I I I I R I . .  : I R  :     GETFIELD d.a : [S
00238 R I I I I I R I . .  : I R  :     ARRAYLENGTH
00239 R I I I I I R I . .  : I I  :     IF_ICMPGE L18
00240 R I I I I I R I . .  :  :     ILOAD 2
00241 R I I I I I R I . .  : I  :     ICONST_1
00242 R I I I I I R I . .  : I I  :     IF_ICMPEQ L27
00243 R I I I I I R I . .  :  :     NEW java/lang/IllegalStateException
00244 R I I I I I R I . .  : R  :     DUP
00245 R I I I I I R I . .  : R R  :     INVOKESPECIAL java/lang/IllegalStateException.<init> ()V
00246 R I I I I I R I . .  : R  :     ATHROW
00247 R I I I I I R I . .  :  :    L27
00248 R I I I I I R I . .  :  :     ALOAD 6
00249 R I I I I I R I . .  : R  :     ALOAD 0
00250 R I I I I I R I . .  : R R  :     GETFIELD d.a : [S
00251 R I I I I I R I . .  : R R  :     ILOAD 7
00252 R I I I I I R I . .  : R R I  :     SALOAD
00253 R I I I I I R I . .  : R I  :     ALOAD 0
00254 R I I I I I R I . .  : R I R  :     GETFIELD d.e : [S
00255 R I I I I I R I . .  : R I R  :     ILOAD 7
00256 R I I I I I R I . .  : R I R I  :     SALOAD
00257 R I I I I I R I . .  : R I I  :     INVOKEVIRTUAL cc.s (SS)V
00258 R I I I I I R I . .  :  :     IINC 7 1
00259 R I I I I I R I . .  :  :     GOTO L22
00260 R I I I I I . . . .  :  :    L6
00261 R I I I I I . . . .  :  :     ILOAD 3
00262 R I I I I I . . . .  : I  :     ICONST_M1
00263 R I I I I I . . . .  : I I  :     IF_ICMPNE L12
00264 R I I I I I . . . .  :  :     GOTO L4
00265 R I I . . . . . . .  : R  :    L28
00266 R I I . . . . . . .  : R  :     NEW java/lang/StringBuilder
00267 R I I . . . . . . .  : R R  :     DUP
00268 R I I . . . . . . .  : R R R  :     INVOKESPECIAL java/lang/StringBuilder.<init> ()V
00269 R I I . . . . . . .  : R R  :     LDC "af.m("
00270 R I I . . . . . . .  : R R R  :     INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
00271 R I I . . . . . . .  : R R  :     LDC 41
00272 R I I . . . . . . .  : R R I  :     INVOKEVIRTUAL java/lang/StringBuilder.append (C)Ljava/lang/StringBuilder;
00273 R I I . . . . . . .  : R R  :     INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
00274 R I I . . . . . . .  : R R  :     INVOKESTATIC b.b (Ljava/lang/Throwable;Ljava/lang/String;)Lcd;
00275 R I I . . . . . . .  : R  :     ATHROW
     TRYCATCHBLOCK L0 L28 L28 java/lang/RuntimeException

Question:

How can I remove the exceptions properly so that I don't get the above errors? It seems like a "Frame" error/problem but I'm not sure how to fix it. Any ideas?

like image 475
Brandon Avatar asked Oct 19 '22 20:10

Brandon


1 Answers

Removing the athrow instruction removes an edge in the control-flow graph, so the verifier now must prove the code is safe if execution falls out of the block formerly containing the athrow. In this case, it cannot ensure a local has been initialized to an int for the iinc instruction on line 41.

The simplest solution is to replace the new; dup; invokespecial java/lang/IllegalStateException/<init>()V; athrow sequence with a constant return (in this case, aconst_null; aret, but that will change to match the return type). This enables the verifier to prove control does not exit the block. (Note that this effectively skips finally handlers by not branching to the catch block.)

In comments, the querent explained they're doing control-flow deobfuscation. In general, you're better off building a high-level SSA-style IR with an explicit control-flow graph and transforming that, rather than working at the level of the operand stack and local variables. On such an IR it's pretty easy to say 'I know this branch is never taken, replace it with a jump to the not-taken block`. Such an IR takes some effort to build (through there are some existing analysis frameworks like Soot), but it will pay for itself many times over if you're doing more than one transformation or need to handle many different code patterns (so simple pattern-matching on bytecode sequences is ineffective).

like image 83
Jeffrey Bosboom Avatar answered Oct 29 '22 15:10

Jeffrey Bosboom