1 /*
   2  * Copyright (c) 2014, 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
  27  * @summary Redefine EMCP and non-EMCP methods that are running in an infinite loop
  28  * @library /testlibrary
  29  * @build RedefineClassHelper
  30  * @run main RedefineClassHelper
  31  * @run main/othervm -javaagent:redefineagent.jar -XX:TraceRedefineClasses=0x600 RedefineRunningMethods
  32  */
  33 public class RedefineRunningMethods {
  34 
  35     public static String newB =
  36                 "class RedefineRunningMethods$B {" +
  37                 "   static int count1 = 0;" +
  38                 "   static int count2 = 0;" +
  39                 "   public static boolean stop = false;" +
  40                 "  static void localSleep() { " +
  41                 "    try{ " +
  42                 "      Thread.currentThread().sleep(10);" +
  43                 "    } catch(InterruptedException ie) { " +
  44                 "    } " +
  45                 " } " +
  46                 "   public static void infinite() { " +
  47                 "       System.out.println(\"infinite called\");" +
  48                 "   }" +
  49                 "   public static void infinite_emcp() { " +
  50                 "       while (!stop) { count2++; localSleep(); }" +
  51                 "   }" +
  52                 "}";
  53 
  54     public static String evenNewerB =
  55                 "class RedefineRunningMethods$B {" +
  56                 "   static int count1 = 0;" +
  57                 "   static int count2 = 0;" +
  58                 "   public static boolean stop = false;" +
  59                 "  static void localSleep() { " +
  60                 "    try{ " +
  61                 "      Thread.currentThread().sleep(1);" +
  62                 "    } catch(InterruptedException ie) { " +
  63                 "    } " +
  64                 " } " +
  65                 "   public static void infinite() { }" +
  66                 "   public static void infinite_emcp() { " +
  67                 "       System.out.println(\"infinite_emcp now obsolete called\");" +
  68                 "   }" +
  69                 "}";
  70 
  71     static class B {
  72         static int count1 = 0;
  73         static int count2 = 0;
  74         public static boolean stop = false;
  75         static void localSleep() {
  76           try{
  77             Thread.currentThread().sleep(10);//sleep for 10 ms
  78           } catch(InterruptedException ie) {
  79           }
  80         }
  81 
  82         public static void infinite() {
  83             while (!stop) { count1++; localSleep(); }
  84         }
  85         public static void infinite_emcp() {
  86             while (!stop) { count2++; localSleep(); }
  87         }
  88     }
  89 
  90 
  91     public static void main(String[] args) throws Exception {
  92 
  93         new Thread() {
  94             public void run() {
  95                 B.infinite();
  96             }
  97         }.start();
  98 
  99         new Thread() {
 100             public void run() {
 101                 B.infinite_emcp();
 102             }
 103         }.start();
 104 
 105         RedefineClassHelper.redefineClass(B.class, newB);
 106 
 107         System.gc();
 108 
 109         B.infinite();
 110 
 111         // Start a thread with the second version of infinite_emcp running
 112         new Thread() {
 113             public void run() {
 114                 B.infinite_emcp();
 115             }
 116         }.start();
 117 
 118         for (int i = 0; i < 20 ; i++) {
 119             String s = new String("some garbage");
 120             System.gc();
 121         }
 122 
 123         RedefineClassHelper.redefineClass(B.class, evenNewerB);
 124         System.gc();
 125 
 126         for (int i = 0; i < 20 ; i++) {
 127             B.infinite();
 128             String s = new String("some garbage");
 129             System.gc();
 130         }
 131 
 132         B.infinite_emcp();
 133 
 134         // purge should clean everything up.
 135         B.stop = true;
 136 
 137         for (int i = 0; i < 20 ; i++) {
 138             B.infinite();
 139             String s = new String("some garbage");
 140             System.gc();
 141         }
 142     }
 143 }