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