Repository: pinpoint-apm/pinpoint
Issue: Javassist bug : Incorrect StackMap Frame generation
Labels: bug

Incorrect StackMapFrame generation

bug regression code

``` java
public class InvalidStackMapFrame {

    public void bytecodeVerifyError() {
        // javassist bug : invalid stack map frame
        List<Integer> test = new ArrayList<Integer>();
        String[] newLine = new String[10];
        for (Integer idx : test) {
            String address = newLine[1];
            int tabPos = -1;
            if (tabPos != -1) {
                address = address.substring(tabPos + 1);
            }
            newLine[4] = address;
        }

    }
}
```

Here is the error message

```
16:40:54 [DEBUG](b.j.JavassistVerifyErrorTest       :49 )  verifyError:Bad type on operand stack in aaload
Exception Details:
  Location:
    bug_regression_jdk7/javassist/InvalidStackMapFrame.bytecodeVerifyError()V @46: aaload
  Reason:
    Type 'java/lang/Object' (current frame, stack[0]) is not assignable to reference type
  Current Frame:
    bci: @46
    flags: { }
    locals: { 'bug_regression_jdk7/javassist/InvalidStackMapFrame', 'java/util/ArrayList', 'java/lang/Object', 'java/util/Iterator', 'java/lang/Integer', top, top, integer }
    stack: { 'java/lang/Object', integer }
  Bytecode:
    0000000: 0436 07bb 0002 59b7 0003 4c10 0abd 0004
    0000010: 4d2b b900 0501 004e 2db9 0006 0100 9900
    0000020: 2f2d b900 0701 00c0 0008 3a04 2c04 323a
    0000030: 0502 3606 1506 029f 000e 1905 1506 0460
    0000040: b600 093a 052c 0719 0553 a7ff ceb1     
  Stackmap Table:
    full_frame(@24,{Object[#40],Object[#2],Object[#11],Object[#48],Top,Top,Top,Integer},{})
    full_frame(@69,{Object[#40],Object[#2],Object[#11],Object[#48],Object[#38],Object[#11],Integer,Integer},{})
    full_frame(@77,{Object[#40],Object[#2],Object[#11],Object[#48],Top,Top,Top,Integer},{})
```

dump stackmap frame

```
dumpVerify:<init>()V
00000 InvalidStackMapFrame  :  :     ALOAD 0
00001 InvalidStackMapFrame  : InvalidStackMapFrame  :     INVOKESPECIAL java/lang/Object.<init> ()V
00002 InvalidStackMapFrame  :  :     RETURN

bytecodeVerifyError()V
00000 InvalidStackMapFrame . . . . . . .  :  :     ICONST_1
00001 InvalidStackMapFrame . . . . . . .  : I  :     ISTORE 7
00002 InvalidStackMapFrame . . . . . . I  :  :     NEW java/util/ArrayList
00003 InvalidStackMapFrame . . . . . . I  : ArrayList  :     DUP
00004 InvalidStackMapFrame . . . . . . I  : ArrayList ArrayList  :     INVOKESPECIAL java/util/ArrayList.<init> ()V
00005 InvalidStackMapFrame . . . . . . I  : ArrayList  :     ASTORE 1
00006 InvalidStackMapFrame ArrayList . . . . . I  :  :     BIPUSH 10
00007 InvalidStackMapFrame ArrayList . . . . . I  : I  :     ANEWARRAY java/lang/String
00008 InvalidStackMapFrame ArrayList . . . . . I  : String  :     ASTORE 2
00009 InvalidStackMapFrame ArrayList String . . . . I  :  :     ALOAD 1
00010 InvalidStackMapFrame ArrayList String . . . . I  : ArrayList  :     INVOKEINTERFACE java/util/List.iterator ()Ljava/util/Iterator;
00011 InvalidStackMapFrame ArrayList String . . . . I  : Iterator  :     ASTORE 3
00012 InvalidStackMapFrame ArrayList String Iterator . . . I  :  :    L0
00013 InvalidStackMapFrame ArrayList String Iterator . . . I  :  :    FRAME FULL 
--> invalid frame   java/lang/Object->[Ljava/lang/String;
--> [bug_regression_jdk7/javassist/InvalidStackMapFrame java/util/ArrayList java/lang/Object->[Ljava/lang/String; java/util/Iterator T T T I] []
[bug_regression_jdk7/javassist/InvalidStackMapFrame java/util/ArrayList java/lang/Object java/util/Iterator T T T I] []
00014 InvalidStackMapFrame ArrayList String Iterator . . . I  :  :     ALOAD 3
00015 InvalidStackMapFrame ArrayList String Iterator . . . I  : Iterator  :     INVOKEINTERFACE java/util/Iterator.hasNext ()Z
00016 InvalidStackMapFrame ArrayList String Iterator . . . I  : I  :     IFEQ L1
00017 InvalidStackMapFrame ArrayList String Iterator . . . I  :  :     ALOAD 3
00018 InvalidStackMapFrame ArrayList String Iterator . . . I  : Iterator  :     INVOKEINTERFACE java/util/Iterator.next ()Ljava/lang/Object;
00019 InvalidStackMapFrame ArrayList String Iterator . . . I  : Object  :     CHECKCAST java/lang/Integer
00020 InvalidStackMapFrame ArrayList String Iterator . . . I  : Integer  :     ASTORE 4
00021 InvalidStackMapFrame ArrayList String Iterator Integer . . I  :  :     ALOAD 2
00022 InvalidStackMapFrame ArrayList String Iterator Integer . . I  : String  :     ICONST_1
00023 InvalidStackMapFrame ArrayList String Iterator Integer . . I  : String I  :     AALOAD
00024 InvalidStackMapFrame ArrayList String Iterator Integer . . I  : String  :     ASTORE 5
00025 InvalidStackMapFrame ArrayList String Iterator Integer String . I  :  :     ICONST_M1
00026 InvalidStackMapFrame ArrayList String Iterator Integer String . I  : I  :     ISTORE 6
00027 InvalidStackMapFrame ArrayList String Iterator Integer String I I  :  :     ILOAD 6
00028 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : I  :     ICONST_M1
00029 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : I I  :     IF_ICMPEQ L2
00030 InvalidStackMapFrame ArrayList String Iterator Integer String I I  :  :     ALOAD 5
00031 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String  :     ILOAD 6
00032 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String I  :     ICONST_1
00033 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String I I  :     IADD
00034 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String I  :     INVOKEVIRTUAL java/lang/String.substring (I)Ljava/lang/String;
00035 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String  :     ASTORE 5
00036 InvalidStackMapFrame ArrayList String Iterator Integer String I I  :  :    L2
--> invalid frame
00037 InvalidStackMapFrame ArrayList String Iterator Integer String I I  :  :    FRAME FULL [bug_regression_jdk7/javassist/InvalidStackMapFrame java/util/ArrayList java/lang/Object java/util/Iterator java/lang/Integer java/lang/Object I I] []
00038 InvalidStackMapFrame ArrayList String Iterator Integer String I I  :  :     ALOAD 2
00039 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String  :     ICONST_4
00040 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String I  :     ALOAD 5
00041 InvalidStackMapFrame ArrayList String Iterator Integer String I I  : String I String  :     AASTORE
00042 InvalidStackMapFrame ArrayList String Iterator Integer String I I  :  :     GOTO L0
00043 InvalidStackMapFrame ArrayList String Iterator . . . I  :  :    L1
--> invalid frame
00044 InvalidStackMapFrame ArrayList String Iterator . . . I  :  :    FRAME FULL [bug_regression_jdk7/javassist/InvalidStackMapFrame java/util/ArrayList java/lang/Object java/util/Iterator T T T I] []
00045 InvalidStackMapFrame ArrayList String Iterator . . . I  :  :     RETURN
```


Issue comments:
fixed: https://github.com/jboss-javassist/javassist/issues/83

There is Javassist 3.21.0-GA version available with fix for this issue.

Thanks. I fixed it.


Pull request review comments:

Patch that resolved the issue:
diff --git a/pom.xml b/pom.xml
index 776bed69e1e7..64159fea9537 100644
--- a/pom.xml
+++ b/pom.xml
@@ -568,7 +568,7 @@
             <dependency>
                 <groupId>org.javassist</groupId>
                 <artifactId>javassist</artifactId>
-                <version>3.20.0-GA</version>
+                <version>3.21.0-GA</version>
             </dependency>
             <dependency>
                 <groupId>cglib</groupId>


Test patch:
diff --git a/profiler-optional/profiler-optional-jdk7/src/test/java/bug_regression_jdk7/javassist/JavassistVerifyErrorTest.java b/profiler-optional/profiler-optional-jdk7/src/test/java/bug_regression_jdk7/javassist/JavassistVerifyErrorTest.java
index 206073ae3c6f..ba5bf79185b3 100644
--- a/profiler-optional/profiler-optional-jdk7/src/test/java/bug_regression_jdk7/javassist/JavassistVerifyErrorTest.java
+++ b/profiler-optional/profiler-optional-jdk7/src/test/java/bug_regression_jdk7/javassist/JavassistVerifyErrorTest.java
@@ -24,6 +24,7 @@
 import javassist.CtMethod;
 import javassist.LoaderClassPath;
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.ClassVisitor;
@@ -48,6 +49,7 @@ public class JavassistVerifyErrorTest {
      * https://github.com/naver/pinpoint/issues/1807
      * @throws Exception
      */
+    @Ignore("fixed Javassist 3.21.0-GA")
     @Test
     public void bug_regression_BytecodeVerifyError_Invalid_StackMapFrame() throws Exception {