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