1 /*
   2  * Copyright (c) 2014, 2018, 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 8055008 8197901 8010319
  27  * @summary Redefine EMCP and non-EMCP methods that are running in an infinite loop
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  * @modules java.compiler
  31  *          java.instrument
  32  *          jdk.jartool/sun.tools.jar
  33  * @run main RedefineClassHelper
  34  * @run main/othervm/timeout=180 -javaagent:redefineagent.jar -Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace,class+loader+data=debug,safepoint+cleanup,gc+phases=debug:rt.log RedefineRunningMethods
  35  */
  36 
  37 
  38 // package access top-level class to avoid problem with RedefineClassHelper
  39 // and nested types.
  40 class RedefineRunningMethods_B {
  41     static int count1 = 0;
  42     static int count2 = 0;
  43     public static volatile boolean stop = false;
  44     static void localSleep() {
  45         try{
  46             Thread.currentThread().sleep(10);//sleep for 10 ms
  47         } catch(InterruptedException ie) {
  48         }
  49     }
  50 
  51     public static void infinite() {
  52         while (!stop) { count1++; localSleep(); }
  53     }
  54     public static void infinite_emcp() {
  55         while (!stop) { count2++; localSleep(); }
  56     }
  57 }
  58 
  59 public class RedefineRunningMethods {
  60 
  61     public static String newB =
  62                 "class RedefineRunningMethods_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(10);" +
  69                 "    } catch(InterruptedException ie) { " +
  70                 "    } " +
  71                 " } " +
  72                 "   public static void infinite() { " +
  73                 "       System.out.println(\"infinite called\");" +
  74                 "   }" +
  75                 "   public static void infinite_emcp() { " +
  76                 "       while (!stop) { count2++; localSleep(); }" +
  77                 "   }" +
  78                 "}";
  79 
  80     public static String evenNewerB =
  81                 "class RedefineRunningMethods_B {" +
  82                 "   static int count1 = 0;" +
  83                 "   static int count2 = 0;" +
  84                 "   public static volatile boolean stop = false;" +
  85                 "  static void localSleep() { " +
  86                 "    try{ " +
  87                 "      Thread.currentThread().sleep(1);" +
  88                 "    } catch(InterruptedException ie) { " +
  89                 "    } " +
  90                 " } " +
  91                 "   public static void infinite() { }" +
  92                 "   public static void infinite_emcp() { " +
  93                 "       System.out.println(\"infinite_emcp now obsolete called\");" +
  94                 "   }" +
  95                 "}";
  96 
  97 
  98     public static void main(String[] args) throws Exception {
  99 
 100         new Thread() {
 101             public void run() {
 102                 RedefineRunningMethods_B.infinite();
 103             }
 104         }.start();
 105 
 106         new Thread() {
 107             public void run() {
 108                 RedefineRunningMethods_B.infinite_emcp();
 109             }
 110         }.start();
 111 
 112         RedefineClassHelper.redefineClass(RedefineRunningMethods_B.class, newB);
 113 
 114         System.gc();
 115 
 116         RedefineRunningMethods_B.infinite();
 117 
 118         // Start a thread with the second version of infinite_emcp running
 119         new Thread() {
 120             public void run() {
 121                 RedefineRunningMethods_B.infinite_emcp();
 122             }
 123         }.start();
 124 
 125         for (int i = 0; i < 20 ; i++) {
 126             String s = new String("some garbage");
 127             System.gc();
 128         }
 129 
 130         RedefineClassHelper.redefineClass(RedefineRunningMethods_B.class, evenNewerB);
 131         System.gc();
 132 
 133         for (int i = 0; i < 20 ; i++) {
 134             RedefineRunningMethods_B.infinite();
 135             String s = new String("some garbage");
 136             System.gc();
 137         }
 138 
 139         RedefineRunningMethods_B.infinite_emcp();
 140 
 141         // purge should clean everything up.
 142         RedefineRunningMethods_B.stop = true;
 143 
 144         for (int i = 0; i < 20 ; i++) {
 145             RedefineRunningMethods_B.infinite();
 146             String s = new String("some garbage");
 147             System.gc();
 148         }
 149     }
 150 }