1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8087315
  27  * @summary Get old method's stack trace elements after GC
  28  * @library /testlibrary
  29  * @modules java.base/jdk.internal.misc
  30  * @modules java.compiler
  31  *          java.instrument
  32  *          jdk.jartool/sun.tools.jar
  33  * @build RedefineClassHelper
  34  * @run main RedefineClassHelper
  35  * @run main/othervm -javaagent:redefineagent.jar RedefineRunningMethodsWithBacktrace
  36  */
  37 
  38 import static jdk.test.lib.Asserts.*;
  39 
  40 public class RedefineRunningMethodsWithBacktrace {
  41 
  42     public static String newB =
  43                 "class RedefineRunningMethodsWithBacktrace$B {" +
  44                 "   static int count1 = 0;" +
  45                 "   static int count2 = 0;" +
  46                 "   public static volatile boolean stop = false;" +
  47                 "  static void localSleep() { " +
  48                 "    try{ " +
  49                 "      Thread.currentThread().sleep(10);" +
  50                 "    } catch(InterruptedException ie) { " +
  51                 "    } " +
  52                 " } " +
  53                 "   public static void infinite() { " +
  54                 "       System.out.println(\"infinite called\");" +
  55                 "   }" +
  56                 "   public static void throwable() { " +
  57                 "       throw new RuntimeException(\"throwable called\");" +
  58                 "   }" +
  59                 "}";
  60 
  61     public static String evenNewerB =
  62                 "class RedefineRunningMethodsWithBacktrace$B {" +
  63                 "   static int count1 = 0;" +
  64                 "   static int count2 = 0;" +
  65                 "   public static volatile boolean stop = false;" +
  66                 "  static void localSleep() { " +
  67                 "    try{ " +
  68                 "      Thread.currentThread().sleep(1);" +
  69                 "    } catch(InterruptedException ie) { " +
  70                 "    } " +
  71                 " } " +
  72                 "   public static void infinite() { }" +
  73                 "   public static void throwable() { " +
  74                 "       throw new RuntimeException(\"throwable called\");" +
  75                 "   }" +
  76                 "}";
  77 
  78     static class B {
  79         static int count1 = 0;
  80         static int count2 = 0;
  81         public static volatile boolean stop = false;
  82         static void localSleep() {
  83             try {
  84                 Thread.currentThread().sleep(10);//sleep for 10 ms
  85             } catch(InterruptedException ie) {
  86             }
  87         }
  88 
  89         public static void infinite() {
  90             while (!stop) { count1++; localSleep(); }
  91         }
  92         public static void throwable() {
  93             // add some stuff to the original constant pool
  94             String s1 = new String ("string1");
  95             String s2 = new String ("string2");
  96             String s3 = new String ("string3");
  97             String s4 = new String ("string4");
  98             String s5 = new String ("string5");
  99             String s6 = new String ("string6");
 100             String s7 = new String ("string7");
 101             String s8 = new String ("string8");
 102             String s9 = new String ("string9");
 103             String s10 = new String ("string10");
 104             String s11 = new String ("string11");
 105             String s12 = new String ("string12");
 106             String s13 = new String ("string13");
 107             String s14 = new String ("string14");
 108             String s15 = new String ("string15");
 109             String s16 = new String ("string16");
 110             String s17 = new String ("string17");
 111             String s18 = new String ("string18");
 112             String s19 = new String ("string19");
 113             throw new RuntimeException("throwable called");
 114         }
 115     }
 116 
 117     private static void touchRedefinedMethodInBacktrace(Throwable throwable) {
 118         System.out.println("touchRedefinedMethodInBacktrace: ");
 119         throwable.printStackTrace();  // this actually crashes with the bug in
 120                                       // java_lang_StackTraceElement::create()
 121 
 122         // Make sure that we can convert the backtrace, which is referring to
 123         // the redefined method, to a  StrackTraceElement[] without crashing.
 124         StackTraceElement[] stackTrace = throwable.getStackTrace();
 125         for (int i = 0; i < stackTrace.length; i++) {
 126             StackTraceElement frame = stackTrace[i];
 127             assertNotNull(frame.getClassName(),
 128               "\nTest failed: trace[" + i + "].getClassName() returned null");
 129             assertNotNull(frame.getMethodName(),
 130               "\nTest failed: trace[" + i + "].getMethodName() returned null");
 131         }
 132     }
 133 
 134     private static Throwable getThrowableInB() {
 135         Throwable t = null;
 136         try {
 137             B.throwable();
 138         } catch (Exception e) {
 139             t = e;
 140             // Don't print here because Throwable will cache the constructed stacktrace
 141             // e.printStackTrace();
 142         }
 143         return t;
 144     }
 145 
 146 
 147     public static void main(String[] args) throws Exception {
 148 
 149         new Thread() {
 150             public void run() {
 151                 B.infinite();
 152             }
 153         }.start();
 154 
 155         Throwable t1 = getThrowableInB();
 156 
 157         RedefineClassHelper.redefineClass(B.class, newB);
 158 
 159         System.gc();
 160 
 161         Throwable t2 = getThrowableInB();
 162 
 163         B.infinite();
 164 
 165         for (int i = 0; i < 20 ; i++) {
 166             String s = new String("some garbage");
 167             System.gc();
 168         }
 169 
 170         RedefineClassHelper.redefineClass(B.class, evenNewerB);
 171         System.gc();
 172 
 173         Throwable t3 = getThrowableInB();
 174 
 175         for (int i = 0; i < 20 ; i++) {
 176             B.infinite();
 177             String s = new String("some garbage");
 178             System.gc();
 179         }
 180 
 181         touchRedefinedMethodInBacktrace(t1);
 182         touchRedefinedMethodInBacktrace(t2);
 183         touchRedefinedMethodInBacktrace(t3);
 184 
 185         // purge should clean everything up.
 186         B.stop = true;
 187 
 188         for (int i = 0; i < 20 ; i++) {
 189             B.infinite();
 190             String s = new String("some garbage");
 191             System.gc();
 192         }
 193     }
 194 }