1 /* 2 * Copyright (c) 2016, 2017, 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 8165246 27 * @summary Test has_previous_versions flag and processing during class unloading. 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 RedefinePreviousVersions test 35 */ 36 37 import jdk.test.lib.process.ProcessTools; 38 import jdk.test.lib.process.OutputAnalyzer; 39 40 public class RedefinePreviousVersions { 41 42 public static String newB = 43 "class RedefinePreviousVersions$B {" + 44 "}"; 45 46 static class B { } 47 48 public static String newRunning = 49 "class RedefinePreviousVersions$Running {" + 50 " public static volatile boolean stop = true;" + 51 " public static volatile boolean running = true;" + 52 " static void localSleep() { }" + 53 " public static void infinite() { }" + 54 "}"; 55 56 static class Running { 57 public static volatile boolean stop = false; 58 public static volatile boolean running = false; 59 static void localSleep() { 60 try{ 61 Thread.sleep(10); // sleep for 10 ms 62 } catch(InterruptedException ie) { 63 } 64 } 65 66 public static void infinite() { 67 running = true; 68 while (!stop) { localSleep(); } 69 } 70 } 71 72 public static void main(String[] args) throws Exception { 73 74 if (args.length > 0) { 75 76 // java -javaagent:redefineagent.jar -Xlog:stuff RedefinePreviousVersions 77 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( "-javaagent:redefineagent.jar", 78 "-Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace", 79 "RedefinePreviousVersions"); 80 new OutputAnalyzer(pb.start()) 81 .shouldContain("Class unloading: has_previous_versions = false") 82 .shouldContain("Class unloading: has_previous_versions = true") 83 .shouldHaveExitValue(0); 84 return; 85 } 86 87 // Redefine a class and create some garbage 88 // Since there are no methods running, the previous version is never added to the 89 // previous_version_list and the flag _has_previous_versions should stay false 90 RedefineClassHelper.redefineClass(B.class, newB); 91 92 for (int i = 0; i < 10 ; i++) { 93 String s = new String("some garbage"); 94 System.gc(); 95 } 96 97 // Start a class that has a method running 98 new Thread() { 99 public void run() { 100 Running.infinite(); 101 } 102 }.start(); 103 104 while (!Running.running) { 105 Thread.sleep(10); // sleep for 10 ms 106 } 107 108 // Since a method of newRunning is running, this class should be added to the previous_version_list 109 // of Running, and _has_previous_versions should return true at class unloading. 110 RedefineClassHelper.redefineClass(Running.class, newRunning); 111 112 for (int i = 0; i < 10 ; i++) { 113 String s = new String("some garbage"); 114 System.gc(); 115 } 116 117 // purge should clean everything up, except Xcomp it might not. 118 Running.stop = true; 119 120 for (int i = 0; i < 10 ; i++) { 121 String s = new String("some garbage"); 122 System.gc(); 123 } 124 } 125 }