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