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