1 /*
2  * Copyright (c) 2013, 2019, 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 package gc.g1;
25 
26 /*
27  * @test
28  * @key regression gc
29  * @bug 8027756
30  * @requires vm.gc.G1
31  * @library /test/lib
32  * @modules java.base/jdk.internal.misc
33  *          java.management
34  * @build sun.hotspot.WhiteBox
35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
36  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
37  * @summary Humongous objects may have references from the code cache
38  * @run main gc.g1.TestHumongousCodeCacheRoots
39 */
40 
41 import jdk.test.lib.process.OutputAnalyzer;
42 import jdk.test.lib.process.ProcessTools;
43 import sun.hotspot.WhiteBox;
44 
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 
48 class TestHumongousCodeCacheRootsHelper {
49 
50     static final int n = 1000000;
51     static final int[] AA = new int[n];
52     static final int[] BB = new int[n];
53 
main(String args[])54     public static void main(String args[]) throws Exception {
55         // do some work so that the compiler compiles this method, inlining the
56         // reference to the integer array (which is a humonguous object) into
57         // the code cache.
58         for(int i = 0; i < n; i++) {
59             AA[i] = 0;
60             BB[i] = 0;
61         }
62         // trigger a GC that checks that the verification code allows humongous
63         // objects with code cache roots; objects should be all live here.
64         System.gc();
65 
66         // deoptimize everyhing: this should make all compiled code zombies.
67         WhiteBox wb = WhiteBox.getWhiteBox();
68         wb.deoptimizeAll();
69 
70         // trigger a GC that checks that the verification code allows humongous
71         // objects with code cache roots; objects should be all live here.
72         System.gc();
73 
74         // wait a little for the code cache sweeper to try to clean up zombie nmethods
75         // and unregister the code roots.
76         try { Thread.sleep(5000); } catch (InterruptedException ex) { }
77 
78         // do some work on the arrays to make sure that they need to be live after the GCs
79         for(int i = 0; i < n; i++) {
80             AA[i] = 1;
81             BB[i] = 10;
82         }
83 
84         System.out.println();
85     }
86 }
87 
88 public class TestHumongousCodeCacheRoots {
89 
90   /**
91    * Executes a class in a new VM process with the given parameters.
92    * @param vmargs Arguments to the VM to run
93    * @param classname Name of the class to run
94    * @param arguments Arguments to the class
95    * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string
96    * @return The OutputAnalyzer with the results for the invocation.
97    */
runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts)98   public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {
99     ArrayList<String> finalargs = new ArrayList<String>();
100 
101     String[] whiteboxOpts = new String[] {
102       "-Xbootclasspath/a:.",
103       "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
104       "-cp", System.getProperty("java.class.path"),
105     };
106 
107     if (useTestDotJavaDotOpts) {
108       // System.getProperty("test.java.opts") is '' if no options is set,
109       // we need to skip such a result
110       String[] externalVMOpts = new String[0];
111       if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {
112         externalVMOpts = System.getProperty("test.java.opts").split(" ");
113       }
114       finalargs.addAll(Arrays.asList(externalVMOpts));
115     }
116 
117     finalargs.addAll(Arrays.asList(vmargs));
118     finalargs.addAll(Arrays.asList(whiteboxOpts));
119     finalargs.add(classname);
120     finalargs.addAll(Arrays.asList(arguments));
121 
122     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
123     OutputAnalyzer output = new OutputAnalyzer(pb.start());
124     output.shouldHaveExitValue(0);
125     return output;
126   }
127 
main(String[] args)128   public static void main(String[] args) throws Exception {
129     final String[] baseArguments = new String[] {
130       "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1M", "-Xmx100M", // make sure we get a humongous region
131       "-XX:+UnlockDiagnosticVMOptions",
132       "-XX:InitiatingHeapOccupancyPercent=1", // strong code root marking
133       "-XX:+G1VerifyHeapRegionCodeRoots", "-XX:+VerifyAfterGC", // make sure that verification is run
134     };
135 
136     runWhiteBoxTest(baseArguments, TestHumongousCodeCacheRootsHelper.class.getName(),
137       new String[] {}, false);
138   }
139 }
140 
141