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