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