{"action":"created","issue":{"url":"https://api.github.com/repos/jboss-javassist/javassist/issues/155","repository_url":"https://api.github.com/repos/jboss-javassist/javassist","labels_url":"https://api.github.com/repos/jboss-javassist/javassist/issues/155/labels{/name}","comments_url":"https://api.github.com/repos/jboss-javassist/javassist/issues/155/comments","events_url":"https://api.github.com/repos/jboss-javassist/javassist/issues/155/events","html_url":"https://github.com/jboss-javassist/javassist/issues/155","id":267708831,"number":155,"title":"Javassist Incorrect Code when wrapping with try/catch","user":{"login":"loren138","id":1224505,"avatar_url":"https://avatars1.githubusercontent.com/u/1224505?v=4","gravatar_id":"","url":"https://api.github.com/users/loren138","html_url":"https://github.com/loren138","followers_url":"https://api.github.com/users/loren138/followers","following_url":"https://api.github.com/users/loren138/following{/other_user}","gists_url":"https://api.github.com/users/loren138/gists{/gist_id}","starred_url":"https://api.github.com/users/loren138/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/loren138/subscriptions","organizations_url":"https://api.github.com/users/loren138/orgs","repos_url":"https://api.github.com/users/loren138/repos","events_url":"https://api.github.com/users/loren138/events{/privacy}","received_events_url":"https://api.github.com/users/loren138/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-10-23T15:17:55Z","updated_at":"2017-11-11T06:57:54Z","closed_at":null,"author_association":"NONE","body":"Java Version: 8\r\nJavassist Version: 3.22.0\r\n\r\nAfter running javassist, some of my test cases error out with VerifyErrors.  I'm not a bytecode expert, but it appears the error occures when the compiler omits the assignment in the try block since it isn't used but doesn't insert a pop command after `invokespecial` leaving an extra item on the stack (which is somehow originally ok).  When I wrap with the try/catch in javassist that pop becomes necessary but isn't added and creates an error?\r\n\r\n## Javassist Calls:\r\n```\r\n        CtClass cc;\r\n        try {\r\n            cc = pool.get(testClassToInstrument);\r\n        } catch (NotFoundException e) {\r\n            e.printStackTrace();\r\n            log.println(\"getting cc failed\");\r\n            return;\r\n        }\r\n\r\n        CtMethod[] methods = cc.getMethods();\r\n\r\n        for (CtMethod testMethod : methods) {\r\n            if (testMethod.getName().startsWith(\"test\")) {\r\n                try {\r\n                    testMethod.instrument(\r\n                        new ExprEditor() {\r\n                            public void edit(Handler m)\r\n                                    throws CannotCompileException\r\n                            {\r\n                                m.insertBefore(\"throw $1;\");\r\n                            }\r\n                        }\r\n                    );\r\n                    testMethod.insertBefore(\"semanticrt.statedump.dumpObjectState.setDumpTestCase(\\\"\"+ testClassName + \".\" + testMethod.getName()+\"\\\");\");\r\n                    CtClass etype = null;\r\n                    try {\r\n                        // In test cases we'll just catch Exceptions not throwables\r\n                        etype = pool.get(\"java.lang.Exception\");\r\n                    } catch (NotFoundException e) {\r\n                        // TODO Auto-generated catch block\r\n                        e.printStackTrace();\r\n                    }\r\n                    // TODO .state or .retval?\r\n                    // TODO need to do this to constructors too\r\n                    // The exceptionCode must in with a return or throw statement (ie throw $e;) or in this case\r\n                    // since I want to swallow it return ($r)null.  The ($r) part casts to nothing if the method\r\n                    // is supposed to be void.\r\n                    // See addCatch() https://jboss-javassist.github.io/javassist/tutorial/tutorial2.html\r\n                    String exceptionCode = \"{ semanticrt.statedump.dumpObjectState.dumpExceptionToFile($e, \" +\r\n                            \"\\\"\" + outputFileRoot + \".\"+ testMethod.getName() + \".exception\\\", false); return; }\\n\";\r\n                    testMethod.addCatch(exceptionCode, etype);\r\n                    //testmethod.insertAfter(\"System.out.println($_);\");\r\n                } catch (CannotCompileException e) {\r\n                    System.out.println(testMethod);\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }\r\n        try {\r\n            //\tSystem.out.println(outputTestDir);\r\n            cc.writeFile(outputTestDir);\r\n        } catch (CannotCompileException e) {\r\n            e.printStackTrace();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n```\r\n\r\n## Test 16 Original Source:\r\n```\r\n    @Test\r\n    public void test016() throws Throwable {\r\n        if (debug)\r\n            System.out.format(\"%n%s%n\", \"RegressionTest0.test016\");\r\n        try {\r\n            org.apache.commons.math.linear.OpenMapRealMatrix openMapRealMatrix2 = new org.apache.commons.math.linear.OpenMapRealMatrix((int) (byte) 1, (int) (byte) 0);\r\n            //org.junit.Assert.fail(\"Expected anonymous exception\");\r\n        } catch (java.lang.IllegalArgumentException e) {\r\n            if (!e.getClass().isAnonymousClass()) {\r\n                //org.junit.Assert.fail(\"Expected anonymous exception, got \" + e.getClass().getCanonicalName());\r\n            }\r\n        }\r\n    }\r\n```\r\n\r\nDe-compiled Result Source: (This recompiles into the \"expected\" byte code above.)\r\n```\r\n    @Test\r\n    public void test016() throws Throwable {\r\n        try {\r\n            dumpObjectState.setDumpTestCase(\"RegressionTest0.test016\");\r\n            if(debug) {\r\n                System.out.format(\"%n%s%n\", new Object[]{\"RegressionTest0.test016\"});\r\n            }\r\n\r\n            try {\r\n                new OpenMapRealMatrix(1, 0);\r\n            } catch (IllegalArgumentException var4) {\r\n                throw var4;\r\n            }\r\n        } catch (Exception var5) {\r\n            dumpObjectState.dumpExceptionToFile(var5, \"out-RegressionTest0.test016.exception\", false);\r\n        }\r\n    }\r\n```\r\n\r\n## Error Generated on Run:\r\n```\r\n[stderr] Exception in thread \"main\" java.lang.VerifyError: Inconsistent stackmap frames at branch target 53\r\n[stderr] Exception Details:\r\n[stderr]   Location:\r\n[stderr]     RegressionTest0.test016()V @53: return\r\n[stderr]   Reason:\r\n[stderr]     Type 'org/apache/commons/math/linear/OpenMapRealMatrix' (current frame, locals[1]) is not assignable to 'RegressionTest0' (stack map, locals[1])\r\n[stderr]   Current Frame:\r\n[stderr]     bci: @39\r\n[stderr]     flags: { }\r\n[stderr]     locals: { 'RegressionTest0', 'org/apache/commons/math/linear/OpenMapRealMatrix' }\r\n[stderr]     stack: { }\r\n[stderr]   Stackmap Frame:\r\n[stderr]     bci: @53\r\n[stderr]     flags: { }\r\n[stderr]     locals: { 'RegressionTest0', 'RegressionTest0', 'org/apache/commons/math/linear/OpenMapRealMatrix' }\r\n[stderr]     stack: { }\r\n[stderr]   Bytecode:\r\n[stderr]     0x0000000: 1256 b80a b5b2 0002 9900 15b2 0003 1204\r\n[stderr]     0x0000010: 04bd 0005 5903 1256 53b6 0007 57bb 0057\r\n[stderr]     0x0000020: 5904 03b7 0058 4ca7 000e 0000 0000 0000\r\n[stderr]     0x0000030: 0000 a7ff f8b1 4d2c bf00 a7ff ff4e 2d13\r\n[stderr]     0x0000040: 0ce1 03b8 0abb b1                      \r\n[stderr]   Exception Handler Table:\r\n[stderr]     bci [29, 39] => handler: 54\r\n[stderr]     bci [0, 61] => handler: 61\r\n[stderr]   Stackmap Table:\r\n[stderr]     same_frame(@29)\r\n[stderr]     same_frame(@42)\r\n[stderr]     append_frame(@53,Object[#953],Object[#1128])\r\n[stderr]     full_frame(@54,{Object[#953]},{Object[#955]})\r\n[stderr]     same_frame(@57)\r\n[stderr]     full_frame(@61,{Object[#953]},{Object[#2749]})\r\n[stderr] \r\n[stderr] \tat java.lang.Class.forName0(Native Method)\r\n[stdin] JUnit version 4.12\r\n[stderr] \tat java.lang.Class.forName(Class.java:348)\r\n[stderr] \tat org.junit.internal.Classes.getClass(Classes.java:16)\r\n[stderr] \tat org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)\r\n[stderr] \tat org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)\r\n[stderr] \tat org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)\r\n[stderr] \tat org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)\r\n[stderr] \tat org.junit.runner.JUnitCore.main(JUnitCore.java:36)\r\n```\r\n\r\n## Initial ByteCode:\r\n```\r\n  public void test016() throws java.lang.Throwable;\r\n    Code:\r\n       0: getstatic     #2                  // Field debug:Z\r\n       3: ifeq          24\r\n       6: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;\r\n       9: ldc           #4                  // String %n%s%n\r\n      11: iconst_1\r\n      12: anewarray     #5                  // class java/lang/Object\r\n      15: dup\r\n      16: iconst_0\r\n      17: ldc           #86                 // String RegressionTest0.test016\r\n      19: aastore\r\n      20: invokevirtual #7                  // Method java/io/PrintStream.format:(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;\r\n      23: pop\r\n      24: new           #87                 // class org/apache/commons/math/linear/OpenMapRealMatrix\r\n      27: dup\r\n      28: iconst_1\r\n      29: iconst_0\r\n      30: invokespecial #88                 // Method org/apache/commons/math/linear/OpenMapRealMatrix.\"<init>\":(II)V\r\n      33: astore_1\r\n      34: goto          48\r\n      37: astore_1\r\n      38: aload_1\r\n      39: invokevirtual #16                 // Method java/lang/Object.getClass:()Ljava/lang/Class;\r\n      42: invokevirtual #17                 // Method java/lang/Class.isAnonymousClass:()Z\r\n      45: ifne          48\r\n      48: return\r\n    Exception table:\r\n       from    to  target type\r\n          24    34    37   Class java/lang/IllegalArgumentException\r\n```\r\n\r\n## Actual Result:\r\n```\r\n  public void test016() throws java.lang.Throwable;\r\n    Code:\r\n       0: ldc           #86                 // String RegressionTest0.test016\r\n       2: invokestatic  #2744               // Method semanticrt/statedump/dumpObjectState.setDumpTestCase:(Ljava/lang/String;)V\r\n       5: getstatic     #2                  // Field debug:Z\r\n       8: ifeq          29\r\n      11: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;\r\n      14: ldc           #4                  // String %n%s%n\r\n      16: iconst_1\r\n      17: anewarray     #5                  // class java/lang/Object\r\n      20: dup\r\n      21: iconst_0\r\n      22: ldc           #86                 // String RegressionTest0.test016\r\n      24: aastore\r\n      25: invokevirtual #7                  // Method java/io/PrintStream.format:(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;\r\n      28: pop\r\n      29: new           #87                 // class org/apache/commons/math/linear/OpenMapRealMatrix\r\n      32: dup\r\n      33: iconst_1\r\n      34: iconst_0\r\n      35: invokespecial #88                 // Method org/apache/commons/math/linear/OpenMapRealMatrix.\"<init>\":(II)V\r\n      38: astore_1\r\n      39: goto          53\r\n      42: nop\r\n      43: nop\r\n      44: nop\r\n      45: nop\r\n      46: nop\r\n      47: nop\r\n      48: nop\r\n      49: nop\r\n      50: goto          42\r\n      53: return\r\n      54: astore_2\r\n      55: aload_2\r\n      56: athrow\r\n      57: nop\r\n      58: goto          57\r\n      61: astore_3\r\n      62: aload_3\r\n      63: ldc_w         #3300               // String out-RegressionTest0.test016.exception\r\n      66: iconst_0\r\n      67: invokestatic  #2750               // Method semanticrt/statedump/dumpObjectState.dumpExceptionToFile:(Ljava/lang/Throwable;Ljava/lang/String;Z)V\r\n      70: return\r\n    Exception table:\r\n       from    to  target type\r\n          29    39    54   Class java/lang/IllegalArgumentException\r\n           0    61    61   Class java/lang/Exception\r\n```\r\n\r\n## Expected Byte Code (a result of re-compiling the de-compiled source above):\r\n```\r\n  public void test016() throws java.lang.Throwable;\r\n    Code:\r\n       0: ldc           #103                // String RegressionTest0.test016\r\n       2: invokestatic  #3                  // Method semanticrt/statedump/dumpObjectState.setDumpTestCase:(Ljava/lang/String;)V\r\n       5: getstatic     #4                  // Field debug:Z\r\n       8: ifeq          29\r\n      11: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;\r\n      14: ldc           #6                  // String %n%s%n\r\n      16: iconst_1\r\n      17: anewarray     #7                  // class java/lang/Object\r\n      20: dup\r\n      21: iconst_0\r\n      22: ldc           #103                // String RegressionTest0.test016\r\n      24: aastore\r\n      25: invokevirtual #8                  // Method java/io/PrintStream.format:(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;\r\n      28: pop\r\n      29: new           #104                // class org/apache/commons/math/linear/OpenMapRealMatrix\r\n      32: dup\r\n      33: iconst_1\r\n      34: iconst_0\r\n      35: invokespecial #105                // Method org/apache/commons/math/linear/OpenMapRealMatrix.\"<init>\":(II)V\r\n      38: pop\r\n      39: goto          45\r\n      42: astore_1\r\n      43: aload_1\r\n      44: athrow\r\n      45: goto          56\r\n      48: astore_1\r\n      49: aload_1\r\n      50: ldc           #106                // String out-RegressionTest0.test016.exception\r\n      52: iconst_0\r\n      53: invokestatic  #19                 // Method semanticrt/statedump/dumpObjectState.dumpExceptionToFile:(Ljava/lang/Throwable;Ljava/lang/String;Z)V\r\n      56: return\r\n    Exception table:\r\n       from    to  target type\r\n          29    39    42   Class java/lang/IllegalArgumentException\r\n           0    45    48   Class java/lang/Exception\r\n```"},"comment":{"url":"https://api.github.com/repos/jboss-javassist/javassist/issues/comments/343645584","html_url":"https://github.com/jboss-javassist/javassist/issues/155#issuecomment-343645584","issue_url":"https://api.github.com/repos/jboss-javassist/javassist/issues/155","id":343645584,"user":{"login":"nickl-","id":1404606,"avatar_url":"https://avatars1.githubusercontent.com/u/1404606?v=4","gravatar_id":"","url":"https://api.github.com/users/nickl-","html_url":"https://github.com/nickl-","followers_url":"https://api.github.com/users/nickl-/followers","following_url":"https://api.github.com/users/nickl-/following{/other_user}","gists_url":"https://api.github.com/users/nickl-/gists{/gist_id}","starred_url":"https://api.github.com/users/nickl-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nickl-/subscriptions","organizations_url":"https://api.github.com/users/nickl-/orgs","repos_url":"https://api.github.com/users/nickl-/repos","events_url":"https://api.github.com/users/nickl-/events{/privacy}","received_events_url":"https://api.github.com/users/nickl-/received_events","type":"User","site_admin":false},"created_at":"2017-11-11T06:57:54Z","updated_at":"2017-11-11T06:57:54Z","author_association":"NONE","body":"I can see what you were aiming for but you have made it impossible to review this issue... I have tried!\r\n\r\nWhen I cannot reproduce what you had, I cannot even start to comment on your results. If I can't even comment on your results how am I suppose to be of any assistance? \r\n\r\nIf you can perhaps get a simpler example, without all the external dependencies (org.apache.etc) and less all the distracting comments which seems to have more to do with what you still want to accomplish and nothing to do with this issue (this just makes my life even more difficult having to sift through irrelevant data). Provide just the most simplest and basic routines which produce the problem. Please include the full classes and not just snippets and methods as you have done here. You can also add anything else you can think of which I might need to know, consider, configure, use, or do in order for me to reproduce exactly the results that you got.\r\n\r\nYou provided a lot of information, a lot more than was required, less is more and if it doesn't help to reproduce the fault then please remove it. \r\nUnfortunately you also left out a lot of information, enough so that even by making assumptions to try and fill in the gaps I was still unable to make it work.\r\n\r\nOnly If you can help me reproduce what you had, can I attempt to help you find a resolve.\r\n\r\nAs I already said, I can see what you were aiming for, I can see the `pop` after `invokespecial` but it is impossible for me to make any comment if I do not know exactly how it came to be `astore_1` after `invokespecial` that is the very least you need to do for me, please.\r\n\r\nSome useful links which may help you to help me:\r\n[Bug writing guidelines](https://developer.mozilla.org/en-US/docs/Mozilla/QA/Bug_writing_guidelines)\r\n[How to write a good bug report: step-by-step instructions](https://musescore.org/en/developers-handbook/how-write-good-bug-report-step-step-instructions)\r\n[How to Report a Bug](http://testthewebforward.org/docs/bugs.html)\r\n\r\n**Suggest close issue\r\nReason: Cannot reproduce.**"}}
{"actor":{"display_login":"nickl-"}}